1 7 package org.ejtools.jndi.browser; 8 9 import java.awt.event.WindowAdapter ; 10 import java.awt.event.WindowEvent ; 11 import java.beans.beancontext.BeanContextServicesSupport ; 12 import java.io.File ; 13 import java.net.URL ; 14 import java.util.Arrays ; 15 import java.util.List ; 16 import java.util.ResourceBundle ; 17 18 import javax.swing.JFileChooser ; 19 import javax.swing.JOptionPane ; 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 51 public class Browser extends BeanContextServicesSupport implements HistoryService.Holder 52 { 53 54 protected AboutServiceProvider aboutService; 55 56 protected ProfileServiceProvider factoryProvider; 57 58 protected MDIFrameServiceProvider frameService; 59 60 protected HistoryServiceProvider historyService; 61 62 protected MenuBarServiceProvider menuBarService; 63 64 protected WorkbenchState stateManager; 65 66 protected ToolBarServiceProvider toolBarService; 67 68 69 private static Logger logger = Logger.getLogger(Browser.class); 70 71 private static ResourceBundle resources = ResourceBundle.getBundle("org.ejtools.jndi.browser.Resources"); 72 73 74 75 public Browser() 76 { 77 logger.debug("JNDI Browser starting..."); 78 79 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 factories = Arrays.asList(datas); 103 if (factories.size() > 1) 104 { 105 Object 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 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 e) 147 { 148 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 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 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 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 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 this.frameService.addWindowListener( 224 new WindowAdapter () 225 { 226 229 public void windowClosing(WindowEvent e) 230 { 231 super.windowClosing(e); 232 Browser.this.quit(); 233 } 234 }); 235 } 236 catch (RuntimeException e) 237 { 238 logger.fatal("An error occured " + e.getMessage()); 239 throw e; 240 } 241 } 242 243 244 250 public void loadResource(URL url, Object context) 251 { 252 this.stateManager.setWorkbenchURL(url); 253 this.stateManager.load(); 254 this.historyService.push(url, context); 255 } 256 257 258 259 public void quit() 260 { 261 System.exit(0); 262 } 263 } 264 | Popular Tags |