KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
11 import java.util.Iterator JavaDoc;
12
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.jdt.core.IJavaElement;
15 import org.eclipse.jdt.core.IMember;
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.ui.IObjectActionDelegate;
18
19 import de.loskutov.bco.BytecodeOutlinePlugin;
20
21 /**
22  * @author Andrei
23  */

24 public class CompareMemberBytecodeAction extends BytecodeAction implements IObjectActionDelegate {
25
26     /**
27      * (non-Javadoc)
28      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
29      */

30     public void run(IAction action) {
31         IJavaElement[] resources = getSelectedResources();
32         try {
33             exec(resources[0], resources[1]);
34         } catch (Exception JavaDoc e) {
35             BytecodeOutlinePlugin.error("Failed to run Compare: "
36                 + e.getMessage(), e);
37         }
38     }
39
40
41     protected IJavaElement[] getSelectedResources() {
42         ArrayList JavaDoc resources = null;
43         if (!selection.isEmpty()) {
44             resources = new ArrayList JavaDoc();
45             for (Iterator JavaDoc elements = selection.iterator(); elements.hasNext();) {
46                 Object JavaDoc next = elements.next();
47                 if (next instanceof IMember) {
48                     resources.add(next);
49                     continue;
50                 } else if (next instanceof IAdaptable) {
51                     IAdaptable a = (IAdaptable) next;
52                     Object JavaDoc adapter = a.getAdapter(IMember.class);
53                     if (adapter instanceof IMember) {
54                         resources.add(adapter);
55                         continue;
56                     }
57                 }
58             }
59         }
60
61         if (resources != null && !resources.isEmpty()) {
62             return (IJavaElement[]) resources.toArray(new IJavaElement[resources
63                 .size()]);
64         }
65
66         return new IJavaElement[0];
67     }
68 }
Popular Tags