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