KickJava   Java API By Example, From Geeks To Geeks.

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


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.BorderLayout JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.FlowLayout JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import javax.swing.ActionMap JavaDoc;
30 import javax.swing.JPanel JavaDoc;
31 import javax.swing.JSeparator JavaDoc;
32 import javax.swing.SwingConstants JavaDoc;
33 import javax.swing.UIManager JavaDoc;
34 import javax.swing.text.DefaultEditorKit JavaDoc;
35 import org.netbeans.core.spi.multiview.CloseOperationState;
36 import org.netbeans.core.spi.multiview.MultiViewElement;
37 import org.netbeans.core.spi.multiview.MultiViewElementCallback;
38 import org.netbeans.core.spi.multiview.MultiViewFactory;
39 import org.netbeans.modules.xml.axi.AXIModel;
40 import org.netbeans.modules.xml.axi.AXIModelFactory;
41 import org.netbeans.modules.xml.schema.abe.InstanceDesignerPanel;
42 import org.netbeans.modules.xml.schema.abe.UIUtilities;
43 import org.netbeans.modules.xml.schema.core.SchemaDataObject;
44 import org.netbeans.modules.xml.schema.core.SchemaEditorSupport;
45 import org.netbeans.modules.xml.schema.model.SchemaModel;
46 import org.netbeans.modules.xml.validation.ShowCookie;
47 import org.netbeans.modules.xml.xam.Component;
48 import org.netbeans.modules.xml.xam.Model.State;
49 import org.netbeans.modules.xml.xam.spi.Validator.ResultItem;
50 import org.netbeans.modules.xml.xam.ui.multiview.ActivatedNodesMediator;
51 import org.netbeans.modules.xml.xam.ui.multiview.CookieProxyLookup;
52 import org.netbeans.modules.xml.xam.ui.undo.QuietUndoManager;
53 import org.openide.util.Lookup;
54 import org.openide.windows.TopComponent;
55 import org.openide.awt.UndoRedo;
56 import org.openide.explorer.ExplorerManager;
57 import org.openide.explorer.ExplorerUtils;
58 import org.openide.nodes.Node;
59 import org.openide.util.HelpCtx;
60 import org.openide.util.NbBundle;
61 import org.openide.util.WeakListeners;
62 import org.openide.util.lookup.Lookups;
63
64 /**
65  *
66  * @author Jeri Lockhart
67  */

68 public class SchemaABEViewMultiViewElement extends TopComponent
69         implements MultiViewElement, PropertyChangeListener JavaDoc,
70         ExplorerManager.Provider {
71     
72     private static final long serialVersionUID = -483941387931729295L;
73     private AXIModel axiModel;
74     private String JavaDoc errorMessage;
75     private SchemaDataObject schemaDataObject;
76     private InstanceDesignerPanel abeDesigner;
77     private transient JPanel JavaDoc toolBarPanel;
78     private javax.swing.JLabel JavaDoc errorLabel = new javax.swing.JLabel JavaDoc();
79     private transient MultiViewElementCallback multiViewCallback;
80     private ExplorerManager manager;
81     
82     public SchemaABEViewMultiViewElement() {
83         super();
84         // For deserialization only
85
}
86     
87     public SchemaABEViewMultiViewElement(SchemaDataObject schemaDataObject) {
88         super();
89         this.schemaDataObject = schemaDataObject;
90         initialize();
91     }
92     
93     
94     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
95         String JavaDoc property = evt.getPropertyName();
96         
97         if(AXIModel.STATE_PROPERTY.equals(property)) {
98             onModelStateChanged(evt);
99             return;
100         }
101     }
102     
103     private void onModelStateChanged(PropertyChangeEvent JavaDoc evt) {
104         State newState = (State)evt.getNewValue();
105         Object JavaDoc source = evt.getSource();
106         if(newState == AXIModel.State.VALID) {
107             errorMessage = null;
108             recreateUI();
109             return;
110         }
111         
112         if(errorMessage == null)
113             errorMessage = NbBundle.getMessage(
114                     SchemaColumnViewMultiViewElement.class,
115                     "MSG_InvalidSchema");
116         setActivatedNodes(new Node[] {schemaDataObject.getNodeDelegate()});
117         emptyUI(errorMessage);
118     }
119     
120     public ExplorerManager getExplorerManager() {
121         return manager;
122     }
123     
124     private void initialize() {
125         // Place the palette controller and some other things into the lookup
126
Node delegate = schemaDataObject.getNodeDelegate();
127         
128         //show cookie
129
ShowCookie showCookie = new ShowCookie() {
130             
131             public void show(ResultItem resultItem) {
132                 Component component = resultItem.getComponents();
133                 if(component!=null) {
134                     abeDesigner.selectUIComponent(component);
135                 }
136             }
137         };
138         
139         ActivatedNodesMediator nodesMediator =
140                 new ActivatedNodesMediator(delegate);
141         manager = new ExplorerManager();
142         nodesMediator.setExplorerManager(this);
143         ActionMap JavaDoc map = getActionMap();
144         map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager));
145         map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(manager));
146         map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(manager));
147         map.put("delete", ExplorerUtils.actionDelete(manager, false));
148         
149         CookieProxyLookup cpl = new CookieProxyLookup(new Lookup[] {
150             nodesMediator.getLookup(),
151             // use a proxy lookup here as the abeDesigner is only
152
// active
153
Lookups.proxy(new Lookup.Provider() {
154                 public Lookup getLookup() {
155                     Lookup lookup = Lookup.EMPTY;
156                     if (abeDesigner != null) {
157                         lookup =
158                                 Lookups.singleton(abeDesigner.getPaletteController());
159                     }
160                     return lookup;
161                 }
162             }),
163             Lookups.fixed(new Object JavaDoc[] {
164                 // Need the action map in our custom lookup so actions work.
165
getActionMap(),
166                 // Need the data object registered in the lookup so that the
167
// projectui code will close our open editor windows when the
168
// project is closed.
169
schemaDataObject,
170                 // The Show Cookie in lookup to show schema component
171
showCookie,
172             }),
173             // The Node delegate Lookup must be the last one in the list
174
// for the CookieProxyLookup to work properly.
175
delegate.getLookup(),
176         }, delegate);
177         associateLookup(cpl);
178         addPropertyChangeListener("activatedNodes", nodesMediator);
179         addPropertyChangeListener("activatedNodes", cpl);
180         //initUI();
181
}
182     
183     
184     /**
185      * Initializes the UI. Here it checks for the state of the underlying
186      * schema model. If valid, draws the UI, else empties the UI with proper
187      * error message.
188      */

189     boolean firsTime = true;
190     private void initUI() {
191         if(firsTime){
192             setLayout(new BorderLayout JavaDoc());
193             //initialize the error label one time.
194
errorLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
195             errorLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
196             Color JavaDoc usualWindowBkg = UIManager.getColor("window"); //NOI18N
197
errorLabel.setBackground(usualWindowBkg != null ? usualWindowBkg :
198                 Color.white);
199             errorLabel.setOpaque(true);
200             firsTime = false;
201         }
202         
203         AXIModel model = getAXIModel();
204         if( (model != null) &&
205                 (model.getState() == AXIModel.State.VALID) ) {
206             recreateUI();
207             return;
208         }
209         
210         //if it comes here, either the schema is not well-formed or invalid
211
if(errorMessage == null)
212             errorMessage = NbBundle.getMessage(
213                     SchemaColumnViewMultiViewElement.class,
214                     "MSG_InvalidSchema");
215         emptyUI(errorMessage);
216     }
217     
218     /**
219      * Creates the UI. Creates the abeDesigner only if it was null
220      * or the underlying documnet was found modifed externally.
221      */

222     boolean propChangeListenerAdded = false;
223     private void recreateUI() {
224         if(abeDesigner == null){
225             abeDesigner = new InstanceDesignerPanel(getAXIModel(), schemaDataObject, this);
226         }
227         // Add the ABE designer panel
228
if(!isChild(abeDesigner)){
229             add(abeDesigner,BorderLayout.CENTER);
230         }
231         if(!propChangeListenerAdded){
232             abeDesigner.addPropertyChangeListener(this);
233             propChangeListenerAdded = true;
234         }
235         if(errorLabel != null)
236             errorLabel.setVisible(false);
237         abeDesigner.setVisible(true);
238         revalidate();
239         repaint();
240     }
241     
242     /**
243      * Empties the UI, with proper error message, when the underlying
244      * schema model is in INVALID/NOT-WELLFORMED state.
245      */

246     
247     private void emptyUI(String JavaDoc errorMessage) {
248         if(abeDesigner != null)
249             abeDesigner.setVisible(false);
250         errorLabel.setText("<" + errorMessage + ">");
251         if(!isChild(errorLabel))
252             add(errorLabel, BorderLayout.NORTH);
253         if(propChangeListenerAdded){
254             abeDesigner.addPropertyChangeListener(this);
255             propChangeListenerAdded = false;
256         }
257         errorLabel.setVisible(true);
258         revalidate();
259         repaint();
260     }
261     
262     private boolean isChild(java.awt.Component JavaDoc comp){
263         java.awt.Component JavaDoc compArry[] = getComponents();
264         if( (compArry == null) || (compArry.length <= 0) ){
265             return false;
266         }
267         return Arrays.asList(compArry).contains(comp);
268     }
269     
270     /////////////////////////////////////////////////////////////////////////////
271
// MultiViewElement implementation
272
/////////////////////////////////////////////////////////////////////////////
273

274     /**
275      *
276      *
277      */

278     public int getPersistenceType() {
279         return PERSISTENCE_NEVER;
280     }
281     
282     public void setMultiViewCallback(MultiViewElementCallback callback) {
283         multiViewCallback = callback;
284     }
285     
286     
287     public HelpCtx getHelpCtx() {
288         return new HelpCtx(SchemaABEViewMultiViewDesc.class);
289     }
290     
291     public CloseOperationState canCloseElement() {
292         // if this is not the last cloned xml editor component, closing is OK
293
if (!SchemaMultiViewSupport.isLastView(multiViewCallback.getTopComponent())) {
294             return CloseOperationState.STATE_OK;
295         }
296         // return a placeholder state - to be sure our CloseHandler is called
297
return MultiViewFactory.createUnsafeCloseState(
298                 "ID_SCHEMA_ABEVIEW_CLOSING", // dummy ID // NOI18N
299
MultiViewFactory.NOOP_CLOSE_ACTION,
300                 MultiViewFactory.NOOP_CLOSE_ACTION);
301     }
302     
303     /**
304      *
305      *
306      */

307     public void componentActivated() {
308         super.componentActivated();
309         ExplorerUtils.activateActions(manager, true);
310         addUndoManager();
311         UIUtilities.hideGlassMessage(true);
312     }
313     
314     
315     /**
316      *
317      *
318      */

319     public void componentClosed() {
320         super.componentClosed();
321         UIUtilities.hideGlassMessage(true);
322         abeDesigner.shutdown();
323         abeDesigner = null;
324         removeUndoManager();
325         this.removeAll();
326         this.setLayout(null);
327         //associateLookup(Lookup.EMPTY);
328
}
329     
330     
331     /**
332      *
333      *
334      */

335     public void componentDeactivated() {
336         super.componentDeactivated();
337         ExplorerUtils.activateActions(manager, false);
338         UIUtilities.hideGlassMessage(true);
339     }
340     
341     
342     /**
343      *
344      *
345      */

346     public void componentHidden() {
347         super.componentHidden();
348         UIUtilities.hideGlassMessage(true);
349     }
350     
351     
352     /**
353      *
354      *
355      */

356     public void componentOpened() {
357         super.componentOpened();
358         UIUtilities.hideGlassMessage(true);
359     }
360     
361     
362     /**
363      *
364      *
365      */

366     public void componentShowing() {
367         super.componentShowing();
368         initUI();
369         addUndoManager();
370         UIUtilities.hideGlassMessage(true);
371     }
372
373     @SuppressWarnings JavaDoc("deprecation")
374     public void requestFocus() {
375         super.requestFocus();
376         // For Help to work properly, need to take focus.
377
if (abeDesigner != null) {
378             abeDesigner.requestFocus();
379         }
380     }
381
382     @SuppressWarnings JavaDoc("deprecation")
383     public boolean requestFocusInWindow() {
384         boolean retVal = super.requestFocusInWindow();
385         // For Help to work properly, need to take focus.
386
if (abeDesigner != null) {
387             return abeDesigner.requestFocusInWindow();
388         }
389         return retVal;
390     }
391
392     /**
393      *
394      *
395      */

396     public javax.swing.JComponent JavaDoc getToolbarRepresentation() {
397         if (toolBarPanel == null) {
398             toolBarPanel = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.LEFT));
399             
400             //dummy panel for spacing
401
//toolBarPanel.add(new JPanel(), BorderLayout.);
402

403             JSeparator JavaDoc jsep = new JSeparator JavaDoc(SwingConstants.VERTICAL);
404             toolBarPanel.add(jsep);
405             
406         }
407         return toolBarPanel;
408     }
409     
410     
411     /**
412      * Adds the undo/redo manager to the schema model as an undoable
413      * edit listener, so it receives the edits onto the queue.
414      */

415     private void addUndoManager() {
416         SchemaModel model = getAXIModel().getSchemaModel();
417         if (model != null) {
418             SchemaEditorSupport editor = schemaDataObject.getSchemaEditorSupport();
419             QuietUndoManager undo = editor.getUndoManager();
420             // Ensure the listener is not added twice.
421
model.removeUndoableEditListener(undo);
422             model.addUndoableEditListener(undo);
423             // Ensure the model is sync'd when undo/redo is invoked,
424
// otherwise the edits are added to the queue and eventually
425
// cause exceptions.
426
undo.setModel(model);
427             undo.addWrapperModel(getAXIModel());
428         }
429     }
430     
431     private void removeUndoManager() {
432         SchemaModel model = getAXIModel().getSchemaModel();
433         if (model != null) {
434             SchemaEditorSupport editor = schemaDataObject.getSchemaEditorSupport();
435             QuietUndoManager undo = editor.getUndoManager();
436             model.removeUndoableEditListener(undo);
437             undo.removeWrapperModel(model);
438         }
439     }
440     
441     /**
442      *
443      *
444      */

445     public UndoRedo getUndoRedo() {
446         return schemaDataObject.getSchemaEditorSupport().getUndoManager();
447     }
448     
449     private AXIModel getAXIModel() {
450         if (axiModel != null) {
451             return axiModel;
452         }
453         try {
454             SchemaModel sModel = schemaDataObject.
455                     getSchemaEditorSupport().getModel();
456             axiModel = AXIModelFactory.getDefault().getModel(sModel);
457             if (axiModel != null) {
458                 PropertyChangeListener JavaDoc pcl = (PropertyChangeListener JavaDoc)WeakListeners.
459                         create(PropertyChangeListener JavaDoc.class, this, axiModel);
460                 axiModel.addPropertyChangeListener(pcl);
461             }
462         } catch (IOException JavaDoc e) {
463             errorMessage = e.getMessage();
464         }
465         return axiModel;
466     }
467     
468     /**
469      *
470      *
471      */

472     public javax.swing.JComponent JavaDoc getVisualRepresentation() {
473         return this;
474     }
475     
476 }
477
Popular Tags