下面列出了怎么用org.eclipse.ui.IPerspectiveRegistry的API类实例代码及写法,或者点击链接到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());
}
/**
* Switch to the perspective with id given as parameter
*
* @param perspectiveID
*/
public static synchronized void switchToPerspective(final String perspectiveID) {
final IWorkbench workbench = PlatformUI.getWorkbench();
final IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
if (window != null) {
final IWorkbenchPage activePage = window.getActivePage();
if (activePage != null) {
final IPerspectiveDescriptor activePerspective = activePage.getPerspective();
if (activePerspective == null || !activePerspective.getId().equals(perspectiveID)) {
final IPerspectiveRegistry registry = workbench.getPerspectiveRegistry();
final IWorkbenchPage page = window.getActivePage();
final IPerspectiveDescriptor desc = registry.findPerspectiveWithId(perspectiveID);
page.setPerspective(desc);
}
}
}
}
@SuppressWarnings("restriction")
private IPerspectiveDescriptor savePerspectiveToRegistryLegacy(MPerspective perspective){
IPerspectiveRegistry perspectiveRegistry =
(PerspectiveRegistry) PlatformUI.getWorkbench().getPerspectiveRegistry();
IPerspectiveDescriptor pd =
perspectiveRegistry.findPerspectiveWithId(perspective.getElementId());
if (pd == null) {
((PerspectiveRegistry) perspectiveRegistry).addPerspective(perspective);
pd = perspectiveRegistry.findPerspectiveWithId(perspective.getElementId());
} else {
LoggerFactory.getLogger(PerspektiveImportHandler.class)
.error("perspective descriptor already exists for perspective id: "
+ perspective.getElementId());
}
return pd;
}
@Override
public int deletePerspective(String perspectiveId){
IPerspectiveRegistry iPerspectiveRegistry =
PlatformUI.getWorkbench().getPerspectiveRegistry();
MApplication mApplication = getService(MApplication.class);
IPerspectiveDescriptor existingPerspectiveDescriptor =
iPerspectiveRegistry.findPerspectiveWithId(perspectiveId);
int idx = -1;
if (existingPerspectiveDescriptor != null) {
idx = closePerspective(existingPerspectiveDescriptor);
//NOT WORKING IF PERSPECTIVE IS PREDEFINED - workaround with generics
iPerspectiveRegistry.deletePerspective(existingPerspectiveDescriptor);
PerspectiveImportService.genericInvokMethod(iPerspectiveRegistry, "removeSnippet",
MSnippetContainer.class, String.class, mApplication,
existingPerspectiveDescriptor.getId());
}
return idx;
}
/**
* Adds to the list all perspective IDs in the Workbench who's original ID
* matches the given ID.
*
* @param perspectiveIds
* the list of perspective IDs to supplement.
* @param id
* the id to query.
* @since 3.0
*/
private static void addPerspectiveAndDescendants(List perspectiveIds, String id) {
IPerspectiveRegistry registry = PlatformUI.getWorkbench().getPerspectiveRegistry();
IPerspectiveDescriptor[] perspectives = registry.getPerspectives();
for (int i = 0; i < perspectives.length; i++) {
// @issue illegal ref to workbench internal class;
// consider adding getOriginalId() as API on IPerspectiveDescriptor
PerspectiveDescriptor descriptor = ((PerspectiveDescriptor) perspectives[i]);
if (descriptor.getOriginalId().equals(id)) {
perspectiveIds.add(descriptor.getId());
}
}
}
public static void switchToModelingPerspective(IWorkbenchWindow window) {
IPreferenceStore prefs = UIPluginActivator.getDefault()
.getPreferenceStore();
boolean hide = prefs.getBoolean(AUTO_SWITCH_PERSPECTIVE);
IWorkbenchPage page = window.getActivePage();
if (!hide) {
IWorkbench workbench = window.getWorkbench();
IPerspectiveRegistry registry = workbench.getPerspectiveRegistry();
IPerspectiveDescriptor descriptor = registry
.findPerspectiveWithId(IYakinduSctPerspectives.ID_PERSPECTIVE_SCT_MODELING);
if ((page != null) && (page.getPerspective() != descriptor)) {
MessageDialogWithToggle dialog = MessageDialogWithToggle
.openYesNoQuestion(
window.getShell(),
"Confirm Perspective Switch",
"This kind of editor is associated with the YAKINDU Modeling perspective. Do you want to switch to this perspective now?",
"Do not offer to switch perspective in the future",
hide, prefs, AUTO_SWITCH_PERSPECTIVE);
if (dialog.getReturnCode() == 2)
page.setPerspective(descriptor);
hide = dialog.getToggleState();
prefs.setValue(AUTO_SWITCH_PERSPECTIVE, hide);
try {
InstanceScope.INSTANCE.getNode(UIPluginActivator.PLUGIN_ID)
.flush();
} catch (BackingStoreException e) {
e.printStackTrace();
}
}
}
}
@Override
public String getInitialWindowPerspectiveId(){
String initPerspective = CoreOperationAdvisorHolder.get().getInitialPerspective();
// avoid that nothing opens up after login in case the stored perspective can't be found
IPerspectiveRegistry perspectiveRegistry =
PlatformUI.getWorkbench().getPerspectiveRegistry();
if (perspectiveRegistry.findPerspectiveWithId(initPerspective) == null) {
initPerspective = UiResourceConstants.PatientPerspektive_ID;
}
return initPerspective;
}
public static IPerspectiveRegistry getPerspectiveRegistry() {
return PlatformUI.getWorkbench().getPerspectiveRegistry();
}
@SuppressWarnings("restriction")
private IPerspectiveDescriptor importPerspectiveFromStream(InputStream in, IStateCallback iStateHandle,
boolean openPerspectiveIfAdded) throws IOException{
MPerspective mPerspective = loadPerspectiveFromStream(in);
if (mPerspective != null) {
IPerspectiveRegistry iPerspectiveRegistry =
PlatformUI.getWorkbench().getPerspectiveRegistry();
// the perspective id to import
String id = mPerspective.getElementId();
IPerspectiveDescriptor existingPerspectiveDescriptor =
iPerspectiveRegistry.findPerspectiveWithId(id);
// the active perspective id
String activePerspectiveId = getActivePerspectiveId();
// check if the import should be done
if (existingPerspectiveDescriptor == null || iStateHandle == null
|| iStateHandle.state(State.OVERRIDE)) {
IPerspectiveDescriptor activePd =
iPerspectiveRegistry.findPerspectiveWithId(activePerspectiveId);
// delete if a perspective with the id already exists
int idx = deletePerspective(id);
// add the new perspective to the registry
((PerspectiveRegistry) iPerspectiveRegistry).addPerspective(mPerspective);
IPerspectiveDescriptor createdPd = iPerspectiveRegistry.findPerspectiveWithId(id);
if (createdPd != null) {
((PerspectiveDescriptor) createdPd).setHasCustomDefinition(false); //no original descriptor should exists
}
// check if the new perspective should be opened
if (idx > -1 || openPerspectiveIfAdded) {
openPerspective(createdPd);
// there was already an opened active perspective switch back to it
openPerspective(activePd);
}
return createdPd;
}
}
return null;
}
public int isPerspectiveInStack(String perspectiveId){
IPerspectiveRegistry iPerspectiveRegistry =
PlatformUI.getWorkbench().getPerspectiveRegistry();
return isPerspectiveInsideStack(iPerspectiveRegistry.findPerspectiveWithId(perspectiveId));
}