1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Patrick Tasse - Initial API and implementation
11 * Bernd Hufmann - Updated to use RGB for the tick color
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ui
.views
.colors
;
16 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Arrays
;
20 import java
.util
.List
;
22 import org
.eclipse
.jdt
.annotation
.NonNull
;
23 import org
.eclipse
.jface
.action
.Action
;
24 import org
.eclipse
.jface
.action
.IToolBarManager
;
25 import org
.eclipse
.jface
.action
.Separator
;
26 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
27 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
28 import org
.eclipse
.jface
.window
.Window
;
29 import org
.eclipse
.swt
.SWT
;
30 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
31 import org
.eclipse
.swt
.events
.MouseAdapter
;
32 import org
.eclipse
.swt
.events
.MouseEvent
;
33 import org
.eclipse
.swt
.events
.MouseListener
;
34 import org
.eclipse
.swt
.events
.PaintEvent
;
35 import org
.eclipse
.swt
.events
.PaintListener
;
36 import org
.eclipse
.swt
.events
.SelectionAdapter
;
37 import org
.eclipse
.swt
.events
.SelectionEvent
;
38 import org
.eclipse
.swt
.graphics
.Color
;
39 import org
.eclipse
.swt
.graphics
.GC
;
40 import org
.eclipse
.swt
.graphics
.Image
;
41 import org
.eclipse
.swt
.graphics
.Point
;
42 import org
.eclipse
.swt
.graphics
.RGB
;
43 import org
.eclipse
.swt
.graphics
.Rectangle
;
44 import org
.eclipse
.swt
.layout
.GridData
;
45 import org
.eclipse
.swt
.layout
.GridLayout
;
46 import org
.eclipse
.swt
.widgets
.Button
;
47 import org
.eclipse
.swt
.widgets
.Canvas
;
48 import org
.eclipse
.swt
.widgets
.ColorDialog
;
49 import org
.eclipse
.swt
.widgets
.Composite
;
50 import org
.eclipse
.swt
.widgets
.Control
;
51 import org
.eclipse
.swt
.widgets
.Display
;
52 import org
.eclipse
.swt
.widgets
.FileDialog
;
53 import org
.eclipse
.swt
.widgets
.Label
;
54 import org
.eclipse
.swt
.widgets
.Shell
;
55 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
56 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Messages
;
57 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.ITmfFilter
;
58 import org
.eclipse
.tracecompass
.tmf
.ui
.dialog
.TmfFileDialogFactory
;
59 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.TmfView
;
60 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.filter
.FilterDialog
;
61 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.TimeGraphColorScheme
;
62 import org
.eclipse
.ui
.IActionBars
;
65 * Color view implementation. This view provides support for managing color settings for filters.
68 * @author Patrick Tasse
71 public class ColorsView
extends TmfView
{
73 /** ID for the color view */
74 public static final @NonNull String ID
= "org.eclipse.linuxtools.tmf.ui.views.colors"; //$NON-NLS-1$
76 private static final Image ADD_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$
77 private static final Image DELETE_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/delete_button.gif"); //$NON-NLS-1$
78 private static final Image MOVE_UP_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/moveup_button.gif"); //$NON-NLS-1$
79 private static final Image MOVE_DOWN_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/movedown_button.gif"); //$NON-NLS-1$
80 private static final Image IMPORT_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/import_button.gif"); //$NON-NLS-1$
81 private static final Image EXPORT_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/export_button.gif"); //$NON-NLS-1$
83 // ------------------------------------------------------------------------
84 // Main data structures
85 // ------------------------------------------------------------------------
88 * The composite shell.
90 protected Shell fShell
;
92 * The main composite (scrolled composite)
94 protected ScrolledComposite fScrolledComposite
;
98 protected Composite fListComposite
;
100 * The filler composite.
102 protected Composite fFillerComposite
;
104 * The selected color settings row
106 protected ColorSettingRow fSelectedRow
= null;
108 * The color scheme instance for managing colors
110 protected TimeGraphColorScheme traceColorScheme
= new TimeGraphColorScheme();
112 * An action to add a color settings row
114 protected Action fAddAction
;
116 * An action to delete a color settings row
118 protected Action fDeleteAction
;
120 * An action to move up a color settings row in the list.
122 protected Action fMoveUpAction
;
124 * An action to move down a color settings row in the list.
126 protected Action fMoveDownAction
;
128 * An action to import color settings from file.
130 protected Action fImportAction
;
132 * An action to export color settings from file.
134 protected Action fExportAction
;
136 * The list of existing color settings
138 protected List
<ColorSetting
> fColorSettings
;
140 // ------------------------------------------------------------------------
142 // ------------------------------------------------------------------------
145 * Default Constructor
147 public ColorsView() {
148 super("Colors"); //$NON-NLS-1$
152 public void createPartControl(Composite parent
) {
153 fShell
= parent
.getShell();
155 fScrolledComposite
= new ScrolledComposite(parent
, SWT
.V_SCROLL
| SWT
.H_SCROLL
);
156 fScrolledComposite
.setExpandHorizontal(true);
157 fScrolledComposite
.setExpandVertical(true);
158 fListComposite
= new Composite(fScrolledComposite
, SWT
.NONE
);
159 fScrolledComposite
.setContent(fListComposite
);
161 GridLayout gl
= new GridLayout();
164 gl
.verticalSpacing
= 1;
165 fListComposite
.setLayout(gl
);
167 fColorSettings
= new ArrayList
<>(Arrays
.asList(ColorSettingsManager
.getColorSettings()));
168 for (ColorSetting colorSetting
: fColorSettings
) {
169 new ColorSettingRow(fListComposite
, colorSetting
);
172 fFillerComposite
= new Composite(fListComposite
, SWT
.NONE
);
173 GridData gd
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
175 fFillerComposite
.setLayoutData(gd
);
176 gl
= new GridLayout();
179 fFillerComposite
.setLayout(gl
);
180 Label fillerLabel
= new Label(fFillerComposite
, SWT
.NONE
);
181 fillerLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
182 fillerLabel
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
184 fFillerComposite
.addPaintListener(new PaintListener() {
186 public void paintControl(PaintEvent e
) {
187 if (fSelectedRow
== null) {
188 Color lineColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
189 Point p
= fFillerComposite
.getSize();
191 gc
.setForeground(lineColor
);
192 gc
.drawLine(0, 0, p
.x
- 1, 0);
197 MouseListener mouseListener
= new MouseAdapter() {
199 public void mouseDown(MouseEvent e
) {
204 fillerLabel
.addMouseListener(mouseListener
);
206 fScrolledComposite
.setMinSize(fListComposite
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
));
212 public void setFocus() {
213 fScrolledComposite
.setFocus();
217 * Refreshes the view display and updates the view actions enablements.
219 public void refresh() {
220 fListComposite
.layout();
221 fScrolledComposite
.setMinSize(fListComposite
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
));
222 fListComposite
.redraw(0, 0, fListComposite
.getBounds().width
, fListComposite
.getBounds().height
, true);
223 if (fSelectedRow
== null) {
224 fDeleteAction
.setEnabled(false);
225 fMoveUpAction
.setEnabled(false);
226 fMoveDownAction
.setEnabled(false);
228 fDeleteAction
.setEnabled(true);
229 fMoveUpAction
.setEnabled(true);
230 fMoveDownAction
.setEnabled(true);
234 private void fillToolBar() {
236 fAddAction
= new AddAction();
237 fAddAction
.setImageDescriptor(ImageDescriptor
.createFromImage(ADD_IMAGE
));
238 fAddAction
.setToolTipText(Messages
.ColorsView_AddActionToolTipText
);
240 fDeleteAction
= new DeleteAction();
241 fDeleteAction
.setImageDescriptor(ImageDescriptor
.createFromImage(DELETE_IMAGE
));
242 fDeleteAction
.setToolTipText(Messages
.ColorsView_DeleteActionToolTipText
);
243 fDeleteAction
.setEnabled(false);
245 fMoveUpAction
= new MoveUpAction();
246 fMoveUpAction
.setImageDescriptor(ImageDescriptor
.createFromImage(MOVE_UP_IMAGE
));
247 fMoveUpAction
.setToolTipText(Messages
.ColorsView_MoveUpActionToolTipText
);
248 fMoveUpAction
.setEnabled(false);
250 fMoveDownAction
= new MoveDownAction();
251 fMoveDownAction
.setImageDescriptor(ImageDescriptor
.createFromImage(MOVE_DOWN_IMAGE
));
252 fMoveDownAction
.setToolTipText(Messages
.ColorsView_MoveDownActionToolTipText
);
253 fMoveDownAction
.setEnabled(false);
255 fExportAction
= new ExportAction();
256 fExportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(EXPORT_IMAGE
));
257 fExportAction
.setToolTipText(Messages
.ColorsView_ExportActionToolTipText
);
259 fImportAction
= new ImportAction();
260 fImportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(IMPORT_IMAGE
));
261 fImportAction
.setToolTipText(Messages
.ColorsView_ImportActionToolTipText
);
263 IActionBars bars
= getViewSite().getActionBars();
264 IToolBarManager manager
= bars
.getToolBarManager();
265 manager
.add(fAddAction
);
266 manager
.add(fDeleteAction
);
267 manager
.add(fMoveUpAction
);
268 manager
.add(fMoveDownAction
);
269 manager
.add(new Separator());
270 manager
.add(fExportAction
);
271 manager
.add(fImportAction
);
274 private class AddAction
extends Action
{
277 ColorSetting colorSetting
= new ColorSetting(
278 checkNotNull(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_FOREGROUND
).getRGB()),
279 checkNotNull(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
).getRGB()),
280 checkNotNull(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_FOREGROUND
).getRGB()),
282 ColorSettingRow row
= new ColorSettingRow(fListComposite
, colorSetting
);
283 if (fSelectedRow
== null) {
284 fColorSettings
.add(colorSetting
);
285 row
.moveAbove(fFillerComposite
);
287 fColorSettings
.add(fColorSettings
.indexOf(fSelectedRow
.getColorSetting()), colorSetting
);
288 row
.moveAbove(fSelectedRow
);
292 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
296 private class DeleteAction
extends Action
{
300 if (fSelectedRow
!= null) {
301 int index
= fColorSettings
.indexOf(fSelectedRow
.getColorSetting());
302 fColorSettings
.remove(index
);
303 fSelectedRow
.fColorSetting
.dispose();
304 fSelectedRow
.dispose();
305 if (index
< fColorSettings
.size()) {
306 fSelectedRow
= (ColorSettingRow
) fListComposite
.getChildren()[index
];
311 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
316 private class MoveUpAction
extends Action
{
319 if (fSelectedRow
!= null) {
320 int index
= fColorSettings
.indexOf(fSelectedRow
.getColorSetting());
322 fColorSettings
.add(index
- 1, fColorSettings
.remove(index
));
323 fSelectedRow
.moveAbove(fListComposite
.getChildren()[index
- 1]);
325 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
331 private class MoveDownAction
extends Action
{
334 if (fSelectedRow
!= null) {
335 int index
= fColorSettings
.indexOf(fSelectedRow
.getColorSetting());
336 if (index
< fColorSettings
.size() - 1) {
337 fColorSettings
.add(index
+ 1, fColorSettings
.remove(index
));
339 fSelectedRow
.moveBelow(fListComposite
.getChildren()[index
+ 1]);
341 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
347 private class ExportAction
extends Action
{
350 FileDialog fileDialog
= TmfFileDialogFactory
.create(fShell
, SWT
.SAVE
);
351 fileDialog
.setFilterExtensions(new String
[] {"*.xml"}); //$NON-NLS-1$
352 fileDialog
.setOverwrite(true);
353 String pathName
= fileDialog
.open();
354 if (pathName
!= null) {
355 ColorSettingsXML
.save(pathName
, fColorSettings
.toArray(new ColorSetting
[0]));
360 private class ImportAction
extends Action
{
363 FileDialog fileDialog
= TmfFileDialogFactory
.create(fShell
, SWT
.OPEN
);
364 fileDialog
.setFilterExtensions(new String
[] {"*.xml"}); //$NON-NLS-1$
365 String pathName
= fileDialog
.open();
366 if (pathName
!= null) {
367 ColorSetting
[] colorSettings
= ColorSettingsXML
.load(pathName
);
368 if (colorSettings
.length
> 0) {
369 if (fColorSettings
.size() > 0) {
370 boolean overwrite
= MessageDialog
.openQuestion(fShell
,
371 Messages
.ColorsView_ImportOverwriteDialogTitle
,
372 Messages
.ColorsView_ImportOverwriteDialogMessage1
+
373 Messages
.ColorsView_ImportOverwriteDialogMessage2
);
375 for (Control control
: fListComposite
.getChildren()) {
376 if (control
instanceof ColorSettingRow
) {
377 ((ColorSettingRow
) control
).fColorSetting
.dispose();
381 fColorSettings
= new ArrayList
<>();
385 for (ColorSetting colorSetting
: colorSettings
) {
386 ColorSettingRow row
= new ColorSettingRow(fListComposite
, colorSetting
);
387 if (fSelectedRow
== null) {
388 fColorSettings
.add(colorSetting
);
389 row
.moveAbove(fFillerComposite
);
391 fColorSettings
.add(fColorSettings
.indexOf(fSelectedRow
.getColorSetting()), colorSetting
);
392 row
.moveAbove(fSelectedRow
);
396 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
402 private class ColorSettingRow
extends Composite
{
404 ColorSetting fColorSetting
;
406 public ColorSettingRow(final Composite parent
, final ColorSetting colorSetting
) {
407 super(parent
, SWT
.NONE
);
408 fColorSetting
= colorSetting
;
410 setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
412 setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
413 GridLayout gl
= new GridLayout(7, false);
416 gl
.horizontalSpacing
= 1;
417 gl
.verticalSpacing
= 0;
420 final Button fgButton
= new Button(this, SWT
.PUSH
);
421 fgButton
.setText(Messages
.ColorsView_ForegroundButtonText
);
422 fgButton
.setSize(fgButton
.computeSize(SWT
.DEFAULT
, 19));
423 fgButton
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
425 final Button bgButton
= new Button(this, SWT
.PUSH
);
426 bgButton
.setText(Messages
.ColorsView_BackgroundButtonText
);
427 bgButton
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
429 final Composite labelComposite
= new Composite(this, SWT
.NONE
);
430 labelComposite
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.FILL
, false, false));
431 gl
= new GridLayout();
434 labelComposite
.setLayout(gl
);
435 labelComposite
.setBackground(colorSetting
.getBackgroundColor());
437 final Label label
= new Label(labelComposite
, SWT
.NONE
);
438 label
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, false, true));
439 label
.setText(" Text "); //$NON-NLS-1$
440 label
.setForeground(colorSetting
.getForegroundColor());
441 label
.setBackground(colorSetting
.getBackgroundColor());
443 fgButton
.addSelectionListener(new SelectionAdapter() {
445 public void widgetSelected(SelectionEvent e
) {
446 fSelectedRow
= ColorSettingRow
.this;
448 ColorDialog dialog
= new ColorDialog(fShell
);
449 dialog
.setRGB(colorSetting
.getForegroundRGB());
450 dialog
.setText(Messages
.ColorsView_ForegroundDialogText
);
452 RGB rgb
= dialog
.getRGB();
454 colorSetting
.setForegroundRGB(rgb
);
455 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
456 label
.setForeground(colorSetting
.getForegroundColor());
460 bgButton
.addSelectionListener(new SelectionAdapter() {
462 public void widgetSelected(SelectionEvent e
) {
463 fSelectedRow
= ColorSettingRow
.this;
465 ColorDialog dialog
= new ColorDialog(fShell
);
466 dialog
.setRGB(colorSetting
.getBackgroundRGB());
467 dialog
.setText(Messages
.ColorsView_BackgroundDialogText
);
469 RGB rgb
= dialog
.getRGB();
471 colorSetting
.setBackgroundRGB(rgb
);
472 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
473 labelComposite
.setBackground(colorSetting
.getBackgroundColor());
474 label
.setBackground(colorSetting
.getBackgroundColor());
478 final Button tickButton
= new Button(this, SWT
.PUSH
);
479 tickButton
.setText(Messages
.ColorsView_TickButtonText
);
480 tickButton
.setSize(tickButton
.computeSize(SWT
.DEFAULT
, 19));
481 tickButton
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
483 final Canvas tickCanvas
= new Canvas(this, SWT
.NONE
);
484 GridData gd
= new GridData(SWT
.CENTER
, SWT
.FILL
, false, false);
486 gd
.heightHint
= bgButton
.getSize().y
;
487 tickCanvas
.setLayoutData(gd
);
488 tickCanvas
.setBackground(traceColorScheme
.getBkColor(false, false, false));
489 tickCanvas
.addPaintListener(new PaintListener() {
491 public void paintControl(PaintEvent e
) {
492 Rectangle bounds
= tickCanvas
.getBounds();
493 e
.gc
.setForeground(traceColorScheme
.getColor(TimeGraphColorScheme
.MID_LINE
));
494 int midy
= bounds
.y
+ bounds
.height
/ 2 - 1;
495 //int midy = e.y + e.height / 2;
496 e
.gc
.drawLine(e
.x
, midy
, e
.x
+ e
.width
, midy
);
497 Rectangle rect
= new Rectangle(e
.x
+ 1, bounds
.y
+ 2, 0, bounds
.height
- 6);
498 for (int i
= 1; i
<= 3; i
++) {
501 e
.gc
.setBackground(fColorSetting
.getTickColor());
502 e
.gc
.fillRectangle(rect
);
506 tickButton
.addSelectionListener(new SelectionAdapter() {
508 public void widgetSelected(SelectionEvent e
) {
509 fSelectedRow
= ColorSettingRow
.this;
510 ColorDialog dialog
= new ColorDialog(fShell
);
511 dialog
.setRGB(colorSetting
.getTickColorRGB());
512 dialog
.setText(Messages
.TickColorDialog_TickColorDialogTitle
);
514 RGB rgb
= dialog
.getRGB();
516 colorSetting
.setTickColorRGB(rgb
);
517 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
522 final Button filterButton
= new Button(this, SWT
.PUSH
);
523 filterButton
.setText(Messages
.ColorsView_FilterButtonText
);
524 filterButton
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
526 final Label filterText
= new Label(this, SWT
.NONE
);
527 ITmfFilter filter
= colorSetting
.getFilter();
528 if (filter
!= null) {
529 filterText
.setText(filter
.toString());
530 filterText
.setToolTipText(filter
.toString());
532 filterText
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
533 filterText
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
535 filterButton
.addSelectionListener(new SelectionAdapter() {
537 public void widgetSelected(SelectionEvent e
) {
538 fSelectedRow
= ColorSettingRow
.this;
540 FilterDialog dialog
= new FilterDialog(fShell
);
541 dialog
.setFilter(colorSetting
.getFilter());
543 if (dialog
.getReturnCode() == Window
.OK
) {
544 if (dialog
.getFilter() != null) {
545 colorSetting
.setFilter(dialog
.getFilter());
546 filterText
.setText(dialog
.getFilter().toString());
547 filterText
.setToolTipText(dialog
.getFilter().toString());
549 colorSetting
.setFilter(null);
550 filterText
.setText(""); //$NON-NLS-1$
551 filterText
.setToolTipText(""); //$NON-NLS-1$
553 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
558 addPaintListener(new PaintListener() {
560 public void paintControl(PaintEvent e
) {
561 if (fSelectedRow
== ColorSettingRow
.this) {
562 Color borderColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
563 Point p
= ColorSettingRow
.this.getSize();
564 Rectangle rect
= new Rectangle(0, 0, p
.x
- 1, p
.y
- 1);
566 gc
.setForeground(borderColor
);
567 gc
.drawRectangle(rect
);
572 MouseListener mouseListener
= new MouseAdapter() {
574 public void mouseDown(MouseEvent e
) {
575 fSelectedRow
= ColorSettingRow
.this;
579 addMouseListener(mouseListener
);
580 label
.addMouseListener(mouseListener
);
581 tickCanvas
.addMouseListener(mouseListener
);
582 filterText
.addMouseListener(mouseListener
);
586 * @return the ColorSetting
588 public ColorSetting
getColorSetting() {
589 return fColorSetting
;