2015-10-22 00:05:29 +02:00
|
|
|
/*******************************************************************************
|
2015-10-22 00:25:12 +02:00
|
|
|
* Copyright (c) 2015 MINRES Technologies GmbH and others.
|
2015-10-22 00:05:29 +02:00
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
*
|
|
|
|
* Contributors:
|
2015-10-22 00:25:12 +02:00
|
|
|
* MINRES Technologies GmbH - initial API and implementation
|
2015-10-22 00:05:29 +02:00
|
|
|
*******************************************************************************/
|
|
|
|
package com.minres.scviewer.e4.application;
|
|
|
|
|
2015-10-27 23:39:33 +01:00
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
|
2015-10-22 00:05:29 +02:00
|
|
|
import org.eclipse.e4.core.contexts.IEclipseContext;
|
2015-10-27 23:39:33 +01:00
|
|
|
import org.eclipse.e4.core.services.events.IEventBroker;
|
|
|
|
import org.eclipse.e4.ui.model.application.MApplication;
|
|
|
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
|
|
|
import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
|
|
|
|
import org.eclipse.e4.ui.workbench.UIEvents;
|
2015-10-22 00:05:29 +02:00
|
|
|
import org.eclipse.e4.ui.workbench.lifecycle.PostContextCreate;
|
|
|
|
import org.eclipse.e4.ui.workbench.lifecycle.PreSave;
|
|
|
|
import org.eclipse.e4.ui.workbench.lifecycle.ProcessAdditions;
|
|
|
|
import org.eclipse.e4.ui.workbench.lifecycle.ProcessRemovals;
|
2015-10-27 23:39:33 +01:00
|
|
|
import org.eclipse.e4.ui.workbench.modeling.EModelService;
|
|
|
|
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
|
|
|
import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
|
|
|
|
import org.eclipse.equinox.app.IApplicationContext;
|
|
|
|
import org.osgi.service.event.Event;
|
|
|
|
import org.osgi.service.event.EventHandler;
|
2015-10-22 00:05:29 +02:00
|
|
|
|
2018-10-14 20:44:17 +02:00
|
|
|
import com.minres.scviewer.e4.application.options.Options;
|
|
|
|
import com.minres.scviewer.e4.application.options.Options.Multiplicity;
|
|
|
|
import com.minres.scviewer.e4.application.options.Options.Separator;
|
|
|
|
|
2015-10-22 00:05:29 +02:00
|
|
|
/**
|
2015-11-22 12:47:07 +01:00
|
|
|
* This implementation contains e4 LifeCycle annotated methods.<br />
|
2015-10-22 00:05:29 +02:00
|
|
|
* There is a corresponding entry in <em>plugin.xml</em> (under the
|
|
|
|
* <em>org.eclipse.core.runtime.products' extension point</em>) that references
|
|
|
|
* this class.
|
|
|
|
**/
|
|
|
|
@SuppressWarnings("restriction")
|
|
|
|
public class E4LifeCycle {
|
|
|
|
|
2015-11-22 12:47:07 +01:00
|
|
|
/**
|
|
|
|
* Post construct.
|
|
|
|
*
|
|
|
|
* @param eventBroker the event broker
|
|
|
|
*/
|
2015-10-27 23:39:33 +01:00
|
|
|
@PostConstruct
|
|
|
|
private static void postConstruct(final IEventBroker eventBroker) {
|
2015-10-22 00:05:29 +02:00
|
|
|
}
|
|
|
|
|
2015-11-22 12:47:07 +01:00
|
|
|
/**
|
|
|
|
* Post context create. Open a database if given on command line using the OpenViewHandler
|
|
|
|
*
|
|
|
|
* @param appContext the app context
|
|
|
|
* @param eventBroker the event broker
|
|
|
|
*/
|
2015-10-27 23:39:33 +01:00
|
|
|
@PostContextCreate
|
|
|
|
void postContextCreate(IApplicationContext appContext, final IEventBroker eventBroker) {
|
2018-10-14 20:44:17 +02:00
|
|
|
final String[] args = (String[])appContext.getArguments().get(IApplicationContext.APPLICATION_ARGS);
|
|
|
|
Options opt = new Options(args, 1);
|
|
|
|
opt.getSet().addOption("c", Separator.BLANK, Multiplicity.ONCE);
|
|
|
|
if (!opt.check(Options.DEFAULT_SET, true, true)) {
|
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
final String confFile =opt.getSet().isSet("c")?opt.getSet().getOption("c").getResultValue(0):"";
|
|
|
|
|
2015-10-27 23:39:33 +01:00
|
|
|
// react on the first view being created, at that time the UI is available
|
|
|
|
eventBroker.subscribe(UIEvents.UILifeCycle.ACTIVATE, new EventHandler() {
|
|
|
|
@Override
|
|
|
|
public void handleEvent(Event event) {
|
2017-01-23 22:53:14 +01:00
|
|
|
MPart part = (MPart) event.getProperty("ChangedElement"); //$NON-NLS-1$
|
2015-10-27 23:39:33 +01:00
|
|
|
if(part!=null){
|
|
|
|
IEclipseContext ctx = part.getContext();
|
|
|
|
OpenViewHandler openViewHandler= new OpenViewHandler();
|
2018-10-14 20:44:17 +02:00
|
|
|
if(confFile.length()>0) openViewHandler.setConfigFile(confFile);
|
2015-10-27 23:39:33 +01:00
|
|
|
ContextInjectionFactory.inject(openViewHandler, ctx);
|
|
|
|
eventBroker.unsubscribe(this);
|
2018-10-14 20:44:17 +02:00
|
|
|
for(String name:opt.getSet().getData()){
|
2015-10-27 23:39:33 +01:00
|
|
|
if(new File(name).exists()) openViewHandler.openViewForFile(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-11-22 12:47:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pre save.
|
|
|
|
*
|
|
|
|
* @param workbenchContext the workbench context
|
|
|
|
*/
|
2015-10-22 00:05:29 +02:00
|
|
|
@PreSave
|
|
|
|
void preSave(IEclipseContext workbenchContext) {
|
|
|
|
}
|
|
|
|
|
2015-11-22 12:47:07 +01:00
|
|
|
/**
|
|
|
|
* Process additions.
|
|
|
|
*
|
|
|
|
* @param workbenchContext the workbench context
|
|
|
|
*/
|
2015-10-22 00:05:29 +02:00
|
|
|
@ProcessAdditions
|
|
|
|
void processAdditions(IEclipseContext workbenchContext) {
|
|
|
|
}
|
|
|
|
|
2015-11-22 12:47:07 +01:00
|
|
|
/**
|
|
|
|
* Process removals.
|
|
|
|
*
|
|
|
|
* @param workbenchContext the workbench context
|
|
|
|
*/
|
2015-10-22 00:05:29 +02:00
|
|
|
@ProcessRemovals
|
|
|
|
void processRemovals(IEclipseContext workbenchContext) {
|
|
|
|
}
|
2015-10-27 23:39:33 +01:00
|
|
|
|
2015-11-22 12:47:07 +01:00
|
|
|
/**
|
|
|
|
* Join.
|
|
|
|
*
|
|
|
|
* @param tokens the tokens
|
|
|
|
* @return the string
|
|
|
|
*/
|
2015-10-27 23:39:33 +01:00
|
|
|
String join(String[] tokens){
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
boolean first=true;
|
|
|
|
for(String token:tokens){
|
2017-01-23 22:53:14 +01:00
|
|
|
if(!first) sb.append(","); //$NON-NLS-1$
|
2015-10-27 23:39:33 +01:00
|
|
|
sb.append(token);
|
|
|
|
first=false;
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
2015-11-22 12:47:07 +01:00
|
|
|
/**
|
|
|
|
* The Class OpenViewHandler.
|
|
|
|
*/
|
2015-10-27 23:39:33 +01:00
|
|
|
private class OpenViewHandler {
|
2015-11-22 12:47:07 +01:00
|
|
|
|
|
|
|
/** The app. */
|
2015-10-27 23:39:33 +01:00
|
|
|
@Inject MApplication app;
|
2015-11-22 12:47:07 +01:00
|
|
|
|
|
|
|
/** The model service. */
|
2015-10-27 23:39:33 +01:00
|
|
|
@Inject EModelService modelService;
|
2015-11-22 12:47:07 +01:00
|
|
|
|
|
|
|
/** The part service. */
|
2015-10-27 23:39:33 +01:00
|
|
|
@Inject EPartService partService;
|
2015-11-22 12:47:07 +01:00
|
|
|
|
2018-10-14 20:44:17 +02:00
|
|
|
String confFile="";
|
2015-11-22 12:47:07 +01:00
|
|
|
/**
|
|
|
|
* Open view for file.
|
|
|
|
*
|
|
|
|
* @param name the name
|
|
|
|
*/
|
2015-10-27 23:39:33 +01:00
|
|
|
public void openViewForFile(String name){
|
2015-11-03 22:29:42 +01:00
|
|
|
File file = new File(name);
|
2017-01-23 22:53:14 +01:00
|
|
|
MPart part = partService.createPart("com.minres.scviewer.e4.application.partdescriptor.waveformviewer"); //$NON-NLS-1$
|
2015-11-03 22:29:42 +01:00
|
|
|
part.setLabel(file.getName());
|
2017-01-23 22:53:14 +01:00
|
|
|
MPartStack partStack = (MPartStack)modelService.find("org.eclipse.editorss", app); //$NON-NLS-1$
|
2015-10-27 23:39:33 +01:00
|
|
|
partStack.getChildren().add(part);
|
|
|
|
partService.showPart(part, PartState.ACTIVATE);
|
|
|
|
IEclipseContext ctx=part.getContext();
|
2017-01-23 22:53:14 +01:00
|
|
|
ctx.modify("input", file); //$NON-NLS-1$
|
2018-10-14 20:44:17 +02:00
|
|
|
//ctx.declareModifiable("input"); //$NON-NLS-1$
|
|
|
|
ctx.modify("config", confFile); //$NON-NLS-1$
|
|
|
|
//ctx.declareModifiable("config"); //$NON-NLS-1$
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setConfigFile(String confFile) {
|
|
|
|
this.confFile=confFile;
|
2015-10-27 23:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
2015-10-22 00:05:29 +02:00
|
|
|
}
|