KickJava   Java API By Example, From Geeks To Geeks.

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


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.EventQueue JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.Serializable JavaDoc;
26 import java.util.Collections JavaDoc;
27 import javax.swing.Action JavaDoc;
28 import org.netbeans.api.project.FileOwnerQuery;
29 import org.netbeans.api.project.Project;
30 import org.openide.text.DataEditorSupport;
31 import org.openide.loaders.DataObject;
32 import org.openide.windows.Mode;
33 import org.openide.windows.WindowManager;
34 import org.openide.windows.CloneableTopComponent;
35 import org.openide.windows.TopComponent;
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.CloseOperationHandler;
40 import org.netbeans.core.spi.multiview.CloseOperationState;
41 import org.netbeans.core.spi.multiview.MultiViewDescription;
42 import org.netbeans.core.spi.multiview.MultiViewFactory;
43 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel;
44 import org.netbeans.modules.websvc.api.jaxws.project.config.Service;
45 import org.openide.DialogDisplayer;
46 import org.openide.NotifyDescriptor;
47 import org.openide.util.NbBundle;
48 import org.netbeans.modules.websvc.core.MultiViewCookie;
49
50 /**
51  * Class for creating the Multiview
52  * @author Ajit Bhate
53  */

54 public class MultiViewSupport implements MultiViewCookie, Serializable JavaDoc {
55     
56     static final java.awt.Image JavaDoc SERVICE_BADGE = org.openide.util.Utilities.loadImage(
57             "org/netbeans/modules/websvc/core/webservices/ui/resources/XMLServiceDataIcon.gif" ); //NOI18N
58
static final long serialVersionUID = 1L;
59     private DataObject dataObject;
60     private transient Service service;
61     
62     public static String JavaDoc SOURCE_UNSAFE_CLOSE = "SOURCE_UNSAFE_CLOSE";
63     private static String JavaDoc DESIGN_UNSAFE_CLOSE = "DESIGN_UNSAFE_CLOSE";
64
65     /**
66      * MultiView enum
67      */

68     public enum View {
69         /**
70          * Source multiview
71          */

72         SOURCE,
73         /**
74          * Design multiview
75          */

76         DESIGN,
77     }
78
79     /**
80      * Constructor for deserialization
81      */

82     public MultiViewSupport() {
83     }
84
85     /**
86      * Constructor
87      * @param displayName
88      * @param dataObject
89      */

90     MultiViewSupport(Service service, DataObject dataObject) {
91         this.dataObject = dataObject;
92         this.service = service;
93     }
94
95     public void open() {
96         view(View.DESIGN);
97     }
98
99     public void edit() {
100         view(View.SOURCE);
101     }
102     
103     DataObject getDataObject() {
104         return dataObject;
105     }
106
107     Service getService() {
108         return service;
109     }
110
111     public boolean equals(Object JavaDoc obj) {
112         if(obj==this) {
113             return true;
114         }
115         if(obj instanceof MultiViewSupport) {
116             MultiViewSupport sup = (MultiViewSupport)obj;
117             return getDataObject().equals(sup.getDataObject());
118         }
119         return false;
120     }
121
122     /**
123      * Ensures that the Multiview is created and is active.
124       */

125     private TopComponent ensureMultiViewActive() {
126         Mode editorMode = WindowManager.getDefault().findMode(
127                 DataEditorSupport.EDITOR_MODE);
128         if (editorMode != null && editorMode.getSelectedTopComponent() != null) {
129             TopComponent activeTC = editorMode.getSelectedTopComponent();
130             if(equals(activeTC.getLookup().lookup(MultiViewSupport.class))) {
131                 return activeTC;
132             }
133             for(TopComponent openedTC:editorMode.getTopComponents()) {
134                 if(equals(openedTC.getLookup().lookup(MultiViewSupport.class))) {
135                     return openedTC;
136                 }
137             }
138         }
139         CloneableTopComponent tc = createMultiView();
140         tc.requestActive();
141         return tc;
142     }
143
144     /**
145      * Create the Multiview, doc into the editor window and open it.
146      * @return CloneableTopComponent new multiview.
147      */

148     private CloneableTopComponent createMultiView() {
149         MultiViewDescription views[] = new MultiViewDescription[2];
150         
151         // Put the source element first so that client code can find its
152
// CloneableEditorSupport.Pane implementation.
153
views[0] = new SourceMultiViewDesc(this);
154         views[1] = new DesignMultiViewDesc(this);
155         
156         
157         // Make the column view the default element.
158
CloneableTopComponent multiview =
159                 MultiViewFactory.createCloneableMultiView(
160                 views,
161                 views[0]
162                 , new CloseHandler(dataObject)
163                 );
164         
165         String JavaDoc displayName = getServiceDisplayName();
166         multiview.setDisplayName(displayName);
167         multiview.setName(displayName);
168         
169         Mode editorMode = WindowManager.getDefault().findMode(
170                 DataEditorSupport.EDITOR_MODE);
171         if (editorMode != null) {
172             editorMode.dockInto(multiview);
173         }
174         multiview.open();
175         return multiview;
176     }
177     
178     
179     /**
180      *
181      * @param view
182      * @param param
183      */

184     public void view(final View view, final Object JavaDoc... param) {
185         if (!EventQueue.isDispatchThread()) {
186             EventQueue.invokeLater(new Runnable JavaDoc() {
187                 public void run() {
188                     viewInSwingThread(view,dataObject,param);
189                 }
190             });
191         } else {
192             viewInSwingThread(view,dataObject,param);
193         }
194     }
195     
196     private void viewInSwingThread(View view, Object JavaDoc... parameters) {
197         TopComponent activeTC = ensureMultiViewActive();
198         switch(view) {
199         case SOURCE:
200             requestMultiviewActive(activeTC,SourceMultiViewDesc.PREFERRED_ID);
201             break;
202         case DESIGN:
203             requestMultiviewActive(activeTC,DesignMultiViewDesc.PREFERRED_ID);
204             break;
205         }
206     }
207
208     /**
209      * Shows the desired multiview element. Must be called after the editor
210      * has been opened (i.e. SchemaEditorSupport.open()) so the TopComponent
211      * will be the active one in the registry.
212      *
213      * @param id identifier of the multiview element.
214      * @param activeTc MultiView CloneableTopComponent
215      */

216     private static void requestMultiviewActive(TopComponent activeTC, String JavaDoc id) {
217         MultiViewHandler handler = MultiViews.findMultiViewHandler(activeTC);
218         if (handler != null) {
219             MultiViewPerspective[] perspectives = handler.getPerspectives();
220             for (MultiViewPerspective perspective : perspectives) {
221                 if (perspective.preferredID().equals(id)) {
222                     handler.requestActive(perspective);
223                 }
224             }
225         }
226     }
227
228     /**
229      * Returns true if the given TopComponent is the last one in the
230      * set of cloneable windows.
231      *
232      * @param tc TopComponent.
233      * @return -1 if not a cloneabletopcomponent
234      * otherwise number of clones including self
235      */

236     public static int getNumberOfClones(TopComponent tc) {
237         if (!(tc instanceof CloneableTopComponent)) {
238             return -1;
239         }
240         return Collections.list(((CloneableTopComponent)tc).getReference().getComponents()).size();
241     }
242     
243     /**
244      * creates display name for service
245      **/

246     private String JavaDoc getServiceDisplayName() {
247         if (service.getWsdlUrl()!=null)
248             return service.getServiceName()+"["+service.getPortName()+"]";
249         return service.getName();
250     }
251     
252     private void writeObject(java.io.ObjectOutputStream JavaDoc out)
253             throws IOException JavaDoc {
254         out.writeObject(dataObject);
255         out.writeObject(service.getImplementationClass());
256     }
257
258     private void readObject(java.io.ObjectInputStream JavaDoc in)
259             throws IOException JavaDoc, ClassNotFoundException JavaDoc {
260         Object JavaDoc firstObject = in.readObject();
261         if(firstObject instanceof DataObject) {
262             dataObject = (DataObject) firstObject;
263         }
264         Object JavaDoc secondObject = in.readObject();
265         if(secondObject instanceof String JavaDoc) {
266             String JavaDoc implClass = (String JavaDoc)secondObject;
267             Project project = FileOwnerQuery.getOwner(dataObject.getPrimaryFile());
268             JaxWsModel model = (JaxWsModel) project.getLookup().lookup(JaxWsModel.class);
269             service = model.findServiceByImplementationClass(implClass);
270         }
271     }
272
273     /**
274      * Implementation of CloseOperationHandler for multiview. Ensures the
275      * editors correctly closed, data object is saved, etc. Holds a
276      * reference to DataObject only - to be serializable with the multiview
277      * TopComponent without problems.
278      */

279     public static class CloseHandler implements CloseOperationHandler, Serializable JavaDoc {
280         private static final long serialVersionUID = -3838395157610633251L;
281         private DataObject sourceDataObject;
282         
283         private CloseHandler() {
284             super();
285         }
286         
287         public CloseHandler(DataObject sourceDataObject) {
288             this.sourceDataObject = sourceDataObject;
289         }
290         
291         private DataEditorSupport getEditorSupport() {
292             return sourceDataObject == null ? null :
293                 (DataEditorSupport) sourceDataObject.getLookup().lookup(
294                 DataEditorSupport.class);
295         }
296         
297         public boolean resolveCloseOperation(CloseOperationState[] elements) {
298             StringBuffer JavaDoc message = new StringBuffer JavaDoc();
299             for(CloseOperationState state:elements) {
300                 if(state.getCloseWarningID().equals(SOURCE_UNSAFE_CLOSE)) {
301                     message.append(NbBundle.getMessage(DataObject.class,
302                             "MSG_SaveFile", // NOI18N
303
sourceDataObject.getPrimaryFile().getNameExt()));
304                     message.append("\n");
305                 }
306             }
307             NotifyDescriptor desc = new NotifyDescriptor.Confirmation(message.toString().trim());
308             Object JavaDoc retVal = DialogDisplayer.getDefault().notify(desc);
309             for(CloseOperationState state:elements) {
310                 Action JavaDoc act = null;
311                 if (retVal == NotifyDescriptor.YES_OPTION) {
312                     act = state.getProceedAction();
313                 } else if (retVal == NotifyDescriptor.NO_OPTION) {
314                     act = state.getDiscardAction();
315                 } else {
316                     return false;
317                 }
318                 if (act != null) {
319                     act.actionPerformed(new ActionEvent JavaDoc(this, ActionEvent.ACTION_PERFORMED, ""));
320                 }
321             }
322             return true;
323         }
324     }
325 }
326
Popular Tags