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
.tmf
.ui
.TmfUiPlugin
;
27 import org
.eclipse
.linuxtools
.tmf
.ui
.internal
.Messages
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.TmfTimeAnalysisProvider
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.model
.ITimeEvent
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.model
.ITmfTimeAnalysisEntry
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.widgets
.TraceColorScheme
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.TmfView
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.filter
.FilterDialog
;
34 import org
.eclipse
.swt
.SWT
;
35 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
36 import org
.eclipse
.swt
.events
.MouseAdapter
;
37 import org
.eclipse
.swt
.events
.MouseEvent
;
38 import org
.eclipse
.swt
.events
.MouseListener
;
39 import org
.eclipse
.swt
.events
.PaintEvent
;
40 import org
.eclipse
.swt
.events
.PaintListener
;
41 import org
.eclipse
.swt
.events
.SelectionAdapter
;
42 import org
.eclipse
.swt
.events
.SelectionEvent
;
43 import org
.eclipse
.swt
.graphics
.Color
;
44 import org
.eclipse
.swt
.graphics
.GC
;
45 import org
.eclipse
.swt
.graphics
.Image
;
46 import org
.eclipse
.swt
.graphics
.Point
;
47 import org
.eclipse
.swt
.graphics
.Rectangle
;
48 import org
.eclipse
.swt
.layout
.GridData
;
49 import org
.eclipse
.swt
.layout
.GridLayout
;
50 import org
.eclipse
.swt
.widgets
.Button
;
51 import org
.eclipse
.swt
.widgets
.Canvas
;
52 import org
.eclipse
.swt
.widgets
.ColorDialog
;
53 import org
.eclipse
.swt
.widgets
.Composite
;
54 import org
.eclipse
.swt
.widgets
.Control
;
55 import org
.eclipse
.swt
.widgets
.Display
;
56 import org
.eclipse
.swt
.widgets
.FileDialog
;
57 import org
.eclipse
.swt
.widgets
.Label
;
58 import org
.eclipse
.swt
.widgets
.Shell
;
59 import org
.eclipse
.ui
.IActionBars
;
61 public class ColorsView
extends TmfView
{
63 public static final String ID
= "org.eclipse.linuxtools.tmf.ui.views.colors"; //$NON-NLS-1$
65 private static final Image ADD_IMAGE
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$
66 private static final Image DELETE_IMAGE
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/elcl16/delete_button.gif"); //$NON-NLS-1$
67 private static final Image MOVE_UP_IMAGE
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/elcl16/moveup_button.gif"); //$NON-NLS-1$
68 private static final Image MOVE_DOWN_IMAGE
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/elcl16/movedown_button.gif"); //$NON-NLS-1$
69 private static final Image IMPORT_IMAGE
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/elcl16/import_button.gif"); //$NON-NLS-1$
70 private static final Image EXPORT_IMAGE
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/elcl16/export_button.gif"); //$NON-NLS-1$
72 // ------------------------------------------------------------------------
73 // Main data structures
74 // ------------------------------------------------------------------------
76 protected Shell fShell
;
77 protected ScrolledComposite fScrolledComposite
;
78 protected Composite fListComposite
;
79 protected Composite fFillerComposite
;
81 private ColorSettingRow fSelectedRow
= null;
83 private TraceColorScheme traceColorScheme
= new TraceColorScheme();
84 private TmfTimeAnalysisProvider timeAnalysisProvider
= new TmfTimeAnalysisProvider() {
86 public StateColor
getEventColor(ITimeEvent event
) {
90 public String
getTraceClassName(ITmfTimeAnalysisEntry trace
) {
94 public String
getEventName(ITimeEvent event
, boolean upper
, boolean extInfo
) {
98 public Map
<String
, String
> getEventHoverToolTipInfo(ITimeEvent event
) {
102 public String
getStateName(StateColor color
) {
107 Action fDeleteAction
;
108 Action fMoveUpAction
;
109 Action fMoveDownAction
;
110 Action fImportAction
;
111 Action fExportAction
;
113 protected List
<ColorSetting
> fColorSettings
;
115 // ------------------------------------------------------------------------
117 // ------------------------------------------------------------------------
120 * Default Constructor
122 public ColorsView() {
123 super("Colors"); //$NON-NLS-1$
127 * @see org.eclipse.linuxtools.tmf.ui.views.TmfView#createPartControl(org.eclipse.swt.widgets.Composite)
130 public void createPartControl(Composite parent
) {
131 fShell
= parent
.getShell();
133 fScrolledComposite
= new ScrolledComposite(parent
, SWT
.V_SCROLL
| SWT
.H_SCROLL
);
134 fScrolledComposite
.setExpandHorizontal(true);
135 fScrolledComposite
.setExpandVertical(true);
136 fListComposite
= new Composite(fScrolledComposite
, SWT
.NONE
);
137 fScrolledComposite
.setContent(fListComposite
);
139 GridLayout gl
= new GridLayout();
142 gl
.verticalSpacing
= 1;
143 fListComposite
.setLayout(gl
);
145 fColorSettings
= new ArrayList
<ColorSetting
>(Arrays
.asList(ColorSettingsManager
.getColorSettings()));
146 for (ColorSetting colorSetting
: fColorSettings
) {
147 new ColorSettingRow(fListComposite
, colorSetting
);
150 fFillerComposite
= new Composite(fListComposite
, SWT
.NONE
);
151 GridData gd
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
153 fFillerComposite
.setLayoutData(gd
);
154 gl
= new GridLayout();
157 fFillerComposite
.setLayout(gl
);
158 Label fillerLabel
= new Label(fFillerComposite
, SWT
.NONE
);
159 fillerLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
160 fillerLabel
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
162 fFillerComposite
.addPaintListener(new PaintListener() {
164 public void paintControl(PaintEvent e
) {
165 if (fSelectedRow
== null) {
166 Color lineColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
167 Point p
= fFillerComposite
.getSize();
169 gc
.setForeground(lineColor
);
170 gc
.drawLine(0, 0, p
.x
- 1, 0);
175 MouseListener mouseListener
= new MouseAdapter() {
177 public void mouseDown(MouseEvent e
) {
182 fillerLabel
.addMouseListener(mouseListener
);
184 fScrolledComposite
.setMinSize(fListComposite
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
));
189 public void refresh() {
190 fListComposite
.layout();
191 fScrolledComposite
.setMinSize(fListComposite
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
));
192 fListComposite
.redraw(0, 0, fListComposite
.getBounds().width
, fListComposite
.getBounds().height
, true);
193 if (fSelectedRow
== null) {
194 fDeleteAction
.setEnabled(false);
195 fMoveUpAction
.setEnabled(false);
196 fMoveDownAction
.setEnabled(false);
198 fDeleteAction
.setEnabled(true);
199 fMoveUpAction
.setEnabled(true);
200 fMoveDownAction
.setEnabled(true);
204 private void fillToolBar() {
206 fAddAction
= new AddAction();
207 fAddAction
.setImageDescriptor(ImageDescriptor
.createFromImage(ADD_IMAGE
));
208 fAddAction
.setToolTipText(Messages
.ColorsView_AddActionToolTipText
);
210 fDeleteAction
= new DeleteAction();
211 fDeleteAction
.setImageDescriptor(ImageDescriptor
.createFromImage(DELETE_IMAGE
));
212 fDeleteAction
.setToolTipText(Messages
.ColorsView_DeleteActionToolTipText
);
213 fDeleteAction
.setEnabled(false);
215 fMoveUpAction
= new MoveUpAction();
216 fMoveUpAction
.setImageDescriptor(ImageDescriptor
.createFromImage(MOVE_UP_IMAGE
));
217 fMoveUpAction
.setToolTipText(Messages
.ColorsView_MoveUpActionToolTipText
);
218 fMoveUpAction
.setEnabled(false);
220 fMoveDownAction
= new MoveDownAction();
221 fMoveDownAction
.setImageDescriptor(ImageDescriptor
.createFromImage(MOVE_DOWN_IMAGE
));
222 fMoveDownAction
.setToolTipText(Messages
.ColorsView_MoveDownActionToolTipText
);
223 fMoveDownAction
.setEnabled(false);
225 fExportAction
= new ExportAction();
226 fExportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(EXPORT_IMAGE
));
227 fExportAction
.setToolTipText(Messages
.ColorsView_ExportActionToolTipText
);
229 fImportAction
= new ImportAction();
230 fImportAction
.setImageDescriptor(ImageDescriptor
.createFromImage(IMPORT_IMAGE
));
231 fImportAction
.setToolTipText(Messages
.ColorsView_ImportActionToolTipText
);
233 IActionBars bars
= getViewSite().getActionBars();
234 IToolBarManager manager
= bars
.getToolBarManager();
235 manager
.add(fAddAction
);
236 manager
.add(fDeleteAction
);
237 manager
.add(fMoveUpAction
);
238 manager
.add(fMoveDownAction
);
239 manager
.add(new Separator());
240 manager
.add(fExportAction
);
241 manager
.add(fImportAction
);
244 private class AddAction
extends Action
{
247 ColorSetting colorSetting
= new ColorSetting(
248 Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_FOREGROUND
).getRGB(),
249 Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
).getRGB(),
250 TraceColorScheme
.BLACK_STATE
,
252 ColorSettingRow row
= new ColorSettingRow(fListComposite
, colorSetting
);
253 if (fSelectedRow
== null) {
254 fColorSettings
.add(colorSetting
);
255 row
.moveAbove(fFillerComposite
);
257 fColorSettings
.add(fColorSettings
.indexOf(fSelectedRow
.getColorSetting()), colorSetting
);
258 row
.moveAbove(fSelectedRow
);
262 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
266 private class DeleteAction
extends Action
{
269 if (fSelectedRow
!= null) {
270 int index
= fColorSettings
.indexOf(fSelectedRow
.getColorSetting());
271 fColorSettings
.remove(index
);
272 fSelectedRow
.fColorSetting
.dispose();
273 fSelectedRow
.dispose();
274 if (index
< fColorSettings
.size()) {
275 fSelectedRow
= (ColorSettingRow
) fListComposite
.getChildren()[index
];
280 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
285 private class MoveUpAction
extends Action
{
288 if (fSelectedRow
!= null) {
289 int index
= fColorSettings
.indexOf(fSelectedRow
.getColorSetting());
291 fColorSettings
.add(index
- 1, fColorSettings
.remove(index
));
292 fSelectedRow
.moveAbove(fListComposite
.getChildren()[index
- 1]);
294 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
300 private class MoveDownAction
extends Action
{
303 if (fSelectedRow
!= null) {
304 int index
= fColorSettings
.indexOf(fSelectedRow
.getColorSetting());
305 if (index
< fColorSettings
.size() - 1) {
306 fColorSettings
.add(index
+ 1, fColorSettings
.remove(index
));
307 fSelectedRow
.moveBelow(fListComposite
.getChildren()[index
+ 1]);
309 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
315 private class ExportAction
extends Action
{
318 FileDialog fileDialog
= new FileDialog(fShell
, SWT
.SAVE
);
319 fileDialog
.setFilterExtensions(new String
[] {"*.xml"}); //$NON-NLS-1$
320 fileDialog
.setOverwrite(true);
321 String pathName
= fileDialog
.open();
322 if (pathName
!= null) {
323 ColorSettingsXML
.save(pathName
, fColorSettings
.toArray(new ColorSetting
[0]));
328 private class ImportAction
extends Action
{
331 FileDialog fileDialog
= new FileDialog(fShell
, SWT
.OPEN
);
332 fileDialog
.setFilterExtensions(new String
[] {"*.xml"}); //$NON-NLS-1$
333 String pathName
= fileDialog
.open();
334 if (pathName
!= null) {
335 ColorSetting
[] colorSettings
= ColorSettingsXML
.load(pathName
);
336 if (colorSettings
.length
> 0) {
337 if (fColorSettings
.size() > 0) {
338 boolean overwrite
= MessageDialog
.openQuestion(fShell
,
339 Messages
.ColorsView_ImportOverwriteDialogTitle
,
340 Messages
.ColorsView_ImportOverwriteDialogMessage1
+
341 Messages
.ColorsView_ImportOverwriteDialogMessage2
);
343 for (Control control
: fListComposite
.getChildren()) {
344 if (control
instanceof ColorSettingRow
) {
345 ((ColorSettingRow
) control
).fColorSetting
.dispose();
349 fColorSettings
= new ArrayList
<ColorSetting
>();
353 for (ColorSetting colorSetting
: colorSettings
) {
354 ColorSettingRow row
= new ColorSettingRow(fListComposite
, colorSetting
);
355 if (fSelectedRow
== null) {
356 fColorSettings
.add(colorSetting
);
357 row
.moveAbove(fFillerComposite
);
359 fColorSettings
.add(fColorSettings
.indexOf(fSelectedRow
.getColorSetting()), colorSetting
);
360 row
.moveAbove(fSelectedRow
);
364 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
370 private class ColorSettingRow
extends Composite
{
372 ColorSetting fColorSetting
;
374 public ColorSettingRow(final Composite parent
, final ColorSetting colorSetting
) {
375 super(parent
, SWT
.NONE
);
376 fColorSetting
= colorSetting
;
378 setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
380 setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
381 GridLayout gl
= new GridLayout(7, false);
384 gl
.horizontalSpacing
= 1;
385 gl
.verticalSpacing
= 0;
388 final Button fgButton
= new Button(this, SWT
.PUSH
);
389 fgButton
.setText(Messages
.ColorsView_ForegroundButtonText
);
390 fgButton
.setSize(fgButton
.computeSize(SWT
.DEFAULT
, 19));
391 fgButton
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
393 final Button bgButton
= new Button(this, SWT
.PUSH
);
394 bgButton
.setText(Messages
.ColorsView_BackgroundButtonText
);
395 bgButton
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
397 final Composite labelComposite
= new Composite(this, SWT
.NONE
);
398 labelComposite
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.FILL
, false, false));
399 gl
= new GridLayout();
402 labelComposite
.setLayout(gl
);
403 labelComposite
.setBackground(colorSetting
.getBackgroundColor());
405 final Label label
= new Label(labelComposite
, SWT
.NONE
);
406 label
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, false, true));
407 label
.setText(" Text "); //$NON-NLS-1$
408 label
.setForeground(colorSetting
.getForegroundColor());
409 label
.setBackground(colorSetting
.getBackgroundColor());
411 fgButton
.addSelectionListener(new SelectionAdapter() {
413 public void widgetSelected(SelectionEvent e
) {
414 fSelectedRow
= ColorSettingRow
.this;
416 ColorDialog dialog
= new ColorDialog(fShell
);
417 dialog
.setRGB(colorSetting
.getForegroundRGB());
418 dialog
.setText(Messages
.ColorsView_ForegroundDialogText
);
420 colorSetting
.setForegroundRGB(dialog
.getRGB());
421 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
422 label
.setForeground(colorSetting
.getForegroundColor());
425 bgButton
.addSelectionListener(new SelectionAdapter() {
427 public void widgetSelected(SelectionEvent e
) {
428 fSelectedRow
= ColorSettingRow
.this;
430 ColorDialog dialog
= new ColorDialog(fShell
);
431 dialog
.setRGB(colorSetting
.getBackgroundRGB());
432 dialog
.setText(Messages
.ColorsView_BackgroundDialogText
);
434 colorSetting
.setBackgroundRGB(dialog
.getRGB());
435 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
436 labelComposite
.setBackground(colorSetting
.getBackgroundColor());
437 label
.setBackground(colorSetting
.getBackgroundColor());
440 final Button tickButton
= new Button(this, SWT
.PUSH
);
441 tickButton
.setText(Messages
.ColorsView_TickButtonText
);
442 tickButton
.setSize(tickButton
.computeSize(SWT
.DEFAULT
, 19));
443 tickButton
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
445 final Canvas tickCanvas
= new Canvas(this, SWT
.NONE
);
446 GridData gd
= new GridData(SWT
.CENTER
, SWT
.FILL
, false, false);
448 gd
.heightHint
= bgButton
.getSize().y
;
449 tickCanvas
.setLayoutData(gd
);
450 tickCanvas
.setBackground(traceColorScheme
.getBkColor(false, false, false));
451 tickCanvas
.addPaintListener(new PaintListener() {
453 public void paintControl(PaintEvent e
) {
454 Rectangle bounds
= tickCanvas
.getBounds();
455 e
.gc
.setForeground(traceColorScheme
.getColor(TraceColorScheme
.MID_LINE
));
456 int midy
= bounds
.y
+ bounds
.height
/ 2 - 1;
457 //int midy = e.y + e.height / 2;
458 e
.gc
.drawLine(e
.x
, midy
, e
.x
+ e
.width
, midy
);
459 Rectangle rect
= new Rectangle(e
.x
+ 1, bounds
.y
+ 2, 0, bounds
.height
- 6);
460 for (int i
= 1; i
<= 3; i
++) {
463 timeAnalysisProvider
.drawState(traceColorScheme
, fColorSetting
.getTickColorIndex(), rect
, e
.gc
, false, false, false);
467 tickButton
.addSelectionListener(new SelectionAdapter() {
469 public void widgetSelected(SelectionEvent e
) {
470 fSelectedRow
= ColorSettingRow
.this;
472 TickColorDialog dialog
= new TickColorDialog(fShell
);
473 dialog
.setColorIndex(colorSetting
.getTickColorIndex());
475 if (dialog
.getReturnCode() == Dialog
.OK
) {
476 if (dialog
.getColorIndex() != colorSetting
.getTickColorIndex()) {
477 colorSetting
.setTickColorIndex(dialog
.getColorIndex());
478 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
484 final Button filterButton
= new Button(this, SWT
.PUSH
);
485 filterButton
.setText(Messages
.ColorsView_FilterButtonText
);
486 filterButton
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
488 final Label filterText
= new Label(this, SWT
.NONE
);
489 if (colorSetting
.getFilter() != null) {
490 filterText
.setText(colorSetting
.getFilter().toString());
491 filterText
.setToolTipText(colorSetting
.getFilter().toString());
493 filterText
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
494 filterText
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
496 filterButton
.addSelectionListener(new SelectionAdapter() {
498 public void widgetSelected(SelectionEvent e
) {
499 fSelectedRow
= ColorSettingRow
.this;
501 FilterDialog dialog
= new FilterDialog(fShell
);
502 dialog
.setFilter(colorSetting
.getFilter());
504 if (dialog
.getReturnCode() == Dialog
.OK
) {
505 if (dialog
.getFilter() != null) {
506 colorSetting
.setFilter(dialog
.getFilter());
507 filterText
.setText(dialog
.getFilter().toString());
508 filterText
.setToolTipText(dialog
.getFilter().toString());
510 colorSetting
.setFilter(null);
511 filterText
.setText(""); //$NON-NLS-1$
512 filterText
.setToolTipText(""); //$NON-NLS-1$
514 ColorSettingsManager
.setColorSettings(fColorSettings
.toArray(new ColorSetting
[0]));
519 addPaintListener(new PaintListener() {
521 public void paintControl(PaintEvent e
) {
522 if (fSelectedRow
== ColorSettingRow
.this) {
523 Color borderColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
524 Point p
= ColorSettingRow
.this.getSize();
525 Rectangle rect
= new Rectangle(0, 0, p
.x
- 1, p
.y
- 1);
527 gc
.setForeground(borderColor
);
528 gc
.drawRectangle(rect
);
533 MouseListener mouseListener
= new MouseAdapter() {
535 public void mouseDown(MouseEvent e
) {
536 fSelectedRow
= ColorSettingRow
.this;
540 addMouseListener(mouseListener
);
541 label
.addMouseListener(mouseListener
);
542 tickCanvas
.addMouseListener(mouseListener
);
543 filterText
.addMouseListener(mouseListener
);
547 * @return the ColorSetting
549 public ColorSetting
getColorSetting() {
550 return fColorSetting
;