KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > ui > column > ColumnListView


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.xam.ui.column;
21
22 import java.awt.Point JavaDoc;
23 import java.awt.Rectangle JavaDoc;
24 import java.awt.event.FocusEvent JavaDoc;
25 import java.awt.event.FocusListener JavaDoc;
26 import java.awt.event.MouseEvent JavaDoc;
27 import java.awt.event.MouseListener JavaDoc;
28 import java.beans.PropertyVetoException JavaDoc;
29 import javax.accessibility.AccessibleContext JavaDoc;
30 import javax.swing.Action JavaDoc;
31 import javax.swing.JList JavaDoc;
32 import javax.swing.JPopupMenu JavaDoc;
33 import javax.swing.KeyStroke JavaDoc;
34 import javax.swing.ListSelectionModel JavaDoc;
35 import javax.swing.SwingUtilities JavaDoc;
36 import org.openide.ErrorManager;
37 import org.openide.awt.MouseUtils;
38 import org.openide.explorer.ExplorerManager;
39 import org.openide.explorer.view.ListView;
40 import org.openide.nodes.Node;
41 import org.openide.nodes.NodeOp;
42 import org.openide.util.Lookup;
43 import org.openide.util.Utilities;
44 import org.openide.util.actions.ActionPerformer;
45 import org.openide.util.actions.CallbackSystemAction;
46 import org.openide.util.actions.SystemAction;
47
48 /**
49  * This is a slightly hackish solution to get the ListView to show the
50  * context menu of the root node if the user clicks in the area below
51  * the last list node.
52  *
53  * <p>Note that we ignore the performObjectAt() method since we don't
54  * have a need for that functionality. And it is not possible to override
55  * anyway, due to package-private code.</p>
56  *
57  * @author Nathan Fiedler
58  */

59 public class ColumnListView extends ListView {
60     /** silence compiler warnings */
61     private static final long serialVersionUID = 1L;
62     /** Popup menu support. */
63     private transient PopupSupport popupSupport;
64     /** Used to select nodes. */
65     private ExplorerManager explorerManager;
66
67     /**
68      * Creates a new instance of ColumnListView.
69      */

70     public ColumnListView() {
71         super();
72         // For deserialization only
73
}
74
75     /**
76      * Creates a new instance of ColumnListView.
77      *
78      * @param em ExplorerManager for selecting nodes.
79      */

80     public ColumnListView(ExplorerManager em) {
81         super();
82         explorerManager = em;
83     }
84
85     public void addNotify() {
86         super.addNotify();
87         MouseListener JavaDoc[] l = list.getMouseListeners();
88         for (MouseListener JavaDoc ml : l) {
89             if (ml instanceof ActionPerformer &&
90                     ml instanceof FocusListener JavaDoc) {
91                 // This is the PopupSupport that is installed by ListView.
92
// It is broken and needs to be replaced with our own.
93
list.removeMouseListener(ml);
94                 list.removeFocusListener((FocusListener JavaDoc) ml);
95                 break;
96             }
97         }
98         popupSupport = new PopupSupport();
99         list.addFocusListener(popupSupport);
100         list.addMouseListener(popupSupport);
101     }
102
103     protected JList JavaDoc createList() {
104         return new ColumnList();
105     }
106
107     public void removeNotify() {
108         super.removeNotify();
109         list.removeFocusListener(popupSupport);
110         list.removeMouseListener(popupSupport);
111     }
112
113     void createPopup(int xpos, int ypos, boolean context) {
114         if (explorerManager == null) {
115             return;
116         }
117         if (!isPopupAllowed()) {
118             return;
119         }
120
121         if (context) {
122             // For invisible root node, show its context menu.
123
// Must set the node selected for this to work.
124
Node[] nodes = new Node[] { explorerManager.getExploredContext() };
125             try {
126                 explorerManager.setSelectedNodes(nodes);
127             } catch (PropertyVetoException JavaDoc pve) {
128                 assert false : pve; // not permitted to be thrown
129
}
130         }
131         Action JavaDoc[] actions = NodeOp.findActions(explorerManager.getSelectedNodes());
132         JPopupMenu JavaDoc popup = Utilities.actionsToPopup(actions, this);
133         if (popup != null && popup.getSubElements().length > 0) {
134             popup.show(list, xpos, ypos);
135         }
136     }
137
138     final class PopupSupport extends MouseUtils.PopupMouseAdapter
139             implements ActionPerformer, Runnable JavaDoc, FocusListener JavaDoc {
140         private CallbackSystemAction csa;
141
142         protected void showPopup(MouseEvent JavaDoc e) {
143             Point JavaDoc p = new Point JavaDoc(e.getX(), e.getY());
144             int i = list.locationToIndex(p);
145             Rectangle JavaDoc r = list.getCellBounds(i, i);
146             boolean contextMenu = (r == null) || !r.contains(p);
147             if (!contextMenu && !list.isSelectedIndex(i)) {
148                 // Do not set the last item selected unless the user
149
// actually clicked on it. This is to avoid conflicting
150
// with createPopup() which will set the root node
151
// selected if contextMenu is true.
152
list.setSelectedIndex(i);
153             }
154             createPopup(e.getX(), e.getY(), contextMenu);
155         }
156
157         public void performAction(SystemAction act) {
158             SwingUtilities.invokeLater(this);
159         }
160
161         public void run() {
162             boolean multisel = (list.getSelectionMode() != ListSelectionModel.SINGLE_SELECTION);
163             int i = (multisel ? list.getLeadSelectionIndex() : list.getSelectedIndex());
164             if (i < 0) {
165                 return;
166             }
167             Point JavaDoc p = list.indexToLocation(i);
168             if (p == null) {
169                 return;
170             }
171             createPopup(p.x, p.y, false);
172         }
173
174         @SuppressWarnings JavaDoc("deprecation")
175         public void focusGained(FocusEvent JavaDoc ev) {
176             if (csa == null) {
177                 try {
178                     ClassLoader JavaDoc l = (ClassLoader JavaDoc)Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
179                     if (l == null) {
180                         l = getClass().getClassLoader();
181                     }
182                     Class JavaDoc popup = Class.forName("org.openide.actions.PopupAction", true, l); // NOI18N
183
csa = (CallbackSystemAction) CallbackSystemAction.get(popup);
184                 } catch (ClassNotFoundException JavaDoc e) {
185                     Error JavaDoc err = new NoClassDefFoundError JavaDoc();
186                     ErrorManager.getDefault().annotate(err, e);
187                     throw err;
188                 }
189             }
190             csa.setActionPerformer(this);
191         }
192
193         @SuppressWarnings JavaDoc("deprecation")
194         public void focusLost(FocusEvent JavaDoc ev) {
195             if (csa != null && csa.getActionPerformer() instanceof PopupSupport) {
196                 csa.setActionPerformer(null);
197             }
198         }
199     }
200
201     /**
202      * Specialized JList that tracks the viewport width in order to
203      * prevent horizontal scrolling within the columns view. This works
204      * in concert with the list cell renderer to show the node display
205      * name in truncated form (with ...) and an arrow border.
206      *
207      * @author Nathan Fiedler
208      */

209     private class ColumnList extends JList JavaDoc {
210         /** silence compiler warnings */
211         private static final long serialVersionUID = 1L;
212
213         ColumnList() {
214             super();
215
216             // fix for 83915
217
// copied from ListView.NbList
218
// fix for #18292
219
// default action map for JList defines these shortcuts
220
// but we use our own mechanism for handling them
221
// following lines disable default L&F handling (if it is
222
// defined on Ctrl-c, Ctrl-v and Ctrl-x)
223
getInputMap().put(KeyStroke.getKeyStroke("control C"), "none"); // NOI18N
224
getInputMap().put(KeyStroke.getKeyStroke("control V"), "none"); // NOI18N
225
getInputMap().put(KeyStroke.getKeyStroke("control X"), "none"); // NOI18N
226
}
227
228         public boolean getScrollableTracksViewportWidth() {
229             // Prevent horizontal scrolling in the column view.
230
return true;
231         }
232
233         // Accessibility:
234
public AccessibleContext JavaDoc getAccessibleContext() {
235             if (accessibleContext == null) {
236                 accessibleContext = new AccessibleColumnList();
237             }
238
239             return accessibleContext;
240         }
241
242         private class AccessibleColumnList extends AccessibleJList {
243             /** silence compiler warnings */
244             private static final long serialVersionUID = 1L;
245
246             AccessibleColumnList() {
247             }
248
249             public String JavaDoc getAccessibleName() {
250                 return ColumnListView.this.getAccessibleContext().getAccessibleName();
251             }
252
253             public String JavaDoc getAccessibleDescription() {
254                 return ColumnListView.this.getAccessibleContext().getAccessibleDescription();
255             }
256         }
257     }
258 }
259
Popular Tags