package genegui; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.io.*; import java.util.*; import javax.swing.*; import main.*; /** ModelStateView.java Constructs a JWindow for viewing all the cells in a model. The user may select which nodes are being displayed in the window and set the values of the nodes. It is implemented in such a way as to function independently of cell shapes and positions. It is accompanied by a toolbox and a node palette.
Originally designed to be used for building initial states and for general purpose stoppers.
@author WJS
*/
public class ModelStateView extends JPanel implements NodeStateChangeListener, GuiInterface {
/** Stroke used for outlining cells */
private static final Stroke NormalStroke = new BasicStroke(2);
/** Color used for outlining cells */
private static final Color OutlineColor = Color.white;
/** Stroke used for outlining selected cells */
private static final Stroke SelectedStroke = new BasicStroke(4);
/** Current paint color of cells allows for more rapid repaint operations */
private Color[] CellColorArray;
/** Cells which are selected */
private boolean[] CellSelectionArray;
/** Stores the cell shapes including scale and translation - speeds up painting */
private Polygon[] CellShapeArray;
/** Stores the base cell shapes and locations */
private Polygon[] CellShapeArrayBase;
/** Area at the top for displaying control buttons and drag bar. Also shows
which ModelStateView is selected */
private JPanel ControlArea;
/** Drag offset used for drag operations. Distance from mouse pointer and UL of window */
private Point DragOffset;
/** The last cell that was selected. Used for copying cell properties. */
private int LastSelectedCell = 0;
/** The nodes that are visible for this viewer */
private boolean[] NodeSelectionArray;
/** Copy of this */
private ModelStateView SaveThis = this;
/** The scale at which to draw the cell shape objects */
private double Scale = 5.0;
/** The MainGui object that this belongs to */
private MainGui TheMainGui;
/** The ModelState that this viewer is attached to */
private ModelState TheModelState;
/** A pointer to the parent ModelStateEditor */
private ModelStateEditor TheModelStateEditor;
/** The pannel used to display the cells themselves */
private ModelStateViewPane TheModelStateViewPane;
/** Translation for drawing of cells */
private Point Translation = new Point(5,5);
/** Primary constructor builds the visual appearance of the viewer and
establishes mouse listeners.
*/
public ModelStateView(ModelState theModelState, ModelStateEditor theModelStateEditor, MainGui theMainGui) {
super();
TheModelState = theModelState;
TheModelStateEditor = theModelStateEditor;
TheMainGui = theMainGui;
NodeSelectionArray = new boolean[TheModelState.getNumberOfNodes()];
setLayout(new BorderLayout());
setBorder(BorderFactory.createMatteBorder(2,2,3,3,Color.darkGray));
// Create a control area for the top of the display
ControlArea = new JPanel();
ControlArea.setLayout(new BoxLayout(ControlArea,BoxLayout.X_AXIS));
ControlArea.setBackground(Color.lightGray);
// Create a filler panel to push things to the right of the control area
JPanel filler = new JPanel();
filler.setOpaque(false);
ControlArea.add(filler);
// Get the metal close icon and create a close button (Default fails on OSX).
Icon closeIcon = javax.swing.plaf.metal.MetalIconFactory.getInternalFrameCloseIcon(16);
JButton closeButton = new JButton(closeIcon);
Dimension d = new Dimension(closeIcon.getIconWidth()+4,closeIcon.getIconHeight()+4);
closeButton.setMaximumSize(d);
closeButton.setMinimumSize(d);
closeButton.setPreferredSize(d);
closeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
TheModelState.removeModelStateView(SaveThis);
TheMainGui.removeGUIListener(SaveThis);
}
});
ControlArea.add(closeButton);
// Create mouse and mouse motion listeners for allowing the window to be
// dragged.
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
DragOffset = me.getPoint();
TheModelState.setActiveModelStateView(SaveThis);
TheModelStateEditor.moveModelStateViewToFront(SaveThis);
}
public void mouseReleased(MouseEvent me) {
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent me) {
Point p = getLocation();
setLocation(p.x+me.getX()-DragOffset.x,p.y+me.getY()-DragOffset.y);
}
});
add(ControlArea,BorderLayout.NORTH);
TheModelStateViewPane = new ModelStateViewPane();
add(TheModelStateViewPane,BorderLayout.CENTER);
// Build the Cell Arrays based upon the Model information
CellColorArray = new Color[TheModelState.getNumberOfCells()];
CellSelectionArray = new boolean[TheModelState.getNumberOfCells()];
CellShapeArray = new Polygon[TheModelState.getNumberOfCells()];
CellShapeArrayBase = new Polygon[TheModelState.getNumberOfCells()];
for (int i=0;i