ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / widgets / timegraph / dialogs / TimeGraphLegend.java
index c8668b76def2a7580e8757595cca35176514c1ac..5e2b084e8599e2a2aa722f21e2811dacd6615eb3 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2009, 2012 Ericsson.\r
- * \r
- * All rights reserved. This program and the accompanying materials are\r
- * made available under the terms of the Eclipse Public License v1.0 which\r
- * accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- * \r
- * Contributors:\r
- *   Alvaro Sanchez-Leon - Initial API and implementation\r
- *   Patrick Tasse - Refactoring\r
- *******************************************************************************/\r
-\r
-package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.dialogs;\r
-\r
-import org.eclipse.jface.dialogs.IDialogConstants;\r
-import org.eclipse.jface.dialogs.TitleAreaDialog;\r
-import org.eclipse.linuxtools.internal.tmf.ui.Messages;\r
-import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphProvider;\r
-import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.TimeGraphProvider.StateColor;\r
-import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.TimeGraphColorScheme;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.graphics.Color;\r
-import org.eclipse.swt.graphics.GC;\r
-import org.eclipse.swt.graphics.Rectangle;\r
-import org.eclipse.swt.layout.GridData;\r
-import org.eclipse.swt.layout.GridLayout;\r
-import org.eclipse.swt.widgets.Canvas;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Control;\r
-import org.eclipse.swt.widgets.Event;\r
-import org.eclipse.swt.widgets.Group;\r
-import org.eclipse.swt.widgets.Label;\r
-import org.eclipse.swt.widgets.Listener;\r
-import org.eclipse.swt.widgets.Shell;\r
-\r
-\r
-public class TimeGraphLegend extends TitleAreaDialog {\r
-\r
-    public static final int interactionColors[] = {\r
-        TimeGraphColorScheme.TI_START_THREAD,\r
-        TimeGraphColorScheme.TI_NOTIFY_JOINED, TimeGraphColorScheme.TI_NOTIFY,\r
-        TimeGraphColorScheme.TI_INTERRUPT, TimeGraphColorScheme.TI_HANDOFF_LOCK };\r
-\r
-    protected TimeGraphColorScheme colors;\r
-    private ITimeGraphProvider ifUtil;\r
-\r
-    public static void open(Shell parent, ITimeGraphProvider rifUtil) {\r
-        (new TimeGraphLegend(parent, rifUtil)).open();\r
-    }\r
-\r
-    public TimeGraphLegend(Shell parent, ITimeGraphProvider rifUtil) {\r
-        super(parent);\r
-        colors = new TimeGraphColorScheme();\r
-        this.ifUtil = rifUtil;\r
-    }\r
-\r
-    @Override\r
-    protected Control createDialogArea(Composite parent) {\r
-        Composite dlgArea = (Composite) super.createDialogArea(parent);\r
-        Composite composite = new Composite(dlgArea, SWT.NONE);\r
-\r
-        GridLayout layout = new GridLayout();\r
-        layout.numColumns = 2;\r
-        composite.setLayout(layout);\r
-        GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);\r
-        composite.setLayoutData(gd);\r
-\r
-        createThreadStatesGroup(composite);\r
-\r
-        setMessage(Messages.TmfTimeLegend_LEGEND);\r
-        setTitle(Messages.TmfTimeLegend_TRACE_STATES_TITLE);\r
-        setDialogHelpAvailable(false);\r
-        setHelpAvailable(false);\r
-\r
-        return composite;\r
-    }\r
-\r
-    private void createThreadStatesGroup(Composite composite) {\r
-        Group gs = new Group(composite, SWT.NONE);\r
-        gs.setText(Messages.TmfTimeLegend_TRACE_STATES);\r
-        GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);\r
-        gs.setLayoutData(gd);\r
-\r
-        GridLayout layout = new GridLayout();\r
-        layout.numColumns = 2;\r
-        layout.marginWidth = 20;\r
-        layout.marginBottom = 10;\r
-        gs.setLayout(layout);\r
-\r
-        // Go through all the defined colors and only add the ones you need. \r
-        // This will not handle several colors assigned to a color, we have \r
-        // 16 mil colors, and should not pick two to mean the same thing. \r
-        for (int i = 0; i <  TimeGraphColorScheme.getStateColors().length; i++) {\r
-            //Get the color enum related to the index\r
-            StateColor stateColor = TimeGraphColorScheme.getStateColors()[i];\r
-            //Get the given name, provided by the interface to the application\r
-            String stateName = ifUtil.getStateName(stateColor);\r
-            if( stateName != "Not mapped" ) { //$NON-NLS-1$\r
-                Bar bar = new Bar(gs, i);\r
-                gd = new GridData();\r
-                gd.widthHint = 40;\r
-                gd.heightHint = 20;\r
-                gd.verticalIndent = 8;\r
-                bar.setLayoutData(gd);\r
-                Label name = new Label(gs, SWT.NONE);\r
-                name.setText(stateName);\r
-                gd = new GridData();\r
-                gd.horizontalIndent = 10;\r
-                gd.verticalIndent = 8;\r
-                name.setLayoutData(gd);\r
-            }\r
-        }\r
-    }\r
-\r
-    @Override\r
-    protected void configureShell(Shell shell) {\r
-        super.configureShell(shell);\r
-        shell.setText(Messages.TmfTimeLegend_WINDOW_TITLE);\r
-    }\r
-\r
-    @Override\r
-    protected void createButtonsForButtonBar(Composite parent) {\r
-        createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,\r
-                true);\r
-    }\r
-\r
-    class Bar extends Canvas {\r
-        private Color color;\r
-\r
-        public Bar(Composite parent, int colorIdx) {\r
-            super(parent, SWT.NONE);\r
-\r
-            color = colors.getColor(colorIdx);\r
-            addListener(SWT.Paint, new Listener() {\r
-                @Override\r
-                public void handleEvent(Event event) {\r
-                    draw(event.gc);\r
-                }\r
-            });\r
-        }\r
-\r
-        private void draw(GC gc) {\r
-            Rectangle r = getClientArea();\r
-            gc.setBackground(color);\r
-            gc.fillRectangle(r);\r
-            gc.setForeground(colors.getColor(TimeGraphColorScheme.BLACK));\r
-            gc.drawRectangle(0, 0, r.width - 1, r.height - 1);\r
-        }\r
-    }\r
-\r
-    class Arrow extends Canvas {\r
-        public final static int HEIGHT = 12;\r
-        public final static int DX = 3;\r
-\r
-        private Color color;\r
-\r
-        public Arrow(Composite parent, int colorIdx) {\r
-            super(parent, SWT.NONE);\r
-\r
-            color = colors.getColor(colorIdx);\r
-            addListener(SWT.Paint, new Listener() {\r
-                @Override\r
-                public void handleEvent(Event event) {\r
-                    draw(event.gc);\r
-                }\r
-            });\r
-        }\r
-\r
-        private void draw(GC gc) {\r
-            Rectangle r = getClientArea();\r
-            gc.setForeground(color);\r
-\r
-            int y0, y1;\r
-            if (r.height > HEIGHT) {\r
-                y0 = (r.height - HEIGHT) / 2;\r
-                y1 = y0 + HEIGHT;\r
-            } else {\r
-                y0 = 0;\r
-                y1 = r.height;\r
-            }\r
-\r
-            gc.drawLine(DX, y0, DX, y1);\r
-\r
-            gc.drawLine(0, y0 + 3, DX, y0);\r
-            gc.drawLine(2 * DX, y0 + 3, DX, y0);\r
-        }\r
-    }\r
-\r
-    class Mark extends Canvas {\r
-        public final static int DX = 3;\r
-\r
-        private Color color;\r
-\r
-        public Mark(Composite parent, int colorIdx) {\r
-            super(parent, SWT.NONE);\r
-\r
-            color = colors.getColor(colorIdx);\r
-            addListener(SWT.Paint, new Listener() {\r
-                @Override\r
-                public void handleEvent(Event event) {\r
-                    draw(event.gc);\r
-                }\r
-            });\r
-        }\r
-\r
-        private void draw(GC gc) {\r
-            Rectangle r = getClientArea();\r
-            gc.setBackground(color);\r
-\r
-            int y = (r.height - DX) / 2;\r
-            int c[] = { 0, y, DX, y + DX, 2 * DX, y };\r
-            gc.fillPolygon(c);\r
-        }\r
-    }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2009, 2013 Ericsson.
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Alvaro Sanchez-Leon - Initial API and implementation
+ *   Patrick Tasse - Refactoring
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.dialogs;
+
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.jface.dialogs.TitleAreaDialog;
+import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.resource.LocalResourceManager;
+import org.eclipse.linuxtools.internal.tmf.ui.Messages;
+import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider;
+import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.StateItem;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.ScrolledComposite;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.GC;
+import org.eclipse.swt.graphics.RGB;
+import org.eclipse.swt.graphics.Rectangle;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Canvas;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Group;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Listener;
+import org.eclipse.swt.widgets.Shell;
+
+/**
+ * Legend for the colors used in the time graph view
+ *
+ * @version 1.0
+ * @author Alvaro Sanchez-Leon
+ * @author Patrick Tasse
+ */
+public class TimeGraphLegend extends TitleAreaDialog {
+
+    private final ITimeGraphPresentationProvider provider;
+    private final LocalResourceManager fResourceManager = new LocalResourceManager(JFaceResources.getResources());
+
+    /**
+     * Open the time graph legend window
+     *
+     * @param parent
+     *            The parent shell
+     * @param provider
+     *            The presentation provider
+     */
+    public static void open(Shell parent, ITimeGraphPresentationProvider provider) {
+        (new TimeGraphLegend(parent, provider)).open();
+    }
+
+    /**
+     * Standard constructor
+     *
+     * @param parent
+     *            The parent shell
+     * @param provider
+     *            The presentation provider
+     */
+    public TimeGraphLegend(Shell parent, ITimeGraphPresentationProvider provider) {
+        super(parent);
+        this.provider = provider;
+        this.setShellStyle(getShellStyle() | SWT.RESIZE);
+    }
+
+    @Override
+    protected Control createDialogArea(Composite parent) {
+        Composite dlgArea = (Composite) super.createDialogArea(parent);
+        Composite composite = new Composite(dlgArea, SWT.NONE);
+
+        GridLayout layout = new GridLayout();
+        composite.setLayout(layout);
+        GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
+        composite.setLayoutData(gd);
+
+        createStatesGroup(composite);
+
+        setTitle(Messages.TmfTimeLegend_LEGEND);
+        setDialogHelpAvailable(false);
+        setHelpAvailable(false);
+
+        return composite;
+    }
+
+    private void createStatesGroup(Composite composite) {
+        ScrolledComposite sc = new ScrolledComposite(composite, SWT.V_SCROLL|SWT.H_SCROLL);
+        sc.setExpandHorizontal(true);
+        sc.setExpandVertical(true);
+        Group gs = new Group(sc, SWT.H_SCROLL);
+        sc.setContent(gs);
+        GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
+        sc.setLayoutData(gd);
+
+        String stateTypeName = provider.getStateTypeName();
+        StringBuffer buffer = new StringBuffer();
+        if (!stateTypeName.isEmpty()) {
+            buffer.append(stateTypeName);
+            buffer.append(" "); //$NON-NLS-1$
+        }
+        buffer.append(Messages.TmfTimeLegend_StateTypeName);
+        gs.setText(buffer.toString());
+
+        GridLayout layout = new GridLayout();
+        layout.numColumns = 2;
+        layout.marginWidth = 20;
+        layout.marginBottom = 10;
+        gs.setLayout(layout);
+
+        // Go through all the defined pairs of state color and state name and display them.
+        StateItem[] stateItems = provider.getStateTable();
+        for (int i = 0; i < stateItems.length; i++) {
+            //Get the color related to the index
+            RGB rgb = stateItems[i].getStateColor();
+
+            //Get the given name, provided by the interface to the application
+            String stateName = stateItems[i].getStateString();
+
+            // draw color with name
+            Bar bar = new Bar(gs, rgb);
+            gd = new GridData();
+            gd.widthHint = 40;
+            gd.heightHint = 20;
+            gd.verticalIndent = 8;
+            bar.setLayoutData(gd);
+            Label name = new Label(gs, SWT.NONE);
+            name.setText(stateName);
+            gd = new GridData();
+            gd.horizontalIndent = 10;
+            gd.verticalIndent = 8;
+            name.setLayoutData(gd);
+        }
+        sc.setMinSize(gs.computeSize(SWT.DEFAULT, SWT.DEFAULT));
+    }
+
+    @Override
+    protected void configureShell(Shell shell) {
+        super.configureShell(shell);
+        shell.setText(Messages.TmfTimeLegend_TRACE_STATES_TITLE);
+    }
+
+    @Override
+    protected void createButtonsForButtonBar(Composite parent) {
+        createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
+                true);
+    }
+
+    class Bar extends Canvas {
+        private final Color color;
+
+        public Bar(Composite parent, RGB rgb) {
+            super(parent, SWT.NONE);
+
+            color = fResourceManager.createColor(rgb);
+            addListener(SWT.Paint, new Listener() {
+                @Override
+                public void handleEvent(Event event) {
+                    draw(event.gc);
+                }
+            });
+        }
+
+        private void draw(GC gc) {
+            Rectangle r = getClientArea();
+            gc.setBackground(color);
+            gc.fillRectangle(r);
+            gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
+            gc.drawRectangle(0, 0, r.width - 1, r.height - 1);
+        }
+
+        @Override
+        public void dispose() {
+            super.dispose();
+            color.dispose();
+        }
+
+    }
+
+}
This page took 0.028968 seconds and 5 git commands to generate.