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
9 * IBM - Initial API and implementation
10 * Bernd Hufmann - Updated for TMF
11 **********************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.preferences
;
15 import java
.util
.Arrays
;
16 import java
.util
.Hashtable
;
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
;
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.
42 public class SDViewPref
implements ISDPreferences
, IPropertyChangeListener
{
44 // ------------------------------------------------------------------------
46 // ------------------------------------------------------------------------
48 * Postfix string for background color property
50 public static final String BACK_COLOR_POSTFIX
= "_BACK_COLOR";//$NON-NLS-1$
52 * Postfix string for foreground color property
54 public static final String FORE_COLOR_POSTFIX
= "_FORE_COLOR";//$NON-NLS-1$
56 * Postfix string for text color property
58 public static final String TEXT_COLOR_POSTFIX
= "_TEXT_COLOR";//$NON-NLS-1$
60 * Array of preference names
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
};
64 * A 2nd array of preference names
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
};
68 * Array of background color preference names
70 private static final String
[] PREF_BACK_COLOR_LIST
= { PREF_LIFELINE
, PREF_EXEC
, PREF_FRAME
, PREF_LIFELINE_HEADER
, PREF_FRAME_NAME
};
72 * Array of foreground color preference names
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
};
76 * Array of text color preference names
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
};
82 protected static final String TEMP_TAG
= "_TEMP";//$NON-NLS-1$
84 // ------------------------------------------------------------------------
86 // ------------------------------------------------------------------------
89 * The sequence diagram preferences singleton instance
91 private static SDViewPref fHandle
= null;
93 * Hashtable for font preferences
95 protected Map
<String
, IFont
> fFontPref
;
97 * Hashtable for foreground color preferences
99 protected Map
<String
, IColor
> fForeColorPref
;
101 * Hashtable for background color preferences
103 protected Map
<String
, IColor
> fBackColorPref
;
105 * Hashtable for text color preferences
107 protected Map
<String
, IColor
> fTextColorPref
;
109 * The reference to the preference store.
111 protected IPreferenceStore fPrefStore
= null;
113 * Color for the time compression selection
115 protected IColor fTimeCompressionSelectionColor
= null;
117 * Flag whether no focus selection or not.
119 protected boolean fNoFocusSelection
= false;
121 // ------------------------------------------------------------------------
123 // ------------------------------------------------------------------------
126 * Builds the Sequence Diagram preference handler: - Define the preference default values. - Load the currently used
127 * preferences setting
129 protected SDViewPref() {
130 fPrefStore
= Activator
.getDefault().getPreferenceStore();
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);
138 fFontPref
= new Hashtable
<String
, IFont
>();
139 fForeColorPref
= new Hashtable
<String
, IColor
>();
140 fBackColorPref
= new Hashtable
<String
, IColor
>();
141 fTextColorPref
= new Hashtable
<String
, IColor
>();
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]);
150 PreferenceConverter
.setDefault(fPrefStore
, FONT_LIST
[i
], Display
.getDefault().getSystemFont().getFontData());
151 PreferenceConverter
.setDefault(fPrefStore
, FONT_LIST
[i
] + TEMP_TAG
, Display
.getDefault().getSystemFont().getFontData());
155 for (int i
= 0; i
< PREF_BACK_COLOR_LIST
.length
; i
++) {
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);
164 color
= new ColorImpl(Display
.getDefault(), 255, 255, 255);
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());
171 for (int i
= 0; i
< PREF_FORE_COLOR_LIST
.length
; i
++) {
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);
180 color
= new ColorImpl(Display
.getDefault(), 134, 176, 212);
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());
187 for (int i
= 0; i
< PREF_TEXT_COLOR_LIST
.length
; i
++) {
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);
196 color
= new ColorImpl(Display
.getDefault(), 134, 176, 212);
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());
203 IColor color
= new ColorImpl(Display
.getDefault(), 218, 232, 238);
204 PreferenceConverter
.setDefault(fPrefStore
, PREF_TIME_COMP
, ((Color
) color
.getColor()).getRGB());
207 buildFontsAndColors();
209 fPrefStore
.addPropertyChangeListener(this);
213 * Returns the PreferenceStore
215 * @return the PreferenceStore
217 public IPreferenceStore
getPreferenceStore() {
222 * Apply the preferences in the preferences handler
224 public void apply() {
225 buildFontsAndColors();
226 fPrefStore
.firePropertyChangeEvent("PREFOK", null, null); //$NON-NLS-1$
230 * Returns an unique instance of the Sequence Diagram preference handler
232 * @return the preference handler instance
234 public static synchronized SDViewPref
getInstance() {
235 if (fHandle
== null) {
236 fHandle
= new SDViewPref();
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
);
246 return ColorImpl
.getSystemColor(SWT
.COLOR_BLACK
);
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
);
254 return ColorImpl
.getSystemColor(SWT
.COLOR_WHITE
);
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
);
262 return ColorImpl
.getSystemColor(SWT
.COLOR_BLACK
);
266 public IColor
getForeGroundColorSelection() {
267 if (fNoFocusSelection
) {
268 return ColorImpl
.getSystemColor(SWT
.COLOR_TITLE_INACTIVE_FOREGROUND
);
270 return ColorImpl
.getSystemColor(SWT
.COLOR_LIST_SELECTION_TEXT
);
274 public IColor
getBackGroundColorSelection() {
275 if (fNoFocusSelection
) {
276 return ColorImpl
.getSystemColor(SWT
.COLOR_WIDGET_BACKGROUND
);
278 return ColorImpl
.getSystemColor(SWT
.COLOR_LIST_SELECTION
);
282 public IFont
getFont(String prefName
) {
283 if (fFontPref
.get(prefName
) != null) {
284 return fFontPref
.get(prefName
);
286 return FontImpl
.getSystemFont();
290 * Returns the SwimLane width chosen
292 * @return the SwimLane width
294 public int getLifelineWidth() {
295 return fPrefStore
.getInt(PREF_LIFELINE_WIDTH
);
299 * Returns if font linkage with zoom has been chosen
301 * @return true if checked false otherwise
303 public boolean fontLinked() {
304 return fPrefStore
.getBoolean(PREF_LINK_FONT
);
308 * Returns the tooltip enablement
310 * @return true if checked false otherwise
312 public boolean tooltipEnabled() {
313 return fPrefStore
.getBoolean(PREF_TOOLTIP
);
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
320 * @return true if checked false otherwise
322 public boolean excludeExternalTime() {
323 return fPrefStore
.getBoolean(PREF_EXCLUDE_EXTERNAL_TIME
);
327 public boolean useGradienColor() {
328 return fPrefStore
.getBoolean(PREF_USE_GRADIENT
);
332 public IColor
getTimeCompressionSelectionColor() {
333 return fTimeCompressionSelectionColor
;
337 * Builds the new colors and fonts according the current user selection when the OK or Apply button is clicked
339 private void buildFontsAndColors() {
341 Display display
= Display
.getDefault();
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();
348 fFontPref
.put(FONT_LIST
[i
], new FontImpl(display
, fontData
));
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();
356 fBackColorPref
.put(PREF_BACK_COLOR_LIST
[i
] + BACK_COLOR_POSTFIX
, new ColorImpl(display
, rgb
.red
, rgb
.green
, rgb
.blue
));
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();
364 fForeColorPref
.put(PREF_FORE_COLOR_LIST
[i
] + FORE_COLOR_POSTFIX
, new ColorImpl(display
, rgb
.red
, rgb
.green
, rgb
.blue
));
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();
372 fTextColorPref
.put(PREF_TEXT_COLOR_LIST
[i
] + TEXT_COLOR_POSTFIX
, new ColorImpl(display
, rgb
.red
, rgb
.green
, rgb
.blue
));
375 RGB rgb
= PreferenceConverter
.getColor(fPrefStore
, PREF_TIME_COMP
);
376 if (fTimeCompressionSelectionColor
!= null) {
377 fTimeCompressionSelectionColor
.dispose();
379 fTimeCompressionSelectionColor
= new ColorImpl(display
, rgb
.red
, rgb
.green
, rgb
.blue
);
383 * Add a property-change listener
386 * The listener to add
388 public void addPropertyChangeListener(IPropertyChangeListener listener
) {
389 fPrefStore
.addPropertyChangeListener(listener
);
393 * Remove a property-change listener
396 * The listerner to remove
398 public void removePropertyChangeListener(IPropertyChangeListener listener
) {
399 fPrefStore
.removePropertyChangeListener(listener
);
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$
411 * Set the "no focus selection" preference
416 public void setNoFocusSelection(boolean v
) {
417 fNoFocusSelection
= v
;
421 * Returns the static font list.
423 * @return static font list
425 public static String
[] getFontList() {
426 return Arrays
.copyOf(FONT_LIST
, FONT_LIST
.length
);
430 * Returns the 2nd static font list.
432 * @return 2nd static font list
434 public static String
[] getFontList2() {
435 return Arrays
.copyOf(FONT_LIST2
, FONT_LIST2
.length
);
439 * Returns the preference background color list.
441 * @return preference background color list
443 public static String
[] getPrefBackColorList() {
444 return Arrays
.copyOf(PREF_BACK_COLOR_LIST
, PREF_BACK_COLOR_LIST
.length
);
448 * Returns the preference foreground color list.
450 * @return preference foreground color list
452 public static String
[] getPrefForeColorList() {
453 return Arrays
.copyOf(PREF_FORE_COLOR_LIST
, PREF_FORE_COLOR_LIST
.length
);
457 * Returns the preference text color list color list.
459 * @return preference text color list color list
461 public static String
[] getPrefTextColorList() {
462 return Arrays
.copyOf(PREF_TEXT_COLOR_LIST
, PREF_TEXT_COLOR_LIST
.length
);