KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > xmlc > MapPanel


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  */

22 package org.enhydra.kelp.common.xmlc;
23
24 // Kelp imports
25
import org.enhydra.kelp.common.event.SwingTableSelectionEvent;
26 import org.enhydra.kelp.common.event.SwingTableSelectionListener;
27 import org.enhydra.kelp.common.swing.CellRendererWithToolTip;
28
29 // Standard imports
30
import javax.swing.*;
31 import javax.swing.table.*;
32 import javax.swing.event.*;
33 import java.awt.*;
34 import java.beans.*;
35 import java.util.Vector JavaDoc;
36 import java.util.ResourceBundle JavaDoc;
37
38 /**
39  * Class declaration
40  *
41  *
42  * @author Paul Mahr
43  */

44 public class MapPanel extends JPanel implements ListSelectionListener {
45
46     //
47
static ResourceBundle JavaDoc res =
48         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
49

50     //
51
protected static final int NO_SELECTION = -1;
52     private JTable table;
53     private JScrollPane scrollTable;
54     private BorderLayout layoutMain;
55     private MapTableModel tableModel = new MapTableModel();
56     private int currentSelectionIndex = NO_SELECTION;
57     private Vector JavaDoc swingTableSelectionListeners = new Vector JavaDoc();
58     private String JavaDoc[][] map = new String JavaDoc[0][2];
59
60     /**
61      * Constructor declaration
62      *
63      */

64     public MapPanel() {
65         try {
66             jbInit();
67             pmInit();
68         } catch (Exception JavaDoc ex) {
69             ex.printStackTrace();
70         }
71     }
72
73     public synchronized void addSwingTableSelectionListener(SwingTableSelectionListener l) {
74         if (!swingTableSelectionListeners.contains(l)) {
75             swingTableSelectionListeners.addElement(l);
76         }
77     }
78
79     public synchronized void removeSwingTableSelectionListener(SwingTableSelectionListener l) {
80         if (swingTableSelectionListeners.contains(l)) {
81             swingTableSelectionListeners.removeElement(l);
82         }
83     }
84
85     /**
86      * ListSelectionListener event
87      */

88     public void valueChanged(ListSelectionEvent e) {
89         ListSelectionModel lsm = (ListSelectionModel) e.getSource();
90
91         if (lsm.isSelectionEmpty()) {
92             currentSelectionIndex = NO_SELECTION;
93         } else {
94             currentSelectionIndex = lsm.getMinSelectionIndex();
95         }
96         SwingTableSelectionListener listener = null;
97
98         for (int i = 0; i < swingTableSelectionListeners.size(); i++) {
99             listener =
100                 (SwingTableSelectionListener) swingTableSelectionListeners.elementAt(i);
101             listener.onSwingTableSelection(new SwingTableSelectionEvent(this,
102                     currentSelectionIndex));
103         }
104     }
105
106     /**
107      * Method declaration
108      *
109      *
110      * @return
111      */

112     protected int getCurrentSelectionIndex() {
113         return currentSelectionIndex;
114     }
115
116     protected String JavaDoc getSelectedFolder() {
117         String JavaDoc folder = null;
118
119         if (currentSelectionIndex > -1) {
120             folder = tableModel.getRow(currentSelectionIndex).getFolder();
121         }
122         return folder;
123     }
124
125     protected void setSelectedFolder(String JavaDoc folder) {
126         if (currentSelectionIndex > -1) {
127             tableModel.getRow(currentSelectionIndex).setFolder(folder);
128             tableModel.fireTableDataChanged();
129         }
130     }
131
132     protected String JavaDoc getSelectedPackageName() {
133         String JavaDoc pack = null;
134
135         if (currentSelectionIndex > -1) {
136             pack = tableModel.getRow(currentSelectionIndex).getPackageName();
137         }
138         return pack;
139     }
140
141     protected void setSelectedPackageName(String JavaDoc pack) {
142         if (currentSelectionIndex > -1) {
143             tableModel.getRow(currentSelectionIndex).setPackageName(pack);
144             tableModel.fireTableDataChanged();
145         }
146     }
147
148     protected void updateSelectedRow(String JavaDoc folder, String JavaDoc pack) {
149         if (currentSelectionIndex > -1) {
150             tableModel.getRow(currentSelectionIndex).setFolder(folder);
151             tableModel.getRow(currentSelectionIndex).setPackageName(pack);
152             tableModel.fireTableDataChanged();
153         }
154     }
155
156     /**
157      * Method declaration
158      *
159      *
160      * @param f
161      * @param p
162      */

163     protected void addRow(String JavaDoc f, String JavaDoc p) {
164         tableModel.addRow(f, p);
165     }
166
167     /**
168      * Method declaration
169      *
170      *
171      * @param index
172      */

173     protected void removeRow(int index) {
174         tableModel.removeRow(index);
175     }
176
177     /**
178      * Method declaration
179      *
180      *
181      * @throws Exception
182      */

183     private void jbInit() throws Exception JavaDoc {
184         layoutMain =
185             (BorderLayout) Beans.instantiate(getClass().getClassLoader(),
186                                              BorderLayout.class.getName());
187         table = (JTable) Beans.instantiate(getClass().getClassLoader(),
188                                            JTable.class.getName());
189         scrollTable = new JScrollPane(table);
190         table.setToolTipText(new String JavaDoc());
191         table.sizeColumnsToFit(JTable.AUTO_RESIZE_ALL_COLUMNS);
192         table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
193         table.setColumnSelectionAllowed(false);
194         scrollTable.getViewport().add(table, BorderLayout.CENTER);
195         ListSelectionModel rowSM = table.getSelectionModel();
196
197         rowSM.addListSelectionListener(this);
198         this.setPreferredSize(new Dimension(620, 200));
199         this.setLayout(layoutMain);
200         this.add(scrollTable, BorderLayout.CENTER);
201     }
202
203     /**
204      * Method declaration
205      *
206      *
207      * @return
208      */

209     public String JavaDoc[][] getMap() {
210         tableModel.readMap();
211         return map;
212     }
213
214     /**
215      * Method declaration
216      *
217      *
218      * @param m
219      */

220     public void setMap(String JavaDoc[][] m) {
221         map = m;
222         tableModel.populateModel();
223         table.setModel(tableModel);
224     }
225
226     /**
227      * Method declaration
228      *
229      */

230     private void pmInit() {
231         tableModel = new MapTableModel();
232         table.setModel(tableModel);
233         table.setDefaultRenderer(String JavaDoc.class, new CellRendererWithToolTip());
234         tableModel.addTableModelListener(table);
235         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
236         table.getTableHeader().setUpdateTableInRealTime(false);
237         table.getTableHeader().setReorderingAllowed(false);
238     }
239
240     /**
241      * Class declaration
242      *
243      *
244      */

245     class MapTableModel extends AbstractTableModel {
246         private Vector JavaDoc rowVector = new Vector JavaDoc();
247
248         public MapTableModel() {}
249
250         public int getRowCount() {
251             int count = 0;
252
253             if (rowVector != null) {
254                 count = rowVector.size();
255             }
256             return count;
257         }
258
259         public int getColumnCount() {
260             return 2;
261         }
262
263         /**
264          * Method declaration
265          *
266          *
267          * @param columnIndex
268          *
269          * @return
270          */

271         public String JavaDoc getColumnName(int columnIndex) {
272             String JavaDoc name = new String JavaDoc();
273
274             switch (columnIndex) {
275             case 0:
276                 name = res.getString("Source_Directory");
277                 break;
278             case 1:
279                 name = res.getString("Package");
280                 break;
281             }
282             return name;
283         }
284
285         /**
286          * Method declaration
287          *
288          *
289          * @param columnIndex
290          *
291          * @return
292          */

293         public Class JavaDoc getColumnClass(int columnIndex) {
294             Class JavaDoc columnClass = null;
295             Object JavaDoc value = getValueAt(0, columnIndex);
296
297             if (value != null) {
298                 columnClass = value.getClass();
299             }
300             return columnClass;
301         }
302
303         /**
304          * Method declaration
305          *
306          *
307          * @param rowIndex
308          * @param columnIndex
309          *
310          * @return
311          */

312         public boolean isCellEditable(int rowIndex, int columnIndex) {
313             return false;
314         }
315
316         /**
317          * Method declaration
318          *
319          *
320          * @param rowIndex
321          * @param columnIndex
322          *
323          * @return
324          */

325         public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
326             Object JavaDoc value = null;
327
328             if (!isTableEmpty()) {
329                 MapRow row = (MapRow) rowVector.elementAt(rowIndex);
330                 MapCell cell = new MapCell(columnIndex, row);
331
332                 value = cell.getValue();
333             }
334             return value;
335         }
336
337         /**
338          * Method declaration
339          *
340          *
341          * @param aValue
342          * @param rowIndex
343          * @param columnIndex
344          */

345         public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex) {
346             if (!isTableEmpty()) {
347                 MapRow row = (MapRow) rowVector.elementAt(rowIndex);
348                 MapCell cell = new MapCell(columnIndex, row);
349
350                 cell.setValue(aValue);
351                 fireTableCellUpdated(columnIndex, rowIndex);
352             }
353         }
354
355         /**
356          * Method declaration
357          *
358          *
359          * @param rowIndex
360          *
361          * @return
362          */

363         protected MapRow getRow(int rowIndex) {
364             MapRow row = null;
365
366             if (!isTableEmpty()) {
367                 if (rowVector.size() > rowIndex) {
368                     row = (MapRow) rowVector.elementAt(rowIndex);
369                 }
370             }
371             return row;
372         }
373
374         protected void readMap() {
375             int rowCount = rowVector.size();
376
377             map = new String JavaDoc[rowCount][2];
378             MapRow row = null;
379
380             for (int i = 0; i < rowCount; i++) {
381                 row = (MapRow) rowVector.elementAt(i);
382                 map[i][0] = row.getFolder();
383                 map[i][1] = row.getPackageName();
384             }
385         }
386
387         protected void populateModel() {
388             while (getRowCount() > 0) {
389                 removeRow(0);
390             }
391             if (map != null) {
392                 int rowCount = map.length;
393
394                 for (int i = 0; i < rowCount; i++) {
395                     addRow(map[i][0], map[i][1]);
396                 }
397             }
398         }
399
400         // /
401
// /
402
private boolean isTableEmpty() {
403             boolean empty = true;
404
405             if (rowVector != null) {
406                 if (rowVector.size() > 0) {
407                     empty = false;
408                 }
409             }
410             return empty;
411         }
412
413         private void addRow(String JavaDoc f, String JavaDoc p) {
414             MapRow newRow = null;
415
416             newRow = new MapRow(f, p);
417             rowVector.addElement(newRow);
418             int size = rowVector.size();
419
420             fireTableDataChanged();
421         }
422
423         private void removeRow(int index) {
424             rowVector.removeElementAt(index);
425             fireTableDataChanged();
426         }
427
428     }
429
430     /**
431      * Class declaration
432      *
433      *
434      * @author
435      * @version %I%, %G%
436      */

437     class MapRow {
438         private String JavaDoc fromFolder = new String JavaDoc();
439         private String JavaDoc toPackage = new String JavaDoc();
440
441         /**
442          * Constructor declaration
443          *
444          *
445          * @param f
446          * @param p
447          */

448         public MapRow(String JavaDoc f, String JavaDoc p) {
449             fromFolder = f;
450             toPackage = p;
451         }
452
453         /**
454          * Method declaration
455          *
456          *
457          * @return
458          */

459         public String JavaDoc getFolder() {
460             return fromFolder;
461         }
462
463         /**
464          * Method declaration
465          *
466          *
467          * @param f
468          */

469         public void setFolder(String JavaDoc f) {
470             fromFolder = f;
471         }
472
473         /**
474          * Method declaration
475          *
476          *
477          * @return
478          */

479         public String JavaDoc getPackageName() {
480             return toPackage;
481         }
482
483         /**
484          * Method declaration
485          *
486          *
487          * @param p
488          */

489         public void setPackageName(String JavaDoc p) {
490             toPackage = p;
491         }
492
493     }
494
495     /**
496      * Class declaration
497      *
498      *
499      * @author
500      * @version %I%, %G%
501      */

502     class MapCell {
503         private MapRow row;
504         private int column;
505
506         /**
507          * Constructor declaration
508          *
509          *
510          * @param c
511          * @param r
512          */

513         public MapCell(int c, MapRow r) {
514             column = c;
515             row = r;
516         }
517
518         /**
519          * Method declaration
520          *
521          *
522          * @param value
523          *
524          * @return
525          */

526         public boolean setValue(Object JavaDoc value) {
527             boolean set = true;
528
529             switch (column) {
530             case 0:
531                 row.setFolder((String JavaDoc) value);
532                 break;
533             case 1:
534                 row.setPackageName((String JavaDoc) value);
535                 break;
536             default:
537                 set = false;
538                 break;
539             }
540             return set;
541         }
542
543         /**
544          * Method declaration
545          *
546          *
547          * @return
548          */

549         public Object JavaDoc getValue() {
550             Object JavaDoc value = null;
551
552             switch (column) {
553             case 0:
554                 value = row.getFolder();
555                 break;
556             case 1:
557                 value = row.getPackageName();
558                 break;
559             }
560             return value;
561         }
562
563         //
564
// PROTECTED
565
//
566
protected MapRow getRow() {
567             return row;
568         }
569
570     }
571 }
572
Popular Tags