KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > core > multiview > SchemaMultiViewSupport


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.schema.core.multiview;
21
22 import java.awt.EventQueue JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import org.netbeans.core.api.multiview.MultiViewHandler;
28 import org.netbeans.core.api.multiview.MultiViewPerspective;
29 import org.netbeans.core.api.multiview.MultiViews;
30 import org.netbeans.core.spi.multiview.MultiViewDescription;
31 import org.netbeans.core.spi.multiview.MultiViewFactory;
32 import org.netbeans.modules.xml.axi.AXIComponent;
33 import org.netbeans.modules.xml.axi.AXIModel;
34 import org.netbeans.modules.xml.axi.AXIModelFactory;
35 import org.netbeans.modules.xml.schema.core.SchemaDataObject;
36 import org.netbeans.modules.xml.schema.core.SchemaEditorSupport;
37 import org.netbeans.modules.xml.schema.model.SchemaComponent;
38 import org.netbeans.modules.xml.xam.Model;
39 import org.netbeans.modules.xml.xam.ui.cookies.GetComponentCookie;
40 import org.netbeans.modules.xml.xam.ui.cookies.ViewComponentCookie;
41 import org.netbeans.modules.xml.xam.ui.cookies.ViewComponentCookie.View;
42 import org.netbeans.modules.xml.validation.ShowCookie;
43 import org.netbeans.modules.xml.xam.Component;
44 import org.netbeans.modules.xml.xam.spi.Validator.ResultItem;
45 import org.openide.nodes.Node;
46 import org.openide.windows.CloneableTopComponent;
47 import org.openide.windows.TopComponent;
48
49 /**
50  * Class for creating the Schema Multiview and implementation
51  * of ViewComponentCookie, which has methods to open the multiview elements
52  * of the Schema Multiview. The instance of this class is in the
53  * SchemaDataObject cookie set.
54  *
55  *
56  *
57  * @author Jeri Lockhart
58  * @author Ajit Bhate
59  */

60 public class SchemaMultiViewSupport implements ViewComponentCookie, ShowCookie {
61     public static final String JavaDoc SCHEMA_EDITOR_HELP_ID = "SCHEMA_EDITOR_HELP_ID";
62     /** The dataobject it supports. */
63     private SchemaDataObject dobj;
64     
65     /**
66      * Constructor
67      */

68     public SchemaMultiViewSupport(SchemaDataObject dobj) {
69         this.dobj = dobj;
70     }
71     
72     /**
73      * Create the Schema Multiview
74      */

75     public static CloneableTopComponent createMultiView(
76             SchemaDataObject schemaDataObject) {
77         MultiViewDescription views[] = new MultiViewDescription[3];
78         
79         // Put the source element first so that client code can find its
80
// CloneableEditorSupport.Pane implementation.
81
views[0] = getSchemaSourceMultiviewDesc(schemaDataObject);
82         views[1] = getSchemaColumnViewMultiViewDesc(schemaDataObject);
83         views[2] = getSchemaABEMultiviewDesc(schemaDataObject);
84         
85         
86         // Make the column view the default element.
87
CloneableTopComponent multiview =
88                 MultiViewFactory.createCloneableMultiView(
89                 views,
90                 views[0],
91                 new SchemaEditorSupport.CloseHandler(schemaDataObject));
92         
93         // This handles the "show file extensions" option automatically.
94
String JavaDoc name = schemaDataObject.getNodeDelegate().getDisplayName();
95         multiview.setDisplayName(name);
96         multiview.setName(name);
97         return multiview;
98     }
99     
100     private static MultiViewDescription getSchemaColumnViewMultiViewDesc(
101             final SchemaDataObject schemaDataObject) {
102         return new SchemaColumnViewMultiViewDesc(schemaDataObject);
103     }
104     
105     
106     private static MultiViewDescription getSchemaSourceMultiviewDesc(
107             final SchemaDataObject schemaDataObject) {
108         return new SchemaSourceMultiViewDesc(schemaDataObject);
109     }
110     
111     private static MultiViewDescription getSchemaABEMultiviewDesc(
112             final SchemaDataObject schemaDataObject) {
113         return new SchemaABEViewMultiViewDesc(schemaDataObject);
114     }
115
116     /**
117      * Returns true if the given TopComponent is the last one in the
118      * set of cloneable windows.
119      *
120      * @param tc TopComponent.
121      * @return false if tc is not cloneable, or there are one or more
122      * clones; true if it is the last clone.
123      */

124     public static boolean isLastView(TopComponent tc) {
125         if (!(tc instanceof CloneableTopComponent)) {
126             return false;
127         }
128         boolean oneOrLess = true;
129         Enumeration JavaDoc en = ((CloneableTopComponent) tc).getReference().getComponents();
130         if (en.hasMoreElements()) {
131             en.nextElement();
132             if (en.hasMoreElements()) {
133                 oneOrLess = false;
134             }
135         }
136         return oneOrLess;
137     }
138
139     /**
140      * Shows the desired multiview element. Must be called after the editor
141      * has been opened (i.e. SchemaEditorSupport.open()) so the TopComponent
142      * will be the active one in the registry.
143      *
144      * @param id identifier of the multiview element.
145      */

146     private static void requestMultiviewActive(String JavaDoc id) {
147         TopComponent activeTC = TopComponent.getRegistry().getActivated();
148         MultiViewHandler handler = MultiViews.findMultiViewHandler(activeTC);
149         if (handler != null) {
150             MultiViewPerspective[] perspectives = handler.getPerspectives();
151             for (MultiViewPerspective perspective : perspectives) {
152                 if (perspective.preferredID().equals(id)) {
153                     handler.requestActive(perspective);
154                 }
155             }
156         }
157     }
158     
159     /**
160      * Finds the preferredID of active multiview element.
161      * If activated tc is not mvtc returns null;
162      *
163      * @return identifier of the active multiview element.
164      */

165     private static String JavaDoc getMultiviewActive() {
166         TopComponent activeTC = TopComponent.getRegistry().getActivated();
167         MultiViewHandler handler = MultiViews.findMultiViewHandler(activeTC);
168         if (handler != null) {
169             return handler.getSelectedPerspective().preferredID();
170         }
171         return null;
172     }
173     
174     /**
175      * Finds the activated components of active top component.
176      *
177      * @return collection of the active components.
178      */

179     private static Collection JavaDoc<Component> getActiveComponents() {
180         TopComponent activeTC = TopComponent.getRegistry().getActivated();
181         Collection JavaDoc<Component> activeComponents = Collections.emptySet();
182         for(Node n:activeTC.getActivatedNodes()) {
183             GetComponentCookie cake = (GetComponentCookie)n.
184                     getCookie(GetComponentCookie.class);
185             Component component = null;
186             if(cake!=null) component = cake.getComponent();
187             if(component==null)
188                 component = (Component) n.getLookup().lookup(Component.class);
189             if(component!=null) {
190                 if(activeComponents.isEmpty()) activeComponents = new HashSet JavaDoc<Component>();
191                 activeComponents.add(component);
192             }
193         }
194         return activeComponents;
195     }
196     
197     public void view(final View view, final Component component,
198             final Object JavaDoc... parameters) {
199         
200         if (!EventQueue.isDispatchThread()) {
201             EventQueue.invokeLater(new Runnable JavaDoc() {
202                 public void run() {
203                     viewInSwingThread(view, component, parameters);
204                 }
205             });
206         } else {
207             viewInSwingThread(view, component, parameters);
208         }
209     }
210     
211     private void viewInSwingThread(View view, Component component,
212             Object JavaDoc... parameters) {
213         if(canView(view, component)) {
214             if(view==View.SUPER) {
215                 // preserve the view
216
TopComponent activeTC = TopComponent.getRegistry().getActivated();
217                 SchemaDataObject sdobj = (SchemaDataObject)activeTC.getLookup().
218                         lookup(SchemaDataObject.class);
219                 if(dobj!=sdobj) // must be multifile
220
{
221                     String JavaDoc activeMVTCId = getMultiviewActive();
222                     view = View.STRUCTURE;
223                     if(SchemaABEViewMultiViewDesc.PREFERRED_ID.equals(activeMVTCId))
224                         view = View.DESIGN;
225                     else if(SchemaSourceMultiViewDesc.PREFERRED_ID.equals(activeMVTCId))
226                         view = View.SOURCE;
227                 }
228             }
229             SchemaEditorSupport editor = dobj.getSchemaEditorSupport();
230             editor.open();
231             // activate view if needed
232
switch (view) {
233                 case STRUCTURE:
234                     if(component instanceof AXIComponent)
235                         component = ((AXIComponent)component).getPeer();
236                     requestMultiviewActive(
237                             SchemaColumnViewMultiViewDesc.PREFERRED_ID);
238                     break;
239                 case DESIGN:
240                     requestMultiviewActive(
241                             SchemaABEViewMultiViewDesc.PREFERRED_ID);
242                     break;
243                 case SOURCE:
244                     if(component instanceof AXIComponent)
245                         component = ((AXIComponent)component).getPeer();
246                     requestMultiviewActive(
247                             SchemaSourceMultiViewDesc.PREFERRED_ID);
248                     break;
249             }
250             // show component in current multiview
251
TopComponent activeTC = TopComponent.getRegistry().getActivated();
252             ShowCookie showCookie = (ShowCookie)activeTC.getLookup().lookup(ShowCookie.class);
253             ResultItem resultItem = null;
254             if (parameters!=null&&parameters.length!=0) {
255                 for(Object JavaDoc o :parameters) {
256                     if(o instanceof ResultItem) {
257                         resultItem = (ResultItem)o;
258                         break;
259                     }
260                 }
261             }
262             if(resultItem == null) resultItem = new ResultItem(null,null,component,null);
263             if(showCookie!=null) showCookie.show(resultItem);
264         }
265     }
266     
267
268     public void show(ResultItem resultItem) {
269         View view = View.SOURCE;
270         Component component = resultItem.getComponents();
271         if(component != null && component.getModel() != null &&
272                 component.getModel().getState() == Model.State.VALID) {
273             // preserve the view
274
TopComponent activeTC = TopComponent.getRegistry().getActivated();
275             SchemaDataObject sdobj = (SchemaDataObject)activeTC.getLookup().
276                     lookup(SchemaDataObject.class);
277             if(dobj==sdobj) { // we can preserve the view
278
String JavaDoc activeMVTCId = getMultiviewActive();
279                 if(SchemaABEViewMultiViewDesc.PREFERRED_ID.equals(activeMVTCId)) {
280                     AXIModel axiModel = null;
281                     if(component instanceof AXIComponent) {
282                         axiModel = ((AXIComponent)component).getModel();
283                     } else if(component instanceof SchemaComponent) {
284                         axiModel = AXIModelFactory.getDefault().getModel(
285                                 ((SchemaComponent)component).getModel());
286                     }
287                     if(axiModel!=null&&axiModel.getState()==AXIModel.State.VALID) {
288                         view = View.DESIGN;
289                     }
290                 }
291                 else if(SchemaColumnViewMultiViewDesc.PREFERRED_ID.equals(activeMVTCId))
292                     view = View.STRUCTURE;
293             }
294         }
295         view(view, component, resultItem);
296     }
297     
298     public boolean canView(View view, Component component) {
299         if(view!=null) {
300             switch(view) {
301                 case SOURCE:
302                     if(!SchemaSourceMultiViewDesc.PREFERRED_ID.equals(
303                             getMultiviewActive()) ||
304                             !getActiveComponents().contains(component)) {
305                         if(component instanceof AXIComponent)
306                             return ((AXIComponent)component).getPeer()!=null;
307                         return true;
308                     } else return false;
309                 case STRUCTURE:
310                     if(!(component instanceof SchemaComponent ||
311                             component instanceof AXIComponent))
312                         return false;
313                     if(component instanceof AXIComponent &&
314                             ((AXIComponent)component).getPeer()==null)
315                         return false;
316                     if(SchemaColumnViewMultiViewDesc.PREFERRED_ID.equals(
317                             getMultiviewActive()) &&
318                             getActiveComponents().contains(component)) {
319                         TopComponent activeTC = TopComponent.getRegistry().getActivated();
320                         SchemaDataObject sdobj = (SchemaDataObject)activeTC.getLookup().
321                                 lookup(SchemaDataObject.class);
322                         return sdobj!=dobj;
323                     } else return true;
324                 case DESIGN:
325                     if(SchemaABEViewMultiViewDesc.PREFERRED_ID.equals(
326                             getMultiviewActive()))
327                         return false;
328                     AXIModel axiModel = null;
329                     if(component instanceof AXIComponent) {
330                         axiModel = ((AXIComponent)component).getModel();
331                     } else if(component instanceof SchemaComponent) {
332                         axiModel = AXIModelFactory.getDefault().getModel(
333                                 ((SchemaComponent)component).getModel());
334                         if(!axiModel.canView((SchemaComponent)component))
335                             return false;
336                     }
337                     if(axiModel!=null&&axiModel.getState()==AXIModel.State.VALID) {
338                         return true;
339                     }
340                     return false;
341                 case SUPER:
342                     return true;
343             }
344         }
345         return false;
346     }
347 }
348
Popular Tags