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.modeling.actions;
14 import org.eclipse.jface.dialogs.Dialog;
15 import org.eclipse.jface.dialogs.IDialogConstants;
16 import org.eclipse.jface.dialogs.IDialogSettings;
17 import org.eclipse.jface.viewers.ArrayContentProvider;
18 import org.eclipse.jface.viewers.DoubleClickEvent;
19 import org.eclipse.jface.viewers.IDoubleClickListener;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.ISelectionChangedListener;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.LabelProvider;
24 import org.eclipse.jface.viewers.SelectionChangedEvent;
25 import org.eclipse.jface.viewers.StructuredSelection;
26 import org.eclipse.jface.viewers.TableViewer;
27 import org.eclipse.jface.viewers.Viewer;
28 import org.eclipse.jface.viewers.ViewerSorter;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.graphics.Point;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Label;
37 import org.eclipse.swt.widgets.Shell;
38 import org.simantics.db.common.NamedResource;
39 import org.simantics.ui.internal.Activator;
40 import org.simantics.utils.strings.AlphanumComparator;
44 * @author Tuukka Lehtonen
46 public class NavigationTargetChooserDialog extends Dialog {
48 private static final String DIALOG = "NavigationTargetChooserDialog"; //$NON-NLS-1$
50 private final NamedResource[] options;
52 private final String title;
54 private final String description;
56 private NamedResource selected;
58 private TableViewer viewer;
60 private IDialogSettings dialogBoundsSettings;
62 public NavigationTargetChooserDialog(Shell parent, NamedResource[] options, String title, String description) {
64 this.options = options;
66 this.description = description;
68 IDialogSettings settings = Activator.getDefault().getDialogSettings();
69 dialogBoundsSettings = settings.getSection(DIALOG);
70 if (dialogBoundsSettings == null)
71 dialogBoundsSettings = settings.addNewSection(DIALOG);
75 protected IDialogSettings getDialogBoundsSettings() {
76 return dialogBoundsSettings;
80 protected void configureShell(Shell newShell) {
82 newShell.setText(title);
84 newShell.setText("Choose Navigation Target");
86 super.configureShell(newShell);
90 protected int getShellStyle() {
91 return super.getShellStyle() | SWT.RESIZE;
95 protected Point getInitialSize() {
96 Point defaultSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
97 Point result = super.getInitialSize();
98 if (defaultSize.equals(result))
99 return new Point(400, 500);
104 protected Control createDialogArea(Composite parent) {
105 Composite composite = (Composite) super.createDialogArea(parent);
107 if (description != null) {
108 Label label = new Label(composite, 0);
109 label.setText(description);
110 label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
113 viewer = new TableViewer(composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.SINGLE);
114 viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
115 viewer.setContentProvider(new ArrayContentProvider());
116 viewer.setLabelProvider(new TargetLabelProvider());
117 viewer.setSorter(sorter);
118 viewer.setInput(options);
120 viewer.addDoubleClickListener(new IDoubleClickListener() {
122 public void doubleClick(DoubleClickEvent event) {
127 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
129 public void selectionChanged(SelectionChangedEvent event) {
130 NavigationTargetChooserDialog.this.selectionChanged(event.getSelection());
134 if (options.length > 0) {
135 viewer.setSelection(new StructuredSelection(options[0]), true);
138 applyDialogFont(composite);
142 private void selectionChanged(ISelection s) {
143 Button ok = getButton(IDialogConstants.OK_ID);
144 IStructuredSelection iss = (IStructuredSelection) s;
145 if (iss == null || iss.isEmpty()) {
147 ok.setEnabled(false);
157 protected void okPressed() {
158 selected = (NamedResource) ((IStructuredSelection) viewer.getSelection()).getFirstElement();
162 public NamedResource getSelection() {
166 private final ViewerSorter sorter = new ViewerSorter() {
168 public int category(Object element) {
172 public int compare(Viewer viewer, Object e1, Object e2) {
173 NamedResource a1 = (NamedResource) e1;
174 NamedResource a2 = (NamedResource) e2;
175 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(a1.getName(), a2.getName());
179 class TargetLabelProvider extends LabelProvider {
181 public Image getImage(Object element) {
186 public String getText(Object element) {
187 NamedResource a = (NamedResource) element;