KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > jmx > browser > Browser


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.jmx.browser;
8
9 import java.awt.event.WindowAdapter JavaDoc;
10 import java.awt.event.WindowEvent JavaDoc;
11 import java.io.File JavaDoc;
12 import java.net.URL JavaDoc;
13 import java.util.Arrays JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.ResourceBundle JavaDoc;
16
17 import javax.swing.JFileChooser JavaDoc;
18 import javax.swing.JOptionPane JavaDoc;
19
20 import org.apache.log4j.Logger;
21 import org.ejtools.adwt.LookAndFeelUtil;
22 import org.ejtools.adwt.action.Command;
23 import org.ejtools.adwt.action.file.ExitAction;
24 import org.ejtools.adwt.action.file.NewAction;
25 import org.ejtools.adwt.action.file.OpenWorkspaceAction;
26 import org.ejtools.adwt.action.file.SaveAsWorkspaceAction;
27 import org.ejtools.adwt.action.file.SaveWorkspaceAction;
28 import org.ejtools.adwt.service.AboutServiceProvider;
29 import org.ejtools.adwt.service.ConsoleServiceProvider;
30 import org.ejtools.adwt.service.HistoryService;
31 import org.ejtools.adwt.service.HistoryServiceProvider;
32 import org.ejtools.adwt.service.MDIFrameServiceProvider;
33 import org.ejtools.adwt.service.MenuBarServiceProvider;
34 import org.ejtools.adwt.service.ToolBarServiceProvider;
35 import org.ejtools.beans.beancontext.CustomBeanContextServicesSupport;
36 import org.ejtools.graph.service.GraphServiceProvider;
37 import org.ejtools.jmx.browser.frame.ServerInternalFrame;
38 import org.ejtools.jmx.browser.model.Server;
39 import org.ejtools.jmx.browser.model.service.ConnectionMetaData;
40 import org.ejtools.jmx.browser.state.WorkbenchState;
41 import org.ejtools.util.FileTools;
42 import org.ejtools.util.service.Profile;
43 import org.ejtools.util.service.ProfileServiceProvider;
44 import org.ejtools.util.state.WorkspaceFileTools;
45
46 /**
47  * Description of the Class
48  *
49  * @author Laurent Etiemble
50  * @version $Revision: 1.3 $
51  * @created 21 mars 2002
52  * @todo Javadoc to complete
53  */

54 public class Browser extends CustomBeanContextServicesSupport implements HistoryService.Holder
55 {
56    /** Description of the Field */
57    protected AboutServiceProvider aboutService;
58    /** Description of the Field */
59    protected ConsoleServiceProvider consoleService;
60    /** Description of the Field */
61    protected ProfileServiceProvider factoryProvider;
62    /** Description of the Field */
63    protected MDIFrameServiceProvider frameService;
64    /** Description of the Field */
65    protected GraphServiceProvider graphService;
66    /** Description of the Field */
67    protected HistoryServiceProvider historyService;
68    /** Description of the Field */
69    protected MenuBarServiceProvider menuBarService;
70    /** Description of the Field */
71    protected WorkbenchState stateManager;
72    /** Description of the Field */
73    protected ToolBarServiceProvider toolBarService;
74    /** Default logger */
75    private static Logger logger = Logger.getLogger(Browser.class);
76    /** Bundle for I18N */
77    private static ResourceBundle JavaDoc resources = ResourceBundle.getBundle("org.ejtools.jmx.browser.Resources");
78
79
80    /**Constructor for the Browser object */
81    public Browser()
82    {
83       logger.debug("JMX Browser starting...");
84
85       // Search for custom Look and Feel
86
LookAndFeelUtil.setLookAndFeel();
87
88       // Set the hyperlink navigation for ObjectName
89
if (System.getProperty("ejtools.objectname.hyperlink") == null)
90       {
91          System.setProperty("ejtools.objectname.hyperlink", "true");
92       }
93
94       this.frameService = new MDIFrameServiceProvider();
95       this.aboutService = new AboutServiceProvider(new AboutDialog());
96       this.menuBarService = new MenuBarServiceProvider();
97       this.toolBarService = new ToolBarServiceProvider();
98       this.factoryProvider = new ProfileServiceProvider(new ConnectionMetaData());
99       this.consoleService = new ConsoleServiceProvider();
100       this.graphService = new GraphServiceProvider();
101       this.historyService = new HistoryServiceProvider(this, 4);
102       this.stateManager = new WorkbenchState(this);
103
104       try
105       {
106          this.add(this.menuBarService);
107          this.add(this.toolBarService);
108
109          this.add(new NewAction(
110             new Command()
111             {
112                public void execute()
113                {
114                   int idx = -1;
115                   Profile[] datas = Browser.this.factoryProvider.getProfiles();
116                   List JavaDoc factories = Arrays.asList(datas);
117                   if (factories.size() > 1)
118                   {
119                      Object JavaDoc selectedValue =
120                         JOptionPane.showInputDialog(
121                         null,
122                         resources.getString("connection.dialog.text.description"),
123                         resources.getString("connection.dialog.title"),
124                         JOptionPane.QUESTION_MESSAGE,
125                         null,
126                         factories.toArray(),
127                         factories.get(0)
128                         );
129
130                      if (selectedValue == null)
131                      {
132                         return;
133                      }
134
135                      idx = factories.indexOf(selectedValue);
136                   }
137                   else if (factories.size() == 1)
138                   {
139                      idx = 0;
140                   }
141
142                   if (idx >= 0)
143                   {
144                      Profile profile = Browser.this.factoryProvider.getProfile(idx);
145                      ServerInternalFrame frame = new ServerInternalFrame();
146                      frame.setProfile(profile);
147                      frame.setServer(new Server());
148                      Browser.this.add(frame);
149                   }
150                }
151             }
152             ));
153
154          this.add(this.stateManager);
155
156          this.add(new OpenWorkspaceAction(
157             new Command()
158             {
159                public void execute()
160                {
161                   try
162                   {
163                      File JavaDoc selectedFile = FileTools.selectFile(resources.getString("file.dialog.title.load"), resources.getString("file.dialog.button.load"), JFileChooser.OPEN_DIALOG, WorkspaceFileTools.WORKSPACE_FILE_FILTER);
164                      if (selectedFile != null)
165                      {
166                         Browser.this.loadResource(selectedFile.toURL(), null);
167                      }
168                   }
169                   catch (Exception JavaDoc e)
170                   {
171                      // JOptionPane.showMessageDialog(null, "Could not load file:" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
172
logger.error("Error while loading workspace", e);
173                   }
174                }
175             }
176             ));
177
178          this.add(new SaveWorkspaceAction(
179             new Command()
180             {
181                public void execute()
182                {
183                   try
184                   {
185                      if (Browser.this.stateManager.getWorkbenchURL() == null)
186                      {
187                         File JavaDoc selectedFile = FileTools.selectFile(resources.getString("file.dialog.title.save"), resources.getString("file.dialog.button.save"), JFileChooser.SAVE_DIALOG, WorkspaceFileTools.WORKSPACE_FILE_FILTER);
188                         if (selectedFile != null)
189                         {
190                            Browser.this.stateManager.setWorkbenchURL(selectedFile.toURL());
191                         }
192                      }
193                      if (Browser.this.stateManager.getWorkbenchURL() != null)
194                      {
195                         Browser.this.stateManager.store();
196                      }
197                   }
198                   catch (Exception JavaDoc e)
199                   {
200                      logger.error("Error while saving workspace", e);
201                   }
202                }
203             }
204             ));
205
206          this.add(new SaveAsWorkspaceAction(
207             new Command()
208             {
209                public void execute()
210                {
211                   try
212                   {
213                      File JavaDoc selectedFile = FileTools.selectFile(resources.getString("file.dialog.title.save"), resources.getString("file.dialog.button.save"), JFileChooser.SAVE_DIALOG, WorkspaceFileTools.WORKSPACE_FILE_FILTER);
214                      if (selectedFile != null)
215                      {
216                         Browser.this.stateManager.setWorkbenchURL(selectedFile.toURL());
217                         Browser.this.stateManager.store();
218                      }
219                   }
220                   catch (Exception JavaDoc e)
221                   {
222                      logger.error("Error while saving workspace", e);
223                   }
224                }
225             }
226             ));
227
228          this.add(this.historyService);
229          this.add(this.consoleService);
230
231          this.add(new ExitAction(
232             new Command()
233             {
234                public void execute()
235                {
236                   Browser.this.quit();
237                }
238             }
239             ));
240
241          this.add(this.frameService);
242          this.add(this.aboutService);
243
244          this.add(this.graphService);
245
246          this.frameService.setTitle(resources.getString("application.title"));
247
248          // Add a window hook
249
this.frameService.addWindowListener(
250             new WindowAdapter JavaDoc()
251             {
252                public void windowClosing(WindowEvent JavaDoc e)
253                {
254                   super.windowClosing(e);
255                   Browser.this.quit();
256                }
257             });
258       }
259       catch (RuntimeException JavaDoc e)
260       {
261          logger.error("An error occured", e);
262          throw e;
263       }
264    }
265
266
267    /**
268     * Description of the Method
269     *
270     * @param url Description of the Parameter
271     * @param context Description of the Parameter
272     */

273    public void loadResource(URL JavaDoc url, Object JavaDoc context)
274    {
275       this.stateManager.setWorkbenchURL(url);
276       this.stateManager.load();
277       this.historyService.push(url, context);
278    }
279
280
281    /** Description of the Method */
282    public void quit()
283    {
284       this.clear();
285       System.exit(0);
286    }
287 }
288
Popular Tags