1 19 20 package org.netbeans.modules.tasklist.core.table; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.util.ArrayList ; 25 26 import javax.swing.ImageIcon ; 27 import javax.swing.JButton ; 28 import javax.swing.JScrollPane ; 29 import javax.swing.JTable ; 30 import javax.swing.table.TableColumn ; 31 import javax.swing.table.TableColumnModel ; 32 import javax.swing.table.TableModel ; 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 46 public class ChooseColumnsPanel extends javax.swing.JPanel { 47 48 private static final long serialVersionUID = 1; 49 50 53 private static class ChooseColumnsActionListener implements ActionListener { 54 private JTable table; 55 56 61 public ChooseColumnsActionListener(JTable t) { 62 this.table = t; 63 } 64 65 public void actionPerformed(ActionEvent e) { 66 TableModel tm = table.getModel(); 67 int n = tm.getColumnCount(); 68 String [] names = new String [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 tcm = table.getColumnModel(); 75 for (int i = 0; i < tcm.getColumnCount(); i++) { 76 checked[tcm.getColumn(i).getModelIndex()] = true; 77 } 78 79 ChooseColumnsPanel ccp = new ChooseColumnsPanel(checked, names); 81 DialogDescriptor dd = new DialogDescriptor( 82 ccp, NbBundle.getMessage(ChooseColumnsPanel.class, 83 "ChangeVisibleColumns")); Object res = DialogDisplayer.getDefault().notify(dd); 85 86 if (res == NotifyDescriptor.OK_OPTION) { 87 ArrayList <TableColumn > old = new ArrayList <TableColumn >(); 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 ) old.get(i)); 95 tcm.moveColumn(tcm.getColumnCount() - 1, i); 96 } 97 98 for (int i = 0; i < tcm.getColumnCount(); ) { 99 TableColumn 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 118 private static int findColumn(TableColumnModel 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 132 public static void installChooseColumnsButton(JScrollPane sp) { 133 JTable table = (JTable ) sp.getViewport().getView(); 134 JButton b = new JButton ( 135 new ImageIcon (ChooseColumnsPanel.class.getResource("columns.gif"))); b.addActionListener(new ChooseColumnsActionListener(table)); 137 sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 138 sp.setCorner(JScrollPane.UPPER_RIGHT_CORNER, b); 139 } 140 141 147 public ChooseColumnsPanel(boolean[] visibility, String [] names) { 148 initComponents(); 149 columnsCheckList.setModel(new DefaultCheckListModel(visibility, names)); 150 } 151 152 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 171 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 173 174 jLabel1 = new javax.swing.JLabel (); 175 jScrollPane1 = new javax.swing.JScrollPane (); 176 columnsCheckList = new org.netbeans.modules.tasklist.core.checklist.CheckList(); 177 178 setLayout(new java.awt.GridBagLayout ()); 179 180 setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (12, 12, 11, 11))); 181 setPreferredSize(new java.awt.Dimension (400, 300)); 182 jLabel1.setLabelFor(columnsCheckList); 183 jLabel1.setText(org.openide.util.NbBundle.getMessage(ChooseColumnsPanel.class, "ChooseTheColumnsToDisplay")); 184 gridBagConstraints = new java.awt.GridBagConstraints (); 185 gridBagConstraints.gridx = 0; 186 gridBagConstraints.gridy = 0; 187 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 188 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 0); 189 add(jLabel1, gridBagConstraints); 190 191 jScrollPane1.setViewportView(columnsCheckList); 192 193 gridBagConstraints = new java.awt.GridBagConstraints (); 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 } 203 204 private org.netbeans.modules.tasklist.core.checklist.CheckList columnsCheckList; 206 private javax.swing.JLabel jLabel1; 207 private javax.swing.JScrollPane jScrollPane1; 208 210 } 211 | Popular Tags |