]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/ClientTableModel.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.spreadsheet.ui / src / org / simantics / spreadsheet / ui / ClientTableModel.java
1 /*******************************************************************************
2  * in Industry THTH ry.
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
7  *
8  * Contributors:
9  *     VTT Technical Research Centre of Finland - initial API and implementation
10  *******************************************************************************/
11 package org.simantics.spreadsheet.ui;
12
13 import java.awt.Color;
14 import java.awt.Font;
15 import java.util.concurrent.atomic.AtomicBoolean;
16
17 import javax.swing.table.DefaultTableModel;
18
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;
27
28 final public class ClientTableModel extends DefaultTableModel {
29
30         private static final long serialVersionUID = 226747623479756815L;
31         private final ClientModel model;
32         
33         public ClientTableModel(ClientModel model) {
34                 this.model = model;
35         }
36         
37         public void setModel(final SpreadsheetModel model) {
38                 
39                 final SpreadsheetTable table = model.getTable();
40                 
41                 this.model.addListener(new ClientModelListener() {
42
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);
47                         
48                         @Override
49                         public void columnLabels(String[] labels) {
50                                 dirtyStructure.set(true);
51                         }
52
53                         @Override
54                         public void rowLabels(String[] labels) {
55                                 dirtyStructure.set(true);
56                         }
57
58                         @Override
59                         public void rows(int amount) {
60                                 dirtyStructure.set(true);
61                         }
62
63                         @Override
64                         public void columns(int amount) {
65                                 dirtyStructure.set(true);
66                         }
67                         
68                         @Override
69                         public void sources(final String[] available, final String current) {
70                                 dirtySources.set(true);
71                         }
72
73                         @Override
74                         public void columnWidths(int[] widths) {
75                                 dirtyHeaderSizes.set(true);
76                         }
77                         
78                         @Override
79                         public void cleared(String location) {
80                                 dirtyValues.set(true);
81                         }
82
83                         @Override
84                         public void propertyChange(String location, String property, Object value) {
85                                 dirtyValues.set(true);
86                         }
87                         
88                         @Override
89                         public void flush() {
90                                 
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);
95                                 
96                                 if(sources) {
97                                         AWTThread.getThreadAccess().asyncExec(new Runnable() {
98                                                 @Override
99                                                 public void run() {
100                                                         
101                                                         model.setSources();
102                                                         
103                                                 }
104                                         });
105                                 }
106
107                                 if (Spreadsheet.DEBUG)
108                                     System.out.println("ClientTableModel.ClientModelListener.flush: structure=" + structure + ", values=" + values + ", headerSizes=" + headerSizes);
109                                 
110                                 if(structure || values || headerSizes) {
111                                         
112                                         AWTThread.getThreadAccess().asyncExec(new Runnable() {
113
114                                                 @Override
115                                                 public void run() {
116                                                         
117                                                         int[] cc = table.getSelectedColumns();
118                                                         int[] rc = table.getSelectedRows();
119                                                         
120                                                         if(structure) fireTableStructureChanged();
121                                                         if(values) fireTableDataChanged();
122
123                                                         if (headerSizes)
124                                                             table.applyHeaderSizes(ClientTableModel.this.model);
125
126                                                         if(rc.length == 1 && cc.length == 1) {
127                                                                 table.setColumnSelectionInterval(cc[0], cc[0]);
128                                                                 table.setRowSelectionInterval(rc[0], rc[0]);
129                                                         }
130                                                         
131                                                 }
132                                                 
133                                         });
134                                         
135                                 }
136                                 
137                         }
138                         
139                 });
140                 
141         }
142         
143         @Override
144         public Object getValueAt(int row, int column) {
145                 String location = SpreadsheetUtils.cellName(row, column);
146                 
147 //              System.out.println("CellValue for location " + location);
148                 
149                 Color foregroundColor = null;
150                 Color backgroundColor = null;
151                 Font font = null;
152                 int borderValue = 0;
153                 int alignValue = 0;
154                 String formatString = null;
155                 int formatIndex = 0;
156                 SpreadsheetStyle style = model.getPropertyAt(location, "style");
157                 if (style != null) {
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;
163                 
164                 formatString = style.formatString;
165                 formatIndex = style.formatIndex;
166                 }
167
168                 Boolean editable = model.getPropertyAt(location, "editable");
169                 boolean editableValue = editable != null ? editable : false;
170                 
171                 OperationMode currentMode = model.getPropertyAt(ClientModel.MODE, ClientModel.MODE_CURRENT);
172                 if (!OperationMode.OPERATION.equals(currentMode))
173                     editableValue = true;
174                 
175                 return new CellValue(SpreadsheetUtils.getFormattedLabel(model, row, column, formatIndex, formatString), font, foregroundColor, backgroundColor, borderValue, alignValue, editableValue);
176         }
177         
178         @Override
179         public int getColumnCount() {
180                 return model.getColumns();
181         }
182         
183         @Override
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);
188         }
189         
190         @Override
191         public int getRowCount() {
192                 //TODO: haxx for initialization
193                 if(model == null) return 0;
194                 return model.getRows();
195         }
196         
197 //      void applyHeaderSizes() {
198 //              
199 //      }
200 //      
201 ////    synchronized void applyDimensions() {
202 ////
203 ////            boolean fitRows = model.getPropertyAt(ClientModel.DIMENSIONS, ClientModel.DIMENSIONS_FIT_ROWS);
204 ////            boolean fitColumns = model.getPropertyAt(ClientModel.DIMENSIONS, ClientModel.DIMENSIONS_FIT_COLS);
205 ////
206 ////            int currentRows = getRowCount();
207 ////            int currentCols = getColumnCount();
208 ////
209 ////            int maxRow = model.getRows();
210 ////            int maxColumn = model.getColumns();
211 ////            
212 ////            int rowCount = model.getPropertyAt(ClientModel.DIMENSIONS, ClientModel.DIMENSIONS_ROW_COUNT);
213 ////            int columnCount = model.getPropertyAt(ClientModel.DIMENSIONS, ClientModel.DIMENSIONS_COL_COUNT);
214 ////            
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());
222 //////                  }
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);
227 ////            }
228 ////            
229 ////            if(fitRows) {
230 ////                    if(maxRow != currentRows) {
231 ////                            setRowCount(maxRow);
232 ////                            if(table != null) table.getRowModel().setSize(maxRow);
233 ////                            setColumnWidths();
234 ////                    }
235 ////            } else {
236 ////                    if(rowCount != currentRows) {
237 ////                            setRowCount(rowCount);
238 ////                            if(table != null) table.getRowModel().setSize(rowCount);
239 ////                            setColumnWidths();
240 ////                    }
241 ////            }
242 ////            if(fitColumns) {
243 ////                    if(maxColumn != currentCols) {
244 ////                            setColumnCount(maxColumn);
245 //////                          applyLabels();
246 ////                            setColumnWidths();
247 ////                    }
248 ////            } else {
249 ////                    if(columnCount != currentCols) {
250 ////                            setColumnCount(columnCount);
251 //////                          applyLabels();
252 ////                            setColumnWidths();
253 ////                    }
254 ////            }
255 ////            
256 ////    }
257 //      
258 ////    private synchronized void applyLabels() {
259 ////            
260 ////            if(Spreadsheet.DEBUG) System.out.println("SimpleContainerTableModel.applyLabels");
261 ////            
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);   
269 ////            }
270 ////            
271 ////    }
272 //      
273 //      private synchronized void setColumnWidths() {
274 //
275 //              if(Spreadsheet.DEBUG) System.out.println("SimpleContainerTableModel.setColumnWidths");
276 //              
277 //              if(table != null) {
278 //
279 //                      int[] columnWidths = model.getPropertyAt(ClientModel.HEADERS, ClientModel.HEADERS_COL_WIDTHS);
280 //
281 //                      int columnCount = table.getColumnModel().getColumnCount();
282 //                      
283 //                      for(int i = 0; i < columnWidths.length && i < columnCount ; i++) 
284 //                      { 
285 //                              TableColumn column = table.getColumnModel().getColumn(i);
286 //                              int preferred = columnWidths[i];
287 //                              if(preferred > 0)
288 //                                      column.setPreferredWidth(preferred); 
289 //                      }
290 //                      
291 //              }
292 //              
293 //      }
294 //      
295 //      synchronized String[] createColumnIdentifiers(String[] defined) {
296 //
297 //              int currentCols = getColumnCount();
298 //              String[] identifiers = new String[currentCols];
299 //              int i=0;
300 //              for(;i<defined.length && i<currentCols;i++) {
301 //                      identifiers[i] = defined[i];
302 //              }
303 //              for(;i<currentCols;i++) {
304 //                      identifiers[i] = SpreadsheetUtils.columnName(i);
305 //              }
306 //              return identifiers;
307 //
308 //      }
309 //      
310 //      @Override
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);
316 //      }
317 //      
318 //      synchronized void applyHeaders() {
319 //
320 //              AWTThread.getThreadAccess().asyncExec(new Runnable() {
321 //
322 //                      @Override
323 //                      public void run() {
324 //
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);
328 //
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));
334 //                              }
335 //                              
336 //                              setColumnIdentifiers(filledColumnLabels);
337 //                              
338 ////                            if(listener != null) {
339 ////                                    listener.rowLabels(rowLabels);
340 ////                            }
341 //                              
342 //                              if(table != null) {
343 //                              table.getRowModel().ensureCapacity(rowLabels.length);
344 //                              int i=0;
345 //                              for(;i<rowLabels.length && i<table.getRowModel().size();i++) {
346 //                                      table.getRowModel().setElementAt(rowLabels[i], i);
347 //                              }
348 //                              for(;i<rowLabels.length;i++) {
349 //                                      table.getRowModel().addElement(rowLabels[i]);
350 //                              }
351 //                              }
352 //                              
353 ////                            applyLabels();
354 //                              
355 //                      }
356 //                      
357 //              });
358 //              
359 //      }
360         
361 }