001: /*
002: * BlobColumnRenderer.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.renderer;
013:
014: import java.awt.Color;
015: import java.awt.Component;
016: import java.awt.Font;
017: import java.awt.event.ActionEvent;
018: import java.awt.event.ActionListener;
019: import java.awt.event.MouseEvent;
020: import java.io.File;
021: import java.util.EventObject;
022: import javax.swing.AbstractCellEditor;
023: import javax.swing.JTable;
024: import javax.swing.SwingConstants;
025: import javax.swing.table.TableCellEditor;
026: import javax.swing.table.TableCellRenderer;
027: import javax.swing.table.TableColumn;
028: import javax.swing.table.TableColumnModel;
029: import workbench.WbManager;
030: import workbench.gui.WbSwingUtilities;
031: import workbench.gui.actions.WbAction;
032: import workbench.gui.components.*;
033: import workbench.resource.Settings;
034:
035: /**
036: * @author support@sql-workbench.net
037: */
038: public class BlobColumnRenderer extends AbstractCellEditor implements
039: TableCellEditor, ActionListener, TableCellRenderer, WbRenderer {
040: private BlobColumnPanel displayPanel;
041: private Object currentValue;
042: private WbTable currentTable;
043: private boolean isPrinting = false;
044: private int currentRow;
045: private int currentColumn;
046: private Color alternateColor = Settings.getInstance()
047: .getAlternateRowColor();
048: private Color nullColor = Settings.getInstance().getNullColor();
049:
050: private boolean useAlternatingColors = Settings.getInstance()
051: .getUseAlternateRowColor();
052:
053: public BlobColumnRenderer() {
054: super ();
055: this .displayPanel = new BlobColumnPanel();
056: this .displayPanel.addActionListener(this );
057: }
058:
059: public void setFont(Font aFont) {
060: this .displayPanel.setFont(aFont);
061: }
062:
063: public Component getTableCellEditorComponent(JTable table,
064: Object value, boolean isSelected, int row, int column) {
065: return getComponent(table, value, true, isSelected, row, column);
066: }
067:
068: public int getHorizontalAlignment() {
069: return SwingConstants.LEFT;
070: }
071:
072: public Component getTableCellRendererComponent(JTable table,
073: Object value, boolean isSelected, boolean hasFocus,
074: int row, int column) {
075: return getComponent(table, value, isSelected, hasFocus, row,
076: column);
077: }
078:
079: private Component getComponent(JTable table, Object value,
080: boolean isSelected, boolean hasFocus, int row, int column) {
081: if (isSelected) {
082: this .displayPanel.setForeground(table
083: .getSelectionForeground());
084: this .displayPanel.setBackground(table
085: .getSelectionBackground());
086: } else {
087: this .displayPanel.setForeground(table.getForeground());
088: if (value == null && nullColor != null) {
089: this .displayPanel.setBackground(nullColor);
090: } else {
091: if (useAlternatingColors && ((row % 2) == 1)) {
092: this .displayPanel
093: .setBackground(this .alternateColor);
094: } else {
095: this .displayPanel.setBackground(table
096: .getBackground());
097: }
098: }
099: }
100: if (hasFocus) {
101: this .displayPanel
102: .setBorder(WbSwingUtilities.FOCUSED_CELL_BORDER);
103: } else {
104: this .displayPanel.setBorder(WbSwingUtilities.EMPTY_BORDER);
105: }
106:
107: currentValue = value;
108: currentRow = row;
109: currentColumn = column;
110: currentTable = (WbTable) table;
111: displayPanel.setValue(value);
112: return displayPanel;
113: }
114:
115: public boolean isCellEditable(EventObject e) {
116: if (e instanceof MouseEvent) {
117: MouseEvent evt = (MouseEvent) e;
118: this .currentTable = (WbTable) e.getSource();
119:
120: int clickedColumn = this .currentTable.columnAtPoint(evt
121: .getPoint());
122: TableColumnModel model = this .currentTable.getColumnModel();
123: int columnOffset = 0;
124: for (int i = 0; i < clickedColumn; i++) {
125: columnOffset += model.getColumn(i).getWidth();
126: }
127: TableColumn col = model.getColumn(clickedColumn);
128: int posInCol = ((int) evt.getPoint().getX() - columnOffset);
129: int buttonStart = (col.getWidth() - displayPanel
130: .getButtonWidth());
131: boolean buttonClicked = (posInCol >= buttonStart);
132: return buttonClicked;
133: }
134: return false;
135: }
136:
137: public void setBackground(Color c) {
138: this .displayPanel.setBackground(c);
139: }
140:
141: public Object getCellEditorValue() {
142: return currentValue;
143: }
144:
145: public String getDisplayValue() {
146: return displayPanel.getLabel();
147: }
148:
149: public void setUseAlternatingColors(boolean flag) {
150: this .useAlternatingColors = flag;
151: }
152:
153: public void actionPerformed(ActionEvent e) {
154: cancelCellEditing();
155: boolean ctrlPressed = WbAction.isCtrlPressed(e);
156: boolean shiftPressed = WbAction.isShiftPressed(e);
157: BlobHandler handler = new BlobHandler();
158: if (ctrlPressed) {
159: handler.showBlobAsText(currentValue);
160: } else if (shiftPressed) {
161: handler.showBlobAsImage(currentValue);
162: } else {
163: handler.showBlobInfoDialog(WbManager.getInstance()
164: .getCurrentWindow(), currentValue);
165: }
166: File f = handler.getUploadFile();
167: if (f != null) {
168: currentTable.setValueAt(f, currentRow, currentColumn);
169: } else if (handler.isChanged()) {
170: currentTable.setValueAt(handler.getNewValue(), currentRow,
171: currentColumn);
172: } else if (handler.setToNull()) {
173: currentTable.setValueAt(null, currentRow, currentColumn);
174: }
175: }
176:
177: }
|