KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > design > multiview > SourceMultiViewElement


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.websvc.design.multiview;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.ObjectInput JavaDoc;
25 import java.io.ObjectOutput JavaDoc;
26 import javax.swing.AbstractAction JavaDoc;
27 import javax.swing.JComponent JavaDoc;
28 import javax.swing.text.Document JavaDoc;
29 import org.netbeans.core.spi.multiview.CloseOperationState;
30 import org.netbeans.core.spi.multiview.MultiViewElement;
31 import org.netbeans.core.spi.multiview.MultiViewElementCallback;
32 import org.netbeans.core.spi.multiview.MultiViewFactory;
33 import org.openide.awt.UndoRedo;
34 import org.openide.nodes.Node;
35 import org.openide.text.CloneableEditor;
36 import org.openide.text.DataEditorSupport;
37 import org.openide.text.NbDocument;
38 import org.openide.util.lookup.Lookups;
39
40 /**
41  * The source editor element for JaxWS node.
42  *
43  * @author Ajit Bhate
44  */

45 public class SourceMultiViewElement extends CloneableEditor
46         implements MultiViewElement {
47     private static final long serialVersionUID = 4403502726950453345L;
48     private transient JComponent JavaDoc toolbar;
49     private transient MultiViewElementCallback multiViewCallback;
50     
51     /**
52      * Constructs a new instance of SourceMultiViewElement.
53      */

54     public SourceMultiViewElement() {
55         // Needed for deserialization, do not remove.
56
super(null);
57     }
58     
59     /**
60      * Constructs a new instance of SourceMultiViewElement.
61      *
62      * @param support
63      */

64     public SourceMultiViewElement(MultiViewSupport mvSupport, DataEditorSupport support) {
65         super(support);
66         initialize(mvSupport);
67    }
68     
69     private void initialize(MultiViewSupport mvSupport) {
70         if (mvSupport==null || mvSupport.getDataObject() != getEditorSupport().getDataObject()) {
71             throw new IllegalStateException JavaDoc(
72                     "The multiviewsupport object represents incorrect data object"); //NOI18N
73
}
74         associateLookup(Lookups.fixed(
75                 mvSupport,
76                 getActionMap(),
77                 mvSupport.getDataObject(),
78                 mvSupport.getDataObject().getNodeDelegate()
79                 ));
80     }
81     
82     public JComponent JavaDoc getToolbarRepresentation() {
83         Document JavaDoc doc = getEditorPane().getDocument();
84         if (doc instanceof NbDocument.CustomToolbar) {
85             if (toolbar == null) {
86                 toolbar = ((NbDocument.CustomToolbar) doc).createToolbar(getEditorPane());
87             }
88             return toolbar;
89         }
90         return null;
91     }
92     
93     public JComponent JavaDoc getVisualRepresentation() {
94         return this;
95     }
96     
97     public void setMultiViewCallback(final MultiViewElementCallback callback) {
98         multiViewCallback = callback;
99     }
100     
101     public void requestVisible() {
102         if (multiViewCallback != null)
103             multiViewCallback.requestVisible();
104         else
105             super.requestVisible();
106     }
107     
108     public void requestActive() {
109         if (multiViewCallback != null)
110             multiViewCallback.requestActive();
111         else
112             super.requestActive();
113     }
114     
115     protected String JavaDoc preferredID() {
116         return getClass().getName();
117     }
118     
119     
120     public UndoRedo getUndoRedo() {
121         return super.getUndoRedo();
122     }
123     
124     protected boolean closeLast() {
125         if(MultiViewSupport.getNumberOfClones(multiViewCallback.getTopComponent()) == 0) {
126             // this is the last editor component so call super.closeLast
127
return super.closeLast();
128         }
129         return true;
130     }
131     
132     public CloseOperationState canCloseElement() {
133         // if this is not the last cloned editor component, closing is OK
134
if(!getEditorSupport().isModified() ||
135                 MultiViewSupport.getNumberOfClones(multiViewCallback.getTopComponent()) > 1) {
136             return CloseOperationState.STATE_OK;
137         }
138         // return a state which will save/discard changes and is called by close handler
139
return MultiViewFactory.createUnsafeCloseState(
140                 MultiViewSupport.SOURCE_UNSAFE_CLOSE,
141                 new AbstractAction JavaDoc() {
142                     public void actionPerformed(ActionEvent JavaDoc arg0) {
143                         //save changes
144
try {
145                             getEditorSupport().saveDocument();
146                             getEditorSupport().getDataObject().setModified(false);
147                         } catch (IOException JavaDoc ex) {
148                         }
149                     }
150                 },
151                 new AbstractAction JavaDoc() {
152                     public void actionPerformed(ActionEvent JavaDoc arg0) {
153                         //discard changes
154
}
155                 });
156     }
157     
158     public void componentActivated() {
159         super.componentActivated();
160         setActivatedNodes(new Node[] {getEditorSupport().getDataObject().getNodeDelegate()});
161     }
162     
163     public void componentDeactivated() {
164         super.componentDeactivated();
165         setActivatedNodes(new Node[] {});
166     }
167     
168     public void componentOpened() {
169         super.componentOpened();
170     }
171     
172     public void componentClosed() {
173         super.componentClosed();
174     }
175     
176     public void componentShowing() {
177         super.componentShowing();
178     }
179     
180     public void componentHidden() {
181         super.componentHidden();
182     }
183     
184     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
185         // The superclass persists things such as the caret position.
186
super.writeExternal(out);
187         Object JavaDoc obj = getLookup().lookup(MultiViewSupport.class);
188         if(obj!=null) {
189             out.writeObject(obj);
190         }
191     }
192     
193     public void readExternal(ObjectInput JavaDoc in)
194             throws IOException JavaDoc, ClassNotFoundException JavaDoc {
195         super.readExternal(in);
196     Object JavaDoc firstObject = in.readObject();
197     if (firstObject instanceof MultiViewSupport ) {
198         initialize((MultiViewSupport)firstObject);
199     }
200     }
201     
202     private DataEditorSupport getEditorSupport() {
203         return (DataEditorSupport) cloneableEditorSupport();
204     }
205 }
206
Popular Tags