下面列出了org.eclipse.ui.internal.registry.PerspectiveDescriptor#org.eclipse.e4.ui.workbench.modeling.EModelService 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void cleanPerspectives() {
final EModelService e = PlatformUI.getWorkbench().getService(EModelService.class);
final MApplication a = PlatformUI.getWorkbench().getService(MApplication.class);
final List<PerspectiveImpl> perspectives = e.findElements(a, PerspectiveImpl.class, EModelService.ANYWHERE,
element -> matches(element.getElementId()));
for ( final PerspectiveImpl p : perspectives ) {
// DEBUG.OUT("Dirty perspective implementation found and removed: " + p.getElementId());
p.getParent().getChildren().remove(p);
}
final IPerspectiveRegistry reg = PlatformUI.getWorkbench().getPerspectiveRegistry();
for ( final IPerspectiveDescriptor desc : reg.getPerspectives() ) {
if ( matches(desc.getId()) ) {
// DEBUG.OUT("Dirty perspective descriptor found and removed: " + desc.getId());
reg.deletePerspective(desc);
}
}
// DEBUG.OUT("Current perspectives: " + listCurrentPerspectives());
}
@Inject
@Optional
public void selectedElement(@EventTopic(UIEvents.ElementContainer.TOPIC_SELECTEDELEMENT) Event event, EModelService modelService) {
if (!UIEvents.isSET(event)) {
return;
}
Object newlySelectedPerspective = event.getProperty(EventTags.NEW_VALUE);
if (newlySelectedPerspective instanceof MPerspective) {
MPerspective perspectiveToBeCloned = (MPerspective) newlySelectedPerspective;
MWindow topLevelWindow = modelService.getTopLevelWindowFor(perspectiveToBeCloned);
// try to find already existing snippet
if (null == modelService.findSnippet(topLevelWindow, perspectiveToBeCloned.getElementId())) {
// clone perspective in case there is no snippet yet
modelService.cloneElement(perspectiveToBeCloned, topLevelWindow);
}
}
}
/**
* Find the edit area.
*
* @param w
*
* @return the MArea element containing the editors
*/
protected MUIElement getEditArea(MWindow w) {
// Seems like we should be able to use modelService.find(ID_EDITOR_AREA, w), but that returns a useless PlaceHolder
// NB Selectors aren't supported until Luna
// final Selector match = new Selector() {
// public boolean select(MApplicationElement element) {
// return !modelService.findElements((MUIElement)element, null, MPart.class, EDITOR_TAG, EModelService.IN_ANY_PERSPECTIVE).isEmpty();
// }
// };
// List<MArea> area = modelService.findElements(w, MArea.class, EModelService.IN_SHARED_AREA, match);
List<MArea> area = modelService.findElements(w, null, MArea.class, null, EModelService.IN_SHARED_AREA);
List<MArea> refined = new ArrayList<MArea>();
if (area != null) {
for (MArea m : area) {
if (!modelService.findElements(m, null, MPart.class, EDITOR_TAG, EModelService.IN_ANY_PERSPECTIVE).isEmpty()) {
refined.add(m);
}
}
}
return refined.isEmpty() ? null : refined.get(0);
}
/**
* Get the list of detached windows, if any
*
* NB: The docs don't guarantee a non-null return, but the implementation seems to
* Nor do they guarantee an order, but the implementation currently returns the same order
* @return the list of detached windows
*/
List<MTrimmedWindow> getDetachedFrames() {
final MWindow topWindow = application.getChildren().get(0);
// NB Selectors aren't supported until Luna
// final Selector match = new Selector() {
// public boolean select(MApplicationElement element) {
// boolean result = element != topWindow && ((MTrimmedWindow)element).isToBeRendered();
// return result && !modelService.findElements((MUIElement)element, null, MPart.class, EDITOR_TAG, EModelService.IN_ANY_PERSPECTIVE).isEmpty();
// }
// };
// List<MTrimmedWindow> mts = modelService.findElements(topWindow, MTrimmedWindow.class, EModelService.IN_ANY_PERSPECTIVE, match);
// get the all detached editor trimmed windows
// the implementation searches all detached windows in this case
List<MTrimmedWindow> mts = modelService.findElements(topWindow, null, MTrimmedWindow.class, null, EModelService.IN_ANY_PERSPECTIVE);
List<MTrimmedWindow> refined = new ArrayList<MTrimmedWindow>();
for (MTrimmedWindow mt : mts) {
if (mt != topWindow && mt.isToBeRendered() && !modelService.findElements(mt, null, MPart.class, EDITOR_TAG, EModelService.IN_ANY_PERSPECTIVE).isEmpty()) {
refined.add(mt);
}
}
return refined;
}
/**
* @see com.mulgasoft.emacsplus.e4.commands.E4WindowCmd#getAdjacentElement(org.eclipse.e4.ui.model.application.ui.MElementContainer, boolean, org.eclipse.e4.ui.model.application.ui.basic.MPart)
*/
protected MElementContainer<MUIElement> getAdjacentElement(MElementContainer<MUIElement> dragStack, MPart part, boolean stackp) {
MElementContainer<MUIElement> result = null;
if (dragStack != null) {
MElementContainer<MUIElement> psash = dragStack.getParent();
MElementContainer<MUIElement> top = getTopElement(psash);
if ((Object)top instanceof MTrimmedWindow) {
// if we contain splits, remove them first
if (top != psash) {
super.joinAll(part);
}
Collection<MPart> parts = getParts(application.getChildren().get(0), EModelService.IN_SHARED_AREA);
for (MPart p : parts) {
List<MElementContainer<MUIElement>> all = getOrderedStacks(p);
// if it has a PartStack, it sh/c/ould be an editor stack
if (!all.isEmpty()) {
result = all.get(0);
break;
};
};
}
}
return result;
}
@Override
public void handleEvent(Event event){
Object property = event.getProperty("org.eclipse.e4.data");
if (property instanceof MApplication) {
MApplication application = (MApplication) property;
EModelService modelService = application.getContext().get(EModelService.class);
UiDesk.asyncExec(new Runnable() {
public void run(){
addMandantSelectionItem(application, modelService);
ElexisFastViewUtil.registerPerspectiveListener();
}
});
}
}
private static MPartStack createFastViewStack(MTrimmedWindow window, MPerspective mPerspective,
EModelService eModelService){
if (window != null && mPerspective != null) {
MPartStack mPartStack = eModelService.createModelElement(MPartStack.class);
mPartStack.setElementId(ELEXIS_FASTVIEW_STACK);
mPartStack.setToBeRendered(true);
mPartStack.getTags().add("Minimized");
mPartStack.setOnTop(false);
mPartStack.setVisible(false);
mPartStack.getTags().add("NoAutoCollapse");
mPartStack.getTags().add("active");
mPerspective.getChildren().add(0, mPartStack);
return mPartStack;
}
return null;
}
private static MToolControl createFastViewToolControl(MTrimmedWindow window,
MPerspective mPerspective, EModelService eModelService, MTrimBar mTrimBar){
if (mTrimBar != null) {
MToolControl mToolControl = eModelService.createModelElement(MToolControl.class);
mToolControl.setElementId(getToolControlId(window, mPerspective));
mToolControl.setContributionURI(
"bundleclass://org.eclipse.e4.ui.workbench.addons.swt/org.eclipse.e4.ui.workbench.addons.minmax.TrimStack");
mToolControl.setToBeRendered(true);
mToolControl.setVisible(true);
mToolControl.getTags().add("TrimStack");
if (!hasFastViewPersistedState(mPerspective)) {
mToolControl.getPersistedState().put("YSize", "600");
}
mTrimBar.getChildren().add(0, mToolControl);
mTrimBar.setVisible(true);
mTrimBar.setToBeRendered(true);
return mToolControl;
}
return null;
}
private static Optional<MToolControl> getFastViewToolControl(EModelService eModelService,
MTrimmedWindow workbenchWindow, String perspectiveId, SideValue sideValue){
if (workbenchWindow != null) {
MTrimBar trimbar = findTrimBar(eModelService, workbenchWindow, sideValue);
if (trimbar != null) {
MToolControl toolControl = (MToolControl) eModelService
.find(getToolControlId(workbenchWindow, perspectiveId), trimbar);
if (toolControl == null && workbenchWindow.getElementId() != null) {
// it also can be that the main view id is also a part of the stack
toolControl = (MToolControl) eModelService.find(ELEXIS_FASTVIEW_STACK + "("
+ workbenchWindow.getElementId() + ").(" + perspectiveId + ")", trimbar);
if (toolControl != null) {
toolControl.setElementId(getToolControlId(workbenchWindow, perspectiveId));
}
}
if (toolControl != null) {
return Optional.of(toolControl);
}
}
}
return Optional.empty();
}
/**
* Transfer persisted state (incl. size) of fastview {@link MToolControl} defined in window, to
* fastview defined in perspective.
*
* @param fromWindow
* @param toPerspective
*/
public static void transferFastViewPersistedState(MTrimmedWindow fromWindow,
MPerspective toPerspective){
EModelService modelService = getService(EModelService.class);
// check if toolcontrol exists
MToolControl toolControl = (MToolControl) modelService
.find(ElexisFastViewUtil.getToolControlId(fromWindow, toPerspective), fromWindow);
if (toolControl != null && toolControl.getPersistedState() != null) {
Optional<MPartStack> mStack = getFastViewPartStack(toPerspective);
mStack.ifPresent(stack -> {
Map<String, String> perspectiveState = stack.getPersistedState();
for (String key : toolControl.getPersistedState().keySet()) {
perspectiveState.put(key, toolControl.getPersistedState().get(key));
}
});
}
}
/**
* Transfer persisted state (incl. size) of fastview defined in perspective, to fastview
* {@link MToolControl} defined in window.
*
* @param fromPerspective
* @param toWindow
*/
public static void transferFastViewPersistedState(MPerspective fromPerspective,
MTrimmedWindow toWindow){
EModelService modelService = getService(EModelService.class);
String perspectiveId = fromPerspective.getElementId();
Optional<MToolControl> mToolControl =
getFastViewToolControl(modelService, toWindow, perspectiveId, SideValue.BOTTOM);
mToolControl.ifPresent(toolControl -> {
Optional<MPartStack> mStack = getFastViewPartStack(fromPerspective);
mStack.ifPresent(stack -> {
if (stack.getPersistedState() != null && !stack.getPersistedState().isEmpty()) {
for (String key : stack.getPersistedState().keySet()) {
toolControl.getPersistedState().put(key,
stack.getPersistedState().get(key));
}
}
});
});
}
private MPerspectiveStack getPerspectiveStack(){
EModelService modelService = getService(EModelService.class);
MWindow window = getActiveWindow();
List<MPerspectiveStack> theStack =
modelService.findElements(window, null, MPerspectiveStack.class, null);
if (theStack.size() > 0) {
return theStack.get(0);
}
for (MWindowElement child : window.getChildren()) {
if (child instanceof MPerspectiveStack) {
return (MPerspectiveStack) child;
}
}
return null;
}
public static void deletePerspectiveFromApplication(final IPerspectiveDescriptor d) {
final MApplication a = PlatformUI.getWorkbench().getService(MApplication.class);
final EModelService e = PlatformUI.getWorkbench().getService(EModelService.class);
final List<PerspectiveImpl> perspectives = e.findElements(a, PerspectiveImpl.class, EModelService.ANYWHERE,
element -> element.getElementId().contains(d.getId()));
for ( final PerspectiveImpl p : perspectives ) {
// DEBUG.OUT("Dirty perspective implementation found and removed: " + p.getElementId());
p.getParent().getChildren().remove(p);
}
}
@Execute
public void execute(MApplication app, EPartService partService,
EModelService modelService) {
MPerspective element = (MPerspective) modelService.find(PART_ID, app);
if (element != null) {
partService.switchPerspective(element);
logger.trace("Switch to " + PART_ID);
}
}
@Execute
public void execute(MApplication app, EPartService partService,
EModelService modelService) {
MPerspective element = (MPerspective) modelService.find(PART_ID, app);
partService.switchPerspective(element);
logger.trace("Switch to " + PART_ID);
}
@Execute
public void execute(EModelService modelService, MWindow window) {
// clone the currently active perspective
MPerspective activePerspective = modelService
.getActivePerspective(window);
modelService.cloneElement(activePerspective, window);
}
@Execute
public void execute(EModelService modelService, EPartService partService,
MWindow window,
@Named("com.vogella.rcp.jface.translation.commandparameter.perspectiveid") String id) {
MPerspective activePerspective = modelService
.getActivePerspective(window);
MUIElement find = modelService.find(id, window);
if (find == null || activePerspective.equals(find)) {
return;
}
partService.switchPerspective((MPerspective) find);
}
@Execute
public void execute(EModelService modelService, MWindow window, EPartService partService) {
// get active perpective and find stored snippet of this perspective
MPerspective activePerspective = modelService.getActivePerspective(window);
MUIElement perspectiveSnippet = modelService.cloneSnippet(window, activePerspective.getElementId(), window);
// remove existing active perspective
MElementContainer<MUIElement> parent = activePerspective.getParent();
modelService.removePerspectiveModel(activePerspective, window);
// add stored perspective snippet and switch to it
parent.getChildren().add(perspectiveSnippet);
partService.switchPerspective((MPerspective) perspectiveSnippet);
}
@CanExecute
public boolean canExecute(EModelService modelService, MWindow window) {
// check whether a snippet for the active perspective exists
MPerspective activePerspective = modelService.getActivePerspective(window);
return modelService.findSnippet(window, activePerspective.getElementId()) != null;
}
@PostConstruct
public void createComposite(Composite parent, IPresentationEngine presentationEngine, EModelService modelService, MApplication app, IEclipseContext context) {
List<MTrimContribution> trimContributions = app.getTrimContributions();
Optional<MTrimElement> findAny = trimContributions.stream().flatMap(tbc -> tbc.getChildren().stream()).filter(tbe -> "com.vogella.e4.renderer.toolbar".equals(tbe.getElementId())).findAny();
findAny.ifPresent(uiElement -> {
ToolBar toolBar = new ToolBar(parent, SWT.NONE);
presentationEngine.createGui(uiElement, toolBar, context);
});
}
void joinOne(MTrimmedWindow mt) {
// As long as it has any editor MParts, we can join it
List<MPart> parts = getParts(mt, EModelService.IN_ANY_PERSPECTIVE);
if (!parts.isEmpty()) {
super.joinOne(parts.get(0));
}
}
protected String getActivePerspectiveId(final MPart part) {
if (part != null && part.getContext() != null) {
final EModelService service = part.getContext().get(EModelService.class);
final MWindow window = service.getTopLevelWindowFor(part);
String activePerspective = null;
final MPerspective selectedElement = service.getActivePerspective(window);
if (selectedElement != null) {
activePerspective = selectedElement.getElementId();
}
return activePerspective;
}
return null;
}
public Object execute(ExecutionEvent event) throws ExecutionException {
MWindow model = ((WorkbenchWindow) PlatformUI.getWorkbench().getActiveWorkbenchWindow()).getModel();
EModelService modelService = model.getContext().get(EModelService.class);
MToolControl bonitaCoolBar = (MToolControl) modelService.find(
"BonitaCoolbar", model);
if(bonitaCoolBar != null){
CoolbarToolControl coolbarControl = (CoolbarToolControl) bonitaCoolBar.getObject();
coolbarControl.maximizeCoolbar();
}
return null;
}
public Object execute(ExecutionEvent event) throws ExecutionException {
MWindow model = ((WorkbenchWindow) PlatformUI.getWorkbench().getActiveWorkbenchWindow()).getModel();
EModelService modelService = model.getContext().get(EModelService.class);
MToolControl bonitaCoolBar = (MToolControl) modelService.find(
"BonitaCoolbar", model);
if(bonitaCoolBar != null){
CoolbarToolControl coolbarControl = (CoolbarToolControl) bonitaCoolBar.getObject();
coolbarControl.minimizeCoolbar();
}
return null;
}
private void addMandantSelectionItem(MApplication mApplication, EModelService eModelService){
MTrimBar trimbar =
(MTrimBar) eModelService.find("org.eclipse.ui.main.toolbar", mApplication);
if (trimbar != null) {
MTrimElement mTrimElement = null;
int position = 0;
int i = 0;
List<MTrimElement> childrens = trimbar.getChildren();
for (MTrimElement element : childrens) {
if ("ch.elexis.core.ui.toolcontrol.mandantselection"
.equals(element.getElementId())) {
mTrimElement = element;
}
if (position == 0 && ("ch.elexis.toolbar1".equals(element.getElementId())
|| "PerspectiveSpacer".equals(element.getElementId()))) {
position = i;
}
i++;
}
if (mTrimElement == null) {
MToolControl mToolControl = eModelService.createModelElement(MToolControl.class);
mToolControl.setElementId("ch.elexis.core.ui.toolcontrol.mandantselection");
mToolControl.setContributionURI(
"bundleclass://ch.elexis.core.ui/ch.elexis.core.ui.coolbar.MandantSelectionContributionItem");
mToolControl.setToBeRendered(true);
mToolControl.setVisible(true);
childrens.add(position, mToolControl);
}
}
}
private static MTrimmedWindow getCurrentWindow(EModelService eModelService,
MApplication mApplication){
MTrimmedWindow window = (MTrimmedWindow) eModelService.find("IDEWindow", mApplication);
if (window == null) {
List<MWindow> windows = mApplication.getChildren();
if (!windows.isEmpty() && windows.get(0) instanceof MTrimmedWindow) {
window = (MTrimmedWindow) windows.get(0);
}
}
return window;
}
private static boolean hasFastViewPersistedState(MTrimmedWindow mWindow, String perspectiveId){
EModelService eModelService = getService(EModelService.class);
Optional<MToolControl> mToolControl =
getFastViewToolControl(eModelService, mWindow, perspectiveId, SideValue.BOTTOM);
if (mToolControl.isPresent()) {
Map<String, String> persistedState = mToolControl.get().getPersistedState();
return persistedState != null && !persistedState.isEmpty();
}
return false;
}
private static Optional<MPartStack> getFastViewPartStack(MPerspective mPerspective){
EModelService modelService = getService(EModelService.class);
MPartStack stack =
(MPartStack) modelService.find(ElexisFastViewUtil.ELEXIS_FASTVIEW_STACK, mPerspective);
if (stack != null) {
return Optional.of(stack);
}
return Optional.empty();
}
private static MTrimBar findTrimBar(EModelService eModelService, MTrimmedWindow workbenchWindow,
SideValue sideValue){
if (workbenchWindow != null) {
MTrimBar trimbar = eModelService.getTrim(workbenchWindow, sideValue);
return trimbar;
}
return null;
}
@Execute
public void execute(MApplication mApplication, EModelService eModelService){
this.mApplication = mApplication;
this.eModelService = eModelService;
if (eModelService != null) {
updateModelVersions(mApplication, eModelService);
}
updateToolbar();
updateInjectViews();
updateE4Views();
}