]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/dialogs/ActionChooserDialog.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / dialogs / ActionChooserDialog.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.dialogs;
13
14 import org.eclipse.jface.action.IAction;
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.jface.dialogs.IDialogSettings;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.resource.JFaceResources;
20 import org.eclipse.jface.resource.LocalResourceManager;
21 import org.eclipse.jface.resource.ResourceManager;
22 import org.eclipse.jface.viewers.ArrayContentProvider;
23 import org.eclipse.jface.viewers.DoubleClickEvent;
24 import org.eclipse.jface.viewers.IDoubleClickListener;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.ISelectionChangedListener;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.jface.viewers.LabelProvider;
29 import org.eclipse.jface.viewers.SelectionChangedEvent;
30 import org.eclipse.jface.viewers.StructuredSelection;
31 import org.eclipse.jface.viewers.TableViewer;
32 import org.eclipse.jface.viewers.Viewer;
33 import org.eclipse.jface.viewers.ViewerSorter;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.swt.graphics.Point;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.widgets.Button;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Control;
41 import org.eclipse.swt.widgets.Event;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Listener;
44 import org.eclipse.swt.widgets.Shell;
45 import org.simantics.ui.internal.Activator;
46 import org.simantics.utils.strings.AlphanumComparator;
47 import org.simantics.utils.ui.action.IPriorityAction;
48
49
50 /**
51  * @author Tuukka Lehtonen
52  */
53 public class ActionChooserDialog extends Dialog {
54
55     private static final String DIALOG = "ActionChooserDialog"; //$NON-NLS-1$
56
57     private final IAction[]     actions;
58
59     private final String        title;
60
61     private final String        description;
62
63     private IAction             chosenAction;
64
65     private TableViewer         viewer;
66
67     private IDialogSettings     dialogBoundsSettings;
68
69     private ResourceManager     resourceManager;
70
71     public ActionChooserDialog(Shell parent, IPriorityAction[] actions, String title, String description) {
72         super(parent);
73         this.actions = actions;
74         this.title = title;
75         this.description = description;
76
77         IDialogSettings settings = Activator.getDefault().getDialogSettings();
78         dialogBoundsSettings = settings.getSection(DIALOG);
79         if (dialogBoundsSettings == null)
80             dialogBoundsSettings = settings.addNewSection(DIALOG);
81     }
82
83     @Override
84     protected IDialogSettings getDialogBoundsSettings() {
85         return dialogBoundsSettings;
86     }
87
88     @Override
89     protected void configureShell(Shell newShell) {
90         if (title != null) {
91             newShell.setText(title);
92         } else {
93             newShell.setText("Choose Action");
94         }
95         super.configureShell(newShell);
96     }
97
98     @Override
99     protected int getShellStyle() {
100         return super.getShellStyle() | SWT.RESIZE;
101     }
102
103     @Override
104     protected Point getInitialSize() {
105         Point defaultSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
106         Point result = super.getInitialSize();
107         if (defaultSize.equals(result))
108             return new Point(300, 300);
109         return result;
110     }
111
112     @Override
113     protected Control createDialogArea(Composite parent) {
114         Composite composite = (Composite) super.createDialogArea(parent);
115
116         this.resourceManager = new LocalResourceManager(JFaceResources.getResources());
117         composite.addListener(SWT.Dispose, new Listener() {
118             @Override
119             public void handleEvent(Event event) {
120                 resourceManager.dispose();
121                 resourceManager = null;
122             }
123         });
124
125         if (description != null) {
126             Label label = new Label(composite, 0);
127             label.setText(description);
128             label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
129         }
130
131         viewer = new TableViewer(composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.SINGLE);
132         viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
133         viewer.setContentProvider(new ArrayContentProvider());
134         viewer.setLabelProvider(new ActionLabelProvider());
135         viewer.setSorter(sorter);
136         viewer.setInput(actions);
137
138         viewer.addDoubleClickListener(new IDoubleClickListener() {
139             @Override
140             public void doubleClick(DoubleClickEvent event) {
141                 okPressed();
142             }
143         });
144
145         viewer.addSelectionChangedListener(new ISelectionChangedListener() {
146             @Override
147             public void selectionChanged(SelectionChangedEvent event) {
148                 ActionChooserDialog.this.selectionChanged(event.getSelection());
149             }
150         });
151
152         if (actions.length > 0) {
153             viewer.setSelection(new StructuredSelection(actions[0]), true);
154         }
155
156         applyDialogFont(composite);
157         return composite;
158     }
159
160     private void selectionChanged(ISelection s) {
161         Button ok = getButton(IDialogConstants.OK_ID);
162         IStructuredSelection iss = (IStructuredSelection) s;
163         if (iss == null || iss.isEmpty()) {
164             if (ok != null)
165                 ok.setEnabled(false);
166             return;
167         }
168
169         if (ok != null)
170             ok.setEnabled(true);
171         return;
172     }
173
174     @Override
175     protected void okPressed() {
176         chosenAction = (IAction) ((IStructuredSelection) viewer.getSelection()).getFirstElement();
177         super.okPressed();
178     }
179
180     public IAction getChosenAction() {
181         return chosenAction;
182     }
183
184     private final ViewerSorter sorter = new ViewerSorter() {
185         @Override
186         public int category(Object element) {
187             // Sort actions in descending priority order
188             if (element instanceof IPriorityAction) {
189                 IPriorityAction action = (IPriorityAction) element;
190                 return -action.getPriority();
191             }
192             return Integer.MAX_VALUE;
193         }
194         @Override
195         public int compare(Viewer viewer, Object e1, Object e2) {
196             IPriorityAction a1 = (IPriorityAction) e1;
197             IPriorityAction a2 = (IPriorityAction) e2;
198             return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(a1.getText(), a2.getText());
199         }
200     };
201
202     class ActionLabelProvider extends LabelProvider {
203         @Override
204         public Image getImage(Object element) {
205             IAction a = (IAction) element;
206             ImageDescriptor desc = a.getImageDescriptor();
207             if(desc == null) return null;
208             else return resourceManager.createImage(desc);
209         }
210
211         @Override
212         public String getText(Object element) {
213             IPriorityAction a = (IPriorityAction) element;
214             return a.getText() + " (" + a.getPriority() + ")";
215 //            return a.getText();
216         }
217     }
218
219 }