KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > table > ChooseColumnsPanel


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.tasklist.core.table;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.util.ArrayList JavaDoc;
25
26 import javax.swing.ImageIcon JavaDoc;
27 import javax.swing.JButton JavaDoc;
28 import javax.swing.JScrollPane JavaDoc;
29 import javax.swing.JTable JavaDoc;
30 import javax.swing.table.TableColumn JavaDoc;
31 import javax.swing.table.TableColumnModel JavaDoc;
32 import javax.swing.table.TableModel JavaDoc;
33
34 import org.netbeans.modules.tasklist.core.checklist.CheckListModel;
35 import org.netbeans.modules.tasklist.core.checklist.DefaultCheckListModel;
36 import org.openide.DialogDescriptor;
37 import org.openide.DialogDisplayer;
38 import org.openide.NotifyDescriptor;
39 import org.openide.util.NbBundle;
40
41 /**
42  * Panel for choosing visible columns in a table.
43  *
44  * @author tl
45  */

46 public class ChooseColumnsPanel extends javax.swing.JPanel JavaDoc {
47
48     private static final long serialVersionUID = 1;
49
50     /**
51      * ActionListener for the "choose visible columns" dialog
52      */

53     private static class ChooseColumnsActionListener implements ActionListener JavaDoc {
54         private JTable JavaDoc table;
55         
56         /**
57          * Constructor
58          *
59          * @param t columns in this table will be configured
60          */

61         public ChooseColumnsActionListener(JTable JavaDoc t) {
62             this.table = t;
63         }
64         
65         public void actionPerformed(ActionEvent JavaDoc e) {
66             TableModel JavaDoc tm = table.getModel();
67             int n = tm.getColumnCount();
68             String JavaDoc[] names = new String JavaDoc[n];
69             for (int i = 0; i < n; i++) {
70                 names[i] = tm.getColumnName(i);
71             }
72             
73             boolean[] checked = new boolean[n];
74             TableColumnModel JavaDoc tcm = table.getColumnModel();
75             for (int i = 0; i < tcm.getColumnCount(); i++) {
76                 checked[tcm.getColumn(i).getModelIndex()] = true;
77             }
78             
79             // Show the dialog
80
ChooseColumnsPanel ccp = new ChooseColumnsPanel(checked, names);
81             DialogDescriptor dd = new DialogDescriptor(
82                 ccp, NbBundle.getMessage(ChooseColumnsPanel.class,
83                 "ChangeVisibleColumns")); // NOI18N
84
Object JavaDoc res = DialogDisplayer.getDefault().notify(dd);
85             
86             if (res == NotifyDescriptor.OK_OPTION) {
87                 ArrayList JavaDoc<TableColumn JavaDoc> old = new ArrayList JavaDoc<TableColumn JavaDoc>();
88                 for (int i = 0; i < tcm.getColumnCount(); i++) {
89                     old.add(tcm.getColumn(i));
90                 }
91                 
92                 table.createDefaultColumnsFromModel();
93                 for (int i = 0; i < old.size(); i++) {
94                     tcm.addColumn((TableColumn JavaDoc) old.get(i));
95                     tcm.moveColumn(tcm.getColumnCount() - 1, i);
96                 }
97                 
98                 for (int i = 0; i < tcm.getColumnCount(); ) {
99                     TableColumn JavaDoc tc = tcm.getColumn(i);
100                     int index = tc.getModelIndex();
101                     if (!checked[index]) {
102                         tcm.removeColumn(tcm.getColumn(i));
103                     } else {
104                         checked[index] = false;
105                         i++;
106                     }
107                 }
108             }
109         }
110     }
111     
112     /**
113      * Finds a column
114      *
115      * @param tcm a table columns model
116      * @param index model's column index or -1
117      */

118     private static int findColumn(TableColumnModel JavaDoc tcm, int modelIndex) {
119         for (int i = 0; i < tcm.getColumnCount(); i++) {
120             if (tcm.getColumn(i).getModelIndex() == modelIndex)
121                 return i;
122         }
123         return -1;
124     }
125     
126     /**
127      * Installs a special button in the right upper corner of a scroll pane
128      * that can be used to change visible columns in a table
129      *
130      * @param sp a scroll pane with a table
131      */

132     public static void installChooseColumnsButton(JScrollPane JavaDoc sp) {
133         JTable JavaDoc table = (JTable JavaDoc) sp.getViewport().getView();
134         JButton JavaDoc b = new JButton JavaDoc(
135             new ImageIcon JavaDoc(ChooseColumnsPanel.class.getResource("columns.gif"))); // NOI18N
136
b.addActionListener(new ChooseColumnsActionListener(table));
137         sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
138         sp.setCorner(JScrollPane.UPPER_RIGHT_CORNER, b);
139     }
140     
141     /**
142      * Creates new form ChooseColumnsPanel
143      *
144      * @param visibility true = column visible
145      * @param name column names
146      */

147     public ChooseColumnsPanel(boolean[] visibility, String JavaDoc[] names) {
148         initComponents();
149         columnsCheckList.setModel(new DefaultCheckListModel(visibility, names));
150     }
151
152     /**
153      * Returns an array with visibility attributes
154      *
155      * @return true = visible
156      */

157     public boolean[] getChecked() {
158         CheckListModel m = (CheckListModel) columnsCheckList.getModel();
159         boolean[] b = new boolean[m.getSize()];
160         for (int i = 0; i < m.getSize(); i++) {
161             b[i] = m.isChecked(i);
162         }
163         return b;
164     }
165     
166     /** This method is called from within the constructor to
167      * initialize the form.
168      * WARNING: Do NOT modify this code. The content of this method is
169      * always regenerated by the Form Editor.
170      */

171     private void initComponents() {//GEN-BEGIN:initComponents
172
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
173
174         jLabel1 = new javax.swing.JLabel JavaDoc();
175         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
176         columnsCheckList = new org.netbeans.modules.tasklist.core.checklist.CheckList();
177
178         setLayout(new java.awt.GridBagLayout JavaDoc());
179
180         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(12, 12, 11, 11)));
181         setPreferredSize(new java.awt.Dimension JavaDoc(400, 300));
182         jLabel1.setLabelFor(columnsCheckList);
183         jLabel1.setText(org.openide.util.NbBundle.getMessage(ChooseColumnsPanel.class, "ChooseTheColumnsToDisplay"));
184         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
185         gridBagConstraints.gridx = 0;
186         gridBagConstraints.gridy = 0;
187         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
188         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 0);
189         add(jLabel1, gridBagConstraints);
190
191         jScrollPane1.setViewportView(columnsCheckList);
192
193         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
194         gridBagConstraints.gridx = 0;
195         gridBagConstraints.gridy = 1;
196         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
197         gridBagConstraints.weightx = 1.0;
198         gridBagConstraints.weighty = 1.0;
199         add(jScrollPane1, gridBagConstraints);
200
201     }//GEN-END:initComponents
202

203
204     // Variables declaration - do not modify//GEN-BEGIN:variables
205
private org.netbeans.modules.tasklist.core.checklist.CheckList columnsCheckList;
206     private javax.swing.JLabel JavaDoc jLabel1;
207     private javax.swing.JScrollPane JavaDoc jScrollPane1;
208     // End of variables declaration//GEN-END:variables
209

210 }
211
Popular Tags