KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > view > ListTableView


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.openide.explorer.view;
20
21 import org.openide.explorer.ExplorerManager;
22 import org.openide.explorer.view.TableSheet.ControlledTableView;
23 import org.openide.nodes.Node;
24 import org.openide.nodes.Node.Property;
25 import org.openide.util.WeakListeners;
26
27 import java.awt.Dimension JavaDoc;
28 import java.awt.event.FocusEvent JavaDoc;
29 import java.awt.event.FocusListener JavaDoc;
30
31 import java.beans.PropertyChangeEvent JavaDoc;
32 import java.beans.PropertyChangeListener JavaDoc;
33
34 import javax.swing.*;
35 import javax.swing.event.ListDataEvent JavaDoc;
36 import javax.swing.event.ListDataListener JavaDoc;
37
38
39 /** Explorer view. Allows to view list of nodes on the left
40  * and its properties in table on the right.
41  *
42  * @deprecated Use <code>org.openide.explorer.view.TreeTableView</code> instead.
43  *
44  * @author jrojcek
45  * @since 1.7
46  */

47 public @Deprecated JavaDoc class ListTableView extends ListView {
48     /** flag that allows group more requests into one */
49     private boolean tableChanging = false;
50
51     /** manager is used only for synchronization of header name */
52     private ExplorerManager manager;
53
54     /** listener on explorer manager */
55     private PropertyChangeListener JavaDoc wlpc;
56
57     /** table view that is controlled by list view */
58     private ControlledTableView controlledTableView;
59
60     /** listener on changes in list model */
61     private Listener JavaDoc listener;
62
63     /** preferred size*/
64     private Dimension JavaDoc prefSize;
65
66     /** table */
67     private JTable table;
68
69     /** Create ListTableView with default NodeTableModel
70      */

71     public ListTableView() {
72         this(null);
73     }
74
75     /** Creates ListTableView with provided NodeTableModel.
76      * @param ntm node table model
77      */

78     public ListTableView(NodeTableModel ntm) {
79         // do not use scroll bars, this scroll pane is dummy, only for border painting
80
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
81         setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
82
83         // remove list from viewportview
84
setViewportView(null);
85
86         // insert list into new scrollpane
87
JScrollPane listView = new JScrollPane(list);
88         listView.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
89         listView.setBorder(null);
90
91         // create table view controlled by new scrollpane
92
controlledTableView = (ntm == null) ? new TableSheet.ControlledTableView(listView)
93                                             : new TableSheet.ControlledTableView(listView, ntm);
94         setViewportView(controlledTableView.compoundScrollPane());
95
96         listener = new Listener JavaDoc();
97         delayedFireTableDataChanged();
98
99         setPreferredSize(new Dimension JavaDoc(400, 400));
100
101         table = controlledTableView.getTable();
102     }
103
104     /** Set columns.
105      * @param props each column is constructed from Node.Property
106      */

107     public void setProperties(Property[] props) {
108         controlledTableView.setProperties(props);
109     }
110
111     /** Sets resize mode of table.
112      *
113      * @param mode - One of 5 legal values: <pre>JTable.AUTO_RESIZE_OFF,
114      * JTable.AUTO_RESIZE_NEXT_COLUMN,
115      * JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS,
116      * JTable.AUTO_RESIZE_LAST_COLUMN,
117      * JTable.AUTO_RESIZE_ALL_COLUMNS</pre>
118      */

119     public final void setTableAutoResizeMode(int mode) {
120         controlledTableView.setAutoResizeMode(mode);
121     }
122
123     /** Gets resize mode of table.
124      *
125      * @return mode - One of 5 legal values: <pre>JTable.AUTO_RESIZE_OFF,
126      * JTable.AUTO_RESIZE_NEXT_COLUMN,
127      * JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS,
128      * JTable.AUTO_RESIZE_LAST_COLUMN,
129      * JTable.AUTO_RESIZE_ALL_COLUMNS</pre>
130      */

131     public final int getTableAutoResizeMode() {
132         return controlledTableView.getAutoResizeMode();
133     }
134
135     /** Sets preferred width of table column
136      * @param index column index
137      * @param width preferred column width
138      */

139     public final void setTableColumnPreferredWidth(int index, int width) {
140         controlledTableView.setColumnPreferredWidth(index, width);
141     }
142
143     /** Gets preferred width of table column
144      * @param index column index
145      * @return preferred column width
146      */

147     public final int getTableColumnPreferredWidth(int index) {
148         return controlledTableView.getColumnPreferredWidth(index);
149     }
150
151     /** Set preferred width of list view
152      * @param width preferred width
153      */

154     public void setListPreferredWidth(int width) {
155         controlledTableView.setControllingViewWidth(width);
156
157         Dimension JavaDoc dim = getPreferredSize();
158
159         // controlledTableView.setPreferredSize(new Dimension(dim.width - width, dim.height));
160
table.setPreferredScrollableViewportSize(new Dimension JavaDoc(dim.width - width, dim.height));
161     }
162
163     /** Get preferred size of list view
164      * @return preferred width of list view
165      */

166     public final int getListPreferredWidth() {
167         return controlledTableView.getControllingViewWidth();
168     }
169
170     public void setPreferredSize(Dimension JavaDoc dim) {
171         super.setPreferredSize(dim);
172         prefSize = dim;
173     }
174
175     public Dimension JavaDoc getPreferredSize() {
176         return prefSize;
177     }
178
179     public void addNotify() {
180         super.addNotify();
181
182         ExplorerManager newManager = ExplorerManager.find(this);
183
184         if (newManager != manager) {
185             if (manager != null) {
186                 manager.removePropertyChangeListener(wlpc);
187             }
188
189             manager = newManager;
190             manager.addPropertyChangeListener(wlpc = org.openide.util.WeakListeners.propertyChange(listener, manager));
191             controlledTableView.setHeaderText(manager.getExploredContext().getDisplayName());
192         }
193
194         list.getModel().addListDataListener(listener);
195         list.addFocusListener(listener);
196         delayedFireTableDataChanged();
197     }
198
199     public void removeNotify() {
200         super.removeNotify();
201         list.getModel().removeListDataListener(listener);
202         list.removeFocusListener(listener);
203
204         // clear node listeners
205
controlledTableView.setNodes(new Node[] { });
206     }
207
208     /** Chnage table data in awt thread */
209     private void delayedFireTableDataChanged() {
210         if (tableChanging) {
211             return;
212         }
213
214         tableChanging = true;
215         SwingUtilities.invokeLater(
216             new Runnable JavaDoc() {
217                 public void run() {
218                     if (list.getCellBounds(0, 0) != null) {
219                         controlledTableView.setRowHeight(list.getCellBounds(0, 0).height);
220                     }
221
222                     changeTableModel();
223                     tableChanging = false;
224                 }
225             }
226         );
227     }
228
229     /** Change table model. Sets rows (nodes) of table mmodel */
230     private void changeTableModel() {
231         Node[] nodes = new Node[list.getModel().getSize()];
232
233         for (int i = 0; i < list.getModel().getSize(); i++) {
234             nodes[i] = Visualizer.findNode(list.getModel().getElementAt(i));
235         }
236
237         controlledTableView.setNodes(nodes);
238     }
239
240     /** Listenes on changes in list model.
241      */

242     private class Listener implements PropertyChangeListener JavaDoc, FocusListener JavaDoc, ListDataListener JavaDoc {
243         Listener() {
244         }
245
246         /**
247          * Sent after the indices in the index0,index1
248          * interval have been inserted in the data model.
249          * The new interval includes both index0 and index1.
250          *
251          * @param e a ListDataEvent encapuslating the event information
252          */

253         public void intervalAdded(ListDataEvent JavaDoc e) {
254             delayedFireTableDataChanged();
255         }
256
257         /**
258          * Sent after the indices in the index0,index1 interval
259          * have been removed from the data model. The interval
260          * includes both index0 and index1.
261          *
262          * @param e a ListDataEvent encapuslating the event information
263          */

264         public void intervalRemoved(ListDataEvent JavaDoc e) {
265             delayedFireTableDataChanged();
266         }
267
268         /**
269          * Sent when the contents of the list has changed in a way
270          * that's too complex to characterize with the previous
271          * methods. Index0 and index1 bracket the change.
272          *
273          * @param e a ListDataEvent encapuslating the event information
274          */

275         public void contentsChanged(ListDataEvent JavaDoc e) {
276             delayedFireTableDataChanged();
277         }
278
279         public void focusGained(FocusEvent JavaDoc evt) {
280         }
281
282         public void focusLost(FocusEvent JavaDoc evt) {
283             if (evt.isTemporary()) {
284                 return;
285             }
286
287             int selectedRow = list.getSelectedIndex();
288             table.getSelectionModel().setAnchorSelectionIndex(selectedRow);
289             table.getColumnModel().getSelectionModel().setAnchorSelectionIndex(0);
290         }
291
292         /** Updates header name.
293          * @param evt event
294          */

295         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
296             if (ExplorerManager.PROP_EXPLORED_CONTEXT.equals(evt.getPropertyName())) {
297                 controlledTableView.setHeaderText(manager.getExploredContext().getDisplayName());
298             }
299         }
300     }
301
302     /*
303         public static void main (String[] args) {
304             SwingUtilities.invokeLater(new Runnable() {
305                 public void run() {
306                     Node n = new org.netbeans.core.ModuleNode();
307                     ExplorerManager em = new ExplorerManager();
308                     em.setRootContext(n);
309                     ListTableView ttv = new ListTableView();
310     // ttv.model.setDepth(2);
311     // ttv.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
312                     ttv.setProperties(
313     // n.getChildren().getNodes()[0].getPropertySets()[0].getProperties());
314                         new Node.Property[]{
315                             new PropertySupport.ReadWrite (
316                                 "enabled", // NOI18N
317                                 Boolean.TYPE,
318                                 org.openide.util.NbBundle.getMessage (org.netbeans.core.Main.class, "PROP_modules_enabled"),
319                                 org.openide.util.NbBundle.getMessage (org.netbeans.core.Main.class, "HINT_modules_enabled")
320                             ) {
321                                 public Object getValue () {
322                                     return null;
323                                 }
324
325                                 public void setValue (Object o) {
326                                 }
327                             },
328                             new PropertySupport.ReadOnly (
329                                 "specVersion", // NOI18N
330                                 String.class,
331                                 org.openide.util.NbBundle.getMessage (org.netbeans.core.Main.class, "PROP_modules_specversion"),
332                                 org.openide.util.NbBundle.getMessage (org.netbeans.core.Main.class, "HINT_modules_specversion")
333                             ) {
334                                 public Object getValue () {
335                                     return null;
336                                 }
337
338                             }
339                         }
340                     );
341
342                     org.openide.explorer.ExplorerPanel ep = new org.openide.explorer.ExplorerPanel (em);
343                     ep.setLayout (new java.awt.BorderLayout ());
344                     ep.add ("Center", ttv);
345                     ep.open ();
346                 }
347             });
348         }
349     */

350 }
351
Popular Tags