1 /*******************************************************************************
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.spreadsheet.ui;
13 import java.awt.Color;
15 import java.util.concurrent.atomic.AtomicBoolean;
17 import javax.swing.table.DefaultTableModel;
19 import org.simantics.spreadsheet.ClientModel;
20 import org.simantics.spreadsheet.ClientModel.ClientModelListener;
21 import org.simantics.spreadsheet.ClientModel.OperationMode;
22 import org.simantics.spreadsheet.graph.SpreadsheetStyle;
23 import org.simantics.spreadsheet.util.SpreadsheetUtils;
24 import org.simantics.ui.colors.Colors;
25 import org.simantics.ui.fonts.Fonts;
26 import org.simantics.utils.threads.AWTThread;
28 final public class ClientTableModel extends DefaultTableModel {
30 private static final long serialVersionUID = 226747623479756815L;
31 private final ClientModel model;
33 public ClientTableModel(ClientModel model) {
37 public void setModel(final SpreadsheetModel model) {
39 final SpreadsheetTable table = model.getTable();
41 this.model.addListener(new ClientModelListener() {
43 private AtomicBoolean dirtyStructure = new AtomicBoolean(false);
44 private AtomicBoolean dirtyHeaderSizes = new AtomicBoolean(false);
45 private AtomicBoolean dirtyValues = new AtomicBoolean(false);
46 private AtomicBoolean dirtySources = new AtomicBoolean(false);
49 public void columnLabels(String[] labels) {
50 dirtyStructure.set(true);
54 public void rowLabels(String[] labels) {
55 dirtyStructure.set(true);
59 public void rows(int amount) {
60 dirtyStructure.set(true);
64 public void columns(int amount) {
65 dirtyStructure.set(true);
69 public void sources(final String[] available, final String current) {
70 dirtySources.set(true);
74 public void columnWidths(int[] widths) {
75 dirtyHeaderSizes.set(true);
79 public void cleared(String location) {
80 dirtyValues.set(true);
84 public void propertyChange(String location, String property, Object value) {
85 dirtyValues.set(true);
91 final boolean structure = dirtyStructure.compareAndSet(true, false);
92 final boolean values = dirtyValues.compareAndSet(true, false);
93 final boolean headerSizes = dirtyHeaderSizes.compareAndSet(true, false);
94 final boolean sources = dirtySources.compareAndSet(true, false);
97 AWTThread.getThreadAccess().asyncExec(new Runnable() {
107 if (Spreadsheet.DEBUG)
108 System.out.println("ClientTableModel.ClientModelListener.flush: structure=" + structure + ", values=" + values + ", headerSizes=" + headerSizes);
110 if(structure || values || headerSizes) {
112 AWTThread.getThreadAccess().asyncExec(new Runnable() {
117 int[] cc = table.getSelectedColumns();
118 int[] rc = table.getSelectedRows();
120 if(structure) fireTableStructureChanged();
121 if(values) fireTableDataChanged();
124 table.applyHeaderSizes(ClientTableModel.this.model);
126 if(rc.length == 1 && cc.length == 1) {
127 table.setColumnSelectionInterval(cc[0], cc[0]);
128 table.setRowSelectionInterval(rc[0], rc[0]);
144 public Object getValueAt(int row, int column) {
145 String location = SpreadsheetUtils.cellName(row, column);
147 // System.out.println("CellValue for location " + location);
149 Color foregroundColor = null;
150 Color backgroundColor = null;
154 String formatString = null;
156 SpreadsheetStyle style = model.getPropertyAt(location, "style");
158 foregroundColor = style.foreground != null ? Colors.awt(style.foreground) : null;
159 backgroundColor = style.background != null ? Colors.awt(style.background) : null;
160 font = style.font != null ? Fonts.awt(style.font) : null;
161 alignValue = style.align;
162 borderValue = style.border;
164 formatString = style.formatString;
165 formatIndex = style.formatIndex;
168 Boolean editable = model.getPropertyAt(location, "editable");
169 boolean editableValue = editable != null ? editable : false;
171 OperationMode currentMode = model.getPropertyAt(ClientModel.MODE, ClientModel.MODE_CURRENT);
172 if (!OperationMode.OPERATION.equals(currentMode))
173 editableValue = true;
175 return new CellValue(SpreadsheetUtils.getFormattedLabel(model, row, column, formatIndex, formatString), font, foregroundColor, backgroundColor, borderValue, alignValue, editableValue);
179 public int getColumnCount() {
180 return model.getColumns();
184 public String getColumnName(int column) {
185 String[] names = model.getPropertyAt(ClientModel.HEADERS, ClientModel.HEADERS_COL_LABELS);
186 if(column < names.length) return names[column];
187 else return super.getColumnName(column);
191 public int getRowCount() {
192 //TODO: haxx for initialization
193 if(model == null) return 0;
194 return model.getRows();
197 // void applyHeaderSizes() {
201 //// synchronized void applyDimensions() {
203 //// boolean fitRows = model.getPropertyAt(ClientModel.DIMENSIONS, ClientModel.DIMENSIONS_FIT_ROWS);
204 //// boolean fitColumns = model.getPropertyAt(ClientModel.DIMENSIONS, ClientModel.DIMENSIONS_FIT_COLS);
206 //// int currentRows = getRowCount();
207 //// int currentCols = getColumnCount();
209 //// int maxRow = model.getRows();
210 //// int maxColumn = model.getColumns();
212 //// int rowCount = model.getPropertyAt(ClientModel.DIMENSIONS, ClientModel.DIMENSIONS_ROW_COUNT);
213 //// int columnCount = model.getPropertyAt(ClientModel.DIMENSIONS, ClientModel.DIMENSIONS_COL_COUNT);
215 //// if(Spreadsheet.DEBUG) {
216 //// System.out.println("SimpleContainerTableModel.applyDimensions " + Thread.currentThread().getName());
217 //// System.out.println("-current model rows " + currentRows);
218 //// System.out.println("-current model cols " + currentCols);
219 ////// if(listener != null) {
220 ////// System.out.println("-current table rows " + table.getRowModel().getSize());
221 ////// System.out.println("-current table cols " + table.getColumnModel().getColumnCount());
223 //// System.out.println("-target rows " + rowCount);
224 //// System.out.println("-target cols " + columnCount);
225 //// System.out.println("-fit rows " + fitRows);
226 //// System.out.println("-fit cols " + fitColumns);
230 //// if(maxRow != currentRows) {
231 //// setRowCount(maxRow);
232 //// if(table != null) table.getRowModel().setSize(maxRow);
233 //// setColumnWidths();
236 //// if(rowCount != currentRows) {
237 //// setRowCount(rowCount);
238 //// if(table != null) table.getRowModel().setSize(rowCount);
239 //// setColumnWidths();
242 //// if(fitColumns) {
243 //// if(maxColumn != currentCols) {
244 //// setColumnCount(maxColumn);
245 ////// applyLabels();
246 //// setColumnWidths();
249 //// if(columnCount != currentCols) {
250 //// setColumnCount(columnCount);
251 ////// applyLabels();
252 //// setColumnWidths();
258 //// private synchronized void applyLabels() {
260 //// if(Spreadsheet.DEBUG) System.out.println("SimpleContainerTableModel.applyLabels");
262 //// for(Map.Entry<String, HashMap<String, Object>> e : cells.entrySet()) {
263 //// String location = e.getKey();
264 //// Range r = SpreadsheetUtils.decodeCellAbsolute(location);
265 //// if(r.startRow >= getRowCount() || r.startColumn >= getColumnCount()) continue;
266 //// String label = (String)e.getValue().get("Label");
267 //// if(Spreadsheet.DEBUG) System.out.println("setLabel '" + label + "' at " + location + " " + getRowCount() + " " + getColumnCount());
268 //// setValueAt(label, r.startRow, r.startColumn);
273 // private synchronized void setColumnWidths() {
275 // if(Spreadsheet.DEBUG) System.out.println("SimpleContainerTableModel.setColumnWidths");
277 // if(table != null) {
279 // int[] columnWidths = model.getPropertyAt(ClientModel.HEADERS, ClientModel.HEADERS_COL_WIDTHS);
281 // int columnCount = table.getColumnModel().getColumnCount();
283 // for(int i = 0; i < columnWidths.length && i < columnCount ; i++)
285 // TableColumn column = table.getColumnModel().getColumn(i);
286 // int preferred = columnWidths[i];
288 // column.setPreferredWidth(preferred);
295 // synchronized String[] createColumnIdentifiers(String[] defined) {
297 // int currentCols = getColumnCount();
298 // String[] identifiers = new String[currentCols];
300 // for(;i<defined.length && i<currentCols;i++) {
301 // identifiers[i] = defined[i];
303 // for(;i<currentCols;i++) {
304 // identifiers[i] = SpreadsheetUtils.columnName(i);
306 // return identifiers;
311 // public String getColumnName(int column) {
312 // new Exception("getColumnName " + column).printStackTrace();
313 // String[] names = model.getPropertyAt(ClientModel.HEADERS, ClientModel.HEADERS_COL_LABELS);
314 // if(column < names.length) return names[column];
315 // else return super.getColumnName(column);
318 // synchronized void applyHeaders() {
320 // AWTThread.getThreadAccess().asyncExec(new Runnable() {
323 // public void run() {
325 // String[] columnLabels = model.getPropertyAt(ClientModel.HEADERS, ClientModel.HEADERS_COL_LABELS);
326 // String[] filledColumnLabels = createColumnIdentifiers(columnLabels);
327 // String[] rowLabels = model.getPropertyAt(ClientModel.HEADERS, ClientModel.HEADERS_ROW_LABELS);
329 // if(Spreadsheet.DEBUG) {
330 // System.out.println("SimpleContainerTableModel.applyHeaders");
331 // System.out.println("-columnLabels=" + Arrays.toString(columnLabels));
332 // System.out.println("-filledColumnLabels=" + Arrays.toString(filledColumnLabels));
333 // System.out.println("-rowLabels=" + Arrays.toString(rowLabels));
336 // setColumnIdentifiers(filledColumnLabels);
338 //// if(listener != null) {
339 //// listener.rowLabels(rowLabels);
342 // if(table != null) {
343 // table.getRowModel().ensureCapacity(rowLabels.length);
345 // for(;i<rowLabels.length && i<table.getRowModel().size();i++) {
346 // table.getRowModel().setElementAt(rowLabels[i], i);
348 // for(;i<rowLabels.length;i++) {
349 // table.getRowModel().addElement(rowLabels[i]);