KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
23 import java.io.ObjectInput JavaDoc;
24 import java.io.ObjectOutput JavaDoc;
25
26 import javax.swing.JComponent JavaDoc;
27 import javax.swing.JEditorPane JavaDoc;
28 import javax.swing.text.Document JavaDoc;
29 import javax.swing.text.StyledDocument JavaDoc;
30
31 import org.netbeans.core.spi.multiview.CloseOperationState;
32 import org.netbeans.core.spi.multiview.MultiViewElement;
33 import org.netbeans.core.spi.multiview.MultiViewElementCallback;
34 import org.netbeans.core.spi.multiview.MultiViewFactory;
35 import org.netbeans.modules.xml.validation.ShowCookie;
36 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
37 import org.netbeans.modules.xml.xam.Component;
38 import org.netbeans.modules.xml.xam.dom.DocumentComponent;
39 import org.netbeans.modules.xml.xam.spi.Validator.ResultItem;
40 import org.openide.awt.UndoRedo;
41 import org.openide.nodes.Node;
42 import org.openide.text.CloneableEditor;
43 import org.openide.text.NbDocument;
44 import org.openide.util.Lookup;
45 import org.openide.util.lookup.Lookups;
46 import org.openide.util.lookup.ProxyLookup;
47
48 /**
49  *
50  * @author Jeri Lockhart
51  */

52
53 public class WSDLSourceMultiViewElement extends CloneableEditor implements MultiViewElement {
54     
55     static final long serialVersionUID = 4403502726950453345L;
56     
57     transient private JComponent JavaDoc toolbar;
58     transient private MultiViewElementCallback multiViewObserver;
59     private WSDLDataObject wsdlDataObject;
60     
61     
62     // Do NOT remove. Only for externalization //
63
public WSDLSourceMultiViewElement() {
64         super();
65     }
66     
67     // Creates new editor //
68
public WSDLSourceMultiViewElement(WSDLDataObject wsdlDataObject) {
69         super(wsdlDataObject.getWSDLEditorSupport());
70         this.wsdlDataObject = wsdlDataObject;
71         
72         // XXX: Please explain why this is being done.
73
setActivatedNodes(new Node[] {wsdlDataObject.getNodeDelegate()});
74
75         // Initialize the editor support properly, which only needs to be
76
// done when the editor is created (deserialization is working
77
// due to CloneableEditor.readResolve() initializing the editor).
78
// Note that this relies on the source view being the first in the
79
// array of MultiViewDescription instances in WSDLMultiViewFactory,
80
// since that results in the source view being created and opened
81
// by default, only to be hidden when the DataObject default action
82
// makes the tree view appear.
83
// This initialization fixes CR 6380287 by ensuring that the Node
84
// listener is registered with the DataObject Node delegate.
85
wsdlDataObject.getWSDLEditorSupport().initializeCloneableEditor(this);
86         initialize();
87     }
88     
89     public JComponent JavaDoc getToolbarRepresentation() {
90         Document JavaDoc doc = getEditorPane().getDocument();
91         if (doc instanceof NbDocument.CustomToolbar) {
92             if (toolbar == null) {
93                 toolbar = ((NbDocument.CustomToolbar) doc).createToolbar(getEditorPane());
94             }
95             return toolbar;
96         }
97         return null;
98     }
99     
100     public JComponent JavaDoc getVisualRepresentation() {
101         return this;
102     }
103     
104     public void setMultiViewCallback(MultiViewElementCallback callback) {
105         multiViewObserver = callback;
106     }
107     
108     @Override JavaDoc
109     public void requestVisible() {
110         if (multiViewObserver != null)
111             multiViewObserver.requestVisible();
112         else
113             super.requestVisible();
114     }
115     
116     @Override JavaDoc
117     public void requestActive() {
118         if (multiViewObserver != null)
119             multiViewObserver.requestActive();
120         else
121             super.requestActive();
122     }
123     
124     @Override JavaDoc
125     protected String JavaDoc preferredID() {
126         
127         return "WSDLSourceMultiViewElementTC"; // NOI18N
128
}
129     
130     
131     @Override JavaDoc
132     public UndoRedo getUndoRedo() {
133     return wsdlDataObject.getWSDLEditorSupport().getUndoManager();
134     }
135
136     /**
137      * The close last method should be called only for the last clone.
138      * If there are still existing clones this method must return false. The
139      * implementation from the FormEditor always returns true but this is
140      * not the expected behavior. The intention is to close the editor support
141      * once the last editor has been closed, using the silent close to avoid
142      * displaying a new dialog which is already being displayed via the
143      * close handler.
144      */

145     @Override JavaDoc
146     protected boolean closeLast() {
147     WSDLEditorSupport support = wsdlDataObject.getWSDLEditorSupport();
148     JEditorPane JavaDoc[] editors = support.getOpenedPanes();
149     if (editors == null || editors.length == 0) {
150         return support.silentClose();
151     }
152     return false;
153     }
154
155     public CloseOperationState canCloseElement() {
156         // if this is not the last cloned xml editor component, closing is OK
157
if (!WSDLEditorSupport.isLastView(multiViewObserver.getTopComponent())) {
158             return CloseOperationState.STATE_OK;
159         }
160         // return a placeholder state - to be sure our CloseHandler is called
161
return MultiViewFactory.createUnsafeCloseState(
162                 "ID_TEXT_CLOSING", // dummy ID // NOI18N
163
MultiViewFactory.NOOP_CLOSE_ACTION,
164                 MultiViewFactory.NOOP_CLOSE_ACTION);
165     }
166     
167     @Override JavaDoc
168     public void componentDeactivated() {
169         super.componentDeactivated();
170         WSDLEditorSupport editor = wsdlDataObject.getWSDLEditorSupport();
171         // Sync model before having undo manager listen to the model,
172
// lest we get redundant undoable edits added to the queue.
173
editor.syncModel();
174         editor.removeUndoManagerFromDocument();
175     }
176     
177     @Override JavaDoc
178     public void componentActivated() {
179         super.componentActivated();
180         WSDLEditorSupport editor = wsdlDataObject.getWSDLEditorSupport();
181         editor.addUndoManagerToDocument();
182     }
183     
184     @Override JavaDoc
185     public void componentClosed() {
186         super.componentClosed();
187     }
188     
189     @Override JavaDoc
190     public void componentShowing() {
191         super.componentShowing();
192         WSDLEditorSupport editor = wsdlDataObject.getWSDLEditorSupport();
193         editor.addUndoManagerToDocument();
194     }
195     
196     @Override JavaDoc
197     public void componentHidden() {
198         super.componentHidden();
199         WSDLEditorSupport editor = wsdlDataObject.getWSDLEditorSupport();
200         // Sync model before having undo manager listen to the model,
201
// lest we get redundant undoable edits added to the queue.
202
editor.syncModel();
203         editor.removeUndoManagerFromDocument();
204     }
205     
206     @Override JavaDoc
207     public void componentOpened() {
208         super.componentOpened();
209     }
210     
211     @Override JavaDoc
212     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
213         super.writeExternal(out);
214         out.writeObject(wsdlDataObject);
215     }
216     
217     @Override JavaDoc
218     public void readExternal(ObjectInput JavaDoc in)
219     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
220         super.readExternal(in);
221         Object JavaDoc firstObject = in.readObject();
222         if (firstObject instanceof WSDLDataObject) {
223             wsdlDataObject = (WSDLDataObject) firstObject;
224             initialize();
225         }
226     }
227
228     private void initialize()
229     {
230         ShowCookie showCookie = new ShowCookie()
231         {
232             
233             public void show(ResultItem resultItem) {
234                 if(isActiveTC()) {
235                     Component JavaDoc component = resultItem.getComponents();
236                     if (component.getModel() == null) return; //may have been deleted.
237

238                     UIUtilities.annotateSourceView(wsdlDataObject, (DocumentComponent) component,
239                             resultItem.getDescription(), true);
240                     if(component instanceof WSDLComponent) {
241                         int position = ((WSDLComponent)component).findPosition();
242                         getEditorPane().setCaretPosition(position);
243                     } else {
244                         int line = resultItem.getLineNumber();
245                         try {
246                             int position = NbDocument.findLineOffset(
247                                     (StyledDocument JavaDoc)getEditorPane().getDocument(),line);
248                             getEditorPane().setCaretPosition(position);
249                         } catch (IndexOutOfBoundsException JavaDoc iob) {
250                             // nothing
251
}
252                     }
253                 }
254             }
255         };
256
257         // create and associate lookup
258
ProxyLookup lookup = new ProxyLookup(new Lookup[] {
259             Lookups.fixed(new Object JavaDoc[] {
260                 // Need ActionMap in lookup so editor actions work.
261
getActionMap(),
262                 // Need the data object registered in the lookup so that the
263
// projectui code will close our open editor windows when the
264
// project is closed.
265
wsdlDataObject,
266                 // The Show Cookie in lookup to show schema component
267
showCookie,
268             }),
269             wsdlDataObject.getNodeDelegate().getLookup(),
270         });
271         associateLookup(lookup);
272     }
273     
274     protected boolean isActiveTC()
275     {
276         return getRegistry().getActivated() == multiViewObserver.getTopComponent();
277     }
278 }
279
Popular Tags