001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /**
043: * MappingTable.java
044: *
045: * @author Ana von Klopp
046: */package org.netbeans.modules.web.wizards;
047:
048: import java.awt.Color;
049: import java.awt.Dimension;
050: import java.awt.Font;
051: import java.awt.FontMetrics;
052: import java.awt.Graphics;
053: import java.util.ArrayList;
054: import java.util.Iterator;
055:
056: import javax.swing.BorderFactory;
057: import javax.swing.JTable;
058: import javax.swing.ListSelectionModel;
059: import javax.swing.table.TableColumnModel;
060: import javax.swing.table.TableModel;
061: import javax.swing.table.AbstractTableModel;
062: import javax.swing.event.TableModelListener;
063:
064: import org.openide.util.NbBundle;
065:
066: class MappingTable extends JTable {
067:
068: private static final boolean debug = false;
069:
070: // Handle resizing for larger fonts
071: private boolean fontChanged = true;
072: private int margin = 6;
073:
074: private static final long serialVersionUID = 3482048644419079279L;
075:
076: MappingTable(String filterName, ArrayList filterMappings) {
077:
078: super ();
079: if (debug)
080: log("::Constructor"); //NOI18N
081: if (debug)
082: log("\tFilterName is " + filterName); //NOI18N
083: this
084: .setModel(new MappingTableModel(filterName,
085: filterMappings));
086:
087: TableColumnModel tcm = this .getColumnModel();
088:
089: // The filter name - this one is never editable
090: tcm.getColumn(0).setPreferredWidth(72);
091:
092: // The pattern or servlet that we match to
093: // This editor depends on whether the value of the other is
094: // URL or Servlet
095: tcm.getColumn(1).setPreferredWidth(72);
096: this .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
097: setColors(false);
098: setIntercellSpacing(new Dimension(margin, margin));
099: }
100:
101: ArrayList getFilterMappings() {
102: return ((MappingTableModel) this .getModel())
103: .getFilterMappings();
104: }
105:
106: void setFilterName(String name) {
107: ((MappingTableModel) this .getModel()).setFilterName(name);
108: this .invalidate();
109: }
110:
111: void addRow(FilterMappingData fmd) {
112: this .invalidate();
113: ((MappingTableModel) getModel()).addRow(fmd);
114: }
115:
116: void addRow(int row, FilterMappingData fmd) {
117: this .invalidate();
118: ((MappingTableModel) getModel()).addRow(row, fmd);
119: }
120:
121: void setRow(int row, FilterMappingData fmd) {
122: this .invalidate();
123: ((MappingTableModel) getModel()).setRow(row, fmd);
124: }
125:
126: FilterMappingData getRow(int row) {
127: return ((MappingTableModel) getModel()).getRow(row);
128: }
129:
130: void moveRowUp(int row) {
131: ((MappingTableModel) getModel()).moveRowUp(row);
132: getSelectionModel().setSelectionInterval(row - 1, row - 1);
133: this .invalidate();
134: }
135:
136: void moveRowDown(int row) {
137: ((MappingTableModel) getModel()).moveRowUp(row + 1);
138: getSelectionModel().setSelectionInterval(row + 1, row + 1);
139: this .invalidate();
140: }
141:
142: void removeRow(int row) {
143: ((MappingTableModel) getModel()).removeRow(row);
144: this .invalidate();
145: return;
146: }
147:
148: public void setValueAt(Object o, int row, int col) {
149: if (debug)
150: log("::setValueAt()"); //NOI18N
151: return;
152: }
153:
154: private void setColors(boolean editable) {
155: Color bg;
156: this .setBorder(BorderFactory.createLoweredBevelBorder());
157: if (!editable) {
158: bg = this .getBackground().darker();
159: } else {
160: bg = Color.white;
161: }
162: this .setBackground(bg);
163: }
164:
165: void addTableModelListener(TableModelListener tml) {
166: TableModel tableModel = getModel();
167: if (tableModel != null) {
168: tableModel.addTableModelListener(tml);
169: }
170: }
171:
172: void removeTableModelListener(TableModelListener tml) {
173: TableModel tableModel = getModel();
174: if (tableModel != null) {
175: tableModel.removeTableModelListener(tml);
176: }
177: }
178:
179: public void setFont(Font f) {
180: if (debug)
181: log("::setFont()"); //NOI18N
182: fontChanged = true;
183: super .setFont(f);
184: }
185:
186: /**
187: * When paint is first invoked, we set the rowheight based on the
188: * size of the font. */
189: public void paint(Graphics g) {
190: if (debug)
191: log("::paint()"); //NOI18N
192:
193: if (fontChanged) {
194:
195: fontChanged = false;
196:
197: int height = 0;
198: if (debug)
199: log("\tGetting font height"); //NOI18N
200: FontMetrics fm = g.getFontMetrics(getFont());
201: height = fm.getHeight() + margin;
202: if (height > rowHeight)
203: rowHeight = height;
204: if (debug)
205: log("\trow height is " + rowHeight); //NOI18N
206: //triggers paint, just return afterwards
207: this .setRowHeight(rowHeight);
208: return;
209: }
210: super .paint(g);
211: }
212:
213: private void log(String s) {
214: System.out.println("MappingTable" + s); //NOI18N
215: }
216:
217: class MappingTableModel extends AbstractTableModel {
218:
219: private final String[] colheaders = {
220: NbBundle.getMessage(MappingTable.class,
221: "LBL_filter_name"),
222: NbBundle.getMessage(MappingTable.class,
223: "LBL_applies_to"), };
224:
225: private ArrayList filterMappings = null;
226: private String filterName;
227:
228: private static final long serialVersionUID = 2845252365404044474L;
229:
230: MappingTableModel(String filterName, ArrayList filterMappings) {
231: this .filterName = filterName;
232: this .filterMappings = filterMappings;
233: }
234:
235: ArrayList getFilterMappings() {
236: return filterMappings;
237: }
238:
239: void setFilterName(String name) {
240: Iterator i = filterMappings.iterator();
241: FilterMappingData fmd;
242: while (i.hasNext()) {
243: fmd = (FilterMappingData) (i.next());
244: if (fmd.getName().equals(filterName))
245: fmd.setName(name);
246: }
247: this .filterName = name;
248: }
249:
250: public int getColumnCount() {
251: return colheaders.length;
252: }
253:
254: public int getRowCount() {
255: return filterMappings.size();
256: }
257:
258: public String getColumnName(int col) {
259: return colheaders[col];
260: }
261:
262: public Object getValueAt(int row, int col) {
263: FilterMappingData fmd = (FilterMappingData) (filterMappings
264: .get(row));
265: if (col == 0)
266: return fmd.getName();
267: else
268: return fmd.getPattern();
269: }
270:
271: public Class getColumnClass(int c) {
272: return String.class;
273: }
274:
275: public boolean isCellEditable(int row, int col) {
276: return false;
277: }
278:
279: public void setValueAt(Object value, int row, int col) {
280: if (debug)
281: log("::setValueAt()"); //NOI18N
282: return;
283: }
284:
285: void addRow(int row, FilterMappingData fmd) {
286: filterMappings.add(row, fmd);
287: }
288:
289: void addRow(FilterMappingData fmd) {
290: filterMappings.add(fmd);
291: }
292:
293: FilterMappingData getRow(int row) {
294: return (FilterMappingData) (filterMappings.get(row));
295: }
296:
297: void setRow(int row, FilterMappingData fmd) {
298: filterMappings.set(row, fmd);
299: }
300:
301: void moveRowUp(int row) {
302: Object o = filterMappings.remove(row);
303: filterMappings.add(row - 1, o);
304: }
305:
306: void removeRow(int row) {
307: filterMappings.remove(row);
308: }
309:
310: private void log(String s) {
311: System.out.println("MappingTableModel" + s); //NOI18N
312: }
313: } // MappingTableModel
314:
315: } // MappingTable
|