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