Merging MK branch revs. 412: Close all windows of common prefix.
This commit is contained in:
		@@ -41,6 +41,8 @@ import javax.swing.JOptionPane;
 | 
			
		||||
import javax.swing.JPanel;
 | 
			
		||||
import javax.swing.JProgressBar;
 | 
			
		||||
import javax.swing.JRadioButtonMenuItem;
 | 
			
		||||
import javax.swing.JScrollPane;
 | 
			
		||||
import javax.swing.JSeparator;
 | 
			
		||||
import javax.swing.JWindow;
 | 
			
		||||
import javax.swing.KeyStroke;
 | 
			
		||||
import javax.swing.SwingUtilities;
 | 
			
		||||
@@ -78,7 +80,8 @@ import eva2.tools.jproxy.RemoteStateListener;
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
public class EvAClient implements RemoteStateListener, Serializable {
 | 
			
		||||
	final int splashScreenTime = 1500;
 | 
			
		||||
	private final int splashScreenTime = 1500;
 | 
			
		||||
	private final int maxWindowMenuLength = 30;
 | 
			
		||||
	private static Properties EVA_PROPERTIES;
 | 
			
		||||
	
 | 
			
		||||
	public static boolean TRACE = false;
 | 
			
		||||
@@ -303,7 +306,7 @@ public class EvAClient implements RemoteStateListener, Serializable {
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		if (withGUI ) {
 | 
			
		||||
			m_Frame = new JEFrame();
 | 
			
		||||
			m_Frame = new JEFrame(EvAInfo.productName + " workbench");
 | 
			
		||||
			BasicResourceLoader loader = BasicResourceLoader.instance();
 | 
			
		||||
			byte[] bytes = loader.getBytesFromResourceLocation(EvAInfo.iconLocation);
 | 
			
		||||
			try {
 | 
			
		||||
@@ -311,7 +314,7 @@ public class EvAClient implements RemoteStateListener, Serializable {
 | 
			
		||||
			} catch (java.lang.NullPointerException e) {
 | 
			
		||||
				System.out.println("Could not find EvA2 icon, please move resources folder to working directory!");
 | 
			
		||||
			}
 | 
			
		||||
			m_Frame.setTitle(EvAInfo.productName + " workbench");
 | 
			
		||||
//			m_Frame.setTitle(EvAInfo.productName + " workbench");
 | 
			
		||||
 | 
			
		||||
			try {
 | 
			
		||||
				Thread.sleep(200);
 | 
			
		||||
@@ -583,22 +586,47 @@ public class EvAClient implements RemoteStateListener, Serializable {
 | 
			
		||||
		m_mnuWindow = new JExtMenu("&Window");
 | 
			
		||||
		m_mnuWindow.addMenuListener(new MenuListener() {
 | 
			
		||||
			public void menuSelected(MenuEvent e) {
 | 
			
		||||
//				System.out.println("Selected");
 | 
			
		||||
				//				System.out.println("Selected");
 | 
			
		||||
				m_mnuWindow.removeAll();
 | 
			
		||||
				JExtMenu curMenu = m_mnuWindow;
 | 
			
		||||
//				JScrollPane jsp = new JScrollPane();
 | 
			
		||||
				Object[] framelist = JEFrameRegister.getFrameList();
 | 
			
		||||
				for (int i = 0; i < framelist.length; i++) {
 | 
			
		||||
					JMenuItem act = new JMenuItem((i + 1) + ". " + ((JEFrame) framelist[i]).getTitle());
 | 
			
		||||
					final JFrame x = ((JEFrame) framelist[i]);
 | 
			
		||||
					
 | 
			
		||||
					act.addActionListener((new ActionListener() {
 | 
			
		||||
						public void actionPerformed(ActionEvent e) {
 | 
			
		||||
							x.setExtendedState(JFrame.NORMAL);
 | 
			
		||||
							x.toFront();
 | 
			
		||||
							if (!x.isActive()) {
 | 
			
		||||
								x.setExtendedState(JFrame.NORMAL);
 | 
			
		||||
								x.setVisible(false);
 | 
			
		||||
								x.setVisible(true); // it seems to be quite a fuss to bring something to the front and actually mean it...
 | 
			
		||||
								x.toFront();		// this seems useless
 | 
			
		||||
								x.requestFocus();	// this seems useless too
 | 
			
		||||
							}
 | 
			
		||||
						}
 | 
			
		||||
					}));
 | 
			
		||||
					if (curMenu.getItemCount()>=maxWindowMenuLength) {
 | 
			
		||||
						JExtMenu subMenu = new JExtMenu("&More...");
 | 
			
		||||
						curMenu.add(new JSeparator());
 | 
			
		||||
						curMenu.add(subMenu);
 | 
			
		||||
						curMenu=subMenu;
 | 
			
		||||
					}
 | 
			
		||||
					)
 | 
			
		||||
					);
 | 
			
		||||
					curMenu.add(act);
 | 
			
		||||
				}
 | 
			
		||||
				String[] commonPrefixes = JEFrameRegister.getCommonPrefixes(10);
 | 
			
		||||
				if (commonPrefixes.length > 0) m_mnuWindow.add(new JSeparator());
 | 
			
		||||
				for (int i=0; i<commonPrefixes.length; i++) {
 | 
			
		||||
					final String prefix = commonPrefixes[i];
 | 
			
		||||
					JMenuItem act = new JMenuItem("Close all of " + prefix + "...");
 | 
			
		||||
					act.addActionListener((new ActionListener() {
 | 
			
		||||
						public void actionPerformed(ActionEvent e) {
 | 
			
		||||
							JEFrameRegister.closeAllByPrefix(prefix);
 | 
			
		||||
						}
 | 
			
		||||
					}));
 | 
			
		||||
					m_mnuWindow.add(act);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
			}
 | 
			
		||||
			public void menuCanceled(MenuEvent e) {
 | 
			
		||||
			}
 | 
			
		||||
 
 | 
			
		||||
@@ -173,6 +173,20 @@ public class FunctionArea extends DArea implements Serializable {
 | 
			
		||||
		 drawIcon(theIcon, label, position, graphID);
 | 
			
		||||
	 }
 | 
			
		||||
	 
 | 
			
		||||
	 /**
 | 
			
		||||
	  * Draw a line with given start and end points.
 | 
			
		||||
	  * 
 | 
			
		||||
	  * @param p1
 | 
			
		||||
	  * @param p2
 | 
			
		||||
	  */
 | 
			
		||||
	 public void drawLine(double[] p1, double[] p2) {
 | 
			
		||||
		DPointSet popRep      = new DPointSet();
 | 
			
		||||
		popRep.setConnected(true);
 | 
			
		||||
		popRep.addDPoint(new DPoint(p1[0], p1[1]));
 | 
			
		||||
		popRep.addDPoint(new DPoint(p2[0], p2[1]));
 | 
			
		||||
		addDElement(popRep);
 | 
			
		||||
	 }
 | 
			
		||||
	 
 | 
			
		||||
	 /**
 | 
			
		||||
	  * Plot a circle icon to the function area which is annotated with a char and
 | 
			
		||||
	  * a double value. The color corresponds to the color of the graph with given ID
 | 
			
		||||
 
 | 
			
		||||
@@ -19,74 +19,85 @@ import java.awt.*;
 | 
			
		||||
 | 
			
		||||
public class JEFrame extends JFrame {
 | 
			
		||||
 | 
			
		||||
  public JEFrame() {
 | 
			
		||||
    super();
 | 
			
		||||
    register();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public JEFrame(String name) {
 | 
			
		||||
    super(name);
 | 
			
		||||
    register();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private void register() {
 | 
			
		||||
    JEFrameRegister.register(this);
 | 
			
		||||
 | 
			
		||||
    this.addWindowListener(new WindowAdapter() {
 | 
			
		||||
      public void windowClosing(WindowEvent e) {
 | 
			
		||||
        JEFrameRegister.unregister((JEFrame) e.getWindow());
 | 
			
		||||
      }
 | 
			
		||||
      public void windowClosed(WindowEvent e) {
 | 
			
		||||
       JEFrameRegister.unregister((JEFrame) e.getWindow());
 | 
			
		||||
      }
 | 
			
		||||
    });
 | 
			
		||||
    this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
 | 
			
		||||
      KeyStroke.getKeyStroke(KeyEvent.VK_F,Event.CTRL_MASK),
 | 
			
		||||
      "ctrlFpressed"
 | 
			
		||||
    );
 | 
			
		||||
    this.getRootPane().getActionMap().put(
 | 
			
		||||
      "ctrlFpressed",
 | 
			
		||||
      new AbstractAction("ctrlFpressed") {
 | 
			
		||||
        public void actionPerformed( ActionEvent actionEvent ) {
 | 
			
		||||
          ((JEFrame) JEFrameRegister.getFrameList()[0]).setExtendedState(JEFrame.NORMAL);
 | 
			
		||||
          ((JEFrame) JEFrameRegister.getFrameList()[0]).toFront();
 | 
			
		||||
       }
 | 
			
		||||
      }
 | 
			
		||||
    );
 | 
			
		||||
    this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
 | 
			
		||||
      KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK),
 | 
			
		||||
      "ctrlOpressed"
 | 
			
		||||
    );
 | 
			
		||||
    this.getRootPane().getActionMap().put(
 | 
			
		||||
      "ctrlOpressed",
 | 
			
		||||
      new AbstractAction("ctrlOpressed") {
 | 
			
		||||
        public void actionPerformed( ActionEvent actionEvent ) {
 | 
			
		||||
          Object[] fl = JEFrameRegister.getFrameList();
 | 
			
		||||
          for (int i = 0; i < fl.length; i++) {
 | 
			
		||||
                      ((JEFrame) JEFrameRegister.getFrameList()[i]).setExtendedState(JEFrame.NORMAL);
 | 
			
		||||
                      ((JEFrame) JEFrameRegister.getFrameList()[i]).toFront();
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
       }
 | 
			
		||||
      }
 | 
			
		||||
    );
 | 
			
		||||
    this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
 | 
			
		||||
      KeyStroke.getKeyStroke(KeyEvent.VK_LESS , Event.CTRL_MASK),
 | 
			
		||||
      "ctrlSmallerpressed"
 | 
			
		||||
    );
 | 
			
		||||
    final JEFrame self = this;
 | 
			
		||||
    this.getRootPane().getActionMap().put(
 | 
			
		||||
      "ctrlSmallerpressed",
 | 
			
		||||
      new AbstractAction("ctrlSmallerpressed") {
 | 
			
		||||
        public void actionPerformed( ActionEvent actionEvent ) {
 | 
			
		||||
          JEFrameRegister.setFocusToNext(self);
 | 
			
		||||
       }
 | 
			
		||||
      }
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
	public JEFrame() {
 | 
			
		||||
		super();
 | 
			
		||||
		init();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public JEFrame(String name) {
 | 
			
		||||
		super(name);
 | 
			
		||||
		init();
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	@Override
 | 
			
		||||
	public void addWindowListener(WindowListener l) {
 | 
			
		||||
		super.addWindowListener(l);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private void init() {
 | 
			
		||||
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
 | 
			
		||||
		
 | 
			
		||||
		this.addWindowListener(new WindowAdapter() {
 | 
			
		||||
			@Override
 | 
			
		||||
			public void windowClosed(WindowEvent e) {
 | 
			
		||||
				super.windowClosed(e);
 | 
			
		||||
				JEFrameRegister.unregister((JEFrame) e.getWindow());
 | 
			
		||||
				//       ((JFrame) e.getWindow()).dispose();
 | 
			
		||||
			}
 | 
			
		||||
			@Override
 | 
			
		||||
			public void windowOpened(WindowEvent e) {
 | 
			
		||||
				super.windowOpened(e);
 | 
			
		||||
				JEFrameRegister.register((JEFrame) e.getWindow());
 | 
			
		||||
			}
 | 
			
		||||
			@Override
 | 
			
		||||
			public void windowActivated(WindowEvent e) {
 | 
			
		||||
				JEFrameRegister.register((JEFrame) e.getWindow());
 | 
			
		||||
				super.windowActivated(e);
 | 
			
		||||
			}
 | 
			
		||||
			
 | 
			
		||||
		});
 | 
			
		||||
		this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
 | 
			
		||||
				KeyStroke.getKeyStroke(KeyEvent.VK_F,Event.CTRL_MASK),
 | 
			
		||||
				"ctrlFpressed"
 | 
			
		||||
		);
 | 
			
		||||
		this.getRootPane().getActionMap().put(
 | 
			
		||||
				"ctrlFpressed",
 | 
			
		||||
				new AbstractAction("ctrlFpressed") {
 | 
			
		||||
					public void actionPerformed( ActionEvent actionEvent ) {
 | 
			
		||||
						((JEFrame) JEFrameRegister.getFrameList()[0]).setExtendedState(JEFrame.NORMAL);
 | 
			
		||||
						((JEFrame) JEFrameRegister.getFrameList()[0]).toFront();
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
		);
 | 
			
		||||
		this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
 | 
			
		||||
				KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK),
 | 
			
		||||
				"ctrlOpressed"
 | 
			
		||||
		);
 | 
			
		||||
		this.getRootPane().getActionMap().put(
 | 
			
		||||
				"ctrlOpressed",
 | 
			
		||||
				new AbstractAction("ctrlOpressed") {
 | 
			
		||||
					public void actionPerformed( ActionEvent actionEvent ) {
 | 
			
		||||
						Object[] fl = JEFrameRegister.getFrameList();
 | 
			
		||||
						for (int i = 0; i < fl.length; i++) {
 | 
			
		||||
							((JEFrame) JEFrameRegister.getFrameList()[i]).setExtendedState(JEFrame.NORMAL);
 | 
			
		||||
							((JEFrame) JEFrameRegister.getFrameList()[i]).toFront();
 | 
			
		||||
						}
 | 
			
		||||
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
		);
 | 
			
		||||
		this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
 | 
			
		||||
				KeyStroke.getKeyStroke(KeyEvent.VK_LESS , Event.CTRL_MASK),
 | 
			
		||||
				"ctrlSmallerpressed"
 | 
			
		||||
		);
 | 
			
		||||
		final JEFrame self = this;
 | 
			
		||||
		this.getRootPane().getActionMap().put(
 | 
			
		||||
				"ctrlSmallerpressed",
 | 
			
		||||
				new AbstractAction("ctrlSmallerpressed") {
 | 
			
		||||
					public void actionPerformed( ActionEvent actionEvent ) {
 | 
			
		||||
						JEFrameRegister.setFocusToNext(self);
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
		);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -13,35 +13,65 @@ import java.util.ArrayList;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public class JEFrameRegister {
 | 
			
		||||
	private static ArrayList<JEFrame> JEFrameList;
 | 
			
		||||
 | 
			
		||||
  private static ArrayList JEFrameList;
 | 
			
		||||
	static {
 | 
			
		||||
		JEFrameList = new ArrayList<JEFrame>();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
  static {
 | 
			
		||||
	public static void register(JEFrame jf) {
 | 
			
		||||
		if (!JEFrameList.contains(jf)) JEFrameList.add(jf);
 | 
			
		||||
//		System.out.println("reg  " + jf.getTitle() + "/" + (jf.hashCode()) + ", list size: " + JEFrameList.size());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    JEFrameList = new ArrayList();
 | 
			
		||||
  }
 | 
			
		||||
	public static void unregister(JEFrame jf) {
 | 
			
		||||
		JEFrameList.remove(jf); // Plot windows produce double closing events, so ignore it
 | 
			
		||||
//		if (!JEFrameList.remove(jf)) System.err.println("Warning: tried to unregister frame " + jf.getTitle() + " which was not registered! (JEFrameRegister)");
 | 
			
		||||
//		System.out.println("unreg " + jf.getTitle() + "/" + jf.hashCode() + ", list size:" + JEFrameList.size());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
  public static void register(JEFrame jf) {
 | 
			
		||||
    JEFrameList.add(jf);
 | 
			
		||||
//    System.out.println("reg  ! JEFSIZE :" + JEFrameList.size());
 | 
			
		||||
  }
 | 
			
		||||
	public static Object[] getFrameList() {
 | 
			
		||||
		return JEFrameList.toArray();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
  public static void unregister(JEFrame jf) {
 | 
			
		||||
    JEFrameList.remove(jf);
 | 
			
		||||
//    System.out.println("unreg! JEFSIZE :" + JEFrameList.size());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public static Object[] getFrameList() {
 | 
			
		||||
    return JEFrameList.toArray();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public static void setFocusToNext(JEFrame jf) {
 | 
			
		||||
    int idx = JEFrameList.indexOf(jf);
 | 
			
		||||
    idx = (idx + 1) % JEFrameList.size();
 | 
			
		||||
    JEFrame toset =    ((JEFrame) JEFrameList.get(idx));
 | 
			
		||||
    toset.setExtendedState(JEFrame.NORMAL);
 | 
			
		||||
    toset.toFront();
 | 
			
		||||
  }
 | 
			
		||||
	public static void setFocusToNext(JEFrame jf) {
 | 
			
		||||
		int idx = JEFrameList.indexOf(jf);
 | 
			
		||||
		idx = (idx + 1) % JEFrameList.size();
 | 
			
		||||
		JEFrame toset =    ((JEFrame) JEFrameList.get(idx));
 | 
			
		||||
		toset.setExtendedState(JEFrame.NORMAL);
 | 
			
		||||
		toset.toFront();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Return all prefixes which occur at least twice in the registered frame list.
 | 
			
		||||
	 * @param prefLen
 | 
			
		||||
	 * @return
 | 
			
		||||
	 */
 | 
			
		||||
	public static String[] getCommonPrefixes(int prefLen) {
 | 
			
		||||
		ArrayList<String> prefixes = new ArrayList<String>();
 | 
			
		||||
		ArrayList<Integer> count = new ArrayList<Integer>();
 | 
			
		||||
		for (int i=0; i<JEFrameList.size(); i++) {
 | 
			
		||||
			String title = JEFrameList.get(i).getTitle();
 | 
			
		||||
			String titPref = title.substring(0, Math.min(prefLen, title.length()));
 | 
			
		||||
			int earlierIndex = prefixes.indexOf(titPref);
 | 
			
		||||
			if (earlierIndex<0) {
 | 
			
		||||
				prefixes.add(titPref);
 | 
			
		||||
				count.add(1);
 | 
			
		||||
			} else count.set(earlierIndex, 1+count.get(earlierIndex));
 | 
			
		||||
		}
 | 
			
		||||
		for (int i=prefixes.size()-1; i>=0; i--) {
 | 
			
		||||
			if (count.get(i)<=1) {
 | 
			
		||||
				prefixes.remove(i);
 | 
			
		||||
				count.remove(i);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		return prefixes.toArray(new String[prefixes.size()]);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static void closeAllByPrefix(String prefix) {
 | 
			
		||||
		for (int i=0; i<JEFrameList.size(); i++) {
 | 
			
		||||
			String title = JEFrameList.get(i).getTitle();
 | 
			
		||||
			if (title.startsWith(prefix)) JEFrameList.get(i).dispose();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -41,14 +41,10 @@ public class PropertyDialog extends JEFrame {
 | 
			
		||||
    try {
 | 
			
		||||
        setIconImage(Toolkit.getDefaultToolkit().createImage(bytes));
 | 
			
		||||
    } catch (java.lang.NullPointerException e) {
 | 
			
		||||
        System.out.println("Could not find EvA2 icon, please move resources folder to working directory!");
 | 
			
		||||
        System.err.println("Could not find EvA2 icon, please move resources folder to working directory!");
 | 
			
		||||
    }
 | 
			
		||||
    //System.out.println("PropertyDialog.Constructor  of "+ Title);
 | 
			
		||||
    addWindowListener(new WindowAdapter() {
 | 
			
		||||
      public void windowClosing(WindowEvent e) {
 | 
			
		||||
	e.getWindow().dispose();
 | 
			
		||||
      }
 | 
			
		||||
    });
 | 
			
		||||
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
 | 
			
		||||
    getContentPane().setLayout(new BorderLayout());
 | 
			
		||||
    m_Editor = editor;
 | 
			
		||||
    m_EditorComponent = editor.getCustomEditor();
 | 
			
		||||
 
 | 
			
		||||
@@ -21,6 +21,7 @@ import java.awt.event.MouseEvent;
 | 
			
		||||
import java.beans.PropertyEditor;
 | 
			
		||||
 | 
			
		||||
import javax.swing.BorderFactory;
 | 
			
		||||
import javax.swing.JFrame;
 | 
			
		||||
import javax.swing.JPanel;
 | 
			
		||||
 | 
			
		||||
import eva2.tools.EVAHELP;
 | 
			
		||||
@@ -51,7 +52,13 @@ public class PropertyPanel extends JPanel {
 | 
			
		||||
					}
 | 
			
		||||
					else {
 | 
			
		||||
						m_PropertyDialog.updateFrameTitle(m_PropertyEditor);
 | 
			
		||||
//						System.out.println("" + BeanInspector.toString(m_PropertyDialog));
 | 
			
		||||
						m_PropertyDialog.setVisible(false);
 | 
			
		||||
						m_PropertyDialog.setExtendedState(JFrame.NORMAL);
 | 
			
		||||
						m_PropertyDialog.setVisible(true);
 | 
			
		||||
						m_PropertyDialog.requestFocus();
 | 
			
		||||
//						System.out.println("" + BeanInspector.toString(m_PropertyDialog));
 | 
			
		||||
//						System.out.println("Aft: " + m_PropertyDialog.isShowing() + " " + m_PropertyDialog.isVisible() + " " + m_PropertyDialog.isActive() + " " + m_PropertyDialog.isFocused());
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 
 | 
			
		||||
@@ -12,10 +12,11 @@ package eva2.gui;
 | 
			
		||||
/*==========================================================================*
 | 
			
		||||
 * IMPORTS
 | 
			
		||||
 *==========================================================================*/
 | 
			
		||||
import java.awt.*;
 | 
			
		||||
import java.awt.Color;
 | 
			
		||||
 | 
			
		||||
import eva2.server.go.problems.Interface2DBorderProblem;
 | 
			
		||||
import eva2.tools.chart2d.*;
 | 
			
		||||
import eva2.server.go.problems.InterfaceFirstOrderDerivableProblem;
 | 
			
		||||
import eva2.tools.chart2d.DRectangle;
 | 
			
		||||
import eva2.tools.diagram.ColorBarCalculator;
 | 
			
		||||
import eva2.tools.math.Mathematics;
 | 
			
		||||
 | 
			
		||||
@@ -28,8 +29,8 @@ import eva2.tools.math.Mathematics;
 | 
			
		||||
 */
 | 
			
		||||
public class TopoPlot extends Plot {
 | 
			
		||||
 | 
			
		||||
  public int gridx = 50;
 | 
			
		||||
  public int gridy = 50;
 | 
			
		||||
  private int gridx = 50;
 | 
			
		||||
  private int gridy = 50;
 | 
			
		||||
  int colorScale = ColorBarCalculator.BLUE_TO_RED;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
@@ -59,19 +60,29 @@ public class TopoPlot extends Plot {
 | 
			
		||||
    colorScale = color_scale;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Defines parameters used for drawing the topology.
 | 
			
		||||
   * @param gridX the x-resolution of the topology, higher value means higher resolution
 | 
			
		||||
   * @param gridY the y-resolution of the topology, higher value means higher resolution
 | 
			
		||||
   */
 | 
			
		||||
  public void setParams(int gridX, int gridY) {
 | 
			
		||||
    setParams(gridX, gridY, colorScale);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Defines the topology (by setting a specific problem) and draws the topology
 | 
			
		||||
   */
 | 
			
		||||
  public void setTopology(Interface2DBorderProblem problem) {
 | 
			
		||||
	  setTopology(problem, problem.get2DBorder());
 | 
			
		||||
	  setTopology(problem, problem.get2DBorder(), false);
 | 
			
		||||
  }
 | 
			
		||||
   /**
 | 
			
		||||
   * Defines the topology (by setting a specific problem) and draws the topology
 | 
			
		||||
   */
 | 
			
		||||
  public void setTopology(Interface2DBorderProblem problem, double[][] border) {
 | 
			
		||||
  public void setTopology(Interface2DBorderProblem problem, double[][] border, boolean withGradientsIfAvailable) {
 | 
			
		||||
	double[] sizeXY=Mathematics.getAbsRange(border);
 | 
			
		||||
    double deltaX = sizeXY[0]/gridx;
 | 
			
		||||
    double deltaY = sizeXY[1]/gridy;
 | 
			
		||||
    double maxDeriv=0;
 | 
			
		||||
    double[] pos = new double[2];
 | 
			
		||||
    //double fitRange = java.lang.Math.abs(problem.getMinFitness()-problem.getMaxFitness() );
 | 
			
		||||
    double fitRange = 0, max = -Double.MAX_VALUE, min = Double.MAX_VALUE, tmp;
 | 
			
		||||
@@ -82,6 +93,10 @@ public class TopoPlot extends Plot {
 | 
			
		||||
    		tmp = (float)(problem.functionValue(pos));
 | 
			
		||||
    		if (tmp < min) min = tmp;
 | 
			
		||||
    		if (tmp > max) max = tmp;
 | 
			
		||||
    		if (problem instanceof InterfaceFirstOrderDerivableProblem) {
 | 
			
		||||
    			double[] deriv = ((InterfaceFirstOrderDerivableProblem)problem).getFirstOrderGradients(pos);
 | 
			
		||||
    			for (int i=0; i<2;i++) maxDeriv=Math.max(maxDeriv, Math.abs(deriv[i])); // maximum deriv of first 2 dims
 | 
			
		||||
    		}
 | 
			
		||||
    	} // for y
 | 
			
		||||
    } // for x
 | 
			
		||||
    fitRange = java.lang.Math.abs(max - min);
 | 
			
		||||
@@ -89,18 +104,31 @@ public class TopoPlot extends Plot {
 | 
			
		||||
 | 
			
		||||
    m_Frame.setVisible(false);
 | 
			
		||||
    for (int x=0; x<gridx; x++) {
 | 
			
		||||
      for (int y=0; y<gridy; y++) {
 | 
			
		||||
    	  pos[0]  = border[0][0]+x*deltaX;
 | 
			
		||||
    	  pos[1]  = border[1][0]+y*deltaY;
 | 
			
		||||
    	  DRectangle rect = new DRectangle(pos[0],pos[1],deltaX,deltaY);
 | 
			
		||||
    	  Color color = new Color(colorBar.getRGB((float)((problem.functionValue(pos)-min)/fitRange)));
 | 
			
		||||
    	  // Color color = new Color(255,(int)(problem.doEvaluation(pos)[0]/fitRange*255),(int)(problem.doEvaluation(pos)[0]/fitRange*255));
 | 
			
		||||
//  	  Color color = new Color(colorBar.getRGB((float)(problem.functionValue(pos)/fitRange))); // Color color = new Color(255,(int)(problem.doEvaluation(pos)[0]/fitRange*255),(int)(problem.doEvaluation(pos)[0]/fitRange*255));
 | 
			
		||||
    	  rect.setColor(color);
 | 
			
		||||
    	  rect.setFillColor(color);
 | 
			
		||||
    	  m_PlotArea.addDElement(rect);
 | 
			
		||||
      } // for y
 | 
			
		||||
    	for (int y=0; y<gridy; y++) {
 | 
			
		||||
    		pos[0]  = border[0][0]+x*deltaX;
 | 
			
		||||
    		pos[1]  = border[1][0]+y*deltaY;
 | 
			
		||||
    		DRectangle rect = new DRectangle(pos[0]-(deltaX/2),pos[1]-(deltaY/2),deltaX,deltaY);
 | 
			
		||||
    		Color color = new Color(colorBar.getRGB((float)((problem.functionValue(pos)-min)/fitRange)));
 | 
			
		||||
    		// Color color = new Color(255,(int)(problem.doEvaluation(pos)[0]/fitRange*255),(int)(problem.doEvaluation(pos)[0]/fitRange*255));
 | 
			
		||||
    		//  	  Color color = new Color(colorBar.getRGB((float)(problem.functionValue(pos)/fitRange))); // Color color = new Color(255,(int)(problem.doEvaluation(pos)[0]/fitRange*255),(int)(problem.doEvaluation(pos)[0]/fitRange*255));
 | 
			
		||||
    		rect.setColor(color);
 | 
			
		||||
    		rect.setFillColor(color);
 | 
			
		||||
    		m_PlotArea.addDElement(rect);
 | 
			
		||||
    	} // for y
 | 
			
		||||
    } // for x
 | 
			
		||||
    if (withGradientsIfAvailable && (problem instanceof InterfaceFirstOrderDerivableProblem)) {
 | 
			
		||||
    	for (int x=0; x<gridx; x++) {
 | 
			
		||||
    		for (int y=0; y<gridy; y++) {
 | 
			
		||||
    			pos[0]  = border[0][0]+x*deltaX;
 | 
			
		||||
    			pos[1]  = border[1][0]+y*deltaY;
 | 
			
		||||
    			double[] derivPos = ((InterfaceFirstOrderDerivableProblem)problem).getFirstOrderGradients(pos);
 | 
			
		||||
    			Mathematics.svDiv(1.1*(2*maxDeriv/Math.max(deltaX, deltaY)), derivPos, derivPos);
 | 
			
		||||
    			Mathematics.vvAdd(pos, derivPos, derivPos);
 | 
			
		||||
    			getFunctionArea().drawLine(pos, derivPos);
 | 
			
		||||
    			getFunctionArea().drawIcon(1, "", derivPos, 0);
 | 
			
		||||
    		} // for y
 | 
			
		||||
    	} // for x
 | 
			
		||||
    }
 | 
			
		||||
    m_Frame.setVisible(true);
 | 
			
		||||
 | 
			
		||||
  } // setTopology
 | 
			
		||||
 
 | 
			
		||||
@@ -760,8 +760,7 @@ public class PostProcess {
 | 
			
		||||
		if (plot == null) {
 | 
			
		||||
			plot = new TopoPlot("PostProcessing: " + title, "x", "y",range[0],range[1]);
 | 
			
		||||
		    if (prob instanceof Interface2DBorderProblem) {
 | 
			
		||||
		    	plot.gridx=60;
 | 
			
		||||
		    	plot.gridy=60;
 | 
			
		||||
				plot.setParams(60, 60);
 | 
			
		||||
		    	plot.setTopology((Interface2DBorderProblem)prob);
 | 
			
		||||
		    }
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
@@ -222,8 +222,7 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis
 | 
			
		||||
            Population              pop;
 | 
			
		||||
 | 
			
		||||
            this.m_Topology          = new TopoPlot("CBN-Species at gen. " + gen,"x","y",a,a);
 | 
			
		||||
            this.m_Topology.gridx = 60;
 | 
			
		||||
            this.m_Topology.gridy = 60;
 | 
			
		||||
			this.m_Topology.setParams(60, 60);
 | 
			
		||||
            this.m_Topology.setTopology((Interface2DBorderProblem)this.m_Problem);
 | 
			
		||||
            //draw the undifferentiated
 | 
			
		||||
            for (int i = 0; i < this.m_Undifferentiated.size(); i++) {
 | 
			
		||||
 
 | 
			
		||||
@@ -1312,8 +1312,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se
 | 
			
		||||
				a[1] = 0.0;
 | 
			
		||||
				if (topoPlot == null) {
 | 
			
		||||
					this.topoPlot = new TopoPlot("CBN-Species","x","y",a,a);
 | 
			
		||||
					this.topoPlot.gridx = 60;
 | 
			
		||||
					this.topoPlot.gridy = 60;
 | 
			
		||||
					this.topoPlot.setParams(60, 60);
 | 
			
		||||
					this.topoPlot.setTopology((Interface2DBorderProblem)this.m_Problem);
 | 
			
		||||
				}
 | 
			
		||||
				
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user