1 7 package org.ejtools.management.browser; 8 9 import java.awt.event.WindowAdapter ; 10 import java.awt.event.WindowEvent ; 11 import java.io.File ; 12 import java.net.URL ; 13 import java.util.Arrays ; 14 import java.util.List ; 15 import java.util.ResourceBundle ; 16 17 import javax.swing.JFileChooser ; 18 import javax.swing.JOptionPane ; 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.management.browser.frame.ServerInternalFrame; 38 import org.ejtools.management.browser.model.Server; 39 import org.ejtools.management.browser.model.service.ConnectionMetaData; 40 import org.ejtools.management.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 54 public final class Browser extends CustomBeanContextServicesSupport implements HistoryService.Holder 55 { 56 private AboutServiceProvider aboutService; 57 private ConsoleServiceProvider consoleService; 58 private ProfileServiceProvider factoryProvider; 59 private MDIFrameServiceProvider frameService; 60 private GraphServiceProvider graphService; 61 private HistoryServiceProvider historyService; 62 private MenuBarServiceProvider menuBarService; 63 private WorkbenchState stateManager; 64 private ToolBarServiceProvider toolBarService; 65 66 67 private static Logger logger = Logger.getLogger(Browser.class); 68 69 private static ResourceBundle resources = ResourceBundle.getBundle("org.ejtools.management.browser.Resources"); 70 71 72 73 public Browser() 74 { 75 logger.debug("Management Browser starting..."); 76 77 LookAndFeelUtil.setLookAndFeel(); 79 80 if (System.getProperty("ejtools.objectname.hyperlink") == null) 82 { 83 System.setProperty("ejtools.objectname.hyperlink", "true"); 84 } 85 86 this.frameService = new MDIFrameServiceProvider(); 87 this.aboutService = new AboutServiceProvider(new AboutDialog()); 88 this.menuBarService = new MenuBarServiceProvider(); 89 this.toolBarService = new ToolBarServiceProvider(); 90 this.factoryProvider = new ProfileServiceProvider(new ConnectionMetaData()); 91 this.consoleService = new ConsoleServiceProvider(); 92 this.graphService = new GraphServiceProvider(); 93 this.historyService = new HistoryServiceProvider(this, 4); 94 this.stateManager = new WorkbenchState(this); 95 96 try 97 { 98 this.add(this.menuBarService); 99 this.add(this.toolBarService); 100 101 this.add(new NewAction( 102 new Command() 103 { 104 public void execute() 105 { 106 int idx = -1; 107 Profile[] datas = Browser.this.factoryProvider.getProfiles(); 108 List factories = Arrays.asList(datas); 109 if (factories.size() > 1) 110 { 111 Object selectedValue = JOptionPane.showInputDialog( 112 null, 113 resources.getString("connection.dialog.text.description"), 114 resources.getString("connection.dialog.title"), 115 JOptionPane.QUESTION_MESSAGE, 116 null, 117 factories.toArray(), 118 factories.get(0) 119 ); 120 121 if (selectedValue == null) 122 { 123 return; 124 } 125 126 idx = factories.indexOf(selectedValue); 127 } 128 else if (factories.size() == 1) 129 { 130 idx = 0; 131 } 132 133 if (idx >= 0) 134 { 135 Profile profile = Browser.this.factoryProvider.getProfile(idx); 136 ServerInternalFrame frame = new ServerInternalFrame(); 137 frame.setProfile(profile); 138 frame.setServer(new Server()); 139 Browser.this.add(frame); 140 } 141 } 142 } 143 )); 144 145 this.add(this.stateManager); 146 147 this.add(new OpenWorkspaceAction( 148 new Command() 149 { 150 public void execute() 151 { 152 try 153 { 154 File selectedFile = FileTools.selectFile(resources.getString("file.dialog.title.load"), resources.getString("file.dialog.button.load"), JFileChooser.OPEN_DIALOG, WorkspaceFileTools.WORKSPACE_FILE_FILTER); 155 if (selectedFile != null) 156 { 157 Browser.this.loadResource(selectedFile.toURL(), null); 158 } 159 } 160 catch (Exception e) 161 { 162 logger.error("Error while loading workspace", e); 164 } 165 } 166 } 167 )); 168 169 this.add(new SaveWorkspaceAction( 170 new Command() 171 { 172 public void execute() 173 { 174 try 175 { 176 if (Browser.this.stateManager.getWorkbenchURL() == null) 177 { 178 File selectedFile = FileTools.selectFile(resources.getString("file.dialog.title.save"), resources.getString("file.dialog.button.save"), JFileChooser.SAVE_DIALOG, WorkspaceFileTools.WORKSPACE_FILE_FILTER); 179 if (selectedFile != null) 180 { 181 Browser.this.stateManager.setWorkbenchURL(selectedFile.toURL()); 182 } 183 } 184 if (Browser.this.stateManager.getWorkbenchURL() != null) 185 { 186 Browser.this.stateManager.store(); 187 } 188 } 189 catch (Exception e) 190 { 191 logger.error("Error while saving workspace", e); 192 } 193 } 194 } 195 )); 196 197 this.add(new SaveAsWorkspaceAction( 198 new Command() 199 { 200 public void execute() 201 { 202 try 203 { 204 File selectedFile = FileTools.selectFile(resources.getString("file.dialog.title.save"), resources.getString("file.dialog.button.save"), JFileChooser.SAVE_DIALOG, WorkspaceFileTools.WORKSPACE_FILE_FILTER); 205 if (selectedFile != null) 206 { 207 Browser.this.stateManager.setWorkbenchURL(selectedFile.toURL()); 208 Browser.this.stateManager.store(); 209 } 210 } 211 catch (Exception e) 212 { 213 logger.error("Error while saving workspace", e); 214 } 215 } 216 } 217 )); 218 219 this.add(this.historyService); 220 this.add(this.consoleService); 221 222 this.add(new ExitAction( 223 new Command() 224 { 225 public void execute() 226 { 227 Browser.this.quit(); 228 } 229 } 230 )); 231 232 this.add(this.frameService); 233 this.add(this.aboutService); 234 this.add(this.graphService); 235 236 this.frameService.setTitle(resources.getString("application.title")); 237 238 this.frameService.addWindowListener( 240 new WindowAdapter () 241 { 242 public void windowClosing(WindowEvent e) 243 { 244 super.windowClosing(e); 245 Browser.this.quit(); 246 } 247 }); 248 } 249 catch (RuntimeException e) 250 { 251 logger.error("An error occured", e); 252 throw e; 253 } 254 } 255 256 257 263 public void loadResource(URL url, Object context) 264 { 265 this.stateManager.setWorkbenchURL(url); 266 this.stateManager.load(); 267 this.historyService.push(url, context); 268 } 269 270 271 272 public void quit() 273 { 274 this.clear(); 275 System.exit(0); 276 } 277 } 278 | Popular Tags |