KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > dialog > model > AttributeTableModel


1 /***
2  * FractalGUI: a graphical tool to edit Fractal component configurations.
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  *
21  * Authors: Eric Bruneton, Patrice Fauvel
22  */

23
24 package org.objectweb.fractal.gui.dialog.model;
25
26 import org.objectweb.fractal.gui.model.Component;
27
28 import javax.swing.table.AbstractTableModel JavaDoc;
29
30 /**
31  * A {@link javax.swing.table.TableModel} based on a {@link Component} model
32  * that represents attribute names and values. This model makes a conversion
33  * from a {@link Component} model to a {@link javax.swing.table.TableModel}.
34  */

35
36 public class AttributeTableModel extends AbstractTableModel JavaDoc {
37
38   /**
39    * The component model on which this model is based.
40    */

41
42   private Component model;
43
44   /**
45    * Sets the component model on which this model is based.
46    *
47    * @param model a component.
48    */

49
50   void setComponentModel (final Component model) {
51     this.model = model;
52     fireTableDataChanged();
53   }
54
55   /**
56    * Notifies this model that an attribute has changed in {@link #model}.
57    *
58    * @param attributeName the attribute that has changed.
59    * @param oldValue the old value of this attribute.
60    */

61
62   void attributeChanged (final String JavaDoc attributeName, final String JavaDoc oldValue) {
63     fireTableDataChanged();
64   }
65
66   // -------------------------------------------------------------------------
67
// Implementation of the TableModel interface
68
// -------------------------------------------------------------------------
69

70   public int getRowCount () {
71     if (model == null) {
72       return 0;
73     }
74     return model.getAttributeNames().size();
75   }
76
77   public int getColumnCount () {
78     return 2;
79   }
80
81   public String JavaDoc getColumnName (final int column) {
82     return column == 0 ? "Name" : "Value";
83   }
84
85   public boolean isCellEditable (final int rowIndex, final int columnIndex) {
86     return true;
87   }
88
89   public Object JavaDoc getValueAt (final int rowIndex, final int columnIndex) {
90     Object JavaDoc key = model.getAttributeNames().get(rowIndex);
91     if (columnIndex == 0) {
92       return key;
93     } else {
94       return model.getAttribute((String JavaDoc)key);
95     }
96   }
97
98   public void setValueAt (
99     final Object JavaDoc aValue,
100     final int rowIndex,
101     final int columnIndex)
102   {
103     String JavaDoc key = (String JavaDoc)model.getAttributeNames().get(rowIndex);
104     if (columnIndex == 0) {
105       String JavaDoc value = model.getAttribute(key);
106       model.setAttribute(key, null);
107       model.setAttribute((String JavaDoc)aValue, value);
108     } else {
109       model.setAttribute(key, (String JavaDoc)aValue);
110     }
111   }
112 }
113
Popular Tags