1 /*******************************************************************************
2 * Copyright (c) 2010 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.colors
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Arrays
;
17 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
.Dialog
;
24 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
25 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
26 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Messages
;
27 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.TmfUiPlugin
;
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
.ITimeGraphProvider
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.TimeGraphProvider
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
.TimeGraphColorScheme
;
35 import org
.eclipse
.swt
.SWT
;
36 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
37 import org
.eclipse
.swt
.events
.MouseAdapter
;
38 import org
.eclipse
.swt
.events
.MouseEvent
;
39 import org
.eclipse
.swt
.events
.MouseListener
;
40 import org
.eclipse
.swt
.events
.PaintEvent
;
41 import org
.eclipse
.swt
.events
.PaintListener
;
42 import org
.eclipse
.swt
.events
.SelectionAdapter
;
43 import org
.eclipse
.swt
.events
.SelectionEvent
;
44 import org
.eclipse
.swt
.graphics
.Color
;
45 import org
.eclipse
.swt
.graphics
.GC
;
46 import org
.eclipse
.swt
.graphics
.Image
;
47 import org
.eclipse
.swt
.graphics
.Point
;
48 import org
.eclipse
.swt
.graphics
.Rectangle
;
49 import org
.eclipse
.swt
.layout
.GridData
;
50 import org
.eclipse
.swt
.layout
.GridLayout
;
51 import org
.eclipse
.swt
.widgets
.Button
;
52 import org
.eclipse
.swt
.widgets
.Canvas
;
53 import org
.eclipse
.swt
.widgets
.ColorDialog
;
54 import org
.eclipse
.swt
.widgets
.Composite
;
55 import org
.eclipse
.swt
.widgets
.Control
;
56 import org
.eclipse
.swt
.widgets
.Display
;
57 import org
.eclipse
.swt
.widgets
.FileDialog
;
58 import org
.eclipse
.swt
.widgets
.Label
;
59 import org
.eclipse
.swt
.widgets
.Shell
;
60 import org
.eclipse
.ui
.IActionBars
;
62 public class ColorsView
extends TmfView
{
64 public static final String ID
= "org.eclipse.linuxtools.tmf.ui.views.colors"; //$NON-NLS-1$
66 private static final Image ADD_IMAGE
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$
67 private static final Image DELETE_IMAGE
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/elcl16/delete_button.gif"); //$NON-NLS-1$
68 private static final Image MOVE_UP_IMAGE
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/elcl16/moveup_button.gif"); //$NON-NLS-1$
69 private static final Image MOVE_DOWN_IMAGE
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/elcl16/movedown_button.gif"); //$NON-NLS-1$
70 private static final Image IMPORT_IMAGE
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/elcl16/import_button.gif"); //$NON-NLS-1$
71 private static final Image EXPORT_IMAGE
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/elcl16/export_button.gif"); //$NON-NLS-1$
73 // ------------------------------------------------------------------------
74 // Main data structures
75 // ------------------------------------------------------------------------
77 protected Shell fShell
;
78 protected ScrolledComposite fScrolledComposite
;
79 protected Composite fListComposite
;
80 protected Composite fFillerComposite
;
82 private ColorSettingRow fSelectedRow
= null;
84 private TimeGraphColorScheme traceColorScheme
= new TimeGraphColorScheme();
85 private ITimeGraphProvider timeAnalysisProvider
= new TimeGraphProvider() {
87 public StateColor
getEventColor(ITimeEvent event
) {
91 public String
getTraceClassName(ITimeGraphEntry trace
) {
95 public String
getEventName(ITimeEvent event
, boolean upper
, boolean extInfo
) {
99 public Map
<String
, String
> getEventHoverToolTipInfo(ITimeEvent event
) {
103 public String
getStateName(StateColor color
) {
108 Action fDeleteAction
;
109 Action fMoveUpAction
;
110 Action fMoveDownAction
;
111 Action fImportAction
;
112 Action fExportAction
;
114 protected List
<ColorSetting
> fColorSettings
;
116 // ------------------------------------------------------------------------
118 // ------------------------------------------------------------------------
121 * Default Constructor
123 public ColorsView() {
124 super("Colors"); //$NON-NLS-1$
128 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(Composite)
131 public void createPartControl(Composite parent
) {
132 fShell
= parent
.getShell();
134 fScrolledComposite
= new ScrolledComposite(parent
, SWT
.V_SCROLL
| SWT
.H_SCROLL
);
135 fScrolledComposite
.setExpandHorizontal(true);
136 fScrolledComposite
.setExpandVertical(true);
137 fListComposite
= new Composite(fScrolledComposite
, SWT
.NONE
);
138 fScrolledComposite
.setContent(fListComposite
);
140 GridLayout gl
= new GridLayout();
143 gl
.verticalSpacing
= 1;
144 fListComposite
.setLayout(gl
);
146 fColorSettings
= new ArrayList
<ColorSetting
>(Arrays
.asList(ColorSettingsManager
.getColorSettings()));
147 for (ColorSetting colorSetting
: fColorSettings
) {
148 new ColorSettingRow(fListComposite
, colorSetting
);
151 fFillerComposite
= new Composite(fListComposite
, SWT
.NONE
);
152 GridData gd
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
154 fFillerComposite
.setLayoutData(gd
);
155 gl
= new GridLayout();
158 fFillerComposite
.setLayout(gl
);
159 Label fillerLabel
= new Label(fFillerComposite
, SWT
.NONE
);
160 fillerLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
161 fillerLabel
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
163 fFillerComposite
.addPaintListener(new PaintListener() {
165 public void paintControl(PaintEvent e
) {
166 if (fSelectedRow
== null) {
167 Color lineColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
168 Point p
= fFillerComposite
.getSize();
170 gc
.setForeground(lineColor
);
171 gc
.drawLine(0, 0, p
.x
- 1, 0);
176 MouseListener mouseListener
= new MouseAdapter() {
178 public void mouseDown(MouseEvent e
) {
183 fillerLabel
.addMouseListener(mouseListener
);
185 fScrolledComposite
.setMinSize(fListComposite
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
));
191 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
194 public void setFocus() {
195 fScrolledComposite
.setFocus();
198 public void refresh() {
199 fListComposite
.layout();
200 fScrolledComposite
.setMinSize(fListComposite
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
));
201 fListComposite
.redraw(0, 0, fListComposite
.getBounds().width
, fListComposite
.getBounds().height
, true);
202 if (fSelectedRow
== null) {
203 fDeleteAction
.setEnabled(false);
204 fMoveUpAction
.setEnabled(false);
205 fMoveDownAction
.setEnabled(false);
207 fDeleteAction
.setEnabled(true);
208 fMoveUpAction
.setEnabled(true);
209 fMoveDownAction
.setEnabled(true);
213 private void fillToolBar() {
215 fAddAction
= new AddAction();
216 fAddAction
.setImageDescriptor(ImageDescriptor
.createFromImage(ADD_IMAGE
));
217 fAddAction
.setToolTipText(Messages
.ColorsView_AddActionToolTipText
);
219 fDeleteAction
= new DeleteAction();
220 fDeleteAction
.setImageDescriptor(ImageDescriptor
.createFromImage(DELETE_IMAGE
));
221 fDeleteAction
.setToolTipText(Messages
.ColorsView_DeleteActionToolTipText
);
222 fDeleteAction
.setEnabled(false);
224 fMoveUpAction
= new MoveUpAction();
225 fMoveUpAction
.setImageDescriptor(ImageDescriptor
.createFromImage(MOVE_UP_IMAGE
));
226 fMoveUpAction
.setToolTipText(Messages
.ColorsView_MoveUpActionToolTipText
);
227 fMoveUpAction
.setEnabled(false);
229 fMoveDownAction
= new MoveDownAction();
230 fMoveDownAction
.setImageDescriptor(ImageDescriptor
.createFromImage(MOVE_DOWN_IMAGE
));
231 fMoveDownAction
.setToolTipText(Messages
.ColorsView_MoveDownActionToolTipText
);
232 fMoveDownAction
.setEnabled(false);
234 fExportAction
= new ExportAction();
235 fExportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(EXPORT_IMAGE
));
236 fExportAction
.setToolTipText(Messages
.ColorsView_ExportActionToolTipText
);
238 fImportAction
= new ImportAction();
239 fImportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(IMPORT_IMAGE
));
240 fImportAction
.setToolTipText(Messages
.ColorsView_ImportActionToolTipText
);
242 IActionBars bars
= getViewSite().getActionBars();
243 IToolBarManager manager
= bars
.getToolBarManager();
244 manager
.add(fAddAction
);
245 manager
.add(fDeleteAction
);
246 manager
.add(fMoveUpAction
);
247 manager
.add(fMoveDownAction
);
248 manager
.add(new Separator());
249 manager
.add(fExportAction
);
250 manager
.add(fImportAction
);
253 private class AddAction
extends Action
{
256 ColorSetting colorSetting
= new ColorSetting(
257 Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_FOREGROUND
).getRGB(),
258 Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
).getRGB(),
259 TimeGraphColorScheme
.BLACK_STATE
,
261 ColorSettingRow row
= new ColorSettingRow(fListComposite
, colorSetting
);
262 if (fSelectedRow
== null) {
263 fColorSettings
.add(colorSetting
);
264 row
.moveAbove(fFillerComposite
);
266 fColorSettings
.add(fColorSettings
.indexOf(fSelectedRow
.getColorSetting()), colorSetting
);
267 row
.moveAbove(fSelectedRow
);
271 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
275 private class DeleteAction
extends Action
{
278 if (fSelectedRow
!= null) {
279 int index
= fColorSettings
.indexOf(fSelectedRow
.getColorSetting());
280 fColorSettings
.remove(index
);
281 fSelectedRow
.fColorSetting
.dispose();
282 fSelectedRow
.dispose();
283 if (index
< fColorSettings
.size()) {
284 fSelectedRow
= (ColorSettingRow
) fListComposite
.getChildren()[index
];
289 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
294 private class MoveUpAction
extends Action
{
297 if (fSelectedRow
!= null) {
298 int index
= fColorSettings
.indexOf(fSelectedRow
.getColorSetting());
300 fColorSettings
.add(index
- 1, fColorSettings
.remove(index
));
301 fSelectedRow
.moveAbove(fListComposite
.getChildren()[index
- 1]);
303 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
309 private class MoveDownAction
extends Action
{
312 if (fSelectedRow
!= null) {
313 int index
= fColorSettings
.indexOf(fSelectedRow
.getColorSetting());
314 if (index
< fColorSettings
.size() - 1) {
315 fColorSettings
.add(index
+ 1, fColorSettings
.remove(index
));
316 fSelectedRow
.moveBelow(fListComposite
.getChildren()[index
+ 1]);
318 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
324 private class ExportAction
extends Action
{
327 FileDialog fileDialog
= new FileDialog(fShell
, SWT
.SAVE
);
328 fileDialog
.setFilterExtensions(new String
[] {"*.xml"}); //$NON-NLS-1$
329 fileDialog
.setOverwrite(true);
330 String pathName
= fileDialog
.open();
331 if (pathName
!= null) {
332 ColorSettingsXML
.save(pathName
, fColorSettings
.toArray(new ColorSetting
[0]));
337 private class ImportAction
extends Action
{
340 FileDialog fileDialog
= new FileDialog(fShell
, SWT
.OPEN
);
341 fileDialog
.setFilterExtensions(new String
[] {"*.xml"}); //$NON-NLS-1$
342 String pathName
= fileDialog
.open();
343 if (pathName
!= null) {
344 ColorSetting
[] colorSettings
= ColorSettingsXML
.load(pathName
);
345 if (colorSettings
.length
> 0) {
346 if (fColorSettings
.size() > 0) {
347 boolean overwrite
= MessageDialog
.openQuestion(fShell
,
348 Messages
.ColorsView_ImportOverwriteDialogTitle
,
349 Messages
.ColorsView_ImportOverwriteDialogMessage1
+
350 Messages
.ColorsView_ImportOverwriteDialogMessage2
);
352 for (Control control
: fListComposite
.getChildren()) {
353 if (control
instanceof ColorSettingRow
) {
354 ((ColorSettingRow
) control
).fColorSetting
.dispose();
358 fColorSettings
= new ArrayList
<ColorSetting
>();
362 for (ColorSetting colorSetting
: colorSettings
) {
363 ColorSettingRow row
= new ColorSettingRow(fListComposite
, colorSetting
);
364 if (fSelectedRow
== null) {
365 fColorSettings
.add(colorSetting
);
366 row
.moveAbove(fFillerComposite
);
368 fColorSettings
.add(fColorSettings
.indexOf(fSelectedRow
.getColorSetting()), colorSetting
);
369 row
.moveAbove(fSelectedRow
);
373 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
379 private class ColorSettingRow
extends Composite
{
381 ColorSetting fColorSetting
;
383 public ColorSettingRow(final Composite parent
, final ColorSetting colorSetting
) {
384 super(parent
, SWT
.NONE
);
385 fColorSetting
= colorSetting
;
387 setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
389 setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
390 GridLayout gl
= new GridLayout(7, false);
393 gl
.horizontalSpacing
= 1;
394 gl
.verticalSpacing
= 0;
397 final Button fgButton
= new Button(this, SWT
.PUSH
);
398 fgButton
.setText(Messages
.ColorsView_ForegroundButtonText
);
399 fgButton
.setSize(fgButton
.computeSize(SWT
.DEFAULT
, 19));
400 fgButton
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
402 final Button bgButton
= new Button(this, SWT
.PUSH
);
403 bgButton
.setText(Messages
.ColorsView_BackgroundButtonText
);
404 bgButton
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
406 final Composite labelComposite
= new Composite(this, SWT
.NONE
);
407 labelComposite
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.FILL
, false, false));
408 gl
= new GridLayout();
411 labelComposite
.setLayout(gl
);
412 labelComposite
.setBackground(colorSetting
.getBackgroundColor());
414 final Label label
= new Label(labelComposite
, SWT
.NONE
);
415 label
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, false, true));
416 label
.setText(" Text "); //$NON-NLS-1$
417 label
.setForeground(colorSetting
.getForegroundColor());
418 label
.setBackground(colorSetting
.getBackgroundColor());
420 fgButton
.addSelectionListener(new SelectionAdapter() {
422 public void widgetSelected(SelectionEvent e
) {
423 fSelectedRow
= ColorSettingRow
.this;
425 ColorDialog dialog
= new ColorDialog(fShell
);
426 dialog
.setRGB(colorSetting
.getForegroundRGB());
427 dialog
.setText(Messages
.ColorsView_ForegroundDialogText
);
429 colorSetting
.setForegroundRGB(dialog
.getRGB());
430 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
431 label
.setForeground(colorSetting
.getForegroundColor());
434 bgButton
.addSelectionListener(new SelectionAdapter() {
436 public void widgetSelected(SelectionEvent e
) {
437 fSelectedRow
= ColorSettingRow
.this;
439 ColorDialog dialog
= new ColorDialog(fShell
);
440 dialog
.setRGB(colorSetting
.getBackgroundRGB());
441 dialog
.setText(Messages
.ColorsView_BackgroundDialogText
);
443 colorSetting
.setBackgroundRGB(dialog
.getRGB());
444 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
445 labelComposite
.setBackground(colorSetting
.getBackgroundColor());
446 label
.setBackground(colorSetting
.getBackgroundColor());
449 final Button tickButton
= new Button(this, SWT
.PUSH
);
450 tickButton
.setText(Messages
.ColorsView_TickButtonText
);
451 tickButton
.setSize(tickButton
.computeSize(SWT
.DEFAULT
, 19));
452 tickButton
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
454 final Canvas tickCanvas
= new Canvas(this, SWT
.NONE
);
455 GridData gd
= new GridData(SWT
.CENTER
, SWT
.FILL
, false, false);
457 gd
.heightHint
= bgButton
.getSize().y
;
458 tickCanvas
.setLayoutData(gd
);
459 tickCanvas
.setBackground(traceColorScheme
.getBkColor(false, false, false));
460 tickCanvas
.addPaintListener(new PaintListener() {
462 public void paintControl(PaintEvent e
) {
463 Rectangle bounds
= tickCanvas
.getBounds();
464 e
.gc
.setForeground(traceColorScheme
.getColor(TimeGraphColorScheme
.MID_LINE
));
465 int midy
= bounds
.y
+ bounds
.height
/ 2 - 1;
466 //int midy = e.y + e.height / 2;
467 e
.gc
.drawLine(e
.x
, midy
, e
.x
+ e
.width
, midy
);
468 Rectangle rect
= new Rectangle(e
.x
+ 1, bounds
.y
+ 2, 0, bounds
.height
- 6);
469 for (int i
= 1; i
<= 3; i
++) {
472 timeAnalysisProvider
.drawState(traceColorScheme
, fColorSetting
.getTickColorIndex(), rect
, e
.gc
, false, false, false);
476 tickButton
.addSelectionListener(new SelectionAdapter() {
478 public void widgetSelected(SelectionEvent e
) {
479 fSelectedRow
= ColorSettingRow
.this;
481 TickColorDialog dialog
= new TickColorDialog(fShell
);
482 dialog
.setColorIndex(colorSetting
.getTickColorIndex());
484 if (dialog
.getReturnCode() == Dialog
.OK
) {
485 if (dialog
.getColorIndex() != colorSetting
.getTickColorIndex()) {
486 colorSetting
.setTickColorIndex(dialog
.getColorIndex());
487 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
493 final Button filterButton
= new Button(this, SWT
.PUSH
);
494 filterButton
.setText(Messages
.ColorsView_FilterButtonText
);
495 filterButton
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
497 final Label filterText
= new Label(this, SWT
.NONE
);
498 if (colorSetting
.getFilter() != null) {
499 filterText
.setText(colorSetting
.getFilter().toString());
500 filterText
.setToolTipText(colorSetting
.getFilter().toString());
502 filterText
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
503 filterText
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
505 filterButton
.addSelectionListener(new SelectionAdapter() {
507 public void widgetSelected(SelectionEvent e
) {
508 fSelectedRow
= ColorSettingRow
.this;
510 FilterDialog dialog
= new FilterDialog(fShell
);
511 dialog
.setFilter(colorSetting
.getFilter());
513 if (dialog
.getReturnCode() == Dialog
.OK
) {
514 if (dialog
.getFilter() != null) {
515 colorSetting
.setFilter(dialog
.getFilter());
516 filterText
.setText(dialog
.getFilter().toString());
517 filterText
.setToolTipText(dialog
.getFilter().toString());
519 colorSetting
.setFilter(null);
520 filterText
.setText(""); //$NON-NLS-1$
521 filterText
.setToolTipText(""); //$NON-NLS-1$
523 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
528 addPaintListener(new PaintListener() {
530 public void paintControl(PaintEvent e
) {
531 if (fSelectedRow
== ColorSettingRow
.this) {
532 Color borderColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
533 Point p
= ColorSettingRow
.this.getSize();
534 Rectangle rect
= new Rectangle(0, 0, p
.x
- 1, p
.y
- 1);
536 gc
.setForeground(borderColor
);
537 gc
.drawRectangle(rect
);
542 MouseListener mouseListener
= new MouseAdapter() {
544 public void mouseDown(MouseEvent e
) {
545 fSelectedRow
= ColorSettingRow
.this;
549 addMouseListener(mouseListener
);
550 label
.addMouseListener(mouseListener
);
551 tickCanvas
.addMouseListener(mouseListener
);
552 filterText
.addMouseListener(mouseListener
);
556 * @return the ColorSetting
558 public ColorSetting
getColorSetting() {
559 return fColorSetting
;