--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.ui.contribution;\r
+\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.eclipse.jface.resource.JFaceResources;\r
+import org.eclipse.jface.resource.LocalResourceManager;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.SelectionAdapter;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.layout.FillLayout;\r
+import org.eclipse.swt.widgets.Button;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.simantics.NameLabelMode;\r
+import org.simantics.NameLabelUtil;\r
+import org.simantics.Simantics;\r
+import org.simantics.utils.ui.BundleUtils;\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ */\r
+public class NameLabelButtonTrim extends Composite {\r
+\r
+ LocalResourceManager resourceManager;\r
+\r
+ public NameLabelButtonTrim(Composite parent) {\r
+ super(parent, SWT.NONE);\r
+\r
+ setLayout(new FillLayout());\r
+\r
+ final NameLabelMode initialMode = NameLabelUtil.getNameLabelMode(Simantics.getSession());\r
+\r
+ final Button b = new Button(this, SWT.PUSH);\r
+ this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), b);\r
+\r
+ b.setToolTipText(tooltip(initialMode));\r
+ b.setImage(resourceManager.createImage(getDescriptor(initialMode)));\r
+ b.addSelectionListener(new SelectionAdapter() {\r
+\r
+ NameLabelMode mode = initialMode; \r
+\r
+ @Override\r
+ public void widgetSelected(SelectionEvent e) {\r
+ mode = mode.cycle();\r
+ b.setImage(resourceManager.createImage(getDescriptor(mode)));\r
+ b.setToolTipText(tooltip(mode));\r
+ NameLabelUtil.setNameLabelMode(Simantics.getSession(), mode);\r
+ }\r
+ });\r
+\r
+ }\r
+\r
+ private String tooltip(NameLabelMode currentMode) {\r
+ return "Switch Name/Label Visualization Mode from " + currentMode.getLabel() + " to " + currentMode.cycle().getLabel();\r
+ }\r
+\r
+ private ImageDescriptor getDescriptor(NameLabelMode mode) {\r
+ switch (mode) {\r
+ case NAME: return BundleUtils.getImageDescriptorFromPlugin("org.simantics.ui", "/icons/N.png");\r
+ case LABEL: return BundleUtils.getImageDescriptorFromPlugin("org.simantics.ui", "/icons/L.png");\r
+ case NAME_AND_LABEL: return BundleUtils.getImageDescriptorFromPlugin("org.simantics.ui", "/icons/N+L.png");\r
+ case LABEL_AND_NAME: return BundleUtils.getImageDescriptorFromPlugin("org.simantics.ui", "/icons/L+N.png");\r
+ default: throw new IllegalArgumentException("unrecognized name/label mode: " + mode);\r
+ }\r
+ }\r
+\r
+}
\ No newline at end of file