75 |
75 |
* @author mdecorde
|
76 |
76 |
*/
|
77 |
77 |
public class LoadBinaryCorporaDirectory extends AbstractHandler {
|
78 |
|
|
|
78 |
|
79 |
79 |
private static final String ID = LoadBinaryCorporaDirectory.class.getName();
|
80 |
|
|
|
80 |
|
81 |
81 |
@Override
|
82 |
82 |
/**
|
83 |
83 |
* calling this command will : open a directory dialog and try to load the content of the selected directory
|
84 |
84 |
*/
|
85 |
85 |
public Object execute(ExecutionEvent event) throws ExecutionException {
|
86 |
|
|
|
86 |
|
87 |
87 |
// create dialog to get the corpus directory
|
88 |
88 |
Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell();
|
89 |
89 |
DirectoryDialog dialog = new DirectoryDialog(shell, SWT.OPEN);
|
90 |
90 |
if (LastOpened.getFile(ID) != null) {
|
91 |
91 |
dialog.setFilterPath(LastOpened.getFolder(ID) + "/" + LastOpened.getFile(ID));
|
92 |
92 |
}
|
93 |
|
|
|
93 |
|
94 |
94 |
if (dialog.open() != null) {
|
95 |
|
|
|
95 |
|
96 |
96 |
File directory = new File(dialog.getFilterPath());
|
97 |
|
|
|
97 |
|
98 |
98 |
if (directory.exists()) {
|
99 |
99 |
LastOpened.set(ID, directory);
|
100 |
100 |
JobHandler job = loadBinaryCorpusFromCorporaDirectory(directory, true, true, true);
|
... | ... | |
111 |
111 |
}
|
112 |
112 |
return null;
|
113 |
113 |
}
|
114 |
|
|
|
114 |
|
115 |
115 |
/**
|
116 |
116 |
* stop engines, try loading binary corpora from a directory, restart engines and refresh views
|
117 |
117 |
*
|
... | ... | |
119 |
119 |
* @return
|
120 |
120 |
*/
|
121 |
121 |
public static JobHandler loadBinaryCorpusFromCorporaDirectory(final File directory, final boolean askSelection, final boolean replace, final boolean loadDirectories) {
|
122 |
|
|
|
122 |
|
123 |
123 |
JobHandler jobhandler = new JobHandler(NLS.bind(TXMUIMessages.LoadBinaryCorporaDirectory_loadingCorporaFromTheP0Directory, directory), true) {
|
124 |
|
|
|
124 |
|
125 |
125 |
JobHandler self = this;
|
126 |
|
|
|
126 |
|
127 |
127 |
@Override
|
128 |
128 |
protected IStatus run(final IProgressMonitor monitor) {
|
129 |
129 |
this.runInit(monitor);
|
130 |
|
|
131 |
|
// try {
|
132 |
|
// SearchEnginesManager.getCQPSearchEngine().stop();
|
133 |
|
// } catch (Exception e1) {
|
134 |
|
// // TODO Auto-generated catch block
|
135 |
|
// e1.printStackTrace();
|
136 |
|
// }
|
137 |
|
|
138 |
|
File corporaDirectory = new File(Toolbox.getTxmHomePath(), "corpora"); //$NON-NLS-1$
|
139 |
|
if (!corporaDirectory.exists()) {
|
140 |
|
Log.severe(TXMUIMessages.LoadBinaryCorporaDirectory_corporaDirectoryNotFoundAborting);
|
|
130 |
|
|
131 |
try {
|
|
132 |
// SearchEnginesManager.getCQPSearchEngine().stop();
|
|
133 |
// } catch (Exception e1) {
|
|
134 |
// // TODO Auto-generated catch block
|
|
135 |
// e1.printStackTrace();
|
|
136 |
// }
|
|
137 |
|
|
138 |
File corporaDirectory = new File(Toolbox.getTxmHomePath(), "corpora"); //$NON-NLS-1$
|
|
139 |
if (!corporaDirectory.exists()) {
|
|
140 |
Log.severe(TXMUIMessages.LoadBinaryCorporaDirectory_corporaDirectoryNotFoundAborting);
|
|
141 |
// try {
|
|
142 |
// SearchEnginesManager.getCQPSearchEngine().start(null);
|
|
143 |
// } catch (Exception e) {
|
|
144 |
// // TODO Auto-generated catch block
|
|
145 |
// e.printStackTrace();
|
|
146 |
// }
|
|
147 |
return Status.CANCEL_STATUS;
|
|
148 |
}
|
|
149 |
|
|
150 |
final ArrayList<Project> newProjects = new ArrayList<>();
|
|
151 |
if (askSelection) {
|
|
152 |
|
|
153 |
this.syncExec(new Runnable() {
|
|
154 |
|
|
155 |
@Override
|
|
156 |
public void run() {
|
|
157 |
List<File> files = getBinaryCorpusSelection(directory, loadDirectories);
|
|
158 |
ArrayList<Project> tmp = loadBinaryCorpusFromCorporaDirectory(files, self, monitor, replace);
|
|
159 |
if (tmp != null) {
|
|
160 |
newProjects.addAll(tmp);
|
|
161 |
}
|
|
162 |
}
|
|
163 |
});
|
|
164 |
|
|
165 |
}
|
|
166 |
else { // try loading corpora and dont replace existing corpus
|
|
167 |
List<File> files = Arrays.asList(getBinaryCorpusFiles(directory, loadDirectories));
|
|
168 |
ArrayList<Project> tmp = loadBinaryCorpusFromCorporaDirectory(files, this, monitor, replace);
|
|
169 |
if (tmp != null) {
|
|
170 |
newProjects.addAll(tmp);
|
|
171 |
}
|
|
172 |
}
|
|
173 |
|
|
174 |
// this.acquireSemaphore();
|
141 |
175 |
// try {
|
142 |
176 |
// SearchEnginesManager.getCQPSearchEngine().start(null);
|
143 |
177 |
// } catch (Exception e) {
|
144 |
|
// // TODO Auto-generated catch block
|
145 |
178 |
// e.printStackTrace();
|
146 |
179 |
// }
|
147 |
|
return Status.CANCEL_STATUS;
|
148 |
|
}
|
149 |
|
|
150 |
|
final ArrayList<Project> newProjects = new ArrayList<>();
|
151 |
|
if (askSelection) {
|
152 |
|
|
|
180 |
// this.releaseSemaphore();
|
|
181 |
|
|
182 |
for (Project newProject2 : newProjects) {
|
|
183 |
try {
|
|
184 |
newProject2.compute(true);
|
|
185 |
}
|
|
186 |
catch (Exception e) {
|
|
187 |
e.printStackTrace();
|
|
188 |
}
|
|
189 |
}
|
|
190 |
if (newProjects.size() > 0) {
|
|
191 |
System.out.println(NLS.bind(TXMUIMessages.P0CorpusLoaded, newProjects.size()));
|
|
192 |
}
|
|
193 |
|
153 |
194 |
this.syncExec(new Runnable() {
|
154 |
|
|
|
195 |
|
155 |
196 |
@Override
|
156 |
197 |
public void run() {
|
157 |
|
List<File> files = getBinaryCorpusSelection(directory, loadDirectories);
|
158 |
|
ArrayList<Project> tmp = loadBinaryCorpusFromCorporaDirectory(files, self, monitor, replace);
|
159 |
|
if (tmp != null) {
|
160 |
|
newProjects.addAll(tmp);
|
161 |
|
}
|
|
198 |
RestartTXM.reloadViews();
|
|
199 |
StatusLine.setMessage(TXMUIMessages.info_txmIsReady);
|
162 |
200 |
}
|
163 |
201 |
});
|
164 |
|
|
|
202 |
} catch(ThreadDeath ex) {
|
|
203 |
Log.info(TXMUIMessages.executionCanceled);
|
|
204 |
return Status.CANCEL_STATUS;
|
165 |
205 |
}
|
166 |
|
else { // try loading corpora and dont replace existing corpus
|
167 |
|
List<File> files = Arrays.asList(getBinaryCorpusFiles(directory, loadDirectories));
|
168 |
|
ArrayList<Project> tmp = loadBinaryCorpusFromCorporaDirectory(files, this, monitor, replace);
|
169 |
|
if (tmp != null) {
|
170 |
|
newProjects.addAll(tmp);
|
171 |
|
}
|
172 |
|
}
|
173 |
|
|
174 |
|
// this.acquireSemaphore();
|
175 |
|
// try {
|
176 |
|
// SearchEnginesManager.getCQPSearchEngine().start(null);
|
177 |
|
// } catch (Exception e) {
|
178 |
|
// e.printStackTrace();
|
179 |
|
// }
|
180 |
|
// this.releaseSemaphore();
|
181 |
|
|
182 |
|
for (Project newProject2 : newProjects) {
|
183 |
|
try {
|
184 |
|
newProject2.compute(true);
|
185 |
|
}
|
186 |
|
catch (Exception e) {
|
187 |
|
e.printStackTrace();
|
188 |
|
}
|
189 |
|
}
|
190 |
|
if (newProjects.size() > 0) {
|
191 |
|
System.out.println(NLS.bind(TXMUIMessages.P0CorpusLoaded, newProjects.size()));
|
192 |
|
}
|
193 |
|
|
194 |
|
this.syncExec(new Runnable() {
|
195 |
|
|
196 |
|
@Override
|
197 |
|
public void run() {
|
198 |
|
RestartTXM.reloadViews();
|
199 |
|
StatusLine.setMessage(TXMUIMessages.info_txmIsReady);
|
200 |
|
}
|
201 |
|
});
|
202 |
|
|
203 |
206 |
return Status.OK_STATUS;
|
204 |
207 |
}
|
205 |
208 |
};
|
206 |
|
|
|
209 |
|
207 |
210 |
jobhandler.schedule();
|
208 |
211 |
return jobhandler;
|
209 |
212 |
}
|
210 |
|
|
|
213 |
|
211 |
214 |
protected static List<File> getBinaryCorpusSelection(File directory, boolean loadDirectories) {
|
212 |
215 |
ArrayList<File> selectedFiles = new ArrayList<>();
|
213 |
216 |
;
|
214 |
217 |
if (!directory.isDirectory()) return selectedFiles;
|
215 |
218 |
if (!directory.exists()) return selectedFiles;
|
216 |
|
|
|
219 |
|
217 |
220 |
Shell shell = null;
|
218 |
|
|
|
221 |
|
219 |
222 |
if (Display.getCurrent() != null) {
|
220 |
223 |
shell = Display.getCurrent().getActiveShell();
|
221 |
224 |
}
|
... | ... | |
229 |
232 |
return selectedFiles;
|
230 |
233 |
}
|
231 |
234 |
File[] files = getBinaryCorpusFiles(directory, loadDirectories);
|
232 |
|
|
|
235 |
|
233 |
236 |
ListSelectionDialog lsd = new ListSelectionDialog(shell, files, new ArrayContentProvider(), new LabelProvider() {
|
234 |
|
|
|
237 |
|
235 |
238 |
@Override
|
236 |
239 |
public Image getImage(Object element) {
|
237 |
240 |
File f = (File) element;
|
... | ... | |
243 |
246 |
}
|
244 |
247 |
return null;
|
245 |
248 |
}
|
246 |
|
|
|
249 |
|
247 |
250 |
@Override
|
248 |
251 |
public String getText(Object element) {
|
249 |
252 |
if (element == null) return "error";//$NON-NLS-1$
|
250 |
|
|
|
253 |
|
251 |
254 |
if (!(element instanceof File)) return "not a file (" + element + ")";//$NON-NLS-1$ //$NON-NLS-2$
|
252 |
|
|
|
255 |
|
253 |
256 |
File f = (File) element;
|
254 |
257 |
// if (f.isDirectory()) {
|
255 |
258 |
// if (new File(f, ".settings").exists()) {//$NON-NLS-1$
|
... | ... | |
263 |
266 |
// return f.getName();
|
264 |
267 |
// }
|
265 |
268 |
return f.getName();
|
266 |
|
|
|
269 |
|
267 |
270 |
// return "not a txm corpus (" + f + ")";//$NON-NLS-1$ //$NON-NLS-2$
|
268 |
271 |
}
|
269 |
272 |
}, TXMUIMessages.bind(TXMUIMessages.LoadBinaryCorporaDirectory_selectTheTXMCorpusToLoadIn, directory));
|
... | ... | |
275 |
278 |
selectedFiles.add(new File(o.toString()));
|
276 |
279 |
}
|
277 |
280 |
}
|
278 |
|
|
|
281 |
|
279 |
282 |
return selectedFiles;
|
280 |
283 |
}
|
281 |
|
|
|
284 |
|
282 |
285 |
private static File[] getBinaryCorpusFiles(File directory, boolean loadDirectories) {
|
283 |
286 |
File[] files = null;
|
284 |
|
|
|
287 |
|
285 |
288 |
if (loadDirectories) {
|
286 |
289 |
files = directory.listFiles(new FileFilter() {
|
287 |
|
|
|
290 |
|
288 |
291 |
@Override
|
289 |
292 |
public boolean accept(File arg0) {
|
290 |
293 |
return IOUtils.HIDDENFILE_FILTER.accept(arg0) &&
|
291 |
294 |
((arg0.isDirectory() && (new File(arg0, "import.xml").exists() || new File(arg0, ".settings").exists()))
|
292 |
295 |
|| arg0.getName().endsWith(".txm")
|
293 |
|
|
294 |
|
);
|
|
296 |
|
|
297 |
);
|
295 |
298 |
}
|
296 |
299 |
});
|
297 |
300 |
}
|
298 |
301 |
else {
|
299 |
302 |
files = directory.listFiles(new FileFilter() {
|
300 |
|
|
|
303 |
|
301 |
304 |
@Override
|
302 |
305 |
public boolean accept(File arg0) {
|
303 |
306 |
return IOUtils.HIDDENFILE_FILTER.accept(arg0) && arg0.getName().endsWith(".txm"); //$NON-NLS-1$
|
304 |
307 |
}
|
305 |
308 |
});
|
306 |
309 |
}
|
307 |
|
|
|
310 |
|
308 |
311 |
Arrays.sort(files, new Comparator<File>() {
|
309 |
|
|
|
312 |
|
310 |
313 |
@Override
|
311 |
314 |
public int compare(File o1, File o2) {
|
312 |
315 |
int i = o1.getName().compareTo(o2.getName());
|
... | ... | |
316 |
319 |
return i;
|
317 |
320 |
}
|
318 |
321 |
});
|
319 |
|
|
|
322 |
|
320 |
323 |
return files;
|
321 |
324 |
}
|
322 |
|
|
|
325 |
|
323 |
326 |
/**
|
324 |
327 |
* dont manage SearchEngine state
|
325 |
328 |
*
|
... | ... | |
332 |
335 |
File[] files = directory.listFiles(IOUtils.HIDDENFILE_FILTER);
|
333 |
336 |
return loadBinaryCorpusFromCorporaDirectory(Arrays.asList(files), job, monitor, replace);
|
334 |
337 |
}
|
335 |
|
|
|
338 |
|
336 |
339 |
/**
|
337 |
340 |
* dont manage SearchEngine state
|
338 |
341 |
*
|
... | ... | |
344 |
347 |
* @return the loaded projects
|
345 |
348 |
*/
|
346 |
349 |
protected static ArrayList<Project> loadBinaryCorpusFromCorporaDirectory(List<File> corporaFiles, JobHandler job, IProgressMonitor monitor, boolean replace) {
|
347 |
|
|
|
350 |
|
348 |
351 |
ArrayList<Project> projects = new ArrayList<>();
|
349 |
|
|
|
352 |
|
350 |
353 |
try {
|
351 |
|
|
|
354 |
|
352 |
355 |
// SearchEngine engine = SearchEnginesManager.getCQPSearchEngine();
|
353 |
356 |
// if (engine.isRunning()) {
|
354 |
357 |
// engine.stop();
|
355 |
358 |
// }
|
356 |
|
|
|
359 |
|
357 |
360 |
monitor.beginTask(TXMUIMessages.loadingBinaryCorpus, 100);
|
358 |
361 |
// job.acquireSemaphore();
|
359 |
362 |
// Toolbox.getEngineManager(EngineType.SEARCH).stopEngines();
|
360 |
363 |
// job.releaseSemaphore();
|
361 |
|
|
|
364 |
|
362 |
365 |
monitor.subTask(TXMUIMessages.loadingTheCorpusInThePlatform);
|
363 |
366 |
for (File corpusDirectory : corporaFiles) {
|
364 |
367 |
try {
|
365 |
368 |
if (corpusDirectory.isHidden()) {
|
366 |
369 |
continue;
|
367 |
370 |
}
|
368 |
|
|
|
371 |
|
369 |
372 |
Project project = Toolbox.workspace.getProject(corpusDirectory.getName().toUpperCase());
|
370 |
373 |
if (project != null) {
|
371 |
374 |
Log.warning(TXMCoreMessages.bind(TXMUIMessages.LoadBinaryCorporaDirectory_aCorpusNamedP0alreadyExistsLoadingOfP1Canceled, corpusDirectory.getName().toUpperCase(),
|
372 |
375 |
corpusDirectory));
|
373 |
376 |
continue;
|
374 |
377 |
}
|
375 |
|
|
|
378 |
|
376 |
379 |
if (corpusDirectory.isDirectory()) {
|
377 |
380 |
project = LoadBinaryCorpus.loadBinaryCorpusAsDirectory(corpusDirectory, monitor, replace);
|
378 |
381 |
}
|
379 |
382 |
else {
|
380 |
383 |
project = LoadBinaryCorpus.loadBinaryCorpusArchive(corpusDirectory, job, monitor, replace);
|
381 |
384 |
}
|
382 |
|
|
|
385 |
|
383 |
386 |
// Project base = LoadBinaryCorpus.loadBinaryCorpusAsDirectory(corpusDirectory);
|
384 |
387 |
if (project == null) {
|
385 |
388 |
// Log.warning("Failed to load binary corpus: "+corpusDirectory);
|
... | ... | |
394 |
397 |
Log.printStackTrace(e2);
|
395 |
398 |
}
|
396 |
399 |
}
|
397 |
|
|
|
400 |
|
398 |
401 |
monitor.subTask(TXMUIMessages.restartingThePlatform);
|
399 |
|
|
|
402 |
|
400 |
403 |
}
|
401 |
404 |
catch (ThreadDeath td) {
|
402 |
405 |
}
|
... | ... | |
406 |
409 |
finally {
|
407 |
410 |
monitor.done();
|
408 |
411 |
}
|
409 |
|
|
|
412 |
|
410 |
413 |
return projects;
|
411 |
414 |
}
|
412 |
415 |
}
|