1 19 20 package org.netbeans.modules.autoupdate; 21 22 import java.awt.Font ; 23 import java.awt.FontMetrics ; 24 import java.awt.Graphics ; 25 import javax.swing.table.AbstractTableModel ; 26 import javax.swing.JTable ; 27 28 import java.util.List ; 29 import java.util.ArrayList ; 30 import java.util.HashMap ; 31 import java.util.Enumeration ; 32 import java.beans.PropertyChangeListener ; 33 import java.beans.PropertyChangeEvent ; 34 import javax.swing.table.TableModel ; 35 36 import org.openide.util.NbBundle; 37 import org.openide.util.Utilities; 38 import org.openide.util.WeakListeners; 39 40 class ServerPanel extends javax.swing.JPanel { 41 42 static final long serialVersionUID =-3335861235683235775L; 43 44 45 public ServerPanel() { 46 initComponents(); 47 48 ServerTableModel model = new ServerTableModel(); 49 table = new AutoResizeTable(model); 50 table.getAccessibleContext().setAccessibleDescription(getBundle("ACS_ServerPanelTable")); 51 jLabel1.setLabelFor(table); 52 table.getColumnModel().getColumn(0).setMaxWidth(36); 53 54 jScrollPane1.setViewportView(table); 55 model.refreshContent(); 56 57 getAccessibleContext().setAccessibleDescription(getBundle("ACS_ServerPanel")); 58 } 59 60 static class AutoResizeTable extends JTable { 61 private boolean firstPaint = true; 62 private int hm = 1; 63 public AutoResizeTable (TableModel model) { 64 super (model); 65 } 66 public AutoResizeTable (TableModel model, int heightMultiple) { 67 this(model); 68 this.hm = heightMultiple; 69 } 70 public void paint (Graphics g) { 71 if (firstPaint) { 72 adjustRowHeight(g); 73 firstPaint = false; 74 return; 76 } 77 super.paint(g); 78 } 79 private void adjustRowHeight(Graphics g) { 80 Font f = getFont(); 81 FontMetrics fm = g.getFontMetrics(f); 82 int rowHeight = hm * fm.getHeight(); 83 setRowHeight (hm == 1 ? rowHeight + 4: rowHeight); 85 } 86 } 87 88 93 private void initComponents() { 95 java.awt.GridBagConstraints gridBagConstraints; 96 97 jScrollPane1 = new javax.swing.JScrollPane (); 98 jLabel1 = new javax.swing.JLabel (); 99 100 setLayout(new java.awt.GridBagLayout ()); 101 102 gridBagConstraints = new java.awt.GridBagConstraints (); 103 gridBagConstraints.gridy = 1; 104 gridBagConstraints.gridheight = java.awt.GridBagConstraints.RELATIVE; 105 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 106 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 107 gridBagConstraints.weightx = 1.0; 108 gridBagConstraints.weighty = 1.0; 109 add(jScrollPane1, gridBagConstraints); 110 111 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, getBundle("LAB_AvailableServers")); 112 gridBagConstraints = new java.awt.GridBagConstraints (); 113 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 114 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 115 add(jLabel1, gridBagConstraints); 116 117 } 119 private javax.swing.JLabel jLabel1; 121 private javax.swing.JScrollPane jScrollPane1; 122 private javax.swing.ButtonGroup buttonGroup; 124 private JTable table; 125 126 public void setEnabled(boolean enabled) { 127 super.setEnabled(enabled); 128 table.setEnabled(enabled); 129 } 130 131 private static String getBundle( String key ) { 132 return NbBundle.getMessage( ServerPanel.class, key ); 133 } 134 135 class ServerTableModel extends AbstractTableModel { 136 final String [] columnNames = {" ", 137 getBundle("LAB_TableHeader")}; 138 private List atypes = new ArrayList (); 139 140 private PropertyChangeListener listener = new PropertyChangeListener () { 141 public void propertyChange(PropertyChangeEvent event) { 142 if (event.getPropertyName().equals(AutoupdateType.PROP_ENABLED)) { 143 table.repaint(); 144 } 145 } 146 }; 147 148 public int getColumnCount() { 149 return columnNames.length; 150 } 151 152 public int getRowCount() { 153 return atypes.size(); 154 } 155 156 public String getColumnName(int col) { 157 return columnNames[col]; 158 } 159 160 public Object getValueAt(int row, int col) { 161 if (col == 0) 162 return ((AutoupdateType)atypes.get(row)).isEnabled() ? Boolean.TRUE : Boolean.FALSE; 163 else 164 return ((AutoupdateType)atypes.get(row)).getName(); 165 } 166 167 public Class getColumnClass(int c) { 168 return getValueAt(0, c).getClass(); 169 } 170 171 public boolean isCellEditable(int row, int col) { 172 if (col > 0) { 173 return false; 174 } else { 175 return true; 176 } 177 } 178 179 public void setValueAt(Object value, int row, int col) { 180 if (value instanceof Boolean ) 181 ((AutoupdateType)atypes.get(row)).setEnabled(((Boolean )value).booleanValue()); 182 } 183 184 void refreshContent() { 185 HashMap allUpdates = new HashMap (); 186 Enumeration en = AutoupdateType.autoupdateTypes(); 187 atypes.clear(); 188 while (en.hasMoreElements()) { 189 AutoupdateType at = (AutoupdateType)en.nextElement(); 190 atypes.add(at); 191 at.addPropertyChangeListener( WeakListeners.propertyChange(listener,at) ); 192 } 193 } 194 } 195 } 196 | Popular Tags |