1 package com.bull.eclipse.jonas.actions; 2 3 import java.util.Iterator ; 4 5 import org.eclipse.jdt.core.ICompilationUnit; 6 import org.eclipse.jdt.internal.ui.JavaPlugin; 7 import org.eclipse.jface.action.IAction; 8 import org.eclipse.jface.dialogs.MessageDialog; 9 import org.eclipse.jface.viewers.ISelection; 10 import org.eclipse.jface.viewers.IStructuredSelection; 11 import org.eclipse.swt.widgets.Shell; 12 import org.eclipse.ui.IWorkbenchWindow; 13 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 14 15 import com.bull.eclipse.jonas.JonasLauncherPlugin; 16 17 abstract public class JonasFileAbstractActionDelegate 18 implements IWorkbenchWindowActionDelegate { 19 protected IWorkbenchWindow window; 20 private String msg; 21 private Iterator it = null; 22 private ICompilationUnit compunit = null; 23 24 27 28 public void dispose() { 29 } 30 31 34 public void init(IWorkbenchWindow window) { 35 this.window = window; 36 } 37 38 41 public void run(IAction action) { 42 Shell shell = JavaPlugin.getActiveWorkbenchShell(); 43 try { 44 45 String actionId = action.getId(); 46 if (actionId.equalsIgnoreCase("EJB.addLog")) { 47 it = getIterator(); 48 if (it == null) 49 return; 50 while (it.hasNext()) { 51 Object compunit = it.next(); 52 if (compunit instanceof ICompilationUnit 53 && compunit != null) { 54 55 if (!this.doActionOn((ICompilationUnit) compunit)) { 56 setMsgToFail("Erreur ..", true); 57 MessageDialog.openInformation(shell, "Jonas", msg); 58 return; 59 } 60 } 61 } 62 63 setMsgToSuccess(); 64 MessageDialog.openInformation(shell, "Jonas", msg); 65 66 } 67 68 else { 69 compunit = this.getCurrentSelection(); 70 if ((compunit != null)) { 71 if (this.doActionOn(compunit)) { 72 setMsgToSuccess(); 73 MessageDialog.openInformation(shell, "Jonas", msg); 74 } 75 } 76 } 77 78 } catch (Exception ex) { 79 System.out.println("Exception : " + ex.getMessage()); 80 JonasLauncherPlugin.log(ex.getStackTrace().toString()); 81 setMsgToFail(ex.getMessage(), true); 82 MessageDialog.openInformation(shell, "Jonas", msg); 83 } 84 85 } 86 87 protected ICompilationUnit getCurrentSelection() { 88 89 IWorkbenchWindow window = JavaPlugin.getActiveWorkbenchWindow(); 90 ICompilationUnit result = null; 91 92 if (window != null) { 93 ISelection selection = window.getSelectionService().getSelection(); 94 if (selection instanceof IStructuredSelection) { 95 Object compunit = 96 ((IStructuredSelection) selection).getFirstElement(); 97 if (compunit instanceof ICompilationUnit) 98 result = (ICompilationUnit) compunit; 99 } 100 } 101 return result; 102 } 103 104 protected Iterator getIterator() { 105 106 IWorkbenchWindow window = JavaPlugin.getActiveWorkbenchWindow(); 107 ICompilationUnit result = null; 108 109 if (window != null) { 110 ISelection selection = window.getSelectionService().getSelection(); 111 if (selection instanceof IStructuredSelection) { 112 return ((IStructuredSelection) selection).iterator(); 113 } 114 } 115 return null; 116 } 117 118 protected ICompilationUnit getNextSelection() { 119 120 IWorkbenchWindow window = JavaPlugin.getActiveWorkbenchWindow(); 121 ICompilationUnit result = null; 122 123 if (window != null) { 124 ISelection selection = window.getSelectionService().getSelection(); 125 if (selection instanceof IStructuredSelection) { 126 Object compunit = 127 ((IStructuredSelection) selection).getFirstElement(); 128 if (compunit instanceof ICompilationUnit) 129 result = (ICompilationUnit) compunit; 130 } 131 } 132 return result; 133 } 134 135 abstract public boolean doActionOn(ICompilationUnit compunit) 136 throws Exception ; 137 138 142 private void setMsgToFail(String detail, boolean seelog) { 143 this.msg = JonasLauncherPlugin.getResourceString("msg.action.failed"); 144 this.msg += "\n" + detail; 145 if (seelog) { 146 this.msg 147 += JonasLauncherPlugin.getResourceString("msg.action.seelog"); 148 } 149 } 150 151 155 private void setMsgToSuccess() { 156 this.msg = 157 JonasLauncherPlugin.getResourceString("msg.action.succeeded"); 158 } 159 160 163 public void selectionChanged(IAction action, ISelection selection) { 164 165 } 166 167 } 168 | Popular Tags |