KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > wizards > MappingTable


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 /**
21  * MappingTable.java
22  *
23  * @author Ana von Klopp
24  */

25 package org.netbeans.modules.web.wizards;
26
27 import java.awt.Color JavaDoc;
28 import java.awt.Dimension JavaDoc;
29 import java.awt.Font JavaDoc;
30 import java.awt.FontMetrics JavaDoc;
31 import java.awt.Graphics JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 import javax.swing.BorderFactory JavaDoc;
36 import javax.swing.JTable JavaDoc;
37 import javax.swing.ListSelectionModel JavaDoc;
38 import javax.swing.table.TableColumnModel JavaDoc;
39 import javax.swing.table.TableModel JavaDoc;
40 import javax.swing.table.AbstractTableModel JavaDoc;
41 import javax.swing.event.TableModelListener JavaDoc;
42
43 import org.openide.util.NbBundle;
44
45 class MappingTable extends JTable JavaDoc {
46
47     private static final boolean debug = false;
48
49     // Handle resizing for larger fonts
50
private boolean fontChanged = true;
51     private int margin = 6;
52
53     private static final long serialVersionUID = 3482048644419079279L;
54     
55     MappingTable(String JavaDoc filterName, ArrayList JavaDoc filterMappings) {
56
57     super();
58     if(debug) log("::Constructor"); //NOI18N
59
if(debug) log("\tFilterName is " + filterName); //NOI18N
60
this.setModel(new MappingTableModel(filterName, filterMappings));
61
62     TableColumnModel JavaDoc tcm = this.getColumnModel();
63
64     // The filter name - this one is never editable
65
tcm.getColumn(0).setPreferredWidth(72);
66
67     // The pattern or servlet that we match to
68
// This editor depends on whether the value of the other is
69
// URL or Servlet
70
tcm.getColumn(1).setPreferredWidth(72);
71     this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
72     setColors(false);
73     setIntercellSpacing(new Dimension JavaDoc(margin, margin));
74     }
75
76     ArrayList JavaDoc getFilterMappings() {
77     return ((MappingTableModel)this.getModel()).getFilterMappings();
78     }
79
80     void setFilterName(String JavaDoc name) {
81     ((MappingTableModel)this.getModel()).setFilterName(name);
82     this.invalidate();
83     }
84
85     void addRow(FilterMappingData fmd) {
86     this.invalidate();
87     ((MappingTableModel)getModel()).addRow(fmd);
88     }
89
90     void addRow(int row, FilterMappingData fmd) {
91     this.invalidate();
92     ((MappingTableModel)getModel()).addRow(row, fmd);
93     }
94
95     void setRow(int row, FilterMappingData fmd) {
96     this.invalidate();
97     ((MappingTableModel)getModel()).setRow(row, fmd);
98     }
99
100     FilterMappingData getRow(int row) {
101     return ((MappingTableModel)getModel()).getRow(row);
102     }
103
104     void moveRowUp(int row) {
105     ((MappingTableModel)getModel()).moveRowUp(row);
106     getSelectionModel().setSelectionInterval(row-1, row-1);
107     this.invalidate();
108     }
109     void moveRowDown(int row) {
110     ((MappingTableModel)getModel()).moveRowUp(row+1);
111     getSelectionModel().setSelectionInterval(row+1, row+1);
112     this.invalidate();
113     }
114
115     void removeRow(int row) {
116     ((MappingTableModel)getModel()).removeRow(row);
117     this.invalidate();
118     return;
119     }
120
121     public void setValueAt(Object JavaDoc o, int row, int col) {
122     if(debug) log("::setValueAt()"); //NOI18N
123
return;
124     }
125
126     private void setColors(boolean editable) {
127     Color JavaDoc bg;
128     this.setBorder(BorderFactory.createLoweredBevelBorder());
129     if (!editable) {
130         bg = this.getBackground().darker();
131     } else {
132         bg = Color.white;
133     }
134     this.setBackground(bg);
135     }
136
137     void addTableModelListener(TableModelListener JavaDoc tml) {
138     TableModel JavaDoc tableModel = getModel();
139     if (tableModel != null) {
140         tableModel.addTableModelListener(tml);
141     }
142     }
143
144     void removeTableModelListener(TableModelListener JavaDoc tml) {
145     TableModel JavaDoc tableModel = getModel();
146     if (tableModel != null) {
147         tableModel.removeTableModelListener(tml);
148     }
149     }
150     
151     public void setFont(Font JavaDoc f) {
152     if(debug) log("::setFont()"); //NOI18N
153
fontChanged = true;
154     super.setFont(f);
155     }
156
157     /**
158      * When paint is first invoked, we set the rowheight based on the
159      * size of the font. */

160     public void paint(Graphics JavaDoc g) {
161     if(debug) log("::paint()"); //NOI18N
162

163     if (fontChanged) {
164         
165         fontChanged = false;
166
167         int height = 0;
168         if(debug) log("\tGetting font height"); //NOI18N
169
FontMetrics JavaDoc fm = g.getFontMetrics(getFont());
170         height = fm.getHeight() + margin;
171         if(height > rowHeight) rowHeight = height;
172         if(debug) log("\trow height is " + rowHeight); //NOI18N
173
//triggers paint, just return afterwards
174
this.setRowHeight(rowHeight);
175         return;
176     }
177     super.paint(g);
178     }
179
180     private void log(String JavaDoc s) {
181     System.out.println("MappingTable" + s); //NOI18N
182
}
183
184     class MappingTableModel extends AbstractTableModel JavaDoc {
185
186     private final String JavaDoc[] colheaders = {
187         NbBundle.getMessage(MappingTable.class, "LBL_filter_name"),
188         NbBundle.getMessage(MappingTable.class, "LBL_applies_to"),
189     };
190
191     private ArrayList JavaDoc filterMappings = null;
192     private String JavaDoc filterName;
193     
194         private static final long serialVersionUID = 2845252365404044474L;
195         
196     MappingTableModel(String JavaDoc filterName, ArrayList JavaDoc filterMappings) {
197         this.filterName = filterName;
198         this.filterMappings = filterMappings;
199     }
200
201     ArrayList JavaDoc getFilterMappings() {
202         return filterMappings;
203     }
204
205     void setFilterName(String JavaDoc name) {
206         Iterator JavaDoc i = filterMappings.iterator();
207         FilterMappingData fmd;
208         while(i.hasNext()) {
209         fmd = (FilterMappingData)(i.next());
210         if(fmd.getName().equals(filterName))
211             fmd.setName(name);
212         }
213         this.filterName = name;
214     }
215
216     public int getColumnCount() {
217         return colheaders.length;
218     }
219
220     public int getRowCount() {
221         return filterMappings.size();
222     }
223
224     public String JavaDoc getColumnName(int col) {
225         return colheaders[col];
226     }
227
228     public Object JavaDoc getValueAt(int row, int col) {
229         FilterMappingData fmd = (FilterMappingData)(filterMappings.get(row));
230         if(col == 0) return fmd.getName();
231         else return fmd.getPattern();
232     }
233
234     public Class JavaDoc getColumnClass(int c) {
235         return String JavaDoc.class;
236     }
237
238     public boolean isCellEditable(int row, int col) {
239         return false;
240     }
241     
242     public void setValueAt(Object JavaDoc value, int row, int col) {
243         if(debug) log("::setValueAt()"); //NOI18N
244
return;
245     }
246
247     void addRow(int row, FilterMappingData fmd) {
248         filterMappings.add(row, fmd);
249     }
250
251     void addRow(FilterMappingData fmd) {
252         filterMappings.add(fmd);
253     }
254
255     FilterMappingData getRow(int row) {
256         return (FilterMappingData)(filterMappings.get(row));
257     }
258     
259     void setRow(int row, FilterMappingData fmd) {
260         filterMappings.set(row, fmd);
261     }
262
263     void moveRowUp(int row) {
264         Object JavaDoc o = filterMappings.remove(row);
265         filterMappings.add(row-1, o);
266     }
267
268     void removeRow(int row) {
269         filterMappings.remove(row);
270     }
271
272     private void log(String JavaDoc s) {
273         System.out.println("MappingTableModel" + s); //NOI18N
274
}
275     } // MappingTableModel
276

277 } // MappingTable
278
Popular Tags