1 19 20 package org.netbeans.modules.xml.multiview.ui; 21 22 import java.awt.Font ; 24 import javax.swing.table.AbstractTableModel ; 25 import javax.swing.ListSelectionModel ; 26 import javax.swing.JButton ; 27 import javax.swing.event.ListSelectionListener ; 28 import javax.swing.event.ListSelectionEvent ; 29 30 import org.openide.util.NbBundle; 32 33 38 public class DefaultTablePanel extends javax.swing.JPanel { 39 protected JButton moveUpButton, moveDownButton, sourceButton; 40 private boolean reordable; 41 private AbstractTableModel model; 42 43 46 public DefaultTablePanel(AbstractTableModel model) { 47 this(model, false); 48 } 49 50 55 public DefaultTablePanel(AbstractTableModel model, final boolean reordable) { 56 this.model=model; 57 this.reordable=reordable; 58 initComponents(); 59 jTable1.setModel(model); 60 61 62 java.awt.Component cellSample 63 = jTable1.getDefaultRenderer(String .class) 64 .getTableCellRendererComponent( 65 jTable1, "N/A", false, false, 0, 0); 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 header = jTable1.getTableHeader(); 76 add(header, java.awt.BorderLayout.NORTH); 77 78 jTable1.getSelectionModel().addListSelectionListener 79 ( 80 new ListSelectionListener () 81 { 82 public void valueChanged(ListSelectionEvent e) 83 { 84 if (e.getValueIsAdjusting()) 86 { 87 return; 88 } 89 90 if (((ListSelectionModel )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 (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 (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 131 private void initComponents() { buttonPanel = new javax.swing.JPanel (); 133 addButton = new javax.swing.JButton (); 134 editButton = new javax.swing.JButton (); 135 removeButton = new javax.swing.JButton (); 136 jPanel1 = new javax.swing.JPanel (); 137 jTable1 = new javax.swing.JTable (); 138 139 setLayout(new java.awt.BorderLayout ()); 140 141 setOpaque(false); 142 buttonPanel.setLayout(new java.awt.FlowLayout (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 (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 (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 (0, 14, 0, 14)); 160 buttonPanel.add(removeButton); 161 162 add(buttonPanel, java.awt.BorderLayout.SOUTH); 163 164 jPanel1.setLayout(new java.awt.BorderLayout ()); 165 166 jTable1.setBorder(new javax.swing.border.LineBorder (new java.awt.Color (0, 0, 0))); 167 jPanel1.add(jTable1, java.awt.BorderLayout.CENTER); 168 169 add(jPanel1, java.awt.BorderLayout.CENTER); 170 171 } 173 174 protected javax.swing.JButton addButton; 176 private javax.swing.JPanel buttonPanel; 177 protected javax.swing.JButton editButton; 178 private javax.swing.JPanel jPanel1; 179 private javax.swing.JTable jTable1; 180 protected javax.swing.JButton removeButton; 181 183 public AbstractTableModel 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 title) { 208 javax.swing.JLabel label = new javax.swing.JLabel (title); 209 label.setFont(new Font (getFont().getFontName(), Font.PLAIN, getFont().getSize() + 2)); 210 label.setBorder(new javax.swing.border.EmptyBorder (5,5,5,0)); 212 add(label, java.awt.BorderLayout.NORTH); 213 } 214 215 public javax.swing.JTable getTable() { 216 return jTable1; 217 } 218 } 219 | Popular Tags |