KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > loskutov > bco > ui > actions > OpenAction


1 /*****************************************************************************************
2  * Copyright (c) 2004 Andrei Loskutov. All rights reserved. This program and the
3  * accompanying materials are made available under the terms of the BSD License which
4  * accompanies this distribution, and is available at
5  * http://www.opensource.org/licenses/bsd-license.php Contributor: Andrei Loskutov -
6  * initial API and implementation
7  ****************************************************************************************/

8 package de.loskutov.bco.ui.actions;
9
10 import org.eclipse.core.resources.IContainer;
11 import org.eclipse.core.resources.IFile;
12 import org.eclipse.core.resources.IResource;
13 import org.eclipse.core.resources.ResourcesPlugin;
14 import org.eclipse.jdt.core.IJavaElement;
15 import org.eclipse.jdt.core.JavaCore;
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.ui.IObjectActionDelegate;
20 import org.eclipse.ui.dialogs.ResourceListSelectionDialog;
21
22 import de.loskutov.bco.BytecodeOutlinePlugin;
23
24 /**
25  * @author Andrei
26  */

27 public class OpenAction extends BytecodeAction implements IObjectActionDelegate {
28
29     /**
30      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
31      */

32     public void run(IAction action) {
33         // always only one element!
34
IJavaElement[] resources = getSelectedResources();
35
36         // select one from input dialog
37
IJavaElement element2 = selectJavaElement();
38         if (element2 == null) {
39             return;
40         }
41         try {
42             exec(resources[0], element2);
43         } catch (Exception JavaDoc e) {
44             BytecodeOutlinePlugin.error("Failed to run Compare: "
45                 + e.getMessage(), e);
46         }
47     }
48
49     private IJavaElement selectJavaElement() {
50         IContainer input = ResourcesPlugin.getWorkspace().getRoot();
51
52         OpenClassFileDialog dialog = new OpenClassFileDialog(
53             shell, input, IResource.FILE);
54
55         int resultCode = dialog.open();
56         if (resultCode != IDialogConstants.OK_ID) {
57             return null;
58         }
59
60         Object JavaDoc[] result = dialog.getResult();
61         if (result == null || result.length == 0
62             || !(result[0] instanceof IFile)) {
63             return null;
64         }
65         return JavaCore.create((IFile) result[0]);
66     }
67
68     /**
69      * @author Andrei
70      */

71     private static final class OpenClassFileDialog extends ResourceListSelectionDialog {
72
73         /**
74          * @param parentShell
75          * @param container
76          * @param typesMask
77          */

78         public OpenClassFileDialog(Shell parentShell, IContainer container,
79             int typesMask) {
80             super(parentShell, container, typesMask);
81             setTitle("Bytecode compare");
82             setMessage("Please select class file to compare");
83         }
84
85         /**
86          * Extends the super's filter to exclude derived resources.
87          * @since 3.0
88          */

89         protected boolean select(IResource resource) {
90             if (resource == null) {
91                 return false;
92             }
93             String JavaDoc fileExtension = resource.getFileExtension();
94             return super.select(resource)
95                 && ("java".equals(fileExtension) || "class"
96                     .equals(fileExtension));
97         }
98     }
99
100 }
101
Popular Tags