KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > swing > lib > DynamicTable


1 /*====================================================================
2
3 Objectweb Explorer Framework
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jerome Moroy, Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26 package org.objectweb.util.explorer.swing.lib;
27
28 import java.awt.Component JavaDoc;
29 import java.awt.Point JavaDoc;
30 import java.awt.event.MouseAdapter JavaDoc;
31 import java.awt.event.MouseEvent JavaDoc;
32 import java.net.URL JavaDoc;
33
34 import javax.swing.Icon JavaDoc;
35 import javax.swing.JPopupMenu JavaDoc;
36 import javax.swing.JTable JavaDoc;
37 import javax.swing.table.AbstractTableModel JavaDoc;
38 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
39 import javax.swing.table.TableCellRenderer JavaDoc;
40
41 import org.objectweb.util.explorer.api.Entry;
42 import org.objectweb.util.explorer.api.IconProvider;
43 import org.objectweb.util.explorer.api.Tree;
44 import org.objectweb.util.explorer.core.common.api.Description;
45 import org.objectweb.util.explorer.core.common.api.ExplorerConstants;
46 import org.objectweb.util.explorer.core.menu.lib.DefaultMenuItemTreeView;
47 import org.objectweb.util.explorer.core.naming.lib.DefaultEntry;
48 import org.objectweb.util.explorer.interpreter.api.DescriptionInterpreter;
49 import org.objectweb.util.explorer.resolver.api.PropertyResolver;
50 import org.objectweb.util.explorer.swing.icon.IconFileProvider;
51 import org.objectweb.util.explorer.swing.icon.ObjectIconProvider;
52
53 /**
54  *
55  *
56  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>,
57  * <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>.
58  *
59  * @version 0.1
60  */

61 public class DynamicTable
62      extends JTable JavaDoc
63 {
64
65     //==================================================================
66
//
67
// Internal States.
68
//
69
// ==================================================================
70

71     protected TableCellRenderer JavaDoc tableCellRenderer_;
72     protected PropertyResolver propertyResolver_;
73     protected DescriptionInterpreter descriptionInterpreter_;
74     protected Tree tree_;
75     protected Icon JavaDoc objectIcon_;
76
77     // ==================================================================
78
//
79
// Constructors.
80
//
81
// ==================================================================
82

83     public DynamicTable(){
84         super();
85         tableCellRenderer_ = new MyTableCellRenderer();
86         addMouseListener(new MyMouseAdapter());
87         setColumnSelectionAllowed(true);
88         setRowSelectionAllowed(true);
89         objectIcon_ = (Icon JavaDoc)(new ObjectIconProvider()).newIcon(null);
90     }
91     
92     
93     // ==================================================================
94
//
95
// Internal methods.
96
//
97
// ==================================================================
98

99     protected PropertyResolver getPropertyResolver(){
100         return propertyResolver_;
101     }
102     
103     protected DescriptionInterpreter getDescriptionInterpreter(){
104         return descriptionInterpreter_;
105     }
106     
107     protected Tree getTree(){
108         return tree_;
109     }
110     
111     protected Icon JavaDoc getImageIcon(Entry entry){
112         if(getPropertyResolver()!=null){
113             Description desc = getPropertyResolver().resolve(ExplorerConstants.ICON_PROPERTY, entry);
114             IconProvider iconProvider = (IconProvider) getDescriptionInterpreter().interprete(desc, null);
115             if(iconProvider!=null){
116                 Object JavaDoc object = iconProvider.newIcon(entry.getValue());
117                 if(object instanceof Icon JavaDoc){
118                     return (Icon JavaDoc)object;
119                 } else if(object instanceof String JavaDoc) {
120                     return (Icon JavaDoc)(new IconFileProvider((String JavaDoc)object)).newIcon(entry.getValue());
121                 } else if (object instanceof URL JavaDoc) {
122                     return (Icon JavaDoc)(new IconFileProvider((URL JavaDoc)object)).newIcon(entry.getValue());
123                 }
124             }
125         }
126         return null;
127     }
128     
129     //==================================================================
130
//
131
// Public methods overriding JTable methods.
132
//
133
//==================================================================
134

135     public TableCellRenderer JavaDoc getCellRenderer(int row, int col) {
136         return tableCellRenderer_;
137     }
138
139     public void setPropertyResolver(PropertyResolver propertyResolver){
140         // TODO: A fractal assembly is needed here!!! (dynamic bindings)
141
propertyResolver_ = propertyResolver;
142     }
143     
144     public void setDescriptionInterpreter(DescriptionInterpreter descriptionInterpreter){
145         // TODO: A fractal assembly is needed here!!! (dynamic bindings)
146
descriptionInterpreter_ = descriptionInterpreter;
147     }
148
149     public void setTree(Tree tree){
150         // TODO: A fractal assembly is needed here!!! (dynamic bindings)
151
tree_ = tree;
152     }
153     
154     public void setData(String JavaDoc[] headers, Object JavaDoc[][] values){
155         setModel(new TableModel JavaDoc(headers, values));
156     }
157
158     //==================================================================
159
//
160
// Public methods for TableConfiguration interface.
161
//
162
//==================================================================
163

164     protected class TableModel
165             extends AbstractTableModel JavaDoc {
166     
167         protected String JavaDoc[] headers_;
168         protected Object JavaDoc[][] values_;
169     
170         public TableModel(String JavaDoc[] headers, Object JavaDoc[][] values){
171             headers_ = headers;
172             values_ = new Object JavaDoc[values.length][0];
173             if(values_!=null){
174                 for(int i = 0 ; i < values.length ; i++){
175                     values_[i] = new Object JavaDoc[values[i].length];
176                     for(int j = 0 ; j < values[i].length ; j++){
177                         Object JavaDoc value = values[i][j];
178                         if(value!=null){
179                             if(value instanceof Entry){
180                                 values_[i][j] = (Entry)value;
181                             } else {
182                                 //values_[i][j] = decoder_.decode(value);
183
Entry newEntry = new DefaultEntry(value.toString(), value);
184                                 values_[i][j] = newEntry;
185                             }
186                         }
187                     }
188                 }
189             }
190         }
191     
192         /**
193          * Overriding methods
194          * @see javax.swing.table.TableModel#getColumnName(int)
195          */

196         public String JavaDoc getColumnName(int column) {
197             if(headers_!=null)
198                 return headers_[column];
199             return super.getColumnName(column);
200         }
201
202         /**
203          * Overriding methods
204          * @see javax.swing.table.TableModel#getColumnCount()
205          */

206         public int getColumnCount() {
207             if(headers_!=null)
208                 return headers_.length;
209             return 0;
210             }
211
212         /**
213          * Overriding methods
214          * @see javax.swing.table.TableModel#getRowCount()
215          */

216         public int getRowCount() {
217             if(values_ != null){
218                 return values_.length;
219             }
220             return 0;
221         }
222
223         /**
224          * Overriding methods
225          * @see javax.swing.table.TableModel#getValueAt(int, int)
226          */

227         public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
228             if(values_ != null)
229                 return values_[rowIndex][columnIndex];
230             return null;
231         }
232         
233         
234     }
235
236     /**
237      * Mouse Adapter
238      * Use to draw Popup Menu
239      */

240     protected final class MyMouseAdapter extends MouseAdapter JavaDoc {
241         public void mousePressed(MouseEvent JavaDoc e) {
242             popupLayout(e);
243         }
244         public void mouseReleased(MouseEvent JavaDoc e) {
245             popupLayout(e);
246         }
247         private void popupLayout(MouseEvent JavaDoc e) {
248             if (e.isPopupTrigger()) {
249                 Point JavaDoc point = e.getPoint();
250                 int row = rowAtPoint(point);
251                 int col = columnAtPoint(point);
252                 if((row != -1) && (col != -1)) {
253                     Entry object = (Entry)getValueAt(row,col);
254                     if(getPropertyResolver()!=null && getDescriptionInterpreter()!=null){
255                         Description desc = getPropertyResolver().resolve(ExplorerConstants.MENU_PROPERTY, object, null);
256                         JPopupMenu JavaDoc menu = (JPopupMenu JavaDoc) getDescriptionInterpreter().interprete(desc, new DefaultMenuItemTreeView(getTree(),object,null));
257                         if (menu != null) {
258                             changeSelection(row,col,false,false);
259                             menu.show(DynamicTable.this,(int) (point.getX()),(int) (point.getY()));
260                         }
261                     }
262                 }
263             }
264         }
265     }
266
267     /**
268      * Table Cell Renderer
269      * Use to put the icon corresponding to the class
270      * of the Value object of the Entry
271      */

272     protected final class MyTableCellRenderer extends DefaultTableCellRenderer JavaDoc {
273
274         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int row, int column) {
275             super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
276             if(value != null) {
277                 Entry entry = (Entry)value;
278                 setText(entry.getName().toString());
279                 Icon JavaDoc icon = getImageIcon(entry);
280                 setIcon(icon==null?objectIcon_:icon);
281             } else {
282                 setIcon(null);
283             }
284             return this;
285         }
286
287     }
288     
289 }
290
291
292
Popular Tags