KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.bull.eclipse.jonas.actions;
2
3 /*
4  * (c) Copyright Bull SA 2003.
5  * All Rights Reserved.
6  */

7  
8
9 import org.eclipse.core.resources.IProject;
10 import org.eclipse.jdt.core.IJavaProject;
11 import org.eclipse.jdt.internal.ui.JavaPlugin;
12 import org.eclipse.jface.action.IAction;
13 import org.eclipse.jface.dialogs.MessageDialog;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.swt.widgets.Shell;
17 import org.eclipse.ui.IWorkbenchWindow;
18 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
19
20 import com.bull.eclipse.jonas.JonasLauncherPlugin;
21 import com.bull.eclipse.jonas.JonasProject;
22
23 abstract public class JonasProjectAbstractActionDelegate implements IWorkbenchWindowActionDelegate {
24     protected IWorkbenchWindow window;
25     private String JavaDoc msg;
26
27     /*
28      * @see IWorkbenchWindowActionDelegate#dispose()
29      */

30     public void dispose() {
31     }
32
33     /*
34      * @see IWorkbenchWindowActionDelegate#init(IWorkbenchWindow)
35      */

36     public void init(IWorkbenchWindow window) {
37         this.window = window;
38     }
39
40     /*
41      * @see IActionDelegate#run(IAction)
42      */

43     public void run(IAction action) {
44         Shell shell= JavaPlugin.getActiveWorkbenchShell();
45         try {
46             JonasProject prj = this.getCurrentSelection();
47             String JavaDoc actionId = action.getId();
48             if((prj != null) ) {
49                 if (this.doActionOn(prj)) {
50                     setMsgToSuccess();
51                     MessageDialog.openInformation(shell,"Jonas", msg);
52                 }
53             }
54         } catch (Exception JavaDoc ex) {
55             JonasLauncherPlugin.log(ex.getStackTrace().toString());
56             setMsgToFail(ex.getMessage(), true);
57             MessageDialog.openInformation(shell,"Jonas", msg);
58         }
59         
60     }
61
62     /*
63      * @see IActionDelegate#selectionChanged(IAction, ISelection)
64      */

65     public void selectionChanged(IAction action, ISelection selection) {
66
67     }
68
69     protected JonasProject getCurrentSelection() {
70         IWorkbenchWindow window = JavaPlugin.getActiveWorkbenchWindow();
71         JonasProject result = null;
72         if (window != null) {
73             ISelection selection= window.getSelectionService().getSelection();
74             if (selection instanceof IStructuredSelection) {
75                 Object JavaDoc project = ((IStructuredSelection)selection).getFirstElement();
76                 if(project instanceof IProject)
77                     result = JonasProject.create((IProject)project);
78                 if(project instanceof IJavaProject)
79                     result = JonasProject.create((IJavaProject)project);
80             }
81         }
82         return result;
83     }
84     
85     abstract public boolean doActionOn(JonasProject prj) throws Exception JavaDoc;
86
87     /**
88      * Sets the msg.
89      * @param msg The msg to set
90      */

91     private void setMsgToFail(String JavaDoc detail, boolean seelog) {
92         this.msg = JonasLauncherPlugin.getResourceString("msg.action.failed");
93         this.msg += "\n" + detail;
94         if(seelog) {
95             this.msg += JonasLauncherPlugin.getResourceString("msg.action.seelog");
96         }
97     }
98     
99     /**
100      * Sets the msg.
101      * @param msg The msg to set
102      */

103     private void setMsgToSuccess() {
104         this.msg = JonasLauncherPlugin.getResourceString("msg.action.succeeded");
105     }
106 }
107
108
Popular Tags