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
9 * IBM - Initial API and implementation
10 * Bernd Hufmann - Updated for TMF
11 **********************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.preferences
;
15 import java
.util
.Arrays
;
16 import java
.util
.Collections
;
17 import java
.util
.Hashtable
;
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
;
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.
44 public class SDViewPref
implements ISDPreferences
, IPropertyChangeListener
{
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 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
63 * Postfix string for background color property
65 public static final String BACK_COLOR_POSTFIX
= "_BACK_COLOR";//$NON-NLS-1$
67 * Postfix string for foreground color property
69 public static final String FORE_COLOR_POSTFIX
= "_FORE_COLOR";//$NON-NLS-1$
71 * Postfix string for text color property
73 public static final String TEXT_COLOR_POSTFIX
= "_TEXT_COLOR";//$NON-NLS-1$
75 * Array of preference names
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
};
79 * A 2nd array of preference names
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
};
84 * Array of background color preference names
86 private static final String
[] PREF_BACK_COLOR_LIST
= { PREF_LIFELINE
, PREF_EXEC
, PREF_FRAME
, PREF_LIFELINE_HEADER
, PREF_FRAME_NAME
};
88 * Array of foreground color preference names
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
};
92 * Array of text color preference names
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
};
98 public static final String TEMP_TAG
= "_TEMP";//$NON-NLS-1$
100 // ------------------------------------------------------------------------
102 // ------------------------------------------------------------------------
105 * The sequence diagram preferences singleton instance
107 private static SDViewPref fHandle
= null;
109 * Hashtable for font preferences
111 private Map
<String
, IFont
> fFontPref
;
114 * Hashtable for foreground color preferences
116 private Map
<String
, IColor
> fForeColorPref
;
118 * Hashtable for background color preferences
120 private Map
<String
, IColor
> fBackColorPref
;
122 * Hashtable for text color preferences
124 private Map
<String
, IColor
> fTextColorPref
;
126 * The reference to the preference store.
128 private IPreferenceStore fPrefStore
= null;
130 * Color for the time compression selection
132 private IColor fTimeCompressionSelectionColor
= null;
134 * Flag whether no focus selection or not.
136 private boolean fNoFocusSelection
= false;
138 // ------------------------------------------------------------------------
140 // ------------------------------------------------------------------------
143 * Builds the Sequence Diagram preference handler: - Define the preference
144 * default values. - Load the currently used preferences setting
146 protected SDViewPref() {
147 fPrefStore
= Activator
.getDefault().getPreferenceStore();
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);
155 fFontPref
= new Hashtable
<>();
156 fForeColorPref
= new Hashtable
<>();
157 fBackColorPref
= new Hashtable
<>();
158 fTextColorPref
= new Hashtable
<>();
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]);
167 PreferenceConverter
.setDefault(fPrefStore
, FONT_LIST
[i
], Display
.getDefault().getSystemFont().getFontData());
168 PreferenceConverter
.setDefault(fPrefStore
, FONT_LIST
[i
] + TEMP_TAG
, Display
.getDefault().getSystemFont().getFontData());
172 for (int i
= 0; i
< PREF_BACK_COLOR_LIST
.length
; i
++) {
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
);
181 color
= new ColorImpl(Display
.getDefault(), BACKGROUND_DEFAULT_RGB
);
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());
188 for (int i
= 0; i
< PREF_FORE_COLOR_LIST
.length
; i
++) {
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
);
197 color
= new ColorImpl(Display
.getDefault(), FOREGROUND_RGB
);
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());
204 for (int i
= 0; i
< PREF_TEXT_COLOR_LIST
.length
; i
++) {
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
);
213 color
= new ColorImpl(Display
.getDefault(), DEFAULT_RGB
);
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());
220 IColor color
= new ColorImpl(Display
.getDefault(), DEFAULT_TIME_COMP_RGB
);
221 PreferenceConverter
.setDefault(fPrefStore
, PREF_TIME_COMP
, ((Color
) color
.getColor()).getRGB());
224 buildFontsAndColors();
226 fPrefStore
.addPropertyChangeListener(this);
230 * Returns the PreferenceStore
232 * @return the PreferenceStore
234 public IPreferenceStore
getPreferenceStore() {
239 * Apply the preferences in the preferences handler
241 public void apply() {
242 buildFontsAndColors();
243 fPrefStore
.firePropertyChangeEvent("PREFOK", null, null); //$NON-NLS-1$
247 * Returns an unique instance of the Sequence Diagram preference handler
249 * @return the preference handler instance
251 public static synchronized SDViewPref
getInstance() {
252 if (fHandle
== null) {
253 fHandle
= new SDViewPref();
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
);
263 return ColorImpl
.getSystemColor(SWT
.COLOR_BLACK
);
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
);
271 return ColorImpl
.getSystemColor(SWT
.COLOR_WHITE
);
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
);
279 return ColorImpl
.getSystemColor(SWT
.COLOR_BLACK
);
283 public IColor
getForeGroundColorSelection() {
284 if (fNoFocusSelection
) {
285 return ColorImpl
.getSystemColor(SWT
.COLOR_TITLE_INACTIVE_FOREGROUND
);
287 return ColorImpl
.getSystemColor(SWT
.COLOR_LIST_SELECTION_TEXT
);
291 public IColor
getBackGroundColorSelection() {
292 if (fNoFocusSelection
) {
293 return ColorImpl
.getSystemColor(SWT
.COLOR_WIDGET_BACKGROUND
);
295 return ColorImpl
.getSystemColor(SWT
.COLOR_LIST_SELECTION
);
299 public IFont
getFont(String prefName
) {
300 if (fFontPref
.get(prefName
) != null) {
301 return fFontPref
.get(prefName
);
303 return FontImpl
.getSystemFont();
307 * Returns the SwimLane width chosen
309 * @return the SwimLane width
311 public int getLifelineWidth() {
312 return fPrefStore
.getInt(PREF_LIFELINE_WIDTH
);
316 * Returns if font linkage with zoom has been chosen
318 * @return true if checked false otherwise
320 public boolean fontLinked() {
321 return fPrefStore
.getBoolean(PREF_LINK_FONT
);
325 * Returns the tooltip enablement
327 * @return true if checked false otherwise
329 public boolean tooltipEnabled() {
330 return fPrefStore
.getBoolean(PREF_TOOLTIP
);
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
338 * @return true if checked false otherwise
340 public boolean excludeExternalTime() {
341 return fPrefStore
.getBoolean(PREF_EXCLUDE_EXTERNAL_TIME
);
345 public boolean useGradienColor() {
346 return fPrefStore
.getBoolean(PREF_USE_GRADIENT
);
350 public IColor
getTimeCompressionSelectionColor() {
351 return fTimeCompressionSelectionColor
;
355 * Builds the new colors and fonts according the current user selection when
356 * the OK or Apply button is clicked
358 private void buildFontsAndColors() {
360 Display display
= Display
.getDefault();
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();
367 fFontPref
.put(FONT_LIST
[i
], new FontImpl(display
, fontData
));
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();
375 fBackColorPref
.put(PREF_BACK_COLOR_LIST
[i
] + BACK_COLOR_POSTFIX
, new ColorImpl(display
, rgb
.red
, rgb
.green
, rgb
.blue
));
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();
383 fForeColorPref
.put(PREF_FORE_COLOR_LIST
[i
] + FORE_COLOR_POSTFIX
, new ColorImpl(display
, rgb
.red
, rgb
.green
, rgb
.blue
));
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();
391 fTextColorPref
.put(PREF_TEXT_COLOR_LIST
[i
] + TEXT_COLOR_POSTFIX
, new ColorImpl(display
, rgb
.red
, rgb
.green
, rgb
.blue
));
394 RGB rgb
= PreferenceConverter
.getColor(fPrefStore
, PREF_TIME_COMP
);
395 if (fTimeCompressionSelectionColor
!= null) {
396 fTimeCompressionSelectionColor
.dispose();
398 fTimeCompressionSelectionColor
= new ColorImpl(display
, rgb
.red
, rgb
.green
, rgb
.blue
);
402 * Add a property-change listener
405 * The listener to add
407 public void addPropertyChangeListener(IPropertyChangeListener listener
) {
408 fPrefStore
.addPropertyChangeListener(listener
);
412 * Remove a property-change listener
415 * The listerner to remove
417 public void removePropertyChangeListener(IPropertyChangeListener listener
) {
418 fPrefStore
.removePropertyChangeListener(listener
);
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$
430 * Set the "no focus selection" preference
435 public void setNoFocusSelection(boolean v
) {
436 fNoFocusSelection
= v
;
440 * Returns a unmodifiable map with font preferences.
442 * @return map with font preferences
444 protected Map
<String
, IFont
> getFontPref() {
445 return Collections
.unmodifiableMap(fFontPref
);
449 * Returns a unmodifiable map with foreground color preferences
451 * @return map with foreground color preferences
453 public Map
<String
, IColor
> getForeColorPref() {
454 return Collections
.unmodifiableMap(fForeColorPref
);
458 * Returns a unmodifiable map with background color preferences
460 * @return map with background color preferences
462 public Map
<String
, IColor
> getBackColorPref() {
463 return Collections
.unmodifiableMap(fBackColorPref
);
467 * Returns a unmodifiable map with text color preferences
469 * @return map with text color preferences
471 public Map
<String
, IColor
> getTextColorPref() {
472 return Collections
.unmodifiableMap(fTextColorPref
);
476 * Returns the preference store.
478 * @return the preference store
480 public IPreferenceStore
getPrefStore() {
485 * Returns flag about focus selection
487 * @return flag about focus selection
489 public boolean isNoFocusSelection() {
490 return fNoFocusSelection
;
494 * Returns the static font list.
496 * @return static font list
498 public static String
[] getFontList() {
499 return Arrays
.copyOf(FONT_LIST
, FONT_LIST
.length
);
503 * Returns the 2nd static font list.
505 * @return 2nd static font list
507 public static String
[] getFontList2() {
508 return Arrays
.copyOf(FONT_LIST2
, FONT_LIST2
.length
);
512 * Returns the preference background color list.
514 * @return preference background color list
516 public static String
[] getPrefBackColorList() {
517 return Arrays
.copyOf(PREF_BACK_COLOR_LIST
, PREF_BACK_COLOR_LIST
.length
);
521 * Returns the preference foreground color list.
523 * @return preference foreground color list
525 public static String
[] getPrefForeColorList() {
526 return Arrays
.copyOf(PREF_FORE_COLOR_LIST
, PREF_FORE_COLOR_LIST
.length
);
530 * Returns the preference text color list color list.
532 * @return preference text color list color list
534 public static String
[] getPrefTextColorList() {
535 return Arrays
.copyOf(PREF_TEXT_COLOR_LIST
, PREF_TEXT_COLOR_LIST
.length
);