1 19 20 25 26 package org.netbeans.modules.j2ee.sun.share.config.ui; 27 28 29 import java.awt.BorderLayout ; 30 import java.io.IOException ; 31 import java.util.*; 32 33 import javax.enterprise.deploy.shared.ModuleType ; 34 import javax.swing.JButton ; 35 import javax.swing.JComponent ; 36 import javax.swing.JSplitPane ; 37 38 import org.openide.*; 39 import org.openide.NotifyDescriptor.Message; 40 import org.openide.cookies.SaveCookie; 41 import org.openide.explorer.view.TreeView; 42 import org.openide.filesystems.FileObject; 43 import org.openide.nodes.*; 44 import org.openide.nodes.Children.Array; 45 import org.openide.windows.*; 46 import org.openide.util.NbBundle; 47 import org.openide.util.Utilities; 48 import org.openide.util.HelpCtx; 49 50 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 51 52 import org.netbeans.modules.j2ee.sun.share.config.ConfigDataObject; 53 import org.netbeans.modules.j2ee.sun.share.config.ConfigurationStorage; 54 import org.netbeans.modules.j2ee.sun.share.config.SecondaryConfigDataObject; 55 56 57 61 public class ConfigBeanTopComponent extends CloneableTopComponent 62 { 63 64 71 private ConfigurationStorage storage = null; 72 private Node rootNode = null; private TwoPanelComponentPanel componentPanel; 74 75 public static final String APPLICATION_ICON_NORMAL = 76 "org/netbeans/modules/j2ee/sun/share/config/ui/resources/application.gif"; public static final String EJBMODULE_ICON_NORMAL = 78 "org/netbeans/modules/j2ee/sun/share/config/ui/resources/ejbmodule.gif"; public static final String WEBMODULE_ICON_NORMAL = 80 "org/netbeans/modules/j2ee/sun/share/config/ui/resources/webmodule.gif"; 85 private boolean appConfig = false; 86 87 88 89 public ConfigBeanTopComponent() { 90 92 putClientProperty("PersistenceType", "Never"); } 97 98 99 public ConfigBeanTopComponent(ConfigurationStorage storage) { 100 this(); 101 this.storage = storage; 102 initialize(); 103 } 104 105 public ConfigDataObject getConfigDataObject() { 106 return storage.getPrimaryDataObject(); 107 } 108 109 public void setName(String name) { 110 if(name != null && name.startsWith("sun-ejb-jar")) { 111 name = "sun-ejb-jar.xml / sun-cmp-mappings.xml"; 112 } 113 114 super.setName(name); 116 } 117 118 138 public boolean isFor(FileObject document) { 139 boolean result = false; 140 ConfigDataObject configDO = getConfigDataObject(); 141 if(configDO != null) { 142 result = configDO.getPrimaryFile().equals(document); 143 } 144 return result; 145 } 146 147 public boolean isFor(ConfigurationStorage otherStorage) { 148 return getConfigStorage() == otherStorage; 149 } 150 151 private ConfigurationStorage getConfigStorage() { 152 return storage; 153 } 154 155 public static ConfigBeanTopComponent findByConfigStorage(ConfigurationStorage configStorage) { 156 Iterator it = TopComponent.getRegistry().getOpened().iterator(); 157 while (it.hasNext()) { 158 TopComponent tc = (TopComponent) it.next(); 159 if (tc instanceof ConfigBeanTopComponent) { 160 ConfigBeanTopComponent beanTC = (ConfigBeanTopComponent) tc; 161 if (configStorage == beanTC.getConfigStorage()) { 162 return beanTC; 163 } 164 } 165 } 166 return null; 167 } 168 169 public void refresh() { 170 try { 171 rootNode = buildTree(); 172 Node [] topNodes = rootNode.getChildren().getNodes(); 173 if(topNodes.length > 0) { 174 Node mainNode = topNodes[0]; 175 componentPanel.refresh(rootNode, mainNode); } else { 178 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, 179 "ConfigBeanTopComponent: empty top level node list. Root: " + rootNode + ", topNodes: " + topNodes); 180 } 181 } catch (java.lang.Exception e) { 182 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 183 } 184 } 185 186 public int getPersistenceType () { 187 return PERSISTENCE_NEVER; 188 } 189 190 protected String preferredID() { 191 String preferredID = ""; ConfigDataObject configDO = getConfigDataObject(); 193 if(configDO != null) { 194 preferredID = configDO.getPrimaryFile().getPath(); 195 } 196 return preferredID; 197 } 198 199 200 public void initialize() { 201 ConfigDataObject configDO = getConfigDataObject(); 203 Node selNode = configDO.getNodeDelegate (); 204 205 try { 206 if (getConfigStorage() == null) { 207 throw new IllegalArgumentException ("ConfigDataObject without ConfigurationStorage cookie!"); } 209 } catch (RuntimeException ex) { 210 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 211 return; 212 } 213 214 initComponents(); 215 216 setName(selNode.getDisplayName()); 218 String fsName = ""; 219 FileObject fo = configDO.getPrimaryFile (); 220 try { 221 fsName = fo.getFileSystem ().getDisplayName () + "/" + fo.getPath (); } catch (org.openide.filesystems.FileStateInvalidException fse) { 223 fsName = fo.getPath (); 224 } 225 char sep = java.io.File.separatorChar; 226 char another = sep == '/' ? '\\' : '/'; 227 fsName = fsName.replace (another, sep); 228 setToolTipText (fsName); 229 Node [] topNodes = rootNode.getChildren().getNodes(); 230 231 Node [] activatedNode = new Node[1]; 233 activatedNode[0] = (topNodes.length > 0) ? topNodes[0] : selNode; 234 setActivatedNodes(activatedNode); 235 236 setIcon (Utilities.loadImage ("org/netbeans/modules/j2ee/sun/share/config/ui/resources/ConfigFile.gif")); } 238 239 public synchronized void reset() { 240 rootNode = null; 242 componentPanel = null; 243 appConfig = false; 244 } 245 246 248 private void initComponents() { 249 try { 250 rootNode = buildTree(); 251 Node [] topNodes = rootNode.getChildren().getNodes(); 252 Node mainNode = null; 253 if(topNodes.length > 0) { 254 mainNode = topNodes[0]; 255 } 256 setLayout(new BorderLayout ()); 257 PanelView panelView = new ConfigBeanPanelView(rootNode); 258 componentPanel = new TwoPanelComponentPanel(panelView, appConfig); 259 add(BorderLayout.CENTER, componentPanel); 260 componentPanel.getExplorerManager().setSelectedNodes(new Node[] { mainNode }); 261 panelView.showSelection(new Node[] { mainNode }); 262 } catch (IllegalArgumentException ie) { 263 ErrorManager.getDefault().log(ie.getMessage()); 264 } catch (java.lang.Exception e) { 265 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 266 } 267 } 268 269 private Node getMainNode() { 270 return getConfigStorage().getMainNode(); 271 } 272 273 private Node buildTree() { 274 ConfigDataObject configDO = getConfigDataObject(); 275 Node filterRoot = configDO.getNodeDelegate(); 276 AbstractNode root = null; 277 Array children = new Array(); 278 { Node[] beanNodes = getConfigStorage().getMainNodes(); 300 children.add(beanNodes); 301 root = new AbstractNode(children); 302 } 303 return root; 304 } 305 306 private Node[] createModuleNode(J2eeModule module) { 307 Array modChildren = new Array(); 308 Node[] modConfigBeanNode = getConfigStorage().getNodes(module); 309 for (int j = 0; j < modConfigBeanNode.length; j++) { 310 if (modConfigBeanNode[j] != null) { 311 modChildren.add(new Node[] { modConfigBeanNode[j] }); 312 } 313 } 314 315 AbstractNode modNode = new AbstractNode(modChildren); 316 modNode.setName(module.getUrl()); 317 if (module.getModuleType() == ModuleType.EJB) { 318 modNode.setIconBaseWithExtension(EJBMODULE_ICON_NORMAL); 319 } else if (module.getModuleType() == ModuleType.WAR) { 320 modNode.setIconBaseWithExtension(WEBMODULE_ICON_NORMAL); 321 } 322 return new Node[] { modNode }; 323 } 324 325 protected void componentClosed () { 326 super.componentClosed(); 328 ConfigDataObject configDO = getConfigDataObject(); 329 if (configDO != null) { 330 configDO.editorClosed(this); 331 } else { 332 } 338 } 339 340 public void open() { 341 super.open(); 343 } 345 346 350 361 362 public boolean closeLast() { 363 super.closeLast(); 365 ConfigDataObject configDO = getConfigDataObject(); 366 if (configDO != null && configDO.isModified ()) { 367 368 ResourceBundle bundle = NbBundle.getBundle(ConfigBeanTopComponent.class); 369 370 String msg = NbBundle.getMessage(ConfigBeanTopComponent.class, "MSG_ConfirmSave", configDO.getName()); 372 JButton saveOption = new JButton (bundle.getString("CTL_Save")); saveOption.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Save")); saveOption.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_CTL_Save")); JButton discardOption = new JButton (bundle.getString("CTL_Discard")); discardOption.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Discard")); discardOption.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_CTL_Discard")); discardOption.setMnemonic(bundle.getString("CTL_Discard_Mnemonic").charAt (0)); 380 NotifyDescriptor nd = new NotifyDescriptor( 381 msg, 382 bundle.getString("LBL_SaveFile_Title"), NotifyDescriptor.YES_NO_CANCEL_OPTION, 384 NotifyDescriptor.QUESTION_MESSAGE, 385 new Object [] {saveOption, discardOption, NotifyDescriptor.CANCEL_OPTION}, 386 saveOption 387 ); 388 389 Object ret = DialogDisplayer.getDefault().notify(nd); 390 391 if(saveOption.equals(ret)){ 392 try { 393 SaveCookie sc = (SaveCookie)configDO.getCookie (SaveCookie.class); 394 if (sc != null) { 395 sc.save (); 396 } 397 } 398 catch (IOException e) { 399 return false; 403 } 404 } else if (discardOption.equals(ret)){ 405 try { 406 configDO.setModified (false); 407 configDO.resetAllChanged(); 408 getConfigStorage().load(); 409 } catch (java.lang.Exception ex) { 410 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 413 DialogDisplayer.getDefault().notify(new Message(ex.getMessage(), NotifyDescriptor.ERROR_MESSAGE)); 414 } 415 } else { return false; 417 } 418 } 419 if (appConfig = true && rootNode != null) { 421 Children modules = rootNode.getChildren(); 422 Node[] modNodes = modules.getNodes(); 423 for (int i = 0; i < modNodes.length; i++) { 424 if (modNodes[i] instanceof AbstractNode ) { 425 AbstractNode mod = (AbstractNode)modNodes[i]; 426 Children ch = mod.getChildren(); 427 ch.remove(ch.getNodes()); 428 } 429 } 430 modules.remove(modules.getNodes()); 431 432 } 433 434 435 return true; 436 } 437 438 444 protected CloneableTopComponent createClonedObject() { 445 return new ConfigBeanTopComponent(storage); 447 } 448 449 private class TwoPanelComponentPanel extends ComponentPanel { 450 451 protected TwoPanelComponentPanel(){ 452 super(); 453 } 454 459 public TwoPanelComponentPanel(PanelView panel){ 460 super(panel); 461 } 462 463 public TwoPanelComponentPanel(PanelView panel, boolean rootVisible){ 464 super(panel); 465 JComponent view = getStructureView(); 466 if (view instanceof TreeView) { 467 TreeView tree = (TreeView) view; 468 tree.setRootVisible(rootVisible); 469 } 470 } 471 472 public HelpCtx getHelpCtx () { 473 Node[] nodes = this.getExplorerManager().getSelectedNodes(); 474 if (nodes.length > 0) { 475 return nodes[0].getHelpCtx(); 476 } else if (rootNode != null) { 477 return rootNode.getHelpCtx(); 478 } else { 479 ConfigDataObject configDO = getConfigDataObject(); 480 if (configDO != null) { 481 return configDO.getHelpCtx(); 482 } else { 483 return HelpCtx.DEFAULT_HELP; 484 } 485 } 486 } 487 488 protected void createHorizontalSplit() { 489 if (panelOrientation == 1) 490 split1 = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT,getContentView(), getStructureView()); 491 else 492 split1 = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT,getStructureView(), getContentView()); 493 split1.setDividerSize (4); 495 } 496 497 protected void createVerticalSplit() { 498 499 } 500 501 protected PanelView getPanelView() { 502 return (PanelView) contentView; 503 } 504 505 public void refresh(Node root, Node selected) throws java.beans.PropertyVetoException { 506 getPanelView().setRoot(root); 507 setRootContext(root); 508 getExplorerManager().setSelectedNodes(new Node[] { selected }); 509 getPanelView().showSelection(new Node[] { selected }); 510 } 511 } 512 } 513 | Popular Tags |