KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > config > 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 package org.netbeans.modules.j2ee.sun.share.config.ui;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.beans.*;
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInput JavaDoc;
26 import java.io.ObjectOutput JavaDoc;
27 import java.io.OptionalDataException JavaDoc;
28 import java.io.Serializable JavaDoc;
29 import java.text.MessageFormat JavaDoc;
30 import java.util.*;
31 import javax.swing.border.LineBorder JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
33 import javax.swing.event.ListSelectionEvent JavaDoc;
34 import javax.swing.event.ListSelectionListener JavaDoc;
35 import javax.swing.*;
36 import javax.swing.table.TableColumn JavaDoc;
37 import javax.swing.undo.CannotRedoException JavaDoc;
38 import javax.swing.undo.CannotUndoException JavaDoc;
39
40 import org.openide.actions.PopupAction;
41 //import org.openide.awt.MouseUtils;
42

43 import org.openide.actions.FindAction;
44 //import org.openide.awt.UndoRedo;
45
import org.openide.cookies.CloseCookie;
46 import org.openide.cookies.OpenCookie;
47 import org.openide.cookies.SaveCookie;
48 import org.openide.explorer.ExplorerManager;
49
50 import org.openide.filesystems.FileObject;
51 import org.openide.filesystems.FileStateInvalidException;
52 import org.openide.loaders.MultiDataObject;
53 import org.openide.loaders.FileEntry;
54 import org.openide.loaders.DataObject;
55 import org.openide.loaders.OpenSupport;
56 import org.openide.NotifyDescriptor;
57 import org.openide.text.CloneableEditorSupport;
58 import org.openide.util.actions.CallbackSystemAction;
59 import org.openide.util.actions.SystemAction;
60 import org.openide.util.*;
61 import org.openide.windows.*;
62 import org.openide.util.Utilities;
63 import org.openide.nodes.*;
64 import org.openide.util.WeakListeners;
65
66
67 /**
68  * The component that will display a panel corresponding to the node selection of its parent ExplorerManager, usually a
69  * ContentPanel. It provides the mapping between selected nodes and displayed panels. A parent ContentPanel will get a node hierarchy
70  * by calling getRoot and display that tree in it's structure view. The PanelView will then show an appropriate panel
71  * based on the node selection. Subclasses should, at a bare minimum, initialize the root node and override the showSelection method.
72  */

73 public abstract class PanelView extends TopComponent implements PanelFocusCookie {
74     
75     private Node root;
76 // /** not null if popup menu enabled */
77
// transient PopupAdapter popupListener;
78
/** the most important listener */
79     transient NodeSelectedListener nodeListener = null;
80     
81     /** Explorer manager, valid when this view is showing */
82     transient private ExplorerManager manager;
83     /** weak variation of the listener for property change on the explorer manager */
84     transient PropertyChangeListener wlpc;
85     /** weak variation of the listener for vetoable change on the explorer manager */
86     transient VetoableChangeListener wlvc;
87     /** ResourceBundle for ejb package. */
88     protected ResourceBundle bundle;
89     
90     
91     /**
92      * Creates a new instance of PanelView
93      */

94     public PanelView() {
95         // init listener & attach it
96
nodeListener = new NodeSelectedListener();
97         bundle = NbBundle.getBundle(PanelView.class);
98     }
99     
100     /**
101      * Gets the current root Node.
102      * @return the Node.
103      */

104     public Node getRoot() {
105         return root;
106     }
107     
108     public void setRoot(Node r){
109         root = r;
110     }
111     
112     /** Called when the view wishes to reuse the context menu from the root node
113      * as its own context menu
114      * @param value true to set the context menu, false otherwise
115      */

116 // public void setPopupAllowed(boolean value) {
117
// if (popupListener == null && value) {
118
// // on
119
// popupListener = new PopupAdapter();
120
// addMouseListener(popupListener);
121
// return;
122
// }
123
// if (popupListener != null && !value) {
124
// // off
125
// removeMouseListener(popupListener);
126
// popupListener = null;
127
// return;
128
// }
129
// }
130

131     /** Popup adapter.
132      */

133 // private class PopupAdapter extends MouseUtils.PopupMouseAdapter {
134
//
135
// PopupAdapter() {}
136
//
137
// protected void showPopup(MouseEvent e) {
138
// JPopupMenu popup = getRoot().getContextMenu();
139
// popup.show(PanelView.this,e.getX(), e.getY());
140
//
141
// }
142
//
143
// }
144

145     /** Called when explorer manager has changed the current selection.
146      * The view should display the panel corresponding to the selected nodes
147      * @param nodes the nodes used to update the view
148      */

149     abstract public void showSelection(Node[] nodes) ;
150     
151     /** The view can change the explorer manager's current selection.
152      * @param value the new node to explore
153      * @param nodes the nodes to select
154      * @return false if the explorer manager is not able to change the selection
155      */

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

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

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

194     public void open(){
195     }
196    /**
197      * A parent ComponentPanel uses this method to notify its PanelView children it is about to close.
198      * and lets them determine if they are ready. Default implementation just returns true.
199      *
200      *
201      * @return boolean True if the PanelView is ready to close, false otherwise.
202      */

203     public boolean canClose(){
204         return true;
205     }
206      /**
207      * This method supports the PanelFocusCookie. It allows an external source set the focus on a ComponentPanel.
208      * See the JavaDoc for PanelFocusCookie for more information.
209      * Default implementation returns true if the panelViewNameHint is the same as the name of the PanelView as determined
210      * by calling get name. It would be the responsibility of any subclass to reset the currently selected node if necessary.
211      *
212      *
213      * @param panelViewNameHint String used as a hint for the appropriate PanelView if there is more than one.
214      * @param panelNameHint String used as a hint for the appropiate panel in the PanelView
215      * @param focusObject Object that can be used to identify the object that should have the focus.
216      * @return true if the ComponentPanel was able to focus on the object.
217      */

218     public boolean setFocusOn(String JavaDoc panelViewNameHint, String JavaDoc panelNameHint, Object JavaDoc focusObject){
219         if (panelViewNameHint !=null && panelViewNameHint.equals(getName()))
220          return true;
221         return false;
222     }
223     
224     
225     /**
226      * Computes the localized string for the key.
227      * @param key The tag of the string.
228      * @return the localized string.
229      */

230     public String JavaDoc getResourceString(String JavaDoc key) {
231         return bundle.getString(key);
232     }
233     
234      /* Initializes the component.
235       * We need to register for ExplorerManager events here
236       */

237     public void addNotify() {
238         super.addNotify();
239         
240         // Enter key in the tree
241

242         
243         ExplorerManager newManager = ExplorerManager.find(this);
244         
245         if (newManager != manager) {
246             if (manager != null) {
247                 manager.removeVetoableChangeListener(wlvc);
248                 manager.removePropertyChangeListener(wlpc);
249             }
250             
251             manager = newManager;
252             
253             manager.addVetoableChangeListener(wlvc = WeakListeners.vetoableChange(nodeListener, manager));
254             manager.addPropertyChangeListener(wlpc = WeakListeners.propertyChange(nodeListener, manager));
255             
256         }
257         
258     }
259     
260     /**
261      * Gets the current ExplorerManager the view is attached.
262      * @return the ExplorerManager.
263      */

264     
265     public ExplorerManager getExplorerManager() {
266         return manager;
267     }
268     
269     class NodeSelectedListener implements VetoableChangeListener,PropertyChangeListener {
270         
271         public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
272             if (evt.getPropertyName().equals(ExplorerManager.PROP_SELECTED_NODES)) {
273                 if (!selectionAccept((Node[])evt.getNewValue())) {
274                     throw new PropertyVetoException("", evt); // NOI18N
275
}
276             }
277         }
278         
279         public void propertyChange(PropertyChangeEvent evt) {
280             if (!ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName()))
281                 return;
282             Node[] selectedNodes = getExplorerManager().getSelectedNodes();
283             showSelection(selectedNodes);
284             
285         }
286     }
287 }
288
289
Popular Tags