]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.common/src/org/simantics/spreadsheet/common/TreeTableCell.java
299c7c7964955e4c47b2aa1beab214342ae35c39
[simantics/platform.git] / bundles / org.simantics.spreadsheet.common / src / org / simantics / spreadsheet / common / TreeTableCell.java
1 /*******************************************************************************
2  * Copyright (c) 2013, 2014 Association for Decentralized 
3  * Information Management in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the THTH Simantics 
6  * Division Member Component License which accompanies this 
7  * distribution, and is available at
8  * http://www.simantics.org/legal/sdmcl-v10.html
9  *
10  * Contributors:
11  *     Semantum Oy - initial API and implementation
12  *******************************************************************************/
13 package org.simantics.spreadsheet.common;
14
15 import org.eclipse.jface.resource.FontDescriptor;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.graphics.FontData;
18 import org.simantics.document.server.io.IFont;
19 import org.simantics.document.server.io.ITreeTableCell;
20 import org.simantics.document.server.io.SimpleFont;
21
22 public class TreeTableCell extends TableCell implements ITreeTableCell {        
23         
24         private int parent = -1;
25         
26         private boolean editable = true;
27         private Object data;
28
29         public TreeTableCell() {
30         }
31         
32         public TreeTableCell(String text, Object data, Object font, int parent, int row, int column, boolean editable) {
33                 super(column, row, 0, 0, text, (IFont)font, null, null, false, 1, 1);
34                 this.editable = editable;
35                 this.parent = parent;
36                 this.data = data;
37         }
38         
39         @Override
40         public int getRowSpan() {
41                 throw new IllegalStateException("Row span is not supported in TreeTableCell");
42         }
43         
44         @Override
45         public int getColumnSpan() {
46                 throw new IllegalStateException("Column span is not supported in TreeTableCell");
47         }
48         
49         public static TreeTableCell createTreeTableCell(String text, Object data, Object font, int parent, int row, int column, boolean editable) {
50                 return new TreeTableCell(text, data, extractIFont(font), parent, row, column, editable);
51         }
52         
53         private static IFont extractIFont(Object font) {
54                 if(font instanceof FontDescriptor) {
55                         FontDescriptor descriptor = (FontDescriptor)font;
56                         String family = "";
57                         String style = "";
58                         int size = 12;
59                         for(FontData d : descriptor.getFontData()) {
60                                 family = d.getName();
61                                 if((d.getStyle() & SWT.ITALIC) != 0) style += "Italic";
62                                 if((d.getStyle() & SWT.BOLD) != 0) style += "Bold";
63                                 size = d.getHeight();
64                         }
65                         return new SimpleFont(family, style, size);
66                 }
67                 return null;
68         }
69         
70         public void setParent(int parent) {
71                 this.parent = parent;
72         }
73         
74         @Override
75         public int getParent() {
76                 return parent;
77         }
78
79         @Override
80         public Object getData() {
81                 return data;
82         }
83
84         @Override
85         public boolean isEditable() {
86                 return editable;
87         }
88
89 }