Source Code Cross Referenced for InteractionApplet.java in  » Web-Framework » argun » biz » hammurapi » web » interaction » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » argun » biz.hammurapi.web.interaction 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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