Révision 3630

TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/Application.java (revision 3630)
44 44
import org.eclipse.equinox.app.IApplicationContext;
45 45
import org.eclipse.jface.dialogs.MessageDialog;
46 46
import org.eclipse.jface.util.Util;
47
import org.eclipse.jface.viewers.StructuredSelection;
47 48
import org.eclipse.osgi.util.TextProcessor;
48 49
import org.eclipse.swt.SWT;
49 50
import org.eclipse.swt.widgets.Display;
......
57 58
import org.osgi.framework.Version;
58 59
import org.osgi.service.prefs.Preferences;
59 60
import org.txm.Toolbox;
61
import org.txm.core.engines.Engine;
62
import org.txm.core.engines.EngineType;
63
import org.txm.core.engines.EnginesManager;
60 64
import org.txm.core.messages.TXMCoreMessages;
61 65
import org.txm.core.preferences.TBXPreferences;
62 66
import org.txm.objects.Project;
63 67
import org.txm.objects.Workspace;
64 68
import org.txm.rcp.commands.workspace.Load080BinaryCorpus;
65 69
import org.txm.rcp.handlers.scripts.ExecuteImportScript;
70
import org.txm.rcp.handlers.scripts.ExecuteScript;
66 71
import org.txm.rcp.messages.TXMUIMessages;
67 72
import org.txm.rcp.preferences.RCPPreferences;
68 73
import org.txm.rcp.swt.dialog.CGUMessageDialog;
......
81 86
public class Application implements IApplication {
82 87
	/** The Constant PLUGIN_ID. */
83 88
	public static final String PLUGIN_ID = "org.txm.rcp"; //$NON-NLS-1$
84

  
89
	
85 90
	/** The Constant FIRST_LAUNCH. */
86 91
	public static final String FIRST_LAUNCH = "first_launch"; //$NON-NLS-1$
87

  
92
	
88 93
	private static ApplicationWorkbenchAdvisor awa;
89

  
94
	
90 95
	private static boolean headlessMode = false;
91 96
	
92 97
	private static boolean restMode = false;
93

  
98
	
94 99
	// public static OpenDocumentEventProcessor openDocProcessor = new OpenDocumentEventProcessor();
95 100
	/*
96 101
	 * (non-Javadoc)
......
98 103
	 */
99 104
	@Override
100 105
	public Object start(IApplicationContext context) throws Exception {
101

  
106
		
102 107
		Map args = context.getArguments();
103 108
		List<String> argsList = Arrays.asList((String[]) (args.get("application.args"))); //$NON-NLS-1$
104 109
		System.out.println(TXMCoreMessages.bind(TXMUIMessages.argsColon, argsList));
105

  
110
		
106 111
		headlessMode = argsList.contains("--headless");
107 112
		if (headlessMode) {
108 113
			return startHeadLess(argsList);
......
112 117
		if (restMode) {
113 118
			return startRest(argsList);
114 119
		}
115

  
120
		
116 121
		return startDesktop(argsList);
117 122
	}
118

  
123
	
119 124
	private int startDesktop(List<String> argsList) {
120 125
		// if not headless continue as usual for the Desktop version to start
121 126
		Display display = PlatformUI.createDisplay();
122

  
123

  
127
		
128
		
124 129
		// if (Platform.inDevelopmentMode() || Platform.inDebugMode()) {
125 130
		// // this can not work in development environment
126 131
		// // as it need to have a default workspace set to none
......
132 137
		// } else {
133 138
		// in Release mode only : platform workspace selection on startup
134 139
		// then restart if needed (on workspace error...)
135

  
140
		
136 141
		System.out.println("instance location=" + Platform.getInstanceLocation().getURL()); //$NON-NLS-1$
137 142
		if (Platform.getInstanceLocation().getURL() == null) {
138

  
143
			
139 144
			Integer ret = openCGU();
140 145
			if (ret == IApplication.EXIT_RESTART) {
141 146
				System.out.println("** Working directory set by user."); //$NON-NLS-1$
......
149 154
				// OK
150 155
			}
151 156
		}
152

  
157
		
153 158
		// Die if platform WS not set (user cancel)
154 159
		// ---------------------------
155

  
160
		
156 161
		if (Platform.getInstanceLocation().getURL() == null) {
157 162
			System.out.println("** Working directory not set."); //$NON-NLS-1$
158 163
			return IApplication.EXIT_OK;
159 164
		}
160

  
161

  
165
		
166
		
162 167
		// load JVM fonts in SWT env
163 168
		if (System.getProperty("os.name").contains("Linux")) { // $NON-NLS-1$ $NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-2$
164 169
			File javaFontsPath = new File(System.getProperty("java.home") //$NON-NLS-1$
165 170
					+ System.getProperty("file.separator") + "lib" //$NON-NLS-1$ //$NON-NLS-2$
166 171
					+ System.getProperty("file.separator") + "fonts" //$NON-NLS-1$ //$NON-NLS-2$
167 172
					+ System.getProperty("file.separator")); //$NON-NLS-1$
168

  
173
			
169 174
			String[] fontFiles = javaFontsPath.list();
170 175
			if (fontFiles != null) {
171 176
				for (int i = 0; i < fontFiles.length; i++) {
......
178 183
				}
179 184
			}
180 185
		}
181

  
186
		
182 187
		if (argsList.contains("-log")) { //$NON-NLS-1$
183 188
			System.out.println(TXMUIMessages.logOptionDetected);
184 189
			Log.setPrintInConsole(true);
......
188 193
			Toolbox.setPreference(TBXPreferences.LOG_LEVEL, "ALL"); //$NON-NLS-1$
189 194
			Toolbox.setPreference(TBXPreferences.LOG_STACKTRACE, true);
190 195
		}
191

  
196
		
192 197
		if (argsList.contains("-run") //$NON-NLS-1$
193 198
				|| System.getProperty("os.name").contains("Mac") //$NON-NLS-1$
194 199
				|| Platform.inDevelopmentMode()) { // special case when TXM is run with Eclipse
......
231 236
		else {
232 237
			System.err.println("TXM must be launch with the argument '-run' to start."); //$NON-NLS-1$
233 238
			System.err.println(TXMUIMessages.thisFileIsNotMeantToRunTXMPleaseConsultTXMUserManual);
234

  
239
			
235 240
			javax.swing.JOptionPane.showMessageDialog(null, TXMUIMessages.thisFileIsNotMeantToRunTXMPleaseConsultTXMUserManual);
236 241
			return IApplication.EXIT_OK;
237 242
		}
238 243
		// test if Java 1.6 is installed
239 244
		String version = System.getProperty("java.version"); //$NON-NLS-1$
240 245
		Log.warning("java.version : " + version); //$NON-NLS-1$
241

  
246
		
242 247
		boolean exit = false;// parseCommandLine();
243 248
		if (exit) {
244 249
			return IApplication.EXIT_OK;
245 250
		}
246

  
247

  
251
		
252
		
248 253
		// display.addListener(SWT.OpenDocument, openDocProcessor);
249

  
254
		
250 255
		// //Lock the RCP workspace, thus there is only one TXM instance running
251 256
		// Log.warning(TXMUIMessages.lockRCPWorkspace);
252 257
		// if (!Platform.getInstanceLocation().lock()){
......
254 259
		// System.out.println("TXM is already running. Exiting...");
255 260
		// return IApplication.EXIT_OK;
256 261
		// }
257

  
262
		
258 263
		try {
259 264
			Log.warning(TXMUIMessages.activateProxyService);
260 265
			activateProxyService();
......
262 267
		catch (Exception e) {
263 268
			Log.warning(TXMCoreMessages.bind(TXMUIMessages.couldNotStartProxyConfigurationColon, e));
264 269
		}
265

  
270
		
266 271
		try {			
267 272
			// run the application mainloop
268 273
			awa = new ApplicationWorkbenchAdvisor();
269 274
			System.out.println("Running TXM workbench."); //$NON-NLS-1$
270

  
275
			
271 276
			int returnCode = PlatformUI.createAndRunWorkbench(display, awa);
272

  
277
			
273 278
			if (returnCode == PlatformUI.RETURN_RESTART) {
274 279
				System.out.println("Restarting TXM."); //$NON-NLS-1$
275 280
				return IApplication.EXIT_RESTART;
276 281
			}
277

  
282
			
278 283
			System.out.println("Exiting TXM."); //$NON-NLS-1$
279 284
			return IApplication.EXIT_OK;
280 285
		}
......
289 294
		
290 295
		return 0;
291 296
	}
292

  
297
	
293 298
	private int startHeadLess(List<String> argsList) throws IllegalStateException, IOException {
294

  
299
		
295 300
		if (Toolbox.getTxmHomePath() == null || Toolbox.getTxmHomePath().isEmpty()) {
296 301
			File txmhomedir = Platform.getLocation().toFile().getCanonicalFile();
297 302
			TBXPreferences.getInstance().put(TBXPreferences.USER_TXM_HOME, txmhomedir.getAbsolutePath());
298 303
		}
299 304
		int ret = IApplication.EXIT_OK;
300

  
305
		
301 306
		try {
302 307
			if (Toolbox.initialize(TBXPreferences.class, new LogMonitor())) {
303

  
308
				
304 309
				if (argsList.contains("--action=list")) {
305 310
					ret = doList();
306 311
				}
307

  
312
				
308 313
				if (argsList.contains("--action=new")) {
309 314
					String path = argsList.get(argsList.size() -2);
310 315
					File sourcePath = new File(path);
311

  
316
					
312 317
					String name = argsList.get(argsList.size() -1);
313 318
					
314 319
					ret = doNew(sourcePath, name);
......
325 330
					String path = argsList.get(argsList.size() -1).trim();
326 331
					ret = doRemove(path);
327 332
				} else if (argsList.contains("--action=command")) {
328

  
333
					
329 334
					String name = argsList.get(argsList.size() -2).trim();
330

  
335
					
331 336
					String path = argsList.get(argsList.size() -1).trim();
332

  
337
					
333 338
					ret = doCommand(name, path);
339
				} else if (argsList.contains("--action=groovy")) {
340
					
341
					
342
					String path = argsList.get(argsList.size() -1).trim();
343
					
344
					ret = doGroovy(path, argsList);
345
				} else if (argsList.contains("--action=R")) {
346
					
347
					
348
					String path = argsList.get(argsList.size() -1).trim();
349
					
350
					ret = doR(path, argsList);
334 351
				}
335

  
352
				
336 353
				Toolbox.workspace.saveParameters(true);
337 354
				Toolbox.shutdown();
338 355
			} else {
......
343 360
			e.printStackTrace();
344 361
			ret = -1;
345 362
		}
346

  
363
		
347 364
		return ret;
348 365
	}
349

  
366
	
367
	private int doGroovy(String path, List<String> argsList) {
368
		
369
		File script = new File(path);
370
		if (!script.isFile()) return -1;
371
		if (!script.exists()) return -1;
372
		if (!script.getName().endsWith(".groovy")) return -1;
373
		ExecuteScript.executeScript(path, null, new StructuredSelection(), argsList);
374
		return 0;
375
	}
376
	
377
	private int doR(String path, List<String> argsList) {
378
		
379
		File script = new File(path);
380
		if (!script.isFile()) return -1;
381
		if (!script.exists()) return -1;
382
		if (!script.getName().endsWith(".R")) return -1;
383
		EnginesManager<? extends Engine> em = Toolbox.getEngineManager(EngineType.STATS);
384
		if (em == null) return -1;
385
		
386
		Engine e = em.getEngine("R");
387
		if (e == null || !e.isRunning()) return -1;
388
		
389
		ExecuteScript.executeScript(path, null, new StructuredSelection(), argsList);
390
		
391
		return 0;
392
	}
393
	
350 394
	private int doList() throws UnexpectedAnswerException, IOException, CqiServerError {
351 395
		
352 396
		System.out.println(Arrays.asList(CQPSearchEngine.getCqiClient().listCorpora()));
353 397
		return IApplication.EXIT_OK;
354 398
	}
355

  
399
	
356 400
	private int doExport(String path) throws IOException {
357 401
		
358 402
		int ret = IApplication.EXIT_OK;
......
372 416
		}
373 417
		return ret;
374 418
	}
375

  
419
	
376 420
	private int doCommand(String name, String path) {
377 421
		
378 422
		int ret = IApplication.EXIT_OK;
......
387 431
		}
388 432
		return ret;
389 433
	}
390

  
434
	
391 435
	private int doRemove(String path) {
392 436
		
393 437
		int ret = IApplication.EXIT_OK;
......
407 451
		}
408 452
		return ret;
409 453
	}
410

  
454
	
411 455
	private int doImport(File sourcePath) throws Exception {
412 456
		
413 457
		int ret = IApplication.EXIT_OK;
......
422 466
			ret = -1;
423 467
		} else { // DO IT
424 468
			Project p = Load080BinaryCorpus.loadBinaryCorpusArchive(sourcePath, new LogMonitor(), true);
425

  
469
			
426 470
			if (p == null) {
427 471
				ret = -1;
428 472
			} 
429

  
473
			
430 474
			System.out.println(Arrays.asList(CQPSearchEngine.getCqiClient().listCorpora()));
431 475
			ret = 0;
432 476
		}
433 477
		return ret;
434 478
	}
435

  
479
	
436 480
	private int doNew(File sourcePath, String name) throws InterruptedException {
437 481
		
438 482
		int ret = IApplication.EXIT_OK;
......
451 495
			p.setDirty();
452 496
			JobHandler job = ExecuteImportScript.executeScript(p);
453 497
			job.join();
454

  
498
			
455 499
			ret = job.getResult() == Status.OK_STATUS ? IApplication.EXIT_OK:-1;
456 500
		}
457 501
		return ret;
458 502
	}
459

  
503
	
460 504
	private Integer openCGU() {
461 505
		try {
462

  
506
			
463 507
			Version v = BundleUtils.getBundleVersion("org.txm.rcp");//$NON-NLS-1$
464 508
			if (v == null) {
465 509
				System.out.println("Error: no 'org.txm.rcp' version found."); //$NON-NLS-1$
......
471 515
				version += "-dev"; //$NON-NLS-1$
472 516
			}
473 517
			System.out.println("Version=" + version); //$NON-NLS-1$
474

  
518
			
475 519
			File txmhomedir = new File(System.getProperty("user.home"), "TXM-" + version); //$NON-NLS-1$ //$NON-NLS-2$
476

  
477

  
478

  
520
			
521
			
522
			
479 523
			// System.out.println("CGU="+ConfigurationScope.INSTANCE.getNode(IDEWorkbenchPlugin.IDE_WORKBENCH).getBoolean(RCPPreferences.CGU, false));
480 524
			if (!ConfigurationScope.INSTANCE.getNode(IDEWorkbenchPlugin.IDE_WORKBENCH).getBoolean(RCPPreferences.CGU, false)) {
481 525
				ChooseWorkspaceData launchData = new ChooseWorkspaceData(txmhomedir.getAbsolutePath());
......
485 529
				boolean ret = (dialog.open() == MessageDialog.OK) && dialog.hasCheckBeenPressed();
486 530
				if (ret) {
487 531
					// TBXPreferences.getInstance().put(RCPPreferences.CGU, true);
488

  
532
					
489 533
					// check choosen path
490 534
					String path = launchData.getSelection();
491 535
					if (path != null) { // reset the letadata file
......
498 542
					if (ret2 != -1) {
499 543
						return ret2;
500 544
					}
501

  
545
					
502 546
					// validate Current as default Workspace, save configuration & history
503

  
547
					
504 548
					launchData.writePersistedData();
505 549
					System.out.println("save workspace location: " + launchData.getSelection()); //$NON-NLS-1$
506 550
					ChooseWorkspaceData.setShowDialogValue(false);
507

  
551
					
508 552
					Preferences node = ConfigurationScope.INSTANCE.getNode(IDEWorkbenchPlugin.IDE_WORKBENCH);
509 553
					node.putBoolean(RCPPreferences.CGU, true);
510 554
					node.flush();
511

  
555
					
512 556
					return -1;
513

  
557
					
514 558
				}
515 559
				else {
516 560
					return IApplication.EXIT_OK;
......
519 563
			else {
520 564
				ChooseWorkspaceData launchData = new ChooseWorkspaceData(txmhomedir.getAbsolutePath());
521 565
				String[] recent = launchData.getRecentWorkspaces();
522

  
566
				
523 567
				// If the selection dialog was not used then the workspace to use is either the
524 568
				// most recent selection or the initialDefault (if there is no history).
525 569
				String workspace = null;
......
530 574
					workspace = launchData.getInitialDefault();
531 575
				}
532 576
				launchData.workspaceSelected(TextProcessor.deprocess(workspace));
533

  
577
				
534 578
				return setInstanceLocation(launchData);
535 579
			}
536 580
		}
......
540 584
			return IApplication.EXIT_OK;
541 585
		}
542 586
	}
543

  
587
	
544 588
	private Integer setInstanceLocation(ChooseWorkspaceData launchData) {
545 589
		String selWS = launchData.getSelection();
546 590
		if (selWS == null) {
547 591
			System.out.println("** No workspace set in configuration scope."); //$NON-NLS-1$
548 592
			return IApplication.EXIT_OK;// user cancelled (no restart)
549 593
		}
550

  
594
		
551 595
		// Switch workspace :
552 596
		URL url = null;
553 597
		try {
......
557 601
			showWorkspaceError("Workspace path could not be found."); //$NON-NLS-1$
558 602
			return IApplication.EXIT_OK;
559 603
		}
560

  
604
		
561 605
		try {
562 606
			if (org.eclipse.core.runtime.Platform.inDevelopmentMode()) {
563 607
				return -1; // can't call Platform.getInstanceLocation().set(...) in dev mode
564 608
			}
565

  
609
			
566 610
			if (!Platform.getInstanceLocation().set(url, true)) {
567 611
				showWorkspaceError("TXM is already running. Exiting..."); //$NON-NLS-1$
568 612
				ChooseWorkspaceData.setShowDialogValue(false);
......
582 626
			ChooseWorkspaceData.setShowDialogValue(true);
583 627
			return IApplication.EXIT_OK;
584 628
		}
585

  
586

  
629
		
630
		
587 631
	}
588

  
632
	
589 633
	// /**
590 634
	// * Open choose workspace dialog and validate the user's choice. Returns true
591 635
	// * if restart needed.
......
638 682
	// ChooseWorkspaceData.setShowDialogValue(false);
639 683
	// return -1;// user cancelled (no restart)
640 684
	// }
641

  
685
	
642 686
	private static void showWorkspaceError(String message) {
643 687
		MessageBox messageBox = new MessageBox(new Shell(), SWT.DIALOG_TRIM | SWT.ERROR);
644 688
		messageBox.setText(TXMUIMessages.TXTStartingError);
645 689
		messageBox.setMessage(message);
646 690
		messageBox.open();
647 691
	}
648

  
692
	
649 693
	public ApplicationWorkbenchAdvisor getApplicationWorkbenchAdvisor() {
650 694
		return awa;
651 695
	}
652

  
696
	
653 697
	@Override
654 698
	public void stop() {
655 699
		// TODO Auto-generated method stub
656

  
700
		
657 701
	}
658

  
702
	
659 703
	/**
660 704
	 * Activates the proxy service and sets the network connection active provider as native.
661 705
	 */
662 706
	private void activateProxyService() {
663 707
		String systemProxyHost = System.getProperty("http.proxyHost"); // must be called before IProxyService is used //$NON-NLS-1$
664

  
708
		
665 709
		Bundle bundle = Platform.getBundle("org.eclipse.ui.ide"); //$NON-NLS-1$
666 710
		IProxyService proxyService = null;
667 711
		if (bundle != null) {
......
679 723
				System.out.println("System proxy detected. Force network provider to manual with proxy_host=" + systemProxyHost); //$NON-NLS-1$
680 724
				proxyService.setProxiesEnabled(true);
681 725
			}
682

  
726
			
683 727
			// if (Util.isMac()) { // configure only for Mac OS X
684 728
			// // String proxy_conf = "";
685 729
			// // ProxyConf conf = new ProxyConf(this);
......
706 750
			// }
707 751
		}
708 752
	}
709

  
753
	
710 754
	public static boolean isHeadLess() {
711 755
		return headlessMode || restMode;
712 756
	}
713

  
757
	
714 758
	// /**
715 759
	// * Parses the command line.
716 760
	// *

Formats disponibles : Unified diff