KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > abe > navigator > NavigatorContent


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.abe.navigator;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Component JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.io.IOException JavaDoc;
28 import javax.swing.JPanel JavaDoc;
29 import javax.swing.SwingConstants JavaDoc;
30 import javax.swing.SwingUtilities JavaDoc;
31 import javax.swing.UIManager JavaDoc;
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.abe.nodes.AXINodeVisitor;
36 import org.netbeans.modules.xml.schema.ui.basic.SchemaModelCookie;
37 import org.netbeans.modules.xml.xam.Model;
38 import org.netbeans.modules.xml.xam.Model.State;
39 import org.openide.explorer.ExplorerManager;
40 import org.openide.explorer.view.BeanTreeView;
41 import org.openide.loaders.DataObject;
42 import org.openide.nodes.Node;
43 import org.openide.util.NbBundle;
44 import org.openide.windows.TopComponent;
45
46 /**
47  * Navigator component containing a tree of abe components along with
48  * element and type filters
49  *
50  * @author Chris Webster
51  */

52 public class NavigatorContent extends JPanel JavaDoc
53     implements ExplorerManager.Provider, PropertyChangeListener JavaDoc {
54     /** silence compiler warnings */
55     private static final long serialVersionUID = 1L;
56     /** Explorer manager for the tree view. */
57     private final ExplorerManager explorerManager;
58     /** Our schema component node tree view. */
59     private final BeanTreeView treeView;
60     private final javax.swing.JLabel JavaDoc notAvailableLabel = new javax.swing.JLabel JavaDoc(
61             NbBundle.getMessage(NavigatorContent.class, "MSG_NotAvailable")); //NOI18N
62

63     /**
64      * Creates a new instance of SchemaNavigatorContent.
65      */

66     public NavigatorContent() {
67     setLayout(new BorderLayout JavaDoc());
68     explorerManager = new ExplorerManager();
69     treeView = new BeanTreeView();
70     treeView.setRootVisible(false);
71     explorerManager.addPropertyChangeListener(this);
72         
73         //initialize the notAvailableLabel
74
notAvailableLabel.setHorizontalAlignment(SwingConstants.CENTER);
75         notAvailableLabel.setEnabled(false);
76         Color JavaDoc usualWindowBkg = UIManager.getColor("window"); //NOI18N
77
notAvailableLabel.setBackground(usualWindowBkg != null ? usualWindowBkg : Color.white);
78         // to ensure our background color will have effect
79
notAvailableLabel.setOpaque(true);
80     }
81     
82     public ExplorerManager getExplorerManager() {
83     return explorerManager;
84     }
85     
86     public void show(DataObject dobj) {
87         AXIModel model = getAXIModel(dobj);
88         if (model == null || model.getState() != Model.State.VALID) {
89             showError();
90         } else {
91             show(model.getRoot());
92         }
93     }
94     
95     private void showError() {
96         if (notAvailableLabel.isShowing()) {
97             return;
98         }
99         remove(treeView);
100         add(notAvailableLabel, BorderLayout.CENTER);
101         redraw();
102     }
103
104     private void redraw() {
105     TopComponent tc = (TopComponent) SwingUtilities.
106                 getAncestorOfClass(TopComponent.class,this);
107     if (tc != null) {
108         tc.revalidate();
109         tc.repaint();
110     }
111     }
112     
113     private void show(AXIComponent component) {
114         remove(notAvailableLabel);
115         add(treeView, BorderLayout.CENTER);
116     AXINodeVisitor nv = new AXINodeVisitor();
117     Node n = nv.getNode(component);
118     getExplorerManager().setRootContext(n);
119         redraw();
120     }
121     
122     // TODO add explorer manager listener to trigger navigation in
123
// main view
124

125     public boolean requestFocusInWindow() {
126     return treeView.requestFocusInWindow();
127     }
128             
129     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
130         String JavaDoc property = evt.getPropertyName();
131         if(AXIModel.STATE_PROPERTY.equals(property)) {
132             onModelStateChanged(evt);
133             return;
134         }
135         
136         TopComponent tc = (TopComponent) SwingUtilities.
137                 getAncestorOfClass(TopComponent.class,this);
138         if (ExplorerManager.PROP_SELECTED_NODES.equals(property)) {
139             Node[] filteredNodes = (Node[])evt.getNewValue();
140             if (filteredNodes != null && filteredNodes.length >= 1) {
141                 // Set the active nodes for the parent TopComponent.
142
if (tc != null) {
143                     tc.setActivatedNodes(filteredNodes);
144                 }
145             }
146         } else if(TopComponent.getRegistry().PROP_ACTIVATED.equals(property) &&
147                 tc == TopComponent.getRegistry().getActivated()) {
148             tc.setActivatedNodes(getExplorerManager().getSelectedNodes());
149         }
150     }
151     
152     public void onModelStateChanged(PropertyChangeEvent JavaDoc evt) {
153         State newState = (State)evt.getNewValue();
154         if(newState != AXIModel.State.VALID) {
155             showError();
156             return;
157         }
158         AXIModel model = (AXIModel)evt.getSource();
159         show(model.getRoot());
160     }
161     
162     //Always listens to the active model. Remove earlier listeners.
163
private AXIModel getAXIModel(DataObject dobj) {
164         try {
165             SchemaModelCookie modelCookie = (SchemaModelCookie)dobj.
166                     getCookie(SchemaModelCookie.class);
167             assert modelCookie != null;
168             AXIModel model = AXIModelFactory.getDefault().getModel(modelCookie.getModel());
169             if(model != null) {
170                 model.removePropertyChangeListener(this);
171                 model.addPropertyChangeListener(this);
172             }
173             
174             return model;
175     } catch (IOException JavaDoc ioe) {
176             //will show blank page if there is an error.
177
}
178         
179         return null;
180     }
181
182     @Override JavaDoc
183     public void addNotify() {
184     super.addNotify();
185     redraw();
186     }
187     
188 }
189
Popular Tags