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  
15  package demo.layout.module;
16  
17  import demo.layout.withoutview.DiagonalLayouter;
18  
19  import java.io.FileInputStream;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.util.Locale;
23  import java.util.MissingResourceException;
24  import java.util.Properties;
25  import y.module.LayoutModule;
26  import y.module.YModule;
27  import y.option.OptionHandler;
28  import y.option.PropertiesIOHandler;
29  import y.option.ResourceBundleGuiFactory;
30  
31  /**
32   * This module represents an interactive configurator and launcher for the demo
33   * Layouter {@link demo.layout.withoutview.DiagonalLayouter}.
34   * <br>
35   * Additionally, this class can be executed separately. In this case it shows off
36   * the internationalization and serialization features of the
37   * {@link y.option.OptionHandler} class.
38   * By launching the module class using a two letter language constant as an
39   * argument, the dialog will be internationalized in that language if the
40   * corresponding localized properties file is available. Try either 'en' for
41   * English or 'de' for German.
42   *
43   *
44   * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/option_basic.html#i18n_l10n">Section Internationalization and Localization</a> in the yFiles for Java Developer's Guide
45   */
46  public class DiagonalLayoutModule extends LayoutModule
47  {
48    public DiagonalLayoutModule()
49    {
50      super("DIAGONAL", "yWorks Layout Team", "Wrapper for DiagonalLayouter");
51    }
52    
53    protected OptionHandler createOptionHandler()
54    {
55      DiagonalLayouter layouter = new DiagonalLayouter();
56      OptionHandler op = new OptionHandler(getModuleName());
57      op.addDouble("MINIMAL_NODE_DISTANCE", layouter.getMinimalNodeDistance());
58      return op;
59    }
60    
61    protected void mainrun()
62    {
63      DiagonalLayouter layouter = new DiagonalLayouter();
64      OptionHandler op = getOptionHandler();
65      layouter.setMinimalNodeDistance(op.getDouble("MINIMAL_NODE_DISTANCE"));
66      launchLayouter(layouter);
67    }
68    
69    
70    /**
71     * Display the option handler
72     */
73    public static void main(String[] args)
74    {
75      // set the locale as given from the arguments
76      if (args.length > 1){
77        Locale.setDefault(new Locale(args[0],args[1]));
78      } else if (args.length > 0){
79        Locale.setDefault(new Locale(args[0],""));
80      }
81      
82      System.out.println("Executing using Locale "+Locale.getDefault().getDisplayName());
83      
84      // initialize the module
85      YModule module = new DiagonalLayoutModule();
86      OptionHandler oh = module.getOptionHandler();
87  
88      // setup a guifactory
89      try{
90        ResourceBundleGuiFactory gf = new ResourceBundleGuiFactory();
91        
92        //this is the globally used information (for the buttons etc.)
93        gf.addBundle("demo.layout.module.OptionHandler");
94        
95        //this is the bundle specific information
96        gf.addBundle(module.getClass().getName());
97        oh.setGuiFactory(gf);
98      } catch (MissingResourceException mre){
99        System.err.println("Could not find resources! "+mre);
100     }
101     
102     // try to read in last session
103     
104     //create properties store
105     Properties p = new Properties();
106     try{
107       // initialize it from file
108       FileInputStream fis = new FileInputStream(module.getClass().getName()+"Settings.properties");
109       p.load(fis);
110       fis.close();
111     } catch (IOException ioe){
112       System.err.println(ioe);
113     }
114     // install an IOHandler
115     oh.setOptionsIOHandler(new PropertiesIOHandler(p));
116     
117     // read the values in
118     oh.read();
119     
120     // display the editor
121     oh.showEditor();
122 
123     // store the properties
124     try{
125       FileOutputStream fos = new FileOutputStream(module.getClass().getName()+"Settings.properties");
126       p.store(fos, "Saved Settings for "+module.getClass().getName());
127       fos.flush();
128       fos.close();
129     } catch (IOException ioe){
130       System.err.println(ioe);
131     }
132     
133     //goodbye
134     System.exit(0);
135   }
136 
137 }
138 
139