Method: restoreItemFromMemento(IMemento)

1: /*
2: * #%~
3: * org.overture.ide.debug
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.debug.ui.launchconfigurations;
23:
24: import java.util.Arrays;
25: import java.util.Comparator;
26: import java.util.HashMap;
27: import java.util.Map;
28:
29: import org.eclipse.core.resources.IProject;
30: import org.eclipse.core.runtime.CoreException;
31: import org.eclipse.core.runtime.IAdaptable;
32: import org.eclipse.core.runtime.IProgressMonitor;
33: import org.eclipse.core.runtime.IStatus;
34: import org.eclipse.core.runtime.Status;
35: import org.eclipse.jface.dialogs.IDialogSettings;
36: import org.eclipse.jface.resource.ImageDescriptor;
37: import org.eclipse.jface.viewers.ILabelProvider;
38: import org.eclipse.jface.viewers.ILabelProviderListener;
39: import org.eclipse.swt.graphics.Image;
40: import org.eclipse.swt.widgets.Composite;
41: import org.eclipse.swt.widgets.Control;
42: import org.eclipse.swt.widgets.Shell;
43: import org.eclipse.ui.IMemento;
44: import org.eclipse.ui.dialogs.FilteredItemsSelectionDialog;
45: import org.eclipse.ui.model.IWorkbenchAdapter;
46: import org.overture.ast.definitions.AExplicitFunctionDefinition;
47: import org.overture.ast.definitions.AExplicitOperationDefinition;
48: import org.overture.ast.node.INode;
49: import org.overture.ide.debug.core.VdmDebugPlugin;
50: import org.overture.ide.ui.utility.ast.AstNameUtil;
51:
52: public class DebugTypeSelectionDialog extends FilteredItemsSelectionDialog
53: {
54:
55:         /**
56:          * Main list label provider
57:          */
58:         public class DebugTypeLabelProvider implements ILabelProvider
59:         {
60:                 Map<ImageDescriptor, Image> fImageMap = new HashMap<ImageDescriptor, Image>();
61:
62:                 public Image getImage(Object element)
63:                 {
64:                         if (element instanceof IAdaptable)
65:                         {
66:                                 IWorkbenchAdapter adapter = (IWorkbenchAdapter) ((IAdaptable) element).getAdapter(IWorkbenchAdapter.class);
67:                                 if (adapter != null)
68:                                 {
69:                                         ImageDescriptor descriptor = adapter.getImageDescriptor(element);
70:                                         Image image = (Image) fImageMap.get(descriptor);
71:                                         if (image == null)
72:                                         {
73:                                                 image = descriptor.createImage();
74:                                                 fImageMap.put(descriptor, image);
75:                                         }
76:                                         return image;
77:                                 }
78:                         }
79:                         return null;
80:                 }
81:
82:                 public String getText(Object element)
83:                 {
84:                         if (element instanceof INode)
85:                         {
86:                                 INode type = (INode) element;
87:                                 String label = AstNameUtil.getName(type);
88:                                 String container = getDeclaringContainerName(type);
89:                                 if (container != null && !"".equals(container)) { //$NON-NLS-1$
90:                                         label += " - " + container; //$NON-NLS-1$
91:                                 }
92:                                 return label;
93:                         }
94:                         return null;
95:                 }
96:
97:                 /**
98:                  * Returns the name of the declaring container name
99:                  *
100:                  * @param type
101:                  * the type to find the container name for
102:                  * @return the container name for the specified type
103:                  */
104:                 protected String getDeclaringContainerName(INode type)
105:                 {
106:
107:                         if (type instanceof AExplicitFunctionDefinition)
108:                         {
109:                                 return ((AExplicitFunctionDefinition) type).getLocation().getModule();
110:                         }
111:                         if (type instanceof AExplicitOperationDefinition)
112:                         {
113:                                 return ((AExplicitOperationDefinition) type).getLocation().getModule();
114:                         }
115:
116:                         return "";
117:                         // INode outer = type.getDeclaringType();
118:                         // if(outer != null) {
119:                         // return outer.getFullyQualifiedName('.');
120:                         // }
121:                         // else {
122:                         // String name = type.getPackageFragment().getElementName();
123:                         //                                if("".equals(name)) { //$NON-NLS-1$
124:                         // name = LauncherMessages.MainMethodLabelProvider_0;
125:                         // }
126:                         // return name;
127:                         // }
128:                 }
129:
130:                 /**
131:                  * Returns the narrowest enclosing <code>IJavaElement</code> which is either an <code>IType</code> (enclosing)
132:                  * or an <code>IPackageFragment</code> (contained in)
133:                  *
134:                  * @param type
135:                  * the type to find the enclosing <code>IJavaElement</code> for.
136:                  * @return the enclosing element or <code>null</code> if none
137:                  */
138:                 protected INode getDeclaringContainer(INode type)
139:                 {
140:                         // IJavaElement outer = type.getDeclaringType();
141:                         // if(outer == null) {
142:                         // outer = type.getPackageFragment();
143:                         // }
144:                         // return outer;
145:
146:                         if (type instanceof AExplicitFunctionDefinition)
147:                         {
148:                                 return ((AExplicitFunctionDefinition) type).getClassDefinition();
149:                         }
150:                         if (type instanceof AExplicitOperationDefinition)
151:                         {
152:                                 return ((AExplicitOperationDefinition) type).getClassDefinition();
153:                         }
154:
155:                         return null;
156:                 }
157:
158:                 public void dispose()
159:                 {
160:                         fImageMap.clear();
161:                         fImageMap = null;
162:                 }
163:
164:                 public void addListener(ILabelProviderListener listener)
165:                 {
166:                 }
167:
168:                 public boolean isLabelProperty(Object element, String property)
169:                 {
170:                         return false;
171:                 }
172:
173:                 public void removeListener(ILabelProviderListener listener)
174:                 {
175:                 }
176:         }
177:
178:         /**
179:          * Provides a label and image for the details area of the dialog
180:          */
181:         class DebugTypeDetailsLabelProvider extends DebugTypeLabelProvider
182:         {
183:                 public String getText(Object element)
184:                 {
185:                         if (element instanceof INode)
186:                         {
187:                                 INode type = (INode) element;
188:                                 String name = getDeclaringContainerName(type);
189:                                 if (name != null)
190:                                 {
191:                                         if (name.equals(LauncherMessages.MainMethodLabelProvider_0))
192:                                         {
193:                                                 // IProject project =vdmProject;// type.getJavaProject();
194:                                                 if (project != null)
195:                                                 {
196:                                                         // try {
197:                                                         return project.getLocation().toOSString().substring(1)
198:                                                                         + " - " + name; //$NON-NLS-1$
199:                                                         // }
200:                                                         // catch (JavaModelException e) {JDIDebugUIPlugin.log(e);}
201:                                                 }
202:                                         } else
203:                                         {
204:                                                 return name;
205:                                         }
206:                                 }
207:                         }
208:                         return null;
209:                 }
210:
211:                 public Image getImage(Object element)
212:                 {
213:                         if (element instanceof INode)
214:                         {
215:                                 return super.getImage(getDeclaringContainer((INode) element));
216:                         }
217:                         return super.getImage(element);
218:                 }
219:         }
220:
221:         /**
222:          * Simple items filter
223:          */
224:         class DebugTypeItemsFilter extends ItemsFilter
225:         {
226:                 public boolean isConsistentItem(Object item)
227:                 {
228:                         return item instanceof INode;
229:                 }
230:
231:                 public boolean matchItem(Object item)
232:                 {
233:                         if (!(item instanceof INode)
234:                                         || !Arrays.asList(fTypes).contains(item))
235:                         {
236:                                 return false;
237:                         }
238:                         return matches(AstNameUtil.getName((INode) item));
239:                 }
240:         }
241:
242:         /**
243:          * The selection history for the dialog
244:          */
245:         class DebugTypeSelectionHistory extends SelectionHistory
246:         {
247:                 protected Object restoreItemFromMemento(IMemento memento)
248:                 {
249:                         // IJavaElement element = JavaCore.create(memento.getTextData());
250:                         Object element = null;
251:•                        return element instanceof INode ? element : null;
252:                 }
253:
254:                 protected void storeItemToMemento(Object item, IMemento memento)
255:                 {
256:                         if (item instanceof INode)
257:                         {
258:                                 // memento.putTextData(((INode) item).getHandleIdentifier());
259:                         }
260:                 }
261:         }
262:
263:         private static final String SETTINGS_ID = "";// JDIDebugUIPlugin.getUniqueIdentifier() + ".MAIN_METHOD_SELECTION_DIALOG"; //$NON-NLS-1$
264:         private INode[] fTypes = null;
265:         private IProject project;
266:
267:         /**
268:          * Constructor
269:          *
270:          * @param elements
271:          * the types to display in the dialog
272:          */
273:         public DebugTypeSelectionDialog(Shell shell, INode[] elements,
274:                         String title, IProject project)
275:         {
276:                 super(shell, false);
277:                 setTitle(title);
278:                 fTypes = elements;
279:                 this.project = project;
280:                 setMessage(LauncherMessages.VdmMainTab_Choose_a_main__type_to_launch__12);
281:                 setInitialPattern("**"); //$NON-NLS-1$
282:                 setListLabelProvider(new DebugTypeLabelProvider());
283:                 setDetailsLabelProvider(new DebugTypeDetailsLabelProvider());
284:                 setSelectionHistory(new DebugTypeSelectionHistory());
285:         }
286:
287:         /*
288:          * (non-Javadoc)
289:          * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
290:          */
291:         protected Control createDialogArea(Composite parent)
292:         {
293:                 Control ctrl = super.createDialogArea(parent);
294:                 // PlatformUI.getWorkbench().getHelpSystem().setHelp(ctrl, IJavaDebugHelpContextIds.SELECT_MAIN_METHOD_DIALOG);
295:                 return ctrl;
296:         }
297:
298:         /**
299:          * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getDialogSettings()
300:          */
301:         protected IDialogSettings getDialogSettings()
302:         {
303:                 IDialogSettings settings = VdmDebugPlugin.getDefault().getDialogSettings();
304:                 IDialogSettings section = settings.getSection(SETTINGS_ID);
305:                 if (section == null)
306:                 {
307:                         section = settings.addNewSection(SETTINGS_ID);
308:                 }
309:                 return section;
310:                 // return null;
311:         }
312:
313:         /**
314:          * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getItemsComparator()
315:          */
316:         protected Comparator<Object> getItemsComparator()
317:         {
318:                 Comparator<Object> comp = new Comparator<Object>()
319:                 {
320:                         public int compare(Object o1, Object o2)
321:                         {
322:                                 if (o1 instanceof INode && o2 instanceof INode)
323:                                 {
324:                                         return AstNameUtil.getName((INode) o1).compareTo(AstNameUtil.getName((INode) o2));
325:                                 }
326:                                 return -1;
327:                         }
328:                 };
329:                 return comp;
330:         }
331:
332:         /**
333:          * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#validateItem(java.lang.Object)
334:          */
335:         protected IStatus validateItem(Object item)
336:         {
337:                 return Status.OK_STATUS;
338:         }
339:
340:         /**
341:          * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createExtendedContentArea(org.eclipse.swt.widgets.Composite)
342:          */
343:         protected Control createExtendedContentArea(Composite parent)
344:         {
345:                 return null;
346:         }
347:
348:         /**
349:          * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createFilter()
350:          */
351:         protected ItemsFilter createFilter()
352:         {
353:                 return new DebugTypeItemsFilter();
354:         }
355:
356:         /**
357:          * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#fillContentProvider(org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.AbstractContentProvider,
358:          * org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter, org.eclipse.core.runtime.IProgressMonitor)
359:          */
360:         protected void fillContentProvider(AbstractContentProvider contentProvider,
361:                         ItemsFilter itemsFilter, IProgressMonitor progressMonitor)
362:                         throws CoreException
363:         {
364:                 if (fTypes != null && fTypes.length > 0)
365:                 {
366:                         for (int i = 0; i < fTypes.length; i++)
367:                         {
368:                                 if (itemsFilter.isConsistentItem(fTypes[i]))
369:                                 {
370:                                         contentProvider.add(fTypes[i], itemsFilter);
371:                                 }
372:                         }
373:                 }
374:         }
375:
376:         /**
377:          * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getElementName(java.lang.Object)
378:          */
379:         public String getElementName(Object item)
380:         {
381:                 if (item instanceof INode)
382:                 {
383:                         return AstNameUtil.getName((INode) item);
384:                 }
385:                 return null;
386:         }
387: }