1 /***
2 * @(#) ChartPanel.java Oct 17, 2003
3 *
4 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
5 * Delft, the Netherlands. All rights reserved.
6 *
7 * See for project information <a href="http://www.simulation.tudelft.nl/">
8 * www.simulation.tudelft.nl </a>.
9 *
10 * The source code and binary code of this software is proprietary information
11 * of Delft University of Technology.
12 */
13
14 package org.gscg.common.gui.statistics;
15
16 import java.awt.BorderLayout;
17 import java.awt.Dimension;
18 import java.awt.datatransfer.DataFlavor;
19 import java.awt.datatransfer.Transferable;
20 import java.awt.dnd.DnDConstants;
21 import java.awt.dnd.DropTarget;
22 import java.awt.dnd.DropTargetDragEvent;
23 import java.awt.dnd.DropTargetDropEvent;
24 import java.awt.dnd.DropTargetEvent;
25 import java.awt.dnd.DropTargetListener;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.Map;
29
30 import javax.swing.BorderFactory;
31 import javax.swing.BoxLayout;
32 import javax.swing.JButton;
33 import javax.swing.JPanel;
34
35 import nl.tudelft.simulation.jstats.Swingable;
36
37 import org.gscg.common.gui.ColorInterface;
38 import org.gscg.common.gui.statistics.actions.AddColumnAction;
39 import org.gscg.common.gui.statistics.actions.AddRowAction;
40 import org.gscg.common.gui.statistics.actions.DeleteColumnAction;
41 import org.gscg.common.gui.statistics.actions.DeleteRowAction;
42 import org.gscg.common.gui.statistics.components.StatisticsInterface;
43 import org.jfree.chart.JFreeChart;
44
45 /***
46 * The ChartPanel <br>
47 * <p>
48 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
49 * Delft, the Netherlands. All rights reserved.
50 *
51 * See for project information <a href="http://www.simulation.tudelft.nl/">
52 * www.simulation.tudelft.nl </a>.
53 *
54 * The source code and binary code of this software is proprietary information
55 * of Delft University of Technology.
56 *
57 * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
58 * Jacobs </a>
59 * @version $Revision: 1.1 $ $Date: 2005/06/16 12:34:00 $
60 * @since 1.0.0 <br>
61 */
62 public class ChartPanel extends JPanel implements DropTargetListener
63 {
64 /*** the serial version uid */
65 private static final long serialVersionUID = 11L;
66
67 /*** the contentPanel of this chartPanel */
68 protected TablePanel contentPanel = null;
69
70 /*** a map with the charts to display */
71 protected Map charts = new HashMap();
72
73 /*** a map with all the statistics */
74 protected Map stats = new HashMap();
75
76 /*** the map with selected charts */
77 protected Map selectedCharts = new HashMap();
78
79 /*** the application */
80 protected ColorInterface application = null;
81
82 /***
83 * constructs a new ChartPanel
84 *
85 * @param application the application
86 * @param charts the map with charts to select the chart from
87 * @param stats the map with stats to select a statistic from for listenener
88 * management
89 * @param numRows the initial number of columns for the content panel
90 * @param numColumns the initial number of rows for the content panel
91 *
92 */
93 public ChartPanel(final ColorInterface application, final Map charts,
94 final Map stats, final int numRows, final int numColumns)
95 {
96 super();
97 this.application = application;
98 this.contentPanel = new TablePanel(numRows, numColumns);
99 this.contentPanel.setBackground(this.application
100 .getBusinessPanelBackGroundColor());
101 this.charts = charts;
102 this.stats = stats;
103 this.initialize();
104 this.validate();
105 this.repaint();
106 new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
107 }
108
109 /***
110 * adds a row to the panel
111 */
112 public void addRow()
113 {
114 this.contentPanel.addRow();
115 for (int c = 0; c < this.contentPanel.getColumns(); c++)
116 {
117 JPanel panel = new JPanel();
118 panel.setPreferredSize(this.getMaximumSize());
119 this.contentPanel
120 .setCell(panel, this.contentPanel.getRows() - 1, c);
121 }
122 }
123
124 /***
125 * adds a column
126 */
127 public void addColumn()
128 {
129 this.contentPanel.addColumn();
130 for (int r = 0; r < this.contentPanel.getRows(); r++)
131 {
132 JPanel panel = new JPanel();
133 panel.setPreferredSize(this.getMaximumSize());
134 this.contentPanel.setCell(panel, r,
135 this.contentPanel.getColumns() - 1);
136 }
137 }
138
139 /***
140 * deletes a row
141 */
142 public void deleteRow()
143 {
144 int row = this.contentPanel.getRows();
145 int columns = this.contentPanel.getColumns();
146
147
148 for (int i = 0; i < columns; i++)
149 {
150 String key = "" + (row - 1) + "_" + "" + i;
151 if ((row - 1) > 0 & this.selectedCharts.get(key) != null)
152 {
153 ((StatisticsInterface) this.selectedCharts.get(key))
154 .setStatus(false);
155 }
156 }
157 this.contentPanel.deleteRow();
158 }
159
160 /***
161 * deletes a column
162 */
163 public void deleteColumn()
164 {
165 int rows = this.contentPanel.getRows();
166 int columns = this.contentPanel.getColumns();
167
168
169 for (int i = 0; i < rows; i++)
170 {
171 String key = "" + i + "_" + "" + (columns - 1);
172 if ((columns - 1) > 0 & this.selectedCharts.get(key) != null)
173 {
174 ((StatisticsInterface) this.selectedCharts.get(key))
175 .setStatus(false);
176 }
177 }
178 this.contentPanel.deleteColumn();
179 }
180
181 /***
182 * @see java.awt.dnd.DropTargetListener
183 * #drop(java.awt.dnd.DropTargetDropEvent)
184 */
185 public void drop(final DropTargetDropEvent dropEvent)
186 {
187 try
188 {
189 Transferable tr = dropEvent.getTransferable();
190 if (dropEvent.isDataFlavorSupported(DataFlavor.stringFlavor))
191 {
192 dropEvent.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
193 dropEvent.dropComplete(true);
194
195 String key = (String) tr
196 .getTransferData(DataFlavor.stringFlavor);
197 try
198 {
199 this.contentPanel.setCell(
200 ((Swingable) this.charts.get(key)).getSwingPanel(),
201 this.contentPanel.getRow(dropEvent.getLocation()),
202 this.contentPanel
203 .getColumn(dropEvent.getLocation()));
204 } catch (ClassCastException classCastException)
205 {
206 org.jfree.chart.ChartPanel result = new org.jfree.chart.ChartPanel(
207 (JFreeChart) this.charts.get(key));
208 result.setMouseZoomable(true, false);
209
210
211 result.autoRangeBoth();
212 result.setPreferredSize(new Dimension(800, 600));
213
214 this.contentPanel.setCell(result, this.contentPanel
215 .getRow(dropEvent.getLocation()), this.contentPanel
216 .getColumn(dropEvent.getLocation()));
217 }
218
219
220
221
222 int row = this.contentPanel.getRow(dropEvent.getLocation());
223 int column = this.contentPanel.getColumn(dropEvent
224 .getLocation());
225
226 String selectedKey = "" + row + "_" + "" + column;
227 if (this.selectedCharts.get(selectedKey) != null)
228 {
229 ((StatisticsInterface) this.selectedCharts.get(selectedKey))
230 .setStatus(false);
231 }
232
233
234 ((StatisticsInterface) this.stats.get(key)).setStatus(true);
235
236
237 this.selectedCharts.put(selectedKey, this.stats.get(key));
238
239 this.doLayout();
240 this.validate();
241 this.repaint();
242
243 } else
244 {
245 dropEvent.rejectDrop();
246 }
247 } catch (Exception exception)
248 {
249 exception = null;
250
251
252 }
253 }
254
255 /***
256 * @see java.awt.dnd.DropTargetListener
257 * #dragEnter(java.awt.dnd.DropTargetDragEvent)
258 */
259 public void dragEnter(final DropTargetDragEvent e)
260 {
261
262 }
263
264 /***
265 * @see java.awt.dnd.DropTargetListener
266 * #dragExit(java.awt.dnd.DropTargetEvent)
267 */
268 public void dragExit(final DropTargetEvent e)
269 {
270
271 }
272
273 /***
274 * @see java.awt.dnd.DropTargetListener
275 * #dragOver(java.awt.dnd.DropTargetDragEvent)
276 */
277 public void dragOver(final DropTargetDragEvent e)
278 {
279
280 }
281
282 /***
283 * @see java.awt.dnd.DropTargetListener
284 * #dropActionChanged(java.awt.dnd.DropTargetDragEvent)
285 */
286 public void dropActionChanged(final DropTargetDragEvent e)
287 {
288
289 }
290
291 /***
292 * initializes the chartPanel
293 */
294 private void initialize()
295 {
296 this.setBorder(BorderFactory.createEmptyBorder());
297 this.setLayout(new BorderLayout());
298
299 JPanel buttons = new JPanel();
300 buttons.setBorder(BorderFactory.createEmptyBorder());
301 buttons.setLayout((new BoxLayout(buttons, BoxLayout.X_AXIS)));
302
303 JButton addRowButton = new JButton(new AddRowAction(this));
304 buttons.add(addRowButton);
305
306 JButton deleteRowButton = new JButton(new DeleteRowAction(this));
307 buttons.add(deleteRowButton);
308
309 JButton addColumnButton = new JButton(new AddColumnAction(this));
310 buttons.add(addColumnButton);
311
312 JButton deleteColumnButton = new JButton(new DeleteColumnAction(this));
313 buttons.add(deleteColumnButton);
314
315 TablePanel buttonHolder = new TablePanel(1, 1);
316 buttonHolder.setBorder(BorderFactory.createEmptyBorder());
317
318 buttonHolder.setCell(buttons, 0, 0);
319 this.add(buttonHolder, BorderLayout.SOUTH);
320 this.add(this.contentPanel, BorderLayout.CENTER);
321
322 for (int r = 0; r < this.contentPanel.getRows(); r++)
323 {
324 for (int c = 0; c < this.contentPanel.getColumns(); c++)
325 {
326 JPanel panel = new JPanel();
327 panel.setPreferredSize(this.getMaximumSize());
328 this.contentPanel.setCell(panel, r, c);
329 }
330 }
331 }
332
333 /***
334 * Method removeAllSelectedCharts removes all the listeners of the selected
335 * charts for server-side events.
336 */
337 public void removeAllSelectedCharts()
338 {
339 Iterator it = this.selectedCharts.keySet().iterator();
340 while (it.hasNext())
341 {
342 ((StatisticsInterface) this.selectedCharts.get(it.next()))
343 .setStatus(false);
344 }
345
346
347 this.selectedCharts.clear();
348 }
349 }