1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.9. 
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2011 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.view.viewmode;
15  
16  import demo.view.DemoBase;
17  import y.base.Node;
18  import y.base.Edge;
19  import y.view.EditMode;
20  import y.view.NodeRealizer;
21  import y.view.PopupMode;
22  import y.view.YLabel;
23  
24  import javax.swing.AbstractAction;
25  import javax.swing.JOptionPane;
26  import javax.swing.JPopupMenu;
27  import java.awt.event.ActionEvent;
28  import java.awt.EventQueue;
29  import java.beans.PropertyChangeEvent;
30  import java.beans.PropertyChangeListener;
31  import java.util.Locale;
32  
33  /**
34   * Demonstrates how to display context sensitive popup menus
35   * in the view.
36   * <p>
37   * This demo does also show how to write an action that opens
38   * an inline text editor in the view to modify the label of a node.  
39   * </p>
40   * <p>
41   * To activate the popup menus right click either on a node or
42   * the view background.
43   * </p>
44   * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/mvc_controller.html#label_editor">Section User Interaction</a> in the yFiles for Java Developer's Guide
45   */
46  public class PopupModeDemo extends DemoBase
47  {
48  
49    protected EditMode createEditMode() {
50      EditMode editMode =  super.createEditMode();
51      //add a popup child mode to editMode (one that listens to the right mouse click
52      //and pops up context sensitive menus)
53      editMode.setPopupMode( new DemoPopupMode() );
54      loadGraph("resource/popup.graphml");
55      return editMode;
56    }
57    
58    class DemoPopupMode extends PopupMode
59    {
60      /** Popup menu for a hit edge */
61      public JPopupMenu getEdgePopup(Edge e)
62      {
63        JPopupMenu pm = new JPopupMenu();
64        pm.add(new ShowEdgeInfo(e));
65        return pm;
66      }
67  
68      /** Popup menu for a hit node */
69      public JPopupMenu getNodePopup(Node v)
70      {
71        JPopupMenu pm = new JPopupMenu();
72        pm.add(new ShowNodeInfo(v));
73        pm.add(new EditLabel(v));
74        return pm;
75      }
76  
77      /** Popup menu for a paper (plain background) hit */
78      public JPopupMenu getPaperPopup(double x, double y)
79      {
80        JPopupMenu pm = new JPopupMenu();
81        pm.add(new Zoom(0.8));
82        pm.add(new Zoom(1.2));
83        pm.add(new FitContent(view));
84        return pm;
85      }
86  
87      /** Popup menu for a paper hit if things are selected */
88      public JPopupMenu getSelectionPopup(double x, double y)
89      {
90        JPopupMenu pm = new JPopupMenu();
91        pm.add(new DeleteSelection(view));
92        return pm;
93      }
94    }
95  
96    /**
97     * Action that displays an information dialog for an edge.
98     */
99    class ShowEdgeInfo extends AbstractAction
100   {
101     Edge e;
102 
103     ShowEdgeInfo(Edge e )
104     {
105       super("Edge Info");
106       this.e = e;
107     }
108 
109     public void actionPerformed(ActionEvent e)
110     {
111       JOptionPane.showMessageDialog(
112               view,
113               "Edge from \"" + this.e.source() +
114               "\" to \"" + this.e.target() + "\".");
115     }
116   }
117 
118   /**
119    * Action that displays an information dialog for a node.
120    */
121   class ShowNodeInfo extends AbstractAction
122   {
123     Node v;
124 
125     ShowNodeInfo(Node v)
126     {
127       super("Node Info");
128       this.v = v;
129     }
130 
131     public void actionPerformed(ActionEvent e)
132     {
133       JOptionPane.showMessageDialog(view,
134                                     "Label text of node is " +
135                                     view.getGraph2D().getLabelText(v) +
136                                     "\n\n(Guess you knew that already :-)");
137     }
138   }
139 
140   /**
141    * Action that opens a text editor for the label of a node 
142    * <p>
143    * The inlined label editor allows to enter multiple lines of
144    * label text for a node. The "Enter" or "Return" key starts
145    * a new line of text. To terminate the label editor click
146    * the mouse somewhere outside of the label editor box.
147    */
148   class EditLabel extends AbstractAction
149   {
150     Node v;
151 
152     EditLabel(Node v)
153     {
154       super("Edit Label");
155       this.v = v;
156     }
157 
158     public void actionPerformed(ActionEvent e)
159     {
160 
161       final NodeRealizer r = view.getGraph2D().getRealizer(v);
162       final YLabel label = r.getLabel();
163 
164 
165       // optional property change listener, that gets invoked
166       // after the label editor has changed the value of the 
167       // label text. what this listener does is to adapt the size
168       // of the node to the new label text
169       PropertyChangeListener pcl = new PropertyChangeListener()
170         {
171           public void propertyChange(PropertyChangeEvent pce)
172             {
173               r.setSize(label.getWidth()+10,label.getHeight()+10);
174             }
175         };
176 
177       view.openLabelEditor(label,
178                            label.getBox().getX(),
179                            label.getBox().getY(),
180                            pcl,    //optional propertyChangeListener
181                            true    //optional single line mode activated
182                            );
183     }
184   }
185 
186   public static void main(String[] args) {
187     EventQueue.invokeLater(new Runnable() {
188       public void run() {
189         Locale.setDefault(Locale.ENGLISH);
190         initLnF();
191         (new PopupModeDemo()).start("Popup Mode Demo");
192       }
193     });
194   }
195 }
196 
197 
198       
199