]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/SpreadsheetTable.java
Database backwards compatibility fix for fail-safe import changes
[simantics/platform.git] / bundles / org.simantics.spreadsheet.ui / src / org / simantics / spreadsheet / ui / SpreadsheetTable.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.spreadsheet.ui;
13
14 import java.awt.Font;
15 import java.awt.Point;
16 import java.awt.Rectangle;
17 import java.awt.event.KeyEvent;
18 import java.util.Arrays;
19
20 import javax.swing.DefaultListModel;
21 import javax.swing.JList;
22 import javax.swing.KeyStroke;
23 import javax.swing.event.ChangeEvent;
24 import javax.swing.event.ListSelectionEvent;
25 import javax.swing.table.TableCellEditor;
26 import javax.swing.table.TableColumn;
27 import javax.swing.table.TableColumnModel;
28
29 import org.simantics.scenegraph.INode;
30 import org.simantics.scenegraph.swing.JTableSG;
31 import org.simantics.spreadsheet.CellEditor;
32 import org.simantics.spreadsheet.ClientModel;
33 import org.simantics.spreadsheet.ClientModel.OperationMode;
34
35 public class SpreadsheetTable extends JTableSG {
36         
37         public static final String uiClassID = "SpreadsheetTableUI"; 
38         
39         final private CellEditor cellEditor;
40         private ClientModel clientModel;
41         private final DefaultListModel rowModel;
42         private final JList rowHeader;
43         
44         private static final long serialVersionUID = -4592686559195294773L;
45
46         @Override
47         public int getRowHeight() {
48             Font font = getFont();
49             if(font != null) {
50                 return font.getSize() + 4;
51             } else {
52                 return super.getRowHeight();
53             }
54         }
55         
56         
57         public SpreadsheetTable(final INode node, final CellEditor cellEditor, final ClientModel clientModel, final DefaultListModel rowModel) {
58
59             super(new ClientTableModel(clientModel), node);
60             
61             this.rowHeader = null;
62                 this.cellEditor = cellEditor;
63                 this.clientModel = clientModel;
64                 this.rowModel = rowModel;
65                 
66 //              ((ClientTableModel)getModel()).setTable(this);
67 //              
68 //        InputMap im = this.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
69 //        Action deleteAction = new AbstractAction() {
70 //            public void actionPerformed(ActionEvent ae) {
71 //                System.out.println("deleteaction");
72 //                RemoveCellHandler removeHandler = serverInterface.getAdapter(RemoveCellHandler.class);
73 //                    int[] rowSelection = getSelectedRows();
74 //                    int[] columnSelection = getSelectedColumns();
75 //                    for (int i = 0; i < columnSelection.length; i++) {
76 //                        for (int j = 0; j < rowSelection.length; j++) {
77 //                            int row = rowSelection[j];
78 //                            int column = columnSelection[i];
79 //                            System.out.println("deleteaction " + row + " " + column);
80 //                            Object cell = getValueAt(row, column);
81 ////                            RemoveHandler remove = cell.getAdapter(RemoveHandler.class);
82 ////                            remove.handle();
83 //                        }
84 //                    }
85 //            }
86 //        };
87 //        KeyStroke delete = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0);
88 //        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), delete);
89 //        getActionMap().put(im.get(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)), deleteAction);
90                 
91                 setDefaultRenderer(Object.class, new Renderer());
92                 
93                 setShowGrid(false);
94                 setRowMargin(0);
95                 
96         }
97
98         public SpreadsheetTable(final INode node, final CellEditor cellEditor, final ClientModel clientModel, final DefaultListModel rowModel, JList rowHeader) {
99
100             super(new ClientTableModel(clientModel), node);
101             
102             this.rowHeader = rowHeader;
103                 this.cellEditor = cellEditor;
104                 this.clientModel = clientModel;
105                 this.rowModel = rowModel;
106                 setDefaultRenderer(Object.class, new Renderer());
107                 
108                 setShowGrid(false);
109                 setRowMargin(0);
110                 
111         }
112         
113         @Override
114         public Rectangle getCellRect(int row, int column, boolean includeSpacing) {
115                 if (clientModel != null) {
116                         Rectangle span = clientModel.getSpan(row, column);
117                         if (span != null) {
118                                 Rectangle rect1 = super.getCellRect(span.y, span.x, includeSpacing);
119                                 Rectangle rect2 = super.getCellRect(span.y + span.height - 1, span.x + span.width - 1, includeSpacing);
120                                 return rect1.union(rect2);
121                         }
122                 }
123                 return super.getCellRect(row, column, includeSpacing);
124         }
125
126         @Override
127     public String getUIClassID() {
128         return uiClassID;
129     }   
130         
131         @Override 
132         public int rowAtPoint(Point p) {
133                 int row = super.rowAtPoint(p);
134                 if (row == -1) {
135                         return -1;
136                 }
137                 int column = super.columnAtPoint(p);
138                 Rectangle span = clientModel.getSpan(row, column);
139                 if (span != null) {
140                         return span.y;
141                 } else {
142                         return row;
143                 }
144         }
145         
146         @Override
147         public int columnAtPoint(Point p) {
148                 int column = super.columnAtPoint(p);
149                 if (column == -1) {
150                         return -1;
151                 }
152                 int row = super.rowAtPoint(p);
153                 Rectangle span = clientModel.getSpan(row, column);
154                 if (span != null) {
155                         return span.x;
156                 } else {
157                         return column;
158                 }
159         }
160
161         @Override
162         public void valueChanged(ListSelectionEvent e) {
163                 super.valueChanged(e);
164         repaint();
165         }
166
167         @Override
168         public void columnSelectionChanged(ListSelectionEvent e) {
169                 super.columnSelectionChanged(e);
170         repaint();
171         }
172
173         
174         public ClientModel getClientModel() {
175                 return clientModel;
176         }
177
178         // wrapper function for SpreadsheetTableUI
179         protected int rowAtPointWithoutSpan(Point p) {
180                 return super.rowAtPoint(p);
181         }       
182
183         // wrapper function for SpreadsheetTableUI      
184         protected int columnAtPointWithoutSpan(Point p) {
185                 return super.columnAtPoint(p);
186         }       
187         
188         @Override
189     protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
190             // turn off edit but still can cause actions
191             if (e.getKeyCode()==KeyEvent.VK_DELETE) {
192                 //System.out.println("processKeyBinding delete");
193                 this.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);
194                 boolean retvalue = super.processKeyBinding(ks, e, condition, pressed);
195                 this.putClientProperty("JTable.autoStartsEdit", Boolean.TRUE);
196                 return retvalue;
197             }
198             boolean retValue = super.processKeyBinding(ks, e, condition, pressed);
199             return retValue;
200         }
201         
202         @Override
203         public TableCellEditor getCellEditor(int row, int column) {
204                 
205 //              CellEditor editor = serverInterface.getAdapter(CellEditor.class);
206                 if(cellEditor == null) return null;
207                 
208                 boolean editable = false;
209                 OperationMode mode = clientModel.getPropertyAt(ClientModel.MODE, ClientModel.MODE_CURRENT);
210                 if (!OperationMode.OPERATION.equals(mode))
211                     editable = true;
212                     
213                 
214                 CellValue cellValue = (CellValue)getValueAt(row, column);
215                 editable = cellValue.editable;
216                 if (editable) {
217                 String initial = ((CellValue)getValueAt(row, column)).label;
218                 return new TextTableCellEditor(row, column, initial, cellEditor, clientModel);
219                 } else {
220                     return null;
221                 }
222                 
223         }
224         
225         @Override
226         public void editingStopped(ChangeEvent e) {
227         // Take in the new value
228             SpreadsheetCellEditor editor = (SpreadsheetCellEditor)getCellEditor();
229         if (editor != null) {
230                 int[] rc = getSelectedRows();
231             editor.commit();
232             removeEditor();
233             if(rc.length == 1 && rc[0] < getRowCount() - 1)
234                 setRowSelectionInterval(rc[0]+1, rc[0]+1);
235         }
236         }
237
238         public void applyRowLabels(String[] rowLabels) {
239         
240                 if(Spreadsheet.DEBUG) System.out.println("Apply row labels: " + Arrays.toString(rowLabels));
241                 
242                 rowModel.ensureCapacity(rowLabels.length);
243                 int i=0;
244                 for(;i<rowLabels.length && i<rowModel.size();i++) {
245                         rowModel.setElementAt(rowLabels[i], i);
246                 }
247                 for(;i<rowLabels.length;i++) {
248                         rowModel.addElement(rowLabels[i]);
249                 }
250         
251         }
252
253         public void applyHeaderSizes(ClientModel clientModel) {
254                 
255                 // TODO: initialization haxx
256                 if(clientModel == null) return;
257                 
258                 int[] rows = clientModel.getPropertyAt(ClientModel.HEADERS, ClientModel.HEADERS_ROW_HEIGHTS);
259                 int[] cols = clientModel.getPropertyAt(ClientModel.HEADERS, ClientModel.HEADERS_COL_WIDTHS);
260                 
261                 for(int i = 0; i < getColumnModel().getColumnCount() && i < cols.length ; i++) { 
262                         TableColumn column = getColumnModel().getColumn(i);
263                         int preferred = cols[i];
264                         if(preferred > 0)
265                                 column.setPreferredWidth(preferred); 
266                 }
267
268                 if(rowHeader!=null)
269                         rowHeader.setCellRenderer(new RowHeaderRenderer(this));
270                 
271                 for(int i = 0; i<getRowCount() && i<rows.length ;i++) {
272                         if (rows[i] > 0) {
273                                 setRowHeight(i, rows[i]);
274                         }
275                 }
276         }
277         
278         @Override
279         public void createDefaultColumnsFromModel() {
280                 super.createDefaultColumnsFromModel();
281                 applyHeaderSizes(clientModel);
282         }
283         
284         @Override
285         protected TableColumnModel createDefaultColumnModel() {
286                 TableColumnModel model = super.createDefaultColumnModel();
287                 model.setColumnMargin(0);
288                 return model; 
289         }
290         
291 }