1 7 package org.ejtools.jmx.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.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 54 public class Browser extends CustomBeanContextServicesSupport implements HistoryService.Holder 55 { 56 57 protected AboutServiceProvider aboutService; 58 59 protected ConsoleServiceProvider consoleService; 60 61 protected ProfileServiceProvider factoryProvider; 62 63 protected MDIFrameServiceProvider frameService; 64 65 protected GraphServiceProvider graphService; 66 67 protected HistoryServiceProvider historyService; 68 69 protected MenuBarServiceProvider menuBarService; 70 71 protected WorkbenchState stateManager; 72 73 protected ToolBarServiceProvider toolBarService; 74 75 private static Logger logger = Logger.getLogger(Browser.class); 76 77 private static ResourceBundle resources = ResourceBundle.getBundle("org.ejtools.jmx.browser.Resources"); 78 79 80 81 public Browser() 82 { 83 logger.debug("JMX Browser starting..."); 84 85 LookAndFeelUtil.setLookAndFeel(); 87 88 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 factories = Arrays.asList(datas); 117 if (factories.size() > 1) 118 { 119 Object 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 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 e) 170 { 171 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 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 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 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 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 this.frameService.addWindowListener( 250 new WindowAdapter () 251 { 252 public void windowClosing(WindowEvent e) 253 { 254 super.windowClosing(e); 255 Browser.this.quit(); 256 } 257 }); 258 } 259 catch (RuntimeException e) 260 { 261 logger.error("An error occured", e); 262 throw e; 263 } 264 } 265 266 267 273 public void loadResource(URL url, Object context) 274 { 275 this.stateManager.setWorkbenchURL(url); 276 this.stateManager.load(); 277 this.historyService.push(url, context); 278 } 279 280 281 282 public void quit() 283 { 284 this.clear(); 285 System.exit(0); 286 } 287 } 288 | Popular Tags |