KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > jndi > 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.jndi.browser;
8
9 import java.awt.event.WindowAdapter JavaDoc;
10 import java.awt.event.WindowEvent JavaDoc;
11 import java.beans.beancontext.BeanContextServicesSupport JavaDoc;
12 import java.io.File JavaDoc;
13 import java.net.URL JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.ResourceBundle JavaDoc;
17
18 import javax.swing.JFileChooser JavaDoc;
19 import javax.swing.JOptionPane JavaDoc;
20
21 import org.apache.log4j.Logger;
22 import org.ejtools.adwt.LookAndFeelUtil;
23 import org.ejtools.adwt.action.Command;
24 import org.ejtools.adwt.action.file.ExitAction;
25 import org.ejtools.adwt.action.file.NewAction;
26 import org.ejtools.adwt.action.file.OpenWorkspaceAction;
27 import org.ejtools.adwt.action.file.SaveAsWorkspaceAction;
28 import org.ejtools.adwt.action.file.SaveWorkspaceAction;
29 import org.ejtools.adwt.service.AboutServiceProvider;
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.jndi.browser.frame.ServerInternalFrame;
36 import org.ejtools.jndi.browser.model.Server;
37 import org.ejtools.jndi.browser.model.service.ConnectionMetaData;
38 import org.ejtools.jndi.browser.state.WorkbenchState;
39 import org.ejtools.util.FileTools;
40 import org.ejtools.util.service.Profile;
41 import org.ejtools.util.service.ProfileServiceProvider;
42 import org.ejtools.util.state.WorkspaceFileTools;
43
44 /**
45  * Main JavaBean of the application. Contains all the JavaBean Services such as MenuBarService, FrameService and AboutService.
46  *
47  * @author letiemble
48  * @version $Revision: 1.4 $
49  * @created 21 mars 2002
50  */

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

229                public void windowClosing(WindowEvent JavaDoc e)
230                {
231                   super.windowClosing(e);
232                   Browser.this.quit();
233                }
234             });
235       }
236       catch (RuntimeException JavaDoc e)
237       {
238          logger.fatal("An error occured " + e.getMessage());
239          throw e;
240       }
241    }
242
243
244    /**
245     * Description of the Method
246     *
247     * @param url Description of the Parameter
248     * @param context Description of the Parameter
249     */

250    public void loadResource(URL JavaDoc url, Object JavaDoc context)
251    {
252       this.stateManager.setWorkbenchURL(url);
253       this.stateManager.load();
254       this.historyService.push(url, context);
255    }
256
257
258    /** Quit method */
259    public void quit()
260    {
261       System.exit(0);
262    }
263 }
264
Popular Tags