-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.ui.dialogs;\r
-\r
-import org.eclipse.jface.action.IAction;\r
-import org.eclipse.jface.dialogs.Dialog;\r
-import org.eclipse.jface.dialogs.IDialogConstants;\r
-import org.eclipse.jface.dialogs.IDialogSettings;\r
-import org.eclipse.jface.resource.ImageDescriptor;\r
-import org.eclipse.jface.resource.JFaceResources;\r
-import org.eclipse.jface.resource.LocalResourceManager;\r
-import org.eclipse.jface.resource.ResourceManager;\r
-import org.eclipse.jface.viewers.ArrayContentProvider;\r
-import org.eclipse.jface.viewers.DoubleClickEvent;\r
-import org.eclipse.jface.viewers.IDoubleClickListener;\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.eclipse.jface.viewers.ISelectionChangedListener;\r
-import org.eclipse.jface.viewers.IStructuredSelection;\r
-import org.eclipse.jface.viewers.LabelProvider;\r
-import org.eclipse.jface.viewers.SelectionChangedEvent;\r
-import org.eclipse.jface.viewers.StructuredSelection;\r
-import org.eclipse.jface.viewers.TableViewer;\r
-import org.eclipse.jface.viewers.Viewer;\r
-import org.eclipse.jface.viewers.ViewerSorter;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.graphics.Image;\r
-import org.eclipse.swt.graphics.Point;\r
-import org.eclipse.swt.layout.GridData;\r
-import org.eclipse.swt.widgets.Button;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Control;\r
-import org.eclipse.swt.widgets.Event;\r
-import org.eclipse.swt.widgets.Label;\r
-import org.eclipse.swt.widgets.Listener;\r
-import org.eclipse.swt.widgets.Shell;\r
-import org.simantics.ui.internal.Activator;\r
-import org.simantics.utils.strings.AlphanumComparator;\r
-import org.simantics.utils.ui.action.IPriorityAction;\r
-\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class ActionChooserDialog extends Dialog {\r
-\r
- private static final String DIALOG = "ActionChooserDialog"; //$NON-NLS-1$\r
-\r
- private final IAction[] actions;\r
-\r
- private final String title;\r
-\r
- private final String description;\r
-\r
- private IAction chosenAction;\r
-\r
- private TableViewer viewer;\r
-\r
- private IDialogSettings dialogBoundsSettings;\r
-\r
- private ResourceManager resourceManager;\r
-\r
- public ActionChooserDialog(Shell parent, IPriorityAction[] actions, String title, String description) {\r
- super(parent);\r
- this.actions = actions;\r
- this.title = title;\r
- this.description = description;\r
-\r
- IDialogSettings settings = Activator.getDefault().getDialogSettings();\r
- dialogBoundsSettings = settings.getSection(DIALOG);\r
- if (dialogBoundsSettings == null)\r
- dialogBoundsSettings = settings.addNewSection(DIALOG);\r
- }\r
-\r
- @Override\r
- protected IDialogSettings getDialogBoundsSettings() {\r
- return dialogBoundsSettings;\r
- }\r
-\r
- @Override\r
- protected void configureShell(Shell newShell) {\r
- if (title != null) {\r
- newShell.setText(title);\r
- } else {\r
- newShell.setText("Choose Action");\r
- }\r
- super.configureShell(newShell);\r
- }\r
-\r
- @Override\r
- protected int getShellStyle() {\r
- return super.getShellStyle() | SWT.RESIZE;\r
- }\r
-\r
- @Override\r
- protected Point getInitialSize() {\r
- Point defaultSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT, true);\r
- Point result = super.getInitialSize();\r
- if (defaultSize.equals(result))\r
- return new Point(300, 300);\r
- return result;\r
- }\r
-\r
- @Override\r
- protected Control createDialogArea(Composite parent) {\r
- Composite composite = (Composite) super.createDialogArea(parent);\r
-\r
- this.resourceManager = new LocalResourceManager(JFaceResources.getResources());\r
- composite.addListener(SWT.Dispose, new Listener() {\r
- @Override\r
- public void handleEvent(Event event) {\r
- resourceManager.dispose();\r
- resourceManager = null;\r
- }\r
- });\r
-\r
- if (description != null) {\r
- Label label = new Label(composite, 0);\r
- label.setText(description);\r
- label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));\r
- }\r
-\r
- viewer = new TableViewer(composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.SINGLE);\r
- viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));\r
- viewer.setContentProvider(new ArrayContentProvider());\r
- viewer.setLabelProvider(new ActionLabelProvider());\r
- viewer.setSorter(sorter);\r
- viewer.setInput(actions);\r
-\r
- viewer.addDoubleClickListener(new IDoubleClickListener() {\r
- @Override\r
- public void doubleClick(DoubleClickEvent event) {\r
- okPressed();\r
- }\r
- });\r
-\r
- viewer.addSelectionChangedListener(new ISelectionChangedListener() {\r
- @Override\r
- public void selectionChanged(SelectionChangedEvent event) {\r
- ActionChooserDialog.this.selectionChanged(event.getSelection());\r
- }\r
- });\r
-\r
- if (actions.length > 0) {\r
- viewer.setSelection(new StructuredSelection(actions[0]), true);\r
- }\r
-\r
- applyDialogFont(composite);\r
- return composite;\r
- }\r
-\r
- private void selectionChanged(ISelection s) {\r
- Button ok = getButton(IDialogConstants.OK_ID);\r
- IStructuredSelection iss = (IStructuredSelection) s;\r
- if (iss == null || iss.isEmpty()) {\r
- if (ok != null)\r
- ok.setEnabled(false);\r
- return;\r
- }\r
-\r
- if (ok != null)\r
- ok.setEnabled(true);\r
- return;\r
- }\r
-\r
- @Override\r
- protected void okPressed() {\r
- chosenAction = (IAction) ((IStructuredSelection) viewer.getSelection()).getFirstElement();\r
- super.okPressed();\r
- }\r
-\r
- public IAction getChosenAction() {\r
- return chosenAction;\r
- }\r
-\r
- private final ViewerSorter sorter = new ViewerSorter() {\r
- @Override\r
- public int category(Object element) {\r
- // Sort actions in descending priority order\r
- if (element instanceof IPriorityAction) {\r
- IPriorityAction action = (IPriorityAction) element;\r
- return -action.getPriority();\r
- }\r
- return Integer.MAX_VALUE;\r
- }\r
- @Override\r
- public int compare(Viewer viewer, Object e1, Object e2) {\r
- IPriorityAction a1 = (IPriorityAction) e1;\r
- IPriorityAction a2 = (IPriorityAction) e2;\r
- return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(a1.getText(), a2.getText());\r
- }\r
- };\r
-\r
- class ActionLabelProvider extends LabelProvider {\r
- @Override\r
- public Image getImage(Object element) {\r
- IAction a = (IAction) element;\r
- ImageDescriptor desc = a.getImageDescriptor();\r
- if(desc == null) return null;\r
- else return resourceManager.createImage(desc);\r
- }\r
-\r
- @Override\r
- public String getText(Object element) {\r
- IPriorityAction a = (IPriorityAction) element;\r
- return a.getText() + " (" + a.getPriority() + ")";\r
-// return a.getText();\r
- }\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.ui.dialogs;
+
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.dialogs.Dialog;
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.jface.dialogs.IDialogSettings;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.resource.LocalResourceManager;
+import org.eclipse.jface.resource.ResourceManager;
+import org.eclipse.jface.viewers.ArrayContentProvider;
+import org.eclipse.jface.viewers.DoubleClickEvent;
+import org.eclipse.jface.viewers.IDoubleClickListener;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.jface.viewers.ViewerSorter;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Listener;
+import org.eclipse.swt.widgets.Shell;
+import org.simantics.ui.internal.Activator;
+import org.simantics.utils.strings.AlphanumComparator;
+import org.simantics.utils.ui.action.IPriorityAction;
+
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class ActionChooserDialog extends Dialog {
+
+ private static final String DIALOG = "ActionChooserDialog"; //$NON-NLS-1$
+
+ private final IAction[] actions;
+
+ private final String title;
+
+ private final String description;
+
+ private IAction chosenAction;
+
+ private TableViewer viewer;
+
+ private IDialogSettings dialogBoundsSettings;
+
+ private ResourceManager resourceManager;
+
+ public ActionChooserDialog(Shell parent, IPriorityAction[] actions, String title, String description) {
+ super(parent);
+ this.actions = actions;
+ this.title = title;
+ this.description = description;
+
+ IDialogSettings settings = Activator.getDefault().getDialogSettings();
+ dialogBoundsSettings = settings.getSection(DIALOG);
+ if (dialogBoundsSettings == null)
+ dialogBoundsSettings = settings.addNewSection(DIALOG);
+ }
+
+ @Override
+ protected IDialogSettings getDialogBoundsSettings() {
+ return dialogBoundsSettings;
+ }
+
+ @Override
+ protected void configureShell(Shell newShell) {
+ if (title != null) {
+ newShell.setText(title);
+ } else {
+ newShell.setText("Choose Action");
+ }
+ super.configureShell(newShell);
+ }
+
+ @Override
+ protected int getShellStyle() {
+ return super.getShellStyle() | SWT.RESIZE;
+ }
+
+ @Override
+ protected Point getInitialSize() {
+ Point defaultSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
+ Point result = super.getInitialSize();
+ if (defaultSize.equals(result))
+ return new Point(300, 300);
+ return result;
+ }
+
+ @Override
+ protected Control createDialogArea(Composite parent) {
+ Composite composite = (Composite) super.createDialogArea(parent);
+
+ this.resourceManager = new LocalResourceManager(JFaceResources.getResources());
+ composite.addListener(SWT.Dispose, new Listener() {
+ @Override
+ public void handleEvent(Event event) {
+ resourceManager.dispose();
+ resourceManager = null;
+ }
+ });
+
+ if (description != null) {
+ Label label = new Label(composite, 0);
+ label.setText(description);
+ label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
+ }
+
+ viewer = new TableViewer(composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.SINGLE);
+ viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+ viewer.setContentProvider(new ArrayContentProvider());
+ viewer.setLabelProvider(new ActionLabelProvider());
+ viewer.setSorter(sorter);
+ viewer.setInput(actions);
+
+ viewer.addDoubleClickListener(new IDoubleClickListener() {
+ @Override
+ public void doubleClick(DoubleClickEvent event) {
+ okPressed();
+ }
+ });
+
+ viewer.addSelectionChangedListener(new ISelectionChangedListener() {
+ @Override
+ public void selectionChanged(SelectionChangedEvent event) {
+ ActionChooserDialog.this.selectionChanged(event.getSelection());
+ }
+ });
+
+ if (actions.length > 0) {
+ viewer.setSelection(new StructuredSelection(actions[0]), true);
+ }
+
+ applyDialogFont(composite);
+ return composite;
+ }
+
+ private void selectionChanged(ISelection s) {
+ Button ok = getButton(IDialogConstants.OK_ID);
+ IStructuredSelection iss = (IStructuredSelection) s;
+ if (iss == null || iss.isEmpty()) {
+ if (ok != null)
+ ok.setEnabled(false);
+ return;
+ }
+
+ if (ok != null)
+ ok.setEnabled(true);
+ return;
+ }
+
+ @Override
+ protected void okPressed() {
+ chosenAction = (IAction) ((IStructuredSelection) viewer.getSelection()).getFirstElement();
+ super.okPressed();
+ }
+
+ public IAction getChosenAction() {
+ return chosenAction;
+ }
+
+ private final ViewerSorter sorter = new ViewerSorter() {
+ @Override
+ public int category(Object element) {
+ // Sort actions in descending priority order
+ if (element instanceof IPriorityAction) {
+ IPriorityAction action = (IPriorityAction) element;
+ return -action.getPriority();
+ }
+ return Integer.MAX_VALUE;
+ }
+ @Override
+ public int compare(Viewer viewer, Object e1, Object e2) {
+ IPriorityAction a1 = (IPriorityAction) e1;
+ IPriorityAction a2 = (IPriorityAction) e2;
+ return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(a1.getText(), a2.getText());
+ }
+ };
+
+ class ActionLabelProvider extends LabelProvider {
+ @Override
+ public Image getImage(Object element) {
+ IAction a = (IAction) element;
+ ImageDescriptor desc = a.getImageDescriptor();
+ if(desc == null) return null;
+ else return resourceManager.createImage(desc);
+ }
+
+ @Override
+ public String getText(Object element) {
+ IPriorityAction a = (IPriorityAction) element;
+ return a.getText() + " (" + a.getPriority() + ")";
+// return a.getText();
+ }
+ }
+
+}