1   /****************************************************************************
2    * This demo file is part of yFiles for Java 2.14.
3    * Copyright (c) 2000-2017 by yWorks GmbH, Vor dem Kreuzberg 28,
4    * 72070 Tuebingen, Germany. All rights reserved.
5    * 
6    * yFiles demo files exhibit yFiles for Java functionalities. Any redistribution
7    * of demo files in source code or binary form, with or without
8    * modification, is not permitted.
9    * 
10   * Owners of a valid software license for a yFiles for Java version that this
11   * demo is shipped with are allowed to use the demo source code as basis
12   * for their own yFiles for Java powered applications. Use of such programs is
13   * governed by the rights and conditions as set out in the yFiles for Java
14   * license agreement.
15   * 
16   * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
17   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19   * NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21   * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   *
27   ***************************************************************************/
28  package demo.view.viewmode;
29  
30  import demo.view.DemoBase;
31  import y.base.Node;
32  import y.base.Edge;
33  import y.view.EditMode;
34  import y.view.NodeRealizer;
35  import y.view.PopupMode;
36  import y.view.YLabel;
37  
38  import javax.swing.AbstractAction;
39  import javax.swing.JOptionPane;
40  import javax.swing.JPopupMenu;
41  import java.awt.event.ActionEvent;
42  import java.awt.EventQueue;
43  import java.beans.PropertyChangeEvent;
44  import java.beans.PropertyChangeListener;
45  import java.util.Locale;
46  
47  /**
48   * Demonstrates how to display context sensitive popup menus
49   * in the view.
50   * <p>
51   * This demo does also show how to write an action that opens
52   * an inline text editor in the view to modify the label of a node.  
53   * </p>
54   * <p>
55   * To activate the popup menus right click either on a node or
56   * the view background.
57   * </p>
58   * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/mvc_controller#label_editor" target="_blank">Section User Interaction</a> in the yFiles for Java Developer's Guide
59   */
60  public class PopupModeDemo extends DemoBase
61  {
62  
63    protected EditMode createEditMode() {
64      EditMode editMode =  super.createEditMode();
65      //add a popup child mode to editMode (one that listens to the right mouse click
66      //and pops up context sensitive menus)
67      editMode.setPopupMode( new DemoPopupMode() );
68      loadGraph("resource/popup.graphml");
69      return editMode;
70    }
71    
72    class DemoPopupMode extends PopupMode
73    {
74      /** Popup menu for a hit edge */
75      public JPopupMenu getEdgePopup(Edge e)
76      {
77        JPopupMenu pm = new JPopupMenu();
78        pm.add(new ShowEdgeInfo(e));
79        return pm;
80      }
81  
82      /** Popup menu for a hit node */
83      public JPopupMenu getNodePopup(Node v)
84      {
85        JPopupMenu pm = new JPopupMenu();
86        pm.add(new ShowNodeInfo(v));
87        pm.add(new EditLabel(v));
88        return pm;
89      }
90  
91      /** Popup menu for a paper (plain background) hit */
92      public JPopupMenu getPaperPopup(double x, double y)
93      {
94        JPopupMenu pm = new JPopupMenu();
95        pm.add(new Zoom(view, 0.8));
96        pm.add(new Zoom(view, 1.2));
97        pm.add(new FitContent(view));
98        return pm;
99      }
100 
101     /** Popup menu for a paper hit if things are selected */
102     public JPopupMenu getSelectionPopup(double x, double y)
103     {
104       JPopupMenu pm = new JPopupMenu();
105       pm.add(new DeleteSelection(view));
106       return pm;
107     }
108   }
109 
110   /**
111    * Action that displays an information dialog for an edge.
112    */
113   class ShowEdgeInfo extends AbstractAction
114   {
115     Edge e;
116 
117     ShowEdgeInfo(Edge e )
118     {
119       super("Edge Info");
120       this.e = e;
121     }
122 
123     public void actionPerformed(ActionEvent e)
124     {
125       JOptionPane.showMessageDialog(
126               view,
127               "Edge from \"" + this.e.source() +
128               "\" to \"" + this.e.target() + "\".");
129     }
130   }
131 
132   /**
133    * Action that displays an information dialog for a node.
134    */
135   class ShowNodeInfo extends AbstractAction
136   {
137     Node v;
138 
139     ShowNodeInfo(Node v)
140     {
141       super("Node Info");
142       this.v = v;
143     }
144 
145     public void actionPerformed(ActionEvent e)
146     {
147       JOptionPane.showMessageDialog(view,
148                                     "Label text of node is " +
149                                     view.getGraph2D().getLabelText(v) +
150                                     "\n\n(Guess you knew that already :-)");
151     }
152   }
153 
154   /**
155    * Action that opens a text editor for the label of a node 
156    * <p>
157    * The inlined label editor allows to enter multiple lines of
158    * label text for a node. The "Enter" or "Return" key starts
159    * a new line of text. To terminate the label editor click
160    * the mouse somewhere outside of the label editor box.
161    */
162   class EditLabel extends AbstractAction
163   {
164     Node v;
165 
166     EditLabel(Node v)
167     {
168       super("Edit Label");
169       this.v = v;
170     }
171 
172     public void actionPerformed(ActionEvent e)
173     {
174 
175       final NodeRealizer r = view.getGraph2D().getRealizer(v);
176       final YLabel label = r.getLabel();
177 
178 
179       // optional property change listener, that gets invoked
180       // after the label editor has changed the value of the 
181       // label text. what this listener does is to adapt the size
182       // of the node to the new label text
183       PropertyChangeListener pcl = new PropertyChangeListener()
184         {
185           public void propertyChange(PropertyChangeEvent pce)
186             {
187               r.setSize(label.getWidth()+10,label.getHeight()+10);
188             }
189         };
190 
191       view.openLabelEditor(label,
192                            label.getBox().getX(),
193                            label.getBox().getY(),
194                            pcl,    //optional propertyChangeListener
195                            true    //optional single line mode activated
196                            );
197     }
198   }
199 
200   public static void main(String[] args) {
201     EventQueue.invokeLater(new Runnable() {
202       public void run() {
203         Locale.setDefault(Locale.ENGLISH);
204         initLnF();
205         (new PopupModeDemo()).start("Popup Mode Demo");
206       }
207     });
208   }
209 }
210 
211 
212       
213