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