001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.contrib.view;
019:
020: import java.awt.*;
021: import java.awt.event.*;
022: import java.text.*;
023: import java.util.*;
024:
025: import javax.swing.*;
026: import javax.swing.border.*;
027: import javax.swing.table.*;
028:
029: import org.apache.poi.hssf.usermodel.*;
030: import org.apache.poi.hssf.util.HSSFColor;
031:
032: /**
033: * Sheet Viewer Table Cell Editor -- not commented via javadoc as it
034: * nearly completely consists of overridden methods.
035: *
036: * @author Jason Height
037: * @since 16 July 2002
038: */
039: public class SVTableCellEditor extends AbstractCellEditor implements
040: TableCellEditor, ActionListener {
041: private static final Color black = getAWTColor(new HSSFColor.BLACK());
042: private static final Color white = getAWTColor(new HSSFColor.WHITE());
043: private Hashtable colors = HSSFColor.getIndexHash();
044:
045: private HSSFWorkbook wb;
046: private JTextField editor;
047:
048: private HSSFCell editorValue;
049:
050: public SVTableCellEditor(HSSFWorkbook wb) {
051: this .wb = wb;
052: this .editor = new JTextField();
053: }
054:
055: /**
056: * Gets the cellEditable attribute of the SVTableCellEditor object
057: *
058: * @return The cellEditable value
059: */
060: public boolean isCellEditable(java.util.EventObject e) {
061: if (e instanceof MouseEvent) {
062: return ((MouseEvent) e).getClickCount() >= 2;
063: }
064: return false;
065: }
066:
067: public boolean shouldSelectCell(EventObject anEvent) {
068: return true;
069: }
070:
071: public boolean startCellEditing(EventObject anEvent) {
072: System.out.println("Start Cell Editing");
073: return true;
074: }
075:
076: public boolean stopCellEditing() {
077: System.out.println("Stop Cell Editing");
078: fireEditingStopped();
079: return true;
080: }
081:
082: public void cancelCellEditing() {
083: System.out.println("Cancel Cell Editing");
084: fireEditingCanceled();
085: }
086:
087: public void actionPerformed(ActionEvent e) {
088: System.out.println("Action performed");
089: stopCellEditing();
090: }
091:
092: /**
093: * Gets the cellEditorValue attribute of the SVTableCellEditor object
094: *
095: * @return The cellEditorValue value
096: */
097: public Object getCellEditorValue() {
098: System.out.println("GetCellEditorValue");
099: //JMH Look at when this method is called. Should it return a HSSFCell?
100: return editor.getText();
101: }
102:
103: /**
104: * Gets the tableCellEditorComponent attribute of the SVTableCellEditor object
105: *
106: * @return The tableCellEditorComponent value
107: */
108: public Component getTableCellEditorComponent(JTable table,
109: Object value, boolean isSelected, int row, int column) {
110: System.out.println("GetTableCellEditorComponent");
111: HSSFCell cell = (HSSFCell) value;
112: if (cell != null) {
113: HSSFCellStyle style = cell.getCellStyle();
114: HSSFFont f = wb.getFontAt(style.getFontIndex());
115: boolean isbold = f.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL;
116: boolean isitalics = f.getItalic();
117:
118: int fontstyle = Font.PLAIN;
119:
120: if (isbold)
121: fontstyle = Font.BOLD;
122: if (isitalics)
123: fontstyle = fontstyle | Font.ITALIC;
124:
125: int fontheight = f.getFontHeightInPoints();
126: if (fontheight == 9)
127: fontheight = 10; //fix for stupid ol Windows
128:
129: Font font = new Font(f.getFontName(), fontstyle, fontheight);
130: editor.setFont(font);
131:
132: if (style.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) {
133: editor.setBackground(getAWTColor(style
134: .getFillForegroundColor(), white));
135: } else
136: editor.setBackground(white);
137:
138: editor.setForeground(getAWTColor(f.getColor(), black));
139:
140: //Set the value that is rendered for the cell
141: switch (cell.getCellType()) {
142: case HSSFCell.CELL_TYPE_BLANK:
143: editor.setText("");
144: break;
145: case HSSFCell.CELL_TYPE_BOOLEAN:
146: if (cell.getBooleanCellValue()) {
147: editor.setText("true");
148: } else {
149: editor.setText("false");
150: }
151: break;
152: case HSSFCell.CELL_TYPE_NUMERIC:
153: editor.setText(Double.toString(cell
154: .getNumericCellValue()));
155: break;
156: case HSSFCell.CELL_TYPE_STRING:
157: editor.setText(cell.getRichStringCellValue()
158: .getString());
159: break;
160: case HSSFCell.CELL_TYPE_FORMULA:
161: default:
162: editor.setText("?");
163: }
164: switch (style.getAlignment()) {
165: case HSSFCellStyle.ALIGN_LEFT:
166: case HSSFCellStyle.ALIGN_JUSTIFY:
167: case HSSFCellStyle.ALIGN_FILL:
168: editor.setHorizontalAlignment(SwingConstants.LEFT);
169: break;
170: case HSSFCellStyle.ALIGN_CENTER:
171: case HSSFCellStyle.ALIGN_CENTER_SELECTION:
172: editor.setHorizontalAlignment(SwingConstants.CENTER);
173: break;
174: case HSSFCellStyle.ALIGN_GENERAL:
175: case HSSFCellStyle.ALIGN_RIGHT:
176: editor.setHorizontalAlignment(SwingConstants.RIGHT);
177: break;
178: default:
179: editor.setHorizontalAlignment(SwingConstants.LEFT);
180: break;
181: }
182:
183: }
184: return editor;
185: }
186:
187: /** This method retrieves the AWT Color representation from the colour hash table
188: *
189: */
190: private final Color getAWTColor(int index, Color deflt) {
191: HSSFColor clr = (HSSFColor) colors.get(new Integer(index));
192: if (clr == null)
193: return deflt;
194: return getAWTColor(clr);
195: }
196:
197: private static final Color getAWTColor(HSSFColor clr) {
198: short[] rgb = clr.getTriplet();
199: return new Color(rgb[0], rgb[1], rgb[2]);
200: }
201:
202: }
|