View | Details | Raw Unified | Return to bug 10975
Collapse All | Expand All

(-)view/SVTableCellRenderer.java (-57 / +22 lines)
Lines 86-88 Link Here
86
  private static final Color black = getAWTColor(new HSSFColor.BLACK());
87
  private static final Color white = getAWTColor(new HSSFColor.WHITE());
88
Line 94 Link Here
94
    private Hashtable colors = HSSFColor.getIndexHash();
Lines 117-118 Link Here
117
//??        textFormatter[0x0C] = new DecimalFormat("# ?/?");
113
        textFormatter[0x0C] = new SVFractionalFormat("# ?/?");
118
//??        textFormatter[0x0D] = new DecimalFormat("# ??/??");
114
        textFormatter[0x0D] = new SVFractionalFormat("# ??/??");
119
--
Line 163 Link Here
163
        else throw new RuntimeException("Sorry. I cant handle a non decimal formatter for a decimal value :"+Integer.toHexString(index));
159
        if (textFormatter[index] instanceof SVFractionalFormat) {
164
--
160
          return ((SVFractionalFormat)textFormatter[index]).format(value);
161
        }
162
        throw new RuntimeException("Sorry. I cant handle a non decimal formatter for a decimal value :"+Integer.toHexString(index));
Lines 191-203 Link Here
191
          boolean isbold = f.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL;
190
          setFont(SVTableUtils.makeFont(f));
192
          boolean isitalics = f.getItalic();
193
194
          int fontstyle = Font.PLAIN;
195
196
          if (isbold) fontstyle = Font.BOLD;
197
          if (isitalics) fontstyle = fontstyle | Font.ITALIC;
198
199
          int fontheight = f.getFontHeightInPoints();
200
          if (fontheight == 9) fontheight = 10; //fix for stupid ol Windows
201
202
          Font font = new Font(f.getFontName(),fontstyle,fontheight);
203
          setFont(font);
204
--
Lines 206-207 Link Here
206
            setBackground(getAWTColor(s.getFillForegroundColor(), white));
193
            setBackground(SVTableUtils.getAWTColor(s.getFillForegroundColor(), SVTableUtils.white));
207
          } else setBackground(white);
194
          } else setBackground(SVTableUtils.white);
208
--
Line 209 Link Here
209
          setForeground(getAWTColor(f.getColor(), black));
196
          setForeground(SVTableUtils.getAWTColor(f.getColor(), SVTableUtils.black));
210
--
Lines 211-214 Link Here
211
          cellBorder.setBorder(getAWTColor(s.getTopBorderColor(), black),
198
          cellBorder.setBorder(SVTableUtils.getAWTColor(s.getTopBorderColor(), SVTableUtils.black),
212
                               getAWTColor(s.getRightBorderColor(), black),
199
                               SVTableUtils.getAWTColor(s.getRightBorderColor(), SVTableUtils.black),
213
                               getAWTColor(s.getBottomBorderColor(), black),
200
                               SVTableUtils.getAWTColor(s.getBottomBorderColor(), SVTableUtils.black),
214
                               getAWTColor(s.getLeftBorderColor(), black),
201
                               SVTableUtils.getAWTColor(s.getLeftBorderColor(), SVTableUtils.black),
215
--
Line 233 Link Here
233
              case HSSFCell.CELL_TYPE_FORMULA:
Line 245 Link Here
231
              case HSSFCell.CELL_TYPE_FORMULA:
Line 269 Link Here
269
          setBackground(white);
256
          setBackground(SVTableUtils.white);
270
--
Lines 275-278 Link Here
275
              cellBorder.setBorder(black,
262
              //This is the border to paint when there is no border
276
                                   black,
263
              //and the cell has focus
277
                                   black,
264
              cellBorder.setBorder(SVTableUtils.black,
278
                                   black,
265
                                   SVTableUtils.black,
279
--
266
                                   SVTableUtils.black,
267
                                   SVTableUtils.black,
Lines 326-341 Link Here
326
327
    /** This method retrieves the AWT Color representation from the colour hash table
328
     *
329
     */
330
    private final Color getAWTColor(int index, Color deflt) {
331
      HSSFColor clr = (HSSFColor)colors.get(new Integer(index));
332
      if (clr == null) return deflt;
333
      return getAWTColor(clr);
334
    }
335
336
    private static final Color getAWTColor(HSSFColor clr) {
337
      short[] rgb = clr.getTriplet();
338
      return new Color(rgb[0],rgb[1],rgb[2]);
339
    }
340
341
(-)view/SVTableModel.java (+10 lines)
Line 112 Link Here
112
  public boolean isCellEditable(int rowIndex, int columnIndex) {
113
    return true;
114
  }
115
116
  public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
117
    if (aValue != null)
118
      System.out.println("SVTableModel.setValueAt. value type = "+aValue.getClass().getName());
119
    else System.out.println("SVTableModel.setValueAt. value type = null");
120
  }
121
(-)view/SViewer.java (-30 / +13 lines)
Lines 61-62 Link Here
61
import java.net.URL;
61
import java.net.*;
62
import java.net.URLConnection;
63
--
Lines 64-65 Link Here
64
import java.io.InputStream;
63
import java.io.*;
65
import java.io.FileInputStream;
66
--
Line 103 Link Here
101
      System.exit(1);
Line 124 Link Here
124
    panel = new SViewerPanel(wb);
123
    panel = new SViewerPanel(wb, false);
125
--
Line 129 Link Here
129
  private HSSFWorkbook constructWorkbook(String filename) {
128
  private HSSFWorkbook constructWorkbook(String filename) throws FileNotFoundException, IOException {
130
--
Lines 131-132 Link Here
131
132
    try {
Lines 136-138 Link Here
136
    } catch (Exception e) {
137
      e.printStackTrace();
138
    }
Line 142 Link Here
142
  private HSSFWorkbook constructWorkbook(InputStream in) {
136
  private HSSFWorkbook constructWorkbook(InputStream in) throws IOException {
143
--
Line 145 Link Here
145
    try {
Lines 148-151 Link Here
148
149
    } catch (Exception e) {
150
      e.printStackTrace();
151
    }
Lines 177-179 Link Here
177
  private InputStream getXLSFromURL(String urlstring) {
166
  private InputStream getXLSFromURL(String urlstring) throws MalformedURLException, IOException {
178
  InputStream is = null;
179
  try {
180
--
Lines 181-183 Link Here
181
    is = url.openStream();
168
    URLConnection uc = url.openConnection();
182
  } catch (Exception e) {
169
    String field = uc.getHeaderField(0);
183
    e.printStackTrace();
170
    for (int i=0;field != null; i++) {
184
--
171
      System.out.println(field);
172
      field = uc.getHeaderField(i);
Line 185 Link Here
174
    BufferedInputStream is = new BufferedInputStream(uc.getInputStream());
(-)view/SViewerPanel.java (-22 / +204 lines)
Line 63 Link Here
63
import javax.swing.table.*;
64
import javax.swing.event.*;
Lines 64-66 Link Here
64
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
66
import org.apache.poi.hssf.usermodel.*;
65
import org.apache.poi.hssf.usermodel.HSSFSheet;
66
import org.apache.poi.hssf.usermodel.HSSFCell;
67
--
Line 76 Link Here
76
  /** This field is the magic number to convert from a Character width to a
77
   *  java pixel width.
78
   *
79
   * When the "normal" font size in a workbook changes, this effects all
80
   * of the heights and widths. Unfortunately there is no way to retrieve this
81
   * information, hence the MAGIC number.
82
   *
83
   * This number may only work for the normal style font size of Arial size 10.
84
   *
85
   */
86
  private static final int magicCharFactor = 7;
87
  /** Reference to the woorkbook that is being displayed*/
Line 77 Link Here
77
  private JTable sheets[];
89
  /** Reference to the tabs component*/
78
--
90
  private JTabbedPane sheetPane;
91
  /** Reference to the cell renderer that is used to render all cells*/
92
  private SVTableCellRenderer cellRenderer;
93
  /** Reference to the cell editor that is used to edit all cells.
94
   *  Only constructed if editing is allowed
95
   */
96
  private SVTableCellEditor cellEditor;
97
  /** Flag indicating if editing is allowed. Otherwise the viewer is in
98
   *  view only mode.
99
   */
100
  private boolean allowEdits;
Line 80 Link Here
80
  public SViewerPanel(HSSFWorkbook wb) {
103
  public SViewerPanel(HSSFWorkbook wb, boolean allowEdits) {
81
--
Line 82 Link Here
105
    this.allowEdits = allowEdits;
106
107
    initialiseGui();
108
  }
109
110
  private void initialiseGui() {
111
    cellRenderer = new SVTableCellRenderer(this.wb);
112
    if (allowEdits)
113
      cellEditor = new SVTableCellEditor(this.wb);
Line 84 Link Here
84
    JTabbedPane sheetPane = new JTabbedPane(JTabbedPane.BOTTOM);
116
    sheetPane = new JTabbedPane(JTabbedPane.BOTTOM);
85
--
117
118
    if (allowEdits)
119
      sheetPane.addMouseListener(createTabListener());
Lines 86-87 Link Here
86
    sheets = new JTable[sheetCount];
87
    SVTableCellRenderer rnd = new SVTableCellRenderer(wb);
Lines 90-93 Link Here
90
      //Construct the view of the sheet
91
      SVTableModel tm = new SVTableModel(wb.getSheetAt(i));
92
      sheets[i] = new JTable(tm);
93
      sheets[i].setDefaultRenderer(HSSFCell.class, rnd);
Line 95 Link Here
95
      sheetPane.addTab(sheetName, new JScrollPane(sheets[i]));
124
      sheetPane.addTab(sheetName, makeSheetView(wb.getSheetAt(i)));
96
--
Line 101 Link Here
130
  protected JComponent makeSheetView(HSSFSheet sheet) {
131
    JTable sheetView = new JTable(new SVTableModel(sheet));
132
    sheetView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
133
    sheetView.setDefaultRenderer(HSSFCell.class, cellRenderer);
134
    if (allowEdits)
135
      sheetView.setDefaultEditor(HSSFCell.class, cellEditor);
136
    JTableHeader header = sheetView.getTableHeader();
137
    //Dont allow column reordering
138
    header.setReorderingAllowed(false);
139
    //Only allow column resizing if editing is allowed
140
    header.setResizingAllowed(allowEdits);
141
142
    //Set the columns the correct size
143
    TableColumnModel columns = sheetView.getColumnModel();
144
    for (int i=0; i< columns.getColumnCount(); i++) {
145
      TableColumn column = columns.getColumn(i);
146
      short width = sheet.getColumnWidth((short)i);
147
      //256 is because the width is in 256ths of a character
148
      column.setPreferredWidth(width/256*magicCharFactor);
149
    }
150
151
    //Set the rows to the correct size
152
    int rows = sheet.getPhysicalNumberOfRows();
153
    Insets insets = cellRenderer.getInsets();
154
    //Need to include the insets in the calculation of the row height to use.
155
    int extraHeight = insets.bottom+insets.top;
156
    for (int i=0; i< rows; i++) {
157
      HSSFRow row = sheet.getRow(i);
158
      if (row == null) {
159
        sheetView.setRowHeight(i, (int)sheet.getDefaultRowHeightInPoints()+extraHeight);
160
      } else {
161
        sheetView.setRowHeight(i, (int)row.getHeightInPoints()+extraHeight);
162
      }
163
    }
164
165
    //Add the row header to the sheet
166
    SVRowHeader rowHeader = new SVRowHeader(sheet, sheetView, extraHeight);
167
    JScrollPane scroll = new JScrollPane( sheetView );
168
    scroll.setRowHeaderView(rowHeader);
169
    return scroll;
170
  }
171
Line 102 Link Here
173
    //JMH I am only overriding this to get a picture of the time taken to paint
Line 108 Link Here
180
  protected MouseListener createTabListener() {
181
    return new TabListener();
182
  }
183
184
  /** This class defines the default MouseListener that listens to
185
   *  mouse events in the tabbed pane
186
   *
187
   *  The default is to popup a menu when the event occurs over a tab
188
   */
189
  private class TabListener implements MouseListener {
190
    public JPopupMenu popup;
191
    public TabListener() {
192
      popup = new JPopupMenu("Sheet");
193
      popup.add(createInsertSheetAction());
194
      popup.add(createDeleteSheetAction());
195
      popup.add(createRenameSheetAction());
196
    }
197
198
    protected Action createInsertSheetAction() {
199
      return new InsertSheetAction();
200
    }
201
202
    protected Action createDeleteSheetAction() {
203
      return new DeleteSheetAction();
204
    }
205
206
    protected Action createRenameSheetAction() {
207
      return new RenameSheetAction();
208
    }
209
210
211
    /** This method will display the popup if the mouseevent is a popup event
212
     *  and the event occurred over a tab
213
     */
214
    protected void checkPopup(MouseEvent e) {
215
      if (e.isPopupTrigger()) {
216
        int tab = sheetPane.getUI().tabForCoordinate(sheetPane, e.getX(), e.getY());
217
        if (tab != -1) {
218
          popup.show(sheetPane, e.getX(), e.getY());
219
        }
220
      }
221
    }
222
223
    public void mouseClicked(MouseEvent e) {
224
      checkPopup(e);
225
    }
226
227
    public void mousePressed(MouseEvent e) {
228
      checkPopup(e);
229
    }
230
231
    public void mouseReleased(MouseEvent e) {
232
      checkPopup(e);
233
    }
234
235
    public void mouseEntered(MouseEvent e) {}
236
    public void mouseExited(MouseEvent e) {}
237
  }
238
239
  /** This class defines the action that is performed when the sheet is renamed*/
240
  private class RenameSheetAction extends AbstractAction {
241
    public RenameSheetAction() {
242
      super("Rename");
243
    }
244
245
    public void actionPerformed(ActionEvent e) {
246
      int tabIndex = sheetPane.getSelectedIndex();
247
      if (tabIndex != -1) {
248
        String newSheetName = (String)JOptionPane.showInputDialog(sheetPane, "Enter a new Sheetname", "Rename Sheet", JOptionPane.QUESTION_MESSAGE);
249
        if (newSheetName != null) {
250
          wb.setSheetName(tabIndex, newSheetName);
251
          sheetPane.setTitleAt(tabIndex, newSheetName);
252
        }
253
      }
254
    }
255
  }
256
257
  /** This class defines the action that is performed when a sheet is inserted*/
258
  private class InsertSheetAction extends AbstractAction {
259
    public InsertSheetAction() {
260
      super("Insert");
261
    }
262
263
    public void actionPerformed(ActionEvent e) {
264
      //Create a new sheet then search for the sheet and make sure that the
265
      //sheetPane shows it.
266
      HSSFSheet newSheet = wb.createSheet();
267
      for (int i=0; i<wb.getNumberOfSheets();i++) {
268
        HSSFSheet sheet = wb.getSheetAt(i);
269
        if (newSheet == sheet) {
270
          sheetPane.insertTab(wb.getSheetName(i), null, makeSheetView(sheet), null, i);
271
        }
272
      }
273
    }
274
  }
275
276
  /** This class defines the action that is performed when the sheet is deleted*/
277
  private class DeleteSheetAction extends AbstractAction {
278
    public DeleteSheetAction() {
279
      super("Delete");
280
    }
281
282
    public void actionPerformed(ActionEvent e) {
283
      int tabIndex = sheetPane.getSelectedIndex();
284
      if (tabIndex != -1) {
285
        if (JOptionPane.showConfirmDialog(sheetPane, "Are you sure that you want to delete the selected sheet", "Delete Sheet?", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
286
          wb.removeSheetAt(tabIndex);
287
          sheetPane.remove(tabIndex);
288
        }
289
      }
290
    }
291
  }
292
293
  public boolean isEditable() {
294
    return allowEdits;
295
  }
296
Line 115 Link Here
115
      SViewerPanel p = new SViewerPanel(wb);
304
      SViewerPanel p = new SViewerPanel(wb, true);
116
--
Line 131 Link Here
131
      frame.setSize(400,320);
320
      frame.setSize(800,640);
132
--

Return to bug 10975