KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > config > ui > ConfigBeanTopComponent


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * ConfigBeanTopComponent.java
22  *
23  * Created on March 6, 2003, 2:09 PM
24  */

25
26 package org.netbeans.modules.j2ee.sun.share.config.ui;
27
28
29 import java.awt.BorderLayout JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.*;
32
33 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
34 import javax.swing.JButton JavaDoc;
35 import javax.swing.JComponent JavaDoc;
36 import javax.swing.JSplitPane JavaDoc;
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 /**
58  *
59  * @author Jeri Lockhart
60  */

61 public class ConfigBeanTopComponent extends CloneableTopComponent
62 {
63     
64 // private static int identitySource;
65
// private final int identity;
66
//
67
// public int getIdentity() {
68
// return identity;
69
// }
70

71     private ConfigurationStorage storage = null;
72     private Node rootNode = null; // Root node for the editor's explorer
73
private TwoPanelComponentPanel componentPanel;
74     
75     public static final String JavaDoc APPLICATION_ICON_NORMAL =
76         "org/netbeans/modules/j2ee/sun/share/config/ui/resources/application.gif"; // NOI18N
77
public static final String JavaDoc EJBMODULE_ICON_NORMAL =
78         "org/netbeans/modules/j2ee/sun/share/config/ui/resources/ejbmodule.gif"; // NOI18N
79
public static final String JavaDoc WEBMODULE_ICON_NORMAL =
80         "org/netbeans/modules/j2ee/sun/share/config/ui/resources/webmodule.gif"; // NOI18N
81
// Also these types:
82
// ModuleType.RAR;
83
// ModuleType.CAR;
84

85     private boolean appConfig = false;
86
87     
88     /** default constructor for deserialization */
89     public ConfigBeanTopComponent() {
90 // identity = ++identitySource;
91

92         // hint to windows system
93
putClientProperty("PersistenceType", "Never"); //NOI18N
94
// putClientProperty("PersistenceType", "OnlyOpened"); // NOI18N
95
// System.out.println("ConfigBeanTopComponent::ctor id #" + getIdentity());
96
}
97     
98     /** Creates a new instance of ConfigBeanTopComponent */
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 JavaDoc name) {
110         if(name != null && name.startsWith("sun-ejb-jar")) {
111             name = "sun-ejb-jar.xml / sun-cmp-mappings.xml";
112         }
113         
114 // new Exception("Who called ConfigBeanTopComponent.setName( '" + name + "' ) -- ").printStackTrace();
115
super.setName(name);
116     }
117
118 // public String getName() {
119
// String result;
120
// result = super.getName();
121
// System.out.println("CBTC.getName() returned '" + result + "'");
122
// return result;
123
// }
124
//
125
// public String getDisplayName() {
126
// String result;
127
// result = super.getDisplayName();
128
// System.out.println("CBTC.getDisplayName() returned '" + result + "'");
129
// return result;
130
// }
131
//
132
// public void setDisplayName(String displayName) {
133
// System.out.println("CBTC.setDisplayName() called with '" + displayName + "'");
134
// new Exception("Who called ConfigBeanTopComponent.setDisplayName( '" + displayName + "' ) -- ").printStackTrace();
135
// super.setDisplayName(displayName);
136
// }
137

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); //todo: get currently selected node.
176
//open();
177
} else {
178                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL,
179                     "ConfigBeanTopComponent: empty top level node list. Root: " + rootNode + ", topNodes: " + topNodes);
180             }
181         } catch (java.lang.Exception JavaDoc e) {
182             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
183         }
184     }
185     
186     public int getPersistenceType () {
187         return PERSISTENCE_NEVER;
188     }
189     
190     protected String JavaDoc preferredID() {
191         String JavaDoc preferredID = ""; // NOI18N
192
ConfigDataObject configDO = getConfigDataObject();
193         if(configDO != null) {
194             preferredID = configDO.getPrimaryFile().getPath();
195         }
196         return preferredID;
197     }
198
199     /** Initializes this instance. Used by construction and deserialization. */
200     public void initialize() {
201 // System.out.println("ConfigBeanTopComponent::initialize() called on #" + getIdentity());
202
ConfigDataObject configDO = getConfigDataObject();
203         Node selNode = configDO.getNodeDelegate ();
204
205         try {
206             if (getConfigStorage() == null) {
207                 throw new IllegalArgumentException JavaDoc("ConfigDataObject without ConfigurationStorage cookie!"); //NOI18N
208
}
209         } catch (RuntimeException JavaDoc ex) {
210             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
211             return;
212         }
213         
214         initComponents();
215         
216         //setName(NbBundle.getMessage (ConfigBeanTopComponent.class, "LBL_Server_specific_settings", selNode.getDisplayName()));
217
setName(selNode.getDisplayName());
218         String JavaDoc fsName = "";
219         FileObject fo = configDO.getPrimaryFile ();
220         try {
221             fsName = fo.getFileSystem ().getDisplayName () + "/" + fo.getPath (); //NOI18N
222
} 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         // Only one activated node at a time, otherwise <Save> enabling doesn't work. See IZ 65225
232
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")); //NOI18N
237
}
238     
239     public synchronized void reset() {
240 // System.out.println("ConfigBeanTopComponent::reset() called on #" + getIdentity());
241
rootNode = null;
242         componentPanel = null;
243         appConfig = false;
244     }
245     
246     /** Inits the subcomponents. Sets layout for this top component .
247      * @see BundleEditPanel */

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 JavaDoc());
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 JavaDoc ie) {
263             ErrorManager.getDefault().log(ie.getMessage());
264         } catch (java.lang.Exception JavaDoc 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         /*J2eeModule prov = getProvider();
279         if (prov instanceof J2eeModuleContainer) {
280             appConfig = true;
281             J2eeModuleContainer appProv = (J2eeModuleContainer) prov;
282             ArrayList childArr = new ArrayList();
283             // The first child is the App's ConfigBeanNode
284             Node[] nodes = getConfigStorage().getMainNodes();
285             for (int i = 0; i < nodes.length; i++) {
286                 childArr.add(nodes[i]);
287             }
288             // Add nodes for modules
289             J2eeModule[] mods = appProv.getModules(getModuleContainerListener());
290             for (int i = 0; i < mods.length; i++) {
291                 childArr.addAll(Arrays.asList(createModuleNode(mods[i])));
292             }
293             // create the app Node
294             children.add((Node[]) childArr.toArray(new Node[childArr.size()]));
295             root = new AbstractNode(children);
296             root.setName(filterRoot.getName());
297             root.setIconBaseWithExtension(APPLICATION_ICON_NORMAL);
298         } else */
{ // not a j2ee app
299
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 // System.out.println("ConfigBeanTopComponent::componentClosed() called on #" + getIdentity());
327
super.componentClosed();
328         ConfigDataObject configDO = getConfigDataObject();
329         if (configDO != null) {
330             configDO.editorClosed(this);
331         } else {
332 // ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new IllegalStateException(
333
// "ConfigBeanTopComponent.componentClosed(): Top component has null dataobject!")); // NOI18N
334
// No data object means file was deleted from underneath the editor.
335
// System.out.println("No data object for config editor being closed.");
336
// System.out.println(" Storage is " + storage);
337
}
338     }
339     
340     public void open() {
341 // System.out.println("ConfigBeanTopComponent::open() called on #" + getIdentity());
342
super.open();
343         //refresh();
344
}
345     
346   /*
347    * Find the "root" App, Module or Ear Node for this node or subnode
348    * If the currNode is the "root", return it.
349    */

350     // private Node getRoot(Node currNode) {
351
// Node parent = currNode.getParentNode();
352
// if (parent != null) {
353
// if (parent.getCookie(J2eeComponentProvider.class) == null) {
354
// return currNode;
355
// }
356
// return getRoot(parent);
357
// }
358
// return currNode;
359
// }
360

361     
362     public boolean closeLast() {
363 // System.out.println("ConfigBeanTopComponent::closeLast() called on #" + getIdentity());
364
super.closeLast();
365         ConfigDataObject configDO = getConfigDataObject();
366         if (configDO != null && configDO.isModified ()) {
367             
368             ResourceBundle bundle = NbBundle.getBundle(ConfigBeanTopComponent.class);
369             
370             String JavaDoc msg = NbBundle.getMessage(ConfigBeanTopComponent.class, "MSG_ConfirmSave", configDO.getName()); // NOI18N
371

372             JButton JavaDoc saveOption = new JButton JavaDoc(bundle.getString("CTL_Save")); // NOI18N
373
saveOption.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Save")); // NOI18N
374
saveOption.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_CTL_Save")); // NOI18N
375
JButton JavaDoc discardOption = new JButton JavaDoc(bundle.getString("CTL_Discard")); // NOI18N
376
discardOption.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Discard")); // NOI18N
377
discardOption.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_CTL_Discard")); // NOI18N
378
discardOption.setMnemonic(bundle.getString("CTL_Discard_Mnemonic").charAt (0)); // NOI18N
379

380             NotifyDescriptor nd = new NotifyDescriptor(
381                 msg,
382                 bundle.getString("LBL_SaveFile_Title"), // NOI18N
383
NotifyDescriptor.YES_NO_CANCEL_OPTION,
384                 NotifyDescriptor.QUESTION_MESSAGE,
385                 new Object JavaDoc[] {saveOption, discardOption, NotifyDescriptor.CANCEL_OPTION},
386                 saveOption
387             );
388                 
389             Object JavaDoc 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 JavaDoc e) {
399 // This message is displayed in SaveCookie.save(), called above.
400
// DialogDisplayer.getDefault().notify(new Message(e.getMessage(), NotifyDescriptor.ERROR_MESSAGE));
401
// Error performing save, don't close.
402
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 JavaDoc ex) {
410                      // Added log message here because we were getting a ConcurrentModificationException
411
// (caused by coding bug) but the message box was empty.
412
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
413                      DialogDisplayer.getDefault().notify(new Message(ex.getMessage(), NotifyDescriptor.ERROR_MESSAGE));
414                  }
415              } else { // user pressed Cancel - don't close
416
return false;
417              }
418          }
419         // remove module nodes from app node
420
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     /**
439      * Overrides superclass method.
440      * Is called from the superclass <code>clone<code> method to create new component from this one.
441      * This implementation only clones the object by calling super.clone method.
442      * @return the copy of this object
443      */

444     protected CloneableTopComponent createClonedObject() {
445 // System.out.println("ConfigBeanTopComponent::createClonedObject() called on #" + getIdentity());
446
return new ConfigBeanTopComponent(storage);
447     }
448
449     private class TwoPanelComponentPanel extends ComponentPanel {
450         
451         protected TwoPanelComponentPanel(){
452             super();
453         }
454         /**
455          * Creates a new instance of ComponentPanel
456          * @param panel The PanelView which will provide the node tree for the structure view
457          * and the set of panels the nodes map to.
458          */

459         public TwoPanelComponentPanel(PanelView panel){
460             super(panel);
461         }
462         
463         public TwoPanelComponentPanel(PanelView panel, boolean rootVisible){
464             super(panel);
465             JComponent JavaDoc 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 JavaDoc(JSplitPane.HORIZONTAL_SPLIT,getContentView(), getStructureView());
491             else
492                 split1 = new JSplitPane JavaDoc(JSplitPane.HORIZONTAL_SPLIT,getStructureView(), getContentView());
493             //split1.setDividerLocation(DEFAULT_CONTENT_SPLIT);
494
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 JavaDoc {
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