001: package org.swingml.tablebrowser.ext;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.util.*;
006: import java.util.List;
007:
008: import javax.swing.*;
009: import javax.swing.table.*;
010:
011: import org.swingml.*;
012: import org.swingml.component.*;
013: import org.swingml.event.*;
014: import org.swingml.model.*;
015: import org.swingml.model.TableColumnModel;
016:
017: /**
018: * @author NumberSix
019: */
020: public class IconComponentCellEditor extends AbstractCellEditor
021: implements TableCellEditor, TableCellRenderer {
022:
023: private class IconMouseListener implements MouseListener {
024:
025: private IconModel model;
026: private TableBrowserComponent tableBrowser;
027:
028: public IconMouseListener(IconModel iconModel,
029: TableBrowserComponent table) {
030: model = iconModel;
031: tableBrowser = table;
032: }
033:
034: public void mouseClicked(MouseEvent e) {
035: String eventName = Constants.MOUSE_SINGLE_CLICKED;
036: switch (e.getClickCount()) {
037: case 1:
038: eventName = Constants.MOUSE_SINGLE_CLICKED;
039: break;
040: case 2:
041: eventName = Constants.MOUSE_DOUBLE_CLICKED;
042: break;
043: }
044: EventHandler.getInstance().handleEvent(model, eventName,
045: tableBrowser);
046: }
047:
048: public void mouseEntered(MouseEvent e) {
049: }
050:
051: public void mouseExited(MouseEvent e) {
052: }
053:
054: public void mousePressed(MouseEvent e) {
055: }
056:
057: public void mouseReleased(MouseEvent e) {
058: }
059: }
060:
061: public IconComponentCellEditor() {
062: }
063:
064: public Object getCellEditorValue() {
065: return null;
066: }
067:
068: public Component getTableCellEditorComponent(JTable table,
069: Object value, boolean isSelected, int row, int column) {
070: return getTableCellRendererComponent(table, value, isSelected,
071: true, row, column);
072:
073: }
074:
075: public Component getTableCellRendererComponent(JTable table,
076: Object value, boolean isSelected, boolean hasFocus,
077: int row, int column) {
078: JPanel result = new JPanel();
079: result.setBackground(Color.WHITE);
080: result.setBorder(BorderFactory.createEmptyBorder());
081: if (isSelected || hasFocus) {
082: result.setBackground(table.getSelectionBackground());
083: result.setForeground(table.getSelectionForeground());
084: }
085: FlowLayout layout = (FlowLayout) result.getLayout();
086:
087: final TableBrowserComponent tableBrowser = (TableBrowserComponent) table;
088: final JTableModel model = (JTableModel) table.getModel();
089: TableDataModel cellModel = model.getDataModel(tableBrowser
090: .convertRowIndexToModel(row), tableBrowser
091: .convertColumnIndexToModel(column));
092:
093: if (!isSelected) {
094: // Do we have foreground/background colors to set?
095: if (cellModel.getBackground() != null) {
096: result.setBackground(cellModel.getBackground());
097: }
098:
099: if (cellModel.getForeground() != null) {
100: result.setForeground(cellModel.getForeground());
101: }
102: }
103:
104: // Look at column model and default this to the column model's ALIGN attribute instead of hard coding it.
105: int columnInModel = table.convertColumnIndexToModel(column);
106: TableColumnModel columnModel = (TableColumnModel) model
107: .getColumns().get(columnInModel);
108: String columnAlignment = columnModel.getAlignment();
109: layout.setAlignment(FlowLayout.LEFT);
110: if (columnAlignment != null) {
111: if (columnAlignment.equals(Constants.CENTER)) {
112: layout.setAlignment(FlowLayout.CENTER);
113: } else if (columnAlignment.equals(Constants.RIGHT)) {
114: layout.setAlignment(FlowLayout.RIGHT);
115: }
116: }
117:
118: layout.setVgap(0);
119: result.setLayout(layout);
120:
121: // add this to fire events on the panel
122: result.addMouseListener(new MouseListener() {
123:
124: public void mouseClicked(MouseEvent e) {
125: String eventName = Constants.MOUSE_SINGLE_CLICKED;
126: switch (e.getClickCount()) {
127: case 1:
128: eventName = Constants.MOUSE_SINGLE_CLICKED;
129: break;
130: case 2:
131: eventName = Constants.MOUSE_DOUBLE_CLICKED;
132: break;
133: }
134: EventHandler.getInstance().handleEvent(model,
135: eventName, tableBrowser);
136: }
137:
138: public void mouseEntered(MouseEvent e) {
139: }
140:
141: public void mouseExited(MouseEvent e) {
142: }
143:
144: public void mousePressed(MouseEvent e) {
145: }
146:
147: public void mouseReleased(MouseEvent e) {
148: }
149: });
150:
151: if (cellModel.hasIcons()) {
152: List iconModels = cellModel.getChildren();
153: if (iconModels != null && iconModels.size() > 0) {
154: Iterator schmiterator = iconModels.iterator();
155: while (schmiterator.hasNext()) {
156: IconModel iconModel = (IconModel) schmiterator
157: .next();
158: JLabel icon = new JLabel();
159: icon.addMouseListener(new IconMouseListener(
160: iconModel, tableBrowser));
161:
162: // set icon
163: if (iconModel.getIcon() != null) {
164: ImageIcon icon2 = IconFactory
165: .getIcon(iconModel);
166: if (icon2 != null) {
167: icon.setIcon(icon2);
168: }
169: } else {
170: icon.setText(iconModel.getText());
171: }
172:
173: if (!isSelected) {
174: // Do we have foreground/background colors to set?
175: if (cellModel.getForeground() != null) {
176: icon.setForeground(cellModel
177: .getForeground());
178: }
179:
180: if (iconModel.getForeground() != null) {
181: icon.setForeground(iconModel
182: .getForeground());
183: }
184: }
185:
186: if (iconModel.getTooltip() != null
187: && iconModel.getTooltip().length() > 0) {
188: icon.setToolTipText(iconModel.getTooltip());
189: }
190:
191: Font font = table.getFont();
192: Font iconFont = new Font(font.getName(),
193: Font.PLAIN, font.getSize());
194: icon.setFont(iconFont);
195:
196: if (isSelected || hasFocus) {
197: icon.setBackground(table
198: .getSelectionBackground());
199: icon.setForeground(table
200: .getSelectionForeground());
201: }
202:
203: if (!table.isEnabled()) {
204: icon.setForeground(Color.LIGHT_GRAY);
205: }
206:
207: result.add(icon);
208: }
209: }
210: }
211:
212: return result;
213: }
214: }
|