1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.contribution;
14 import org.eclipse.jface.resource.ImageDescriptor;
15 import org.eclipse.jface.resource.JFaceResources;
16 import org.eclipse.jface.resource.LocalResourceManager;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.layout.FillLayout;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Composite;
23 import org.simantics.NameLabelMode;
24 import org.simantics.NameLabelUtil;
25 import org.simantics.Simantics;
26 import org.simantics.utils.ui.BundleUtils;
29 * @author Antti Villberg
31 public class NameLabelButtonTrim extends Composite {
33 LocalResourceManager resourceManager;
35 public NameLabelButtonTrim(Composite parent) {
36 super(parent, SWT.NONE);
38 setLayout(new FillLayout());
40 final NameLabelMode initialMode = NameLabelUtil.getNameLabelMode(Simantics.getSession());
42 final Button b = new Button(this, SWT.PUSH);
43 this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), b);
45 b.setToolTipText(tooltip(initialMode));
46 b.setImage(resourceManager.createImage(getDescriptor(initialMode)));
47 b.addSelectionListener(new SelectionAdapter() {
49 NameLabelMode mode = initialMode;
52 public void widgetSelected(SelectionEvent e) {
54 b.setImage(resourceManager.createImage(getDescriptor(mode)));
55 b.setToolTipText(tooltip(mode));
56 NameLabelUtil.setNameLabelMode(Simantics.getSession(), mode);
62 private String tooltip(NameLabelMode currentMode) {
63 return "Switch Name/Label Visualization Mode from " + currentMode.getLabel() + " to " + currentMode.cycle().getLabel();
66 private ImageDescriptor getDescriptor(NameLabelMode mode) {
68 case NAME: return BundleUtils.getImageDescriptorFromPlugin("org.simantics.ui", "/icons/N.png");
69 case LABEL: return BundleUtils.getImageDescriptorFromPlugin("org.simantics.ui", "/icons/L.png");
70 case NAME_AND_LABEL: return BundleUtils.getImageDescriptorFromPlugin("org.simantics.ui", "/icons/N+L.png");
71 case LABEL_AND_NAME: return BundleUtils.getImageDescriptorFromPlugin("org.simantics.ui", "/icons/L+N.png");
72 default: throw new IllegalArgumentException("unrecognized name/label mode: " + mode);