001: package org.swingml.tablebrowser.ext;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.net.*;
006: import java.util.*;
007:
008: import javax.swing.*;
009: import javax.swing.event.*;
010: import javax.swing.table.*;
011:
012: import org.swingml.*;
013: import org.swingml.model.*;
014:
015: /**
016: * @author Crosslogic
017: */
018: public class TableBrowserCellLabelEditor implements TableCellEditor {
019:
020: static WeakHashMap iconCache;
021: TableCellEditor originalEditor;
022:
023: public TableBrowserCellLabelEditor(TableCellEditor anEditor) {
024: this .originalEditor = anEditor;
025: }
026:
027: public void addCellEditorListener(CellEditorListener listener) {
028: originalEditor.addCellEditorListener(listener);
029: }
030:
031: public void cancelCellEditing() {
032: originalEditor.cancelCellEditing();
033: }
034:
035: public Object getCellEditorValue() {
036: return originalEditor.getCellEditorValue();
037: }
038:
039: protected Icon getIconForUrl(String url) {
040: Icon result = null;
041: if (iconCache == null) {
042: iconCache = new WeakHashMap();
043: }
044: if (iconCache.containsKey(url)) {
045: result = (Icon) iconCache.get(url);
046: } else {
047: try {
048: result = new ImageIcon(new URL(url));
049: iconCache.put(url, result);
050: } catch (MalformedURLException e) {
051: }
052: }
053: return result;
054: }
055:
056: public Component getTableCellEditorComponent(JTable table,
057: Object value, boolean isSelected, final int row,
058: final int column) {
059: final Component result = originalEditor
060: .getTableCellEditorComponent(table, value, isSelected,
061: row, column);
062: if (result instanceof JTextField) {
063: final JTextField editor = (JTextField) result;
064: final TableBrowserComponent tableBrowser = (TableBrowserComponent) table;
065: TableBrowserModel model = (TableBrowserModel) tableBrowser
066: .getModel();
067: TableDataModel dataModel = model.getModelAt(row, column);
068:
069: // Set alignment
070: int alignment = JLabel.LEFT;
071: if (dataModel.getAlignment().equalsIgnoreCase(
072: Constants.RIGHT)) {
073: alignment = JLabel.RIGHT;
074: } else if (dataModel.getAlignment().equalsIgnoreCase(
075: Constants.CENTER)) {
076: alignment = JLabel.CENTER;
077: }
078: editor.setHorizontalAlignment(alignment);
079:
080: // set auto-select feature
081: if (dataModel.shoudlAutoSelectOnEdit()) {
082: editor.selectAll();
083: }
084:
085: // Do we have any custom key navigation to handle?
086: ITableBrowserCellNavigationManager cellManager = tableBrowser
087: .getCellManager();
088: if (cellManager != null) {
089: String key = "cellLabelEditorKeyStrokeHandler";
090:
091: // bind keystrokes for this manager
092: InputMap inputs = editor
093: .getInputMap(JComponent.WHEN_FOCUSED);
094: KeyStroke[] keyStrokes = cellManager.getKeyStrokes();
095: for (int x = 0; x < keyStrokes.length; x++) {
096: KeyStroke keyStroke = keyStrokes[x];
097: inputs.put(keyStroke, key);
098: }
099:
100: // Set up the custom action to handle the cell editing completion
101: AbstractAction myAction = new AbstractAction() {
102:
103: public void actionPerformed(ActionEvent e) {
104: stopCellEditing();
105: }
106: };
107: editor.getActionMap().put(key, myAction);
108: }
109: }
110:
111: return result;
112: }
113:
114: public boolean isCellEditable(EventObject anEvent) {
115: return originalEditor.isCellEditable(anEvent);
116: }
117:
118: public void removeCellEditorListener(CellEditorListener l) {
119: originalEditor.removeCellEditorListener(l);
120: }
121:
122: public boolean shouldSelectCell(EventObject anEvent) {
123: return originalEditor.shouldSelectCell(anEvent);
124: }
125:
126: public boolean stopCellEditing() {
127: return originalEditor.stopCellEditing();
128: }
129:
130: }
|