Skip to content

Commit edd14b0

Browse files
committed
refactoring: shift responsibility for display from GCDocument to
ChartPanelView
1 parent fb5ca53 commit edd14b0

File tree

2 files changed

+107
-69
lines changed

2 files changed

+107
-69
lines changed

src/main/java/com/tagtraum/perf/gcviewer/ChartPanelView.java

+85-7
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,31 @@
3030
import javax.swing.JTextArea;
3131
import javax.swing.SwingConstants;
3232
import javax.swing.SwingUtilities;
33+
import javax.swing.event.ChangeListener;
3334
import javax.swing.event.SwingPropertyChangeSupport;
3435

3536
import com.tagtraum.perf.gcviewer.log.TextAreaLogHandler;
3637

37-
3838
/**
3939
*
4040
* @author <a href="mailto:[email protected]">Hendrik Schreiber</a>
4141
* <p>Date: May 5, 2005<br/>
4242
* Time: 2:14:36 PM</p>
4343
*
4444
*/
45-
public class ChartPanelView {
45+
public class ChartPanelView extends JPanel {
4646

47+
public static final String EVENT_MINIMIZED = "minimized";
48+
49+
private static final long serialVersionUID = -4259423581681643286L;
4750
private static final ResourceBundle localStrings = ResourceBundle.getBundle("com.tagtraum.perf.gcviewer.localStrings");
4851
private static final DataReaderFactory factory = new DataReaderFactory();
4952

5053
private GCPreferences preferences;
51-
5254
private ModelChartImpl modelChart;
5355
private ModelPanel modelPanel;
5456
private GCModel model;
5557
private ViewBar viewBar;
56-
private boolean viewBarVisible;
5758
private boolean minimized;
5859
private SwingPropertyChangeSupport propertyChangeSupport;
5960
private GCDocument gcDocument;
@@ -70,12 +71,59 @@ public ChartPanelView(GCDocument gcDocument, URL url) throws IOException {
7071
this.viewBar = new ViewBar(this);
7172
this.propertyChangeSupport = new SwingPropertyChangeSupport(this);
7273
this.textAreaLogHandler = new TextAreaLogHandler();
74+
75+
addComponents();
76+
7377
final GCModel model = loadModel(url);
7478
setModel(model);
7579
// TODO delete
7680
model.printDetailedInformation();
7781
}
7882

83+
private void addComponents() {
84+
GridBagLayout layout = new GridBagLayout();
85+
setLayout(layout);
86+
87+
int row = 0;
88+
GridBagConstraints constraints = new GridBagConstraints();
89+
constraints.fill = GridBagConstraints.HORIZONTAL;
90+
constraints.anchor = GridBagConstraints.NORTH;
91+
92+
// show viewbar if needed (GCDocument knows, when this is necessary
93+
// -> more than one chartPanel is displayed in the same document)
94+
constraints.gridy = row;
95+
//if (isViewBarVisible()) {
96+
viewBar.setVisible(false);
97+
constraints.gridwidth = 2;
98+
constraints.weightx = 2;
99+
add(getViewBar(), constraints);
100+
row++;
101+
//}
102+
103+
// show modelchart if not minimised
104+
constraints.fill = GridBagConstraints.BOTH;
105+
constraints.gridy = row;
106+
constraints.gridwidth = 1;
107+
constraints.gridheight = 1;
108+
constraints.gridx = 0;
109+
constraints.weightx = 2;
110+
constraints.weighty = 2;
111+
modelChart.setPreferredSize(new Dimension(800, 1024));
112+
modelChart.setVisible(!isMinimized());
113+
add(modelChart, constraints);
114+
115+
// show modelPanel if not minimised
116+
constraints.gridy = row;
117+
constraints.gridheight = 1;
118+
constraints.gridx = 1;
119+
constraints.weightx = 0;
120+
constraints.weighty = 0;
121+
constraints.fill = GridBagConstraints.HORIZONTAL;
122+
constraints.anchor = GridBagConstraints.SOUTH;
123+
add(modelPanel, constraints);
124+
modelPanel.setVisible(preferences.isShowDataPanel());
125+
}
126+
79127
/**
80128
* @return true, if the files has been reloaded
81129
* @throws IOException
@@ -124,13 +172,33 @@ public void run() {
124172
}
125173

126174
public void invalidate() {
175+
super.invalidate();
176+
127177
modelChart.invalidate();
128178
modelPanel.invalidate();
129179
}
130180

131181
public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
132182
propertyChangeSupport.addPropertyChangeListener(propertyChangeListener);
133183
}
184+
185+
public void addChartScrollBarChangeListener(ChangeListener changeListener) {
186+
modelChart.getViewport().addChangeListener(changeListener);
187+
}
188+
189+
public void removeChartScrollBarChangeListeners() {
190+
for (ChangeListener listener : modelChart.getViewport().getChangeListeners()) {
191+
if (listener instanceof ChartPanelView) {
192+
modelChart.getViewport().removeChangeListener(listener);
193+
}
194+
}
195+
}
196+
197+
public void setScrollBarVisible(boolean scrollBarVisible) {
198+
modelChart.setHorizontalScrollBarPolicy(scrollBarVisible
199+
? JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
200+
: JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
201+
}
134202

135203
public ViewBar getViewBar() {
136204
return viewBar;
@@ -141,11 +209,11 @@ public JTextArea getParseLog() {
141209
}
142210

143211
public boolean isViewBarVisible() {
144-
return viewBarVisible;
212+
return viewBar.isVisible();
145213
}
146214

147215
public void setViewBarVisible(boolean viewBarVisible) {
148-
this.viewBarVisible = viewBarVisible;
216+
this.viewBar.setVisible(viewBarVisible);
149217
}
150218

151219
public boolean isMinimized() {
@@ -156,7 +224,11 @@ public void setMinimized(boolean minimized) {
156224
boolean oldValue = this.minimized;
157225
if (minimized != this.minimized) {
158226
this.minimized = minimized;
159-
propertyChangeSupport.firePropertyChange("minimized", oldValue, minimized);
227+
228+
modelChart.setVisible(!minimized);
229+
modelPanel.setVisible(preferences.isShowDataPanel() && !minimized);
230+
231+
propertyChangeSupport.firePropertyChange(EVENT_MINIMIZED, oldValue, minimized);
160232
}
161233
}
162234

@@ -175,6 +247,7 @@ public GCModel getModel() {
175247
public void setModel(GCModel model) {
176248
this.model = model;
177249
this.modelPanel.setModel(model);
250+
this.modelPanel.setVisible(preferences.isShowDataPanel());
178251
this.modelChart.setModel(model, preferences);
179252
this.viewBar.setTitle(model.getURL().toString());
180253
}
@@ -184,6 +257,7 @@ public void close() {
184257
}
185258

186259
private static class ViewBar extends JPanel {
260+
private static final long serialVersionUID = -5219100256784644319L;
187261
private JLabel title = new JLabel();
188262
private ViewBarButton closeButton = new ViewBarButton("images/close.png", "images/close_selected.png");
189263
private MinMaxButton minimizeButton = new MinMaxButton();
@@ -237,6 +311,8 @@ protected void paintComponent(Graphics graphics) {
237311
}
238312

239313
private static class ViewBarButton extends JButton {
314+
private static final long serialVersionUID = 587920510707520092L;
315+
240316
public ViewBarButton(String image1, String image2) {
241317
final ImageIcon imageIcon1 = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource(image1)));
242318
final ImageIcon imageIcon2 = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource(image2)));
@@ -254,7 +330,9 @@ public void setIcons(final ImageIcon imageIcon1, final ImageIcon imageIcon2) {
254330
}
255331

256332
}
333+
257334
private static class MinMaxButton extends JButton {
335+
private static final long serialVersionUID = -6518226257396629789L;
258336
private final ImageIcon min1 = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/minimize.png")));
259337
private final ImageIcon min2 = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/minimize_selected.png")));
260338
private final ImageIcon max1 = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/maximize.png")));

src/main/java/com/tagtraum/perf/gcviewer/GCDocument.java

+22-62
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.awt.Dimension;
44
import java.awt.GridBagConstraints;
5-
import java.awt.GridBagLayout;
65
import java.awt.Point;
76
import java.awt.datatransfer.DataFlavor;
87
import java.awt.datatransfer.Transferable;
@@ -22,6 +21,7 @@
2221
import java.util.List;
2322

2423
import javax.swing.BoundedRangeModel;
24+
import javax.swing.BoxLayout;
2525
import javax.swing.DefaultBoundedRangeModel;
2626
import javax.swing.JInternalFrame;
2727
import javax.swing.JPanel;
@@ -51,8 +51,9 @@ public GCDocument(final GCViewer gcViewer, String s) {
5151
this.refreshWatchDog = new RefreshWatchDog();
5252
refreshWatchDog.setGcDocument(this);
5353
preferences = gcViewer.getPreferences();
54+
showModelPanel = preferences.isShowDataPanel();
5455
modelChartListFacade = new MultiModelChartFacade();
55-
GridBagLayout layout = new GridBagLayout();
56+
BoxLayout layout = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
5657
getContentPane().setLayout(layout);
5758
// TODO refactor; looks very similar to DesktopPane implementation
5859
getContentPane().setDropTarget(new DropTarget(this, DnDConstants.ACTION_COPY, new DropTargetListener(){
@@ -142,7 +143,7 @@ public void add(final URL url) throws IOException {
142143
chartPanelViews.add(chartPanelView);
143144
chartPanelView.addPropertyChangeListener(new PropertyChangeListener() {
144145
public void propertyChange(PropertyChangeEvent event) {
145-
if ("minimized".equals(event.getPropertyName())) relayout();
146+
if (ChartPanelView.EVENT_MINIMIZED.equals(event.getPropertyName())) relayout();
146147
}
147148
});
148149
// make sure all models in one document have the same display properties
@@ -176,65 +177,28 @@ public void relayout() {
176177
setTitle(((ChartPanelView) chartPanelViews.get(0)).getModel().getURL().toString());
177178
else
178179
setTitle("");
179-
int row = 0;
180+
180181
boolean noMaximizedChartPanelView = true;
181182
ChartPanelView lastMaximizedChartPanelView = getLastMaximizedChartPanelView();
182183
MasterViewPortChangeListener masterViewPortChangeListener = new MasterViewPortChangeListener();
183184

184185
for (int i = 0; i < chartPanelViews.size(); i++) {
185186
final ChartPanelView chartPanelView = (ChartPanelView) chartPanelViews.get(i);
186187
final ModelChartImpl modelChart = (ModelChartImpl) chartPanelView.getModelChart();
187-
final ModelPanel modelPanel = chartPanelView.getModelPanel();
188-
//final JComponent parseLog = new JScrollPane(chartPanelView.getParseLog());
189-
modelChart.invalidate();
190-
GridBagConstraints constraints = new GridBagConstraints();
191-
constraints.fill = GridBagConstraints.HORIZONTAL;
192-
constraints.anchor = GridBagConstraints.NORTH;
193-
194-
constraints.gridy = row;
195-
if (chartPanelViews.size() > 1 || (chartPanelView.isMinimized() && chartPanelViews.size() == 1)) {
196-
constraints.gridwidth = 2;
197-
constraints.weightx = 2;
198-
//constraints.weighty = 1;
199-
getContentPane().add(chartPanelView.getViewBar(), constraints);
200-
row++;
201-
}
202-
constraints.fill = GridBagConstraints.BOTH;
203-
constraints.gridy = row;
204-
constraints.gridwidth = 1;
205-
constraints.gridheight = 1; //2
206-
constraints.gridx = 0;
207-
constraints.weightx = 2;
208-
constraints.weighty = 2;
209-
modelChart.setPreferredSize(new Dimension(800, 600));
210-
modelChart.setVisible(!chartPanelView.isMinimized());
211-
getContentPane().add(modelChart, constraints);
212-
213-
/*
214-
constraints.gridheight = 1;
215-
constraints.gridy = row;
216-
constraints.gridx = 1;
217-
constraints.weightx = 0;
218-
constraints.weighty = 4;
219-
constraints.fill = GridBagConstraints.BOTH;
220-
constraints.anchor = GridBagConstraints.NORTH;
221-
getContentPane().add(parseLog, constraints);
222-
parseLog.setVisible(!chartPanelView.isMinimized());
223-
224-
row++;
225-
*/
226-
227-
constraints.gridy = row;
228-
constraints.gridheight = 1;
229-
constraints.gridx = 1;
230-
constraints.weightx = 0;
231-
constraints.weighty = 0;
232-
constraints.fill = GridBagConstraints.HORIZONTAL;
233-
constraints.anchor = GridBagConstraints.SOUTH;
234-
getContentPane().add(modelPanel, constraints);
235-
modelPanel.setVisible(showModelPanel && (!chartPanelView.isMinimized()));
236-
188+
189+
//chartPanelView.invalidate();
190+
getContentPane().add(chartPanelView);
191+
192+
chartPanelView.setViewBarVisible(
193+
chartPanelViews.size() > 1
194+
|| (chartPanelView.isMinimized() && chartPanelViews.size() == 1));
195+
237196
if (!chartPanelView.isMinimized()) {
197+
// if more than one chart is displayed, only the last visible has a scrollbar
198+
// and all charts must scroll synchronously
199+
// -> event.source for scrolling:
200+
// - user moves scrollbar
201+
// - model of last visible chart has changed and is watched
238202
noMaximizedChartPanelView = false;
239203
final boolean isLastMaximizedChartPanelView = lastMaximizedChartPanelView == chartPanelView;
240204
// lock viewports with each other...
@@ -257,16 +221,12 @@ public void relayout() {
257221
horizontalScrollBar.setEnabled(!isWatched());
258222
}
259223
}
260-
row++;
261224
}
262225
if (noMaximizedChartPanelView) {
263-
// add dummy panel
264-
GridBagConstraints constraints = new GridBagConstraints();
265-
constraints.fill = GridBagConstraints.BOTH;
266-
constraints.gridy = row;
267-
constraints.weightx = 3;
268-
constraints.weighty = 3;
269-
getContentPane().add(new JPanel(), constraints);
226+
// fill empty space below minimized chartPanelView
227+
JPanel dummyPanel = new JPanel();
228+
dummyPanel.setPreferredSize(new Dimension(100, 3000));
229+
getContentPane().add(dummyPanel);
270230
}
271231
scaleModelChart();
272232
revalidate();

0 commit comments

Comments
 (0)