KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.objectweb.fractal.gui.model.Interface;
28
29 import javax.swing.table.AbstractTableModel JavaDoc;
30
31 /**
32  * A {@link javax.swing.table.TableModel} based on a {@link Component} model
33  * that represents interfaces names, signatures... This model makes a conversion
34  * from a {@link Component} model to a {@link javax.swing.table.TableModel}.
35  */

36
37 public class InterfaceTableModel extends AbstractTableModel JavaDoc {
38
39   /**
40    * If this model represents client interfaces or server interfaces.
41    */

42
43   private boolean isClient;
44
45   /**
46    * The component model on which this model is based.
47    */

48
49   private Component model;
50
51   /**
52    * Constructs a new {@link InterfaceTableModel}.
53    *
54    * @param isClient if this model must represent client or server interfaces.
55    */

56
57   InterfaceTableModel (final boolean isClient) {
58     this.isClient = isClient;
59   }
60
61   /**
62    * Sets the component model on which this model is based.
63    *
64    * @param model a component.
65    */

66
67   void setComponentModel (final Component model) {
68     this.model = model;
69     fireTableDataChanged();
70   }
71
72   /**
73    * Notifies this listener that an interface has changed.
74    *
75    * @param i the interface that has changed.
76    */

77
78   void interfaceChanged (final Interface i) {
79     final int index;
80     if (isClient) {
81       index = i.getOwner().getClientInterfaces().indexOf(i);
82     } else {
83       index = i.getOwner().getServerInterfaces().indexOf(i);
84     }
85     if (index != -1) {
86       fireTableRowsUpdated(index, index);
87     }
88   }
89
90   /**
91    * Notifies this listener that an interface has been added.
92    *
93    * @param i the interface that has been added.
94    * @param index the index of this interface in the client or server interface
95    * list of the component.
96    */

97
98   void interfaceAdded (final Interface i, final int index) {
99     fireTableRowsInserted(index, index);
100   }
101
102   /**
103    * Notifies this listener that an interface has been removed.
104    *
105    * @param i the interface that has been removed.
106    * @param index the index of this interface in the client or server interface
107    * list of the component.
108    */

109
110   void interfaceRemoved (final Interface i, final int index) {
111     fireTableRowsDeleted(index, index);
112   }
113
114   // -------------------------------------------------------------------------
115
// Implementation of the TableModel interface
116
// -------------------------------------------------------------------------
117

118   public int getRowCount () {
119     if (model == null) {
120       return 0;
121     }
122     if (isClient) {
123       return model.getClientInterfaces().size();
124     } else {
125       return model.getServerInterfaces().size();
126     }
127   }
128
129   public int getColumnCount () {
130     return 4;
131   }
132
133   public String JavaDoc getColumnName (final int column) {
134     switch (column) {
135       case 0:
136         return "Name";
137       case 1:
138         return "Signature";
139       case 2:
140         return "Contingency";
141       default:
142         return "Cardinality";
143     }
144   }
145
146   public boolean isCellEditable (final int rowIndex, final int columnIndex) {
147     Interface i;
148     if (isClient) {
149       i = (Interface)model.getClientInterfaces().get(rowIndex);
150     } else {
151       i = (Interface)model.getServerInterfaces().get(rowIndex);
152     }
153     return i.getMasterCollectionInterface() == null;
154   }
155
156   public Object JavaDoc getValueAt (final int rowIndex, final int columnIndex) {
157     Interface i;
158     if (isClient) {
159       i = (Interface)model.getClientInterfaces().get(rowIndex);
160     } else {
161       i = (Interface)model.getServerInterfaces().get(rowIndex);
162     }
163     switch (columnIndex) {
164       case 0:
165         return i.getName();
166       case 1:
167         return i.getSignature();
168       case 2:
169         return i.isOptional() ? "optional" : "mandatory";
170       case 3:
171         return i.isCollection() ? "collection" : "single";
172       default:
173         return i;
174     }
175   }
176
177   public void setValueAt (
178     final Object JavaDoc aValue,
179     final int rowIndex,
180     final int columnIndex)
181   {
182     Interface i;
183     if (isClient) {
184       i = (Interface)model.getClientInterfaces().get(rowIndex);
185     } else {
186       i = (Interface)model.getServerInterfaces().get(rowIndex);
187     }
188     switch (columnIndex) {
189       case 0:
190         i.setName((String JavaDoc)aValue);
191         break;
192       case 1:
193         i.setSignature((String JavaDoc)aValue);
194         break;
195       case 2:
196         i.setIsOptional(aValue.equals("optional")); // catcher exception Joptionpane
197
break;
198       default:
199         i.setIsCollection(aValue.equals("collection"));
200     }
201   }
202 }
203
Popular Tags