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.layout.router;
29  
30  import demo.view.DemoBase;
31  import y.option.CompoundEditor;
32  import y.option.ConstraintManager;
33  import y.option.DefaultEditorFactory;
34  import y.option.Editor;
35  import y.option.IntOptionItem;
36  import y.option.ItemEditor;
37  import y.option.OptionGroup;
38  import y.option.OptionHandler;
39  import y.option.OptionItem;
40  import y.option.ResourceBundleGuiFactory;
41  import y.view.EditMode;
42  import y.view.Graph2DView;
43  import y.view.MovePortMode;
44  import y.view.View2DConstants;
45  import y.view.ViewMode;
46  
47  import javax.swing.JComponent;
48  import java.beans.PropertyChangeEvent;
49  import java.beans.PropertyChangeListener;
50  import java.util.ArrayList;
51  import java.util.Iterator;
52  import java.util.List;
53  import java.util.MissingResourceException;
54  
55  /**
56   * Governs the settings which are present in the demo's side panel.
57   */
58  class BusRouterDemoTools {
59  
60    private static final String GROUP_GRID = "GROUP_GRID";
61    private static final String GROUP_EDIT = "GROUP_EDIT";
62  
63    private static final String GRID_ENABLED = "GRID_ENABLED";
64    private static final String GRID_SPACING = "GRID_SPACING";
65    private static final String SNAPPING = "SNAPPING";
66  
67    private final OptionHandler optionHandler;
68    private final DemoBase.SnappingConfiguration snappingConfiguration;
69    private BusRouterDemoModule module;
70    private Graph2DView view;
71  
72    /**
73     * Creates a new instance.
74     */
75    BusRouterDemoTools() {
76      this.snappingConfiguration = DemoBase.createDefaultSnappingConfiguration();
77      this.snappingConfiguration.setGridType(View2DConstants.GRID_POINTS);
78      this.optionHandler = createOptionHandler();
79    }
80  
81    void setViewAndRouter(Graph2DView view, BusRouterDemoModule module) {
82      this.view = view;
83      this.module = module;
84    }
85  
86    /**
87     * Creates an option handler containing items for grid, orthogonal mode, snap lines and automatic routing.
88     */
89    OptionHandler createOptionHandler() {
90      OptionHandler oh = new OptionHandler("BUS_ROUTER_DEMO_SETTINGS");
91      OptionItem item;
92      OptionGroup og;
93  
94      // items of group GRID
95      item = oh.addBool(GRID_ENABLED, false);
96      item.addPropertyChangeListener("value", new PropertyChangeListener() {
97        public void propertyChange(PropertyChangeEvent evt) {
98          updateGrid();
99        }
100     });
101     item = oh.addInt(GRID_SPACING, 20);
102     item.setAttribute(IntOptionItem.ATTRIBUTE_MIN_VALUE, new Integer(1));
103     item.addPropertyChangeListener("value", new PropertyChangeListener() {
104       public void propertyChange(PropertyChangeEvent evt) {
105         updateGrid();
106       }
107     });
108 
109     og = new OptionGroup();
110     og.setAttribute(OptionGroup.ATTRIBUTE_TITLE, GROUP_GRID);
111     og.addItem(oh.getItem(GRID_ENABLED));
112     og.addItem(oh.getItem(GRID_SPACING));
113 
114     ConstraintManager cm = new ConstraintManager(oh);
115     cm.setEnabledOnValueEquals(GRID_ENABLED, Boolean.TRUE, GRID_SPACING);
116 
117     item = oh.addBool(SNAPPING, true);
118     item.addPropertyChangeListener("value", new PropertyChangeListener() {
119       public void propertyChange(PropertyChangeEvent evt) {
120         updateSnapping();
121       }
122     });
123 
124     og = new OptionGroup();
125     og.setAttribute(OptionGroup.ATTRIBUTE_TITLE, GROUP_EDIT);
126     og.addItem(oh.getItem(SNAPPING));
127 
128     return oh;
129   }
130 
131   /**
132    * Creates a component for this class's option handler.
133    *
134    * @return a component for this class's option handler.
135    */
136   JComponent createOptionComponent() {
137     DefaultEditorFactory editorFactory = new DefaultEditorFactory();
138     try {
139       ResourceBundleGuiFactory gf = new ResourceBundleGuiFactory();
140       gf.addBundle(BusRouterDemo.class.getName());
141       editorFactory.setGuiFactory(gf);
142     } catch (final MissingResourceException mre) {
143       //noinspection UseOfSystemOutOrSystemErr
144       System.err.println("Could not find resources! " + mre);
145     }
146 
147     Editor editor = editorFactory.createEditor(optionHandler);
148 
149     // set the editor to auto adopt and auto commit, so no OK button is needed
150     final List stack = new ArrayList();
151     stack.add(editor);
152     while (!stack.isEmpty()) {
153       final Object last = stack.remove(stack.size() - 1);
154       if (last instanceof CompoundEditor) {
155         for (Iterator it = ((CompoundEditor) last).editors(); it.hasNext(); ) {
156           stack.add(it.next());
157         }
158       }
159       if (last instanceof ItemEditor) {
160         ((ItemEditor) last).setAutoCommit(true);
161         ((ItemEditor) last).setAutoAdopt(true);
162       }
163     }
164 
165     return editor.getComponent();
166   }
167 
168   /**
169    * Updates the grid of the set view and bus router to the values specified by this class's option handler.
170    */
171   void updateGrid() {
172     module.setGridRoutingEnabled(optionHandler.getBool(GRID_ENABLED));
173     module.setGridSpacing(optionHandler.getInt(GRID_SPACING));
174     snappingConfiguration.setGridSnappingEnabled(optionHandler.getBool(GRID_ENABLED));
175     snappingConfiguration.setGridDistance((double) optionHandler.getInt(GRID_SPACING));
176     configureSnapping();
177   }
178 
179   /**
180    * Updates the snapping of the set view to the values specified by this class's option handler.
181    */
182   void updateSnapping() {
183     snappingConfiguration.setSnappingEnabled(optionHandler.getBool(SNAPPING));
184     configureSnapping();
185   }
186 
187   /**
188    * Configures the snapping of the set view according to this class's snapping configuration..
189    */
190   private void configureSnapping() {
191     final EditMode editMode = (EditMode) view.getViewModes().next();
192     snappingConfiguration.configureView(view);
193     snappingConfiguration.configureEditMode(editMode);
194 
195     // configure snapping disables singleRealizerPortCandidates, so we enable it manually
196     final ViewMode movePortMode = editMode.getMovePortMode();
197     if (movePortMode instanceof MovePortMode) {
198       ((MovePortMode) movePortMode).setUsingRealizerPortCandidates(true);
199     }
200   }
201 
202 }
203