public code v1
This commit is contained in:
+24
@@ -0,0 +1,24 @@
|
||||
package flintstones.model.method.phase.provider;
|
||||
|
||||
import org.eclipse.e4.core.contexts.ContextFunction;
|
||||
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
|
||||
import org.eclipse.e4.core.contexts.IEclipseContext;
|
||||
import org.eclipse.e4.ui.model.application.MApplication;
|
||||
|
||||
import flintstones.model.method.phase.IPhaseMethodService;
|
||||
|
||||
/**
|
||||
* The Class PhaseMethodServiceContextFunction add the PhaseMethod Service instance to global context.
|
||||
*/
|
||||
public class PhaseMethodServiceContextFunction extends ContextFunction {
|
||||
|
||||
@Override
|
||||
public Object compute(IEclipseContext context, String contextKey) {
|
||||
IPhaseMethodService service = ContextInjectionFactory.make(PhaseMethodServiceProvider.class, context);
|
||||
MApplication application = context.get(MApplication.class);
|
||||
IEclipseContext applicationContext = application.getContext();
|
||||
applicationContext.set(IPhaseMethodService.class, service);
|
||||
return service;
|
||||
}
|
||||
|
||||
}
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
package flintstones.model.method.phase.provider;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.eclipse.e4.core.contexts.IEclipseContext;
|
||||
import org.eclipse.e4.core.services.events.IEventBroker;
|
||||
import org.osgi.service.event.EventHandler;
|
||||
|
||||
import flintstones.entity.method.phase.PhaseMethod;
|
||||
import flintstones.entity.method.phase.ui.PhaseMethodUI;
|
||||
import flintstones.helper.extensionpoint.BaseRegistry;
|
||||
import flintstones.model.method.phase.IPhaseMethodService;
|
||||
|
||||
/**
|
||||
* The Class PhaseMethodServiceProvider.
|
||||
*/
|
||||
class PhaseMethodServiceProvider implements IPhaseMethodService {
|
||||
|
||||
/** The context. */
|
||||
@Inject
|
||||
IEclipseContext context;
|
||||
|
||||
@Inject
|
||||
IEventBroker broker;
|
||||
|
||||
/** The reg. */
|
||||
private BaseRegistry reg;
|
||||
private BaseRegistry uireg;
|
||||
|
||||
/** The PhaseMethod list. */
|
||||
private final LinkedHashMap<String, PhaseMethodUI> phases = new LinkedHashMap<>();
|
||||
|
||||
/**
|
||||
* Instantiates a new phasemethod service provider.
|
||||
*/
|
||||
@Inject
|
||||
public PhaseMethodServiceProvider() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize.
|
||||
*/
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
this.reg = new BaseRegistry(PhaseMethod.EXTENSION_POINT);
|
||||
this.uireg = new BaseRegistry(PhaseMethodUI.EXTENSION_POINT);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see flintstones.phasemethod.service.IPhaseMethodService#getAll()
|
||||
*/
|
||||
@Override
|
||||
public PhaseMethodUI[] getAll() {
|
||||
return this.phases.values().toArray(new PhaseMethodUI[this.phases.values().size()]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PhaseMethodUI[] createPhases(String[] phasesId) {
|
||||
|
||||
// Unsuscribe from event broker so garbage collector can remove the class from memory
|
||||
phases.values() //
|
||||
.stream() //
|
||||
.filter(k -> k instanceof EventHandler ) //
|
||||
.forEach(k -> broker.unsubscribe((EventHandler)k));
|
||||
|
||||
phases.clear();
|
||||
|
||||
PhaseMethodUI[] phasesUI = new PhaseMethodUI[phasesId.length];
|
||||
PhaseMethod phase = null;
|
||||
int x = 0;
|
||||
for (String phaseId : phasesId) {
|
||||
// Create the Phase
|
||||
PhaseMethodUI phaseUI = createPhase(phaseId);
|
||||
phasesUI[x++] = phaseUI;
|
||||
|
||||
phases.put(phaseId, phaseUI);
|
||||
|
||||
// Link phases models
|
||||
if( phase != null )
|
||||
phaseUI.getModel().setPreviousPhase(phase);
|
||||
phase = phaseUI.getModel();
|
||||
|
||||
}
|
||||
|
||||
return phasesUI;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PhaseMethodUI get(String id) {
|
||||
return phases.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Entry<String, PhaseMethodUI> entry : this.phases.entrySet()) {
|
||||
sb.append(entry.getKey() + " ==> " + entry.getValue().getModel().getName() + " | "
|
||||
+ entry.getValue().toString() + "\n");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
this.phases.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PhaseMethodUI createPhase(String phaseId) {
|
||||
|
||||
// Create the model
|
||||
PhaseMethod phase = (PhaseMethod) this.reg.instantiate(phaseId, this.context);
|
||||
phase.setId(phaseId);
|
||||
phase.setPosition(phases.size());
|
||||
|
||||
// Create the Phase UI
|
||||
String phaseUiId = getIdFor(phase.getId());
|
||||
PhaseMethodUI phaseui = (PhaseMethodUI) this.uireg.instantiate(phaseUiId, this.context);
|
||||
phaseui.setModel(phase);
|
||||
|
||||
return phaseui;
|
||||
}
|
||||
|
||||
private String getIdFor(String phaseId) {
|
||||
String[] matches = this.uireg.getAllAttributesWhere(PhaseMethodUI.Fields.phasemethod, phaseId,
|
||||
PhaseMethodUI.Fields.uid);
|
||||
if (matches.length != 1)
|
||||
throw new Error("Encontrados " + matches.length + " " + phaseId + " (Debe ser 1) --> "
|
||||
+ this.uireg.getRegistryId());
|
||||
return this.uireg.getAllAttributesWhere(PhaseMethodUI.Fields.phasemethod, phaseId, PhaseMethodUI.Fields.uid)[0];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user