]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.common/src/org/simantics/spreadsheet/common/TreeTableCell.java
TreeGridWidget performance
[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         private Object parentData;
26         
27         private boolean editable = true;
28         private Object data;
29
30         public TreeTableCell() {
31         }
32         
33         public TreeTableCell(String text, Object data, Object font, Object parentData, int parent, int row, int column, boolean editable) {
34                 super(column, row, 0, 0, text, (IFont)font, null, null, false, 1, 1);
35                 this.editable = editable;
36                 this.parent = parent;
37                 this.parentData = parentData;
38                 this.data = data;
39         }
40         
41         @Override
42         public int getRowSpan() {
43                 throw new IllegalStateException("Row span is not supported in TreeTableCell");
44         }
45         
46         @Override
47         public int getColumnSpan() {
48                 throw new IllegalStateException("Column span is not supported in TreeTableCell");
49         }
50         
51         public static TreeTableCell createTreeTableCell(String text, Object data, Object font, int parent, int row, int column, boolean editable) {
52                 return new TreeTableCell(text, data, extractIFont(font), null, parent, row, column, editable);
53         }
54         
55         public static TreeTableCell createTreeTableCell2(String text, Object data, Object font, Object parentData, int row, int column, boolean editable) {
56                 return new TreeTableCell(text, data, extractIFont(font), parentData, -1, row, column, editable);
57         }
58
59         private static IFont extractIFont(Object font) {
60                 if(font instanceof FontDescriptor) {
61                         FontDescriptor descriptor = (FontDescriptor)font;
62                         String family = "";
63                         String style = "";
64                         int size = 12;
65                         for(FontData d : descriptor.getFontData()) {
66                                 family = d.getName();
67                                 if((d.getStyle() & SWT.ITALIC) != 0) style += "Italic";
68                                 if((d.getStyle() & SWT.BOLD) != 0) style += "Bold";
69                                 size = d.getHeight();
70                         }
71                         return new SimpleFont(family, style, size);
72                 }
73                 return null;
74         }
75         
76         public void setParent(int parent) {
77                 this.parent = parent;
78         }
79         
80         @Override
81         public Object getParentData() {
82                 return parentData;
83         }
84         
85         @Override
86         public int getParent() {
87                 return parent;
88         }
89
90         @Override
91         public Object getData() {
92                 return data;
93         }
94
95         @Override
96         public boolean isEditable() {
97                 return editable;
98         }
99
100         @Override
101         public int hashCode() {
102                 
103                 final int prime = 31;
104                 
105                 int result = super.hashCode();
106
107                 result = data != null ? prime * result + data.hashCode() : parent;
108                 result = parentData != null ? prime * result + parentData.hashCode() : result;
109                 result = prime * result + Integer.hashCode(parent);
110                 result = prime * result + Boolean.hashCode(editable);
111                 
112                 return result;
113                 
114         }
115         @Override
116         public boolean equals(Object obj) {
117                 if (this == obj)
118                         return true;
119                 if (obj == null)
120                         return false;
121                 if (getClass() != obj.getClass())
122                         return false;
123                 TreeTableCell other = (TreeTableCell) obj;
124                 if (data == null) {
125                         if (other.data != null)
126                                 return false;
127                 } else if (!data.equals(other.data))
128                         return false;
129                 if (parentData == null) {
130                         if (other.parentData != null)
131                                 return false;
132                 } else if (!parentData.equals(other.parentData))
133                         return false;
134                 if (parent != other.parent)
135                         return false;
136                 if (editable != other.editable)
137                         return false;
138                 return super.equals(obj);
139         }
140         
141 }