KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
21  * WSDLDesignMultiViewElement.java
22  *
23  * Created on 2006/08/15, 20:43
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.wsdl.ui.netbeans.module;
30
31 import java.awt.BorderLayout JavaDoc;
32 import java.awt.Color JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.ObjectInput JavaDoc;
35 import java.io.ObjectOutput JavaDoc;
36 import java.util.Date JavaDoc;
37 import javax.swing.Action JavaDoc;
38 import javax.swing.ActionMap JavaDoc;
39 import javax.swing.InputMap JavaDoc;
40 import javax.swing.JComponent JavaDoc;
41 import javax.swing.JToolBar JavaDoc;
42 import javax.swing.KeyStroke JavaDoc;
43 import javax.swing.UIManager JavaDoc;
44 import javax.swing.text.DefaultEditorKit JavaDoc;
45 import org.netbeans.core.spi.multiview.CloseOperationState;
46 import org.netbeans.core.spi.multiview.MultiViewElement;
47 import org.netbeans.core.spi.multiview.MultiViewElementCallback;
48 import org.netbeans.core.spi.multiview.MultiViewFactory;
49 import org.netbeans.modules.print.spi.PrintProvider;
50 import org.netbeans.modules.print.spi.PrintProviderCookie;
51 import org.netbeans.modules.xml.validation.ShowCookie;
52 import org.netbeans.modules.xml.validation.ValidateAction;
53 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
54 import org.netbeans.modules.xml.wsdl.ui.view.grapheditor.GraphView;
55 import org.netbeans.modules.xml.wsdl.ui.view.grapheditor.palette.WSDLPaletteFactory;
56 import org.netbeans.modules.xml.xam.Component;
57 import org.netbeans.modules.xml.xam.spi.Validator.ResultItem;
58 import org.netbeans.modules.xml.xam.ui.multiview.ActivatedNodesMediator;
59 import org.netbeans.modules.xml.xam.ui.multiview.CookieProxyLookup;
60 import org.openide.actions.SaveAction;
61 import org.openide.awt.UndoRedo;
62 import org.openide.explorer.ExplorerManager;
63 import org.openide.explorer.ExplorerUtils;
64 import org.openide.nodes.Node;
65 import org.openide.util.HelpCtx;
66 import org.openide.util.Lookup;
67 import org.openide.util.NbBundle;
68 import org.openide.util.actions.SystemAction;
69 import org.openide.util.lookup.Lookups;
70 import org.openide.windows.TopComponent;
71
72 /**
73  * TopComponent MultiViewElement for the WSDL graphical editor.
74  *
75  * @author radval
76  * @author Nathan Fiedler
77  */

78 public class WSDLDesignMultiViewElement extends TopComponent
79         implements MultiViewElement, ExplorerManager.Provider {
80     private static final long serialVersionUID = -655912409997381426L;
81     private static final String JavaDoc ACTIVATED_NODES = "activatedNodes";//NOI18N
82
/** The WSDL DataObject this view represents. */
83     private WSDLDataObject wsdlDataObject;
84     /** Used to enable various node actions, used in the graphical view. */
85     private ExplorerManager explorerManager;
86     /** The component which displays the graphical editor. */
87     private GraphView graphComponent;
88     private transient MultiViewElementCallback multiViewObserver;
89     private transient javax.swing.JLabel JavaDoc errorLabel = new javax.swing.JLabel JavaDoc();
90     private transient JToolBar JavaDoc mToolbar;
91     
92     public WSDLDesignMultiViewElement() {
93         super();
94     }
95     
96     public WSDLDesignMultiViewElement(WSDLDataObject dObj) {
97         wsdlDataObject = dObj;
98         initialize();
99     }
100     
101     private void initialize() {
102         // Enable widgets to get the keyboard focus.
103
setFocusable(true);
104         // This is needed so that copy/paste and delete node actions are
105
// enabled in the context menu of the widgets, which are based
106
// on the underlying nodes. In addition, the ExplorerUtils.
107
// createLookup() call below is required.
108
explorerManager = new ExplorerManager();
109         ActionMap JavaDoc map = getActionMap();
110         map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(explorerManager));
111         map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(explorerManager));
112         map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(explorerManager));
113         map.put("delete", ExplorerUtils.actionDelete(explorerManager, false));
114
115         // For some reason, the Ctrl-s is not in our action map (issue 94698).
116
SaveAction saveAction = (SaveAction) SystemAction.get(SaveAction.class);
117         map.put("save", saveAction);
118         InputMap JavaDoc keys = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
119         KeyStroke JavaDoc key = (KeyStroke JavaDoc) saveAction.getValue(Action.ACCELERATOR_KEY);
120         if (key == null) {
121             key = KeyStroke.getKeyStroke("control S");
122         }
123         keys.put(key, "save");
124
125         //show cookie
126
ShowCookie showCookie = new ShowCookie() {
127             public void showComponent(Component JavaDoc component,
128                     String JavaDoc errorMessage, String JavaDoc recommmendedCorrection) {
129                 assert false:"ShowCookie.showComponent method is deprecated";
130             }
131
132             public void show(ResultItem resultItem) {
133                 Component JavaDoc component = resultItem.getComponents();
134                 graphComponent.showComponent(component);
135             }
136         };
137
138         Node delegate = wsdlDataObject.getNodeDelegate();
139         ActivatedNodesMediator nodesMediator =
140                 new ActivatedNodesMediator(delegate);
141         nodesMediator.setExplorerManager(this);
142         CookieProxyLookup cpl = new CookieProxyLookup(new Lookup[] {
143             Lookups.fixed(new Object JavaDoc[] {
144                 // Need the data object registered in the lookup so that the
145
// projectui code will close our open editor windows when the
146
// project is closed.
147
wsdlDataObject,
148                 // The Show Cookie in lookup to show the component
149
showCookie,
150                 // Provides the PrintProvider for printing
151
new DesignViewPrintProvider(),
152                 // Component palette for the partner view.
153
WSDLPaletteFactory.getPalette(),
154                 // This is unusal, not sure why this is here.
155
this,
156             }),
157             // For the explorer actions to be enabled.
158
ExplorerUtils.createLookup(explorerManager, map),
159             nodesMediator.getLookup(),
160             // The Node delegate Lookup must be the last one in the list
161
// for the CookieProxyLookup to work properly.
162
delegate.getLookup(),
163         }, delegate);
164         associateLookup(cpl);
165         addPropertyChangeListener(ACTIVATED_NODES, nodesMediator);
166         addPropertyChangeListener(ACTIVATED_NODES, cpl);
167         setLayout(new BorderLayout JavaDoc());
168         initUI();
169     }
170
171     public ExplorerManager getExplorerManager() {
172     return explorerManager;
173     }
174
175     @Override JavaDoc
176     public int getPersistenceType() {
177         // This is likely ignored, the one in MultiViewDesc matters.
178
return PERSISTENCE_ALWAYS;
179     }
180     
181     public void setMultiViewCallback(final MultiViewElementCallback callback) {
182         multiViewObserver = callback;
183     }
184
185     public CloseOperationState canCloseElement() {
186         // if this is not the last cloned xml editor component, closing is OK
187
if (!WSDLEditorSupport.isLastView(multiViewObserver.getTopComponent())) {
188             return CloseOperationState.STATE_OK;
189         }
190         // return a placeholder state - to be sure our CloseHandler is called
191
return MultiViewFactory.createUnsafeCloseState(
192                 "ID_TEXT_CLOSING", // dummy ID // NOI18N
193
MultiViewFactory.NOOP_CLOSE_ACTION,
194                 MultiViewFactory.NOOP_CLOSE_ACTION);
195     }
196
197     @Override JavaDoc
198     public UndoRedo getUndoRedo() {
199     return wsdlDataObject.getWSDLEditorSupport().getUndoManager();
200     }
201
202
203     @Override JavaDoc
204     public void componentHidden() {
205         super.componentHidden();
206     }
207     
208     @Override JavaDoc
209     public void componentClosed() {
210         super.componentClosed();
211     }
212     
213     @Override JavaDoc
214     public void componentOpened() {
215         super.componentOpened();
216     }
217     
218     @Override JavaDoc
219     public void componentActivated() {
220         super.componentActivated();
221     ExplorerUtils.activateActions(explorerManager, true);
222         wsdlDataObject.getWSDLEditorSupport().syncModel();
223     }
224     
225     @Override JavaDoc
226     public void componentDeactivated() {
227     ExplorerUtils.activateActions(explorerManager, false);
228         super.componentDeactivated();
229     }
230     
231     @Override JavaDoc
232     public void componentShowing() {
233         super.componentShowing();
234         initUI();
235     }
236     
237     @Override JavaDoc
238     protected String JavaDoc preferredID() {
239         return "WSDLDesignMultiViewElementTC"; // NOI18N
240
}
241
242     /**
243      * Construct the user interface.
244      */

245     private void initUI() {
246         WSDLEditorSupport editor = wsdlDataObject.getWSDLEditorSupport();
247         WSDLModel wsdlModel = null;
248         String JavaDoc errorMessage = null;
249         try {
250             wsdlModel = editor.getModel();
251             if (wsdlModel != null &&
252                     wsdlModel.getState() == WSDLModel.State.VALID) {
253                 // Construct the standard editor interface.
254
if (graphComponent == null) {
255                     graphComponent = new GraphView(wsdlModel);
256                 }
257                 removeAll();
258                 add(graphComponent, BorderLayout.CENTER);
259                 return;
260             }
261         } catch (IOException JavaDoc ex) {
262             errorMessage = ex.getMessage();
263         }
264
265         // If it comes here, either the model is not well-formed or invalid.
266
if (wsdlModel == null ||
267                 wsdlModel.getState() == WSDLModel.State.NOT_WELL_FORMED) {
268             if (errorMessage == null) {
269                 errorMessage = NbBundle.getMessage(
270                         WSDLTreeViewMultiViewElement.class,
271                         "MSG_NotWellformedWsdl");
272             }
273         }
274
275         // Clear the interface and show the error message.
276
removeAll();
277         errorLabel.setText("<" + errorMessage + ">");
278         errorLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
279         errorLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
280         errorLabel.setEnabled(false);
281         Color JavaDoc usualWindowBkg = UIManager.getColor("window"); //NOI18N
282
errorLabel.setBackground(usualWindowBkg != null ? usualWindowBkg :
283             Color.white);
284         errorLabel.setOpaque(true);
285         add(errorLabel, BorderLayout.CENTER);
286     }
287
288     public javax.swing.JComponent JavaDoc getToolbarRepresentation() {
289         if (mToolbar == null) {
290             try {
291                 WSDLModel model = wsdlDataObject.getWSDLEditorSupport().getModel();
292                 if (model != null && model.getState() == WSDLModel.State.VALID) {
293                     mToolbar = new JToolBar JavaDoc();
294                     mToolbar.setFloatable(false);
295                     mToolbar.addSeparator();
296                     graphComponent.addToolbarActions(mToolbar);
297                     mToolbar.addSeparator();
298                     mToolbar.add(new ValidateAction(model));
299                 }
300             } catch (IOException JavaDoc e) {
301                 //wait until the model is loaded
302
}
303         }
304         return mToolbar;
305     }
306
307     public javax.swing.JComponent JavaDoc getVisualRepresentation() {
308         return this;
309     }
310
311     @SuppressWarnings JavaDoc("deprecation")
312     public void requestFocus() {
313         super.requestFocus();
314         // Ensure the graph widgets have the focus.
315
// Also helps to make F1 open the correct help topic.
316
if (graphComponent != null) {
317             graphComponent.requestFocus();
318         }
319     }
320
321     @SuppressWarnings JavaDoc("deprecation")
322     public boolean requestFocusInWindow() {
323         boolean retVal = super.requestFocusInWindow();
324         // Ensure the graph widgets have the focus.
325
// Also helps to make F1 open the correct help topic.
326
if (graphComponent != null) {
327             return graphComponent.requestFocusInWindow();
328         }
329         return retVal;
330     }
331
332     public HelpCtx getHelpCtx() {
333     return new HelpCtx(WSDLDesignMultiViewDesc.class);
334     }
335
336     public void readExternal(ObjectInput JavaDoc in) throws
337             IOException JavaDoc, ClassNotFoundException JavaDoc {
338         super.readExternal(in);
339         Object JavaDoc obj = in.readObject();
340         if (obj instanceof WSDLDataObject) {
341             wsdlDataObject = (WSDLDataObject) obj;
342             initialize();
343         }
344         try {
345             // By default the widgets are visible, so only need to make
346
// them not visible, if the settings dictate.
347
if (!in.readBoolean()) {
348                 graphComponent.setCollaborationsVisible(false);
349             }
350             if (!in.readBoolean()) {
351                 graphComponent.setMessagesVisible(false);
352             }
353         } catch (IOException JavaDoc ioe) {
354             // Visibility settings must not have been saved.
355
}
356     }
357
358     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
359         super.writeExternal(out);
360         out.writeObject(wsdlDataObject);
361         // Persist the visibility of the widgets.
362
out.writeBoolean(graphComponent.isCollaborationsShowing());
363         out.writeBoolean(graphComponent.isMessagesShowing());
364     }
365
366     /**
367      * Provides the PrintProvider which allows us to print the design view
368      * to a printer using the Print API.
369      */

370     private class DesignViewPrintProvider implements PrintProviderCookie {
371
372         public PrintProvider getPrintProvider() {
373             return new PrintProvider.Component() {
374                 public String JavaDoc getName() {
375                     return wsdlDataObject.getName();
376                 }
377
378                 public Date JavaDoc getLastModifiedDate() {
379                     return wsdlDataObject.getPrimaryFile().lastModified();
380                 }
381
382                 public JComponent JavaDoc getComponent() {
383                     return graphComponent.getContent();
384                 }
385             };
386         }
387     }
388 }
389
Popular Tags