]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/actions/NavigationTargetChooserDialog.java
Copy URI context menu action to Model Browser for development mode
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / actions / NavigationTargetChooserDialog.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.modeling.actions;
13
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;
41
42
43 /**
44  * @author Tuukka Lehtonen
45  */
46 public class NavigationTargetChooserDialog extends Dialog {
47
48     private static final String DIALOG = "NavigationTargetChooserDialog"; //$NON-NLS-1$
49
50     private final NamedResource[] options;
51
52     private final String          title;
53
54     private final String          description;
55
56     private NamedResource         selected;
57
58     private TableViewer           viewer;
59
60     private IDialogSettings       dialogBoundsSettings;
61
62     public NavigationTargetChooserDialog(Shell parent, NamedResource[] options, String title, String description) {
63         super(parent);
64         this.options = options;
65         this.title = title;
66         this.description = description;
67
68         IDialogSettings settings = Activator.getDefault().getDialogSettings();
69         dialogBoundsSettings = settings.getSection(DIALOG);
70         if (dialogBoundsSettings == null)
71             dialogBoundsSettings = settings.addNewSection(DIALOG);
72     }
73
74     @Override
75     protected IDialogSettings getDialogBoundsSettings() {
76         return dialogBoundsSettings;
77     }
78
79     @Override
80     protected void configureShell(Shell newShell) {
81         if (title != null) {
82             newShell.setText(title);
83         } else {
84             newShell.setText("Choose Navigation Target");
85         }
86         super.configureShell(newShell);
87     }
88
89     @Override
90     protected int getShellStyle() {
91         return super.getShellStyle() | SWT.RESIZE;
92     }
93
94     @Override
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);
100         return result;
101     }
102
103     @Override
104     protected Control createDialogArea(Composite parent) {
105         Composite composite = (Composite) super.createDialogArea(parent);
106
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));
111         }
112
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);
119
120         viewer.addDoubleClickListener(new IDoubleClickListener() {
121             @Override
122             public void doubleClick(DoubleClickEvent event) {
123                 okPressed();
124             }
125         });
126
127         viewer.addSelectionChangedListener(new ISelectionChangedListener() {
128             @Override
129             public void selectionChanged(SelectionChangedEvent event) {
130                 NavigationTargetChooserDialog.this.selectionChanged(event.getSelection());
131             }
132         });
133
134         if (options.length > 0) {
135             viewer.setSelection(new StructuredSelection(options[0]), true);
136         }
137
138         applyDialogFont(composite);
139         return composite;
140     }
141
142     private void selectionChanged(ISelection s) {
143         Button ok = getButton(IDialogConstants.OK_ID);
144         IStructuredSelection iss = (IStructuredSelection) s;
145         if (iss == null || iss.isEmpty()) {
146             if (ok != null)
147                 ok.setEnabled(false);
148             return;
149         }
150
151         if (ok != null)
152             ok.setEnabled(true);
153         return;
154     }
155
156     @Override
157     protected void okPressed() {
158         selected = (NamedResource) ((IStructuredSelection) viewer.getSelection()).getFirstElement();
159         super.okPressed();
160     }
161
162     public NamedResource getSelection() {
163         return selected;
164     }
165
166     private final ViewerSorter sorter = new ViewerSorter() {
167         @Override
168         public int category(Object element) {
169             return 0;
170         }
171         @Override
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());
176         }
177     };
178
179     class TargetLabelProvider extends LabelProvider {
180         @Override
181         public Image getImage(Object element) {
182             return null;
183         }
184
185         @Override
186         public String getText(Object element) {
187             NamedResource a = (NamedResource) element;
188             return a.getName();
189         }
190     }
191
192 }