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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.ui.contribution;
\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
29 * @author Antti Villberg
\r
31 public class NameLabelButtonTrim extends Composite {
\r
33 LocalResourceManager resourceManager;
\r
35 public NameLabelButtonTrim(Composite parent) {
\r
36 super(parent, SWT.NONE);
\r
38 setLayout(new FillLayout());
\r
40 final NameLabelMode initialMode = NameLabelUtil.getNameLabelMode(Simantics.getSession());
\r
42 final Button b = new Button(this, SWT.PUSH);
\r
43 this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), b);
\r
45 b.setToolTipText(tooltip(initialMode));
\r
46 b.setImage(resourceManager.createImage(getDescriptor(initialMode)));
\r
47 b.addSelectionListener(new SelectionAdapter() {
\r
49 NameLabelMode mode = initialMode;
\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
62 private String tooltip(NameLabelMode currentMode) {
\r
63 return "Switch Name/Label Visualization Mode from " + currentMode.getLabel() + " to " + currentMode.cycle().getLabel();
\r
66 private ImageDescriptor getDescriptor(NameLabelMode 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