package genegui; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import main.*; /** CellViewer.java This is a re-write of the CellViewer class to make it more efficient and possibly less silly.
The viewer is composed on a series verticaly arrayed panels. The first panel
will always be used to display time step and other information. Each additional
panel is a CellViewerPanel which can be configured to display the color
information collected from one or more nodes.
@author WJS
*/
public class CellSurfaceViewer extends JInternalFrame implements ModelStateChangeListener,GuiInterface {
public static final int GRAPH_CELL = 0;
public static final int GRAPH_BAR = 1;
public static final int GRAPH_FUNCTION_LINEAR=0;
public static final int GRAPH_FUNCTION_LOG=1;
public static final int GRAPH_FUNCTION_EXP=2;
public static final int GRAPH_FUNCTION_THRESH=3;
public static final int GRAPH_FUNCTION_PSUEDO=4;
/** A pointer to the attached NodeStateViewer if there is one */
private NodeStateViewer AttachedNodeStateViewer;
/** Stores the cell shapes including scale and translation - speeds up painting */
private Polygon[] CellShapeArray;
/** Stores the base cell shapes and locations */
private Polygon[] CellShapeArrayBase;
/** Multiplier used for exp color maping*/
private float ExpMultiplier = 1f;
/** Sets the type of graph being displayed */
private int GraphMode = GRAPH_CELL;
/** Specifies what transformation function should be applied to the graph color */
private int GraphFunction = GRAPH_FUNCTION_LINEAR;
/** Multiplier used for log color maping*/
private float LogMultiplier = 1f;
/** The Main JMenuBar */
private JMenuBar MainMenu;
/** The main panel */
private JPanel MainPanel;
/** The unique instance of the optionspanel */
private CellViewOptionsPanel OptionsPanel;
/** An array list of all the SingleNodeNodeViewerPaintPaenls */
private ArrayList PaintPanels = new ArrayList();
/** Controls the rate at which the display refreshes by rejecting changes in size below the RefreshReject */
private float RefreshReject = .03125f;
/** Self Reference */
private CellSurfaceViewer SaveThis = this;
/** The scale at which to draw the cell shape objects */
private double Scale = 5.0;
/** If set to true, simple display mode is used which just shows values */
private boolean SimpleDisplay = false;
/** Preferred dimension for the SingleNodeViewerPanels */
private Dimension SingleNodeViewerPanelDimension;
/** The MainGui object that this belongs to */
private MainGui TheMainGui;
/** A pointer to the Model being displayed */
private Model TheModel = null;
/** A pointer to the ModelState used to pass information to the CellViewer */
private ModelState TheModelState = null;
/** A pointer to the network found in each cell */
private Network TheNetwork = null;
/** The threshold value used by the threshold colormap */
private float ThresholdValue = 0.5f;
/** Translation for drawing of cells */
private Point Translation = new Point(5,5);
/** Simple constructor.
@author WJS
*/
public CellSurfaceViewer(Model model, MainGui theMainGUI, String title, boolean simpleDisplay) {
super(title,false,true,true,true);
TheMainGui = theMainGUI;
SimpleDisplay = simpleDisplay;
MainPanel = new JPanel();
setContentPane(MainPanel);
MainPanel.setLayout(new BoxLayout(MainPanel,BoxLayout.Y_AXIS));
setModel(model);
setNetwork(model.net);
// Build the unique copy of the options panel
OptionsPanel = new CellViewOptionsPanel();
OptionsPanel.setLocation(getLocation());
OptionsPanel.setVisible(false);
// Build the JMenuBar for the CellViewer
MainMenu = new JMenuBar();
MainMenu.setBackground(Color.lightGray);
JMenuItem item;
// Displays
JMenu displays = new JMenu("Displays");
displays.setBackground(Color.lightGray);
item = new JMenuItem("Add New Node");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
addNewNode();
}
});
displays.add(item);
item = new JMenuItem("Remove All Nodes");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
removeAllNodes();
}
});
displays.add(item);
displays.addSeparator();
item = new JMenuItem("Options");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
OptionsPanel.toFront();
OptionsPanel.setLocation(getLocation());
OptionsPanel.setVisible(true);
OptionsPanel.pack();
}
});
displays.add(item);
displays.addSeparator();
item = new JMenuItem("Close");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
close();
}
});
displays.add(item);
MainMenu.add(displays);
final JMenu graphs = new JMenu("Graphs");
graphs.setBackground(Color.lightGray);
item = new JMenuItem("Cell Graph",new ImageIcon("icons/CSVChecked.gif"));
item.setHorizontalTextPosition(SwingConstants.LEFT);
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
unCheckJMenuItem(graphs,GraphFunction);
GraphMode = GRAPH_CELL;
checkMenuItem(graphs,GraphFunction);
repaint();
}
});
graphs.add(item);
item = new JMenuItem("Bar Graph");
item.setHorizontalTextPosition(SwingConstants.LEFT);
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
// TODO Implement bar graph deal
// unCheckJMenuItem(graphs,GraphFunction);
// GraphMode = GRAPH_BAR;
// checkMenuItem(graphs,GraphFunction);
// repaint();
}
});
graphs.add(item);
MainMenu.add(graphs);
// Color maps menu
final JMenu colorMaps = new JMenu("ColorMaps");
colorMaps.setBackground(Color.lightGray);
item = new JMenuItem("Linear",new ImageIcon("icons/CSVChecked.gif"));
item.setHorizontalTextPosition(SwingConstants.LEFT);
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
unCheckJMenuItem(colorMaps,GraphFunction);
GraphFunction = GRAPH_FUNCTION_LINEAR;
checkMenuItem(colorMaps,GraphFunction);
paintAll();
repaint();
}
});
colorMaps.add(item);
item = new JMenuItem("Logarithmic");
item.setHorizontalTextPosition(SwingConstants.LEFT);
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
unCheckJMenuItem(colorMaps,GraphFunction);
GraphFunction = GRAPH_FUNCTION_LOG;
checkMenuItem(colorMaps,GraphFunction);
CellViewerLogControl viewer = new CellViewerLogControl();
Point p = SaveThis.getLocation();
// TODO - Autoposition
viewer.setLocation(p);
paintAll();
repaint();
}
});
colorMaps.add(item);
item = new JMenuItem("Exponential");
item.setHorizontalTextPosition(SwingConstants.LEFT);
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
unCheckJMenuItem(colorMaps,GraphFunction);
GraphFunction = GRAPH_FUNCTION_EXP;
checkMenuItem(colorMaps,GraphFunction);
CellViewerExpControl viewer = new CellViewerExpControl();
Point p = SaveThis.getLocation();
// TODO - Autoposition
viewer.setLocation(p);
paintAll();
repaint();
}
});
colorMaps.add(item);
item = new JMenuItem("Linear Threshold");
item.setHorizontalTextPosition(SwingConstants.LEFT);
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
unCheckJMenuItem(colorMaps,GraphFunction);
GraphFunction = GRAPH_FUNCTION_THRESH;
checkMenuItem(colorMaps,GraphFunction);
CellViewerThresholdControl viewer = new CellViewerThresholdControl();
Point p = SaveThis.getLocation();
// TODO - Autoposition
viewer.setLocation(p);
paintAll();
repaint();
}
});
colorMaps.add(item);
item = new JMenuItem("PseudoColor");
item.setHorizontalTextPosition(SwingConstants.LEFT);
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
unCheckJMenuItem(colorMaps,GraphFunction);
GraphFunction = GRAPH_FUNCTION_PSUEDO;
checkMenuItem(colorMaps,GraphFunction);
paintAll();
repaint();
}
});
colorMaps.add(item);
MainMenu.add(colorMaps);
setJMenuBar(MainMenu);
// Add an internal frame listener for capturing frame closing events
addInternalFrameListener(new InternalFrameAdapter() {
public void internalFrameClosing(InternalFrameEvent ife) {
close();
}
});
pack();
setVisible(true);
setIconifiable(true);
}
/** Adds a new node display panel to the viewer. The new panel is put at
the top of the display and its value is set to that of the first node in
the list ofall nodes.
@author WJS
*/
private void addNewNode() {
// At the node panel
NodeTemplate node = TheNetwork.getNode(0);
SingleNodeViewerPanel p = new SingleNodeViewerPanel(node);
MainPanel.add(p,0);
// Keep track of the node pamel's paint panel for fast paint operations
SingleNodeViewerPanel.SingleNodeViewerPaintPanel pp = p.getPaintPanel();
PaintPanels.add(pp);
pp.paintOffScreen(true);
pp.repaint();
pack();
}
/** Checks the selected menu item.
@param JMenu menu
@param int index
@author WJS
*/
private void checkMenuItem(JMenu menu, int index) {
menu.getItem(index).setIcon(new ImageIcon("icons/CSVChecked.gif"));
}
/** Close up the JFrame properly, and disconnect fromlistener lists.
@author WJS
*/
private void close() {
TheModelState.removeModelStateChangeListener(this);
dispose();
GeneNet.TheMainGui.removeInternalFrame(this);
GeneNet.TheMainGui.removeGUIListener(this);
OptionsPanel.dispose();
}
/** Find the cell which contains point p. This is implemented in a slower
but more general way to allow for different/multiple cells shapes and cell
positions. It searche through the list of cell shapes and uses the contains
method to identify the cell.
@param Point p - A point in the ModelStateViewPane.
@return int - The index of the cell or -1 if the point is not in a cell.
@author WJS
*/
private int findCellAt(Point p) {
for (int i=0;i