forked from farrellf/Telemetry-Viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChartsRegion.java
221 lines (169 loc) · 6.5 KB
/
ChartsRegion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
* Manages the grid region and all charts on the screen.
*
* Users can click-and-drag in this region to create new charts.
* When this panel is resized, all contained charts will be repositioned and resized accordingly.
*/
@SuppressWarnings("serial")
public class ChartsRegion extends JPanel {
// grid size
int columnCount;
int rowCount;
// grid locations for the opposite corners of where a new chart will be placed
int startX;
int startY;
int endX;
int endY;
/**
* Create a new ChartsRegion.
*/
public ChartsRegion() {
super();
columnCount = Controller.getGridColumns();
rowCount = Controller.getGridRows();
startX = -1;
startY = -1;
endX = -1;
endY = -1;
setLayout(null); // absolute layout
setMinimumSize(new Dimension(200, 200));
// update the column and row counts when they change
Controller.addGridChangedListener(new GridChangedListener() {
@Override public void gridChanged(int columns, int rows) {
columnCount = columns;
rowCount = rows;
getComponentListeners()[0].componentResized(null);
}
});
// add and remove charts as needed
Controller.addChartsListener(new ChartListener() {
@Override public void chartRemoved(PositionedChart chart) {
remove(chart);
revalidate();
repaint();
}
@Override public void chartAdded(PositionedChart chart) {
add(chart);
revalidate();
repaint();
}
});
// listen for mouse presses and releases (the user clicking and dragging a region to place a new chart)
addMouseListener(new MouseListener() {
// the mouse was pressed, attempting to start a new chart region
@Override public void mousePressed(MouseEvent me) {
int proposedStartX = me.getX() * columnCount / getWidth();
int proposedStartY = me.getY() * rowCount / getHeight();
if(proposedStartX < columnCount && proposedStartY < rowCount && Controller.gridRegionAvailable(proposedStartX, proposedStartY, proposedStartX, proposedStartY)) {
startX = endX = proposedStartX;
startY = endY = proposedStartY;
repaint();
}
}
// the mouse was released, attempting to create a new chart
@Override public void mouseReleased(MouseEvent me) {
if(endX == -1 || endY == -1)
return;
int proposedEndX = me.getX() * columnCount / getWidth();
int proposedEndY = me.getY() * rowCount / getHeight();
if(proposedEndX < columnCount && proposedEndY < rowCount && Controller.gridRegionAvailable(startX, startY, proposedEndX, proposedEndY)) {
endX = proposedEndX;
endY = proposedEndY;
}
JFrame parentWindow = (JFrame) SwingUtilities.windowForComponent(ChartsRegion.this);
new NewChartWindow(parentWindow, startX, startY, endX, endY);
startX = startY = -1;
endX = endY = -1;
repaint();
}
@Override public void mouseExited(MouseEvent me) { }
@Override public void mouseEntered(MouseEvent me) { }
@Override public void mouseClicked(MouseEvent me) { }
});
// listen for mouse drags (the user clicking and dragging a region for a new chart)
addMouseMotionListener(new MouseMotionListener() {
@Override public void mouseDragged(MouseEvent me) {
if(endX == -1 || endY == -1)
return;
int proposedEndX = me.getX() * columnCount / getWidth();
int proposedEndY = me.getY() * rowCount / getHeight();
if(proposedEndX < columnCount && proposedEndY < rowCount && Controller.gridRegionAvailable(startX, startY, proposedEndX, proposedEndY)) {
endX = proposedEndX;
endY = proposedEndY;
repaint();
}
}
@Override public void mouseMoved(MouseEvent me) { }
});
// listen for a change in size
// this is required because paintComponent() will not be automatically called if the window is un-maximized and an existing chart would obscure the rest of the ChartsRegion
addComponentListener(new ComponentListener() {
@Override public void componentResized(ComponentEvent ce) {
int width = getWidth();
int height = getHeight();
int columnWidth = width / columnCount;
int rowHeight = height / rowCount;
Controller.repositionCharts(columnWidth, rowHeight);
revalidate();
repaint();
}
@Override public void componentShown(ComponentEvent ce) { }
@Override public void componentMoved(ComponentEvent ce) { }
@Override public void componentHidden(ComponentEvent ce) { }
});
}
/**
* Redraws the ChartsRegion based on the current width/height, and the number of columns/rows.
*
* @param g The graphics object.
*/
@Override public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
int width = getWidth();
int height = getHeight();
int columnWidth = width / columnCount;
int rowHeight = height / rowCount;
// resize and reposition all charts
Controller.repositionCharts(columnWidth, rowHeight);
revalidate();
// draw a neutral background
g2.setColor(getBackground());
g2.fillRect(0, 0, width, height);
width = columnWidth * columnCount;
height = rowHeight * rowCount;
// draw a white background for the grid
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, width, height);
// set the stroke
float factor = Controller.getDisplayScalingFactor();
g2.setStroke(new BasicStroke(1.0f * factor, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, new float[] {5.0f * factor}, 0.0f));
g2.setColor(Color.BLACK);
// draw vertical lines
for(int i = 1; i < columnCount; i++)
g2.drawLine(columnWidth * i, 0, columnWidth * i, height);
// draw horizontal lines
for(int i = 1; i < rowCount; i++)
g2.drawLine(0, rowHeight * i, width, rowHeight * i);
// draw active bounding box where the user is clicking-and-dragging to place a new chart
g2.setColor(Color.GRAY);
int x = startX < endX ? startX * columnWidth : endX * columnWidth;
int y = startY < endY ? startY * rowHeight : endY * rowHeight;
int boxWidth = (Math.abs(endX - startX) + 1) * columnWidth;
int boxHeight = (Math.abs(endY - startY) + 1) * rowHeight;
g2.fillRect(x, y, boxWidth, boxHeight);
}
}