1 /*******************************************************************************
2 * Copyright (c) 2010, 2012 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
.linuxtools
.tmf
.ui
.views
.colors
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Arrays
;
18 import java
.util
.List
;
20 import org
.eclipse
.jface
.action
.Action
;
21 import org
.eclipse
.jface
.action
.IToolBarManager
;
22 import org
.eclipse
.jface
.action
.Separator
;
23 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
24 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
25 import org
.eclipse
.jface
.window
.Window
;
26 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
27 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Messages
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.TmfView
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.filter
.FilterDialog
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
.TimeGraphColorScheme
;
31 import org
.eclipse
.swt
.SWT
;
32 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
33 import org
.eclipse
.swt
.events
.MouseAdapter
;
34 import org
.eclipse
.swt
.events
.MouseEvent
;
35 import org
.eclipse
.swt
.events
.MouseListener
;
36 import org
.eclipse
.swt
.events
.PaintEvent
;
37 import org
.eclipse
.swt
.events
.PaintListener
;
38 import org
.eclipse
.swt
.events
.SelectionAdapter
;
39 import org
.eclipse
.swt
.events
.SelectionEvent
;
40 import org
.eclipse
.swt
.graphics
.Color
;
41 import org
.eclipse
.swt
.graphics
.GC
;
42 import org
.eclipse
.swt
.graphics
.Image
;
43 import org
.eclipse
.swt
.graphics
.Point
;
44 import org
.eclipse
.swt
.graphics
.Rectangle
;
45 import org
.eclipse
.swt
.layout
.GridData
;
46 import org
.eclipse
.swt
.layout
.GridLayout
;
47 import org
.eclipse
.swt
.widgets
.Button
;
48 import org
.eclipse
.swt
.widgets
.Canvas
;
49 import org
.eclipse
.swt
.widgets
.ColorDialog
;
50 import org
.eclipse
.swt
.widgets
.Composite
;
51 import org
.eclipse
.swt
.widgets
.Control
;
52 import org
.eclipse
.swt
.widgets
.Display
;
53 import org
.eclipse
.swt
.widgets
.FileDialog
;
54 import org
.eclipse
.swt
.widgets
.Label
;
55 import org
.eclipse
.swt
.widgets
.Shell
;
56 import org
.eclipse
.ui
.IActionBars
;
59 * Color view implementation. This view provides support for managing color settings for filters.
62 * @author Patrick Tasse
65 public class ColorsView
extends TmfView
{
67 /** ID for the color view */
68 public static final String ID
= "org.eclipse.linuxtools.tmf.ui.views.colors"; //$NON-NLS-1$
70 private static final Image ADD_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$
71 private static final Image DELETE_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/delete_button.gif"); //$NON-NLS-1$
72 private static final Image MOVE_UP_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/moveup_button.gif"); //$NON-NLS-1$
73 private static final Image MOVE_DOWN_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/movedown_button.gif"); //$NON-NLS-1$
74 private static final Image IMPORT_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/import_button.gif"); //$NON-NLS-1$
75 private static final Image EXPORT_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/export_button.gif"); //$NON-NLS-1$
77 // ------------------------------------------------------------------------
78 // Main data structures
79 // ------------------------------------------------------------------------
82 * The composite shell.
84 protected Shell fShell
;
86 * The main composite (scrolled composite)
88 protected ScrolledComposite fScrolledComposite
;
92 protected Composite fListComposite
;
94 * The filler composite.
96 protected Composite fFillerComposite
;
98 * The selected color settings row
100 protected ColorSettingRow fSelectedRow
= null;
102 * The color scheme instance for managing colors
104 protected TimeGraphColorScheme traceColorScheme
= new TimeGraphColorScheme();
106 * An action to add a color settings row
108 protected Action fAddAction
;
110 * An action to delete a color settings row
112 protected Action fDeleteAction
;
114 * An action to move up a color settings row in the list.
116 protected Action fMoveUpAction
;
118 * An action to move down a color settings row in the list.
120 protected Action fMoveDownAction
;
122 * An action to import color settings from file.
124 protected Action fImportAction
;
126 * An action to export color settings from file.
128 protected Action fExportAction
;
130 * The list of existing color settings
132 protected List
<ColorSetting
> fColorSettings
;
134 // ------------------------------------------------------------------------
136 // ------------------------------------------------------------------------
139 * Default Constructor
141 public ColorsView() {
142 super("Colors"); //$NON-NLS-1$
146 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(Composite)
149 public void createPartControl(Composite parent
) {
150 fShell
= parent
.getShell();
152 fScrolledComposite
= new ScrolledComposite(parent
, SWT
.V_SCROLL
| SWT
.H_SCROLL
);
153 fScrolledComposite
.setExpandHorizontal(true);
154 fScrolledComposite
.setExpandVertical(true);
155 fListComposite
= new Composite(fScrolledComposite
, SWT
.NONE
);
156 fScrolledComposite
.setContent(fListComposite
);
158 GridLayout gl
= new GridLayout();
161 gl
.verticalSpacing
= 1;
162 fListComposite
.setLayout(gl
);
164 fColorSettings
= new ArrayList
<ColorSetting
>(Arrays
.asList(ColorSettingsManager
.getColorSettings()));
165 for (ColorSetting colorSetting
: fColorSettings
) {
166 new ColorSettingRow(fListComposite
, colorSetting
);
169 fFillerComposite
= new Composite(fListComposite
, SWT
.NONE
);
170 GridData gd
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
172 fFillerComposite
.setLayoutData(gd
);
173 gl
= new GridLayout();
176 fFillerComposite
.setLayout(gl
);
177 Label fillerLabel
= new Label(fFillerComposite
, SWT
.NONE
);
178 fillerLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
179 fillerLabel
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
181 fFillerComposite
.addPaintListener(new PaintListener() {
183 public void paintControl(PaintEvent e
) {
184 if (fSelectedRow
== null) {
185 Color lineColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
186 Point p
= fFillerComposite
.getSize();
188 gc
.setForeground(lineColor
);
189 gc
.drawLine(0, 0, p
.x
- 1, 0);
194 MouseListener mouseListener
= new MouseAdapter() {
196 public void mouseDown(MouseEvent e
) {
201 fillerLabel
.addMouseListener(mouseListener
);
203 fScrolledComposite
.setMinSize(fListComposite
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
));
209 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
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 Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_FOREGROUND
).getRGB(),
279 Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
).getRGB(),
280 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
= new FileDialog(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
= new FileDialog(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
<ColorSetting
>();
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 colorSetting
.setForegroundRGB(dialog
.getRGB());
453 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
454 label
.setForeground(colorSetting
.getForegroundColor());
457 bgButton
.addSelectionListener(new SelectionAdapter() {
459 public void widgetSelected(SelectionEvent e
) {
460 fSelectedRow
= ColorSettingRow
.this;
462 ColorDialog dialog
= new ColorDialog(fShell
);
463 dialog
.setRGB(colorSetting
.getBackgroundRGB());
464 dialog
.setText(Messages
.ColorsView_BackgroundDialogText
);
466 colorSetting
.setBackgroundRGB(dialog
.getRGB());
467 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
468 labelComposite
.setBackground(colorSetting
.getBackgroundColor());
469 label
.setBackground(colorSetting
.getBackgroundColor());
472 final Button tickButton
= new Button(this, SWT
.PUSH
);
473 tickButton
.setText(Messages
.ColorsView_TickButtonText
);
474 tickButton
.setSize(tickButton
.computeSize(SWT
.DEFAULT
, 19));
475 tickButton
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
477 final Canvas tickCanvas
= new Canvas(this, SWT
.NONE
);
478 GridData gd
= new GridData(SWT
.CENTER
, SWT
.FILL
, false, false);
480 gd
.heightHint
= bgButton
.getSize().y
;
481 tickCanvas
.setLayoutData(gd
);
482 tickCanvas
.setBackground(traceColorScheme
.getBkColor(false, false, false));
483 tickCanvas
.addPaintListener(new PaintListener() {
485 public void paintControl(PaintEvent e
) {
486 Rectangle bounds
= tickCanvas
.getBounds();
487 e
.gc
.setForeground(traceColorScheme
.getColor(TimeGraphColorScheme
.MID_LINE
));
488 int midy
= bounds
.y
+ bounds
.height
/ 2 - 1;
489 //int midy = e.y + e.height / 2;
490 e
.gc
.drawLine(e
.x
, midy
, e
.x
+ e
.width
, midy
);
491 Rectangle rect
= new Rectangle(e
.x
+ 1, bounds
.y
+ 2, 0, bounds
.height
- 6);
492 for (int i
= 1; i
<= 3; i
++) {
495 e
.gc
.setBackground(fColorSetting
.getTickColor());
496 e
.gc
.fillRectangle(rect
);
500 tickButton
.addSelectionListener(new SelectionAdapter() {
502 public void widgetSelected(SelectionEvent e
) {
503 fSelectedRow
= ColorSettingRow
.this;
504 ColorDialog dialog
= new ColorDialog(fShell
);
505 dialog
.setRGB(colorSetting
.getTickColorRGB());
506 dialog
.setText(Messages
.TickColorDialog_TickColorDialogTitle
);
508 colorSetting
.setTickColorRGB(dialog
.getRGB());
509 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
513 final Button filterButton
= new Button(this, SWT
.PUSH
);
514 filterButton
.setText(Messages
.ColorsView_FilterButtonText
);
515 filterButton
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
517 final Label filterText
= new Label(this, SWT
.NONE
);
518 if (colorSetting
.getFilter() != null) {
519 filterText
.setText(colorSetting
.getFilter().toString());
520 filterText
.setToolTipText(colorSetting
.getFilter().toString());
522 filterText
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
523 filterText
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
525 filterButton
.addSelectionListener(new SelectionAdapter() {
527 public void widgetSelected(SelectionEvent e
) {
528 fSelectedRow
= ColorSettingRow
.this;
530 FilterDialog dialog
= new FilterDialog(fShell
);
531 dialog
.setFilter(colorSetting
.getFilter());
533 if (dialog
.getReturnCode() == Window
.OK
) {
534 if (dialog
.getFilter() != null) {
535 colorSetting
.setFilter(dialog
.getFilter());
536 filterText
.setText(dialog
.getFilter().toString());
537 filterText
.setToolTipText(dialog
.getFilter().toString());
539 colorSetting
.setFilter(null);
540 filterText
.setText(""); //$NON-NLS-1$
541 filterText
.setToolTipText(""); //$NON-NLS-1$
543 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
548 addPaintListener(new PaintListener() {
550 public void paintControl(PaintEvent e
) {
551 if (fSelectedRow
== ColorSettingRow
.this) {
552 Color borderColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
553 Point p
= ColorSettingRow
.this.getSize();
554 Rectangle rect
= new Rectangle(0, 0, p
.x
- 1, p
.y
- 1);
556 gc
.setForeground(borderColor
);
557 gc
.drawRectangle(rect
);
562 MouseListener mouseListener
= new MouseAdapter() {
564 public void mouseDown(MouseEvent e
) {
565 fSelectedRow
= ColorSettingRow
.this;
569 addMouseListener(mouseListener
);
570 label
.addMouseListener(mouseListener
);
571 tickCanvas
.addMouseListener(mouseListener
);
572 filterText
.addMouseListener(mouseListener
);
576 * @return the ColorSetting
578 public ColorSetting
getColorSetting() {
579 return fColorSetting
;