You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SVTableCellEditor.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.view;
  16. import java.awt.*;
  17. import java.awt.event.*;
  18. import java.util.*;
  19. import javax.swing.*;
  20. import javax.swing.table.*;
  21. import org.apache.poi.hssf.usermodel.*;
  22. import org.apache.poi.hssf.util.HSSFColor;
  23. /**
  24. * Sheet Viewer Table Cell Editor -- not commented via javadoc as it
  25. * nearly completely consists of overridden methods.
  26. *
  27. * @author Jason Height
  28. */
  29. public class SVTableCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
  30. private static final Color black = getAWTColor(new HSSFColor.BLACK());
  31. private static final Color white = getAWTColor(new HSSFColor.WHITE());
  32. private Hashtable colors = HSSFColor.getIndexHash();
  33. private HSSFWorkbook wb;
  34. private JTextField editor;
  35. private HSSFCell editorValue;
  36. public SVTableCellEditor(HSSFWorkbook wb) {
  37. this.wb = wb;
  38. this.editor = new JTextField();
  39. }
  40. /**
  41. * Gets the cellEditable attribute of the SVTableCellEditor object
  42. *
  43. * @return The cellEditable value
  44. */
  45. public boolean isCellEditable(java.util.EventObject e) {
  46. if (e instanceof MouseEvent) {
  47. return ((MouseEvent) e).getClickCount() >= 2;
  48. }
  49. return false;
  50. }
  51. public boolean shouldSelectCell(EventObject anEvent) {
  52. return true;
  53. }
  54. public boolean startCellEditing(EventObject anEvent) {
  55. System.out.println("Start Cell Editing");
  56. return true;
  57. }
  58. public boolean stopCellEditing() {
  59. System.out.println("Stop Cell Editing");
  60. fireEditingStopped();
  61. return true;
  62. }
  63. public void cancelCellEditing() {
  64. System.out.println("Cancel Cell Editing");
  65. fireEditingCanceled();
  66. }
  67. public void actionPerformed(ActionEvent e) {
  68. System.out.println("Action performed");
  69. stopCellEditing();
  70. }
  71. /**
  72. * Gets the cellEditorValue attribute of the SVTableCellEditor object
  73. *
  74. * @return The cellEditorValue value
  75. */
  76. public Object getCellEditorValue() {
  77. System.out.println("GetCellEditorValue");
  78. //JMH Look at when this method is called. Should it return a HSSFCell?
  79. return editor.getText();
  80. }
  81. /**
  82. * Gets the tableCellEditorComponent attribute of the SVTableCellEditor object
  83. *
  84. * @return The tableCellEditorComponent value
  85. */
  86. public Component getTableCellEditorComponent(JTable table, Object value,
  87. boolean isSelected,
  88. int row,
  89. int column) {
  90. System.out.println("GetTableCellEditorComponent");
  91. HSSFCell cell = (HSSFCell) value;
  92. if (cell != null) {
  93. HSSFCellStyle style = cell.getCellStyle();
  94. HSSFFont f = wb.getFontAt(style.getFontIndex());
  95. boolean isbold = f.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL;
  96. boolean isitalics = f.getItalic();
  97. int fontstyle = Font.PLAIN;
  98. if (isbold) fontstyle = Font.BOLD;
  99. if (isitalics) fontstyle = fontstyle | Font.ITALIC;
  100. int fontheight = f.getFontHeightInPoints();
  101. if (fontheight == 9) fontheight = 10; //fix for stupid ol Windows
  102. Font font = new Font(f.getFontName(),fontstyle,fontheight);
  103. editor.setFont(font);
  104. if (style.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) {
  105. editor.setBackground(getAWTColor(style.getFillForegroundColor(), white));
  106. } else editor.setBackground(white);
  107. editor.setForeground(getAWTColor(f.getColor(), black));
  108. //Set the value that is rendered for the cell
  109. switch (cell.getCellType()) {
  110. case HSSFCell.CELL_TYPE_BLANK:
  111. editor.setText("");
  112. break;
  113. case HSSFCell.CELL_TYPE_BOOLEAN:
  114. if (cell.getBooleanCellValue()) {
  115. editor.setText("true");
  116. } else {
  117. editor.setText("false");
  118. }
  119. break;
  120. case HSSFCell.CELL_TYPE_NUMERIC:
  121. editor.setText(Double.toString(cell.getNumericCellValue()));
  122. break;
  123. case HSSFCell.CELL_TYPE_STRING:
  124. editor.setText(cell.getRichStringCellValue().getString());
  125. break;
  126. case HSSFCell.CELL_TYPE_FORMULA:
  127. default:
  128. editor.setText("?");
  129. }
  130. switch (style.getAlignment()) {
  131. case HSSFCellStyle.ALIGN_LEFT:
  132. case HSSFCellStyle.ALIGN_JUSTIFY:
  133. case HSSFCellStyle.ALIGN_FILL:
  134. editor.setHorizontalAlignment(SwingConstants.LEFT);
  135. break;
  136. case HSSFCellStyle.ALIGN_CENTER:
  137. case HSSFCellStyle.ALIGN_CENTER_SELECTION:
  138. editor.setHorizontalAlignment(SwingConstants.CENTER);
  139. break;
  140. case HSSFCellStyle.ALIGN_GENERAL:
  141. case HSSFCellStyle.ALIGN_RIGHT:
  142. editor.setHorizontalAlignment(SwingConstants.RIGHT);
  143. break;
  144. default:
  145. editor.setHorizontalAlignment(SwingConstants.LEFT);
  146. break;
  147. }
  148. }
  149. return editor;
  150. }
  151. /** This method retrieves the AWT Color representation from the colour hash table
  152. *
  153. */
  154. private final Color getAWTColor(int index, Color deflt) {
  155. HSSFColor clr = (HSSFColor)colors.get(Integer.valueOf(index));
  156. if (clr == null) return deflt;
  157. return getAWTColor(clr);
  158. }
  159. private static final Color getAWTColor(HSSFColor clr) {
  160. short[] rgb = clr.getTriplet();
  161. return new Color(rgb[0],rgb[1],rgb[2]);
  162. }
  163. }