KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > multiview > InnerTablePanel


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.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 JavaDoc;
28 import javax.swing.event.ListSelectionListener JavaDoc;
29 import javax.swing.event.TableModelEvent JavaDoc;
30 import javax.swing.event.TableModelListener JavaDoc;
31 import javax.swing.table.AbstractTableModel JavaDoc;
32 import javax.swing.table.TableCellEditor JavaDoc;
33 import javax.swing.table.TableColumn JavaDoc;
34 import javax.swing.table.TableColumnModel JavaDoc;
35 import java.awt.*;
36 import java.awt.event.ActionEvent JavaDoc;
37 import java.awt.event.ActionListener JavaDoc;
38 import java.awt.event.MouseAdapter JavaDoc;
39 import java.awt.event.MouseEvent JavaDoc;
40
41 /**
42  * @author pfiala
43  */

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 JavaDoc() {
52             public void actionPerformed(ActionEvent JavaDoc e) {
53                 stopCellEditing(table);
54                 selectCell(model.addRow(), 0);
55                 model.modelUpdatedFromUI();
56                 Utils.scrollToVisible(tablePanel);
57             }
58         });
59         getEditButton().addActionListener(new ActionListener JavaDoc() {
60             public void actionPerformed(ActionEvent JavaDoc e) {
61                 editCell(table.getSelectedRow(), table.getSelectedColumn());
62             }
63         });
64         getRemoveButton().addActionListener(new ActionListener JavaDoc() {
65             public void actionPerformed(ActionEvent JavaDoc e) {
66                 int row = table.getSelectedRow();
67                 final int column = table.getSelectedColumn();
68                 final TableCellEditor JavaDoc 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 JavaDoc() {
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); // finish possible editing
92
}
93
94     private class TablePanel extends DefaultTablePanel {
95
96         /**
97          * Creates a new InnerTablePanel.
98          *
99          * @param model DefaultTableModel for included table
100          */

101         public TablePanel(final AbstractTableModel JavaDoc model) {
102             super(model);
103             final JTable table = getTable();
104             table.getSelectionModel().addListSelectionListener(new ListSelectionListener JavaDoc() {
105                 public void valueChanged(ListSelectionEvent JavaDoc 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 JavaDoc() {
130             public void tableChanged(TableModelEvent JavaDoc 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 JavaDoc() {
141             /**
142              * Invoked when the mouse has been clicked on a component.
143              */

144             public void mouseClicked(MouseEvent JavaDoc 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"); //NOI18N
152
inputMap.put(KeyStroke.getKeyStroke("shift ENTER"), "selectPreviousColumnCell"); //NOI18N
153
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 JavaDoc columnModel = table.getColumnModel();
163         for (int i = 0, n = columnModel.getColumnCount(); i < n; i++) {
164             final TableCellEditor JavaDoc 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 JavaDoc columnModel = table.getColumnModel();
173         for (int i = 0; i < columnModel.getColumnCount(); i++) {
174             TableCellEditor JavaDoc 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 JavaDoc columnModel = table.getColumnModel();
201         for (int i = 0, n = columnModel.getColumnCount(); i < n; i++) {
202             int width = tableModel.getDefaultColumnWidth(i);
203             final TableColumn JavaDoc 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 JavaDoc() {
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 JavaDoc() {
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 JavaDoc errorId) {
235         return null;
236     }
237
238     public void setValue(JComponent source, Object JavaDoc value) {
239     }
240
241     public void dataModelPropertyChange(Object JavaDoc source, String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
242         ((InnerTableModel) getTable().getModel()).tableChanged();
243         scheduleRefreshView();
244     }
245
246     public void linkButtonPressed(Object JavaDoc ddBean, String JavaDoc ddProperty) {
247     }
248
249 }
250
Popular Tags