KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > actions > JonasPackageAbstractActionDelegate


1 package com.bull.eclipse.jonas.actions;
2
3 import org.eclipse.jdt.core.IPackageFragment;
4 import org.eclipse.jdt.internal.ui.JavaPlugin;
5 import org.eclipse.jface.action.IAction;
6 import org.eclipse.jface.dialogs.MessageDialog;
7 import org.eclipse.jface.viewers.ISelection;
8 import org.eclipse.jface.viewers.IStructuredSelection;
9 import org.eclipse.swt.widgets.Shell;
10 import org.eclipse.ui.IWorkbenchWindow;
11 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
12
13 import com.bull.eclipse.jonas.JonasLauncherPlugin;
14
15 abstract public class JonasPackageAbstractActionDelegate implements IWorkbenchWindowActionDelegate {
16     protected IWorkbenchWindow window;
17     private String JavaDoc msg;
18
19     /*
20      * @see IWorkbenchWindowActionDelegate#dispose()
21      */

22     public void dispose() {
23     }
24
25     /*
26      * @see IWorkbenchWindowActionDelegate#init(IWorkbenchWindow)
27      */

28     public void init(IWorkbenchWindow window) {
29         this.window = window;
30     }
31
32     /*
33      * @see IActionDelegate#run(IAction)
34      */

35     public void run(IAction action) {
36         Shell shell= JavaPlugin.getActiveWorkbenchShell();
37         try {
38             IPackageFragment packFragment = this.getCurrentSelection();
39             String JavaDoc actionId = action.getId();
40             if((packFragment != null) ) {
41                 if (this.doActionOn(packFragment)) {
42                     setMsgToSuccess();
43                     MessageDialog.openInformation(shell,"Jonas", msg);
44                 }
45             }
46         } catch (Exception JavaDoc ex) {
47             System.out.println("Exception : " + ex.getMessage());
48             JonasLauncherPlugin.log(ex.getStackTrace().toString());
49             setMsgToFail(ex.getMessage(), true);
50             MessageDialog.openInformation(shell,"Jonas", msg);
51         }
52         
53     }
54
55
56     protected IPackageFragment getCurrentSelection() {
57         IWorkbenchWindow window = JavaPlugin.getActiveWorkbenchWindow();
58         IPackageFragment resultFragment = null;
59         if (window != null) {
60             ISelection selection= window.getSelectionService().getSelection();
61             if (selection instanceof IStructuredSelection) {
62                 Object JavaDoc packageFragment = ((IStructuredSelection)selection).getFirstElement();
63                 if(packageFragment instanceof IPackageFragment)
64                     resultFragment = (IPackageFragment) packageFragment;
65             }
66         }
67         return resultFragment;
68     }
69     
70     abstract public boolean doActionOn(IPackageFragment packFrag) throws Exception JavaDoc;
71
72     /**
73      * Sets the msg.
74      * @param msg The msg to set
75      */

76     private void setMsgToFail(String JavaDoc detail, boolean seelog) {
77         this.msg = JonasLauncherPlugin.getResourceString("msg.action.failed");
78         this.msg += "\n" + detail;
79         if(seelog) {
80             this.msg += JonasLauncherPlugin.getResourceString("msg.action.seelog");
81         }
82     }
83     
84     /**
85      * Sets the msg.
86      * @param msg The msg to set
87      */

88     private void setMsgToSuccess() {
89         this.msg = JonasLauncherPlugin.getResourceString("msg.action.succeeded");
90     }
91     
92     /*
93      * @see IActionDelegate#selectionChanged(IAction, ISelection)
94      */

95     public void selectionChanged(IAction action, ISelection selection) {
96
97     }
98     
99 }
100
101
Popular Tags