1 |
|
// Copyright © 2010-2013 ENS de Lyon.
|
2 |
|
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
|
3 |
|
// Lyon 2, University of Franche-Comté, University of Nice
|
4 |
|
// Sophia Antipolis, University of Paris 3.
|
5 |
|
//
|
6 |
|
// The TXM platform is free software: you can redistribute it
|
7 |
|
// and/or modify it under the terms of the GNU General Public
|
8 |
|
// License as published by the Free Software Foundation,
|
9 |
|
// either version 2 of the License, or (at your option) any
|
10 |
|
// later version.
|
11 |
|
//
|
12 |
|
// The TXM platform is distributed in the hope that it will be
|
13 |
|
// useful, but WITHOUT ANY WARRANTY; without even the implied
|
14 |
|
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
15 |
|
// PURPOSE. See the GNU General Public License for more
|
16 |
|
// details.
|
17 |
|
//
|
18 |
|
// You should have received a copy of the GNU General
|
19 |
|
// Public License along with the TXM platform. If not, see
|
20 |
|
// http://www.gnu.org/licenses.
|
21 |
|
//
|
22 |
|
//
|
23 |
|
//
|
24 |
|
// $LastChangedDate:$
|
25 |
|
// $LastChangedRevision:$
|
26 |
|
// $LastChangedBy:$
|
27 |
|
//
|
28 |
|
package org.txm.rcp.commands.function;
|
29 |
|
|
30 |
|
import javax.inject.Inject;
|
31 |
|
|
32 |
|
import org.eclipse.core.commands.AbstractHandler;
|
33 |
|
import org.eclipse.core.commands.ExecutionEvent;
|
34 |
|
import org.eclipse.core.commands.ExecutionException;
|
35 |
|
import org.eclipse.core.runtime.IAdaptable;
|
36 |
|
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
|
37 |
|
import org.eclipse.e4.ui.model.application.ui.advanced.impl.PerspectiveStackImpl;
|
38 |
|
import org.eclipse.e4.ui.model.application.ui.advanced.impl.PlaceholderImpl;
|
39 |
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
40 |
|
import org.eclipse.e4.ui.model.application.ui.basic.MPartSashContainerElement;
|
41 |
|
import org.eclipse.e4.ui.model.application.ui.basic.MStackElement;
|
42 |
|
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
|
43 |
|
import org.eclipse.e4.ui.model.application.ui.basic.MWindowElement;
|
44 |
|
import org.eclipse.e4.ui.model.application.ui.basic.impl.PartSashContainerImpl;
|
45 |
|
import org.eclipse.e4.ui.model.application.ui.basic.impl.PartStackImpl;
|
46 |
|
import org.eclipse.e4.ui.workbench.modeling.EModelService;
|
47 |
|
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
48 |
|
import org.eclipse.e4.ui.workbench.modeling.EPlaceholderResolver;
|
49 |
|
import org.eclipse.jface.viewers.ISelection;
|
50 |
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
51 |
|
import org.eclipse.osgi.util.NLS;
|
52 |
|
import org.eclipse.ui.IEditorPart;
|
53 |
|
import org.eclipse.ui.IEditorReference;
|
54 |
|
import org.eclipse.ui.IPageLayout;
|
55 |
|
import org.eclipse.ui.IPartService;
|
56 |
|
import org.eclipse.ui.IPerspectiveDescriptor;
|
57 |
|
import org.eclipse.ui.ISaveablePart;
|
58 |
|
import org.eclipse.ui.IViewReference;
|
59 |
|
import org.eclipse.ui.IWorkbench;
|
60 |
|
import org.eclipse.ui.IWorkbenchPage;
|
61 |
|
import org.eclipse.ui.IWorkbenchWindow;
|
62 |
|
import org.eclipse.ui.IWorkingSet;
|
63 |
|
import org.eclipse.ui.PartInitException;
|
64 |
|
import org.eclipse.ui.PlatformUI;
|
65 |
|
import org.eclipse.ui.handlers.HandlerUtil;
|
66 |
|
import org.eclipse.ui.internal.EditorReference;
|
67 |
|
import org.eclipse.ui.internal.PartService;
|
68 |
|
import org.eclipse.ui.internal.Workbench;
|
69 |
|
import org.eclipse.ui.internal.WorkbenchPage;
|
70 |
|
import org.eclipse.ui.internal.WorkbenchWindow;
|
71 |
|
import org.eclipse.ui.internal.WorkingSet;
|
72 |
|
import org.eclipse.ui.internal.registry.PerspectiveDescriptor;
|
73 |
|
import org.txm.Toolbox;
|
74 |
|
import org.txm.rcp.Messages;
|
75 |
|
import org.txm.rcp.StatusLine;
|
76 |
|
import org.txm.rcp.TXMWindows;
|
77 |
|
import org.txm.rcp.editors.concordances.ConcordancesEditor;
|
78 |
|
import org.txm.rcp.editors.input.ConcordancesEditorInput;
|
79 |
|
import org.txm.searchengine.cqp.corpus.Corpus;
|
80 |
|
|
81 |
|
import cern.colt.Arrays;
|
82 |
|
|
83 |
|
// TODO: Auto-generated Javadoc
|
84 |
|
/**
|
85 |
|
* Open the concordance editor @ author mdecorde.
|
86 |
|
*/
|
87 |
|
public class ComputeConcordance extends AbstractHandler {
|
88 |
|
|
89 |
|
/** The selection. */
|
90 |
|
private IStructuredSelection selection;
|
91 |
|
|
92 |
|
/** The editor input. */
|
93 |
|
private ConcordancesEditorInput editorInput;
|
94 |
|
|
95 |
|
/* (non-Javadoc)
|
96 |
|
* @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
|
97 |
|
*/
|
98 |
|
@Override
|
99 |
|
public Object execute(ExecutionEvent event) throws ExecutionException {
|
100 |
|
|
101 |
|
ISelection sel = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
|
102 |
|
if (!(sel instanceof IStructuredSelection)) return null;
|
103 |
|
selection = (IStructuredSelection) sel;
|
104 |
|
|
105 |
|
Object s = selection.getFirstElement();
|
106 |
|
if (!(s instanceof Corpus))
|
107 |
|
return null;
|
108 |
|
Corpus corpus = (Corpus) s;
|
109 |
|
if (openEditor(corpus)) {
|
110 |
|
|
111 |
|
} else {
|
112 |
|
System.out.println(Messages.ComputeConcordance_1);
|
113 |
|
}
|
114 |
|
|
115 |
|
return null;
|
116 |
|
}
|
117 |
|
|
118 |
|
@Inject
|
119 |
|
public static boolean openEditor(Corpus corpus) {
|
120 |
|
if (!Toolbox.isSearchEngineInitialized()) return false;
|
121 |
|
ConcordancesEditorInput editorInput = new ConcordancesEditorInput(corpus, null);
|
122 |
|
|
123 |
|
// MD: test of manipulating the editor's position in the UI
|
124 |
|
/*
|
125 |
|
Workbench wb = (Workbench) PlatformUI.getWorkbench();
|
126 |
|
|
127 |
|
System.out.println("Workbench: " + wb);
|
128 |
|
for (IWorkbenchWindow iwindow : wb.getWorkbenchWindows()) {
|
129 |
|
WorkbenchWindow window = (WorkbenchWindow) iwindow;
|
130 |
|
|
131 |
|
System.out.println(" Window: "+window);
|
132 |
|
for (IWorkbenchPage ipage :window.getPages()) {
|
133 |
|
WorkbenchPage page = (WorkbenchPage)ipage;
|
134 |
|
System.out.println(" Page: "+page);
|
135 |
|
|
136 |
|
for (IEditorReference ieditor : page.getInternalEditorReferences()) {
|
137 |
|
EditorReference editor = (EditorReference)ieditor;
|
138 |
|
System.out.println(" Editor: "+editor+" "+editor.getId());
|
139 |
|
System.out.println(" part: "+ieditor.getPart(false));
|
140 |
|
}
|
141 |
|
for (IEditorPart dirtyEditor : page.getDirtyEditors()) {
|
142 |
|
System.out.println(" IEditorPart: "+dirtyEditor);
|
143 |
|
}
|
144 |
|
|
145 |
|
for (ISaveablePart dirtyPart : page.getDirtyParts()) {
|
146 |
|
System.out.println(" ISaveablePart: "+dirtyPart);
|
147 |
|
}
|
148 |
|
|
149 |
|
for (EditorReference editorref : page.getInternalEditorReferences()) {
|
150 |
|
System.out.println(" EditorReference: "+editorref+" "+editorref.getId());
|
151 |
|
}
|
152 |
|
// for (IViewReference viewref : page.getViewReferences()) {
|
153 |
|
// System.out.println(" IViewReference: "+viewref+" "+viewref.getId());
|
154 |
|
// }
|
155 |
|
for (IWorkingSet iworkingset : page.getWorkingSets()) {
|
156 |
|
WorkingSet workingset = (WorkingSet)iworkingset;
|
157 |
|
System.out.println(" WorkingSet: "+workingset+" "+workingset.getId());
|
158 |
|
for (IAdaptable a : workingset.getElements()) {
|
159 |
|
System.out.println(" IAdaptable: "+a);
|
160 |
|
}
|
161 |
|
}
|
162 |
|
|
163 |
|
MWindow modelwindow = window.getModel();
|
164 |
|
|
165 |
|
System.out.println("MWindow: "+modelwindow);
|
166 |
|
for (MWindowElement elem : modelwindow.getChildren()) {
|
167 |
|
if (elem instanceof PartSashContainerImpl) {
|
168 |
|
printChildren(elem, " ");
|
169 |
|
// PartSashContainerImpl psc = (PartSashContainerImpl)elem;
|
170 |
|
// System.out.println(" PartSashContainerImpl: "+psc);
|
171 |
|
// for (MPartSashContainerElement elem2 : psc.getChildren()) {
|
172 |
|
//
|
173 |
|
// if (elem2 instanceof PerspectiveStackImpl) {
|
174 |
|
// PerspectiveStackImpl psi = (PerspectiveStackImpl)elem2;
|
175 |
|
// System.out.println(" PerspectiveStackImpl: "+psi);
|
176 |
|
// for (MPerspective p : psi.getChildren()) {
|
177 |
|
// System.out.println(" MPerspective: "+p);
|
178 |
|
// for (MPartSashContainerElement elem3 : p.getChildren()) {
|
179 |
|
// if (elem3 instanceof PartSashContainerImpl) {
|
180 |
|
// PartSashContainerImpl psci = (PartSashContainerImpl)elem3;
|
181 |
|
// printChildren(psci, " ");
|
182 |
|
// } else {
|
183 |
|
// System.out.println(" MPartSashContainerElement: "+elem3);
|
184 |
|
// }
|
185 |
|
// }
|
186 |
|
// }
|
187 |
|
// } else if (elem2 instanceof PartStackImpl) {
|
188 |
|
// PartStackImpl psi = (PartStackImpl)elem2;
|
189 |
|
// System.out.println(" PartStackImpl: "+psi);
|
190 |
|
// for (MStackElement e : psi.getChildren()) {
|
191 |
|
// System.out.println(" MStackElement: "+e);
|
192 |
|
// }
|
193 |
|
// } else {
|
194 |
|
// System.out.println(" MPartSashContainerElement: "+elem2+" id="+elem2.getElementId());
|
195 |
|
// }
|
196 |
|
// }
|
197 |
|
} else {
|
198 |
|
System.out.println(" MWindowElement: "+elem);
|
199 |
|
}
|
200 |
|
}
|
201 |
|
}
|
202 |
|
}
|
203 |
|
*/
|
204 |
|
try {
|
205 |
|
StatusLine.setMessage(Messages.ComputeConcordance_0);
|
206 |
|
IEditorPart editor = TXMWindows.getActiveWindow().getActivePage()
|
207 |
|
.openEditor(editorInput, "org.txm.rcp.editors.ConcordancesEditor"); //$NON-NLS-1$
|
208 |
|
|
209 |
|
// WorkbenchWindow window = (WorkbenchWindow)TXMWindows.getActiveWindow();
|
210 |
|
// MWindow model = window.getModel();
|
211 |
|
// EModelService modelService = model.getContext().get(EModelService.class);
|
212 |
|
// EPartService partService = model.getContext().get(EPartService.class);
|
213 |
|
// modelService.find("org.eclipse.ui.editorss", model); // find org.eclipse.ui.editorss?
|
214 |
|
// MPart part = partService.createPart("org.txm.rcp.editors.ConcordancesEditor");
|
215 |
|
//// NEXT STEPS : find partStack, add the part, show the part http://stackoverflow.com/questions/26912078/open-an-editing-partas-an-editor-in-previous-eclipse-versions-in-eclipse-rcp-4
|
216 |
|
// ? direct access to PlaceholderImpl : elementId=org.eclipse.ui.editorss
|
217 |
|
if (editor == null) return false;
|
218 |
|
|
219 |
|
} catch (PartInitException e) {
|
220 |
|
System.err.println(NLS.bind(Messages.GetConcordances_4, e));
|
221 |
|
}
|
222 |
|
return true;
|
223 |
|
}
|
224 |
|
|
225 |
|
@SuppressWarnings("unused")
|
226 |
|
private static void printChildren(Object o, String space) {
|
227 |
|
|
228 |
|
System.out.println(space+o.getClass().getSimpleName()+": "+o);
|
229 |
|
|
230 |
|
if (o instanceof PlaceholderImpl) {
|
231 |
|
PlaceholderImpl phi = (PlaceholderImpl)o;
|
232 |
|
System.out.println("phi: "+phi);
|
233 |
|
} else if (o instanceof PartSashContainerImpl) {
|
234 |
|
PartSashContainerImpl psci = (PartSashContainerImpl)o;
|
235 |
|
for (MPartSashContainerElement e :psci.getChildren()) {
|
236 |
|
printChildren(e, space+" ");
|
237 |
|
}
|
238 |
|
} else if (o instanceof PerspectiveStackImpl) {
|
239 |
|
PerspectiveStackImpl psi = (PerspectiveStackImpl)o;
|
240 |
|
for (MPerspective p : psi.getChildren()) {
|
241 |
|
printChildren(p,space+" ");
|
242 |
|
}
|
243 |
|
} else if (o instanceof MPerspective) {
|
244 |
|
MPerspective p = (MPerspective)o;
|
245 |
|
for (MPartSashContainerElement e : p.getChildren()) {
|
246 |
|
printChildren(e, space+" ");
|
247 |
|
}
|
248 |
|
} else if (o instanceof PartStackImpl) {
|
249 |
|
PartStackImpl psi = (PartStackImpl)o;
|
250 |
|
for (MStackElement e : psi.getChildren()) {
|
251 |
|
printChildren(e, space+" ");
|
252 |
|
}
|
253 |
|
}
|
254 |
|
}
|
255 |
|
}
|