KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > autoupdate > ServerPanel


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.autoupdate;
21
22 import java.awt.Font JavaDoc;
23 import java.awt.FontMetrics JavaDoc;
24 import java.awt.Graphics JavaDoc;
25 import javax.swing.table.AbstractTableModel JavaDoc;
26 import javax.swing.JTable JavaDoc;
27
28 import java.util.List JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.beans.PropertyChangeListener JavaDoc;
33 import java.beans.PropertyChangeEvent JavaDoc;
34 import javax.swing.table.TableModel JavaDoc;
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 JavaDoc {
41
42     static final long serialVersionUID =-3335861235683235775L;
43     
44     /** Creates new ServerPanel */
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 JavaDoc {
61             private boolean firstPaint = true;
62             private int hm = 1;
63             public AutoResizeTable (TableModel JavaDoc model) {
64                 super (model);
65             }
66             public AutoResizeTable (TableModel JavaDoc model, int heightMultiple) {
67                 this(model);
68                 this.hm = heightMultiple;
69             }
70             public void paint (Graphics JavaDoc g) {
71                 if (firstPaint) {
72                     adjustRowHeight(g);
73                     firstPaint = false;
74                     //it will trigger another paint anyway
75
return;
76                 }
77                 super.paint(g);
78             }
79         private void adjustRowHeight(Graphics JavaDoc g) {
80             Font JavaDoc f = getFont();
81             FontMetrics JavaDoc fm = g.getFontMetrics(f);
82             int rowHeight = hm * fm.getHeight();
83             // #55754: Cell height too small for checkbox
84
setRowHeight (hm == 1 ? rowHeight + 4: rowHeight);
85         }
86     }
87             
88     /** This method is called from within the constructor to
89      * initialize the form.
90      * WARNING: Do NOT modify this code. The content of this method is
91      * always regenerated by the FormEditor.
92      */

93     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
94
private void initComponents() {
95         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
96
97         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
98         jLabel1 = new javax.swing.JLabel JavaDoc();
99
100         setLayout(new java.awt.GridBagLayout JavaDoc());
101
102         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc();
113         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
114         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
115         add(jLabel1, gridBagConstraints);
116
117     }// </editor-fold>//GEN-END:initComponents
118

119     // Variables declaration - do not modify//GEN-BEGIN:variables
120
private javax.swing.JLabel JavaDoc jLabel1;
121     private javax.swing.JScrollPane JavaDoc jScrollPane1;
122     // End of variables declaration//GEN-END:variables
123
private javax.swing.ButtonGroup JavaDoc buttonGroup;
124     private JTable JavaDoc table;
125
126     public void setEnabled(boolean enabled) {
127         super.setEnabled(enabled);
128         table.setEnabled(enabled);
129     }
130     
131     private static String JavaDoc getBundle( String JavaDoc key ) {
132         return NbBundle.getMessage( ServerPanel.class, key );
133     }
134     
135     class ServerTableModel extends AbstractTableModel JavaDoc {
136         final String JavaDoc[] columnNames = {" ",
137                                       getBundle("LAB_TableHeader")};
138         private List JavaDoc atypes = new ArrayList JavaDoc();
139
140         private PropertyChangeListener JavaDoc listener = new PropertyChangeListener JavaDoc() {
141                           public void propertyChange(PropertyChangeEvent JavaDoc 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 JavaDoc getColumnName(int col) {
157             return columnNames[col];
158         }
159
160         public Object JavaDoc 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 JavaDoc 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 JavaDoc value, int row, int col) {
180             if (value instanceof Boolean JavaDoc)
181                 ((AutoupdateType)atypes.get(row)).setEnabled(((Boolean JavaDoc)value).booleanValue());
182         }
183         
184         void refreshContent() {
185             HashMap JavaDoc allUpdates = new HashMap JavaDoc();
186             Enumeration JavaDoc 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