KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > configbean > customizers > common > BeanTablePanel


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  * BeanTablePanel.java
21  *
22  * Created on October 4, 2003, 5:35 PM
23  */

24
25 package org.netbeans.modules.j2ee.sun.share.configbean.customizers.common;
26
27 import java.util.ResourceBundle JavaDoc;
28 import javax.accessibility.AccessibleContext JavaDoc;
29 import javax.swing.JTable JavaDoc;
30 import javax.swing.ListSelectionModel JavaDoc;
31 import javax.swing.JButton JavaDoc;
32 import javax.swing.event.ListSelectionListener JavaDoc;
33 import javax.swing.event.ListSelectionEvent JavaDoc;
34
35 import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean;
36
37
38 /** Generic panel containing the table and NEW - EDIT - DELETE buttons.
39  *
40  * @author Rajeshwar Patil
41  * @version %I%, %G%
42  */

43
44 public abstract class BeanTablePanel extends javax.swing.JPanel JavaDoc {
45     
46     protected JTable JavaDoc table;
47     protected JButton JavaDoc moveUpButton, moveDownButton, sourceButton;
48     private boolean reordable;
49     protected BeanTableModel model;
50     private boolean isSource;
51
52     static final ResourceBundle JavaDoc bundle =
53         ResourceBundle.getBundle(
54             "org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.Bundle"); //NOI18N
55

56     /** Creates a new BeanTablePanel.
57     * @param model AbstractTableModel for included table
58     */

59     public BeanTablePanel(BeanTableModel model) {
60         this(model, false, false);
61     }
62
63     /** Creates a new BeanTablePanel.
64     * @param model AbstractTableModel for included table
65     * @param reordable specifies whether the order of the rows is
66     * important(in DD filter-mappings for example the order of elements
67     * is important)
68     */

69     public BeanTablePanel(BeanTableModel model, boolean reordable) {
70         this(model, reordable, false);
71     }
72
73     /** Creates a new BeanTablePanel.
74     * @param model AbstractTableModel for included table
75     * @param reordable specifies whether the order of the rows is important
76     * (in DD filter-mappings for example the order of elements is important)
77     * @param isSource specifies if there is a reasonable source file/link
78     * related to the table row
79     */

80     public BeanTablePanel(BeanTableModel model, final boolean reordable,
81             final boolean isSource) {
82         this.model=model;
83         this.reordable=reordable;
84         this.isSource=isSource;
85
86         initComponents();
87
88         table = new FixedHeightJTable();
89         
90         table.getAccessibleContext().setAccessibleName(getAccessibleName());
91         table.getAccessibleContext().setAccessibleDescription(getAccessibleDesc());
92         getAccessibleContext().setAccessibleName(getAccessibleName());
93         getAccessibleContext().setAccessibleDescription(getAccessibleDesc());
94         
95         //table = new SortableTable(new SortableTableModel(model));
96
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
97         table.setModel(model);
98         table.getSelectionModel().addListSelectionListener(
99                     new ListSelectionListener JavaDoc(){
100             public void valueChanged(ListSelectionEvent JavaDoc e){
101                 // ignore extra messages
102
if (e.getValueIsAdjusting()){
103                     return;
104                 }
105
106                 if(((ListSelectionModel JavaDoc)e.getSource()).isSelectionEmpty() ||
107                     table.getModel().getRowCount() == 0 // BUG 5030679
108
) {
109                     editButton.setEnabled(false);
110                     removeButton.setEnabled(false);
111                     if (reordable) {
112                         moveUpButton.setEnabled(false);
113                         moveDownButton.setEnabled(false);
114                     }
115                     if (isSource) {
116                         sourceButton.setEnabled(false);
117                     }
118                 } else {
119                     editButton.setEnabled(true);
120                     removeButton.setEnabled(true);
121                     if (reordable) {
122                         int row = table.getSelectedRow();
123                         if (row<table.getModel().getRowCount()-1){
124                             moveDownButton.setEnabled(true);
125                         }else{
126                             moveDownButton.setEnabled(false);
127                         }
128                         if (row>0){
129                             moveUpButton.setEnabled(true);
130                         }else{
131                             moveUpButton.setEnabled(false);
132                         }
133                     }
134                     if (isSource) {
135                         sourceButton.setEnabled(true);
136                     }
137                 }
138             }
139         });
140
141         jScrollPane1.setViewportView(table);
142         if (reordable) {
143             moveUpButton = new JButton JavaDoc(bundle.getString("LBL_Move_Up")); //NOI18N
144
moveDownButton = new JButton JavaDoc(bundle.getString("LBL_Move_Down")); //NOI18N
145
moveUpButton.setEnabled(false);
146             moveDownButton.setEnabled(false);
147             buttonPanel.add(moveUpButton);
148             buttonPanel.add(moveDownButton);
149         }
150         if (isSource) {
151             sourceButton = new JButton JavaDoc(bundle.getString("LBL_Go_To_Source")); //NOI18N
152
sourceButton.setEnabled(false);
153             rightPanel.add(sourceButton);
154         }
155
156         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
157             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
158                 int row = table.getSelectedRow();
159                 //ddView.setModelChanged(true);
160
if(row >= 0) {
161                     getModel().removeRow(row);
162
163                     if(row >= getModel().getRowCount()) {
164                         --row;
165                     }
166
167                     if(row >= 0) {
168                         table.getSelectionModel().setSelectionInterval(row, row);
169                     }
170                 }
171             }
172         });
173
174         editButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
175             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
176                 displayDialog(true);
177             }
178         });
179
180         addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
181             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
182                 displayDialog(false);
183             }
184         });
185     }
186
187
188     public void setModel(Object JavaDoc parent, CommonDDBean[] children) {
189         model.setData(parent, children);
190     }
191
192
193     protected void displayDialog(boolean isEditOperation){
194         int row = -1;
195         BeanInputDialog dialog = null;
196
197         if(isEditOperation) {
198             row = table.getSelectedRow();
199             if(row < 0) {
200                 return;
201             }
202
203             Object JavaDoc[] values = model.getValues(row);
204             dialog = getInputDialog(values);
205         } else {
206             dialog = getInputDialog();
207         }
208
209         do {
210             int result = dialog.display();
211             if(result == dialog.CANCEL_OPTION) {
212                 break;
213             }
214
215             if(result == dialog.OK_OPTION) {
216                 if (dialog.hasErrors()) {
217                     dialog.showErrors();
218                 } else {
219                     ///ddView.setModelChanged(true);
220
if(isEditOperation){
221                         model.editRow(row, dialog.getValues());
222                     } else {
223                         model.addRow(dialog.getValues());
224                     }
225                 }
226             }
227         } while (dialog.hasErrors());
228     }
229
230
231     private void initComponents() {
232         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
233         southPanel = new javax.swing.JPanel JavaDoc();
234         buttonPanel = new javax.swing.JPanel JavaDoc();
235         addButton = new javax.swing.JButton JavaDoc();
236         editButton = new javax.swing.JButton JavaDoc();
237         removeButton = new javax.swing.JButton JavaDoc();
238         rightPanel = new javax.swing.JPanel JavaDoc();
239
240         setLayout(new java.awt.BorderLayout JavaDoc());
241         add(jScrollPane1, java.awt.BorderLayout.CENTER);
242         southPanel.setLayout(new java.awt.BorderLayout JavaDoc());
243
244         String JavaDoc buttonText = bundle.getString("LBL_New");
245         int mnemonicIndex = buttonText.indexOf(bundle.getString("MNC_New").charAt(0));
246         if(mnemonicIndex < 0) {
247             mnemonicIndex = 0;
248         }
249         addButton.setText(buttonText); //NOI18N
250
addButton.setDisplayedMnemonicIndex(mnemonicIndex);
251         AccessibleContext JavaDoc accessibleContext = addButton.getAccessibleContext();
252         accessibleContext.setAccessibleName(bundle.getString("ACSN_New")); // NOI18N
253
accessibleContext.setAccessibleDescription(bundle.getString("ACSD_New")); // NOI18N
254
buttonPanel.add(addButton);
255
256         buttonText = bundle.getString("LBL_Edit");
257         mnemonicIndex = buttonText.indexOf(bundle.getString("MNC_Edit").charAt(0));
258         if(mnemonicIndex < 0) {
259             mnemonicIndex = 0;
260         }
261         editButton.setText(buttonText); //NOI18N
262
editButton.setDisplayedMnemonicIndex(mnemonicIndex);
263         editButton.setEnabled(false);
264         accessibleContext = editButton.getAccessibleContext();
265         accessibleContext.setAccessibleName(bundle.getString("ACSN_Edit")); // NOI18N
266
accessibleContext.setAccessibleDescription(bundle.getString("ACSD_Edit")); // NOI18N
267
buttonPanel.add(editButton);
268
269         buttonText = bundle.getString("LBL_Delete");
270         mnemonicIndex = buttonText.indexOf(bundle.getString("MNC_Delete").charAt(0));
271         if(mnemonicIndex < 0) {
272             mnemonicIndex = 0;
273         }
274         removeButton.setText(buttonText); //NOI18N
275
removeButton.setDisplayedMnemonicIndex(mnemonicIndex);
276         removeButton.setEnabled(false);
277         accessibleContext = removeButton.getAccessibleContext();
278         accessibleContext.setAccessibleName(bundle.getString("ACSN_Delete")); // NOI18N
279
accessibleContext.setAccessibleDescription(bundle.getString("ACSD_Delete")); // NOI18N
280
buttonPanel.add(removeButton);
281
282         southPanel.add(buttonPanel, java.awt.BorderLayout.CENTER);
283         southPanel.add(rightPanel, java.awt.BorderLayout.EAST);
284         add(southPanel, java.awt.BorderLayout.SOUTH);
285     }
286
287     private javax.swing.JPanel JavaDoc southPanel;
288     protected javax.swing.JButton JavaDoc addButton;
289     private javax.swing.JPanel JavaDoc buttonPanel;
290     private javax.swing.JScrollPane JavaDoc jScrollPane1;
291     protected javax.swing.JButton JavaDoc editButton;
292     protected javax.swing.JButton JavaDoc removeButton;
293     private javax.swing.JPanel JavaDoc rightPanel;
294
295     abstract public BeanInputDialog getInputDialog(Object JavaDoc[] values);
296     abstract public BeanInputDialog getInputDialog();
297
298         protected String JavaDoc getAccessibleName(){
299             //default value; used in case derived class does not provide one
300
return bundle.getString("ACSN_Table"); //NOI18N
301
}
302
303         protected String JavaDoc getAccessibleDesc(){
304             //default value; used in case derived class does not provide one
305
return bundle.getString("ACSD_Table"); //NOI18N
306
}
307
308
309     public BeanTableModel getModel() {
310         return model;
311     }
312
313
314     public void setTitle(String JavaDoc title) {
315         javax.swing.JLabel JavaDoc label = new javax.swing.JLabel JavaDoc(title);
316         label.setBorder(new javax.swing.border.EmptyBorder JavaDoc(5,5,5,0));
317         add(label, java.awt.BorderLayout.NORTH);
318     }
319
320
321     public void setSelectedRow(int row) {
322         table.setRowSelectionInterval(row,row);
323     }
324
325
326     public void setButtons(boolean b1, boolean b2, boolean b3) {
327         addButton.setEnabled(b1);
328         editButton.setEnabled(b2);
329         removeButton.setEnabled(b3);
330     }
331
332
333     public void setButtons(boolean b1, boolean b2, boolean b3, boolean b4,
334             boolean b5, boolean b6) {
335         this.setButtons(b1,b2,b3);
336         moveUpButton.setEnabled(b4);
337         moveDownButton.setEnabled(b5);
338     }
339
340
341     public boolean isReordable() {
342         return reordable;
343     }
344
345
346     public boolean isSource() {
347         return isSource;
348     }
349 }
350
Popular Tags