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.workbench.dialogs;
14 import java.util.Comparator;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.jface.dialogs.IDialogSettings;
22 import org.eclipse.jface.viewers.ILabelProvider;
23 import org.eclipse.jface.viewers.LabelProvider;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.ui.IMemento;
28 import org.eclipse.ui.dialogs.FilteredItemsSelectionDialog;
29 import org.eclipse.ui.dialogs.SearchPattern;
30 import org.simantics.db.Resource;
31 import org.simantics.db.exception.InvalidResourceReferenceException;
32 import org.simantics.db.service.SerialisationSupport;
33 import org.simantics.ui.SimanticsUI;
35 public abstract class ResourceSelectionDialog<T> extends FilteredItemsSelectionDialog {
37 Map<T, String> nameMap;
40 class ResourceSelectionHistory extends FilteredItemsSelectionDialog.SelectionHistory {
43 protected Object restoreItemFromMemento(IMemento memento) {
44 // FIXME: somehow create a collective transaction inside which the Graph.getRandomAccessReference should be invoked.
45 // Resource r = SimanticsUI.getSession().getRandomAccessReference(memento.getTextData());
51 protected void storeItemToMemento(Object item, IMemento memento) {
52 if(item instanceof Resource) {
54 SerialisationSupport support = SimanticsUI.getSession().getService(SerialisationSupport.class);
55 memento.putTextData(support.getResourceSerializer().createRandomAccessId((Resource)item));
56 } catch (InvalidResourceReferenceException e) {
64 public ResourceSelectionDialog(Shell shell, Map<T, String> parameter, String title) {
66 this.nameMap = parameter;
69 ILabelProvider labelProvider = new LabelProvider() {
71 public String getText(Object element) {
72 return nameMap.get(element);
76 setListLabelProvider(labelProvider);
77 setDetailsLabelProvider(labelProvider);
79 setSelectionHistory(new ResourceSelectionHistory());
85 protected Control createExtendedContentArea(Composite parent) {
86 // Don't create anything extra at the moment
90 class ResourceSelectionDialogItemsFilter extends FilteredItemsSelectionDialog.ItemsFilter {
92 public ResourceSelectionDialogItemsFilter() {
93 String patternText = getPattern();
94 patternMatcher = new SearchPattern();
95 if(patternText != null && patternText.length() > 0)
96 patternMatcher.setPattern(patternText);
98 patternMatcher.setPattern("*");
102 public boolean isConsistentItem(Object item) {
107 public boolean matchItem(Object item) {
108 return matches(nameMap.get(item));
113 protected ItemsFilter createFilter() {
114 return new ResourceSelectionDialogItemsFilter();
118 protected void fillContentProvider(AbstractContentProvider contentProvider,
119 ItemsFilter itemsFilter, IProgressMonitor progressMonitor) throws CoreException {
120 for(T o : nameMap.keySet())
121 contentProvider.add(o, itemsFilter);
122 if (progressMonitor != null)
123 progressMonitor.done();
126 protected abstract IDialogSettings getBaseDialogSettings();
129 protected IDialogSettings getDialogSettings() {
130 IDialogSettings base = getBaseDialogSettings();
131 IDialogSettings settings = base.getSection(title);
132 if (settings == null)
133 settings = base.addNewSection(title);
138 public String getElementName(Object item) {
139 return nameMap.get(item);
143 protected Comparator<?> getItemsComparator() {
144 return new Comparator<Object>() {
147 public int compare(Object o1, Object o2) {
148 return nameMap.get(o1).compareToIgnoreCase(nameMap.get(o2));
155 protected IStatus validateItem(Object item) {
156 return Status.OK_STATUS;