001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: */
022: package org.enhydra.kelp.common.xmlc;
023:
024: // Kelp imports
025: import org.enhydra.kelp.common.event.SwingTableSelectionEvent;
026: import org.enhydra.kelp.common.event.SwingTableSelectionListener;
027: import org.enhydra.kelp.common.swing.CellRendererWithToolTip;
028:
029: // Standard imports
030: import javax.swing.*;
031: import javax.swing.table.*;
032: import javax.swing.event.*;
033: import java.awt.*;
034: import java.beans.*;
035: import java.util.Vector;
036: import java.util.ResourceBundle;
037:
038: /**
039: * Class declaration
040: *
041: *
042: * @author Paul Mahr
043: */
044: public class MapPanel extends JPanel implements ListSelectionListener {
045:
046: //
047: static ResourceBundle res = ResourceBundle
048: .getBundle("org.enhydra.kelp.common.Res"); // nores
049:
050: //
051: protected static final int NO_SELECTION = -1;
052: private JTable table;
053: private JScrollPane scrollTable;
054: private BorderLayout layoutMain;
055: private MapTableModel tableModel = new MapTableModel();
056: private int currentSelectionIndex = NO_SELECTION;
057: private Vector swingTableSelectionListeners = new Vector();
058: private String[][] map = new String[0][2];
059:
060: /**
061: * Constructor declaration
062: *
063: */
064: public MapPanel() {
065: try {
066: jbInit();
067: pmInit();
068: } catch (Exception ex) {
069: ex.printStackTrace();
070: }
071: }
072:
073: public synchronized void addSwingTableSelectionListener(
074: SwingTableSelectionListener l) {
075: if (!swingTableSelectionListeners.contains(l)) {
076: swingTableSelectionListeners.addElement(l);
077: }
078: }
079:
080: public synchronized void removeSwingTableSelectionListener(
081: SwingTableSelectionListener l) {
082: if (swingTableSelectionListeners.contains(l)) {
083: swingTableSelectionListeners.removeElement(l);
084: }
085: }
086:
087: /**
088: * ListSelectionListener event
089: */
090: public void valueChanged(ListSelectionEvent e) {
091: ListSelectionModel lsm = (ListSelectionModel) e.getSource();
092:
093: if (lsm.isSelectionEmpty()) {
094: currentSelectionIndex = NO_SELECTION;
095: } else {
096: currentSelectionIndex = lsm.getMinSelectionIndex();
097: }
098: SwingTableSelectionListener listener = null;
099:
100: for (int i = 0; i < swingTableSelectionListeners.size(); i++) {
101: listener = (SwingTableSelectionListener) swingTableSelectionListeners
102: .elementAt(i);
103: listener
104: .onSwingTableSelection(new SwingTableSelectionEvent(
105: this , currentSelectionIndex));
106: }
107: }
108:
109: /**
110: * Method declaration
111: *
112: *
113: * @return
114: */
115: protected int getCurrentSelectionIndex() {
116: return currentSelectionIndex;
117: }
118:
119: protected String getSelectedFolder() {
120: String folder = null;
121:
122: if (currentSelectionIndex > -1) {
123: folder = tableModel.getRow(currentSelectionIndex)
124: .getFolder();
125: }
126: return folder;
127: }
128:
129: protected void setSelectedFolder(String folder) {
130: if (currentSelectionIndex > -1) {
131: tableModel.getRow(currentSelectionIndex).setFolder(folder);
132: tableModel.fireTableDataChanged();
133: }
134: }
135:
136: protected String getSelectedPackageName() {
137: String pack = null;
138:
139: if (currentSelectionIndex > -1) {
140: pack = tableModel.getRow(currentSelectionIndex)
141: .getPackageName();
142: }
143: return pack;
144: }
145:
146: protected void setSelectedPackageName(String pack) {
147: if (currentSelectionIndex > -1) {
148: tableModel.getRow(currentSelectionIndex).setPackageName(
149: pack);
150: tableModel.fireTableDataChanged();
151: }
152: }
153:
154: protected void updateSelectedRow(String folder, String pack) {
155: if (currentSelectionIndex > -1) {
156: tableModel.getRow(currentSelectionIndex).setFolder(folder);
157: tableModel.getRow(currentSelectionIndex).setPackageName(
158: pack);
159: tableModel.fireTableDataChanged();
160: }
161: }
162:
163: /**
164: * Method declaration
165: *
166: *
167: * @param f
168: * @param p
169: */
170: protected void addRow(String f, String p) {
171: tableModel.addRow(f, p);
172: }
173:
174: /**
175: * Method declaration
176: *
177: *
178: * @param index
179: */
180: protected void removeRow(int index) {
181: tableModel.removeRow(index);
182: }
183:
184: /**
185: * Method declaration
186: *
187: *
188: * @throws Exception
189: */
190: private void jbInit() throws Exception {
191: layoutMain = (BorderLayout) Beans.instantiate(getClass()
192: .getClassLoader(), BorderLayout.class.getName());
193: table = (JTable) Beans.instantiate(getClass().getClassLoader(),
194: JTable.class.getName());
195: scrollTable = new JScrollPane(table);
196: table.setToolTipText(new String());
197: table.sizeColumnsToFit(JTable.AUTO_RESIZE_ALL_COLUMNS);
198: table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
199: table.setColumnSelectionAllowed(false);
200: scrollTable.getViewport().add(table, BorderLayout.CENTER);
201: ListSelectionModel rowSM = table.getSelectionModel();
202:
203: rowSM.addListSelectionListener(this );
204: this .setPreferredSize(new Dimension(620, 200));
205: this .setLayout(layoutMain);
206: this .add(scrollTable, BorderLayout.CENTER);
207: }
208:
209: /**
210: * Method declaration
211: *
212: *
213: * @return
214: */
215: public String[][] getMap() {
216: tableModel.readMap();
217: return map;
218: }
219:
220: /**
221: * Method declaration
222: *
223: *
224: * @param m
225: */
226: public void setMap(String[][] m) {
227: map = m;
228: tableModel.populateModel();
229: table.setModel(tableModel);
230: }
231:
232: /**
233: * Method declaration
234: *
235: */
236: private void pmInit() {
237: tableModel = new MapTableModel();
238: table.setModel(tableModel);
239: table.setDefaultRenderer(String.class,
240: new CellRendererWithToolTip());
241: tableModel.addTableModelListener(table);
242: table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
243: table.getTableHeader().setUpdateTableInRealTime(false);
244: table.getTableHeader().setReorderingAllowed(false);
245: }
246:
247: /**
248: * Class declaration
249: *
250: *
251: */
252: class MapTableModel extends AbstractTableModel {
253: private Vector rowVector = new Vector();
254:
255: public MapTableModel() {
256: }
257:
258: public int getRowCount() {
259: int count = 0;
260:
261: if (rowVector != null) {
262: count = rowVector.size();
263: }
264: return count;
265: }
266:
267: public int getColumnCount() {
268: return 2;
269: }
270:
271: /**
272: * Method declaration
273: *
274: *
275: * @param columnIndex
276: *
277: * @return
278: */
279: public String getColumnName(int columnIndex) {
280: String name = new String();
281:
282: switch (columnIndex) {
283: case 0:
284: name = res.getString("Source_Directory");
285: break;
286: case 1:
287: name = res.getString("Package");
288: break;
289: }
290: return name;
291: }
292:
293: /**
294: * Method declaration
295: *
296: *
297: * @param columnIndex
298: *
299: * @return
300: */
301: public Class getColumnClass(int columnIndex) {
302: Class columnClass = null;
303: Object value = getValueAt(0, columnIndex);
304:
305: if (value != null) {
306: columnClass = value.getClass();
307: }
308: return columnClass;
309: }
310:
311: /**
312: * Method declaration
313: *
314: *
315: * @param rowIndex
316: * @param columnIndex
317: *
318: * @return
319: */
320: public boolean isCellEditable(int rowIndex, int columnIndex) {
321: return false;
322: }
323:
324: /**
325: * Method declaration
326: *
327: *
328: * @param rowIndex
329: * @param columnIndex
330: *
331: * @return
332: */
333: public Object getValueAt(int rowIndex, int columnIndex) {
334: Object value = null;
335:
336: if (!isTableEmpty()) {
337: MapRow row = (MapRow) rowVector.elementAt(rowIndex);
338: MapCell cell = new MapCell(columnIndex, row);
339:
340: value = cell.getValue();
341: }
342: return value;
343: }
344:
345: /**
346: * Method declaration
347: *
348: *
349: * @param aValue
350: * @param rowIndex
351: * @param columnIndex
352: */
353: public void setValueAt(Object aValue, int rowIndex,
354: int columnIndex) {
355: if (!isTableEmpty()) {
356: MapRow row = (MapRow) rowVector.elementAt(rowIndex);
357: MapCell cell = new MapCell(columnIndex, row);
358:
359: cell.setValue(aValue);
360: fireTableCellUpdated(columnIndex, rowIndex);
361: }
362: }
363:
364: /**
365: * Method declaration
366: *
367: *
368: * @param rowIndex
369: *
370: * @return
371: */
372: protected MapRow getRow(int rowIndex) {
373: MapRow row = null;
374:
375: if (!isTableEmpty()) {
376: if (rowVector.size() > rowIndex) {
377: row = (MapRow) rowVector.elementAt(rowIndex);
378: }
379: }
380: return row;
381: }
382:
383: protected void readMap() {
384: int rowCount = rowVector.size();
385:
386: map = new String[rowCount][2];
387: MapRow row = null;
388:
389: for (int i = 0; i < rowCount; i++) {
390: row = (MapRow) rowVector.elementAt(i);
391: map[i][0] = row.getFolder();
392: map[i][1] = row.getPackageName();
393: }
394: }
395:
396: protected void populateModel() {
397: while (getRowCount() > 0) {
398: removeRow(0);
399: }
400: if (map != null) {
401: int rowCount = map.length;
402:
403: for (int i = 0; i < rowCount; i++) {
404: addRow(map[i][0], map[i][1]);
405: }
406: }
407: }
408:
409: // /
410: // /
411: private boolean isTableEmpty() {
412: boolean empty = true;
413:
414: if (rowVector != null) {
415: if (rowVector.size() > 0) {
416: empty = false;
417: }
418: }
419: return empty;
420: }
421:
422: private void addRow(String f, String p) {
423: MapRow newRow = null;
424:
425: newRow = new MapRow(f, p);
426: rowVector.addElement(newRow);
427: int size = rowVector.size();
428:
429: fireTableDataChanged();
430: }
431:
432: private void removeRow(int index) {
433: rowVector.removeElementAt(index);
434: fireTableDataChanged();
435: }
436:
437: }
438:
439: /**
440: * Class declaration
441: *
442: *
443: * @author
444: * @version %I%, %G%
445: */
446: class MapRow {
447: private String fromFolder = new String();
448: private String toPackage = new String();
449:
450: /**
451: * Constructor declaration
452: *
453: *
454: * @param f
455: * @param p
456: */
457: public MapRow(String f, String p) {
458: fromFolder = f;
459: toPackage = p;
460: }
461:
462: /**
463: * Method declaration
464: *
465: *
466: * @return
467: */
468: public String getFolder() {
469: return fromFolder;
470: }
471:
472: /**
473: * Method declaration
474: *
475: *
476: * @param f
477: */
478: public void setFolder(String f) {
479: fromFolder = f;
480: }
481:
482: /**
483: * Method declaration
484: *
485: *
486: * @return
487: */
488: public String getPackageName() {
489: return toPackage;
490: }
491:
492: /**
493: * Method declaration
494: *
495: *
496: * @param p
497: */
498: public void setPackageName(String p) {
499: toPackage = p;
500: }
501:
502: }
503:
504: /**
505: * Class declaration
506: *
507: *
508: * @author
509: * @version %I%, %G%
510: */
511: class MapCell {
512: private MapRow row;
513: private int column;
514:
515: /**
516: * Constructor declaration
517: *
518: *
519: * @param c
520: * @param r
521: */
522: public MapCell(int c, MapRow r) {
523: column = c;
524: row = r;
525: }
526:
527: /**
528: * Method declaration
529: *
530: *
531: * @param value
532: *
533: * @return
534: */
535: public boolean setValue(Object value) {
536: boolean set = true;
537:
538: switch (column) {
539: case 0:
540: row.setFolder((String) value);
541: break;
542: case 1:
543: row.setPackageName((String) value);
544: break;
545: default:
546: set = false;
547: break;
548: }
549: return set;
550: }
551:
552: /**
553: * Method declaration
554: *
555: *
556: * @return
557: */
558: public Object getValue() {
559: Object value = null;
560:
561: switch (column) {
562: case 0:
563: value = row.getFolder();
564: break;
565: case 1:
566: value = row.getPackageName();
567: break;
568: }
569: return value;
570: }
571:
572: //
573: // PROTECTED
574: //
575: protected MapRow getRow() {
576: return row;
577: }
578:
579: }
580: }
|