Method: JarClassSelector()

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.utils;
23:
24: import java.util.List;
25: import java.util.Vector;
26:
27: import org.eclipse.core.resources.IContainer;
28: import org.eclipse.core.resources.IResource;
29: import org.eclipse.core.resources.IResourceVisitor;
30: import org.eclipse.core.runtime.CoreException;
31: import org.eclipse.jface.dialogs.IDialogConstants;
32: import org.eclipse.jface.viewers.LabelProvider;
33: import org.eclipse.swt.widgets.Shell;
34: import org.eclipse.ui.dialogs.ElementListSelectionDialog;
35:
36: public class JarClassSelector
37: {
38:
39:         public static String selectClass(Shell shell, IContainer container)
40:                         throws CoreException
41:         {
42:                 // TODO Auto-generated method stub
43:                 // final ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(getShell(), new
44:                 // DecorationgVdmLabelProvider(new VdmUILabelProvider()), new MergedModuleVdmOutlineTreeContentProvider());
45:                 // ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(getShell(), new WorkbenchLabelProvider(),
46:                 // new BaseWorkbenchContentProvider());
47:
48:                 ElementListSelectionDialog dialog = new ElementListSelectionDialog(shell, new LabelProvider());
49:
50:                 dialog.setTitle("Select Class");
51:                 dialog.setMessage("Select a remote control class");
52:
53:                 // dialog.addFilter(new ExecutableFilter());
54:                 // dialog.setComparator(new
55:                 // ResourceComparator(ResourceComparator.NAME));
56:                 // dialog.setAllowMultiple(false);
57:
58:                 final List<String> jars = new Vector<String>();
59:
60:                 if (container.exists())
61:                 {
62:                         container.accept(new IResourceVisitor()
63:                         {
64:
65:                                 @Override
66:                                 public boolean visit(IResource resource) throws CoreException
67:                                 {
68:                                         if (resource.getType() == IResource.FILE
69:                                                         && resource.getFileExtension() != null
70:                                                         && resource.getFileExtension().equals("jar"))
71:                                         {
72:                                                 jars.add(resource.getLocation().toFile().getAbsolutePath());
73:                                         } else if (resource.getType() == IResource.FOLDER)
74:                                         {
75:                                                 return true;
76:                                         }
77:                                         return false;
78:                                 }
79:                         });
80:                 }
81:
82:                 List<String> classes = new Vector<String>();
83:                 for (String path : jars)
84:                 {
85:                         classes.addAll(PackageUtils.getClasseNamesInPackage(path, null));
86:                 }
87:
88:                 // dialog.setInput(classes);
89:                 dialog.setElements(classes.toArray());
90:                 if (dialog.open() == IDialogConstants.OK_ID)
91:                 {
92:                         if (dialog.getFirstResult() instanceof String)
93:                         {
94:                                 return dialog.getFirstResult().toString();
95:                         }
96:
97:                 }
98:                 return null;
99:         }
100:
101: }