tmf: Update copyright headers in tmf.ui
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / preferences / SDViewPref.java
1 /**********************************************************************
2 * Copyright (c) 2005, 2012 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.SDMessages;
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 = { SDMessages._88, SDMessages._89, SDMessages._90, SDMessages._91, SDMessages._92, SDMessages._93, SDMessages._94, SDMessages._95, SDMessages._96 };
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 /*
242 * (non-Javadoc)
243 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.ISDPreferences#getForeGroundColor(java.lang.String)
244 */
245 @Override
246 public IColor getForeGroundColor(String prefName) {
247 if ((fForeColorPref.get(prefName + FORE_COLOR_POSTFIX) != null) && (fForeColorPref.get(prefName + FORE_COLOR_POSTFIX) instanceof ColorImpl)) {
248 return fForeColorPref.get(prefName + FORE_COLOR_POSTFIX);
249 }
250 return ColorImpl.getSystemColor(SWT.COLOR_BLACK);
251 }
252
253 /*
254 * (non-Javadoc)
255 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.ISDPreferences#getBackGroundColor(java.lang.String)
256 */
257 @Override
258 public IColor getBackGroundColor(String prefName) {
259 if ((fBackColorPref.get(prefName + BACK_COLOR_POSTFIX) != null) && (fBackColorPref.get(prefName + BACK_COLOR_POSTFIX) instanceof ColorImpl)) {
260 return fBackColorPref.get(prefName + BACK_COLOR_POSTFIX);
261 }
262 return ColorImpl.getSystemColor(SWT.COLOR_WHITE);
263 }
264
265 /*
266 * (non-Javadoc)
267 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.ISDPreferences#getFontColor(java.lang.String)
268 */
269 @Override
270 public IColor getFontColor(String prefName) {
271 if ((fTextColorPref.get(prefName + TEXT_COLOR_POSTFIX) != null) && (fTextColorPref.get(prefName + TEXT_COLOR_POSTFIX) instanceof ColorImpl)) {
272 return fTextColorPref.get(prefName + TEXT_COLOR_POSTFIX);
273 }
274 return ColorImpl.getSystemColor(SWT.COLOR_BLACK);
275 }
276
277 /*
278 * (non-Javadoc)
279 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.ISDPreferences#getForeGroundColorSelection()
280 */
281 @Override
282 public IColor getForeGroundColorSelection() {
283 if (fNoFocusSelection) {
284 return ColorImpl.getSystemColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND);
285 }
286 return ColorImpl.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT);
287 }
288
289 /*
290 * (non-Javadoc)
291 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.ISDPreferences#getBackGroundColorSelection()
292 */
293 @Override
294 public IColor getBackGroundColorSelection() {
295 if (fNoFocusSelection) {
296 return ColorImpl.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
297 }
298 return ColorImpl.getSystemColor(SWT.COLOR_LIST_SELECTION);
299 }
300
301 /*
302 * (non-Javadoc)
303 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.ISDPreferences#getFont(java.lang.String)
304 */
305 @Override
306 public IFont getFont(String prefName) {
307 if (fFontPref.get(prefName) != null) {
308 return fFontPref.get(prefName);
309 }
310 return FontImpl.getSystemFont();
311 }
312
313 /**
314 * Returns the SwimLane width chosen
315 *
316 * @return the SwimLane width
317 */
318 public int getLifelineWidth() {
319 return fPrefStore.getInt(PREF_LIFELINE_WIDTH);
320 }
321
322 /**
323 * Returns if font linkage with zoom has been chosen
324 *
325 * @return true if checked false otherwise
326 */
327 public boolean fontLinked() {
328 return fPrefStore.getBoolean(PREF_LINK_FONT);
329 }
330
331 /**
332 * Returns the tooltip enablement
333 *
334 * @return true if checked false otherwise
335 */
336 public boolean tooltipEnabled() {
337 return fPrefStore.getBoolean(PREF_TOOLTIP);
338 }
339
340 /**
341 * Return true if the user do not want to take external time (basically found and lost messages with time) into
342 * account in the min max computation
343 *
344 * @return true if checked false otherwise
345 */
346 public boolean excludeExternalTime() {
347 return fPrefStore.getBoolean(PREF_EXCLUDE_EXTERNAL_TIME);
348 }
349
350 /*
351 * (non-Javadoc)
352 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.ISDPreferences#useGradienColor()
353 */
354 @Override
355 public boolean useGradienColor() {
356 return fPrefStore.getBoolean(PREF_USE_GRADIENT);
357 }
358
359 /*
360 * (non-Javadoc)
361 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.ISDPreferences#getTimeCompressionSelectionColor()
362 */
363 @Override
364 public IColor getTimeCompressionSelectionColor() {
365 return fTimeCompressionSelectionColor;
366 }
367
368 /**
369 * Builds the new colors and fonts according the current user selection when the OK or Apply button is clicked
370 */
371 private void buildFontsAndColors() {
372
373 Display display = Display.getDefault();
374
375 for (int i = 0; i < FONT_LIST.length; i++) {
376 FontData fontData = PreferenceConverter.getFontData(fPrefStore, FONT_LIST[i]);
377 if (fFontPref.get(FONT_LIST[i]) != null) {
378 fFontPref.get(FONT_LIST[i]).dispose();
379 }
380 fFontPref.put(FONT_LIST[i], new FontImpl(display, fontData));
381 }
382
383 for (int i = 0; i < PREF_BACK_COLOR_LIST.length; i++) {
384 RGB rgb = PreferenceConverter.getColor(fPrefStore, PREF_BACK_COLOR_LIST[i] + BACK_COLOR_POSTFIX);
385 if (fBackColorPref.get(PREF_BACK_COLOR_LIST[i] + BACK_COLOR_POSTFIX) != null) {
386 fBackColorPref.get(PREF_BACK_COLOR_LIST[i] + BACK_COLOR_POSTFIX).dispose();
387 }
388 fBackColorPref.put(PREF_BACK_COLOR_LIST[i] + BACK_COLOR_POSTFIX, new ColorImpl(display, rgb.red, rgb.green, rgb.blue));
389 }
390
391 for (int i = 0; i < PREF_FORE_COLOR_LIST.length; i++) {
392 RGB rgb = PreferenceConverter.getColor(fPrefStore, PREF_FORE_COLOR_LIST[i] + FORE_COLOR_POSTFIX);
393 if (fForeColorPref.get(PREF_FORE_COLOR_LIST[i] + FORE_COLOR_POSTFIX) != null) {
394 fForeColorPref.get(PREF_FORE_COLOR_LIST[i] + FORE_COLOR_POSTFIX).dispose();
395 }
396 fForeColorPref.put(PREF_FORE_COLOR_LIST[i] + FORE_COLOR_POSTFIX, new ColorImpl(display, rgb.red, rgb.green, rgb.blue));
397 }
398
399 for (int i = 0; i < PREF_TEXT_COLOR_LIST.length; i++) {
400 RGB rgb = PreferenceConverter.getColor(fPrefStore, PREF_TEXT_COLOR_LIST[i] + TEXT_COLOR_POSTFIX);
401 if (fTextColorPref.get(PREF_TEXT_COLOR_LIST[i] + TEXT_COLOR_POSTFIX) != null) {
402 fTextColorPref.get(PREF_TEXT_COLOR_LIST[i] + TEXT_COLOR_POSTFIX).dispose();
403 }
404 fTextColorPref.put(PREF_TEXT_COLOR_LIST[i] + TEXT_COLOR_POSTFIX, new ColorImpl(display, rgb.red, rgb.green, rgb.blue));
405 }
406
407 RGB rgb = PreferenceConverter.getColor(fPrefStore, PREF_TIME_COMP);
408 if (fTimeCompressionSelectionColor != null) {
409 fTimeCompressionSelectionColor.dispose();
410 }
411 fTimeCompressionSelectionColor = new ColorImpl(display, rgb.red, rgb.green, rgb.blue);
412 }
413
414 /**
415 * Add a property-change listener
416 *
417 * @param listener
418 * The listener to add
419 */
420 public void addPropertyChangeListener(IPropertyChangeListener listener) {
421 fPrefStore.addPropertyChangeListener(listener);
422 }
423
424 /**
425 * Remove a property-change listener
426 *
427 * @param listener
428 * The listerner to remove
429 */
430 public void removePropertyChangeListener(IPropertyChangeListener listener) {
431 fPrefStore.removePropertyChangeListener(listener);
432 }
433
434 /*
435 * (non-Javadoc)
436 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
437 */
438 @Override
439 public void propertyChange(PropertyChangeEvent event) {
440 if (!event.getProperty().equals("PREFOK")) { //$NON-NLS-1$
441 buildFontsAndColors();
442 fPrefStore.firePropertyChangeEvent("PREFOK", null, null); //$NON-NLS-1$
443 }
444 }
445
446 /**
447 * Set the "no focus selection" preference
448 *
449 * @param v
450 * New value to use
451 */
452 public void setNoFocusSelection(boolean v) {
453 fNoFocusSelection = v;
454 }
455
456 /**
457 * Returns the static font list.
458 *
459 * @return static font list
460 */
461 public static String[] getFontList() {
462 return Arrays.copyOf(FONT_LIST, FONT_LIST.length);
463 }
464
465 /**
466 * Returns the 2nd static font list.
467 *
468 * @return 2nd static font list
469 */
470 public static String[] getFontList2() {
471 return Arrays.copyOf(FONT_LIST2, FONT_LIST2.length);
472 }
473
474 /**
475 * Returns the preference background color list.
476 *
477 * @return preference background color list
478 */
479 public static String[] getPrefBackColorList() {
480 return Arrays.copyOf(PREF_BACK_COLOR_LIST, PREF_BACK_COLOR_LIST.length);
481 }
482
483 /**
484 * Returns the preference foreground color list.
485 *
486 * @return preference foreground color list
487 */
488 public static String[] getPrefForeColorList() {
489 return Arrays.copyOf(PREF_FORE_COLOR_LIST, PREF_FORE_COLOR_LIST.length);
490 }
491
492 /**
493 * Returns the preference text color list color list.
494 *
495 * @return preference text color list color list
496 */
497 public static String[] getPrefTextColorList() {
498 return Arrays.copyOf(PREF_TEXT_COLOR_LIST, PREF_TEXT_COLOR_LIST.length);
499 }
500 }
This page took 0.041048 seconds and 5 git commands to generate.