| 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.Iterator; |
| 16 | import java.util.Set; |
| 17 | |
| 18 | import org.eclipse.jface.dialogs.Dialog; |
| 19 | import org.eclipse.jface.preference.BooleanFieldEditor; |
| 20 | import org.eclipse.jface.preference.ColorFieldEditor; |
| 21 | import org.eclipse.jface.preference.FontFieldEditor; |
| 22 | import org.eclipse.jface.preference.IntegerFieldEditor; |
| 23 | import org.eclipse.jface.preference.PreferencePage; |
| 24 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SDMessages; |
| 25 | import org.eclipse.swt.SWT; |
| 26 | import org.eclipse.swt.events.SelectionEvent; |
| 27 | import org.eclipse.swt.events.SelectionListener; |
| 28 | import org.eclipse.swt.layout.GridData; |
| 29 | import org.eclipse.swt.layout.GridLayout; |
| 30 | import org.eclipse.swt.widgets.Composite; |
| 31 | import org.eclipse.swt.widgets.Control; |
| 32 | import org.eclipse.swt.widgets.Label; |
| 33 | import org.eclipse.swt.widgets.List; |
| 34 | import org.eclipse.ui.IWorkbench; |
| 35 | import org.eclipse.ui.IWorkbenchPreferencePage; |
| 36 | |
| 37 | /** |
| 38 | * The Sequence Diagram preferences page implementation. |
| 39 | * |
| 40 | * @version 1.0 |
| 41 | * @author sveyrier |
| 42 | */ |
| 43 | public class SDViewerPage extends PreferencePage implements IWorkbenchPreferencePage, SelectionListener { |
| 44 | |
| 45 | // ------------------------------------------------------------------------ |
| 46 | // Constants |
| 47 | // ------------------------------------------------------------------------ |
| 48 | /** |
| 49 | * Temporary preferences tag |
| 50 | */ |
| 51 | protected static final String TEMP_TAG = SDViewPref.TEMP_TAG; |
| 52 | |
| 53 | // ------------------------------------------------------------------------ |
| 54 | // Attributes |
| 55 | // ------------------------------------------------------------------------ |
| 56 | /** |
| 57 | * The preference handler used to access the PreferenceStore |
| 58 | */ |
| 59 | protected SDViewPref fPreferences = null; |
| 60 | /** |
| 61 | * BackGround color selector |
| 62 | */ |
| 63 | protected ColorFieldEditor fLineColor = null; |
| 64 | /** |
| 65 | * Foreground color selector |
| 66 | */ |
| 67 | protected ColorFieldEditor fBackGroundColor = null; |
| 68 | /** |
| 69 | * Font color selector |
| 70 | */ |
| 71 | protected ColorFieldEditor fTextColor = null; |
| 72 | /** |
| 73 | * List which display all modifiable sequence Diagram font |
| 74 | */ |
| 75 | protected List fClassItemList = null; |
| 76 | /** |
| 77 | * Font selector (The same is used for each modifiable font) |
| 78 | */ |
| 79 | protected FontFieldEditor fFont = null; |
| 80 | /** |
| 81 | * Link font when zooming selector |
| 82 | */ |
| 83 | protected BooleanFieldEditor fLink = null; |
| 84 | /** |
| 85 | * Enable tooltip selector |
| 86 | */ |
| 87 | protected BooleanFieldEditor fTooltip = null; |
| 88 | /** |
| 89 | * Do not take external time into account in the min max computation |
| 90 | */ |
| 91 | protected BooleanFieldEditor fNoExternalTime = null; |
| 92 | /** |
| 93 | * Use gradient color selector |
| 94 | */ |
| 95 | protected BooleanFieldEditor fUseGrad = null; |
| 96 | /** |
| 97 | * A button area. |
| 98 | */ |
| 99 | protected Composite fButtonArea; |
| 100 | /** |
| 101 | * SwimLane width selector |
| 102 | */ |
| 103 | protected IntegerFieldEditor fLifelineWidth = null; |
| 104 | |
| 105 | // ------------------------------------------------------------------------ |
| 106 | // Methods |
| 107 | // ------------------------------------------------------------------------ |
| 108 | |
| 109 | /* |
| 110 | * (non-Javadoc) |
| 111 | * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite) |
| 112 | */ |
| 113 | @Override |
| 114 | protected Control createContents(Composite parent) { |
| 115 | parent.setLayout(new GridLayout()); |
| 116 | Composite page = new Composite(parent, SWT.NONE); |
| 117 | GridLayout pageLayout = new GridLayout(); |
| 118 | pageLayout.numColumns = 2; |
| 119 | GridData pageLayoutdata = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL); |
| 120 | page.setLayoutData(pageLayoutdata); |
| 121 | page.setLayout(pageLayout); |
| 122 | |
| 123 | fTooltip = new BooleanFieldEditor(ISDPreferences.PREF_TOOLTIP, SDMessages._97, page); |
| 124 | fTooltip.setPreferenceStore(fPreferences.getPreferenceStore()); |
| 125 | fTooltip.load(); |
| 126 | |
| 127 | // link font with zoom pref |
| 128 | fLink = new BooleanFieldEditor(ISDPreferences.PREF_LINK_FONT, SDMessages._82, page); |
| 129 | fLink.setPreferenceStore(fPreferences.getPreferenceStore()); |
| 130 | fLink.load(); |
| 131 | |
| 132 | fNoExternalTime = new BooleanFieldEditor(ISDPreferences.PREF_EXCLUDE_EXTERNAL_TIME, SDMessages._83, page); |
| 133 | fNoExternalTime.setPreferenceStore(fPreferences.getPreferenceStore()); |
| 134 | fNoExternalTime.load(); |
| 135 | |
| 136 | // use gradient color pref |
| 137 | fUseGrad = new BooleanFieldEditor(ISDPreferences.PREF_USE_GRADIENT, SDMessages._84, page); |
| 138 | fUseGrad.setPreferenceStore(fPreferences.getPreferenceStore()); |
| 139 | fUseGrad.load(); |
| 140 | |
| 141 | Label separator = new Label(page, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.SHADOW_NONE); |
| 142 | GridData sepData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL); |
| 143 | separator.setLayoutData(sepData); |
| 144 | |
| 145 | Composite prefPage = new Composite(page, SWT.NONE); |
| 146 | GridLayout prefPageLayout = new GridLayout(); |
| 147 | prefPage.setLayoutData(pageLayoutdata); |
| 148 | prefPageLayout.numColumns = 1; |
| 149 | prefPage.setLayout(prefPageLayout); |
| 150 | |
| 151 | // swimLane width pref |
| 152 | fLifelineWidth = new IntegerFieldEditor(ISDPreferences.PREF_LIFELINE_WIDTH, SDMessages._80, prefPage); |
| 153 | fLifelineWidth.setPreferenceStore(fPreferences.getPreferenceStore()); |
| 154 | fLifelineWidth.setValidRange(119, 500); |
| 155 | fLifelineWidth.load(); |
| 156 | |
| 157 | // not very nice |
| 158 | new Label(prefPage, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.SHADOW_NONE); |
| 159 | new Label(prefPage, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.SHADOW_NONE); |
| 160 | |
| 161 | // Font list pref |
| 162 | fClassItemList = new List(prefPage, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); |
| 163 | GridData tabItemLayoutdata = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL); |
| 164 | fClassItemList.setLayoutData(tabItemLayoutdata); |
| 165 | |
| 166 | String[] fontList2 = SDViewPref.getFontList2(); |
| 167 | for (int i = 0; i < fontList2.length; i++) { |
| 168 | fClassItemList.add(fontList2[i]); |
| 169 | } |
| 170 | fClassItemList.setSelection(0); |
| 171 | fClassItemList.addSelectionListener(this); |
| 172 | fButtonArea = new Composite(prefPage, SWT.NONE); |
| 173 | GridData tabItemLayoutdata2 = new GridData(GridData.HORIZONTAL_ALIGN_FILL/* |GridData.GRAB_HORIZONTAL */| GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL); |
| 174 | fButtonArea.setLayoutData(tabItemLayoutdata2); |
| 175 | GridLayout buttonAreaLayout = new GridLayout(); |
| 176 | buttonAreaLayout.numColumns = 1; |
| 177 | fButtonArea.setLayout(buttonAreaLayout); |
| 178 | |
| 179 | // font selector initialise for the lifeline font pref |
| 180 | String[] fontList = SDViewPref.getFontList(); |
| 181 | fFont = new FontFieldEditor(fontList[0], "",//$NON-NLS-1$ |
| 182 | SDMessages._81, fButtonArea); |
| 183 | fFont.getPreviewControl().setSize(500, 500); |
| 184 | fFont.setPreferenceStore(fPreferences.getPreferenceStore()); |
| 185 | fFont.load(); |
| 186 | |
| 187 | fBackGroundColor = new ColorFieldEditor(fontList[0] + SDViewPref.BACK_COLOR_POSTFIX, SDMessages._85, fButtonArea); |
| 188 | fBackGroundColor.setPreferenceStore(fPreferences.getPreferenceStore()); |
| 189 | fBackGroundColor.load(); |
| 190 | |
| 191 | fLineColor = new ColorFieldEditor(fontList[0] + SDViewPref.FORE_COLOR_POSTFIX, SDMessages._86, fButtonArea); |
| 192 | fLineColor.setPreferenceStore(fPreferences.getPreferenceStore()); |
| 193 | fLineColor.load(); |
| 194 | |
| 195 | fTextColor = new ColorFieldEditor(fontList[0] + SDViewPref.TEXT_COLOR_POSTFIX, SDMessages._87, fButtonArea); |
| 196 | fTextColor.setPreferenceStore(fPreferences.getPreferenceStore()); |
| 197 | fTextColor.load(); |
| 198 | swapPref(true); |
| 199 | Dialog.applyDialogFont(page); |
| 200 | |
| 201 | return page; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * (non-Javadoc) |
| 206 | * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench) |
| 207 | */ |
| 208 | @Override |
| 209 | public void init(IWorkbench workbench) { |
| 210 | fPreferences = SDViewPref.getInstance(); |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * (non-Javadoc) |
| 215 | * @see org.eclipse.jface.preference.PreferencePage#performApply() |
| 216 | */ |
| 217 | @Override |
| 218 | protected void performApply() { |
| 219 | // Store the prefrences in the PreferenceStore |
| 220 | if (!fLifelineWidth.isValid()) { |
| 221 | fLifelineWidth.showErrorMessage(); |
| 222 | return; |
| 223 | } |
| 224 | fFont.store(); |
| 225 | fBackGroundColor.store(); |
| 226 | fLineColor.store(); |
| 227 | fLink.store(); |
| 228 | fTooltip.store(); |
| 229 | fNoExternalTime.store(); |
| 230 | fTextColor.store(); |
| 231 | fUseGrad.store(); |
| 232 | fLifelineWidth.store(); |
| 233 | swapPref(false); |
| 234 | // then save them in the preference file |
| 235 | fPreferences.apply(); |
| 236 | swapPref(true); |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * (non-Javadoc) |
| 241 | * @see org.eclipse.jface.preference.PreferencePage#performOk() |
| 242 | */ |
| 243 | @Override |
| 244 | public boolean performOk() { |
| 245 | performApply(); |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * (non-Javadoc) |
| 251 | * @see org.eclipse.jface.preference.PreferencePage#performDefaults() |
| 252 | */ |
| 253 | @Override |
| 254 | protected void performDefaults() { |
| 255 | fLink.loadDefault(); |
| 256 | fTooltip.loadDefault(); |
| 257 | fNoExternalTime.loadDefault(); |
| 258 | fUseGrad.loadDefault(); |
| 259 | fLifelineWidth.loadDefault(); |
| 260 | |
| 261 | // and all the fonts and colors |
| 262 | // fonts and colors are stored for each time because |
| 263 | // we are using only one FontFieldEditor |
| 264 | Set<String> keySet = SDViewPref.getInstance().fFontPref.keySet(); |
| 265 | Iterator<String> it = keySet.iterator(); |
| 266 | while (it.hasNext()) { |
| 267 | Object prefName = it.next(); |
| 268 | if (prefName instanceof String) { |
| 269 | fFont.setPreferenceName((String) prefName); |
| 270 | fFont.loadDefault(); |
| 271 | fFont.setPreferenceName((String) prefName + TEMP_TAG); |
| 272 | fFont.store(); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | keySet = SDViewPref.getInstance().fBackColorPref.keySet(); |
| 277 | it = keySet.iterator(); |
| 278 | while (it.hasNext()) { |
| 279 | Object prefName = it.next(); |
| 280 | if (prefName instanceof String) { |
| 281 | fBackGroundColor.setPreferenceName((String) prefName); |
| 282 | fBackGroundColor.loadDefault(); |
| 283 | fBackGroundColor.setPreferenceName((String) prefName + TEMP_TAG); |
| 284 | fBackGroundColor.store(); |
| 285 | } |
| 286 | |
| 287 | } |
| 288 | |
| 289 | String[] fontList = SDViewPref.getFontList(); |
| 290 | fBackGroundColor.setPreferenceName(fontList[fClassItemList.getSelectionIndex()] + SDViewPref.BACK_COLOR_POSTFIX + TEMP_TAG); |
| 291 | fBackGroundColor.load(); |
| 292 | |
| 293 | keySet = SDViewPref.getInstance().fForeColorPref.keySet(); |
| 294 | it = keySet.iterator(); |
| 295 | while (it.hasNext()) { |
| 296 | Object prefName = it.next(); |
| 297 | if (prefName instanceof String) { |
| 298 | fLineColor.setPreferenceName((String) prefName); |
| 299 | fLineColor.loadDefault(); |
| 300 | fLineColor.setPreferenceName((String) prefName + TEMP_TAG); |
| 301 | fLineColor.store(); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | fLineColor.setPreferenceName(fontList[fClassItemList.getSelectionIndex()] + SDViewPref.FORE_COLOR_POSTFIX + TEMP_TAG); |
| 306 | fLineColor.load(); |
| 307 | |
| 308 | keySet = SDViewPref.getInstance().fTextColorPref.keySet(); |
| 309 | it = keySet.iterator(); |
| 310 | while (it.hasNext()) { |
| 311 | Object prefName = it.next(); |
| 312 | if (prefName instanceof String) { |
| 313 | fTextColor.setPreferenceName((String) prefName); |
| 314 | fTextColor.loadDefault(); |
| 315 | fTextColor.setPreferenceName((String) prefName + TEMP_TAG); |
| 316 | fTextColor.store(); |
| 317 | } |
| 318 | } |
| 319 | fTextColor.setPreferenceName(fontList[fClassItemList.getSelectionIndex()] + SDViewPref.TEXT_COLOR_POSTFIX + TEMP_TAG); |
| 320 | fTextColor.load(); |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * (non-Javadoc) |
| 325 | * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent) |
| 326 | */ |
| 327 | @Override |
| 328 | public void widgetSelected(SelectionEvent e) { |
| 329 | // Store the past set font preference or else the |
| 330 | // FontFieldEditor reassignment will make us loose the current modification |
| 331 | fFont.store(); |
| 332 | fLineColor.store(); |
| 333 | fBackGroundColor.store(); |
| 334 | fTextColor.store(); |
| 335 | |
| 336 | String[] fontList = SDViewPref.getFontList(); |
| 337 | |
| 338 | // set the FontFieldEditor for the new selected graphNode font |
| 339 | fFont.setPreferenceName(fontList[fClassItemList.getSelectionIndex()] + TEMP_TAG); |
| 340 | fFont.load(); |
| 341 | |
| 342 | fBackGroundColor.setPreferenceName(fontList[fClassItemList.getSelectionIndex()] + SDViewPref.BACK_COLOR_POSTFIX + TEMP_TAG); |
| 343 | fBackGroundColor.load(); |
| 344 | |
| 345 | fLineColor.setPreferenceName(fontList[fClassItemList.getSelectionIndex()] + SDViewPref.FORE_COLOR_POSTFIX + TEMP_TAG); |
| 346 | fLineColor.load(); |
| 347 | |
| 348 | fTextColor.setPreferenceName(fontList[fClassItemList.getSelectionIndex()] + SDViewPref.TEXT_COLOR_POSTFIX + TEMP_TAG); |
| 349 | fTextColor.load(); |
| 350 | |
| 351 | // No Background for message graphNodes |
| 352 | if ((fontList[fClassItemList.getSelectionIndex()].equals(ISDPreferences.PREF_SYNC_MESS)) || (fontList[fClassItemList.getSelectionIndex()].equals(ISDPreferences.PREF_SYNC_MESS_RET)) |
| 353 | || (fontList[fClassItemList.getSelectionIndex()].equals(ISDPreferences.PREF_ASYNC_MESS)) || (fontList[fClassItemList.getSelectionIndex()].equals(ISDPreferences.PREF_ASYNC_MESS_RET))) { |
| 354 | fBackGroundColor.setEnabled(false, fButtonArea); |
| 355 | } else { |
| 356 | fBackGroundColor.setEnabled(true, fButtonArea); |
| 357 | } |
| 358 | |
| 359 | // No font used for execution occurrence and global frame |
| 360 | if ((fontList[fClassItemList.getSelectionIndex()].equals(ISDPreferences.PREF_EXEC)) || (fontList[fClassItemList.getSelectionIndex()].equals(ISDPreferences.PREF_FRAME))) { |
| 361 | fTextColor.setEnabled(false, fButtonArea); |
| 362 | } else { |
| 363 | fTextColor.setEnabled(true, fButtonArea); |
| 364 | } |
| 365 | |
| 366 | if (fontList[fClassItemList.getSelectionIndex()].equals(ISDPreferences.PREF_FRAME)) { |
| 367 | fFont.setEnabled(false, fButtonArea); |
| 368 | } else { |
| 369 | fFont.setEnabled(true, fButtonArea); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Swap viewer preferences. |
| 375 | * |
| 376 | * @param toTemp Switch to the temporary preferences |
| 377 | */ |
| 378 | protected void swapPref(boolean toTemp) { |
| 379 | String TAG1 = "";//$NON-NLS-1$ |
| 380 | String TAG2 = TEMP_TAG; |
| 381 | if (!toTemp) { |
| 382 | TAG1 = TEMP_TAG; |
| 383 | TAG2 = "";//$NON-NLS-1$ |
| 384 | } |
| 385 | Set<String> keySet = SDViewPref.getInstance().fFontPref.keySet(); |
| 386 | Iterator<String> it = keySet.iterator(); |
| 387 | while (it.hasNext()) { |
| 388 | Object prefName = it.next(); |
| 389 | if (prefName instanceof String) { |
| 390 | fFont.setPreferenceName((String) prefName + TAG1); |
| 391 | fFont.load(); |
| 392 | fFont.setPreferenceName((String) prefName + TAG2); |
| 393 | fFont.store(); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | keySet = SDViewPref.getInstance().fBackColorPref.keySet(); |
| 398 | it = keySet.iterator(); |
| 399 | while (it.hasNext()) { |
| 400 | Object prefName = it.next(); |
| 401 | if (prefName instanceof String) { |
| 402 | fBackGroundColor.setPreferenceName((String) prefName + TAG1); |
| 403 | fBackGroundColor.load(); |
| 404 | fBackGroundColor.setPreferenceName((String) prefName + TAG2); |
| 405 | fBackGroundColor.store(); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | keySet = SDViewPref.getInstance().fForeColorPref.keySet(); |
| 410 | it = keySet.iterator(); |
| 411 | while (it.hasNext()) { |
| 412 | Object prefName = it.next(); |
| 413 | if (prefName instanceof String) { |
| 414 | fLineColor.setPreferenceName((String) prefName + TAG1); |
| 415 | fLineColor.load(); |
| 416 | fLineColor.setPreferenceName((String) prefName + TAG2); |
| 417 | fLineColor.store(); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | keySet = SDViewPref.getInstance().fTextColorPref.keySet(); |
| 422 | it = keySet.iterator(); |
| 423 | while (it.hasNext()) { |
| 424 | Object prefName = it.next(); |
| 425 | if (prefName instanceof String) { |
| 426 | fTextColor.setPreferenceName((String) prefName + TAG1); |
| 427 | fTextColor.load(); |
| 428 | fTextColor.setPreferenceName((String) prefName + TAG2); |
| 429 | fTextColor.store(); |
| 430 | } |
| 431 | } |
| 432 | String[] fontList = SDViewPref.getFontList(); |
| 433 | if (toTemp) { |
| 434 | // set the FontFieldEditor for the new selected graphNode font |
| 435 | fFont.setPreferenceName(fontList[fClassItemList.getSelectionIndex()] + TEMP_TAG); |
| 436 | fFont.load(); |
| 437 | |
| 438 | fBackGroundColor.setPreferenceName(fontList[fClassItemList.getSelectionIndex()] + SDViewPref.BACK_COLOR_POSTFIX + TEMP_TAG); |
| 439 | fBackGroundColor.load(); |
| 440 | |
| 441 | fLineColor.setPreferenceName(fontList[fClassItemList.getSelectionIndex()] + SDViewPref.FORE_COLOR_POSTFIX + TEMP_TAG); |
| 442 | fLineColor.load(); |
| 443 | |
| 444 | fTextColor.setPreferenceName(fontList[fClassItemList.getSelectionIndex()] + SDViewPref.TEXT_COLOR_POSTFIX + TEMP_TAG); |
| 445 | fTextColor.load(); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * (non-Javadoc) |
| 451 | * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent) |
| 452 | */ |
| 453 | @Override |
| 454 | public void widgetDefaultSelected(SelectionEvent e) { |
| 455 | } |
| 456 | } |