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;imaxX) maxX = rect.getX()+rect.getWidth(); if ((rect.getY()+rect.getHeight())>maxY) maxY = rect.getY()+rect.getHeight(); } setSize((int)maxX+10,(int)maxY+closeIcon.getIconHeight()+14); setVisible(true); } /** Deselects all cells. @author WJS */ public void deselectAllCells() { for (int i=0;iCellSelectionArray.length)) return; CellSelectionArray[index] = select; TheModelState.setSelectedCells(CellSelectionArray); LastSelectedCell = index; } /** Sets scale and translation for CellShapeArray used in painting operations. @param double scale - The magnification factor for the cell. @param Point translation - the Translation for the cell shape - not subjected to scale factor. @author WJS */ public void setScaleAndTranslation(double scale, Point translation) { Scale = scale; Translation = translation; Polygon p; Polygon q; for (int i=0;i255) r = 255; if (g>255) g = 255; if (b>255) b = 255; CellColorArray[index] = new Color(r,g,b); } else { CellColorArray[index] = getBackground(); } } /** Repaints a single cell. @param int cellIndex - The index of the cell to be repainted. @author WJS */ public void updateCell(int cellIndex) { updateCellColor(cellIndex); //TODO speed up by repainting only changed cells? // TheModelStateViewPane.paintCell(i); // Repaint whole thing until above speed-up is made TheModelStateViewPane.repaint(); } /** Updates the colors for all the selected cells. @param boolean[] selectedCells - Array of cells that are selected. @author WJS */ public void updateCells(boolean[] selectedCells) { for (int i=0;i