KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > netbeans > module > WSDLTreeViewMultiViewElement


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.wsdl.ui.netbeans.module;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import javax.swing.AbstractAction JavaDoc;
28 import javax.swing.Action JavaDoc;
29 import javax.swing.ActionMap JavaDoc;
30 import javax.swing.InputMap JavaDoc;
31 import javax.swing.JComponent JavaDoc;
32 import javax.swing.JToolBar JavaDoc;
33 import javax.swing.KeyStroke JavaDoc;
34 import javax.swing.UIManager JavaDoc;
35 import javax.swing.text.DefaultEditorKit JavaDoc;
36 import org.netbeans.core.api.multiview.MultiViewHandler;
37 import org.netbeans.core.api.multiview.MultiViewPerspective;
38 import org.netbeans.core.api.multiview.MultiViews;
39 import org.netbeans.core.spi.multiview.CloseOperationState;
40 import org.netbeans.core.spi.multiview.MultiViewElement;
41 import org.netbeans.core.spi.multiview.MultiViewElementCallback;
42 import org.netbeans.core.spi.multiview.MultiViewFactory;
43 import org.netbeans.modules.xml.validation.ShowCookie;
44 import org.netbeans.modules.xml.validation.ValidateAction;
45 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
46 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.WSDLSettings.ViewMode;
47 import org.netbeans.modules.xml.xam.Component;
48 import org.netbeans.modules.xml.xam.dom.DocumentComponent;
49 import org.netbeans.modules.xml.xam.spi.Validator.ResultItem;
50 import org.netbeans.modules.xml.xam.ui.category.Category;
51 import org.netbeans.modules.xml.xam.ui.category.CategoryPane;
52 import org.netbeans.modules.xml.xam.ui.category.DefaultCategoryPane;
53 import org.netbeans.modules.xml.xam.ui.multiview.ActivatedNodesMediator;
54 import org.netbeans.modules.xml.xam.ui.multiview.CookieProxyLookup;
55 import org.openide.actions.FindAction;
56 import org.openide.awt.UndoRedo;
57 import org.openide.explorer.ExplorerManager;
58 import org.openide.explorer.ExplorerUtils;
59 import org.openide.nodes.Node;
60 import org.openide.util.HelpCtx;
61 import org.openide.util.Lookup;
62 import org.openide.util.NbBundle;
63 import org.openide.util.actions.CallbackSystemAction;
64 import org.openide.util.actions.SystemAction;
65 import org.openide.util.lookup.Lookups;
66 import org.openide.windows.Mode;
67 import org.openide.windows.TopComponent;
68 import org.openide.windows.TopComponentGroup;
69 import org.openide.windows.WindowManager;
70
71 /**
72  * @author radval
73  *
74  * To change the template for this generated type comment go to
75  * Window - Preferences - Java - Code Generation - Code and Comments
76  */

77 public class WSDLTreeViewMultiViewElement extends TopComponent
78         implements MultiViewElement, ExplorerManager.Provider {
79     private static final long serialVersionUID = -655912409997381426L;
80     private static final String JavaDoc ACTIVATED_NODES = "activatedNodes";//NOI18N
81
private ExplorerManager manager;
82     private WSDLDataObject mObj;
83     private CategoryPane categoryPane;
84     private transient MultiViewElementCallback multiViewObserver;
85     private transient javax.swing.JLabel JavaDoc errorLabel = new javax.swing.JLabel JavaDoc();
86     private transient JToolBar JavaDoc mToolbar;
87
88     public WSDLTreeViewMultiViewElement() {
89         super();
90     }
91
92     public WSDLTreeViewMultiViewElement(WSDLDataObject dObj) {
93         super();
94         this.mObj = dObj;
95         initialize();
96     }
97
98     private void initialize() {
99     manager = new ExplorerManager();
100         // Install our own actions.
101
CallbackSystemAction globalFindAction =
102                 (CallbackSystemAction) SystemAction.get(FindAction.class);
103         Object JavaDoc mapKey = globalFindAction.getActionMapKey();
104         Action JavaDoc action = new WSDLFindAction();
105         ActionMap JavaDoc map = getActionMap();
106         map.put(mapKey, action);
107         map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager));
108         map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(manager));
109         map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(manager));
110         map.put("delete", ExplorerUtils.actionDelete(manager, false));
111
112         // Define the keyboard shortcuts for the actions.
113
InputMap JavaDoc keys = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
114         KeyStroke JavaDoc key = (KeyStroke JavaDoc) globalFindAction.getValue(Action.ACCELERATOR_KEY);
115         if (key == null) {
116             key = KeyStroke.getKeyStroke("control F");
117         }
118         keys.put(key, mapKey);
119
120         //show cookie
121
ShowCookie showCookie = new ShowCookie() {
122             
123             public void show(ResultItem resultItem) {
124                 Component JavaDoc component = resultItem.getComponents();
125                 if (categoryPane != null && component instanceof DocumentComponent) {
126                     categoryPane.getCategory().showComponent(component);
127                 }
128             }
129         };
130
131         Node delegate = mObj.getNodeDelegate();
132         ActivatedNodesMediator nodesMediator =
133                 new ActivatedNodesMediator(delegate);
134         nodesMediator.setExplorerManager(this);
135         CookieProxyLookup cpl = new CookieProxyLookup(new Lookup[] {
136             Lookups.fixed(new Object JavaDoc[] {
137                 // Need ActionMap in lookup so our actions are used.
138
map,
139                 // Need the data object registered in the lookup so that the
140
// projectui code will close our open editor windows when the
141
// project is closed.
142
mObj,
143                 // The Show Cookie in lookup to show the component
144
showCookie
145             }),
146             nodesMediator.getLookup(),
147             // The Node delegate Lookup must be the last one in the list
148
// for the CookieProxyLookup to work properly.
149
delegate.getLookup(),
150         }, delegate);
151         associateLookup(cpl);
152         addPropertyChangeListener(ACTIVATED_NODES, nodesMediator);
153         addPropertyChangeListener(ACTIVATED_NODES, cpl);
154
155         setLayout(new BorderLayout JavaDoc());
156
157         initUI();
158     }
159
160     public ExplorerManager getExplorerManager() {
161     return manager;
162     }
163
164     @Override JavaDoc
165     public int getPersistenceType() {
166         return PERSISTENCE_NEVER;
167     }
168     
169     public void setMultiViewCallback(final MultiViewElementCallback callback) {
170         multiViewObserver = callback;
171     }
172
173     public CloseOperationState canCloseElement() {
174         // if this is not the last cloned xml editor component, closing is OK
175
if (!WSDLEditorSupport.isLastView(multiViewObserver.getTopComponent())) {
176             return CloseOperationState.STATE_OK;
177         }
178         // return a placeholder state - to be sure our CloseHandler is called
179
return MultiViewFactory.createUnsafeCloseState(
180                 "ID_TEXT_CLOSING", // dummy ID // NOI18N
181
MultiViewFactory.NOOP_CLOSE_ACTION,
182                 MultiViewFactory.NOOP_CLOSE_ACTION);
183     }
184
185     @Override JavaDoc
186     public UndoRedo getUndoRedo() {
187     return mObj.getWSDLEditorSupport().getUndoManager();
188     }
189
190     @Override JavaDoc
191     public void componentHidden() {
192         super.componentHidden();
193         if (categoryPane != null) {
194             Category cat = categoryPane.getCategory();
195             if (cat != null) {
196                 cat.componentHidden();
197             }
198         }
199     }
200     
201     @Override JavaDoc
202     public void componentClosed() {
203         super.componentClosed();
204     }
205     
206     @Override JavaDoc
207     public void componentOpened() {
208         super.componentOpened();
209     }
210     
211     @Override JavaDoc
212     public void componentActivated() {
213         super.componentActivated();
214     ExplorerUtils.activateActions(manager, true);
215         mObj.getWSDLEditorSupport().syncModel();
216         updateGroupVisibility(false);
217     }
218     
219     @Override JavaDoc
220     public void componentDeactivated() {
221     ExplorerUtils.activateActions(manager, false);
222         super.componentDeactivated();
223         updateGroupVisibility(true);
224     }
225     
226     @Override JavaDoc
227     public void componentShowing() {
228         super.componentShowing();
229         initUI();
230
231         if (categoryPane != null) {
232             Category cat = categoryPane.getCategory();
233             if (cat != null) {
234                 cat.componentShown();
235             }
236         }
237     }
238
239     @SuppressWarnings JavaDoc("deprecation")
240     public void requestFocus() {
241         super.requestFocus();
242         // For Help to work properly, need to take focus.
243
if (categoryPane != null) {
244             categoryPane.getComponent().requestFocus();
245         }
246     }
247
248     @SuppressWarnings JavaDoc("deprecation")
249     public boolean requestFocusInWindow() {
250         boolean retVal = super.requestFocusInWindow();
251         // For Help to work properly, need to take focus.
252
if (categoryPane != null) {
253             return categoryPane.getComponent().requestFocusInWindow();
254         }
255         return retVal;
256     }
257
258     public HelpCtx getHelpCtx() {
259     return new HelpCtx(WSDLTreeViewMultiViewDesc.class);
260     }
261
262     private static Boolean JavaDoc groupVisible = null;
263     
264     private void updateGroupVisibility(boolean closeGroup) {
265         WindowManager wm = WindowManager.getDefault();
266         final TopComponentGroup group = wm.findTopComponentGroup("wsdl_ui"); // NOI18N
267
if (group == null) {
268             return; // group not found (should not happen)
269
}
270         //
271
boolean isWSDLViewSelected = false;
272         Iterator JavaDoc it = wm.getModes().iterator();
273         while (it.hasNext()) {
274             Mode mode = (Mode) it.next();
275             TopComponent selected = mode.getSelectedTopComponent();
276             if (selected != null) {
277             MultiViewHandler mvh = MultiViews.findMultiViewHandler(selected);
278                 if (mvh != null) {
279                     MultiViewPerspective mvp = mvh.getSelectedPerspective();
280                     if (mvp != null) {
281                         String JavaDoc id = mvp.preferredID();
282                         if (WSDLTreeViewMultiViewDesc.PREFERRED_ID.equals(id)) {
283                             isWSDLViewSelected = true;
284                             break;
285                         }
286                     }
287                 }
288             }
289         }
290         //
291
if (isWSDLViewSelected && !Boolean.TRUE.equals(groupVisible)) {
292             group.open();
293         } else if (!isWSDLViewSelected && !Boolean.FALSE.equals(groupVisible)) {
294             group.close();
295         }
296         //
297
groupVisible = isWSDLViewSelected ? Boolean.TRUE : Boolean.FALSE;
298         
299     }
300
301     @Override JavaDoc
302     protected String JavaDoc preferredID() {
303         return "WSDLTreeViewMultiViewElementTC"; // NOI18N
304
}
305
306     /**
307      * Construct the user interface.
308      */

309     private void initUI() {
310         WSDLEditorSupport editor = mObj.getWSDLEditorSupport();
311         WSDLModel wsdlModel = null;
312         String JavaDoc errorMessage = null;
313         try {
314             wsdlModel = editor.getModel();
315             if (wsdlModel != null &&
316                     wsdlModel.getState() == WSDLModel.State.VALID) {
317                 // Construct the standard editor interface.
318
if (categoryPane == null) {
319                     Lookup lookup = getLookup();
320                     categoryPane = new DefaultCategoryPane();
321                     Category tree = new WSDLTreeCategory(wsdlModel, lookup);
322                     categoryPane.addCategory(tree);
323                     Category columns = new WSDLColumnsCategory(wsdlModel, lookup);
324                     categoryPane.addCategory(columns);
325                     // Set the default view according to the persisted setting.
326
ViewMode mode = WSDLSettings.getDefault().getViewMode();
327                     switch (mode) {
328                         case COLUMN:
329                             categoryPane.setCategory(columns);
330                             break;
331                         case TREE:
332                             categoryPane.setCategory(tree);
333                             break;
334                     }
335                 }
336                 removeAll();
337                 add(categoryPane.getComponent(), BorderLayout.CENTER);
338                 return;
339             }
340         } catch (IOException JavaDoc ex) {
341             errorMessage = ex.getMessage();
342         }
343
344         // If it comes here, either the model is not well-formed or invalid.
345
if (wsdlModel == null ||
346                 wsdlModel.getState() == WSDLModel.State.NOT_WELL_FORMED) {
347             if (errorMessage == null) {
348                 errorMessage = NbBundle.getMessage(
349                         WSDLTreeViewMultiViewElement.class,
350                         "MSG_NotWellformedWsdl");
351             }
352         }
353
354         // Clear the interface and show the error message.
355
removeAll();
356         errorLabel.setText("<" + errorMessage + ">");
357         errorLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
358         errorLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
359         errorLabel.setEnabled(false);
360         Color JavaDoc usualWindowBkg = UIManager.getColor("window"); //NOI18N
361
errorLabel.setBackground(usualWindowBkg != null ? usualWindowBkg :
362             Color.white);
363         errorLabel.setOpaque(true);
364         add(errorLabel, BorderLayout.CENTER);
365     }
366
367     public javax.swing.JComponent JavaDoc getToolbarRepresentation() {
368         if (mToolbar == null) {
369             try {
370             WSDLModel model = mObj.getWSDLEditorSupport().getModel();
371             if (model != null && model.getState() == WSDLModel.State.VALID) {
372                 mToolbar = new JToolBar JavaDoc();
373                 mToolbar.setFloatable(false);
374                 if (categoryPane != null) {
375                     mToolbar.addSeparator();
376                     categoryPane.populateToolbar(mToolbar);
377                 }
378                 mToolbar.addSeparator();
379                 mToolbar.add(new ValidateAction(model));
380             }
381             } catch (IOException JavaDoc e) {
382                 //wait until the model is loaded
383
}
384         }
385         return mToolbar;
386     }
387     
388     public javax.swing.JComponent JavaDoc getVisualRepresentation() {
389         return this;
390     }
391
392     /**
393      * Find action for WSDL editor.
394      *
395      * @author Nathan Fiedler
396      */

397     private class WSDLFindAction extends AbstractAction JavaDoc {
398         /** silence compiler warnings */
399         private static final long serialVersionUID = 1L;
400
401         /**
402          * Creates a new instance of WSDLFindAction.
403          */

404         public WSDLFindAction() {
405         }
406
407         public void actionPerformed(ActionEvent JavaDoc event) {
408             WSDLTreeViewMultiViewElement parent =
409                     WSDLTreeViewMultiViewElement.this;
410             if (parent.categoryPane != null) {
411                 CategoryPane pane = parent.categoryPane;
412                 pane.getSearchComponent().showComponent();
413             }
414         }
415     }
416 }
417
Popular Tags