]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/contribution/NameLabelButtonTrim.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / contribution / NameLabelButtonTrim.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.ui.contribution;\r
13 \r
14 import org.eclipse.jface.resource.ImageDescriptor;\r
15 import org.eclipse.jface.resource.JFaceResources;\r
16 import org.eclipse.jface.resource.LocalResourceManager;\r
17 import org.eclipse.swt.SWT;\r
18 import org.eclipse.swt.events.SelectionAdapter;\r
19 import org.eclipse.swt.events.SelectionEvent;\r
20 import org.eclipse.swt.layout.FillLayout;\r
21 import org.eclipse.swt.widgets.Button;\r
22 import org.eclipse.swt.widgets.Composite;\r
23 import org.simantics.NameLabelMode;\r
24 import org.simantics.NameLabelUtil;\r
25 import org.simantics.Simantics;\r
26 import org.simantics.utils.ui.BundleUtils;\r
27 \r
28 /**\r
29  * @author Antti Villberg\r
30  */\r
31 public class NameLabelButtonTrim extends Composite {\r
32 \r
33     LocalResourceManager resourceManager;\r
34 \r
35     public NameLabelButtonTrim(Composite parent) {\r
36         super(parent, SWT.NONE);\r
37 \r
38         setLayout(new FillLayout());\r
39 \r
40         final NameLabelMode initialMode = NameLabelUtil.getNameLabelMode(Simantics.getSession());\r
41 \r
42         final Button b = new Button(this, SWT.PUSH);\r
43         this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), b);\r
44 \r
45         b.setToolTipText(tooltip(initialMode));\r
46         b.setImage(resourceManager.createImage(getDescriptor(initialMode)));\r
47         b.addSelectionListener(new SelectionAdapter() {\r
48 \r
49             NameLabelMode mode = initialMode; \r
50 \r
51             @Override\r
52             public void widgetSelected(SelectionEvent e) {\r
53                 mode = mode.cycle();\r
54                 b.setImage(resourceManager.createImage(getDescriptor(mode)));\r
55                 b.setToolTipText(tooltip(mode));\r
56                 NameLabelUtil.setNameLabelMode(Simantics.getSession(), mode);\r
57             }\r
58         });\r
59 \r
60     }\r
61 \r
62     private String tooltip(NameLabelMode currentMode) {\r
63         return "Switch Name/Label Visualization Mode from " + currentMode.getLabel() + " to " + currentMode.cycle().getLabel();\r
64     }\r
65 \r
66     private ImageDescriptor getDescriptor(NameLabelMode mode) {\r
67         switch (mode) {\r
68         case NAME: return BundleUtils.getImageDescriptorFromPlugin("org.simantics.ui", "/icons/N.png");\r
69         case LABEL: return BundleUtils.getImageDescriptorFromPlugin("org.simantics.ui", "/icons/L.png");\r
70         case NAME_AND_LABEL: return BundleUtils.getImageDescriptorFromPlugin("org.simantics.ui", "/icons/N+L.png");\r
71         case LABEL_AND_NAME: return BundleUtils.getImageDescriptorFromPlugin("org.simantics.ui", "/icons/L+N.png");\r
72         default: throw new IllegalArgumentException("unrecognized name/label mode: " + mode);\r
73         }\r
74     }\r
75 \r
76 }