2cf40b9c7a936730c4c84fdf19ca80e8c394ec74
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / preferences / SDViewPref.java
1 /**********************************************************************
2 * Copyright (c) 2005, 2013 IBM Corporation, Ericsson
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 * IBM - Initial API and implementation
10 * Bernd Hufmann - Updated for TMF
11 **********************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.views.uml2sd.preferences;
14
15 import java.util.Arrays;
16 import java.util.Hashtable;
17 import java.util.Map;
18
19 import org.eclipse.jface.preference.IPreferenceStore;
20 import org.eclipse.jface.preference.PreferenceConverter;
21 import org.eclipse.jface.util.IPropertyChangeListener;
22 import org.eclipse.jface.util.PropertyChangeEvent;
23 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
24 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IColor;
25 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IFont;
26 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.impl.ColorImpl;
27 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.impl.FontImpl;
28 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.Messages;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.graphics.Color;
31 import org.eclipse.swt.graphics.FontData;
32 import org.eclipse.swt.graphics.RGB;
33 import org.eclipse.swt.widgets.Display;
34
35 /**
36 * This is the Sequence Diagram preference handler. This class is responsible for accessing the current user preferences
37 * selection This class also provider getters for each modifiable preferences.
38 *
39 * @version 1.0
40 * @author sveyrier
41 */
42 public class SDViewPref implements ISDPreferences, IPropertyChangeListener {
43
44 // ------------------------------------------------------------------------
45 // Constants
46 // ------------------------------------------------------------------------
47 /**
48 * Postfix string for background color property
49 */
50 public static final String BACK_COLOR_POSTFIX = "_BACK_COLOR";//$NON-NLS-1$
51 /**
52 * Postfix string for foreground color property
53 */
54 public static final String FORE_COLOR_POSTFIX = "_FORE_COLOR";//$NON-NLS-1$
55 /**
56 * Postfix string for text color property
57 */
58 public static final String TEXT_COLOR_POSTFIX = "_TEXT_COLOR";//$NON-NLS-1$
59 /**
60 * Array of preference names
61 */
62 private static final String[] FONT_LIST = { PREF_LIFELINE, PREF_EXEC, PREF_SYNC_MESS, PREF_SYNC_MESS_RET, PREF_ASYNC_MESS, PREF_ASYNC_MESS_RET, PREF_FRAME, PREF_LIFELINE_HEADER, PREF_FRAME_NAME };
63 /**
64 * A 2nd array of preference names
65 */
66 private static final String[] FONT_LIST2 = { Messages.SequenceDiagram_Lifeline, Messages.SequenceDiagram_ExecutionOccurrence, Messages.SequenceDiagram_SyncMessage, Messages.SequenceDiagram_SyncMessageReturn, Messages.SequenceDiagram_AsyncMessage, Messages.SequenceDiagram_AsyncMessageReturn, Messages.SequenceDiagram_Frame, Messages.SequenceDiagram_LifelineHeader, Messages.SequenceDiagram_FrameTitle };
67 /**
68 * Array of background color preference names
69 */
70 private static final String[] PREF_BACK_COLOR_LIST = { PREF_LIFELINE, PREF_EXEC, PREF_FRAME, PREF_LIFELINE_HEADER, PREF_FRAME_NAME };
71 /**
72 * Array of foreground color preference names
73 */
74 private static final String[] PREF_FORE_COLOR_LIST = { PREF_LIFELINE, PREF_EXEC, PREF_SYNC_MESS, PREF_SYNC_MESS_RET, PREF_ASYNC_MESS, PREF_ASYNC_MESS_RET, PREF_FRAME, PREF_LIFELINE_HEADER, PREF_FRAME_NAME };
75 /**
76 * Array of text color preference names
77 */
78 private static final String[] PREF_TEXT_COLOR_LIST = { PREF_LIFELINE, PREF_SYNC_MESS, PREF_SYNC_MESS_RET, PREF_ASYNC_MESS, PREF_ASYNC_MESS_RET, PREF_LIFELINE_HEADER, PREF_FRAME_NAME };
79 /**
80 * Temporary tag
81 */
82 protected static final String TEMP_TAG = "_TEMP";//$NON-NLS-1$
83
84 // ------------------------------------------------------------------------
85 // Attributes
86 // ------------------------------------------------------------------------
87
88 /**
89 * The sequence diagram preferences singleton instance
90 */
91 private static SDViewPref fHandle = null;
92 /**
93 * Hashtable for font preferences
94 */
95 protected Map<String, IFont> fFontPref;
96 /**
97 * Hashtable for foreground color preferences
98 */
99 protected Map<String, IColor> fForeColorPref;
100 /**
101 * Hashtable for background color preferences
102 */
103 protected Map<String, IColor> fBackColorPref;
104 /**
105 * Hashtable for text color preferences
106 */
107 protected Map<String, IColor> fTextColorPref;
108 /**
109 * The reference to the preference store.
110 */
111 protected IPreferenceStore fPrefStore = null;
112 /**
113 * Color for the time compression selection
114 */
115 protected IColor fTimeCompressionSelectionColor = null;
116 /**
117 * Flag whether no focus selection or not.
118 */
119 protected boolean fNoFocusSelection = false;
120
121 // ------------------------------------------------------------------------
122 // Constructors
123 // ------------------------------------------------------------------------
124
125 /**
126 * Builds the Sequence Diagram preference handler: - Define the preference default values. - Load the currently used
127 * preferences setting
128 */
129 protected SDViewPref() {
130 fPrefStore = Activator.getDefault().getPreferenceStore();
131
132 fPrefStore.setDefault(PREF_LINK_FONT, true);
133 fPrefStore.setDefault(PREF_EXCLUDE_EXTERNAL_TIME, true);
134 fPrefStore.setDefault(PREF_LIFELINE_WIDTH, 200);
135 fPrefStore.setDefault(PREF_USE_GRADIENT, true);
136 fPrefStore.setDefault(PREF_TOOLTIP, true);
137
138 fFontPref = new Hashtable<String, IFont>();
139 fForeColorPref = new Hashtable<String, IColor>();
140 fBackColorPref = new Hashtable<String, IColor>();
141 fTextColorPref = new Hashtable<String, IColor>();
142
143 for (int i = 0; i < FONT_LIST.length; i++) {
144 if (FONT_LIST[i].equals(PREF_FRAME_NAME)) {
145 FontData[] data = Display.getDefault().getSystemFont().getFontData();
146 data[0].setStyle(SWT.BOLD);
147 PreferenceConverter.setDefault(fPrefStore, FONT_LIST[i], data[0]);
148 PreferenceConverter.setDefault(fPrefStore, FONT_LIST[i] + TEMP_TAG, data[0]);
149 } else {
150 PreferenceConverter.setDefault(fPrefStore, FONT_LIST[i], Display.getDefault().getSystemFont().getFontData());
151 PreferenceConverter.setDefault(fPrefStore, FONT_LIST[i] + TEMP_TAG, Display.getDefault().getSystemFont().getFontData());
152 }
153 }
154
155 for (int i = 0; i < PREF_BACK_COLOR_LIST.length; i++) {
156 IColor color;
157 if ((PREF_BACK_COLOR_LIST[i].equals(PREF_EXEC)) || PREF_BACK_COLOR_LIST[i].equals(PREF_FRAME_NAME)) {
158 color = new ColorImpl(Display.getDefault(), 201, 222, 233);
159 } else if (PREF_BACK_COLOR_LIST[i].equals(PREF_LIFELINE)) {
160 color = new ColorImpl(Display.getDefault(), 220, 220, 220);
161 } else if (PREF_BACK_COLOR_LIST[i].equals(PREF_LIFELINE_HEADER)) {
162 color = new ColorImpl(Display.getDefault(), 245, 244, 244);
163 } else {
164 color = new ColorImpl(Display.getDefault(), 255, 255, 255);
165 }
166 PreferenceConverter.setDefault(fPrefStore, PREF_BACK_COLOR_LIST[i] + BACK_COLOR_POSTFIX, ((Color) color.getColor()).getRGB());
167 PreferenceConverter.setDefault(fPrefStore, PREF_BACK_COLOR_LIST[i] + BACK_COLOR_POSTFIX + TEMP_TAG, ((Color) color.getColor()).getRGB());
168 color.dispose();
169 }
170
171 for (int i = 0; i < PREF_FORE_COLOR_LIST.length; i++) {
172 IColor color;
173 if (PREF_FORE_COLOR_LIST[i].equals(PREF_LIFELINE)) {
174 color = new ColorImpl(Display.getDefault(), 129, 129, 129);
175 } else if (PREF_FORE_COLOR_LIST[i].equals(PREF_FRAME_NAME)) {
176 color = new ColorImpl(Display.getDefault(), 81, 153, 200);
177 } else if (PREF_FORE_COLOR_LIST[i].equals(PREF_LIFELINE_HEADER)) {
178 color = new ColorImpl(Display.getDefault(), 129, 127, 137);
179 } else {
180 color = new ColorImpl(Display.getDefault(), 134, 176, 212);
181 }
182 PreferenceConverter.setDefault(fPrefStore, PREF_FORE_COLOR_LIST[i] + FORE_COLOR_POSTFIX, ((Color) color.getColor()).getRGB());
183 PreferenceConverter.setDefault(fPrefStore, PREF_FORE_COLOR_LIST[i] + FORE_COLOR_POSTFIX + TEMP_TAG, ((Color) color.getColor()).getRGB());
184 color.dispose();
185 }
186
187 for (int i = 0; i < PREF_TEXT_COLOR_LIST.length; i++) {
188 IColor color;
189 if (PREF_TEXT_COLOR_LIST[i].equals(PREF_LIFELINE)) {
190 color = new ColorImpl(Display.getDefault(), 129, 129, 129);
191 } else if (PREF_TEXT_COLOR_LIST[i].equals(PREF_FRAME_NAME)) {
192 color = new ColorImpl(Display.getDefault(), 0, 0, 0);
193 } else if (PREF_TEXT_COLOR_LIST[i].equals(PREF_LIFELINE_HEADER)) {
194 color = new ColorImpl(Display.getDefault(), 129, 127, 137);
195 } else {
196 color = new ColorImpl(Display.getDefault(), 134, 176, 212);
197 }
198 PreferenceConverter.setDefault(fPrefStore, PREF_TEXT_COLOR_LIST[i] + TEXT_COLOR_POSTFIX, ((Color) color.getColor()).getRGB());
199 PreferenceConverter.setDefault(fPrefStore, PREF_TEXT_COLOR_LIST[i] + TEXT_COLOR_POSTFIX + TEMP_TAG, ((Color) color.getColor()).getRGB());
200 color.dispose();
201 }
202
203 IColor color = new ColorImpl(Display.getDefault(), 218, 232, 238);
204 PreferenceConverter.setDefault(fPrefStore, PREF_TIME_COMP, ((Color) color.getColor()).getRGB());
205 color.dispose();
206
207 buildFontsAndColors();
208
209 fPrefStore.addPropertyChangeListener(this);
210 }
211
212 /**
213 * Returns the PreferenceStore
214 *
215 * @return the PreferenceStore
216 */
217 public IPreferenceStore getPreferenceStore() {
218 return fPrefStore;
219 }
220
221 /**
222 * Apply the preferences in the preferences handler
223 */
224 public void apply() {
225 buildFontsAndColors();
226 fPrefStore.firePropertyChangeEvent("PREFOK", null, null); //$NON-NLS-1$
227 }
228
229 /**
230 * Returns an unique instance of the Sequence Diagram preference handler
231 *
232 * @return the preference handler instance
233 */
234 public static synchronized SDViewPref getInstance() {
235 if (fHandle == null) {
236 fHandle = new SDViewPref();
237 }
238 return fHandle;
239 }
240
241 @Override
242 public IColor getForeGroundColor(String prefName) {
243 if ((fForeColorPref.get(prefName + FORE_COLOR_POSTFIX) != null) && (fForeColorPref.get(prefName + FORE_COLOR_POSTFIX) instanceof ColorImpl)) {
244 return fForeColorPref.get(prefName + FORE_COLOR_POSTFIX);
245 }
246 return ColorImpl.getSystemColor(SWT.COLOR_BLACK);
247 }
248
249 @Override
250 public IColor getBackGroundColor(String prefName) {
251 if ((fBackColorPref.get(prefName + BACK_COLOR_POSTFIX) != null) && (fBackColorPref.get(prefName + BACK_COLOR_POSTFIX) instanceof ColorImpl)) {
252 return fBackColorPref.get(prefName + BACK_COLOR_POSTFIX);
253 }
254 return ColorImpl.getSystemColor(SWT.COLOR_WHITE);
255 }
256
257 @Override
258 public IColor getFontColor(String prefName) {
259 if ((fTextColorPref.get(prefName + TEXT_COLOR_POSTFIX) != null) && (fTextColorPref.get(prefName + TEXT_COLOR_POSTFIX) instanceof ColorImpl)) {
260 return fTextColorPref.get(prefName + TEXT_COLOR_POSTFIX);
261 }
262 return ColorImpl.getSystemColor(SWT.COLOR_BLACK);
263 }
264
265 @Override
266 public IColor getForeGroundColorSelection() {
267 if (fNoFocusSelection) {
268 return ColorImpl.getSystemColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND);
269 }
270 return ColorImpl.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT);
271 }
272
273 @Override
274 public IColor getBackGroundColorSelection() {
275 if (fNoFocusSelection) {
276 return ColorImpl.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
277 }
278 return ColorImpl.getSystemColor(SWT.COLOR_LIST_SELECTION);
279 }
280
281 @Override
282 public IFont getFont(String prefName) {
283 if (fFontPref.get(prefName) != null) {
284 return fFontPref.get(prefName);
285 }
286 return FontImpl.getSystemFont();
287 }
288
289 /**
290 * Returns the SwimLane width chosen
291 *
292 * @return the SwimLane width
293 */
294 public int getLifelineWidth() {
295 return fPrefStore.getInt(PREF_LIFELINE_WIDTH);
296 }
297
298 /**
299 * Returns if font linkage with zoom has been chosen
300 *
301 * @return true if checked false otherwise
302 */
303 public boolean fontLinked() {
304 return fPrefStore.getBoolean(PREF_LINK_FONT);
305 }
306
307 /**
308 * Returns the tooltip enablement
309 *
310 * @return true if checked false otherwise
311 */
312 public boolean tooltipEnabled() {
313 return fPrefStore.getBoolean(PREF_TOOLTIP);
314 }
315
316 /**
317 * Return true if the user do not want to take external time (basically found and lost messages with time) into
318 * account in the min max computation
319 *
320 * @return true if checked false otherwise
321 */
322 public boolean excludeExternalTime() {
323 return fPrefStore.getBoolean(PREF_EXCLUDE_EXTERNAL_TIME);
324 }
325
326 @Override
327 public boolean useGradienColor() {
328 return fPrefStore.getBoolean(PREF_USE_GRADIENT);
329 }
330
331 @Override
332 public IColor getTimeCompressionSelectionColor() {
333 return fTimeCompressionSelectionColor;
334 }
335
336 /**
337 * Builds the new colors and fonts according the current user selection when the OK or Apply button is clicked
338 */
339 private void buildFontsAndColors() {
340
341 Display display = Display.getDefault();
342
343 for (int i = 0; i < FONT_LIST.length; i++) {
344 FontData fontData = PreferenceConverter.getFontData(fPrefStore, FONT_LIST[i]);
345 if (fFontPref.get(FONT_LIST[i]) != null) {
346 fFontPref.get(FONT_LIST[i]).dispose();
347 }
348 fFontPref.put(FONT_LIST[i], new FontImpl(display, fontData));
349 }
350
351 for (int i = 0; i < PREF_BACK_COLOR_LIST.length; i++) {
352 RGB rgb = PreferenceConverter.getColor(fPrefStore, PREF_BACK_COLOR_LIST[i] + BACK_COLOR_POSTFIX);
353 if (fBackColorPref.get(PREF_BACK_COLOR_LIST[i] + BACK_COLOR_POSTFIX) != null) {
354 fBackColorPref.get(PREF_BACK_COLOR_LIST[i] + BACK_COLOR_POSTFIX).dispose();
355 }
356 fBackColorPref.put(PREF_BACK_COLOR_LIST[i] + BACK_COLOR_POSTFIX, new ColorImpl(display, rgb.red, rgb.green, rgb.blue));
357 }
358
359 for (int i = 0; i < PREF_FORE_COLOR_LIST.length; i++) {
360 RGB rgb = PreferenceConverter.getColor(fPrefStore, PREF_FORE_COLOR_LIST[i] + FORE_COLOR_POSTFIX);
361 if (fForeColorPref.get(PREF_FORE_COLOR_LIST[i] + FORE_COLOR_POSTFIX) != null) {
362 fForeColorPref.get(PREF_FORE_COLOR_LIST[i] + FORE_COLOR_POSTFIX).dispose();
363 }
364 fForeColorPref.put(PREF_FORE_COLOR_LIST[i] + FORE_COLOR_POSTFIX, new ColorImpl(display, rgb.red, rgb.green, rgb.blue));
365 }
366
367 for (int i = 0; i < PREF_TEXT_COLOR_LIST.length; i++) {
368 RGB rgb = PreferenceConverter.getColor(fPrefStore, PREF_TEXT_COLOR_LIST[i] + TEXT_COLOR_POSTFIX);
369 if (fTextColorPref.get(PREF_TEXT_COLOR_LIST[i] + TEXT_COLOR_POSTFIX) != null) {
370 fTextColorPref.get(PREF_TEXT_COLOR_LIST[i] + TEXT_COLOR_POSTFIX).dispose();
371 }
372 fTextColorPref.put(PREF_TEXT_COLOR_LIST[i] + TEXT_COLOR_POSTFIX, new ColorImpl(display, rgb.red, rgb.green, rgb.blue));
373 }
374
375 RGB rgb = PreferenceConverter.getColor(fPrefStore, PREF_TIME_COMP);
376 if (fTimeCompressionSelectionColor != null) {
377 fTimeCompressionSelectionColor.dispose();
378 }
379 fTimeCompressionSelectionColor = new ColorImpl(display, rgb.red, rgb.green, rgb.blue);
380 }
381
382 /**
383 * Add a property-change listener
384 *
385 * @param listener
386 * The listener to add
387 */
388 public void addPropertyChangeListener(IPropertyChangeListener listener) {
389 fPrefStore.addPropertyChangeListener(listener);
390 }
391
392 /**
393 * Remove a property-change listener
394 *
395 * @param listener
396 * The listerner to remove
397 */
398 public void removePropertyChangeListener(IPropertyChangeListener listener) {
399 fPrefStore.removePropertyChangeListener(listener);
400 }
401
402 @Override
403 public void propertyChange(PropertyChangeEvent event) {
404 if (!event.getProperty().equals("PREFOK")) { //$NON-NLS-1$
405 buildFontsAndColors();
406 fPrefStore.firePropertyChangeEvent("PREFOK", null, null); //$NON-NLS-1$
407 }
408 }
409
410 /**
411 * Set the "no focus selection" preference
412 *
413 * @param v
414 * New value to use
415 */
416 public void setNoFocusSelection(boolean v) {
417 fNoFocusSelection = v;
418 }
419
420 /**
421 * Returns the static font list.
422 *
423 * @return static font list
424 */
425 public static String[] getFontList() {
426 return Arrays.copyOf(FONT_LIST, FONT_LIST.length);
427 }
428
429 /**
430 * Returns the 2nd static font list.
431 *
432 * @return 2nd static font list
433 */
434 public static String[] getFontList2() {
435 return Arrays.copyOf(FONT_LIST2, FONT_LIST2.length);
436 }
437
438 /**
439 * Returns the preference background color list.
440 *
441 * @return preference background color list
442 */
443 public static String[] getPrefBackColorList() {
444 return Arrays.copyOf(PREF_BACK_COLOR_LIST, PREF_BACK_COLOR_LIST.length);
445 }
446
447 /**
448 * Returns the preference foreground color list.
449 *
450 * @return preference foreground color list
451 */
452 public static String[] getPrefForeColorList() {
453 return Arrays.copyOf(PREF_FORE_COLOR_LIST, PREF_FORE_COLOR_LIST.length);
454 }
455
456 /**
457 * Returns the preference text color list color list.
458 *
459 * @return preference text color list color list
460 */
461 public static String[] getPrefTextColorList() {
462 return Arrays.copyOf(PREF_TEXT_COLOR_LIST, PREF_TEXT_COLOR_LIST.length);
463 }
464 }
This page took 0.053212 seconds and 4 git commands to generate.