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.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.Comparator;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.jface.dialogs.IDialogSettings;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.jface.resource.JFaceResources;
27 import org.eclipse.jface.resource.LocalResourceManager;
28 import org.eclipse.jface.viewers.ILabelProvider;
29 import org.eclipse.jface.viewers.LabelProvider;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.ui.IMemento;
35 import org.eclipse.ui.dialogs.FilteredItemsSelectionDialog;
36 import org.eclipse.ui.dialogs.SearchPattern;
37 import org.simantics.db.Resource;
38 import org.simantics.db.exception.InvalidResourceReferenceException;
39 import org.simantics.db.service.SerialisationSupport;
40 import org.simantics.ui.SimanticsUI;
41 import org.simantics.ui.internal.Activator;
42 import org.simantics.utils.datastructures.Pair;
44 public abstract class ResourceSelectionDialog3<T> extends FilteredItemsSelectionDialog {
46 private final Map<T, Pair<String, ImageDescriptor>> contentMap;
47 private final String title;
48 private LocalResourceManager resourceManager;
50 class ResourceSelectionHistory extends FilteredItemsSelectionDialog.SelectionHistory {
53 protected Object restoreItemFromMemento(IMemento memento) {
54 // FIXME: somehow create a collective transaction inside which the Graph.getRandomAccessReference should be invoked.
55 // Resource r = SimanticsUI.getSession().getRandomAccessReference(memento.getTextData());
61 protected void storeItemToMemento(Object item, IMemento memento) {
62 if(item instanceof Resource) {
64 SerialisationSupport support = SimanticsUI.getSession().getService(SerialisationSupport.class);
65 memento.putTextData(support.getResourceSerializer().createRandomAccessId((Resource)item));
66 } catch (InvalidResourceReferenceException e) {
74 public ResourceSelectionDialog3(Shell shell, Map<T, Pair<String, ImageDescriptor>> parameter, String title) {
75 this(shell, parameter, title, true);
79 protected Control createContents(Composite parent) {
80 this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), parent);
81 return super.createContents(parent);
84 public ResourceSelectionDialog3(Shell shell, Map<T, Pair<String, ImageDescriptor>> parameter, String title, boolean multi) {
87 this.contentMap = parameter;
90 ILabelProvider labelProvider = new LabelProvider() {
92 public String getText(Object element) {
93 Pair<String, ImageDescriptor> pair = contentMap.get(element);
94 if(pair != null) return pair.first;
98 public Image getImage(Object element) {
99 Pair<String, ImageDescriptor> pair = contentMap.get(element);
100 if(pair != null && pair.second != null) return resourceManager.createImage(pair.second);
105 setListLabelProvider(labelProvider);
106 setDetailsLabelProvider(labelProvider);
108 setSelectionHistory(new ResourceSelectionHistory());
114 protected Control createExtendedContentArea(Composite parent) {
115 // Don't create anything extra at the moment
119 class ResourceSelectionDialogItemsFilter extends FilteredItemsSelectionDialog.ItemsFilter {
121 public ResourceSelectionDialogItemsFilter() {
122 String patternText = getPattern();
123 patternMatcher = new SearchPattern();
124 if(patternText != null && patternText.length() > 0)
125 patternMatcher.setPattern(patternText);
127 patternMatcher.setPattern("*");
131 public boolean isConsistentItem(Object item) {
136 public boolean matchItem(Object item) {
137 return matches(contentMap.get(item).first);
142 protected ItemsFilter createFilter() {
143 return new ResourceSelectionDialogItemsFilter();
147 protected void fillContentProvider(AbstractContentProvider contentProvider,
148 ItemsFilter itemsFilter, IProgressMonitor progressMonitor) throws CoreException {
149 for(T o : contentMap.keySet())
150 contentProvider.add(o, itemsFilter);
151 if (progressMonitor != null)
152 progressMonitor.done();
155 protected abstract IDialogSettings getBaseDialogSettings();
158 protected IDialogSettings getDialogSettings() {
159 IDialogSettings base = getBaseDialogSettings();
160 if (base == null) base = Activator.getDefault().getDialogSettings();
161 IDialogSettings settings = base.getSection(title);
162 if (settings == null)
163 settings = base.addNewSection(title);
168 public String getElementName(Object item) {
169 return contentMap.get(item).first;
173 protected Comparator<?> getItemsComparator() {
174 return new Comparator<Object>() {
177 public int compare(Object o1, Object o2) {
178 return contentMap.get(o1).first.compareToIgnoreCase(contentMap.get(o2).first);
185 protected IStatus validateItem(Object item) {
186 return Status.OK_STATUS;
190 * Made publicly visible.
191 * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#updateStatus(org.eclipse.core.runtime.IStatus)
194 public void updateStatus(IStatus status) {
195 super.updateStatus(status);
198 @SuppressWarnings("unchecked")
199 public Collection<T> getResultT() {
200 Object[] res = getResult();
201 if(res == null) return Collections.emptyList();
202 ArrayList<T> result = new ArrayList<T>();
203 for(Object o : res) {