]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/dialogs/ResourceSelectionDialog.java
Remove usage of deprecated SimanticsUI-methods
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / dialogs / ResourceSelectionDialog.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.Comparator;
15 import java.util.Map;
16
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.Simantics;
31 import org.simantics.db.Resource;
32 import org.simantics.db.exception.InvalidResourceReferenceException;
33 import org.simantics.db.service.SerialisationSupport;
34
35 public abstract class ResourceSelectionDialog<T> extends FilteredItemsSelectionDialog {
36
37     Map<T, String> nameMap;
38     String         title;      
39     
40     class ResourceSelectionHistory extends FilteredItemsSelectionDialog.SelectionHistory {
41
42         @Override
43         protected Object restoreItemFromMemento(IMemento memento) {
44             // FIXME: somehow create a collective transaction inside which the Graph.getRandomAccessReference should be invoked.
45 //            Resource r = Simantics.getSession().getRandomAccessReference(memento.getTextData());
46 //            return r;
47             return null;
48         }
49
50         @Override
51         protected void storeItemToMemento(Object item, IMemento memento) {
52             if(item instanceof Resource) {
53                 try {
54                         SerialisationSupport support = Simantics.getSession().getService(SerialisationSupport.class);
55                     memento.putTextData(support.getResourceSerializer().createRandomAccessId((Resource)item));
56                 } catch (InvalidResourceReferenceException e) {
57                     e.printStackTrace();
58                 }
59             }
60         }
61         
62     };
63     
64     public ResourceSelectionDialog(Shell shell, Map<T, String> parameter, String title) {
65         super(shell, true);
66         this.nameMap = parameter;
67         this.title = title;
68         
69         ILabelProvider labelProvider = new LabelProvider() {
70             @Override
71             public String getText(Object element) {
72                 return nameMap.get(element);
73             }
74         }; 
75                                
76         setListLabelProvider(labelProvider);
77         setDetailsLabelProvider(labelProvider);
78         
79         setSelectionHistory(new ResourceSelectionHistory());
80         setTitle(title);
81         
82     }
83
84     @Override
85     protected Control createExtendedContentArea(Composite parent) {
86         // Don't create anything extra at the moment
87         return null;
88     }                                        
89
90     class ResourceSelectionDialogItemsFilter extends FilteredItemsSelectionDialog.ItemsFilter {
91         
92         public ResourceSelectionDialogItemsFilter() {
93             String patternText = getPattern();
94             patternMatcher = new SearchPattern();
95             if(patternText != null && patternText.length() > 0)
96                 patternMatcher.setPattern(patternText);
97             else 
98                 patternMatcher.setPattern("*");            
99         }
100         
101         @Override
102         public boolean isConsistentItem(Object item) {
103             return true;
104         }
105
106         @Override
107         public boolean matchItem(Object item) {
108             return matches(nameMap.get(item));
109         }
110     }
111     
112     @Override
113     protected ItemsFilter createFilter() {                                
114         return new ResourceSelectionDialogItemsFilter();
115     }
116
117     @Override
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();
124     }
125
126     protected abstract IDialogSettings getBaseDialogSettings();     
127     
128     @Override
129     protected IDialogSettings getDialogSettings() {
130         IDialogSettings base = getBaseDialogSettings();
131         IDialogSettings settings = base.getSection(title);
132         if (settings == null)
133             settings = base.addNewSection(title);
134         return settings;                                
135     }
136
137     @Override
138     public String getElementName(Object item) {
139         return nameMap.get(item);
140     }
141
142     @Override
143     protected Comparator<?> getItemsComparator() {
144         return new Comparator<Object>() {
145
146             @Override
147             public int compare(Object o1, Object o2) {
148                 return nameMap.get(o1).compareToIgnoreCase(nameMap.get(o2));
149             }
150             
151         };
152     }
153
154     @Override
155     protected IStatus validateItem(Object item) {
156         return Status.OK_STATUS;
157     }
158 }