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
.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
.linuxtools
.internal
.tmf
.ui
.Activator
;
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
.Messages
;
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
;
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.
43 public class SDViewPref
implements ISDPreferences
, IPropertyChangeListener
{
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
49 * Postfix string for background color property
51 public static final String BACK_COLOR_POSTFIX
= "_BACK_COLOR";//$NON-NLS-1$
53 * Postfix string for foreground color property
55 public static final String FORE_COLOR_POSTFIX
= "_FORE_COLOR";//$NON-NLS-1$
57 * Postfix string for text color property
59 public static final String TEXT_COLOR_POSTFIX
= "_TEXT_COLOR";//$NON-NLS-1$
61 * Array of preference names
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
};
65 * A 2nd array of preference names
67 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
};
69 * Array of background color preference names
71 private static final String
[] PREF_BACK_COLOR_LIST
= { PREF_LIFELINE
, PREF_EXEC
, PREF_FRAME
, PREF_LIFELINE_HEADER
, PREF_FRAME_NAME
};
73 * Array of foreground color preference names
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
};
77 * Array of text color preference names
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
};
84 public static final String TEMP_TAG
= "_TEMP";//$NON-NLS-1$
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
91 * The sequence diagram preferences singleton instance
93 private static SDViewPref fHandle
= null;
95 * Hashtable for font preferences
97 private Map
<String
, IFont
> fFontPref
;
100 * Hashtable for foreground color preferences
102 private Map
<String
, IColor
> fForeColorPref
;
104 * Hashtable for background color preferences
106 private Map
<String
, IColor
> fBackColorPref
;
108 * Hashtable for text color preferences
110 private Map
<String
, IColor
> fTextColorPref
;
112 * The reference to the preference store.
114 private IPreferenceStore fPrefStore
= null;
116 * Color for the time compression selection
118 private IColor fTimeCompressionSelectionColor
= null;
120 * Flag whether no focus selection or not.
122 private boolean fNoFocusSelection
= false;
124 // ------------------------------------------------------------------------
126 // ------------------------------------------------------------------------
129 * Builds the Sequence Diagram preference handler: - Define the preference default values. - Load the currently used
130 * preferences setting
132 protected SDViewPref() {
133 fPrefStore
= Activator
.getDefault().getPreferenceStore();
135 fPrefStore
.setDefault(PREF_LINK_FONT
, true);
136 fPrefStore
.setDefault(PREF_EXCLUDE_EXTERNAL_TIME
, true);
137 fPrefStore
.setDefault(PREF_LIFELINE_WIDTH
, 200);
138 fPrefStore
.setDefault(PREF_USE_GRADIENT
, true);
139 fPrefStore
.setDefault(PREF_TOOLTIP
, true);
141 fFontPref
= new Hashtable
<String
, IFont
>();
142 fForeColorPref
= new Hashtable
<String
, IColor
>();
143 fBackColorPref
= new Hashtable
<String
, IColor
>();
144 fTextColorPref
= new Hashtable
<String
, IColor
>();
146 for (int i
= 0; i
< FONT_LIST
.length
; i
++) {
147 if (FONT_LIST
[i
].equals(PREF_FRAME_NAME
)) {
148 FontData
[] data
= Display
.getDefault().getSystemFont().getFontData();
149 data
[0].setStyle(SWT
.BOLD
);
150 PreferenceConverter
.setDefault(fPrefStore
, FONT_LIST
[i
], data
[0]);
151 PreferenceConverter
.setDefault(fPrefStore
, FONT_LIST
[i
] + TEMP_TAG
, data
[0]);
153 PreferenceConverter
.setDefault(fPrefStore
, FONT_LIST
[i
], Display
.getDefault().getSystemFont().getFontData());
154 PreferenceConverter
.setDefault(fPrefStore
, FONT_LIST
[i
] + TEMP_TAG
, Display
.getDefault().getSystemFont().getFontData());
158 for (int i
= 0; i
< PREF_BACK_COLOR_LIST
.length
; i
++) {
160 if ((PREF_BACK_COLOR_LIST
[i
].equals(PREF_EXEC
)) || PREF_BACK_COLOR_LIST
[i
].equals(PREF_FRAME_NAME
)) {
161 color
= new ColorImpl(Display
.getDefault(), 201, 222, 233);
162 } else if (PREF_BACK_COLOR_LIST
[i
].equals(PREF_LIFELINE
)) {
163 color
= new ColorImpl(Display
.getDefault(), 220, 220, 220);
164 } else if (PREF_BACK_COLOR_LIST
[i
].equals(PREF_LIFELINE_HEADER
)) {
165 color
= new ColorImpl(Display
.getDefault(), 245, 244, 244);
167 color
= new ColorImpl(Display
.getDefault(), 255, 255, 255);
169 PreferenceConverter
.setDefault(fPrefStore
, PREF_BACK_COLOR_LIST
[i
] + BACK_COLOR_POSTFIX
, ((Color
) color
.getColor()).getRGB());
170 PreferenceConverter
.setDefault(fPrefStore
, PREF_BACK_COLOR_LIST
[i
] + BACK_COLOR_POSTFIX
+ TEMP_TAG
, ((Color
) color
.getColor()).getRGB());
174 for (int i
= 0; i
< PREF_FORE_COLOR_LIST
.length
; i
++) {
176 if (PREF_FORE_COLOR_LIST
[i
].equals(PREF_LIFELINE
)) {
177 color
= new ColorImpl(Display
.getDefault(), 129, 129, 129);
178 } else if (PREF_FORE_COLOR_LIST
[i
].equals(PREF_FRAME_NAME
)) {
179 color
= new ColorImpl(Display
.getDefault(), 81, 153, 200);
180 } else if (PREF_FORE_COLOR_LIST
[i
].equals(PREF_LIFELINE_HEADER
)) {
181 color
= new ColorImpl(Display
.getDefault(), 129, 127, 137);
183 color
= new ColorImpl(Display
.getDefault(), 134, 176, 212);
185 PreferenceConverter
.setDefault(fPrefStore
, PREF_FORE_COLOR_LIST
[i
] + FORE_COLOR_POSTFIX
, ((Color
) color
.getColor()).getRGB());
186 PreferenceConverter
.setDefault(fPrefStore
, PREF_FORE_COLOR_LIST
[i
] + FORE_COLOR_POSTFIX
+ TEMP_TAG
, ((Color
) color
.getColor()).getRGB());
190 for (int i
= 0; i
< PREF_TEXT_COLOR_LIST
.length
; i
++) {
192 if (PREF_TEXT_COLOR_LIST
[i
].equals(PREF_LIFELINE
)) {
193 color
= new ColorImpl(Display
.getDefault(), 129, 129, 129);
194 } else if (PREF_TEXT_COLOR_LIST
[i
].equals(PREF_FRAME_NAME
)) {
195 color
= new ColorImpl(Display
.getDefault(), 0, 0, 0);
196 } else if (PREF_TEXT_COLOR_LIST
[i
].equals(PREF_LIFELINE_HEADER
)) {
197 color
= new ColorImpl(Display
.getDefault(), 129, 127, 137);
199 color
= new ColorImpl(Display
.getDefault(), 134, 176, 212);
201 PreferenceConverter
.setDefault(fPrefStore
, PREF_TEXT_COLOR_LIST
[i
] + TEXT_COLOR_POSTFIX
, ((Color
) color
.getColor()).getRGB());
202 PreferenceConverter
.setDefault(fPrefStore
, PREF_TEXT_COLOR_LIST
[i
] + TEXT_COLOR_POSTFIX
+ TEMP_TAG
, ((Color
) color
.getColor()).getRGB());
206 IColor color
= new ColorImpl(Display
.getDefault(), 218, 232, 238);
207 PreferenceConverter
.setDefault(fPrefStore
, PREF_TIME_COMP
, ((Color
) color
.getColor()).getRGB());
210 buildFontsAndColors();
212 fPrefStore
.addPropertyChangeListener(this);
216 * Returns the PreferenceStore
218 * @return the PreferenceStore
220 public IPreferenceStore
getPreferenceStore() {
225 * Apply the preferences in the preferences handler
227 public void apply() {
228 buildFontsAndColors();
229 fPrefStore
.firePropertyChangeEvent("PREFOK", null, null); //$NON-NLS-1$
233 * Returns an unique instance of the Sequence Diagram preference handler
235 * @return the preference handler instance
237 public static synchronized SDViewPref
getInstance() {
238 if (fHandle
== null) {
239 fHandle
= new SDViewPref();
245 public IColor
getForeGroundColor(String prefName
) {
246 if ((fForeColorPref
.get(prefName
+ FORE_COLOR_POSTFIX
) != null) && (fForeColorPref
.get(prefName
+ FORE_COLOR_POSTFIX
) instanceof ColorImpl
)) {
247 return fForeColorPref
.get(prefName
+ FORE_COLOR_POSTFIX
);
249 return ColorImpl
.getSystemColor(SWT
.COLOR_BLACK
);
253 public IColor
getBackGroundColor(String prefName
) {
254 if ((fBackColorPref
.get(prefName
+ BACK_COLOR_POSTFIX
) != null) && (fBackColorPref
.get(prefName
+ BACK_COLOR_POSTFIX
) instanceof ColorImpl
)) {
255 return fBackColorPref
.get(prefName
+ BACK_COLOR_POSTFIX
);
257 return ColorImpl
.getSystemColor(SWT
.COLOR_WHITE
);
261 public IColor
getFontColor(String prefName
) {
262 if ((fTextColorPref
.get(prefName
+ TEXT_COLOR_POSTFIX
) != null) && (fTextColorPref
.get(prefName
+ TEXT_COLOR_POSTFIX
) instanceof ColorImpl
)) {
263 return fTextColorPref
.get(prefName
+ TEXT_COLOR_POSTFIX
);
265 return ColorImpl
.getSystemColor(SWT
.COLOR_BLACK
);
269 public IColor
getForeGroundColorSelection() {
270 if (fNoFocusSelection
) {
271 return ColorImpl
.getSystemColor(SWT
.COLOR_TITLE_INACTIVE_FOREGROUND
);
273 return ColorImpl
.getSystemColor(SWT
.COLOR_LIST_SELECTION_TEXT
);
277 public IColor
getBackGroundColorSelection() {
278 if (fNoFocusSelection
) {
279 return ColorImpl
.getSystemColor(SWT
.COLOR_WIDGET_BACKGROUND
);
281 return ColorImpl
.getSystemColor(SWT
.COLOR_LIST_SELECTION
);
285 public IFont
getFont(String prefName
) {
286 if (fFontPref
.get(prefName
) != null) {
287 return fFontPref
.get(prefName
);
289 return FontImpl
.getSystemFont();
293 * Returns the SwimLane width chosen
295 * @return the SwimLane width
297 public int getLifelineWidth() {
298 return fPrefStore
.getInt(PREF_LIFELINE_WIDTH
);
302 * Returns if font linkage with zoom has been chosen
304 * @return true if checked false otherwise
306 public boolean fontLinked() {
307 return fPrefStore
.getBoolean(PREF_LINK_FONT
);
311 * Returns the tooltip enablement
313 * @return true if checked false otherwise
315 public boolean tooltipEnabled() {
316 return fPrefStore
.getBoolean(PREF_TOOLTIP
);
320 * Return true if the user do not want to take external time (basically found and lost messages with time) into
321 * account in the min max computation
323 * @return true if checked false otherwise
325 public boolean excludeExternalTime() {
326 return fPrefStore
.getBoolean(PREF_EXCLUDE_EXTERNAL_TIME
);
330 public boolean useGradienColor() {
331 return fPrefStore
.getBoolean(PREF_USE_GRADIENT
);
335 public IColor
getTimeCompressionSelectionColor() {
336 return fTimeCompressionSelectionColor
;
340 * Builds the new colors and fonts according the current user selection when the OK or Apply button is clicked
342 private void buildFontsAndColors() {
344 Display display
= Display
.getDefault();
346 for (int i
= 0; i
< FONT_LIST
.length
; i
++) {
347 FontData fontData
= PreferenceConverter
.getFontData(fPrefStore
, FONT_LIST
[i
]);
348 if (fFontPref
.get(FONT_LIST
[i
]) != null) {
349 fFontPref
.get(FONT_LIST
[i
]).dispose();
351 fFontPref
.put(FONT_LIST
[i
], new FontImpl(display
, fontData
));
354 for (int i
= 0; i
< PREF_BACK_COLOR_LIST
.length
; i
++) {
355 RGB rgb
= PreferenceConverter
.getColor(fPrefStore
, PREF_BACK_COLOR_LIST
[i
] + BACK_COLOR_POSTFIX
);
356 if (fBackColorPref
.get(PREF_BACK_COLOR_LIST
[i
] + BACK_COLOR_POSTFIX
) != null) {
357 fBackColorPref
.get(PREF_BACK_COLOR_LIST
[i
] + BACK_COLOR_POSTFIX
).dispose();
359 fBackColorPref
.put(PREF_BACK_COLOR_LIST
[i
] + BACK_COLOR_POSTFIX
, new ColorImpl(display
, rgb
.red
, rgb
.green
, rgb
.blue
));
362 for (int i
= 0; i
< PREF_FORE_COLOR_LIST
.length
; i
++) {
363 RGB rgb
= PreferenceConverter
.getColor(fPrefStore
, PREF_FORE_COLOR_LIST
[i
] + FORE_COLOR_POSTFIX
);
364 if (fForeColorPref
.get(PREF_FORE_COLOR_LIST
[i
] + FORE_COLOR_POSTFIX
) != null) {
365 fForeColorPref
.get(PREF_FORE_COLOR_LIST
[i
] + FORE_COLOR_POSTFIX
).dispose();
367 fForeColorPref
.put(PREF_FORE_COLOR_LIST
[i
] + FORE_COLOR_POSTFIX
, new ColorImpl(display
, rgb
.red
, rgb
.green
, rgb
.blue
));
370 for (int i
= 0; i
< PREF_TEXT_COLOR_LIST
.length
; i
++) {
371 RGB rgb
= PreferenceConverter
.getColor(fPrefStore
, PREF_TEXT_COLOR_LIST
[i
] + TEXT_COLOR_POSTFIX
);
372 if (fTextColorPref
.get(PREF_TEXT_COLOR_LIST
[i
] + TEXT_COLOR_POSTFIX
) != null) {
373 fTextColorPref
.get(PREF_TEXT_COLOR_LIST
[i
] + TEXT_COLOR_POSTFIX
).dispose();
375 fTextColorPref
.put(PREF_TEXT_COLOR_LIST
[i
] + TEXT_COLOR_POSTFIX
, new ColorImpl(display
, rgb
.red
, rgb
.green
, rgb
.blue
));
378 RGB rgb
= PreferenceConverter
.getColor(fPrefStore
, PREF_TIME_COMP
);
379 if (fTimeCompressionSelectionColor
!= null) {
380 fTimeCompressionSelectionColor
.dispose();
382 fTimeCompressionSelectionColor
= new ColorImpl(display
, rgb
.red
, rgb
.green
, rgb
.blue
);
386 * Add a property-change listener
389 * The listener to add
391 public void addPropertyChangeListener(IPropertyChangeListener listener
) {
392 fPrefStore
.addPropertyChangeListener(listener
);
396 * Remove a property-change listener
399 * The listerner to remove
401 public void removePropertyChangeListener(IPropertyChangeListener listener
) {
402 fPrefStore
.removePropertyChangeListener(listener
);
406 public void propertyChange(PropertyChangeEvent event
) {
407 if (!event
.getProperty().equals("PREFOK")) { //$NON-NLS-1$
408 buildFontsAndColors();
409 fPrefStore
.firePropertyChangeEvent("PREFOK", null, null); //$NON-NLS-1$
414 * Set the "no focus selection" preference
419 public void setNoFocusSelection(boolean v
) {
420 fNoFocusSelection
= v
;
424 * Returns a unmodifiable map with font preferences.
426 * @return map with font preferences
429 protected Map
<String
, IFont
> getFontPref() {
430 return Collections
.unmodifiableMap(fFontPref
);
434 * Returns a unmodifiable map with foreground color preferences
436 * @return map with foreground color preferences
439 public Map
<String
, IColor
> getForeColorPref() {
440 return Collections
.unmodifiableMap(fForeColorPref
);
444 * Returns a unmodifiable map with background color preferences
446 * @return map with background color preferences
449 public Map
<String
, IColor
> getBackColorPref() {
450 return Collections
.unmodifiableMap(fBackColorPref
);
454 * Returns a unmodifiable map with text color preferences
456 * @return map with text color preferences
459 public Map
<String
, IColor
> getTextColorPref() {
460 return Collections
.unmodifiableMap(fTextColorPref
);
464 * Returns the preference store.
466 * @return the preference store
469 public IPreferenceStore
getPrefStore() {
474 * Returns flag about focus selection
476 * @return flag about focus selection
479 public boolean isNoFocusSelection() {
480 return fNoFocusSelection
;
484 * Returns the static font list.
486 * @return static font list
488 public static String
[] getFontList() {
489 return Arrays
.copyOf(FONT_LIST
, FONT_LIST
.length
);
493 * Returns the 2nd static font list.
495 * @return 2nd static font list
497 public static String
[] getFontList2() {
498 return Arrays
.copyOf(FONT_LIST2
, FONT_LIST2
.length
);
502 * Returns the preference background color list.
504 * @return preference background color list
506 public static String
[] getPrefBackColorList() {
507 return Arrays
.copyOf(PREF_BACK_COLOR_LIST
, PREF_BACK_COLOR_LIST
.length
);
511 * Returns the preference foreground color list.
513 * @return preference foreground color list
515 public static String
[] getPrefForeColorList() {
516 return Arrays
.copyOf(PREF_FORE_COLOR_LIST
, PREF_FORE_COLOR_LIST
.length
);
520 * Returns the preference text color list color list.
522 * @return preference text color list color list
524 public static String
[] getPrefTextColorList() {
525 return Arrays
.copyOf(PREF_TEXT_COLOR_LIST
, PREF_TEXT_COLOR_LIST
.length
);