/******************************************************************* Copyright (c) 2001-2010 Apprisant Technologies Inc. All rights reserved. Apprisant Technologies permits non-exclusive royalty free use, copying and modification of this demo program source code for the purposes of evaluating the Apprisant Toolkit or building applications that use it. This software and documentation is provided "AS IS". APPRISANT TECHNOLOGIES DISCLAIMS ANY REPRESENTATIONS AND WARRANTIES, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTY OF MERCHANTABILITY AND FITNESS OF THIS PRODUCT FOR ANY PARTICULAR PURPOSE. APPRISANT TECHNOLOGIES SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY ANY PARTY, INCLUDING LOST PROFITS, ARISING FROM THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ITS DERIVATIONS. ********************************************************************/ package red; import java.awt.Color; import java.awt.Dimension; import java.awt.BorderLayout; import java.beans.*; import java.util.List; import javax.swing.*; import apprisant.diagram.*; import apprisant.diagram.render.*; import apprisant.diagram.io.*; import apprisant.diagram.event.*; import apprisant.diagram.env.*; /* Main class of the Red demo configures the diagram, defines the listeners, and processes the command line arguments. See index.html for instructions on running this program. */ public class Red extends JFrame { static final String ME = "Red"; // Command line arguments static final String A_H = "-h"; static final String A_HELP = "-help"; static final String A_SOURCE = "-source"; static final String A_PATH = "-path"; static final String A_PROPERTIES = "-properties"; static final String USAGE = "Usage: red.Red [-properties ] \n" + " [-path ] \n" + " [-source ] \n" + " []"; Diagram diagram; DiagramSelectionModel selectionModel; RedModel model; Red (String title) throws Exception { super(title); // Read our renderers RenderingParams renParams = new RendererScript().read("red/red.rdr"); // Get the renderer whose bounds determine the node size for layout AGRenderer nodeRen = renParams.getRenderer("node"); // Element model model = new RedModel(nodeRen); // Diagram diagram = new Diagram(ME, model); diagram.getRenderManager().setParams(renParams); // Get the background color from the renderer file Color bgColor = (Color) renParams.getDefinitions().get("bgColor"); diagram.setBackground(bgColor); // View manager: keep the diagram in the upper left corner to keep // the tree from jumping around as subtrees are opened and closed ViewManager viewMgr = diagram.getViewManager(); viewMgr.setMargin(new Dimension(16, 16)); viewMgr.setAlignment(AGAlignment.NORTH_WEST); // Selection model selectionModel = diagram.getSelectionModel(); selectionModel.setSelectionMode(SelectionParams.SINGLE); // Selection listener selectionModel.addAGSelectionListener(new AGSelectionListener() { public void agSelectionChanged (AGSelectionEvent ev) { String key = (String) selectionModel.getSingleSelection(); if (key != null) { // Print the selected renderer RedElement re = (RedElement) model.getElement(key); AGRenderer ren = (AGRenderer) re.getDataObject(); if (ren != null) { System.out.println(ren.toString()); System.out.println(); } } } }); // Set the TreeLayout's tag listener for the yellow triangles model.enableStateListener(); // Add the diagram to the frame add(new JScrollPane(diagram), BorderLayout.CENTER); } public static void main (final String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { VetoListener vetoListener = new VetoListener(); RendererScript reader = new RendererScript(); RenderingParams renParams = null; String title = null; // Parse arguments try { // The properties file sets rendering hints AGEnvironment.loadProperties("red/red.properties"); for (int i = 0; i < args.length; i++) { if (args[i].equalsIgnoreCase(A_SOURCE) && args.length > i+1) { Class cls = Class.forName(args[i+1]); RendererSource rs = (RendererSource) cls.newInstance(); renParams = rs.getRenderingParams(); title = args[i+1]; i++; } else if (args[i].equalsIgnoreCase(A_PATH) && args.length > i+1) { renParams = reader.read(args[i+1]); title = args[i+1]; i++; } else if (args[i].equalsIgnoreCase(A_PROPERTIES) && args.length > i+1) { AGLog.addVetoableChangeListener(vetoListener); AGEnvironment.loadProperties(args[i+1]); AGLog.removeVetoableChangeListener(vetoListener); i++; } else if (args[i].equalsIgnoreCase(A_H) || args[i].equalsIgnoreCase(A_HELP)) { System.out.println(USAGE); System.exit(0); } else { renParams = reader.read(args[i]); title = args[i]; } } } catch (Exception ae) { AGLog.log(ME, "parsing arguments", AGLog.FATAL, ae); System.exit(1); } if (renParams == null) { AGLog.log(ME, "parsing arguments", AGLog.FATAL, "No renderers specified."); System.out.println(USAGE); System.exit(1); } Red red = null; try { red = new Red(title); // Set the renderers to be displayed red.model.setRenderers(renParams.getRenderers()); } catch (Exception ae) { AGLog.log(ME, "initializing", AGLog.FATAL, ae); System.exit(1); } red.setLocation(100, 100); red.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); red.pack(); Dimension size = red.getPreferredSize(); if (size.height > 600) red.setSize(size.width + 24, 600); red.setVisible(true); } }); } /* Use a veto listener to prevent logging parameters in the client properties file from overriding our settings. */ private static class VetoListener implements VetoableChangeListener { public void vetoableChange (PropertyChangeEvent ev) throws PropertyVetoException { throw new PropertyVetoException(null, ev); } } }