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.dialogs;
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;
51 * @author Tuukka Lehtonen
53 public class ActionChooserDialog extends Dialog {
55 private static final String DIALOG = "ActionChooserDialog"; //$NON-NLS-1$
57 private final IAction[] actions;
59 private final String title;
61 private final String description;
63 private IAction chosenAction;
65 private TableViewer viewer;
67 private IDialogSettings dialogBoundsSettings;
69 private ResourceManager resourceManager;
71 public ActionChooserDialog(Shell parent, IPriorityAction[] actions, String title, String description) {
73 this.actions = actions;
75 this.description = description;
77 IDialogSettings settings = Activator.getDefault().getDialogSettings();
78 dialogBoundsSettings = settings.getSection(DIALOG);
79 if (dialogBoundsSettings == null)
80 dialogBoundsSettings = settings.addNewSection(DIALOG);
84 protected IDialogSettings getDialogBoundsSettings() {
85 return dialogBoundsSettings;
89 protected void configureShell(Shell newShell) {
91 newShell.setText(title);
93 newShell.setText("Choose Action");
95 super.configureShell(newShell);
99 protected int getShellStyle() {
100 return super.getShellStyle() | SWT.RESIZE;
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);
113 protected Control createDialogArea(Composite parent) {
114 Composite composite = (Composite) super.createDialogArea(parent);
116 this.resourceManager = new LocalResourceManager(JFaceResources.getResources());
117 composite.addListener(SWT.Dispose, new Listener() {
119 public void handleEvent(Event event) {
120 resourceManager.dispose();
121 resourceManager = null;
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));
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);
138 viewer.addDoubleClickListener(new IDoubleClickListener() {
140 public void doubleClick(DoubleClickEvent event) {
145 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
147 public void selectionChanged(SelectionChangedEvent event) {
148 ActionChooserDialog.this.selectionChanged(event.getSelection());
152 if (actions.length > 0) {
153 viewer.setSelection(new StructuredSelection(actions[0]), true);
156 applyDialogFont(composite);
160 private void selectionChanged(ISelection s) {
161 Button ok = getButton(IDialogConstants.OK_ID);
162 IStructuredSelection iss = (IStructuredSelection) s;
163 if (iss == null || iss.isEmpty()) {
165 ok.setEnabled(false);
175 protected void okPressed() {
176 chosenAction = (IAction) ((IStructuredSelection) viewer.getSelection()).getFirstElement();
180 public IAction getChosenAction() {
184 private final ViewerSorter sorter = new ViewerSorter() {
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();
192 return Integer.MAX_VALUE;
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());
202 class ActionLabelProvider extends LabelProvider {
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);
212 public String getText(Object element) {
213 IPriorityAction a = (IPriorityAction) element;
214 return a.getText() + " (" + a.getPriority() + ")";
215 // return a.getText();