KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > browser > core > common > DynamicTable


1 /*====================================================================
2
3 Objectweb Browser Framework
4 Copyright (C) 2000-2003 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): Philippe Merle, Jerome Moroy.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.util.browser.core.common;
28
29 import java.awt.Component JavaDoc;
30 import java.awt.Point JavaDoc;
31 import java.awt.event.MouseAdapter JavaDoc;
32 import java.awt.event.MouseEvent 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.browser.api.Entry;
42 import org.objectweb.util.browser.core.api.Decoder;
43 import org.objectweb.util.browser.core.api.TableConfiguration;
44
45 import org.objectweb.util.browser.core.naming.DefaultEntry;
46 import org.objectweb.util.browser.core.naming.DefaultName;
47
48 /**
49  *
50  *
51  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>,
52  * <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>
53  *
54  * @version 0.1
55  */

56 public class DynamicTable
57      extends JTable JavaDoc
58   implements TableConfiguration {
59
60     //==================================================================
61
//
62
// Internal states.
63
//
64
//==================================================================
65

66     protected AdminCustomization custom_;
67
68     protected TableCellRenderer JavaDoc tableCellRenderer_;
69         
70     protected Decoder decoder_;
71         
72     //==================================================================
73
//
74
// Constructor.
75
//
76
//==================================================================
77

78     public DynamicTable(Decoder currentDecoder){
79         super();
80         tableCellRenderer_ = new MyTableCellRenderer();
81         decoder_ = currentDecoder;
82         addMouseListener(new MyMouseAdapter());
83         setColumnSelectionAllowed(true);
84         setRowSelectionAllowed(true);
85     }
86
87     public DynamicTable(){
88         this(null);
89     }
90
91     //==================================================================
92
//
93
// No internal method.
94
//
95
//==================================================================
96

97     //==================================================================
98
//
99
// Public methods overriding JTable methods.
100
//
101
//==================================================================
102

103     public TableCellRenderer JavaDoc getCellRenderer(int row, int col) {
104         return tableCellRenderer_;
105     }
106
107     //==================================================================
108
//
109
// Public methods for TableConfiguration interface.
110
//
111
//==================================================================
112

113     public void setAdminCustomization(AdminCustomization custom) {
114         custom_ = custom;
115     }
116
117     public void setData(String JavaDoc[] headers, Object JavaDoc[][] values){
118         setModel(new TableModel JavaDoc(headers, values));
119     }
120
121     //==================================================================
122
//
123
// Public methods for TableConfiguration interface.
124
//
125
//==================================================================
126

127     protected class TableModel
128             extends AbstractTableModel JavaDoc {
129     
130         protected String JavaDoc[] headers_;
131         protected Object JavaDoc[][] values_;
132     
133         public TableModel(String JavaDoc[] headers, Object JavaDoc[][] values){
134             headers_ = headers;
135             values_ = values;
136             if(values_!=null && decoder_!=null){
137                 for(int i = 0 ; i < values_.length ; i++){
138                     for(int j = 0 ; j < values_[i].length ; j++){
139                         Object JavaDoc value = getValueAt(i,j);
140                         if(value!=null){
141                             if(Entry.class.isAssignableFrom(value.getClass())){
142                                 Entry currentEntry = (Entry)value;
143                                 Entry newEntry = new DefaultEntry(decoder_.decode(currentEntry.getValue()),new DefaultName(currentEntry.getName().toString()),null);
144                                 values_[i][j] = newEntry;
145                             } else {
146                                 values_[i][j] = decoder_.decode(value);
147                             }
148                         }
149                     }
150                 }
151             }
152         }
153     
154         /**
155          * Overriding methods
156          * @see javax.swing.table.TableModel#getColumnName(int)
157          */

158         public String JavaDoc getColumnName(int column) {
159             if(headers_!=null)
160                 return headers_[column];
161             return super.getColumnName(column);
162         }
163
164         /**
165          * Overriding methods
166          * @see javax.swing.table.TableModel#getColumnCount()
167          */

168         public int getColumnCount() {
169             if(headers_!=null)
170                 return headers_.length;
171             return 0;
172             }
173
174         /**
175          * Overriding methods
176          * @see javax.swing.table.TableModel#getRowCount()
177          */

178         public int getRowCount() {
179             if(values_ != null){
180                 return values_.length;
181             }
182             return 0;
183         }
184
185         /**
186          * Overriding methods
187          * @see javax.swing.table.TableModel#getValueAt(int, int)
188          */

189         public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
190             if(values_ != null)
191                 return values_[rowIndex][columnIndex];
192             return null;
193         }
194         
195         
196     }
197
198     /**
199      * Mouse Adapter
200      * Use to draw Popup Menu
201      */

202     protected final class MyMouseAdapter extends MouseAdapter JavaDoc {
203         public void mousePressed(MouseEvent JavaDoc e) {
204             popupLayout(e);
205         }
206         public void mouseReleased(MouseEvent JavaDoc e) {
207             popupLayout(e);
208         }
209         private void popupLayout(MouseEvent JavaDoc e) {
210             if (e.isPopupTrigger()) {
211                 Point JavaDoc point = e.getPoint();
212                 int row = rowAtPoint(point);
213                 int col = columnAtPoint(point);
214                 if((row != -1) && (col != -1)) {
215                     Object JavaDoc object = getValueAt(row,col);
216                     if (custom_ != null) {
217                         JPopupMenu JavaDoc menu = custom_.getMenu(object,AdminCustomization.TABLE_TARGET);
218                         if (menu != null) {
219                             changeSelection(row,col,false,false);
220                             menu.show(DynamicTable.this,(int) (point.getX()),(int) (point.getY()));
221                         }
222                     }
223                 }
224             }
225         }
226     }
227
228     /**
229      * Table Cell Renderer
230      * Use to put the icon corresponding to the class
231      * of the Value object of the Entry
232      */

233     protected final class MyTableCellRenderer extends DefaultTableCellRenderer JavaDoc {
234
235         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int row, int column) {
236             super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
237             if(value != null) {
238                 if(Entry.class.isAssignableFrom(value.getClass()))
239                     setText(((Entry)value).getName().toString());
240                 else
241                     setText(value.toString());
242               
243                 if (custom_ != null) {
244                     Icon JavaDoc icon = custom_.getIcon(value);
245                     if (icon != null)
246                         setIcon(icon);
247                     else
248                         setIcon(null);
249                 }
250             } else {
251                 setIcon(null);
252             }
253             return this;
254              
255         }
256
257     }
258
259
260 }
Popular Tags