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
.views
.TmfView
;
59 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.filter
.FilterDialog
;
60 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.TimeGraphColorScheme
;
61 import org
.eclipse
.ui
.IActionBars
;
64 * Color view implementation. This view provides support for managing color settings for filters.
67 * @author Patrick Tasse
70 public class ColorsView
extends TmfView
{
72 /** ID for the color view */
73 public static final @NonNull String ID
= "org.eclipse.linuxtools.tmf.ui.views.colors"; //$NON-NLS-1$
75 private static final Image ADD_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$
76 private static final Image DELETE_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/delete_button.gif"); //$NON-NLS-1$
77 private static final Image MOVE_UP_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/moveup_button.gif"); //$NON-NLS-1$
78 private static final Image MOVE_DOWN_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/movedown_button.gif"); //$NON-NLS-1$
79 private static final Image IMPORT_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/import_button.gif"); //$NON-NLS-1$
80 private static final Image EXPORT_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/export_button.gif"); //$NON-NLS-1$
82 // ------------------------------------------------------------------------
83 // Main data structures
84 // ------------------------------------------------------------------------
87 * The composite shell.
89 protected Shell fShell
;
91 * The main composite (scrolled composite)
93 protected ScrolledComposite fScrolledComposite
;
97 protected Composite fListComposite
;
99 * The filler composite.
101 protected Composite fFillerComposite
;
103 * The selected color settings row
105 protected ColorSettingRow fSelectedRow
= null;
107 * The color scheme instance for managing colors
109 protected TimeGraphColorScheme traceColorScheme
= new TimeGraphColorScheme();
111 * An action to add a color settings row
113 protected Action fAddAction
;
115 * An action to delete a color settings row
117 protected Action fDeleteAction
;
119 * An action to move up a color settings row in the list.
121 protected Action fMoveUpAction
;
123 * An action to move down a color settings row in the list.
125 protected Action fMoveDownAction
;
127 * An action to import color settings from file.
129 protected Action fImportAction
;
131 * An action to export color settings from file.
133 protected Action fExportAction
;
135 * The list of existing color settings
137 protected List
<ColorSetting
> fColorSettings
;
139 // ------------------------------------------------------------------------
141 // ------------------------------------------------------------------------
144 * Default Constructor
146 public ColorsView() {
147 super("Colors"); //$NON-NLS-1$
151 public void createPartControl(Composite parent
) {
152 fShell
= parent
.getShell();
154 fScrolledComposite
= new ScrolledComposite(parent
, SWT
.V_SCROLL
| SWT
.H_SCROLL
);
155 fScrolledComposite
.setExpandHorizontal(true);
156 fScrolledComposite
.setExpandVertical(true);
157 fListComposite
= new Composite(fScrolledComposite
, SWT
.NONE
);
158 fScrolledComposite
.setContent(fListComposite
);
160 GridLayout gl
= new GridLayout();
163 gl
.verticalSpacing
= 1;
164 fListComposite
.setLayout(gl
);
166 fColorSettings
= new ArrayList
<>(Arrays
.asList(ColorSettingsManager
.getColorSettings()));
167 for (ColorSetting colorSetting
: fColorSettings
) {
168 new ColorSettingRow(fListComposite
, colorSetting
);
171 fFillerComposite
= new Composite(fListComposite
, SWT
.NONE
);
172 GridData gd
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
174 fFillerComposite
.setLayoutData(gd
);
175 gl
= new GridLayout();
178 fFillerComposite
.setLayout(gl
);
179 Label fillerLabel
= new Label(fFillerComposite
, SWT
.NONE
);
180 fillerLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
181 fillerLabel
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
183 fFillerComposite
.addPaintListener(new PaintListener() {
185 public void paintControl(PaintEvent e
) {
186 if (fSelectedRow
== null) {
187 Color lineColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
188 Point p
= fFillerComposite
.getSize();
190 gc
.setForeground(lineColor
);
191 gc
.drawLine(0, 0, p
.x
- 1, 0);
196 MouseListener mouseListener
= new MouseAdapter() {
198 public void mouseDown(MouseEvent e
) {
203 fillerLabel
.addMouseListener(mouseListener
);
205 fScrolledComposite
.setMinSize(fListComposite
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
));
211 public void setFocus() {
212 fScrolledComposite
.setFocus();
216 * Refreshes the view display and updates the view actions enablements.
218 public void refresh() {
219 fListComposite
.layout();
220 fScrolledComposite
.setMinSize(fListComposite
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
));
221 fListComposite
.redraw(0, 0, fListComposite
.getBounds().width
, fListComposite
.getBounds().height
, true);
222 if (fSelectedRow
== null) {
223 fDeleteAction
.setEnabled(false);
224 fMoveUpAction
.setEnabled(false);
225 fMoveDownAction
.setEnabled(false);
227 fDeleteAction
.setEnabled(true);
228 fMoveUpAction
.setEnabled(true);
229 fMoveDownAction
.setEnabled(true);
233 private void fillToolBar() {
235 fAddAction
= new AddAction();
236 fAddAction
.setImageDescriptor(ImageDescriptor
.createFromImage(ADD_IMAGE
));
237 fAddAction
.setToolTipText(Messages
.ColorsView_AddActionToolTipText
);
239 fDeleteAction
= new DeleteAction();
240 fDeleteAction
.setImageDescriptor(ImageDescriptor
.createFromImage(DELETE_IMAGE
));
241 fDeleteAction
.setToolTipText(Messages
.ColorsView_DeleteActionToolTipText
);
242 fDeleteAction
.setEnabled(false);
244 fMoveUpAction
= new MoveUpAction();
245 fMoveUpAction
.setImageDescriptor(ImageDescriptor
.createFromImage(MOVE_UP_IMAGE
));
246 fMoveUpAction
.setToolTipText(Messages
.ColorsView_MoveUpActionToolTipText
);
247 fMoveUpAction
.setEnabled(false);
249 fMoveDownAction
= new MoveDownAction();
250 fMoveDownAction
.setImageDescriptor(ImageDescriptor
.createFromImage(MOVE_DOWN_IMAGE
));
251 fMoveDownAction
.setToolTipText(Messages
.ColorsView_MoveDownActionToolTipText
);
252 fMoveDownAction
.setEnabled(false);
254 fExportAction
= new ExportAction();
255 fExportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(EXPORT_IMAGE
));
256 fExportAction
.setToolTipText(Messages
.ColorsView_ExportActionToolTipText
);
258 fImportAction
= new ImportAction();
259 fImportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(IMPORT_IMAGE
));
260 fImportAction
.setToolTipText(Messages
.ColorsView_ImportActionToolTipText
);
262 IActionBars bars
= getViewSite().getActionBars();
263 IToolBarManager manager
= bars
.getToolBarManager();
264 manager
.add(fAddAction
);
265 manager
.add(fDeleteAction
);
266 manager
.add(fMoveUpAction
);
267 manager
.add(fMoveDownAction
);
268 manager
.add(new Separator());
269 manager
.add(fExportAction
);
270 manager
.add(fImportAction
);
273 private class AddAction
extends Action
{
276 ColorSetting colorSetting
= new ColorSetting(
277 checkNotNull(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_FOREGROUND
).getRGB()),
278 checkNotNull(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
).getRGB()),
279 checkNotNull(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_FOREGROUND
).getRGB()),
281 ColorSettingRow row
= new ColorSettingRow(fListComposite
, colorSetting
);
282 if (fSelectedRow
== null) {
283 fColorSettings
.add(colorSetting
);
284 row
.moveAbove(fFillerComposite
);
286 fColorSettings
.add(fColorSettings
.indexOf(fSelectedRow
.getColorSetting()), colorSetting
);
287 row
.moveAbove(fSelectedRow
);
291 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
295 private class DeleteAction
extends Action
{
299 if (fSelectedRow
!= null) {
300 int index
= fColorSettings
.indexOf(fSelectedRow
.getColorSetting());
301 fColorSettings
.remove(index
);
302 fSelectedRow
.fColorSetting
.dispose();
303 fSelectedRow
.dispose();
304 if (index
< fColorSettings
.size()) {
305 fSelectedRow
= (ColorSettingRow
) fListComposite
.getChildren()[index
];
310 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
315 private class MoveUpAction
extends Action
{
318 if (fSelectedRow
!= null) {
319 int index
= fColorSettings
.indexOf(fSelectedRow
.getColorSetting());
321 fColorSettings
.add(index
- 1, fColorSettings
.remove(index
));
322 fSelectedRow
.moveAbove(fListComposite
.getChildren()[index
- 1]);
324 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
330 private class MoveDownAction
extends Action
{
333 if (fSelectedRow
!= null) {
334 int index
= fColorSettings
.indexOf(fSelectedRow
.getColorSetting());
335 if (index
< fColorSettings
.size() - 1) {
336 fColorSettings
.add(index
+ 1, fColorSettings
.remove(index
));
338 fSelectedRow
.moveBelow(fListComposite
.getChildren()[index
+ 1]);
340 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
346 private class ExportAction
extends Action
{
349 FileDialog fileDialog
= new FileDialog(fShell
, SWT
.SAVE
);
350 fileDialog
.setFilterExtensions(new String
[] {"*.xml"}); //$NON-NLS-1$
351 fileDialog
.setOverwrite(true);
352 String pathName
= fileDialog
.open();
353 if (pathName
!= null) {
354 ColorSettingsXML
.save(pathName
, fColorSettings
.toArray(new ColorSetting
[0]));
359 private class ImportAction
extends Action
{
362 FileDialog fileDialog
= new FileDialog(fShell
, SWT
.OPEN
);
363 fileDialog
.setFilterExtensions(new String
[] {"*.xml"}); //$NON-NLS-1$
364 String pathName
= fileDialog
.open();
365 if (pathName
!= null) {
366 ColorSetting
[] colorSettings
= ColorSettingsXML
.load(pathName
);
367 if (colorSettings
.length
> 0) {
368 if (fColorSettings
.size() > 0) {
369 boolean overwrite
= MessageDialog
.openQuestion(fShell
,
370 Messages
.ColorsView_ImportOverwriteDialogTitle
,
371 Messages
.ColorsView_ImportOverwriteDialogMessage1
+
372 Messages
.ColorsView_ImportOverwriteDialogMessage2
);
374 for (Control control
: fListComposite
.getChildren()) {
375 if (control
instanceof ColorSettingRow
) {
376 ((ColorSettingRow
) control
).fColorSetting
.dispose();
380 fColorSettings
= new ArrayList
<>();
384 for (ColorSetting colorSetting
: colorSettings
) {
385 ColorSettingRow row
= new ColorSettingRow(fListComposite
, colorSetting
);
386 if (fSelectedRow
== null) {
387 fColorSettings
.add(colorSetting
);
388 row
.moveAbove(fFillerComposite
);
390 fColorSettings
.add(fColorSettings
.indexOf(fSelectedRow
.getColorSetting()), colorSetting
);
391 row
.moveAbove(fSelectedRow
);
395 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
401 private class ColorSettingRow
extends Composite
{
403 ColorSetting fColorSetting
;
405 public ColorSettingRow(final Composite parent
, final ColorSetting colorSetting
) {
406 super(parent
, SWT
.NONE
);
407 fColorSetting
= colorSetting
;
409 setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
411 setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
412 GridLayout gl
= new GridLayout(7, false);
415 gl
.horizontalSpacing
= 1;
416 gl
.verticalSpacing
= 0;
419 final Button fgButton
= new Button(this, SWT
.PUSH
);
420 fgButton
.setText(Messages
.ColorsView_ForegroundButtonText
);
421 fgButton
.setSize(fgButton
.computeSize(SWT
.DEFAULT
, 19));
422 fgButton
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
424 final Button bgButton
= new Button(this, SWT
.PUSH
);
425 bgButton
.setText(Messages
.ColorsView_BackgroundButtonText
);
426 bgButton
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
428 final Composite labelComposite
= new Composite(this, SWT
.NONE
);
429 labelComposite
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.FILL
, false, false));
430 gl
= new GridLayout();
433 labelComposite
.setLayout(gl
);
434 labelComposite
.setBackground(colorSetting
.getBackgroundColor());
436 final Label label
= new Label(labelComposite
, SWT
.NONE
);
437 label
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, false, true));
438 label
.setText(" Text "); //$NON-NLS-1$
439 label
.setForeground(colorSetting
.getForegroundColor());
440 label
.setBackground(colorSetting
.getBackgroundColor());
442 fgButton
.addSelectionListener(new SelectionAdapter() {
444 public void widgetSelected(SelectionEvent e
) {
445 fSelectedRow
= ColorSettingRow
.this;
447 ColorDialog dialog
= new ColorDialog(fShell
);
448 dialog
.setRGB(colorSetting
.getForegroundRGB());
449 dialog
.setText(Messages
.ColorsView_ForegroundDialogText
);
451 RGB rgb
= dialog
.getRGB();
453 colorSetting
.setForegroundRGB(rgb
);
454 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
455 label
.setForeground(colorSetting
.getForegroundColor());
459 bgButton
.addSelectionListener(new SelectionAdapter() {
461 public void widgetSelected(SelectionEvent e
) {
462 fSelectedRow
= ColorSettingRow
.this;
464 ColorDialog dialog
= new ColorDialog(fShell
);
465 dialog
.setRGB(colorSetting
.getBackgroundRGB());
466 dialog
.setText(Messages
.ColorsView_BackgroundDialogText
);
468 RGB rgb
= dialog
.getRGB();
470 colorSetting
.setBackgroundRGB(rgb
);
471 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
472 labelComposite
.setBackground(colorSetting
.getBackgroundColor());
473 label
.setBackground(colorSetting
.getBackgroundColor());
477 final Button tickButton
= new Button(this, SWT
.PUSH
);
478 tickButton
.setText(Messages
.ColorsView_TickButtonText
);
479 tickButton
.setSize(tickButton
.computeSize(SWT
.DEFAULT
, 19));
480 tickButton
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
482 final Canvas tickCanvas
= new Canvas(this, SWT
.NONE
);
483 GridData gd
= new GridData(SWT
.CENTER
, SWT
.FILL
, false, false);
485 gd
.heightHint
= bgButton
.getSize().y
;
486 tickCanvas
.setLayoutData(gd
);
487 tickCanvas
.setBackground(traceColorScheme
.getBkColor(false, false, false));
488 tickCanvas
.addPaintListener(new PaintListener() {
490 public void paintControl(PaintEvent e
) {
491 Rectangle bounds
= tickCanvas
.getBounds();
492 e
.gc
.setForeground(traceColorScheme
.getColor(TimeGraphColorScheme
.MID_LINE
));
493 int midy
= bounds
.y
+ bounds
.height
/ 2 - 1;
494 //int midy = e.y + e.height / 2;
495 e
.gc
.drawLine(e
.x
, midy
, e
.x
+ e
.width
, midy
);
496 Rectangle rect
= new Rectangle(e
.x
+ 1, bounds
.y
+ 2, 0, bounds
.height
- 6);
497 for (int i
= 1; i
<= 3; i
++) {
500 e
.gc
.setBackground(fColorSetting
.getTickColor());
501 e
.gc
.fillRectangle(rect
);
505 tickButton
.addSelectionListener(new SelectionAdapter() {
507 public void widgetSelected(SelectionEvent e
) {
508 fSelectedRow
= ColorSettingRow
.this;
509 ColorDialog dialog
= new ColorDialog(fShell
);
510 dialog
.setRGB(colorSetting
.getTickColorRGB());
511 dialog
.setText(Messages
.TickColorDialog_TickColorDialogTitle
);
513 RGB rgb
= dialog
.getRGB();
515 colorSetting
.setTickColorRGB(rgb
);
516 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
521 final Button filterButton
= new Button(this, SWT
.PUSH
);
522 filterButton
.setText(Messages
.ColorsView_FilterButtonText
);
523 filterButton
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
525 final Label filterText
= new Label(this, SWT
.NONE
);
526 ITmfFilter filter
= colorSetting
.getFilter();
527 if (filter
!= null) {
528 filterText
.setText(filter
.toString());
529 filterText
.setToolTipText(filter
.toString());
531 filterText
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
532 filterText
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
534 filterButton
.addSelectionListener(new SelectionAdapter() {
536 public void widgetSelected(SelectionEvent e
) {
537 fSelectedRow
= ColorSettingRow
.this;
539 FilterDialog dialog
= new FilterDialog(fShell
);
540 dialog
.setFilter(colorSetting
.getFilter());
542 if (dialog
.getReturnCode() == Window
.OK
) {
543 if (dialog
.getFilter() != null) {
544 colorSetting
.setFilter(dialog
.getFilter());
545 filterText
.setText(dialog
.getFilter().toString());
546 filterText
.setToolTipText(dialog
.getFilter().toString());
548 colorSetting
.setFilter(null);
549 filterText
.setText(""); //$NON-NLS-1$
550 filterText
.setToolTipText(""); //$NON-NLS-1$
552 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
557 addPaintListener(new PaintListener() {
559 public void paintControl(PaintEvent e
) {
560 if (fSelectedRow
== ColorSettingRow
.this) {
561 Color borderColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
562 Point p
= ColorSettingRow
.this.getSize();
563 Rectangle rect
= new Rectangle(0, 0, p
.x
- 1, p
.y
- 1);
565 gc
.setForeground(borderColor
);
566 gc
.drawRectangle(rect
);
571 MouseListener mouseListener
= new MouseAdapter() {
573 public void mouseDown(MouseEvent e
) {
574 fSelectedRow
= ColorSettingRow
.this;
578 addMouseListener(mouseListener
);
579 label
.addMouseListener(mouseListener
);
580 tickCanvas
.addMouseListener(mouseListener
);
581 filterText
.addMouseListener(mouseListener
);
585 * @return the ColorSetting
587 public ColorSetting
getColorSetting() {
588 return fColorSetting
;