View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: PropretiesLabelProvider.java
13   * Created: 2009-08-04
14   * Author: kpietak
15   * $Id$
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   * Label provider for properties tree viewer in {@link PropertiesEditorDialog}. It uses {@link PropertyTreeEntry}
39   * objects as a model.
40   *
41   * @author AGH CAST Team
42   */
43  public class PropertiesLabelProvider extends LabelProvider implements ITableLabelProvider, ITableColorProvider {
44  
45  	private static final String CHECKED_KEY = "CHECKED"; //$NON-NLS-1$
46  
47  	private static final String UNCHECKED_KEY = "UNCHECKED"; //$NON-NLS-1$
48  
49  	private final Shell dialogShell;
50  
51  	private boolean showIsLabelColumn;
52  
53  	/**
54  	 * Constructor.
55  	 *
56  	 * @param dialogShell
57  	 *            dialog shell
58  	 * @param viewer
59  	 *            tree viewer for this provider
60  	 * @param showIsLabelColumn
61  	 *            flag which indicates if is-label column is shown or not
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  	 * {@inheritDoc}
78  	 *
79  	 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
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  	 * {@inheritDoc}
99  	 *
100 	 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
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 //$NON-NLS-1$
115 			        .getProperty().getValue().toString();
116 
117 		}
118 		return null;
119 	}
120 
121 	/**
122 	 * {@inheritDoc}
123 	 *
124 	 * @see org.eclipse.jface.viewers.ITableColorProvider#getBackground(java.lang.Object, int)
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 	 * {@inheritDoc}
167 	 *
168 	 * @see org.eclipse.jface.viewers.ITableColorProvider#getForeground(java.lang.Object, int)
169 	 */
170 	@Override
171 	public Color getForeground(Object element, int columnIndex) {
172 		return null;
173 	}
174 
175 	/**
176 	 * This snippet is copied from <a
177 	 * href="http://www.playingwithwire.com/2007/12/swt-jface-tableviewer-checkbox/">http:
178 	 * //www.playingwithwire.com/2007/12/swt-jface-tableviewer-checkbox/</a>. It creates native-look check-box image
179 	 * (cleared and selected).
180 	 */
181 	private Image makeShot(Control control, boolean type) {
182 		// Hopefully no platform uses exactly this color because we'll make
183 		// it transparent in the image.
184 		Color greenScreen = new Color(control.getDisplay(), 222, 223, 224);
185 
186 		Shell shell = new Shell(control.getShell(), SWT.NO_TRIM);
187 
188 		// otherwise we have a default gray color
189 		shell.setBackground(greenScreen);
190 
191 		Button button = new Button(shell, SWT.CHECK);
192 		button.setBackground(greenScreen);
193 		button.setSelection(type);
194 
195 		// otherwise an image is located in a corner
196 		button.setLocation(1, 1);
197 		Point bsize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);
198 
199 		// otherwise an image is stretched by width
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 }