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