0001: /*
0002: * StateMachine editor applet.
0003: * Based on GraphEd from JGraph.
0004: *
0005: * @(#)GraphEd.java 3.3 23-APR-04
0006: *
0007: * Copyright (c) 2001-2004, Gaudenz Alder All rights reserved.
0008: *
0009: * This library is free software; you can redistribute it and/or
0010: * modify it under the terms of the GNU Lesser General Public
0011: * License as published by the Free Software Foundation; either
0012: * version 2.1 of the License, or (at your option) any later version.
0013: *
0014: * This library is distributed in the hope that it will be useful,
0015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0017: * Lesser General Public License for more details.
0018: *
0019: * You should have received a copy of the GNU Lesser General Public
0020: * License along with this library; if not, write to the Free Software
0021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0022: *
0023: */
0024: package biz.hammurapi.web.statemachine;
0025:
0026: import java.awt.BorderLayout;
0027: import java.awt.Color;
0028: import java.awt.Container;
0029: import java.awt.Cursor;
0030: import java.awt.Font;
0031: import java.awt.Graphics;
0032: import java.awt.Point;
0033: import java.awt.event.ActionEvent;
0034: import java.awt.event.KeyEvent;
0035: import java.awt.event.KeyListener;
0036: import java.awt.event.MouseEvent;
0037: import java.awt.geom.Point2D;
0038: import java.awt.geom.Rectangle2D;
0039: import java.awt.image.BufferedImage;
0040: import java.io.ByteArrayInputStream;
0041: import java.io.ByteArrayOutputStream;
0042: import java.io.ObjectInputStream;
0043: import java.io.ObjectOutputStream;
0044: import java.net.URL;
0045: import java.util.ArrayList;
0046: import java.util.Date;
0047: import java.util.Hashtable;
0048: import java.util.List;
0049: import java.util.Map;
0050: import java.util.zip.GZIPOutputStream;
0051:
0052: import javax.imageio.ImageIO;
0053: import javax.swing.AbstractAction;
0054: import javax.swing.Action;
0055: import javax.swing.BorderFactory;
0056: import javax.swing.ImageIcon;
0057: import javax.swing.JApplet;
0058: import javax.swing.JFrame;
0059: import javax.swing.JLabel;
0060: import javax.swing.JOptionPane;
0061: import javax.swing.JPanel;
0062: import javax.swing.JPopupMenu;
0063: import javax.swing.JScrollPane;
0064: import javax.swing.JToolBar;
0065: import javax.swing.SwingUtilities;
0066: import javax.swing.event.UndoableEditEvent;
0067:
0068: import org.apache.commons.httpclient.HttpClient;
0069: import org.apache.commons.httpclient.HttpMethod;
0070: import org.apache.commons.httpclient.HttpStatus;
0071: import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
0072: import org.apache.commons.httpclient.methods.GetMethod;
0073: import org.apache.commons.httpclient.methods.PostMethod;
0074: import org.jgraph.JGraph;
0075: import org.jgraph.event.GraphModelEvent;
0076: import org.jgraph.event.GraphModelListener;
0077: import org.jgraph.event.GraphSelectionEvent;
0078: import org.jgraph.event.GraphSelectionListener;
0079: import org.jgraph.graph.BasicMarqueeHandler;
0080: import org.jgraph.graph.CellHandle;
0081: import org.jgraph.graph.CellView;
0082: import org.jgraph.graph.DefaultCellViewFactory;
0083: import org.jgraph.graph.EdgeView;
0084: import org.jgraph.graph.GraphConstants;
0085: import org.jgraph.graph.GraphContext;
0086: import org.jgraph.graph.GraphLayoutCache;
0087: import org.jgraph.graph.GraphModel;
0088: import org.jgraph.graph.GraphUndoManager;
0089: import org.jgraph.graph.Port;
0090: import org.jgraph.graph.PortView;
0091:
0092: import biz.hammurapi.util.Attributable;
0093:
0094: public class StateMachineApplet extends JApplet implements
0095: GraphSelectionListener, KeyListener {
0096:
0097: private String stateMachineDownloadUrl;
0098: private String stateMachineUploadUrl = "bred sobachii";
0099: private String cookie;
0100:
0101: // JGraph instance
0102: protected JGraph graph;
0103:
0104: // Undo Manager
0105: protected GraphUndoManager undoManager;
0106:
0107: // Actions which Change State
0108: // protected Action undo;
0109: // protected Action redo;
0110: protected Action delete;
0111: // protected Action group;
0112: // protected Action ungroup;
0113: // protected Action tofront;
0114: // protected Action toback;
0115: // protected Action cut;
0116: // protected Action copy;
0117: // protected Action paste;
0118: private Action saveAction;
0119:
0120: // Status Bar
0121: protected EdStatusBar statusBar;
0122:
0123: private StateMachine stateMachine;
0124:
0125: //
0126: // Main
0127: //
0128:
0129: // Main Method
0130: public static void main(String[] args) {
0131: // Construct Frame
0132: JFrame frame = new JFrame("State machine editor");
0133: StateMachineApplet stateMachineApplet = new StateMachineApplet();
0134: stateMachineApplet.start();
0135: // Set Close Operation to Exit
0136: // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
0137: // Add an Editor Panel
0138: frame.getContentPane().add(stateMachineApplet);
0139: // Fetch URL to Icon Resource
0140: URL jgraphUrl = StateMachineApplet.class
0141: .getClassLoader()
0142: .getResource(
0143: "biz/hammurapi/web/interaction/resources/jgraph.gif");
0144: // If Valid URL
0145: if (jgraphUrl != null) {
0146: // Load Icon
0147: ImageIcon jgraphIcon = new ImageIcon(jgraphUrl);
0148: // Use in Window
0149: frame.setIconImage(jgraphIcon.getImage());
0150: }
0151: // Set Default Size
0152: frame.setSize(520, 390);
0153: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
0154: // Show Frame
0155: frame.setVisible(true);
0156: }
0157:
0158: //
0159: // Editor Panel
0160: //
0161:
0162: // Construct an Editor Panel
0163: public StateMachineApplet() {
0164: // Construct the Graph
0165: StateMachineModel theModel = new StateMachineModel();
0166: graph = new MyGraph(theModel);
0167: graph.getGraphLayoutCache().setFactory(
0168: new DefaultCellViewFactory() {
0169:
0170: // Override Superclass Method to Return Custom EdgeView
0171: protected EdgeView createEdgeView(Object cell) {
0172:
0173: // Return Custom EdgeView
0174: return new EdgeView(cell) {
0175:
0176: /**
0177: * Returns a cell handle for the view.
0178: */
0179: public CellHandle getHandle(
0180: GraphContext context) {
0181: return new MyEdgeHandle(this , context);
0182: }
0183:
0184: };
0185: }
0186: });
0187:
0188: // Use a Custom Marquee Handler
0189: graph.setMarqueeHandler(new MyMarqueeHandler());
0190: // Construct Command History
0191: //
0192: // Create a GraphUndoManager which also Updates the ToolBar
0193: undoManager = new GraphUndoManager() {
0194: // Override Superclass
0195: public void undoableEditHappened(UndoableEditEvent e) {
0196: // First Invoke Superclass
0197: super .undoableEditHappened(e);
0198: // Then Update Undo/Redo Buttons
0199: updateHistoryButtons();
0200: }
0201: };
0202: // Use Border Layout
0203: getContentPane().setLayout(new BorderLayout());
0204: // Add a ToolBar
0205: getContentPane().add(createToolBar(), BorderLayout.NORTH);
0206: // Add the Graph as Center Component
0207: getContentPane().add(new JScrollPane(graph),
0208: BorderLayout.CENTER);
0209: statusBar = new EdStatusBar();
0210: getContentPane().add(statusBar, BorderLayout.SOUTH);
0211:
0212: /**********************************************************************
0213: * GraphModelChange explaination
0214: *********************************************************************/
0215: // Uncomment out the code below if you want to see the model change outputs
0216: // the new attributes are obtained from getPreviousAttributes(). The
0217: // mode change is the undo edit of the change. That is, if you undo,
0218: // that change edit is executed directly. So the previous attributes
0219: // are the attributes before the undo and the attributes are the
0220: // attributes after the undo. But when listening to a change,
0221: // getPreviousAttributes() returns the new attributes and
0222: // getAttributes() the old attributes.
0223: // graph.getModel().addGraphModelListener(new GraphModelListener() {
0224: // public void graphChanged(GraphModelEvent e) {
0225: // GraphModelEvent.GraphModelChange c = e.getChange();
0226: // if (c.getRemoved() == null && c.getInserted() == null) {
0227: // Map previousAttributes = c.getPreviousAttributes();
0228: // Set keySet = previousAttributes.keySet();
0229: // Iterator iter = keySet.iterator();
0230: // while (iter.hasNext()) {
0231: // Object attribute = iter.next();
0232: // System.out.println("Prev Key " + String.valueOf(attribute));
0233: // Object value = c.getPreviousAttributes().get(attribute);
0234: // System.out.println("\t" + String.valueOf(value));
0235: // }
0236: // Map attributes = c.getAttributes();
0237: // keySet = attributes.keySet();
0238: // iter = keySet.iterator();
0239: // while (iter.hasNext()) {
0240: // Object attribute = iter.next();
0241: // System.out.println("Curr Key " + String.valueOf(attribute));
0242: // Object value = c.getAttributes().get(attribute);
0243: // System.out.println("\t" + String.valueOf(value));
0244: // }
0245: // System.out.println("\n\n");
0246: // }
0247: // }
0248: // });
0249: // Add Listeners to Graph
0250: //
0251: // Register UndoManager with the Model
0252: graph.getModel().addUndoableEditListener(undoManager);
0253: // Update ToolBar based on Selection Changes
0254: graph.getSelectionModel().addGraphSelectionListener(this );
0255: // Listen for Delete Keystroke when the Graph has Focus
0256: graph.addKeyListener(this );
0257: graph.getModel().addGraphModelListener(statusBar);
0258:
0259: graph.setAntiAliased(true);
0260: }
0261:
0262: // Hook for subclassers
0263: protected void uninstallListeners(JGraph graph) {
0264: graph.getModel().removeUndoableEditListener(undoManager);
0265: graph.getSelectionModel().removeGraphSelectionListener(this );
0266: graph.removeKeyListener(this );
0267: graph.getModel().removeGraphModelListener(statusBar);
0268: }
0269:
0270: // Insert a new Vertex at point
0271: public void insert(Point2D point) {
0272: insert(point, stateMachine.createState());
0273: }
0274:
0275: public void insert(Point2D point, State state) {
0276: // Add input and output ports
0277: state
0278: .addPort(new Point2D.Double(
0279: GraphConstants.PERMILLE / 2, 0));
0280: state.addPort(new Point2D.Double(GraphConstants.PERMILLE / 2,
0281: GraphConstants.PERMILLE));
0282: // Create a Map that holds the attributes for the Vertex
0283: Map cellAttributes = new Hashtable();
0284: // Snap the Point to the Grid
0285: point = graph.snap((Point2D) point.clone());
0286:
0287: // Add a Bounds Attribute to the Map
0288: GraphConstants.setBounds(cellAttributes,
0289: new Rectangle2D.Double(point.getX(), point.getY(), 40,
0290: 20));
0291:
0292: // Make sure the cell is resized on insert
0293: // Add a nice looking gradient background
0294: // GraphConstants.setGradientColor(cellAttributes, new Color(200, 255, 200));
0295: // Add a Border Color Attribute to the Map
0296: GraphConstants.setBorderColor(cellAttributes, Color.BLACK);
0297: //GraphConstants.setBorder(cellAttributes, BorderFactory.createLineBorder(Color.RED, 3));
0298:
0299: // Add a White Background
0300: GraphConstants.setBackground(cellAttributes, new Color(252,
0301: 242, 227));
0302: // Make Vertex Opaque
0303: GraphConstants.setOpaque(cellAttributes, true);
0304:
0305: //GraphConstants.setConstrained(map, true);
0306: GraphConstants.setAutoSize(cellAttributes, true);
0307: //GraphConstants.setResize(cellAttributes, true);
0308:
0309: GraphConstants.setInset(cellAttributes, 5);
0310:
0311: Font font = GraphConstants.DEFAULTFONT;
0312: GraphConstants.setFont(cellAttributes, font.deriveFont(
0313: Font.PLAIN, 10));
0314:
0315: //GraphConstants.setBeginSize(cellAttributes, 5);
0316:
0317: state.getAttributes().applyMap(cellAttributes);
0318: // Insert the Vertex (including child port and attributes)
0319: graph.getGraphLayoutCache().insert(state);
0320: }
0321:
0322: // Insert a new Edge between source and target
0323: public void connect(Port source, Port target) {
0324: connect(stateMachine.createTransition(), source, target, null);
0325: }
0326:
0327: public void connect(Transition edge, Port source, Port target,
0328: List points) {
0329: if (graph.getModel().acceptsSource(edge, source)
0330: && graph.getModel().acceptsTarget(edge, target)) {
0331: // Create a Map thath holds the attributes for the edge
0332: Map edgeAttributes = new Hashtable();
0333: // Add a Line End Attribute
0334: GraphConstants.setLineEnd(edgeAttributes,
0335: GraphConstants.ARROW_SIMPLE);
0336: // Add a label along edge attribute
0337: GraphConstants.setLabelAlongEdge(edgeAttributes, true);
0338: GraphConstants.setBendable(edgeAttributes, true);
0339: GraphConstants.setLineWidth(edgeAttributes, 2);
0340: //GraphConstants.setLineStyle(edgeAttributes, GraphConstants.STYLE_SPLINE);
0341: GraphConstants.setDisconnectable(edgeAttributes, true);
0342:
0343: edge.getAttributes().applyMap(edgeAttributes);
0344: // Insert the Edge and its Attributes
0345: graph.getGraphLayoutCache()
0346: .insertEdge(edge, source, target);
0347:
0348: if (points != null) {
0349: List allPoints = new ArrayList();
0350: allPoints.add(source);
0351: allPoints.addAll(points);
0352: allPoints.add(target);
0353: Map newAttributes = new Hashtable();
0354: GraphConstants.setPoints(newAttributes, allPoints);
0355: // edge.getAttributes().applyMap(newAttributes);
0356: // graph.getGraphLayoutCache().reload();
0357: graph.getGraphLayoutCache().editCell(edge,
0358: newAttributes);
0359: }
0360: } else {
0361: stateMachine.removeTransition(edge);
0362: }
0363: graph.repaint();
0364: }
0365:
0366: // // Ungroup the Groups in Cells and Select the Children
0367: // public void ungroup(Object[] cells) {
0368: // graph.getGraphLayoutCache().ungroup(cells);
0369: // }
0370:
0371: // Determines if a Cell is a Group
0372: public boolean isGroup(Object cell) {
0373: // Map the Cell to its View
0374: CellView view = graph.getGraphLayoutCache().getMapping(cell,
0375: false);
0376: if (view != null)
0377: return !view.isLeaf();
0378: return false;
0379: }
0380:
0381: // Brings the Specified Cells to Front
0382: public void toFront(Object[] c) {
0383: graph.getGraphLayoutCache().toFront(c);
0384: }
0385:
0386: // Sends the Specified Cells to Back
0387: public void toBack(Object[] c) {
0388: graph.getGraphLayoutCache().toBack(c);
0389: }
0390:
0391: // Undo the last Change to the Model or the View
0392: public void undo() {
0393: try {
0394: undoManager.undo(graph.getGraphLayoutCache());
0395: } catch (Exception ex) {
0396: System.err.println(ex);
0397: } finally {
0398: updateHistoryButtons();
0399: }
0400: }
0401:
0402: // Redo the last Change to the Model or the View
0403: public void redo() {
0404: try {
0405: undoManager.redo(graph.getGraphLayoutCache());
0406: } catch (Exception ex) {
0407: System.err.println(ex);
0408: } finally {
0409: updateHistoryButtons();
0410: }
0411: }
0412:
0413: // Update Undo/Redo Button State based on Undo Manager
0414: protected void updateHistoryButtons() {
0415: // The View Argument Defines the Context
0416: // undo.setEnabled(undoManager.canUndo(graph.getGraphLayoutCache()));
0417: // redo.setEnabled(undoManager.canRedo(graph.getGraphLayoutCache()));
0418: }
0419:
0420: //
0421: // Listeners
0422: //
0423:
0424: // From GraphSelectionListener Interface
0425: public void valueChanged(GraphSelectionEvent e) {
0426: // Group Button only Enabled if more than One Cell Selected
0427: // group.setEnabled(graph.getSelectionCount() > 1);
0428: // Update Button States based on Current Selection
0429: boolean enabled = !graph.isSelectionEmpty();
0430: delete.setEnabled(enabled);
0431: // ungroup.setEnabled(enabled);
0432: // tofront.setEnabled(enabled);
0433: // toback.setEnabled(enabled);
0434: // copy.setEnabled(enabled);
0435: // cut.setEnabled(enabled);
0436: }
0437:
0438: //
0439: // KeyListener for Delete KeyStroke
0440: //
0441: public void keyReleased(KeyEvent e) {
0442: }
0443:
0444: public void keyTyped(KeyEvent e) {
0445: }
0446:
0447: public void keyPressed(KeyEvent e) {
0448: // Listen for Delete Key Press
0449: if (e.getKeyCode() == KeyEvent.VK_DELETE)
0450: // Execute Remove Action on Delete Key Press
0451: delete.actionPerformed(null);
0452: }
0453:
0454: //
0455: // Custom Graph
0456: //
0457:
0458: // Defines a Graph that uses the Shift-Button (Instead of the Right
0459: // Mouse Button, which is Default) to add/remove point to/from an edge.
0460: public static class MyGraph extends JGraph {
0461:
0462: // Construct the Graph using the Model as its Data Source
0463: public MyGraph(GraphModel model) {
0464: this (model, null);
0465: }
0466:
0467: // Construct the Graph using the Model as its Data Source
0468: public MyGraph(GraphModel model, GraphLayoutCache cache) {
0469: super (model, cache);
0470: // Make Ports Visible by Default
0471: setPortsVisible(true);
0472: // Use the Grid (but don't make it Visible)
0473: setGridEnabled(true);
0474: // Set the Grid Size to 10 Pixel
0475: setGridSize(6);
0476: // Set the Tolerance to 2 Pixel
0477: setTolerance(2);
0478: // Accept edits if click on background
0479: setInvokesStopCellEditing(true);
0480: // Allows control-drag
0481: setCloneable(false);
0482: // Jump to default port on connect
0483: setJumpToDefaultPort(true);
0484: }
0485:
0486: }
0487:
0488: //
0489: // Custom Edge Handle
0490: //
0491:
0492: // Defines a EdgeHandle that uses the Shift-Button (Instead of the Right
0493: // Mouse Button, which is Default) to add/remove point to/from an edge.
0494: public static class MyEdgeHandle extends EdgeView.EdgeHandle {
0495:
0496: /**
0497: * @param edge
0498: * @param ctx
0499: */
0500: public MyEdgeHandle(EdgeView edge, GraphContext ctx) {
0501: super (edge, ctx);
0502: }
0503:
0504: // Override Superclass Method
0505: public boolean isAddPointEvent(MouseEvent event) {
0506: // Points are Added using Shift-Click
0507: return event.isShiftDown();
0508: }
0509:
0510: // Override Superclass Method
0511: public boolean isRemovePointEvent(MouseEvent event) {
0512: // Points are Removed using Shift-Click
0513: return event.isShiftDown();
0514: }
0515:
0516: }
0517:
0518: //
0519: // Custom Model
0520: //
0521:
0522: // MarqueeHandler that Connects Vertices and Displays PopupMenus
0523: public class MyMarqueeHandler extends BasicMarqueeHandler {
0524:
0525: // Holds the Start and the Current Point
0526: protected Point2D start, current;
0527:
0528: // Holds the First and the Current Port
0529: protected PortView port, firstPort;
0530:
0531: // Override to Gain Control (for PopupMenu and ConnectMode)
0532: public boolean isForceMarqueeEvent(MouseEvent e) {
0533: if (e.isShiftDown())
0534: return false;
0535: // If Right Mouse Button we want to Display the PopupMenu
0536: if (SwingUtilities.isRightMouseButton(e))
0537: // Return Immediately
0538: return true;
0539: // Find and Remember Port
0540: port = getSourcePortAt(e.getPoint());
0541: // If Port Found and in ConnectMode (=Ports Visible)
0542: if (port != null && graph.isPortsVisible())
0543: return true;
0544: // Else Call Superclass
0545: return super .isForceMarqueeEvent(e);
0546: }
0547:
0548: // Display PopupMenu or Remember Start Location and First Port
0549: public void mousePressed(final MouseEvent e) {
0550: // If Right Mouse Button
0551: if (SwingUtilities.isRightMouseButton(e)) {
0552: // Find Cell in Model Coordinates
0553: Object cell = graph.getFirstCellForLocation(e.getX(), e
0554: .getY());
0555: // Create PopupMenu for the Cell
0556: JPopupMenu menu = createPopupMenu(e.getPoint(), cell);
0557: // Display PopupMenu
0558: menu.show(graph, e.getX(), e.getY());
0559: // Else if in ConnectMode and Remembered Port is Valid
0560: } else if (port != null && graph.isPortsVisible()) {
0561: // Remember Start Location
0562: start = graph.toScreen(port.getLocation());
0563: // Remember First Port
0564: firstPort = port;
0565: } else {
0566: // Call Superclass
0567: super .mousePressed(e);
0568: }
0569: }
0570:
0571: // Find Port under Mouse and Repaint Connector
0572: public void mouseDragged(MouseEvent e) {
0573: // If remembered Start Point is Valid
0574: if (start != null) {
0575: // Fetch Graphics from Graph
0576: Graphics g = graph.getGraphics();
0577: // Reset Remembered Port
0578: PortView newPort = getTargetPortAt(e.getPoint());
0579: // Do not flicker (repaint only on real changes)
0580: if (newPort == null || newPort != port) {
0581: // Xor-Paint the old Connector (Hide old Connector)
0582: paintConnector(Color.black, graph.getBackground(),
0583: g);
0584: // If Port was found then Point to Port Location
0585: port = newPort;
0586: if (port != null)
0587: current = graph.toScreen(port.getLocation());
0588: // Else If no Port was found then Point to Mouse Location
0589: else
0590: current = graph.snap(e.getPoint());
0591: // Xor-Paint the new Connector
0592: paintConnector(graph.getBackground(), Color.black,
0593: g);
0594: }
0595: }
0596: // Call Superclass
0597: super .mouseDragged(e);
0598: }
0599:
0600: public PortView getSourcePortAt(Point2D point) {
0601: // Disable jumping
0602: graph.setJumpToDefaultPort(false);
0603: PortView result;
0604: try {
0605: // Find a Port View in Model Coordinates and Remember
0606: result = graph
0607: .getPortViewAt(point.getX(), point.getY());
0608: } finally {
0609: graph.setJumpToDefaultPort(true);
0610: }
0611: return result;
0612: }
0613:
0614: // Find a Cell at point and Return its first Port as a PortView
0615: protected PortView getTargetPortAt(Point2D point) {
0616: // Find a Port View in Model Coordinates and Remember
0617: return graph.getPortViewAt(point.getX(), point.getY());
0618: }
0619:
0620: // Connect the First Port and the Current Port in the Graph or Repaint
0621: public void mouseReleased(MouseEvent e) {
0622: // If Valid Event, Current and First Port
0623: if (e != null && port != null && firstPort != null
0624: && firstPort != port) {
0625: // Then Establish Connection
0626: connect((Port) firstPort.getCell(), (Port) port
0627: .getCell());
0628: e.consume();
0629: // Else Repaint the Graph
0630: } else
0631: graph.repaint();
0632: // Reset Global Vars
0633: firstPort = port = null;
0634: start = current = null;
0635: // Call Superclass
0636: super .mouseReleased(e);
0637: }
0638:
0639: // Show Special Cursor if Over Port
0640: public void mouseMoved(MouseEvent e) {
0641: // Check Mode and Find Port
0642: if (e != null && getSourcePortAt(e.getPoint()) != null
0643: && graph.isPortsVisible()) {
0644: // Set Cusor on Graph (Automatically Reset)
0645: graph.setCursor(new Cursor(Cursor.HAND_CURSOR));
0646: // Consume Event
0647: // Note: This is to signal the BasicGraphUI's
0648: // MouseHandle to stop further event stateMachineing.
0649: e.consume();
0650: } else
0651: // Call Superclass
0652: super .mouseMoved(e);
0653: }
0654:
0655: // Use Xor-Mode on Graphics to Paint Connector
0656: protected void paintConnector(Color fg, Color bg, Graphics g) {
0657: // Set Foreground
0658: g.setColor(fg);
0659: // Set Xor-Mode Color
0660: g.setXORMode(bg);
0661: // Highlight the Current Port
0662: paintPort(graph.getGraphics());
0663: // If Valid First Port, Start and Current Point
0664: if (firstPort != null && start != null && current != null)
0665: // Then Draw A Line From Start to Current Point
0666: g.drawLine((int) start.getX(), (int) start.getY(),
0667: (int) current.getX(), (int) current.getY());
0668: }
0669:
0670: // Use the Preview Flag to Draw a Highlighted Port
0671: protected void paintPort(Graphics g) {
0672: // If Current Port is Valid
0673: if (port != null) {
0674: // If Not Floating Port...
0675: boolean o = (GraphConstants.getOffset(port
0676: .getAllAttributes()) != null);
0677: // ...Then use Parent's Bounds
0678: Rectangle2D r = (o) ? port.getBounds() : port
0679: .getParentView().getBounds();
0680: // Scale from Model to Screen
0681: r = graph.toScreen((Rectangle2D) r.clone());
0682: // Add Space For the Highlight Border
0683: r.setFrame(r.getX() - 3, r.getY() - 3,
0684: r.getWidth() + 6, r.getHeight() + 6);
0685: // Paint Port in Preview (=Highlight) Mode
0686: graph.getUI().paintCell(g, port, r, true);
0687: }
0688: }
0689:
0690: } // End of Editor.MyMarqueeHandler
0691:
0692: //
0693: //
0694: //
0695:
0696: //
0697: // PopupMenu and ToolBar
0698: //
0699:
0700: //
0701: //
0702: //
0703:
0704: //
0705: // PopupMenu
0706: //
0707: public JPopupMenu createPopupMenu(final Point pt, final Object cell) {
0708: JPopupMenu menu = new JPopupMenu();
0709: if (cell != null) {
0710: // Edit
0711: menu.add(new AbstractAction("Edit") {
0712: public void actionPerformed(ActionEvent e) {
0713: Container owner = graph;
0714: while (owner != null && !(owner instanceof JFrame)) {
0715: owner = owner.getParent();
0716: }
0717:
0718: if (cell instanceof State) {
0719: StatePropertiesDialog pd = new StatePropertiesDialog(
0720: owner instanceof JFrame ? (JFrame) owner
0721: : null, (State) cell);
0722: pd.setVisible(true);
0723: } else if (cell instanceof Transition) {
0724: TransitionPropertiesDialog pd = new TransitionPropertiesDialog(
0725: owner instanceof JFrame ? (JFrame) owner
0726: : null, (Transition) cell);
0727: pd.setVisible(true);
0728: }
0729:
0730: GraphLayoutCache graphLayoutCache = graph
0731: .getGraphLayoutCache();
0732: CellView cellView = graphLayoutCache.getMapping(
0733: cell, false);
0734: cellView.update(graphLayoutCache);
0735: // Map attributes = cellView.getAllAttributes();
0736: // Iterator it=attributes.entrySet().iterator();
0737: // while (it.hasNext()) {
0738: // System.out.println(it.next());
0739: // }
0740: graph.updateAutoSize(cellView);
0741: }
0742: });
0743: }
0744: // Remove
0745: if (!graph.isSelectionEmpty()) {
0746: menu.addSeparator();
0747: menu.add(new AbstractAction("Delete") {
0748: public void actionPerformed(ActionEvent e) {
0749: delete.actionPerformed(e);
0750: }
0751: });
0752: }
0753: menu.addSeparator();
0754: // Insert
0755: menu.add(new AbstractAction("Insert") {
0756: public void actionPerformed(ActionEvent ev) {
0757: insert(pt);
0758: }
0759: });
0760: return menu;
0761: }
0762:
0763: // Time when applet was loaded to generate unique id.
0764: private long loadTime = System.currentTimeMillis();
0765: // Applet ID.
0766: private String aid;
0767:
0768: //
0769: // ToolBar
0770: //
0771: public JToolBar createToolBar() {
0772: JToolBar toolbar = new JToolBar();
0773: toolbar.setFloatable(false);
0774:
0775: // Insert
0776: URL insertUrl = getClass().getClassLoader().getResource(
0777: "biz/hammurapi/web/interaction/resources/insert.gif");
0778: ImageIcon insertIcon = new ImageIcon(insertUrl);
0779: toolbar.add(new AbstractAction("Insert state", insertIcon) {
0780: public void actionPerformed(ActionEvent e) {
0781: insert(new Point(20, 20)); // TODO - iterate over cells to avoid inserting state over other states.
0782: }
0783: });
0784:
0785: // Toggle Connect Mode
0786: URL connectUrl = getClass()
0787: .getClassLoader()
0788: .getResource(
0789: "biz/hammurapi/web/interaction/resources/connecton.gif");
0790: ImageIcon connectIcon = new ImageIcon(connectUrl);
0791: toolbar.add(new AbstractAction("Toggle connect mode",
0792: connectIcon) {
0793: public void actionPerformed(ActionEvent e) {
0794: graph.setPortsVisible(!graph.isPortsVisible());
0795: URL connectUrl;
0796: if (graph.isPortsVisible())
0797: connectUrl = getClass()
0798: .getClassLoader()
0799: .getResource(
0800: "biz/hammurapi/web/interaction/resources/connectoff.gif");
0801: else
0802: connectUrl = getClass()
0803: .getClassLoader()
0804: .getResource(
0805: "biz/hammurapi/web/interaction/resources/connecton.gif");
0806: ImageIcon connectIcon = new ImageIcon(connectUrl);
0807: putValue(SMALL_ICON, connectIcon);
0808: }
0809: });
0810:
0811: toolbar.addSeparator();
0812:
0813: URL propertiesUrl = getClass()
0814: .getClassLoader()
0815: .getResource(
0816: "biz/hammurapi/web/interaction/resources/properties.gif");
0817: ImageIcon propertiesIcon = new ImageIcon(propertiesUrl);
0818: toolbar.add(new AbstractAction("Properties", propertiesIcon) {
0819: public void actionPerformed(ActionEvent e) {
0820: showProperties();
0821: }
0822: });
0823:
0824: URL saveUrl = getClass().getClassLoader().getResource(
0825: "biz/hammurapi/web/interaction/resources/save.gif");
0826: ImageIcon saveIcon = new ImageIcon(saveUrl);
0827: saveAction = new AbstractAction("Save", saveIcon) {
0828: public void actionPerformed(ActionEvent e) {
0829: if (aid == null) {
0830: aid = Long.toHexString(loadTime)
0831: + ":"
0832: + Long.toHexString(System
0833: .currentTimeMillis());
0834: }
0835: if (stateMachine.getData().getName() == null
0836: || stateMachine.getData().getName().trim()
0837: .length() == 0) {
0838: JOptionPane.showMessageDialog(
0839: StateMachineApplet.this ,
0840: "StateMachine name shall not be blank",
0841: "Warning", JOptionPane.WARNING_MESSAGE);
0842: showProperties();
0843: return;
0844: }
0845: setEnabled(false);
0846: statusBar.getLeftSideStatus().setText(
0847: "StateMachine editor. Saving...");
0848: stateMachine.storeGeometry();
0849: try {
0850: ByteArrayOutputStream out = new ByteArrayOutputStream();
0851: Color bg = graph.getBackground();
0852: BufferedImage img = graph.getImage(bg, 5);
0853: if (img != null) {
0854: ImageIO.write(img, "PNG", out);
0855: out.flush();
0856: out.close();
0857: stateMachine.getData().setStateMachineImage(
0858: out.toByteArray());
0859: stateMachine.getData()
0860: .setImageType("image/png");
0861: }
0862:
0863: ((Attributable) stateMachine.getData())
0864: .setAttribute("create-id", aid);
0865:
0866: ByteArrayOutputStream baos = new ByteArrayOutputStream();
0867: GZIPOutputStream gzos = new GZIPOutputStream(baos);
0868: ObjectOutputStream oos = new ObjectOutputStream(
0869: gzos);
0870: if (stateMachineDownloadUrl == null) {
0871: ((Attributable) stateMachine.getData())
0872: .setAttribute("new", Boolean.TRUE);
0873: }
0874: oos.writeObject(stateMachine.getData());
0875: oos.close();
0876: gzos.close();
0877: baos.close();
0878: HttpClient client = new HttpClient();
0879: PostMethod method = new PostMethod(
0880: stateMachineUploadUrl);
0881: method.setRequestEntity(new ByteArrayRequestEntity(
0882: baos.toByteArray()));
0883: if (cookie != null) {
0884: method.setRequestHeader("Cookie", cookie);
0885: }
0886: int status = client.executeMethod(method);
0887: if (status != HttpStatus.SC_OK) {
0888: String errorMessage = "HTTP Error: " + status
0889: + "\n"
0890: + method.getResponseBodyAsString();
0891: JOptionPane.showMessageDialog(
0892: StateMachineApplet.this ,
0893: "Could not save stateMachine:\n"
0894: + errorMessage, "Error",
0895: JOptionPane.ERROR_MESSAGE);
0896: statusBar.getLeftSideStatus().setText(
0897: "StateMachine editor. Save failed: "
0898: + errorMessage);
0899: setEnabled(true);
0900: }
0901: statusBar.getLeftSideStatus().setText(
0902: "StateMachine editor. Last saved on "
0903: + new Date());
0904: } catch (Exception ex) {
0905: JOptionPane.showMessageDialog(
0906: StateMachineApplet.this ,
0907: "Could not save stateMachine:\n" + ex,
0908: "Error", JOptionPane.ERROR_MESSAGE);
0909: ex.printStackTrace();
0910: statusBar.getLeftSideStatus().setText(
0911: "StateMachine editor. Save failed: " + ex);
0912: setEnabled(true);
0913: } catch (Error er) {
0914: JOptionPane.showMessageDialog(
0915: StateMachineApplet.this ,
0916: "Could not save stateMachine:\n" + er,
0917: "Error", JOptionPane.ERROR_MESSAGE);
0918: statusBar.getLeftSideStatus().setText(
0919: "StateMachine editor. Save failed: " + er);
0920: setEnabled(true);
0921: throw er;
0922: }
0923: }
0924: };
0925: // Save
0926: saveAction.setEnabled(false);
0927: toolbar.add(saveAction);
0928:
0929: toolbar.addSeparator();
0930:
0931: // Undo
0932: // URL undoUrl = getClass().getClassLoader().getResource("biz/hammurapi/web/interaction/resources/undo.gif");
0933: // ImageIcon undoIcon = new ImageIcon(undoUrl);
0934: // undo = new AbstractAction("Undo", undoIcon) {
0935: // public void actionPerformed(ActionEvent e) {
0936: // undo();
0937: // }
0938: // };
0939: // undo.setEnabled(false);
0940: // toolbar.add(undo);
0941: //
0942: // // Redo
0943: // URL redoUrl = getClass().getClassLoader().getResource("biz/hammurapi/web/interaction/resources/redo.gif");
0944: // ImageIcon redoIcon = new ImageIcon(redoUrl);
0945: // redo = new AbstractAction("Redo", redoIcon) {
0946: // public void actionPerformed(ActionEvent e) {
0947: // redo();
0948: // }
0949: // };
0950: // redo.setEnabled(false);
0951: // toolbar.add(redo);
0952: //
0953: // //
0954: // // Edit Block
0955: // //
0956: // toolbar.addSeparator();
0957: // Action action;
0958: // URL url;
0959: //
0960: // // Copy
0961: // action = javax.swing.TransferHandler // JAVA13:
0962: // // org.jgraph.plaf.basic.TransferHandler
0963: // .getCopyAction();
0964: // url = getClass().getClassLoader().getResource("biz/hammurapi/web/interaction/resources/copy.gif");
0965: // action.putValue(Action.SMALL_ICON, new ImageIcon(url));
0966: // toolbar.add(copy = new EventRedirector(action));
0967: //
0968: // // Paste
0969: // action = javax.swing.TransferHandler // JAVA13:
0970: // // org.jgraph.plaf.basic.TransferHandler
0971: // .getPasteAction();
0972: // url = getClass().getClassLoader().getResource("biz/hammurapi/web/interaction/resources/paste.gif");
0973: // action.putValue(Action.SMALL_ICON, new ImageIcon(url));
0974: // toolbar.add(paste = new EventRedirector(action));
0975: //
0976: // // Cut
0977: // action = javax.swing.TransferHandler // JAVA13:
0978: // // org.jgraph.plaf.basic.TransferHandler
0979: // .getCutAction();
0980: // url = getClass().getClassLoader().getResource(
0981: // "biz/hammurapi/web/interaction/resources/cut.gif");
0982: // action.putValue(Action.SMALL_ICON, new ImageIcon(url));
0983: // toolbar.add(cut = new EventRedirector(action));
0984:
0985: // Remove
0986: URL removeUrl = getClass().getClassLoader().getResource(
0987: "biz/hammurapi/web/interaction/resources/delete.gif");
0988: ImageIcon removeIcon = new ImageIcon(removeUrl);
0989: delete = new AbstractAction("Deletes selected object(s)",
0990: removeIcon) {
0991: public void actionPerformed(ActionEvent e) {
0992: if (!graph.isSelectionEmpty()
0993: && JOptionPane.showConfirmDialog(
0994: StateMachineApplet.this ,
0995: "Delete object(s)?", "Confirm delete",
0996: JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
0997: Object[] cells = graph.getSelectionCells();
0998: cells = graph.getDescendants(cells);
0999: graph.getModel().remove(cells);
1000: }
1001: }
1002: };
1003: delete.setEnabled(false);
1004: toolbar.add(delete);
1005:
1006: // // To Front
1007: // toolbar.addSeparator();
1008: // URL toFrontUrl = getClass().getClassLoader().getResource(
1009: // "biz/hammurapi/web/interaction/resources/tofront.gif");
1010: // ImageIcon toFrontIcon = new ImageIcon(toFrontUrl);
1011: // tofront = new AbstractAction("", toFrontIcon) {
1012: // public void actionPerformed(ActionEvent e) {
1013: // if (!graph.isSelectionEmpty())
1014: // toFront(graph.getSelectionCells());
1015: // }
1016: // };
1017: // tofront.setEnabled(false);
1018: // toolbar.add(tofront);
1019: //
1020: // // To Back
1021: // URL toBackUrl = getClass().getClassLoader().getResource(
1022: // "biz/hammurapi/web/interaction/resources/toback.gif");
1023: // ImageIcon toBackIcon = new ImageIcon(toBackUrl);
1024: // toback = new AbstractAction("", toBackIcon) {
1025: // public void actionPerformed(ActionEvent e) {
1026: // if (!graph.isSelectionEmpty())
1027: // toBack(graph.getSelectionCells());
1028: // }
1029: // };
1030: // toback.setEnabled(false);
1031: // toolbar.add(toback);
1032:
1033: // Zoom Std
1034: toolbar.addSeparator();
1035: URL zoomUrl = getClass().getClassLoader().getResource(
1036: "biz/hammurapi/web/interaction/resources/zoom.gif");
1037: ImageIcon zoomIcon = new ImageIcon(zoomUrl);
1038: toolbar.add(new AbstractAction("Zoom", zoomIcon) {
1039: public void actionPerformed(ActionEvent e) {
1040: graph.setScale(1.0);
1041: }
1042: });
1043: // Zoom In
1044: URL zoomInUrl = getClass().getClassLoader().getResource(
1045: "biz/hammurapi/web/interaction/resources/zoomin.gif");
1046: ImageIcon zoomInIcon = new ImageIcon(zoomInUrl);
1047: toolbar.add(new AbstractAction("Zoom in", zoomInIcon) {
1048: public void actionPerformed(ActionEvent e) {
1049: graph.setScale(1.5 * graph.getScale());
1050: }
1051: });
1052: // Zoom Out
1053: URL zoomOutUrl = getClass().getClassLoader().getResource(
1054: "biz/hammurapi/web/interaction/resources/zoomout.gif");
1055: ImageIcon zoomOutIcon = new ImageIcon(zoomOutUrl);
1056: toolbar.add(new AbstractAction("Zoom out", zoomOutIcon) {
1057: public void actionPerformed(ActionEvent e) {
1058: graph.setScale(graph.getScale() / 1.5);
1059: }
1060: });
1061:
1062: // // Group
1063: // toolbar.addSeparator();
1064: // URL groupUrl = getClass().getClassLoader().getResource("biz/hammurapi/web/interaction/resources/group.gif");
1065: // ImageIcon groupIcon = new ImageIcon(groupUrl);
1066: // group = new AbstractAction("Group", groupIcon) {
1067: // public void actionPerformed(ActionEvent e) {
1068: // group(graph.getSelectionCells());
1069: // }
1070: // };
1071: // group.setEnabled(false);
1072: // toolbar.add(group);
1073: //
1074: // // Ungroup
1075: // URL ungroupUrl = getClass().getClassLoader().getResource("biz/hammurapi/web/interaction/resources/ungroup.gif");
1076: // ImageIcon ungroupIcon = new ImageIcon(ungroupUrl);
1077: // ungroup = new AbstractAction("Ungroup", ungroupIcon) {
1078: // public void actionPerformed(ActionEvent e) {
1079: // ungroup(graph.getSelectionCells());
1080: // }
1081: // };
1082: // ungroup.setEnabled(false);
1083: // toolbar.add(ungroup);
1084:
1085: return toolbar;
1086: }
1087:
1088: /**
1089: * @return Returns the graph.
1090: */
1091: public JGraph getGraph() {
1092: return graph;
1093: }
1094:
1095: /**
1096: * @param graph
1097: * The graph to set.
1098: */
1099: public void setGraph(JGraph graph) {
1100: this .graph = graph;
1101: }
1102:
1103: // This will change the source of the actionevent to graph.
1104: public class EventRedirector extends AbstractAction {
1105:
1106: protected Action action;
1107:
1108: // Construct the "Wrapper" Action
1109: public EventRedirector(Action a) {
1110: super ("", (ImageIcon) a.getValue(Action.SMALL_ICON));
1111: this .action = a;
1112: }
1113:
1114: // Redirect the Actionevent
1115: public void actionPerformed(ActionEvent e) {
1116: e = new ActionEvent(graph, e.getID(), e.getActionCommand(),
1117: e.getModifiers());
1118: action.actionPerformed(e);
1119: }
1120: }
1121:
1122: public class EdStatusBar extends JPanel implements
1123: GraphModelListener {
1124: /**
1125: *
1126: */
1127: protected JLabel leftSideStatus;
1128:
1129: /**
1130: * contains the scale for the current graph
1131: */
1132: protected JLabel rightSideStatus;
1133:
1134: /**
1135: * Constructor for GPStatusBar.
1136: *
1137: */
1138: public EdStatusBar() {
1139: super ();
1140: // Add this as graph model change listener
1141: setLayout(new BorderLayout());
1142: leftSideStatus = new JLabel("StateMachine editor");
1143: rightSideStatus = new JLabel("0/0Mb");
1144: leftSideStatus.setBorder(BorderFactory
1145: .createLoweredBevelBorder());
1146: rightSideStatus.setBorder(BorderFactory
1147: .createLoweredBevelBorder());
1148: add(leftSideStatus, BorderLayout.CENTER);
1149: add(rightSideStatus, BorderLayout.EAST);
1150: }
1151:
1152: protected void updateStatusBar() {
1153: Runtime runtime = Runtime.getRuntime();
1154: int freeMemory = (int) (runtime.freeMemory() / 1024);
1155: int totalMemory = (int) (runtime.totalMemory() / 1024);
1156: int usedMemory = (totalMemory - freeMemory);
1157: String str = (usedMemory / 1024) + "/"
1158: + (totalMemory / 1024) + "Mb";
1159: rightSideStatus.setText(str);
1160: }
1161:
1162: /**
1163: * @return Returns the leftSideStatus.
1164: */
1165: public JLabel getLeftSideStatus() {
1166: return leftSideStatus;
1167: }
1168:
1169: /**
1170: * @param leftSideStatus
1171: * The leftSideStatus to set.
1172: */
1173: public void setLeftSideStatus(JLabel leftSideStatus) {
1174: this .leftSideStatus = leftSideStatus;
1175: }
1176:
1177: /**
1178: * @return Returns the rightSideStatus.
1179: */
1180: public JLabel getRightSideStatus() {
1181: return rightSideStatus;
1182: }
1183:
1184: /**
1185: * @param rightSideStatus
1186: * The rightSideStatus to set.
1187: */
1188: public void setRightSideStatus(JLabel rightSideStatus) {
1189: this .rightSideStatus = rightSideStatus;
1190: }
1191:
1192: public void graphChanged(GraphModelEvent changeEvent) {
1193: modelChanged();
1194: }
1195: }
1196:
1197: /**
1198: * Notification that model has changed.
1199: *
1200: */
1201: public void modelChanged() {
1202: saveAction.setEnabled(true);
1203: }
1204:
1205: // /**
1206: // * @return Returns the copy.
1207: // */
1208: // public Action getCopy() {
1209: // return copy;
1210: // }
1211: //
1212: // /**
1213: // * @param copy
1214: // * The copy to set.
1215: // */
1216: // public void setCopy(Action copy) {
1217: // this.copy = copy;
1218: // }
1219: //
1220: // /**
1221: // * @return Returns the cut.
1222: // */
1223: // public Action getCut() {
1224: // return cut;
1225: // }
1226: //
1227: // /**
1228: // * @param cut
1229: // * The cut to set.
1230: // */
1231: // public void setCut(Action cut) {
1232: // this.cut = cut;
1233: // }
1234: //
1235: // /**
1236: // * @return Returns the paste.
1237: // */
1238: // public Action getPaste() {
1239: // return paste;
1240: // }
1241: //
1242: // /**
1243: // * @param paste
1244: // * The paste to set.
1245: // */
1246: // public void setPaste(Action paste) {
1247: // this.paste = paste;
1248: // }
1249: //
1250: // /**
1251: // * @return Returns the toback.
1252: // */
1253: // public Action getToback() {
1254: // return toback;
1255: // }
1256: //
1257: // /**
1258: // * @param toback
1259: // * The toback to set.
1260: // */
1261: // public void setToback(Action toback) {
1262: // this.toback = toback;
1263: // }
1264: //
1265: // /**
1266: // * @return Returns the tofront.
1267: // */
1268: // public Action getTofront() {
1269: // return tofront;
1270: // }
1271: //
1272: // /**
1273: // * @param tofront
1274: // * The tofront to set.
1275: // */
1276: // public void setTofront(Action tofront) {
1277: // this.tofront = tofront;
1278: // }
1279:
1280: /**
1281: * @return Returns the remove.
1282: */
1283: public Action getDelete() {
1284: return delete;
1285: }
1286:
1287: /**
1288: * @param delete
1289: * The remove to set.
1290: */
1291: public void setDelete(Action delete) {
1292: this .delete = delete;
1293: }
1294:
1295: public void start() {
1296: super .init();
1297: try {
1298: // File inFile = new File("stateMachine.bin");
1299: // if (inFile.exists()) {
1300: // FileInputStream fis = new FileInputStream(inFile);
1301: // GZIPInputStream gzis = new GZIPInputStream(fis);
1302: // ObjectInputStream ois = new ObjectInputStream(gzis);
1303: // biz.hammurapi.web.stateMachine.sql.StateMachine data = (biz.hammurapi.web.stateMachine.sql.StateMachine) ois.readObject();
1304: // ois.close();
1305: // gzis.close();
1306: // fis.close();
1307: //
1308: // stateMachine = new StateMachine(data);
1309: // } else {
1310: // stateMachine = new StateMachine();
1311: // }
1312:
1313: if (stateMachineDownloadUrl != null) {
1314: HttpClient httpClient = new HttpClient();
1315: HttpMethod method = new GetMethod(
1316: stateMachineDownloadUrl);
1317: if (cookie != null) {
1318: method.setRequestHeader("Cookie", cookie);
1319: }
1320: int statusCode = httpClient.executeMethod(method);
1321: if (statusCode != HttpStatus.SC_OK) {
1322: JOptionPane.showMessageDialog(
1323: StateMachineApplet.this ,
1324: "Could not load stateMachine from "
1325: + stateMachineDownloadUrl
1326: + ".\nHttp error code: "
1327: + statusCode + "\n"
1328: + method.getResponseBodyAsString(),
1329: "Error", JOptionPane.ERROR_MESSAGE);
1330: stateMachine = new StateMachine();
1331: statusBar.getLeftSideStatus().setText(
1332: "StateMachine editor: Disabled");
1333: setEnabled(false);
1334: } else {
1335: ByteArrayInputStream bis = new ByteArrayInputStream(
1336: method.getResponseBody());
1337: method.releaseConnection();
1338: //GZIPInputStream gzis = new GZIPInputStream(bis);
1339: ObjectInputStream ois = new ObjectInputStream(bis /* gzis */);
1340: biz.hammurapi.web.statemachine.sql.StateMachine data = (biz.hammurapi.web.statemachine.sql.StateMachine) ois
1341: .readObject();
1342: ois.close();
1343: //gzis.close();
1344: bis.close();
1345:
1346: stateMachine = new StateMachine(data);
1347: }
1348: } else {
1349: stateMachine = new StateMachine();
1350: }
1351:
1352: stateMachine.setApplet(this );
1353: ((StateMachineModel) graph.getModel())
1354: .setStateMachine(stateMachine);
1355: if (stateMachineUploadUrl == null) {
1356: statusBar.getLeftSideStatus().setText(
1357: "StateMachine viewer");
1358: setEnabled(false);
1359: }
1360: saveAction.setEnabled(false);
1361: } catch (Exception e) {
1362: e.printStackTrace();
1363: JOptionPane.showMessageDialog(StateMachineApplet.this ,
1364: "Could not load stateMachine:\n" + e, "Error",
1365: JOptionPane.ERROR_MESSAGE);
1366: statusBar.getLeftSideStatus().setText(
1367: "StateMachine editor: Disabled");
1368: setEnabled(false);
1369: } catch (Error er) {
1370: JOptionPane.showMessageDialog(StateMachineApplet.this ,
1371: "Could not save stateMachine:\n" + er, "Error",
1372: JOptionPane.ERROR_MESSAGE);
1373: statusBar.getLeftSideStatus().setText(
1374: "StateMachine editor: Disabled");
1375: setEnabled(false);
1376: throw er;
1377: }
1378: }
1379:
1380: /**
1381: * Reads download URL, upload URL and Cookie string.
1382: */
1383: public void init() {
1384: super .init();
1385: cookie = getParameter("Cookie");
1386: stateMachineDownloadUrl = getParameter("DownloadURL");
1387: stateMachineUploadUrl = getParameter("UploadURL");
1388: }
1389:
1390: private void showProperties() {
1391: Container owner = graph;
1392: while (owner != null && !(owner instanceof JFrame)) {
1393: owner = owner.getParent();
1394: }
1395: StateMachinePropertiesDialog ipd = new StateMachinePropertiesDialog(
1396: owner instanceof JFrame ? (JFrame) owner : null,
1397: stateMachine);
1398: ipd.setVisible(true);
1399: }
1400: }
|