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 y.layout.router.BusRouter;
17  import y.module.BusRouterModule;
18  import y.option.IntOptionItem;
19  import y.option.OptionGroup;
20  import y.option.OptionHandler;
21  import y.option.OptionItem;
22  import y.option.ResourceBundleGuiFactory;
23  
24  import java.util.MissingResourceException;
25  
26  /**
27   * A modified {@link y.module.BusRouterModule} which omits the scope and the bus definition option which are not
28   * applicable to {@link demo.layout.router.BusRouterDemo}.
29   */
30  class BusRouterDemoModule extends BusRouterModule {
31  
32    private static final String GROUP_LAYOUT = "GROUP_LAYOUT";
33    private static final String MIN_DISTANCE_TO_NODES = "MIN_DISTANCE_TO_NODES";
34    private static final String MIN_DISTANCE_TO_EDGES = "MIN_DISTANCE_TO_EDGES";
35  
36    /**
37     * Creates a new instance.
38     */
39    BusRouterDemoModule() {
40      optionsLayout = false;
41      optionsSelection = true;
42      optionsRouting = true;
43    }
44  
45    /**
46     * Adds the option items used by this module to the given <code>OptionHandler</code>.
47     *
48     * @param oh the <code>OptionHandler</code> to add the items to
49     */
50    protected void addOptionItems(final OptionHandler oh) {
51      OptionItem item;
52      OptionGroup og;
53  
54      item = oh.addInt(MIN_DISTANCE_TO_NODES, 20);
55      item.setAttribute(IntOptionItem.ATTRIBUTE_MIN_VALUE, new Integer(1));
56      item = oh.addInt(MIN_DISTANCE_TO_EDGES, 10);
57      item.setAttribute(IntOptionItem.ATTRIBUTE_MIN_VALUE, new Integer(1));
58  
59      og = new OptionGroup();
60      og.setAttribute(OptionGroup.ATTRIBUTE_TITLE, GROUP_LAYOUT);
61      og.addItem(oh.getItem(MIN_DISTANCE_TO_EDGES));
62      og.addItem(oh.getItem(MIN_DISTANCE_TO_NODES));
63  
64      super.addOptionItems(oh);
65    }
66  
67    protected BusRouter getBusRouter() {
68      return super.getBusRouter();
69    }
70  
71    /**
72     * Creates an option handler for this module and adds to it its original and the demo's resource bundle.
73     */
74    protected OptionHandler createOptionHandler() {
75      final OptionHandler oh = super.createOptionHandler();
76      ResourceBundleGuiFactory gf = new ResourceBundleGuiFactory();
77      try {
78        gf.addBundle(BusRouterModule.class.getName());
79        gf.addBundle(BusRouterDemo.class.getName());
80      } catch (MissingResourceException mre) {
81      }
82      oh.setGuiFactory(gf);
83  
84      // change some initial settings of the module
85      final BusRouter router = getBusRouter();
86      router.setPreferredBackboneSegmentCount(1);
87      initOptionHandler(oh, router);
88  
89      return oh;
90    }
91  
92    /**
93     * Configures an instance of {@link y.layout.router.BusRouter}. The values provided by this module's option handler
94     * are being used for this purpose.
95     *
96     * @param busRouter the BusRouter to be configured.
97     */
98    public void configure(BusRouter busRouter) {
99      super.configure(busRouter);
100     final OptionHandler oh = getOptionHandler();
101 
102     busRouter.setMinimumDistanceToNode(oh.getInt(MIN_DISTANCE_TO_NODES));
103     busRouter.setMinimumDistanceToEdge(oh.getInt(MIN_DISTANCE_TO_EDGES));
104   }
105 
106   /**
107    * Sets the option items of the given option handler to the settings of the given <code>BusRouter</code>.
108    *
109    * @param oh        the option handler
110    * @param busRouter the bus router
111    */
112   protected void initOptionHandler(OptionHandler oh, BusRouter busRouter) {
113     super.initOptionHandler(oh, busRouter);
114 
115     oh.set(MIN_DISTANCE_TO_NODES, new Integer(busRouter.getMinimumDistanceToNode()));
116     oh.set(MIN_DISTANCE_TO_EDGES, new Integer(busRouter.getMinimumDistanceToEdge()));
117   }
118 
119 }
120