Eclipseplugins
AbstractProAlphaHandler.java
1 package com.proalpha.pds.ui;
2 
3 import org.eclipse.core.commands.AbstractHandler;
4 import org.eclipse.core.commands.ExecutionEvent;
5 import org.eclipse.core.commands.ExecutionException;
6 import org.eclipse.core.resources.IFile;
7 import org.eclipse.core.resources.IProject;
8 import org.eclipse.core.resources.IResource;
9 import org.eclipse.core.runtime.IAdaptable;
10 import org.eclipse.jface.text.ITextSelection;
11 import org.eclipse.jface.viewers.ISelection;
12 import org.eclipse.jface.viewers.IStructuredSelection;
13 import org.eclipse.ui.handlers.HandlerUtil;
14 
15 import com.openedge.pdt.project.IOpenEdgeProject;
16 import com.openedge.pdt.project.OEProject;
17 import com.openedge.pdt.project.OEProjectPlugin;
18 import com.proalpha.pds.exception.ProALPHANotAvailableException;
19 import com.proalpha.pds.paconnector.Activator;
20 import com.proalpha.pds.paconnector.PaProject;
21 
22 public class AbstractProAlphaHandler extends AbstractHandler {
23 
24  /*
25  * (non-Javadoc)
26  *
27  * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
28  * ExecutionEvent)
29  */
30  @Override
31  public Object execute(ExecutionEvent arg0) throws ExecutionException {
32  throw new ExecutionException("AbstractProAlphaHandler must be extended by an existing proALPHA handler!");
33  }
34 
43  public PaProject getPaProjectOfEvent(ExecutionEvent event)
44  throws ProALPHANotAvailableException, ExecutionException {
45 
46  IProject project = null;
47  Object firstElement = null;
48 
49  ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
50 
51  if (selection == null)
52  throw new ProALPHANotAvailableException("No pA-Selection found!");
53 
54  // The selection is an instance of interface IStructuredSelection if the handler
55  // gets triggered from Project Explorer
56  if (selection instanceof IStructuredSelection) {
57  IStructuredSelection structuredSelection = (IStructuredSelection) selection;
58  firstElement = structuredSelection.getFirstElement();
59  }
60 
61  // The selection is an instance of interface ITextSelection if the handler gets
62  // triggered from an editor view
63  else if (selection instanceof ITextSelection)
64  firstElement = HandlerUtil.getActiveEditorInput(event);
65 
66  else
67  throw new ProALPHANotAvailableException("No pA-Resource selected!");
68 
69  // IResource should cover all cases if the action was executed from within
70  // Resource Explorer
71  if (firstElement instanceof IResource) {
72  IResource res = (IResource) firstElement;
73  project = res.getProject();
74  }
75 
76  else if (firstElement instanceof IFile) {
77  IFile file = (IFile) firstElement;
78  project = file.getProject();
79  }
80 
81  else if (firstElement instanceof IAdaptable) {
82  project = ((IAdaptable) firstElement).getAdapter(IProject.class);
83 
84  if (project == null) {
85  IResource resource = ((IAdaptable) firstElement).getAdapter(IResource.class);
86  if (resource != null) {
87  project = resource.getProject();
88  }
89  }
90  }
91 
92  if (project == null)
93  throw new ProALPHANotAvailableException("Could not find current pA-project!");
94 
95  IOpenEdgeProject currProj = OEProjectPlugin.getDefault().getOpenEdgeModel()
96  .getOpenEdgeProject((IResource) project);
97 
98  PaProject paProject = Activator.getDefault().getProjectManager().getPaProject((OEProject) currProj);
99 
100  if (paProject == null || !paProject.isAvailable())
101  throw new ProALPHANotAvailableException("No pA-project runtime available!");
102 
103  return paProject;
104  }
105 
114  public String getOeFileOfEvent(ExecutionEvent event) throws ProALPHANotAvailableException, ExecutionException {
115 
116  String fileName = null;
117  Object firstElement = null;
118 
119  ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
120 
121  if (selection == null)
122  throw new ProALPHANotAvailableException("No pA-Selection found!");
123 
124  // The selection is an instance of interface IStructuredSelection if the handler
125  // gets triggered from Project Explorer
126  if (selection instanceof IStructuredSelection) {
127  IStructuredSelection structuredSelection = (IStructuredSelection) selection;
128  firstElement = structuredSelection.getFirstElement();
129  }
130 
131  // The selection is an instance of interface ITextSelection if the handler gets
132  // triggered from an editor view
133  else if (selection instanceof ITextSelection)
134  firstElement = HandlerUtil.getActiveEditorInput(event);
135 
136  else
137  throw new ProALPHANotAvailableException("No pA-Resource selected!");
138 
139  // IResource should cover all cases if the action was executed from within
140  // Resource Explorer
141  if (firstElement instanceof IFile) {
142  IFile file = (IFile) firstElement;
143  fileName = file.getName();
144  }
145 
146  else if (firstElement instanceof IAdaptable) {
147  IResource resource = ((IAdaptable) firstElement).getAdapter(IResource.class);
148  if (resource != null) {
149  fileName = resource.getName();
150  }
151  }
152 
153  if (fileName == null)
154  throw new ProALPHANotAvailableException("No pA-File selected!");
155 
156  return fileName;
157  }
158 }
PaProject getPaProjectOfEvent(ExecutionEvent event)