View Javadoc

1   package net.sf.mmapps.applications.developer;
2   
3   import java.awt.*;
4   import java.awt.event.*;
5   import java.beans.*;
6   import java.io.File;
7   
8   import javax.swing.*;
9   
10  //import net.sf.mmapps.applications.developer.context.MMBaseLiveConfigurationReader;
11  import net.sf.mmapps.applications.developer.dialog.ExceptionDialog;
12  import net.sf.mmapps.applications.developer.plaf.ThemeManager;
13  import net.sf.mmapps.modules.config.*;
14  
15  import org.apache.commons.logging.*;
16  /***
17   * Main entry point for the Developer application
18   * @author Kees Jongenburger
19   * @version $Id: Developer.java,v 1.4 2004/07/29 09:05:18 keesj Exp $
20   */
21  public class Developer extends JFrame implements ActionListener, WindowListener, PropertyChangeListener {
22  
23  	static {
24  		System.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.SimpleLog");
25  	}
26      private static Log log = LogFactory.getLog(Developer.class);
27      /***
28       * the default language when needed
29       * DEFAULT_LANGUAGE="en";
30       **/
31      public final static String DEFAULT_LANGUAGE = "en";
32      public File lastImportDir;
33      Project project;
34  
35      //private data members
36      private String language = DEFAULT_LANGUAGE;
37      private JMenuBar jMenuBar;
38  
39      //project
40      private JMenu projectMenu;
41      
42  	private JMenuItem loadProjectMenuItem;
43      private JMenuItem clearProjectMenuItem;
44      private JMenuItem importMenuItem;
45      private JMenuItem liveImportMenuItem;
46      private JMenuItem jarImportMenuItem;
47      private JMenuItem exitMenuItem;
48      private JMenuItem saveProjectMenuItem;
49  
50      //settings
51      private JMenu settingsMenu;
52      private JMenu lookAndFeel;
53  
54      private ExplorerPanel explorerPanel;
55      private MainPanel mainPanel;
56      private StatusBar statusBar;
57      private JSplitPane jSplitPane;
58  
59      //private static data members
60      private static Developer developer = null;
61  
62      /***
63       * private for singleton patern  see getInstance
64       **/
65      private Developer() {
66          super();
67          UIManager.addPropertyChangeListener(this);
68  		
69          //setDeveloperTheme("MMBase");
70          //try {
71          //java.rmi.server.RMISocketFactory.setSocketFactory(new sun.rmi.transport.proxy.RMIHttpToCGISocketFactory());
72          //} catch (Exception e){
73          //			e.getMessage();
74          //	}
75  
76          //set the mmbase icon
77          Image image = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("images/logo.gif"));
78          setIconImage(image);
79  
80          //set the title of the application
81          setTitle("MMBase Developer");
82  
83          //add a shutdown hoock
84          addWindowListener(this);
85  
86          //menu stuff
87          jMenuBar = new JMenuBar();
88          setJMenuBar(jMenuBar);
89  
90          //project menu
91          projectMenu = new JMenu("Project");
92          jMenuBar.add(projectMenu);
93  
94          //project import menu
95          importMenuItem = new JMenuItem("config directory import");
96          importMenuItem.addActionListener(this);
97          importMenuItem.setToolTipText("Import mmbase configuration from the mmbase config directory.");
98          projectMenu.add(importMenuItem);
99  
100         //project live import menu item
101         liveImportMenuItem = new JMenuItem("rmmci config import");
102         liveImportMenuItem.addActionListener(this);
103         liveImportMenuItem.setToolTipText("Import mmbase configuration for a host running rmmci.");
104         projectMenu.add(liveImportMenuItem);
105 
106         //		project live import menu item
107         jarImportMenuItem = new JMenuItem("jar config import");
108         jarImportMenuItem.addActionListener(this);
109         jarImportMenuItem.setToolTipText("Import mmbase configuration for jar file.");
110         projectMenu.add(jarImportMenuItem);
111 
112         //		project live import menu item
113         saveProjectMenuItem = new JMenuItem("save");
114         saveProjectMenuItem.addActionListener(this);
115         saveProjectMenuItem.setToolTipText("save project");
116         projectMenu.add(saveProjectMenuItem);
117 
118         //project clear all
119         clearProjectMenuItem = new JMenuItem("clear all");
120         clearProjectMenuItem.addActionListener(this);
121         clearProjectMenuItem.setToolTipText("Clear current project");
122         projectMenu.add(clearProjectMenuItem);
123 
124         //project exit
125         exitMenuItem = new JMenuItem("exit");
126         exitMenuItem.addActionListener(this);
127         exitMenuItem.setToolTipText("Exit the application.");
128         projectMenu.add(exitMenuItem);
129 
130         //settings menu
131         settingsMenu = new JMenu("Settings");
132         ThemeManager t = new ThemeManager();
133         settingsMenu.add(t.getMenu());
134         jMenuBar.add(settingsMenu);
135         //set the thems
136         t.setTheme("Helix");
137 
138         //lookAndFeel.addActionListener(this);
139 
140         //status bar
141         statusBar = new StatusBar();
142         getContentPane().add(statusBar, BorderLayout.SOUTH);
143 
144         //create a schollpane and add the projectpanel
145         //and the cloud view to it
146         jSplitPane = new JSplitPane();
147         jSplitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
148         jSplitPane.setDividerLocation(180);
149 
150         //project panel
151         explorerPanel = new ExplorerPanel();
152         jSplitPane.setLeftComponent(explorerPanel);
153 
154         //cloud view application
155         jSplitPane.setRightComponent(mainPanel = new MainPanel());
156 
157         getContentPane().add(jSplitPane, BorderLayout.CENTER);
158         project = new Project();
159         //prepare for to display
160         pack();
161         setSize(900, 700);
162         show();
163     }
164 
165     public void addFrame(String title, Component comp) {
166         mainPanel.createAndAddInternalFrame(title, comp);
167         //jSplitPane.setRightComponent(comp);
168     }
169     /***
170      * Singleton patern to get the Main application
171      **/
172     public static synchronized Developer getInstance() {
173         if (developer == null) {
174             developer = new Developer();
175         }
176         return developer;
177     }
178 
179     //methods from the window listener
180     public void windowOpened(WindowEvent evt) {};
181     public void windowClosed(WindowEvent evt) {};
182     public void windowIconified(WindowEvent evt) {};
183     public void windowDeiconified(WindowEvent evt) {};
184     public void windowActivated(WindowEvent evt) {};
185     public void windowDeactivated(WindowEvent evt) {};
186     public void windowClosing(WindowEvent evt) {
187         shutDown();
188     }
189 
190     public void actionPerformed(ActionEvent actionEvent) {
191         if (actionEvent.getSource() == exitMenuItem) {
192             shutDown();
193         } else if (actionEvent.getSource() == importMenuItem) {
194             JFileChooser fc = new JFileChooser();
195             fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
196 
197             if (lastImportDir != null) {
198                 fc.setSelectedFile(lastImportDir);
199             }
200             int returnVal = fc.showOpenDialog(this);
201             if (returnVal == JFileChooser.APPROVE_OPTION) {
202                 File file = fc.getSelectedFile();
203                 lastImportDir = file;
204                 explorerPanel.getProjectPanel().importConfiguration(file);
205             }
206         } else if (actionEvent.getSource() == jarImportMenuItem) {
207             JFileChooser fc = new JFileChooser();
208 
209             if (lastImportDir != null) {
210                 fc.setSelectedFile(lastImportDir);
211             }
212             int returnVal = fc.showOpenDialog(this);
213             if (returnVal == JFileChooser.APPROVE_OPTION) {
214                 File file = fc.getSelectedFile();
215                 lastImportDir = file.getParentFile();
216                 try {
217 
218                     Configuration conf = JarConfigurationReader.getConfiguration(file);
219                     if (conf != null) {
220                         ApplicationConfigurations apps = conf.getApplicationConfigurations();
221                         for (int x = 0; x < apps.size(); x++) {
222                             explorerPanel.getProjectPanel().addApplicationConfiguration(apps.getApplicationConfiguration(x));
223                         }
224                     }
225                 } catch (Exception e) {
226                     new ExceptionDialog(this, "error while reading jar configuration", e).show();
227                 }
228             }
229         } else if (actionEvent.getSource() == liveImportMenuItem) {
230             new MMBaseLiveConfigurationReader().startRead();
231         } else if (actionEvent.getSource() == clearProjectMenuItem) {
232             getExplorerPanel().getProjectPanel().clearProject();
233         } else if (actionEvent.getSource() == saveProjectMenuItem) {
234             log.debug("saving");
235             //TODAO: add dialog asking the name of the project
236             Configuration config = getExplorerPanel().getProjectPanel().getConfiguration();
237             String home = project.getProperty("home");
238             log.debug("mmdeveloper home is " + home);
239             if (home != null) {
240                 File homeFile = new File(home);
241                 File projectDir = new File(homeFile, "default");
242                 log.debug("project home is " + projectDir.getPath());
243                 try {
244                     ConfigurationXMLWriter.writeConfiguration(config, projectDir);
245                 } catch (Exception e) {
246                     new ExceptionDialog(this, "error while saving project", e);
247                 }
248             }
249         }
250     }
251 
252     /***
253      * gracefully shutdown application
254      * at some stage this could save projects etc
255      **/
256     public void shutDown() {
257         //at later stage we might want to first save work in progress before
258         //exiting the system
259         System.exit(0);
260     }
261 
262     /***
263      * @return the statusbar of the application
264      **/
265     public StatusBar getStatusBar() {
266         return statusBar;
267     }
268 
269     public String getLanguage() {
270         return language;
271     }
272 
273     /***
274      * Developer application start point
275      **/
276     public static void main(String argv[]) {
277         Developer.getInstance();
278     }
279 
280     public ExplorerPanel getExplorerPanel() {
281         return explorerPanel;
282     }
283 
284     public void propertyChange(PropertyChangeEvent e) {
285         String name = e.getPropertyName();
286         if (name.equals("lookAndFeel")) {
287             JComponent c = (JComponent) getRootPane();
288             SwingUtilities.updateComponentTreeUI(c);
289             //c.invalidate();
290             //c.validate();
291             c.repaint();
292         }
293     }
294 }