]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/dialogs/ResourceSelectionDialog3.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / dialogs / ResourceSelectionDialog3.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.ui.workbench.dialogs;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.Comparator;
18 import java.util.Map;
19
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.Simantics;
38 import org.simantics.db.Resource;
39 import org.simantics.db.exception.InvalidResourceReferenceException;
40 import org.simantics.db.service.SerialisationSupport;
41 import org.simantics.ui.internal.Activator;
42 import org.simantics.utils.datastructures.Pair;
43
44 public abstract class ResourceSelectionDialog3<T> extends FilteredItemsSelectionDialog {
45
46     private final Map<T, Pair<String, ImageDescriptor>> contentMap;
47     private final String title;
48     private LocalResourceManager resourceManager;
49
50     class ResourceSelectionHistory extends FilteredItemsSelectionDialog.SelectionHistory {
51
52         @Override
53         protected Object restoreItemFromMemento(IMemento memento) {
54             // FIXME: somehow create a collective transaction inside which the Graph.getRandomAccessReference should be invoked.
55 //            Resource r = Simantics.getSession().getRandomAccessReference(memento.getTextData());
56 //            return r;
57             return null;
58         }
59
60         @Override
61         protected void storeItemToMemento(Object item, IMemento memento) {
62             if(item instanceof Resource) {
63                 try {
64                     SerialisationSupport support = Simantics.getSession().getService(SerialisationSupport.class);
65                     memento.putTextData(support.getResourceSerializer().createRandomAccessId((Resource)item));
66                 } catch (InvalidResourceReferenceException e) {
67                     e.printStackTrace();
68                 }
69             }
70         }
71         
72     };
73
74     public ResourceSelectionDialog3(Shell shell, Map<T, Pair<String, ImageDescriptor>> parameter, String title) {
75         this(shell, parameter, title, true);
76     }
77
78     @Override
79     protected Control createContents(Composite parent) {
80         this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), parent);
81         return super.createContents(parent);
82     }
83     
84     public ResourceSelectionDialog3(Shell shell, Map<T, Pair<String, ImageDescriptor>> parameter, String title, boolean multi) {
85         
86         super(shell, multi);
87         this.contentMap = parameter;
88         this.title = title;
89
90         ILabelProvider labelProvider = new LabelProvider() {
91             @Override
92             public String getText(Object element) {
93                 Pair<String, ImageDescriptor> pair = contentMap.get(element);
94                 if(pair != null) return pair.first;
95                 else return null;
96             }
97             @Override
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);
101                 else return null;
102             }
103         }; 
104
105         setListLabelProvider(labelProvider);
106         setDetailsLabelProvider(labelProvider);
107
108         setSelectionHistory(new ResourceSelectionHistory());
109         setTitle(title);
110         
111     }
112
113     @Override
114     protected Control createExtendedContentArea(Composite parent) {
115         // Don't create anything extra at the moment
116         return null;
117     }
118
119     class ResourceSelectionDialogItemsFilter extends FilteredItemsSelectionDialog.ItemsFilter {
120         
121         public ResourceSelectionDialogItemsFilter() {
122             String patternText = getPattern();
123             patternMatcher = new SearchPattern();
124             if(patternText != null && patternText.length() > 0)
125                 patternMatcher.setPattern(patternText);
126             else 
127                 patternMatcher.setPattern("*");
128         }
129
130         @Override
131         public boolean isConsistentItem(Object item) {
132             return true;
133         }
134
135         @Override
136         public boolean matchItem(Object item) {
137             return matches(contentMap.get(item).first);
138         }
139     }
140
141     @Override
142     protected ItemsFilter createFilter() {
143         return new ResourceSelectionDialogItemsFilter();
144     }
145
146     @Override
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();
153     }
154
155     protected abstract IDialogSettings getBaseDialogSettings();
156
157     @Override
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);
164         return settings;
165     }
166
167     @Override
168     public String getElementName(Object item) {
169         return contentMap.get(item).first;
170     }
171
172     @Override
173     protected Comparator<?> getItemsComparator() {
174         return new Comparator<Object>() {
175
176             @Override
177             public int compare(Object o1, Object o2) {
178                 return contentMap.get(o1).first.compareToIgnoreCase(contentMap.get(o2).first);
179             }
180             
181         };
182     }
183
184     @Override
185     protected IStatus validateItem(Object item) {
186         return Status.OK_STATUS;
187     }
188
189     /**
190      * Made publicly visible.
191      * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#updateStatus(org.eclipse.core.runtime.IStatus)
192      */
193     @Override
194     public void updateStatus(IStatus status) {
195         super.updateStatus(status);
196     }
197
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) {
204                 result.add((T)o);
205         }
206         return result;
207     }
208     
209 }