KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > multiview > ui > DefaultTablePanel


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.xml.multiview.ui;
21
22 // Swing
23
import java.awt.Font JavaDoc;
24 import javax.swing.table.AbstractTableModel JavaDoc;
25 import javax.swing.ListSelectionModel JavaDoc;
26 import javax.swing.JButton JavaDoc;
27 import javax.swing.event.ListSelectionListener JavaDoc;
28 import javax.swing.event.ListSelectionEvent JavaDoc;
29
30 // Netbeans
31
import org.openide.util.NbBundle;
32
33 /** Generic panel containing the table and NEW - EDIT - DELETE buttons.
34  *
35  * @author mk115033
36  * Created on October 1, 2002, 3:52 PM
37  */

38 public class DefaultTablePanel extends javax.swing.JPanel JavaDoc {
39     protected JButton JavaDoc moveUpButton, moveDownButton, sourceButton;
40     private boolean reordable;
41     private AbstractTableModel JavaDoc model;
42     
43     /** Creates a new TablePanel.
44     * @param model AbstractTableModel for included table
45     */

46     public DefaultTablePanel(AbstractTableModel JavaDoc model) {
47         this(model, false);
48     }
49
50     /** Creates a new TablePanel.
51     * @param model AbstractTableModel for included table
52     * @param reordable specifies whether the order of the rows is important(in DD filter-mappings for example the order of elements is important)
53     * @param isSource specifies if there is a reasonable source file/link related to the table row
54     */

55     public DefaultTablePanel(AbstractTableModel JavaDoc model, final boolean reordable) {
56         this.model=model;
57         this.reordable=reordable;
58         initComponents();
59         jTable1.setModel(model);
60
61         /* accomodate row height so that characters can fit: */
62         java.awt.Component JavaDoc cellSample
63                 = jTable1.getDefaultRenderer(String JavaDoc.class)
64                   .getTableCellRendererComponent(
65                           jTable1, //table
66
"N/A", //value //NOI18N
67
false, //isSelected
68
false, //hasFocus
69
0, 0); //row, column
70
int cellHeight = cellSample.getPreferredSize().height;
71         int rowHeight = cellHeight + jTable1.getRowMargin();
72         jTable1.setRowHeight(Math.max(16, rowHeight));
73
74         jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
75         javax.swing.table.JTableHeader JavaDoc header = jTable1.getTableHeader();
76         add(header, java.awt.BorderLayout.NORTH);
77         
78         jTable1.getSelectionModel().addListSelectionListener
79         (
80                 new ListSelectionListener JavaDoc()
81                 {
82                         public void valueChanged(ListSelectionEvent JavaDoc e)
83                         {
84                                 // ignore extra messages
85
if (e.getValueIsAdjusting())
86                                 {
87                                         return;
88                                 }
89
90                                 if (((ListSelectionModel JavaDoc)e.getSource()).isSelectionEmpty())
91                                 {
92                                         editButton.setEnabled(false);
93                                         removeButton.setEnabled(false);
94                                         if (reordable) {
95                                             moveUpButton.setEnabled(false);
96                                             moveDownButton.setEnabled(false);
97                                         }
98                                 }
99                                 else
100                                 {
101                                         editButton.setEnabled(true);
102                                         removeButton.setEnabled(true);
103                                         if (reordable) {
104                                             int row = jTable1.getSelectedRow();
105                                             if (row<jTable1.getModel().getRowCount()-1) moveDownButton.setEnabled(true);
106                                             else moveDownButton.setEnabled(false);
107                                             if (row>0) moveUpButton.setEnabled(true);
108                                             else moveUpButton.setEnabled(false);
109                                         }
110                                 }
111                         }
112                 }
113         );
114         if (reordable) {
115             moveUpButton = new JButton JavaDoc(NbBundle.getMessage(DefaultTablePanel.class,"LBL_Move_Up"));
116             moveUpButton.setMnemonic(org.openide.util.NbBundle.getMessage(DefaultTablePanel.class, "LBL_Move_Up_mnem").charAt(0));
117             moveDownButton = new JButton JavaDoc(NbBundle.getMessage(DefaultTablePanel.class,"LBL_Move_Down"));
118             moveDownButton.setMnemonic(org.openide.util.NbBundle.getMessage(DefaultTablePanel.class, "LBL_Move_Down_mnem").charAt(0));
119             moveUpButton.setEnabled(false);
120             moveDownButton.setEnabled(false);
121             buttonPanel.add(moveUpButton);
122             buttonPanel.add(moveDownButton);
123         }
124     }
125     
126     /** This method is called from within the constructor to
127      * initialize the form.
128      * WARNING: Do NOT modify this code. The content of this method is
129      * always regenerated by the Form Editor.
130      */

131     private void initComponents() {//GEN-BEGIN:initComponents
132
buttonPanel = new javax.swing.JPanel JavaDoc();
133         addButton = new javax.swing.JButton JavaDoc();
134         editButton = new javax.swing.JButton JavaDoc();
135         removeButton = new javax.swing.JButton JavaDoc();
136         jPanel1 = new javax.swing.JPanel JavaDoc();
137         jTable1 = new javax.swing.JTable JavaDoc();
138
139         setLayout(new java.awt.BorderLayout JavaDoc());
140
141         setOpaque(false);
142         buttonPanel.setLayout(new java.awt.FlowLayout JavaDoc(java.awt.FlowLayout.LEFT));
143
144         buttonPanel.setOpaque(false);
145         addButton.setMnemonic(org.openide.util.NbBundle.getMessage(DefaultTablePanel.class, "LBL_Add_mnem").charAt(0));
146         addButton.setText(org.openide.util.NbBundle.getBundle(DefaultTablePanel.class).getString("LBL_Add"));
147         addButton.setMargin(new java.awt.Insets JavaDoc(0, 14, 0, 14));
148         buttonPanel.add(addButton);
149
150         editButton.setMnemonic(org.openide.util.NbBundle.getMessage(DefaultTablePanel.class, "LBL_Edit_mnem").charAt(0));
151         editButton.setText(org.openide.util.NbBundle.getBundle(DefaultTablePanel.class).getString("LBL_Edit"));
152         editButton.setEnabled(false);
153         editButton.setMargin(new java.awt.Insets JavaDoc(0, 14, 0, 14));
154         buttonPanel.add(editButton);
155
156         removeButton.setMnemonic(org.openide.util.NbBundle.getMessage(DefaultTablePanel.class, "LBL_Remove_mnem").charAt(0));
157         removeButton.setText(org.openide.util.NbBundle.getBundle(DefaultTablePanel.class).getString("LBL_Remove"));
158         removeButton.setEnabled(false);
159         removeButton.setMargin(new java.awt.Insets JavaDoc(0, 14, 0, 14));
160         buttonPanel.add(removeButton);
161
162         add(buttonPanel, java.awt.BorderLayout.SOUTH);
163
164         jPanel1.setLayout(new java.awt.BorderLayout JavaDoc());
165
166         jTable1.setBorder(new javax.swing.border.LineBorder JavaDoc(new java.awt.Color JavaDoc(0, 0, 0)));
167         jPanel1.add(jTable1, java.awt.BorderLayout.CENTER);
168
169         add(jPanel1, java.awt.BorderLayout.CENTER);
170
171     }//GEN-END:initComponents
172

173     
174     // Variables declaration - do not modify//GEN-BEGIN:variables
175
protected javax.swing.JButton JavaDoc addButton;
176     private javax.swing.JPanel JavaDoc buttonPanel;
177     protected javax.swing.JButton JavaDoc editButton;
178     private javax.swing.JPanel JavaDoc jPanel1;
179     private javax.swing.JTable JavaDoc jTable1;
180     protected javax.swing.JButton JavaDoc removeButton;
181     // End of variables declaration//GEN-END:variables
182

183     public AbstractTableModel JavaDoc getModel() {
184         return model;
185     }
186     
187     public void setButtons(boolean b1, boolean b2, boolean b3) {
188         addButton.setEnabled(b1);
189         editButton.setEnabled(b2);
190         removeButton.setEnabled(b3);
191     }
192     
193     public void setButtons(boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6) {
194         this.setButtons(b1,b2,b3);
195         moveUpButton.setEnabled(b4);
196         moveDownButton.setEnabled(b5);
197     }
198     
199     public boolean isReordable() {
200         return reordable;
201     }
202     
203     public void setSelectedRow(int row) {
204         jTable1.setRowSelectionInterval(row,row);
205     }
206     
207     public void setTitle(String JavaDoc title) {
208         javax.swing.JLabel JavaDoc label = new javax.swing.JLabel JavaDoc(title);
209         label.setFont(new Font JavaDoc(getFont().getFontName(), Font.PLAIN, getFont().getSize() + 2));
210 // label.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
211
label.setBorder(new javax.swing.border.EmptyBorder JavaDoc(5,5,5,0));
212         add(label, java.awt.BorderLayout.NORTH);
213     }
214     
215     public javax.swing.JTable JavaDoc getTable() {
216         return jTable1;
217     }
218 }
219
Popular Tags