View Javadoc
1 package net.sf.panoptes.view; 2 import java.io.InputStream; 3 import java.lang.reflect.Constructor; 4 import java.util.HashMap; 5 import java.util.HashSet; 6 import java.util.Iterator; 7 import java.util.Set; 8 9 import net.sf.panoptes.controller.MainController; 10 import net.sf.panoptes.model.component.Component; 11 import net.sf.panoptes.model.component.ComponentGroup; 12 import net.sf.panoptes.model.component.registry.ComponentNotFoundException; 13 import net.sf.panoptes.model.component.registry.ConfiguratorEntry; 14 import net.sf.panoptes.model.node.Node; 15 import net.sf.panoptes.model.node.RootNode; 16 import net.sf.panoptes.model.node.SymLinkNode; 17 import net.sf.panoptes.module.log4j.LoggerConsole; 18 import net.sf.panoptes.module.log4j.LoggerTable; 19 import net.sf.panoptes.view.configurator.NodeConfigurator; 20 21 import org.eclipse.jface.dialogs.MessageDialog; 22 import org.eclipse.jface.viewers.ISelectionChangedListener; 23 import org.eclipse.jface.viewers.SelectionChangedEvent; 24 import org.eclipse.jface.viewers.StructuredSelection; 25 import org.eclipse.jface.viewers.TreeViewer; 26 import org.eclipse.jface.window.Window; 27 import org.eclipse.swt.SWT; 28 import org.eclipse.swt.custom.SashForm; 29 import org.eclipse.swt.dnd.DND; 30 import org.eclipse.swt.dnd.DragSource; 31 import org.eclipse.swt.dnd.DragSourceEvent; 32 import org.eclipse.swt.dnd.DragSourceListener; 33 import org.eclipse.swt.dnd.DropTarget; 34 import org.eclipse.swt.dnd.DropTargetAdapter; 35 import org.eclipse.swt.dnd.DropTargetEvent; 36 import org.eclipse.swt.dnd.TextTransfer; 37 import org.eclipse.swt.dnd.Transfer; 38 import org.eclipse.swt.graphics.Font; 39 import org.eclipse.swt.graphics.FontData; 40 import org.eclipse.swt.graphics.Image; 41 import org.eclipse.swt.layout.GridData; 42 import org.eclipse.swt.layout.GridLayout; 43 import org.eclipse.swt.widgets.Composite; 44 import org.eclipse.swt.widgets.Control; 45 import org.eclipse.swt.widgets.Display; 46 import org.eclipse.swt.widgets.Event; 47 import org.eclipse.swt.widgets.Label; 48 import org.eclipse.swt.widgets.Listener; 49 import org.eclipse.swt.widgets.Menu; 50 import org.eclipse.swt.widgets.MenuItem; 51 import org.eclipse.swt.widgets.Shell; 52 import org.eclipse.swt.widgets.TabFolder; 53 import org.eclipse.swt.widgets.TabItem; 54 import org.eclipse.swt.widgets.TreeItem; 55 56 /*** 57 * MainWindow.java 58 * 59 * This class is a bit monolithic and does not cleanly separate controller 60 * from view at the moment. Currently this windows has all child views as components 61 * directly on it. I believe that a more MDI-like interface could be better in the 62 * future. Especially if we implement interval refresh, graphing etc. 63 * 64 * @author Dag Liodden 65 * @version 0.1 66 */ 67 public class MainWindow extends Window { 68 69 private LoggerConsole loggerConsole; 70 private RootNode rootNode; 71 private MainController controller; 72 73 74 /*** 75 * The TabFolder containing the configurators 76 */ 77 private TabFolder tabFolder = null; 78 79 /*** 80 * List of the current configurators so that they can be refreshed 81 */ 82 private final Set currentConfigurators = new HashSet(); 83 private final Set currentConfiguratorEntries = new HashSet(); 84 85 86 SashForm sash = null; 87 88 /*** 89 * TreeViewer for all elements in the console 90 */ 91 TreeViewer tree = null; 92 93 /*** 94 * Panel for displaying log events 95 */ 96 LoggerTable loggerPanel = null; 97 98 /*** 99 * Constructor for MainWindow. 100 */ 101 public MainWindow(MainController controller, RootNode rootNode) { 102 super(null); 103 this.controller = controller; 104 this.rootNode = rootNode; 105 } 106 107 /*** 108 * @see org.eclipse.jface.window.Window#createContents(org.eclipse.swt.widgets.Composite) 109 */ 110 protected Control createContents(Composite parent) { 111 getShell().setText("JBoss Management Console"); 112 parent.setBounds(0, 0, 1024, 700); 113 setShellStyle(SWT.TITLE); 114 115 /*** 116 * Create a really simple menu 117 */ 118 Menu bar = new Menu(getShell(), SWT.BAR); 119 getShell().setMenuBar(bar); 120 121 MenuItem item; 122 item = new MenuItem(bar, SWT.CASCADE); 123 item.setText("File"); 124 Menu submenu = new Menu(getShell(), SWT.DROP_DOWN); 125 item.setMenu(submenu); 126 127 item = new MenuItem(submenu, 0); 128 item.setText("Register new JMX Server (CTRL+R) - Dunno how to set the CTRL+R stuff automatically :)"); 129 item.setAccelerator(SWT.CTRL | 'R'); 130 item.addListener(SWT.Selection, new Listener() { 131 public void handleEvent(Event e) { 132 controller.getMBeanConnectionController().newMBeanConnectionWizard(); 133 } 134 }); 135 136 item = new MenuItem(submenu, 0); 137 item.setText("Refresh"); 138 item.addListener(SWT.Selection, new Listener() { 139 public void handleEvent(Event e) { 140 tree.refresh(true); 141 } 142 }); 143 144 item = new MenuItem(submenu, 0); 145 item.setText("E&xit"); 146 item.addListener(SWT.Selection, new Listener() { 147 public void handleEvent(Event e) { 148 System.exit(0); 149 } 150 }); 151 152 GridLayout layout = new GridLayout(); 153 layout.marginWidth = 0; 154 layout.marginHeight = 2; 155 parent.setLayout(layout); 156 157 GridData gd; 158 159 /*** 160 * Create the top panel which includes the JBoss logo on the right 161 */ 162 Composite top = new Composite(parent, SWT.NULL); 163 top.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE)); 164 top.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE)); 165 top.setLayout(new GridLayout(2, false)); 166 gd = new GridData(GridData.FILL_HORIZONTAL); 167 top.setLayoutData(gd); 168 169 Label splash; 170 171 splash = new Label(top, SWT.NULL); 172 splash.setText("JBoss Management Console"); 173 Font font = Display.getCurrent().getSystemFont(); 174 FontData fontData = font.getFontData()[0]; 175 fontData.height = 14; 176 fontData.setStyle(SWT.BOLD); 177 font = new Font(Display.getCurrent(), fontData); 178 splash.setFont(font); 179 splash.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE)); 180 splash.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL)); 181 182 splash = new Label(top, SWT.RIGHT); 183 splash.setImage(new Image(Display.getCurrent(), getResourceAsStream("icons/jboss.gif"))); 184 185 // Build the separator line 186 Label separator = new Label(parent, SWT.HORIZONTAL | SWT.SEPARATOR); 187 separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 188 189 /*** 190 * Sash / Splitter which separates the tree from the details 191 */ 192 sash = new SashForm(parent, SWT.HORIZONTAL); 193 sash.setLayoutData(new GridData(GridData.FILL_BOTH)); 194 sash.setLayout(new GridLayout(2, true)); 195 196 /*** 197 * Copmonent Tree 198 */ 199 tree = new TreeViewer(sash, SWT.BORDER); 200 tree.setContentProvider(new TreeContentProvider()); 201 tree.setLabelProvider(new TreeLabelProvider()); 202 tree.getTree().setMenu(createNodeMenu(getShell())); 203 204 tree.setInput(rootNode); 205 tree.getTree().setLayoutData(new GridData(GridData.BEGINNING)); 206 setupDND(); 207 208 /*** 209 * Parent panel on the right 210 */ 211 final Composite rightPanel = new Composite(sash, SWT.BORDER); 212 rightPanel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL)); 213 rightPanel.setLayout(new GridLayout(1, false)); 214 215 /*** 216 * Top right panel with white background, description of the selected item 217 * in the tree and an image on the far right end 218 */ 219 Composite rightTopPanel = new Composite(rightPanel, SWT.NULL); 220 rightTopPanel.setLayout(new GridLayout(2, false)); 221 rightTopPanel.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE)); 222 rightTopPanel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 223 224 // The selection description 225 final Label configurableDescription = new Label(rightTopPanel, SWT.NULL); 226 font = Display.getCurrent().getSystemFont(); 227 fontData = font.getFontData()[0]; 228 fontData.height = 10; 229 fontData.setStyle(SWT.BOLD); 230 font = new Font(Display.getCurrent(), fontData); 231 configurableDescription.setFont(font); 232 configurableDescription.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE)); 233 configurableDescription.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL)); 234 235 // The image 236 splash = new Label(rightTopPanel, SWT.RIGHT); 237 splash.setImage(new Image(Display.getCurrent(), getResourceAsStream("icons/troodon_logo_left.gif"))); 238 239 splash = new Label(rightPanel, SWT.SEPARATOR | SWT.HORIZONTAL); 240 splash.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 241 242 /*** 243 * Selection listener for the tree. Should perhaps be separated into a new class to avoid clutter 244 */ 245 tree.addSelectionChangedListener(new ISelectionChangedListener() { 246 public void selectionChanged(SelectionChangedEvent event) { 247 StructuredSelection selection = (StructuredSelection) event.getSelection(); 248 249 250 /*** 251 * Fetch the selected node and get the component it represents 252 */ 253 Node node = (Node) selection.getFirstElement(); 254 255 String script = null; 256 Component component = null; 257 if (node instanceof Component) { 258 component = (Component) node; 259 } else if (node instanceof SymLinkNode) { 260 try { 261 component = ((SymLinkNode) node).dereference(); 262 } catch (ComponentNotFoundException e) { 263 return; 264 } 265 } else { 266 return; //throw new RuntimeException("Invalid element in tree, " + node.toString()); 267 } 268 controller.setCurrentNode(component); 269 270 /*** 271 * Fetch the configurators that match this node 272 */ 273 Set configuratorEntries = rootNode.getConfigRegistry().findConfigurators(component.getComponentName()); 274 275 /*** 276 * If we have the same set of entries, keep'em so we, among other things, 277 * can select a tab, and then browse similar components with the same 278 * configurator 279 */ 280 if (configuratorEntries.equals(currentConfiguratorEntries)) { 281 282 } else { 283 /*** 284 * Dispose previous tabs 285 */ 286 if (!(getTabFolder() == null)) 287 getTabFolder().dispose(); 288 TabFolder folder = new TabFolder(rightPanel, SWT.NULL); 289 folder.setLayoutData(new GridData(GridData.FILL_BOTH)); 290 setTabFolder(folder); 291 292 /*** 293 * Create the configurators 294 */ 295 currentConfigurators.clear(); 296 for (Iterator i = configuratorEntries.iterator(); i.hasNext();) { 297 ConfiguratorEntry ce = (ConfiguratorEntry) i.next(); 298 Constructor constructor; 299 try { 300 constructor = 301 ce.getClazz().getConstructor( 302 new Class[] { MainController.class, Composite.class, int.class, HashMap.class }); 303 NodeConfigurator configurator = 304 (NodeConfigurator) constructor.newInstance( 305 new Object[] { controller, folder, new Integer(SWT.NULL), ce.getAttributes()}); 306 TabItem ti = new TabItem(folder, SWT.NULL); 307 ti.setText(ce.getName()); 308 ti.setControl(configurator); 309 currentConfigurators.add(configurator); 310 } catch (Exception e) { 311 e.printStackTrace(); 312 } 313 314 } 315 // Update the set 316 currentConfiguratorEntries.clear(); 317 currentConfiguratorEntries.addAll(configuratorEntries); 318 } 319 320 /*** 321 * Loop through the configurators and set the component 322 */ 323 for (Iterator i = currentConfigurators.iterator(); i.hasNext();) { 324 NodeConfigurator configurator = (NodeConfigurator) i.next(); 325 if (component != null) configurator.setNode(component); 326 else configurator.setNode(node); 327 } 328 329 configurableDescription.setText(node.getConfigDescriptor().getDescription()); 330 rightPanel.layout(true); 331 332 } 333 }); 334 335 tree.refresh(); 336 sash.setWeights(new int[] { 30, 70 }); 337 338 return sash; 339 } 340 341 private void setupDND() { 342 Transfer[] types = new Transfer[] { TextTransfer.getInstance()}; 343 int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK; 344 345 final DragSource source = new DragSource(tree.getTree(), operations); 346 source.setTransfer(types); 347 final TreeItem[] dragSourceItem = new TreeItem[1]; 348 source.addDragListener(new DragSourceListener() { 349 public void dragStart(DragSourceEvent event) { 350 TreeItem[] selection = tree.getTree().getSelection(); 351 if (selection.length > 0 && (selection[0].getData() instanceof Node)) { 352 event.doit = true; 353 dragSourceItem[0] = selection[0]; 354 } else { 355 event.doit = false; 356 } 357 }; 358 public void dragSetData(DragSourceEvent event) { 359 event.data = dragSourceItem[0].getData(); 360 } 361 public void dragFinished(DragSourceEvent event) { 362 if (event.detail == DND.DROP_COPY) 363 dragSourceItem[0].dispose(); 364 dragSourceItem[0] = null; 365 } 366 }); 367 368 DropTarget target = new DropTarget(tree.getTree(), operations); 369 target.setTransfer(types); 370 target.addDropListener(new DropTargetAdapter() { 371 372 public void dragOver(DropTargetEvent event) { 373 if ((event.item.getData() instanceof Node) && ((Node) event.item.getData()).acceptsChildren()) { 374 event.feedback = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL | DND.FEEDBACK_SELECT; 375 event.detail = DND.DROP_LINK; 376 System.out.println("OK"); 377 } else { 378 event.feedback = DND.FEEDBACK_NONE; 379 event.detail = DND.DROP_NONE; 380 System.out.println("NotOK"); 381 } 382 } 383 public void dragOperationChanged(DropTargetEvent event) { 384 385 } 386 387 public void drop(DropTargetEvent event) { 388 if (event.item == null) { 389 } else { 390 TreeItem parent = (TreeItem) event.item; 391 392 if (parent.getData() instanceof ComponentGroup) { 393 Node configurable = (Node) dragSourceItem[0].getData(); 394 ((ComponentGroup) parent.getData()).addChild(configurable); 395 tree.refresh(parent.getData(), true); 396 } 397 } 398 } 399 }); 400 } 401 402 /*** 403 * @param shell 404 * @return Menu 405 */ 406 private Menu createNodeMenu(Shell shell) { 407 Menu menu = new Menu(shell); 408 MenuItem item; 409 410 item = new MenuItem(menu, SWT.PUSH); 411 item.setText("Refresh"); 412 item.addListener(SWT.Selection, new Listener() { 413 public void handleEvent(Event arg0) { 414 tree.refresh(true); 415 } 416 }); 417 418 /* item = new MenuItem(menu, SWT.PUSH); 419 item.setText("Create group"); 420 421 item = new MenuItem(menu, SWT.PUSH); 422 item.setText("Delete group"); 423 424 item = new MenuItem(menu, SWT.SEPARATOR); 425 426 item = new MenuItem(menu, SWT.PUSH); 427 item.setText("Copy"); 428 429 item = new MenuItem(menu, SWT.PUSH); 430 item.setText("Paste"); */ 431 432 return menu; 433 } 434 435 436 /*** 437 * Helper method for displaying errors. All errors should be shown with this method so the display method is similar 438 * for all parts of the application. 439 * 440 * @param title 441 * @param message 442 * @param cause 443 */ 444 public void showError(String title, String message, Throwable cause) { 445 MessageDialog.openError(getShell(), title, message); 446 } 447 448 protected InputStream getResourceAsStream(String uri) { 449 return getClass().getClassLoader().getResourceAsStream(uri); 450 } 451 452 public void refresh(Node component) { 453 tree.refresh(component, true); 454 } 455 456 /*** 457 * @return 458 */ 459 public TabFolder getTabFolder() { 460 return tabFolder; 461 } 462 463 /*** 464 * @param folder 465 */ 466 public void setTabFolder(TabFolder folder) { 467 tabFolder = folder; 468 } 469 470 /*** 471 * @return 472 */ 473 public Set getCurrentConfigurators() { 474 return currentConfigurators; 475 } 476 477 }

This page was automatically generated by Maven