1 19 20 package org.netbeans.modules.j2ee.ddloaders.multiview; 21 22 import org.netbeans.modules.xml.multiview.ui.DefaultTablePanel; 23 import org.netbeans.modules.xml.multiview.ui.SectionNodeInnerPanel; 24 import org.netbeans.modules.xml.multiview.ui.SectionNodeView; 25 26 import javax.swing.*; 27 import javax.swing.event.ListSelectionEvent ; 28 import javax.swing.event.ListSelectionListener ; 29 import javax.swing.event.TableModelEvent ; 30 import javax.swing.event.TableModelListener ; 31 import javax.swing.table.AbstractTableModel ; 32 import javax.swing.table.TableCellEditor ; 33 import javax.swing.table.TableColumn ; 34 import javax.swing.table.TableColumnModel ; 35 import java.awt.*; 36 import java.awt.event.ActionEvent ; 37 import java.awt.event.ActionListener ; 38 import java.awt.event.MouseAdapter ; 39 import java.awt.event.MouseEvent ; 40 41 44 public class InnerTablePanel extends SectionNodeInnerPanel { 45 46 private final TablePanel tablePanel; 47 private JTable table; 48 49 protected void setButtonListeners(final InnerTableModel model) { 50 final JTable table = getTable(); 51 getAddButton().addActionListener(new ActionListener () { 52 public void actionPerformed(ActionEvent e) { 53 stopCellEditing(table); 54 selectCell(model.addRow(), 0); 55 model.modelUpdatedFromUI(); 56 Utils.scrollToVisible(tablePanel); 57 } 58 }); 59 getEditButton().addActionListener(new ActionListener () { 60 public void actionPerformed(ActionEvent e) { 61 editCell(table.getSelectedRow(), table.getSelectedColumn()); 62 } 63 }); 64 getRemoveButton().addActionListener(new ActionListener () { 65 public void actionPerformed(ActionEvent e) { 66 int row = table.getSelectedRow(); 67 final int column = table.getSelectedColumn(); 68 final TableCellEditor cellEditor = table.getCellEditor(row, column); 69 if (cellEditor != null) { 70 cellEditor.cancelCellEditing(); 71 } 72 int rowCount = model.getRowCount() - 1; 73 model.removeRow(row); 74 model.modelUpdatedFromUI(); 75 if (row >= rowCount) { 76 row = rowCount - 1; 77 } 78 if (row >= 0) { 79 final int n = row; 80 Utils.runInAwtDispatchThread(new Runnable () { 81 public void run() { 82 selectCell(n, column); 83 } 84 }); 85 } 86 } 87 }); 88 } 89 90 private void stopCellEditing(final JTable table) { 91 table.editCellAt(-1, -1); } 93 94 private class TablePanel extends DefaultTablePanel { 95 96 101 public TablePanel(final AbstractTableModel model) { 102 super(model); 103 final JTable table = getTable(); 104 table.getSelectionModel().addListSelectionListener(new ListSelectionListener () { 105 public void valueChanged(ListSelectionEvent e) { 106 int selectedRowCount = table.getSelectedRowCount(); 107 removeButton.setEnabled(selectedRowCount > 0); 108 editButton.setEnabled(selectedRowCount == 1); 109 } 110 }); 111 } 112 113 public JButton getAddButton() { 114 return addButton; 115 } 116 117 public JButton getEditButton() { 118 return editButton; 119 } 120 121 public JButton getRemoveButton() { 122 return removeButton; 123 } 124 } 125 126 public InnerTablePanel(SectionNodeView sectionNodeView, final InnerTableModel model) { 127 super(sectionNodeView); 128 tablePanel = new TablePanel(model); 129 model.addTableModelListener(new TableModelListener () { 130 public void tableChanged(TableModelEvent e) { 131 int type = e.getType(); 132 if (type == TableModelEvent.INSERT || type == TableModelEvent.DELETE) { 133 scheduleRefreshView(); 134 } 135 } 136 }); 137 table = tablePanel.getTable(); 138 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 139 table.setRowSelectionAllowed(true); 140 table.addMouseListener(new MouseAdapter () { 141 144 public void mouseClicked(MouseEvent e) { 145 if (e.getClickCount() == 2) { 146 editCell(table.getSelectedRow(), table.getSelectedColumn()); 147 } 148 } 149 }); 150 InputMap inputMap = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 151 inputMap.put(KeyStroke.getKeyStroke("ENTER"), "selectNextColumnCell"); inputMap.put(KeyStroke.getKeyStroke("shift ENTER"), "selectPreviousColumnCell"); setLayout(new BorderLayout()); 154 add(tablePanel, BorderLayout.WEST); 155 setColumnWidths(); 156 if (model instanceof InnerTableModel) { 157 InnerTableModel innerTableModel = (InnerTableModel) model; 158 setButtonListeners(innerTableModel); 159 setColumnEditors(innerTableModel); 160 } 161 scheduleRefreshView(); 162 final TableColumnModel columnModel = table.getColumnModel(); 163 for (int i = 0, n = columnModel.getColumnCount(); i < n; i++) { 164 final TableCellEditor tableCellEditor = model.getTableCellEditor(i); 165 if (tableCellEditor != null) { 166 columnModel.getColumn(i).setCellEditor(tableCellEditor); 167 } 168 } 169 } 170 171 private void setColumnEditors(InnerTableModel model) { 172 TableColumnModel columnModel = table.getColumnModel(); 173 for (int i = 0; i < columnModel.getColumnCount(); i++) { 174 TableCellEditor cellEditor = model.getCellEditor(i); 175 if (cellEditor != null) { 176 columnModel.getColumn(i).setCellEditor(cellEditor); 177 } 178 } 179 } 180 181 public JTable getTable() { 182 return table; 183 } 184 185 public JButton getAddButton() { 186 return tablePanel.getAddButton(); 187 } 188 189 public JButton getEditButton() { 190 return tablePanel.getEditButton(); 191 } 192 193 public JButton getRemoveButton() { 194 return tablePanel.getRemoveButton(); 195 } 196 197 public void setColumnWidths() { 198 final JTable table = tablePanel.getTable(); 199 InnerTableModel tableModel = (InnerTableModel) table.getModel(); 200 TableColumnModel columnModel = table.getColumnModel(); 201 for (int i = 0, n = columnModel.getColumnCount(); i < n; i++) { 202 int width = tableModel.getDefaultColumnWidth(i); 203 final TableColumn column = columnModel.getColumn(i); 204 column.setPreferredWidth(width); 205 column.setWidth(width); 206 } 207 } 208 209 protected void editCell(final int row, final int column) { 210 if (table.isCellEditable(row, column)) { 211 selectCell(row, column); 212 Utils.runInAwtDispatchThread(new Runnable () { 213 public void run() { 214 final JTable table = getTable(); 215 table.editCellAt(row, column); 216 Component editorComponent = table.getEditorComponent(); 217 editorComponent.requestFocus(); 218 } 219 }); 220 } 221 } 222 223 private void selectCell(final int row, final int column) { 224 Utils.runInAwtDispatchThread(new Runnable () { 225 public void run() { 226 final JTable table = getTable(); 227 table.getSelectionModel().setLeadSelectionIndex(row); 228 table.getColumnModel().getSelectionModel().setLeadSelectionIndex(column); 229 table.requestFocus(); 230 } 231 }); 232 } 233 234 public JComponent getErrorComponent(String errorId) { 235 return null; 236 } 237 238 public void setValue(JComponent source, Object value) { 239 } 240 241 public void dataModelPropertyChange(Object source, String propertyName, Object oldValue, Object newValue) { 242 ((InnerTableModel) getTable().getModel()).tableChanged(); 243 scheduleRefreshView(); 244 } 245 246 public void linkButtonPressed(Object ddBean, String ddProperty) { 247 } 248 249 } 250 | Popular Tags |