]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/contribution/NameLabelButtonTrim.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / contribution / NameLabelButtonTrim.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.ui.contribution;
13
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;
27
28 /**
29  * @author Antti Villberg
30  */
31 public class NameLabelButtonTrim extends Composite {
32
33     LocalResourceManager resourceManager;
34
35     public NameLabelButtonTrim(Composite parent) {
36         super(parent, SWT.NONE);
37
38         setLayout(new FillLayout());
39
40         final NameLabelMode initialMode = NameLabelUtil.getNameLabelMode(Simantics.getSession());
41
42         final Button b = new Button(this, SWT.PUSH);
43         this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), b);
44
45         b.setToolTipText(tooltip(initialMode));
46         b.setImage(resourceManager.createImage(getDescriptor(initialMode)));
47         b.addSelectionListener(new SelectionAdapter() {
48
49             NameLabelMode mode = initialMode; 
50
51             @Override
52             public void widgetSelected(SelectionEvent e) {
53                 mode = mode.cycle();
54                 b.setImage(resourceManager.createImage(getDescriptor(mode)));
55                 b.setToolTipText(tooltip(mode));
56                 NameLabelUtil.setNameLabelMode(Simantics.getSession(), mode);
57             }
58         });
59
60     }
61
62     private String tooltip(NameLabelMode currentMode) {
63         return "Switch Name/Label Visualization Mode from " + currentMode.getLabel() + " to " + currentMode.cycle().getLabel();
64     }
65
66     private ImageDescriptor getDescriptor(NameLabelMode mode) {
67         switch (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);
73         }
74     }
75
76 }