KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > multiview > ui > PanelView


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.multiview.ui;
21
22 import java.awt.event.MouseEvent JavaDoc;
23 import java.beans.*;
24 import javax.swing.JPopupMenu JavaDoc;
25
26 import org.openide.awt.MouseUtils;
27 import org.openide.explorer.ExplorerManager;
28 import org.openide.windows.TopComponent;
29 import org.openide.nodes.Node;
30 import org.openide.util.WeakListeners;
31
32 /**
33  * The component that will display a panel corresponding to the node selection of its parent ExplorerManager, usually a
34  * ContentPanel. It provides the mapping between selected nodes and displayed panels. A parent ContentPanel will get a node hierarchy
35  * by calling getRoot and display that tree in it's structure view. The PanelView will then show an appropriate panel
36  * based on the node selection. Subclasses should, at a bare minimum, initialize the root node and override the showSelection method.
37  
38  * Created on October 29, 2002, 12:24 PM
39  * @author B.Ashby, M.Kuchtiak
40  */

41 public abstract class PanelView extends javax.swing.JPanel JavaDoc {
42     
43     private Node root;
44     /** not null if popup menu enabled */
45     transient boolean sectionHeaderClicked;
46     transient PopupAdapter popupListener;
47     /** the most important listener */
48     //transient NodeSelectedListener nodeListener = null;
49

50     /** Explorer manager, valid when this view is showing */
51     transient private ExplorerManager manager;
52     /** weak variation of the listener for property change on the explorer manager */
53     transient PropertyChangeListener wlpc;
54     /** weak variation of the listener for vetoable change on the explorer manager */
55     transient VetoableChangeListener wlvc;
56     /** ResourceBundle for ejb package. */
57     transient ErrorPanel errorPanel;
58     
59     public PanelView() {
60         initComponents();
61     }
62     
63     public void initComponents () {
64     }
65     
66     void attachErrorPanel(ErrorPanel errorPanel) {
67         this.errorPanel=errorPanel;
68     }
69     
70     public ErrorPanel getErrorPanel() {
71         return errorPanel;
72     }
73     
74     protected abstract org.netbeans.modules.xml.multiview.Error validateView();
75     
76     public final void checkValidity() {
77         org.netbeans.modules.xml.multiview.Error error = validateView();
78         if (error!=null) {
79             errorPanel.setError(error);
80         } else {
81             errorPanel.clearError();
82         }
83     }
84     
85     /**
86      * Gets the current root Node.
87      * @return the Node.
88      */

89     public Node getRoot() {
90         return root;
91     }
92     
93     public void setRoot(Node r){
94         root = r;
95     }
96
97     public void setSectionHeaderClicked(boolean value) {
98         sectionHeaderClicked=value;
99     }
100     
101     public boolean isSectionHeaderClicked() {
102         return sectionHeaderClicked;
103     }
104     
105     /** Called when the view wishes to reuse the context menu from the root node
106      * as its own context menu
107      * @param value true to set the context menu, false otherwise
108      */

109     public void setPopupAllowed(boolean value) {
110         if (popupListener == null && value) {
111             // on
112
popupListener = new PopupAdapter();
113             addMouseListener(popupListener);
114             return;
115         }
116         if (popupListener != null && !value) {
117             // off
118
removeMouseListener(popupListener);
119             popupListener = null;
120             return;
121         }
122     }
123     
124     /** Popup adapter.
125      */

126     private class PopupAdapter extends MouseUtils.PopupMouseAdapter {
127         
128         PopupAdapter() {
129         }
130         
131         protected void showPopup(MouseEvent JavaDoc e) {
132             JPopupMenu JavaDoc popup = getRoot().getContextMenu();
133             popup.show(PanelView.this,e.getX(), e.getY());
134             
135         }
136         
137     }
138     
139     /** Called when explorer manager has changed the current selection.
140      * The view should display the panel corresponding to the selected nodes
141      * @param nodes the nodes used to update the view
142      */

143     abstract public void showSelection(Node[] nodes) ;
144     
145     /** The view can change the explorer manager's current selection.
146      * @param value the new node to explore
147      * @param nodes the nodes to select
148      * @return false if the explorer manager is not able to change the selection
149      */

150     
151     public boolean setManagerExploredContextAndSelection(Node value, Node[] nodes) {
152         try{
153             getExplorerManager().setExploredContextAndSelection(value, nodes);
154         }
155         catch (PropertyVetoException e) {
156             return false;
157         }
158         return true;
159     }
160     /** The view can change the explorer manager's current selection.
161      * @param nodes the nodes to select
162      * @return false if the explorer manager is not able to change the selection
163      */

164     
165     public boolean setManagerSelection(Node[] nodes) {
166         try{
167             getExplorerManager().setSelectedNodes(nodes);
168         }
169         catch (PropertyVetoException e) {
170             return false;
171         }
172         return true;
173     }
174     /** Called when explorer manager is about to change the current selection.
175      * The view can forbid the change if it is not able to display such
176      * selection.
177      * @param nodes the nodes to select
178      * @return false if the view is not able to change the selection
179      */

180     protected boolean selectionAccept(Node[] nodes) {
181         return true;
182     }
183     
184      /**
185      * A parent ComponentPanel uses this method to notify its PanelView children that it was opened
186      * and lets them do any needed initialization as a result. Default implementation does nothing.
187      */

188     public void open(){
189     }
190    /**
191      * A parent ComponentPanel uses this method to notify its PanelView children it is about to close.
192      * and lets them determine if they are ready. Default implementation just returns true.
193      * @return boolean True if the PanelView is ready to close, false otherwise.
194      */

195     public boolean canClose(){
196         return true;
197     }
198     
199      /* Initializes the component.
200       * We need to register for ExplorerManager events here
201       */

202     
203     public void addNotify() {
204         super.addNotify();
205         
206         // Enter key in the tree
207
/*
208         ExplorerManager newManager = ExplorerManager.find(this);
209         System.out.println("newManager="+newManager);
210         if (newManager != manager) {
211             if (manager != null) {
212                 manager.removeVetoableChangeListener(wlvc);
213                 manager.removePropertyChangeListener(wlpc);
214             }
215             
216             manager = newManager;
217             
218             manager.addVetoableChangeListener(wlvc = WeakListeners.vetoableChange(nodeListener, manager));
219             manager.addPropertyChangeListener(wlpc = WeakListeners.propertyChange(nodeListener, manager));
220             //manager.addPropertyChangeListener(nodeListener);
221             
222         }
223          */

224     }
225     
226     /**
227      * Gets the current ExplorerManager the view is attached.
228      * @return the ExplorerManager.
229      */

230     
231     public ExplorerManager getExplorerManager() {
232         return ExplorerManager.find(this);
233         //return manager;
234
}
235     /*
236     class NodeSelectedListener implements VetoableChangeListener,PropertyChangeListener {
237         
238         public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
239             if (evt.getPropertyName().equals(ExplorerManager.PROP_SELECTED_NODES)) {
240                 if (!selectionAccept((Node[])evt.getNewValue())) {
241                     throw new PropertyVetoException("", evt); // NOI18N
242                 }
243             }
244         }
245         
246         public void propertyChange(PropertyChangeEvent evt) {
247             System.out.println("propName = "+evt.getPropertyName()+":"+ExplorerManager.PROP_SELECTED_NODES);
248             if (!ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName()))
249                 return;
250             Node[] selectedNodes = getExplorerManager().getSelectedNodes();
251             showSelection(selectedNodes);
252             
253         }
254     }
255      */

256 }
257
Popular Tags