SwingInspector.java
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 package org.gecode.explorer.swing;
00025
00026 import org.gecode.Space;
00027 import org.gecode.explorer.ExplorerEventListener;
00028 import org.gecode.explorer.SpaceNode;
00029
00030
00031 import java.awt.Graphics;
00032 import java.awt.Graphics2D;
00033 import java.awt.Color;
00034 import java.awt.Dimension;
00035 import java.awt.GridLayout;
00036 import javax.swing.JDialog;
00037 import javax.swing.JFrame;
00038 import javax.swing.JComponent;
00039 import javax.swing.JScrollPane;
00040 import javax.swing.ScrollPaneConstants;
00041 import javax.swing.JPanel;
00042 import javax.swing.ImageIcon;
00043
00047 public class SwingInspector
00048 extends JComponent
00049 implements Cloneable, ExplorerEventListener {
00051 protected Space s;
00053 protected String name;
00059 protected boolean isList;
00062 JDialog dialog = null;
00063 JComponent list = null;
00064
00067 public SwingInspector(String name, Boolean isList) {
00068 this.name = name;
00069 this.isList = isList;
00070 }
00071
00072 public String getName() { return name; }
00073 public void nodeClickEvent(org.gecode.explorer.Inspector inspect, SpaceNode s) {
00074 Object o = inspect;
00075 if (o instanceof SwingUI) {
00076 SwingUI ui = (SwingUI)o;
00077 this.s = s.getSpace();
00078 if (!isList) {
00079 create(ui.canvas.getFrame());
00080 } else {
00081 if (dialog==null) create(ui.canvas.getFrame());
00082 dialog.setVisible(true);
00083 list.add(clone());
00084 }
00085 this.s = null;
00086 } else {
00087 System.err.println("Inspection through SwingInspector not supported if Swing UI not used!");
00088 inspect.inspect(s.toString());
00089 }
00090 }
00091
00095 public void paintComponent(Graphics g) {
00096 super.paintComponent(g);
00097 Graphics2D g2 = (Graphics2D)g;
00098 draw(s, g2);
00099 }
00103 public void draw(Space s, Graphics2D g) {
00104 System.err.println("Default draw-method on SwingInspector called, space=" + s);
00105 }
00106
00109 public SwingInspector clone() {
00110 try {
00111 SwingInspector res = (SwingInspector)super.clone();
00112 res.dialog = null; res.list = null;
00113 return res;
00114 } catch(CloneNotSupportedException ex) {
00115 System.err.println("Cloning of SwingInspector does not work:" + ex);
00116 return null;
00117 }
00118 }
00119
00120 static int poscnt = 0;
00121 void create(JFrame parent) {
00122 dialog = new JDialog(parent, name);
00123
00124
00125 if (!isList) {
00126 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
00127 dialog.getContentPane().add(clone());
00128 } else {
00129 list = new JPanel();
00130 list.setLayout(new GridLayout(0, 1));
00131 JScrollPane scrollPane = new JScrollPane(list,
00132 ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
00133 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
00134 dialog.add(scrollPane);
00135 }
00136 dialog.pack();
00137 dialog.setSize(new Dimension(200,200));
00138 dialog.setLocation(30*poscnt, 30*poscnt);
00139 poscnt = (poscnt%20)+1;
00140 dialog.setVisible(true);
00141 }
00142
00143
00146 protected final Color colors[] =
00147 {Color.blue, Color.cyan, Color.gray, Color.green, Color.magenta,
00148 Color.orange, Color.pink, Color.red, Color.yellow};
00152 protected Color getColor(int i) {
00153 Color c = colors[i%colors.length];
00154 for (int j = i/colors.length; j-->0; )
00155 if (i%1==0) c = c.darker();
00156 else c = c.brighter();
00157 return c;
00158 }
00159 }