tmf: Switch tmf.ui to Java 7 + fix warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / preferences / SDViewPref.java
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
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.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.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;
35
36 /**
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.
39 *
40 * @version 1.0
41 * @author sveyrier
42 */
43 public class SDViewPref implements ISDPreferences, IPropertyChangeListener {
44
45 // ------------------------------------------------------------------------
46 // Constants
47 // ------------------------------------------------------------------------
48 /**
49 * Postfix string for background color property
50 */
51 public static final String BACK_COLOR_POSTFIX = "_BACK_COLOR";//$NON-NLS-1$
52 /**
53 * Postfix string for foreground color property
54 */
55 public static final String FORE_COLOR_POSTFIX = "_FORE_COLOR";//$NON-NLS-1$
56 /**
57 * Postfix string for text color property
58 */
59 public static final String TEXT_COLOR_POSTFIX = "_TEXT_COLOR";//$NON-NLS-1$
60 /**
61 * Array of preference names
62 */
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 };
64 /**
65 * A 2nd array of preference names
66 */
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 };
68 /**
69 * Array of background color preference names
70 */
71 private static final String[] PREF_BACK_COLOR_LIST = { PREF_LIFELINE, PREF_EXEC, PREF_FRAME, PREF_LIFELINE_HEADER, PREF_FRAME_NAME };
72 /**
73 * Array of foreground color preference names
74 */
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 };
76 /**
77 * Array of text color preference names
78 */
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 };
80 /**
81 * Temporary tag
82 * @since 2.0
83 */
84 public static final String TEMP_TAG = "_TEMP";//$NON-NLS-1$
85
86 // ------------------------------------------------------------------------
87 // Attributes
88 // ------------------------------------------------------------------------
89
90 /**
91 * The sequence diagram preferences singleton instance
92 */
93 private static SDViewPref fHandle = null;
94 /**
95 * Hashtable for font preferences
96 */
97 private Map<String, IFont> fFontPref;
98
99 /**
100 * Hashtable for foreground color preferences
101 */
102 private Map<String, IColor> fForeColorPref;
103 /**
104 * Hashtable for background color preferences
105 */
106 private Map<String, IColor> fBackColorPref;
107 /**
108 * Hashtable for text color preferences
109 */
110 private Map<String, IColor> fTextColorPref;
111 /**
112 * The reference to the preference store.
113 */
114 private IPreferenceStore fPrefStore = null;
115 /**
116 * Color for the time compression selection
117 */
118 private IColor fTimeCompressionSelectionColor = null;
119 /**
120 * Flag whether no focus selection or not.
121 */
122 private boolean fNoFocusSelection = false;
123
124 // ------------------------------------------------------------------------
125 // Constructors
126 // ------------------------------------------------------------------------
127
128 /**
129 * Builds the Sequence Diagram preference handler: - Define the preference default values. - Load the currently used
130 * preferences setting
131 */
132 protected SDViewPref() {
133 fPrefStore = Activator.getDefault().getPreferenceStore();
134
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);
140
141 fFontPref = new Hashtable<>();
142 fForeColorPref = new Hashtable<>();
143 fBackColorPref = new Hashtable<>();
144 fTextColorPref = new Hashtable<>();
145
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]);
152 } else {
153 PreferenceConverter.setDefault(fPrefStore, FONT_LIST[i], Display.getDefault().getSystemFont().getFontData());
154 PreferenceConverter.setDefault(fPrefStore, FONT_LIST[i] + TEMP_TAG, Display.getDefault().getSystemFont().getFontData());
155 }
156 }
157
158 for (int i = 0; i < PREF_BACK_COLOR_LIST.length; i++) {
159 IColor color;
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);
166 } else {
167 color = new ColorImpl(Display.getDefault(), 255, 255, 255);
168 }
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());
171 color.dispose();
172 }
173
174 for (int i = 0; i < PREF_FORE_COLOR_LIST.length; i++) {
175 IColor color;
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);
182 } else {
183 color = new ColorImpl(Display.getDefault(), 134, 176, 212);
184 }
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());
187 color.dispose();
188 }
189
190 for (int i = 0; i < PREF_TEXT_COLOR_LIST.length; i++) {
191 IColor color;
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);
198 } else {
199 color = new ColorImpl(Display.getDefault(), 134, 176, 212);
200 }
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());
203 color.dispose();
204 }
205
206 IColor color = new ColorImpl(Display.getDefault(), 218, 232, 238);
207 PreferenceConverter.setDefault(fPrefStore, PREF_TIME_COMP, ((Color) color.getColor()).getRGB());
208 color.dispose();
209
210 buildFontsAndColors();
211
212 fPrefStore.addPropertyChangeListener(this);
213 }
214
215 /**
216 * Returns the PreferenceStore
217 *
218 * @return the PreferenceStore
219 */
220 public IPreferenceStore getPreferenceStore() {
221 return fPrefStore;
222 }
223
224 /**
225 * Apply the preferences in the preferences handler
226 */
227 public void apply() {
228 buildFontsAndColors();
229 fPrefStore.firePropertyChangeEvent("PREFOK", null, null); //$NON-NLS-1$
230 }
231
232 /**
233 * Returns an unique instance of the Sequence Diagram preference handler
234 *
235 * @return the preference handler instance
236 */
237 public static synchronized SDViewPref getInstance() {
238 if (fHandle == null) {
239 fHandle = new SDViewPref();
240 }
241 return fHandle;
242 }
243
244 @Override
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);
248 }
249 return ColorImpl.getSystemColor(SWT.COLOR_BLACK);
250 }
251
252 @Override
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);
256 }
257 return ColorImpl.getSystemColor(SWT.COLOR_WHITE);
258 }
259
260 @Override
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);
264 }
265 return ColorImpl.getSystemColor(SWT.COLOR_BLACK);
266 }
267
268 @Override
269 public IColor getForeGroundColorSelection() {
270 if (fNoFocusSelection) {
271 return ColorImpl.getSystemColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND);
272 }
273 return ColorImpl.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT);
274 }
275
276 @Override
277 public IColor getBackGroundColorSelection() {
278 if (fNoFocusSelection) {
279 return ColorImpl.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
280 }
281 return ColorImpl.getSystemColor(SWT.COLOR_LIST_SELECTION);
282 }
283
284 @Override
285 public IFont getFont(String prefName) {
286 if (fFontPref.get(prefName) != null) {
287 return fFontPref.get(prefName);
288 }
289 return FontImpl.getSystemFont();
290 }
291
292 /**
293 * Returns the SwimLane width chosen
294 *
295 * @return the SwimLane width
296 */
297 public int getLifelineWidth() {
298 return fPrefStore.getInt(PREF_LIFELINE_WIDTH);
299 }
300
301 /**
302 * Returns if font linkage with zoom has been chosen
303 *
304 * @return true if checked false otherwise
305 */
306 public boolean fontLinked() {
307 return fPrefStore.getBoolean(PREF_LINK_FONT);
308 }
309
310 /**
311 * Returns the tooltip enablement
312 *
313 * @return true if checked false otherwise
314 */
315 public boolean tooltipEnabled() {
316 return fPrefStore.getBoolean(PREF_TOOLTIP);
317 }
318
319 /**
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
322 *
323 * @return true if checked false otherwise
324 */
325 public boolean excludeExternalTime() {
326 return fPrefStore.getBoolean(PREF_EXCLUDE_EXTERNAL_TIME);
327 }
328
329 @Override
330 public boolean useGradienColor() {
331 return fPrefStore.getBoolean(PREF_USE_GRADIENT);
332 }
333
334 @Override
335 public IColor getTimeCompressionSelectionColor() {
336 return fTimeCompressionSelectionColor;
337 }
338
339 /**
340 * Builds the new colors and fonts according the current user selection when the OK or Apply button is clicked
341 */
342 private void buildFontsAndColors() {
343
344 Display display = Display.getDefault();
345
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();
350 }
351 fFontPref.put(FONT_LIST[i], new FontImpl(display, fontData));
352 }
353
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();
358 }
359 fBackColorPref.put(PREF_BACK_COLOR_LIST[i] + BACK_COLOR_POSTFIX, new ColorImpl(display, rgb.red, rgb.green, rgb.blue));
360 }
361
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();
366 }
367 fForeColorPref.put(PREF_FORE_COLOR_LIST[i] + FORE_COLOR_POSTFIX, new ColorImpl(display, rgb.red, rgb.green, rgb.blue));
368 }
369
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();
374 }
375 fTextColorPref.put(PREF_TEXT_COLOR_LIST[i] + TEXT_COLOR_POSTFIX, new ColorImpl(display, rgb.red, rgb.green, rgb.blue));
376 }
377
378 RGB rgb = PreferenceConverter.getColor(fPrefStore, PREF_TIME_COMP);
379 if (fTimeCompressionSelectionColor != null) {
380 fTimeCompressionSelectionColor.dispose();
381 }
382 fTimeCompressionSelectionColor = new ColorImpl(display, rgb.red, rgb.green, rgb.blue);
383 }
384
385 /**
386 * Add a property-change listener
387 *
388 * @param listener
389 * The listener to add
390 */
391 public void addPropertyChangeListener(IPropertyChangeListener listener) {
392 fPrefStore.addPropertyChangeListener(listener);
393 }
394
395 /**
396 * Remove a property-change listener
397 *
398 * @param listener
399 * The listerner to remove
400 */
401 public void removePropertyChangeListener(IPropertyChangeListener listener) {
402 fPrefStore.removePropertyChangeListener(listener);
403 }
404
405 @Override
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$
410 }
411 }
412
413 /**
414 * Set the "no focus selection" preference
415 *
416 * @param v
417 * New value to use
418 */
419 public void setNoFocusSelection(boolean v) {
420 fNoFocusSelection = v;
421 }
422
423 /**
424 * Returns a unmodifiable map with font preferences.
425 *
426 * @return map with font preferences
427 * @since 2.0
428 */
429 protected Map<String, IFont> getFontPref() {
430 return Collections.unmodifiableMap(fFontPref);
431 }
432
433 /**
434 * Returns a unmodifiable map with foreground color preferences
435 *
436 * @return map with foreground color preferences
437 * @since 2.0
438 */
439 public Map<String, IColor> getForeColorPref() {
440 return Collections.unmodifiableMap(fForeColorPref);
441 }
442
443 /**
444 * Returns a unmodifiable map with background color preferences
445 *
446 * @return map with background color preferences
447 * @since 2.0
448 */
449 public Map<String, IColor> getBackColorPref() {
450 return Collections.unmodifiableMap(fBackColorPref);
451 }
452
453 /**
454 * Returns a unmodifiable map with text color preferences
455 *
456 * @return map with text color preferences
457 * @since 2.0
458 */
459 public Map<String, IColor> getTextColorPref() {
460 return Collections.unmodifiableMap(fTextColorPref);
461 }
462
463 /**
464 * Returns the preference store.
465 *
466 * @return the preference store
467 * @since 2.0
468 */
469 public IPreferenceStore getPrefStore() {
470 return fPrefStore;
471 }
472
473 /**
474 * Returns flag about focus selection
475 *
476 * @return flag about focus selection
477 * @since 2.0
478 */
479 public boolean isNoFocusSelection() {
480 return fNoFocusSelection;
481 }
482
483 /**
484 * Returns the static font list.
485 *
486 * @return static font list
487 */
488 public static String[] getFontList() {
489 return Arrays.copyOf(FONT_LIST, FONT_LIST.length);
490 }
491
492 /**
493 * Returns the 2nd static font list.
494 *
495 * @return 2nd static font list
496 */
497 public static String[] getFontList2() {
498 return Arrays.copyOf(FONT_LIST2, FONT_LIST2.length);
499 }
500
501 /**
502 * Returns the preference background color list.
503 *
504 * @return preference background color list
505 */
506 public static String[] getPrefBackColorList() {
507 return Arrays.copyOf(PREF_BACK_COLOR_LIST, PREF_BACK_COLOR_LIST.length);
508 }
509
510 /**
511 * Returns the preference foreground color list.
512 *
513 * @return preference foreground color list
514 */
515 public static String[] getPrefForeColorList() {
516 return Arrays.copyOf(PREF_FORE_COLOR_LIST, PREF_FORE_COLOR_LIST.length);
517 }
518
519 /**
520 * Returns the preference text color list color list.
521 *
522 * @return preference text color list color list
523 */
524 public static String[] getPrefTextColorList() {
525 return Arrays.copyOf(PREF_TEXT_COLOR_LIST, PREF_TEXT_COLOR_LIST.length);
526 }
527 }
This page took 0.045328 seconds and 5 git commands to generate.