1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.ui.dialogs.property;
19
20 import org.eclipse.jface.resource.JFaceResources;
21 import org.eclipse.jface.viewers.ITableColorProvider;
22 import org.eclipse.jface.viewers.ITableLabelProvider;
23 import org.eclipse.jface.viewers.LabelProvider;
24 import org.eclipse.jface.viewers.TreeViewer;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.graphics.Color;
27 import org.eclipse.swt.graphics.GC;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.swt.graphics.ImageData;
30 import org.eclipse.swt.graphics.Point;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Shell;
34
35 import pl.edu.agh.cast.ui.dialogs.property.PropertyTreeEntry.PropertyEditType;
36
37
38
39
40
41
42
43 public class PropertiesLabelProvider extends LabelProvider implements ITableLabelProvider, ITableColorProvider {
44
45 private static final String CHECKED_KEY = "CHECKED";
46
47 private static final String UNCHECKED_KEY = "UNCHECKED";
48
49 private final Shell dialogShell;
50
51 private boolean showIsLabelColumn;
52
53
54
55
56
57
58
59
60
61
62
63 public PropertiesLabelProvider(Shell dialogShell, TreeViewer viewer, boolean showIsLabelColumn) {
64 if (dialogShell == null) {
65 throw new IllegalArgumentException();
66 }
67 this.dialogShell = dialogShell;
68 this.showIsLabelColumn = showIsLabelColumn;
69
70 if (JFaceResources.getImageRegistry().getDescriptor(CHECKED_KEY) == null) {
71 JFaceResources.getImageRegistry().put(UNCHECKED_KEY, makeShot(viewer.getControl(), false));
72 JFaceResources.getImageRegistry().put(CHECKED_KEY, makeShot(viewer.getControl(), true));
73 }
74 }
75
76
77
78
79
80
81 @Override
82 public Image getColumnImage(Object element, int columnIndex) {
83 if (columnIndex == getIsLabelColumnIndex()) {
84 PropertyTreeEntry entry = (PropertyTreeEntry)element;
85
86 if (entry.getType().equals(PropertyEditType.CATEGORY)) {
87 return null;
88 }
89 return entry.isMarkAsLabel() ? JFaceResources.getImageRegistry().get(CHECKED_KEY) : JFaceResources
90 .getImageRegistry().get(UNCHECKED_KEY);
91
92 } else {
93 return null;
94 }
95 }
96
97
98
99
100
101
102 @Override
103 public String getColumnText(Object element, int columnIndex) {
104 PropertyTreeEntry entry = (PropertyTreeEntry)element;
105 if (columnIndex == getNameColumnIndex()) {
106 String res = null;
107 if (entry.getProperty() != null) {
108 res = entry.getProperty().getMetaProperty().getDisplayName();
109 } else {
110 res = entry.getLabel();
111 }
112 return res;
113 } else if (columnIndex == getValueColumnIndex()) {
114 return (entry.getProperty() == null) || (entry.getProperty().getValue() == null) ? "" : entry
115 .getProperty().getValue().toString();
116
117 }
118 return null;
119 }
120
121
122
123
124
125
126 @Override
127 public Color getBackground(Object element, int columnIndex) {
128 PropertyTreeEntry entry = (PropertyTreeEntry)element;
129 Color res = null;
130
131 if (columnIndex == getIsLabelColumnIndex()) {
132 res = dialogShell.getDisplay().getSystemColor(SWT.COLOR_GRAY);
133 } else if (columnIndex == getNameColumnIndex()) {
134 if (entry.getType().equals(PropertyEditType.CATEGORY)) {
135 res = dialogShell.getDisplay().getSystemColor(SWT.COLOR_GRAY);
136 } else {
137 res = dialogShell.getDisplay().getSystemColor(SWT.COLOR_WHITE);
138 }
139 } else if (columnIndex == getValueColumnIndex()) {
140 switch (entry.getType()) {
141 case CATEGORY:
142 res = dialogShell.getDisplay().getSystemColor(SWT.COLOR_GRAY);
143 break;
144 case NOT_EDITABLE:
145 res = dialogShell.getDisplay().getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND);
146 break;
147 case EDITABLE:
148 res = dialogShell.getDisplay().getSystemColor(SWT.COLOR_TITLE_BACKGROUND);
149 break;
150 case CUSTOM_NOT_EDITABLE:
151 res = dialogShell.getDisplay().getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT);
152 break;
153 case CUSTOM_EDITABLE:
154 res = dialogShell.getDisplay().getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT);
155 break;
156 default:
157 res = dialogShell.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND);
158 break;
159 }
160 }
161
162 return res;
163 }
164
165
166
167
168
169
170 @Override
171 public Color getForeground(Object element, int columnIndex) {
172 return null;
173 }
174
175
176
177
178
179
180
181 private Image makeShot(Control control, boolean type) {
182
183
184 Color greenScreen = new Color(control.getDisplay(), 222, 223, 224);
185
186 Shell shell = new Shell(control.getShell(), SWT.NO_TRIM);
187
188
189 shell.setBackground(greenScreen);
190
191 Button button = new Button(shell, SWT.CHECK);
192 button.setBackground(greenScreen);
193 button.setSelection(type);
194
195
196 button.setLocation(1, 1);
197 Point bsize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);
198
199
200 bsize.x = Math.max(bsize.x - 1, bsize.y - 1);
201 bsize.y = Math.max(bsize.x - 1, bsize.y - 1);
202 button.setSize(bsize);
203 shell.setSize(bsize);
204
205 shell.open();
206 GC gc = new GC(shell);
207 Image image = new Image(control.getDisplay(), bsize.x, bsize.y);
208 gc.copyArea(image, 0, 0);
209 gc.dispose();
210 shell.close();
211
212 ImageData imageData = image.getImageData();
213 imageData.transparentPixel = imageData.palette.getPixel(greenScreen.getRGB());
214
215 return new Image(control.getDisplay(), imageData);
216 }
217
218 private int getIsLabelColumnIndex() {
219 return showIsLabelColumn ? 0 : -1;
220 }
221
222 private int getNameColumnIndex() {
223 return showIsLabelColumn ? 1 : 0;
224 }
225
226 private int getValueColumnIndex() {
227 return showIsLabelColumn ? 2 : 1;
228 }
229
230 }