View Javadoc

    1   /***
    2    * @(#) ChartPanel.java Oct 17, 2003
    3    *
    4    * Copyright (c) 2002-2005 Delft University of Technology Jaffalaan 5, 2628 BX
    5    * Delft, the Netherlands. All rights reserved.
    6    *
    7    * This software is proprietary information of Delft University of Technology
    8    * The code is published under the Lesser General Public License
    9    */
    10  package nl.tudelft.simulation.dsol.gui.statistics;
    11
    12  import java.awt.BorderLayout;
    13  import java.awt.Color;
    14  import java.awt.datatransfer.DataFlavor;
    15  import java.awt.datatransfer.Transferable;
    16  import java.awt.dnd.DnDConstants;
    17  import java.awt.dnd.DropTarget;
    18  import java.awt.dnd.DropTargetDragEvent;
    19  import java.awt.dnd.DropTargetDropEvent;
    20  import java.awt.dnd.DropTargetEvent;
    21  import java.awt.dnd.DropTargetListener;
    22
    23  import javax.swing.BorderFactory;
    24  import javax.swing.BoxLayout;
    25  import javax.swing.JButton;
    26  import javax.swing.JPanel;
    27
    28  import nl.tudelft.simulation.dsol.gui.panels.TablePanel;
    29  import nl.tudelft.simulation.dsol.gui.statistics.actions.AddColumnAction;
    30  import nl.tudelft.simulation.dsol.gui.statistics.actions.AddRowAction;
    31  import nl.tudelft.simulation.dsol.gui.statistics.actions.DeleteColumnAction;
    32  import nl.tudelft.simulation.dsol.gui.statistics.actions.DeleteRowAction;
    33  import nl.tudelft.simulation.jstats.Swingable;
    34  import nl.tudelft.simulation.logger.Logger;
    35  import nl.tudelft.simulation.naming.InitialEventContext;
    36
    37  /***
    38   * The ChartPanel <br>
    39   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
    40   * University of Technology </a>, the Netherlands. <br>
    41   * See for project information <a
    42   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
    43   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
    44   * General Public License (LGPL) </a>, no warranty.
    45   *
    46   * @version $Revision$ $Date$
    47   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja">Peter Jacobs </a>
    48   */
    49  public class ChartPanel extends JPanel implements DropTargetListener
    50  {
    51  	/*** the contentPanel of this chartPanel */
    52  	private TablePanel contentPanel = new TablePanel(2, 2);
    53
    54  	/***
    55  	 * constructs a new ChartPanel
    56  	 */
    57  	public ChartPanel()
    58  	{
    59  		super();
    60  		this.initialize();
    61  		this.validate();
    62  		this.repaint();
    63  		new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
    64  	}
    65
    66  	/***
    67  	 * adds a row to the panel
    68  	 */
    69  	public void addRow()
    70  	{
    71  		this.contentPanel.addRow();
    72  		for (int c = 0; c < this.contentPanel.getColumns(); c++)
    73  		{
    74  			JPanel panel = new JPanel();
    75  			panel.setPreferredSize(this.getMaximumSize());
    76  			panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    77  			this.contentPanel
    78  					.setCell(panel, this.contentPanel.getRows() - 1, c);
    79  		}
    80  	}
    81
    82  	/***
    83  	 * adds a column
    84  	 */
    85  	public void addColumn()
    86  	{
    87  		this.contentPanel.addColumn();
    88  		for (int r = 0; r < this.contentPanel.getRows(); r++)
    89  		{
    90  			JPanel panel = new JPanel();
    91  			panel.setPreferredSize(this.getMaximumSize());
    92  			panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    93  			this.contentPanel.setCell(panel, r,
    94  					this.contentPanel.getColumns() - 1);
    95  		}
    96  	}
    97
    98  	/***
    99  	 * deletes a row
    100 	 */
    101 	public void deleteRow()
    102 	{
    103 		this.contentPanel.deleteRow();
    104 	}
    105
    106 	/***
    107 	 * deletes a row
    108 	 */
    109 	public void deleteColumn()
    110 	{
    111 		this.contentPanel.deleteColumn();
    112 	}
    113
    114 	/***
    115 	 * @see java.awt.dnd.DropTargetListener
    116 	 *      #drop(java.awt.dnd.DropTargetDropEvent)
    117 	 */
    118 	public void drop(final DropTargetDropEvent dropEvent)
    119 	{
    120 		try
    121 		{
    122 			Transferable tr = dropEvent.getTransferable();
    123 			if (dropEvent.isDataFlavorSupported(DataFlavor.stringFlavor))
    124 			{
    125 				dropEvent.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
    126 				dropEvent.dropComplete(true);
    127 				String key = (String) tr
    128 						.getTransferData(DataFlavor.stringFlavor);
    129 				Swingable chart = (Swingable) new InitialEventContext()
    130 						.lookup(key);
    131 				this.contentPanel.setCell(chart.getSwingPanel(),
    132 						this.contentPanel.getRow(dropEvent.getLocation()),
    133 						this.contentPanel.getColumn(dropEvent.getLocation()));
    134 				this.validate();
    135 				this.repaint();
    136
    137 			} else
    138 			{
    139 				dropEvent.rejectDrop();
    140 			}
    141 		} catch (Exception ufe)
    142 		{
    143 			Logger.warning(this, "drop", ufe);
    144 		}
    145 	}
    146
    147 	/***
    148 	 * @see java.awt.dnd.DropTargetListener
    149 	 *      #dragEnter(java.awt.dnd.DropTargetDragEvent)
    150 	 */
    151 	public void dragEnter(final DropTargetDragEvent e)
    152 	{
    153 		// The chartPanel does not react on enters
    154 	}
    155
    156 	/***
    157 	 * @see java.awt.dnd.DropTargetListener
    158 	 *      #dragExit(java.awt.dnd.DropTargetEvent)
    159 	 */
    160 	public void dragExit(final DropTargetEvent e)
    161 	{
    162 		// The chartPanel does not react on exits
    163 	}
    164
    165 	/***
    166 	 * @see java.awt.dnd.DropTargetListener
    167 	 *      #dragOver(java.awt.dnd.DropTargetDragEvent)
    168 	 */
    169 	public void dragOver(final DropTargetDragEvent e)
    170 	{
    171 		// The chartPanel does not react on overs
    172 	}
    173
    174 	/***
    175 	 * @see java.awt.dnd.DropTargetListener
    176 	 *      #dropActionChanged(java.awt.dnd.DropTargetDragEvent)
    177 	 */
    178 	public void dropActionChanged(final DropTargetDragEvent e)
    179 	{
    180 		// The chartPanel does not react on action changes
    181 	}
    182
    183 	/***
    184 	 * initializes the chartPanel
    185 	 */
    186 	private void initialize()
    187 	{
    188 		this.setBorder(BorderFactory.createEmptyBorder());
    189 		this.setLayout(new BorderLayout());
    190
    191 		JPanel buttons = new JPanel();
    192 		buttons.setBorder(BorderFactory.createEmptyBorder());
    193 		buttons.setLayout((new BoxLayout(buttons, BoxLayout.X_AXIS)));
    194
    195 		JButton addRowButton = new JButton(new AddRowAction(this));
    196 		buttons.add(addRowButton);
    197
    198 		JButton deleteRowButton = new JButton(new DeleteRowAction(this));
    199 		buttons.add(deleteRowButton);
    200
    201 		JButton addColumnButton = new JButton(new AddColumnAction(this));
    202 		buttons.add(addColumnButton);
    203
    204 		JButton deleteColumnButton = new JButton(new DeleteColumnAction(this));
    205 		buttons.add(deleteColumnButton);
    206
    207 		TablePanel buttonHolder = new TablePanel(1, 1);
    208 		buttonHolder.setBorder(BorderFactory.createEmptyBorder());
    209
    210 		buttonHolder.setCell(buttons, 0, 0);
    211 		this.add(buttonHolder, BorderLayout.SOUTH);
    212 		this.add(this.contentPanel, BorderLayout.CENTER);
    213
    214 		for (int r = 0; r < this.contentPanel.getRows(); r++)
    215 		{
    216 			for (int c = 0; c < this.contentPanel.getColumns(); c++)
    217 			{
    218 				JPanel panel = new JPanel();
    219 				panel.setPreferredSize(this.getMaximumSize());
    220 				panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    221 				this.contentPanel.setCell(panel, r, c);
    222 			}
    223 		}
    224 	}
    225 }