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 y.layout.router.BusRouter;
31  import demo.layout.module.BusRouterModule;
32  import y.option.IntOptionItem;
33  import y.option.OptionGroup;
34  import y.option.OptionHandler;
35  import y.option.OptionItem;
36  import y.option.ResourceBundleGuiFactory;
37  
38  import java.util.MissingResourceException;
39  
40  /**
41   * A modified {@link y.module.BusRouterModule} which omits the scope and the bus definition option which are not
42   * applicable to {@link demo.layout.router.BusRouterDemo}.
43   */
44  class BusRouterDemoModule extends BusRouterModule {
45  
46    private static final String GROUP_LAYOUT = "GROUP_LAYOUT";
47    private static final String MIN_DISTANCE_TO_NODES = "MIN_DISTANCE_TO_NODES";
48    private static final String MIN_DISTANCE_TO_EDGES = "MIN_DISTANCE_TO_EDGES";
49    private byte scope;
50    private boolean gridRoutingEnabled;
51    private int gridSpacing;
52  
53    /**
54     * Creates a new instance.
55     */
56    BusRouterDemoModule() {
57      optionsLayout = false;
58      optionsSelection = true;
59      optionsRouting = true;
60    }
61  
62    /**
63     * Adds the option items used by this module to the given <code>OptionHandler</code>.
64     * @param defaults a <code>BusRouter</code> instance that provides default option values.
65     * @param options the <code>OptionHandler</code> to add the items to
66     */
67    protected void addOptionItems(final BusRouter defaults, final OptionHandler options) {
68      OptionItem item;
69      OptionGroup og;
70  
71      item = options.addInt(MIN_DISTANCE_TO_NODES, defaults.getMinimumDistanceToNode());
72      item.setAttribute(IntOptionItem.ATTRIBUTE_MIN_VALUE, new Integer(1));
73      item = options.addInt(MIN_DISTANCE_TO_EDGES, defaults.getMinimumDistanceToEdge());
74      item.setAttribute(IntOptionItem.ATTRIBUTE_MIN_VALUE, new Integer(1));
75  
76      og = new OptionGroup();
77      og.setAttribute(OptionGroup.ATTRIBUTE_TITLE, GROUP_LAYOUT);
78      og.addItem(options.getItem(MIN_DISTANCE_TO_EDGES));
79      og.addItem(options.getItem(MIN_DISTANCE_TO_NODES));
80  
81      defaults.setPreferredBackboneSegmentCount(1);
82      super.addOptionItems(defaults, options);
83    }
84  
85    /**
86     * Registers the demo's resource bundle for localization of the module's 
87     * user interface.
88     */
89    protected void initGuiFactory( final OptionHandler optionHandler ) {
90      ResourceBundleGuiFactory gf = new ResourceBundleGuiFactory();
91      try {
92        gf.addBundle(BusRouterModule.class.getName());
93        gf.addBundle(BusRouterDemo.class.getName());
94      } catch (MissingResourceException mre) {
95        // nothing to do here
96      }
97      optionHandler.setGuiFactory(gf);
98    }
99  
100   /**
101    * Configures an instance of {@link y.layout.router.BusRouter}. The values provided by this module's option handler
102    * are being used for this purpose.
103    *
104    * @param bus the BusRouter to be configured.
105    */
106   protected void configure(final BusRouter bus, final OptionHandler options) {
107     super.configure(bus, options);
108 
109     bus.setMinimumDistanceToNode(options.getInt(MIN_DISTANCE_TO_NODES));
110     bus.setMinimumDistanceToEdge(options.getInt(MIN_DISTANCE_TO_EDGES));
111 
112     bus.setScope(scope);
113     bus.setGridRoutingEnabled(gridRoutingEnabled);
114     bus.setGridSpacing(gridSpacing);
115   }
116 
117 
118   void setScope(final byte scope) {
119     this.scope = scope;
120   }
121 
122   void setGridRoutingEnabled(final boolean gridRoutingEnabled) {
123     this.gridRoutingEnabled = gridRoutingEnabled;
124   }
125 
126   void setGridSpacing(final int gridSpacing) {
127     this.gridSpacing = gridSpacing;
128   }
129 }
130