KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > contrib > view > SVTableCellEditor


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18 package org.apache.poi.hssf.contrib.view;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import java.text.*;
23 import java.util.*;
24
25 import javax.swing.*;
26 import javax.swing.border.*;
27 import javax.swing.table.*;
28
29 import org.apache.poi.hssf.usermodel.*;
30 import org.apache.poi.hssf.util.HSSFColor;
31
32 /**
33  * Sheet Viewer Table Cell Editor -- not commented via javadoc as it
34  * nearly completely consists of overridden methods.
35  *
36  * @author Jason Height
37  * @since 16 July 2002
38  */

39 public class SVTableCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
40   private static final Color black = getAWTColor(new HSSFColor.BLACK());
41   private static final Color white = getAWTColor(new HSSFColor.WHITE());
42   private Hashtable colors = HSSFColor.getIndexHash();
43
44
45   private HSSFWorkbook wb;
46   private JTextField editor;
47
48   private HSSFCell editorValue;
49
50
51   public SVTableCellEditor(HSSFWorkbook wb) {
52     this.wb = wb;
53     this.editor = new JTextField();
54   }
55
56
57   /**
58    * Gets the cellEditable attribute of the SVTableCellEditor object
59    *
60    * @return The cellEditable value
61    */

62   public boolean isCellEditable(java.util.EventObject JavaDoc e) {
63     if (e instanceof MouseEvent) {
64       return ((MouseEvent) e).getClickCount() >= 2;
65     }
66     return false;
67   }
68
69
70   public boolean shouldSelectCell(EventObject anEvent) {
71     return true;
72   }
73
74
75   public boolean startCellEditing(EventObject anEvent) {
76     System.out.println("Start Cell Editing");
77     return true;
78   }
79
80
81   public boolean stopCellEditing() {
82     System.out.println("Stop Cell Editing");
83     fireEditingStopped();
84     return true;
85   }
86
87
88   public void cancelCellEditing() {
89     System.out.println("Cancel Cell Editing");
90     fireEditingCanceled();
91   }
92
93
94   public void actionPerformed(ActionEvent e) {
95     System.out.println("Action performed");
96     stopCellEditing();
97   }
98
99
100   /**
101    * Gets the cellEditorValue attribute of the SVTableCellEditor object
102    *
103    * @return The cellEditorValue value
104    */

105   public Object JavaDoc getCellEditorValue() {
106     System.out.println("GetCellEditorValue");
107     //JMH Look at when this method is called. Should it return a HSSFCell?
108
return editor.getText();
109   }
110
111
112   /**
113    * Gets the tableCellEditorComponent attribute of the SVTableCellEditor object
114    *
115    * @return The tableCellEditorComponent value
116    */

117   public Component getTableCellEditorComponent(JTable table, Object JavaDoc value,
118       boolean isSelected,
119       int row,
120       int column) {
121     System.out.println("GetTableCellEditorComponent");
122     HSSFCell cell = (HSSFCell) value;
123     if (cell != null) {
124           HSSFCellStyle style = cell.getCellStyle();
125           HSSFFont f = wb.getFontAt(style.getFontIndex());
126           boolean isbold = f.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL;
127           boolean isitalics = f.getItalic();
128
129           int fontstyle = Font.PLAIN;
130
131           if (isbold) fontstyle = Font.BOLD;
132           if (isitalics) fontstyle = fontstyle | Font.ITALIC;
133
134           int fontheight = f.getFontHeightInPoints();
135           if (fontheight == 9) fontheight = 10; //fix for stupid ol Windows
136

137           Font font = new Font(f.getFontName(),fontstyle,fontheight);
138           editor.setFont(font);
139
140           if (style.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) {
141             editor.setBackground(getAWTColor(style.getFillForegroundColor(), white));
142           } else editor.setBackground(white);
143
144           editor.setForeground(getAWTColor(f.getColor(), black));
145
146
147       //Set the value that is rendered for the cell
148
switch (cell.getCellType()) {
149         case HSSFCell.CELL_TYPE_BLANK:
150           editor.setText("");
151           break;
152         case HSSFCell.CELL_TYPE_BOOLEAN:
153           if (cell.getBooleanCellValue()) {
154             editor.setText("true");
155           } else {
156             editor.setText("false");
157           }
158           break;
159         case HSSFCell.CELL_TYPE_NUMERIC:
160           editor.setText(Double.toString(cell.getNumericCellValue()));
161           break;
162         case HSSFCell.CELL_TYPE_STRING:
163           editor.setText(cell.getStringCellValue());
164           break;
165         case HSSFCell.CELL_TYPE_FORMULA:
166         default:
167           editor.setText("?");
168       }
169       switch (style.getAlignment()) {
170         case HSSFCellStyle.ALIGN_LEFT:
171         case HSSFCellStyle.ALIGN_JUSTIFY:
172         case HSSFCellStyle.ALIGN_FILL:
173           editor.setHorizontalAlignment(SwingConstants.LEFT);
174           break;
175         case HSSFCellStyle.ALIGN_CENTER:
176         case HSSFCellStyle.ALIGN_CENTER_SELECTION:
177           editor.setHorizontalAlignment(SwingConstants.CENTER);
178           break;
179         case HSSFCellStyle.ALIGN_GENERAL:
180         case HSSFCellStyle.ALIGN_RIGHT:
181           editor.setHorizontalAlignment(SwingConstants.RIGHT);
182           break;
183         default:
184           editor.setHorizontalAlignment(SwingConstants.LEFT);
185           break;
186       }
187
188     }
189     return editor;
190   }
191
192     /** This method retrieves the AWT Color representation from the colour hash table
193      *
194      */

195     private final Color getAWTColor(int index, Color deflt) {
196       HSSFColor clr = (HSSFColor)colors.get(new Integer JavaDoc(index));
197       if (clr == null) return deflt;
198       return getAWTColor(clr);
199     }
200
201     private static final Color getAWTColor(HSSFColor clr) {
202       short[] rgb = clr.getTriplet();
203       return new Color(rgb[0],rgb[1],rgb[2]);
204     }
205
206 }
207
Popular Tags