use of org.eclipse.ui.internal.WorkbenchPage in project translationstudio8 by heartsome.
the class ApplicationWorkbenchWindowAdvisor method restorEditorHistory.
/**
* 重新恢复产品上次退时出所保存的编辑器,此修复针对 mac 下的 bug 2998 启动:某客户安装的软件只能使用一次. robert 2013-05-21
*/
private void restorEditorHistory() {
// 只针对 mac 下的用户
if (System.getProperty("os.name").indexOf("Mac") == -1) {
return;
}
final WorkbenchPage page = (WorkbenchPage) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.beginTask("", 10);
String tempEditorHistoryLC = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(Constant.TEMP_EDITOR_HISTORY).toOSString();
File tempEditorHistoryFile = new File(tempEditorHistoryLC);
if (!tempEditorHistoryFile.exists()) {
return;
}
monitor.worked(1);
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
VTDGen vg = new VTDGen();
try {
boolean parseResult = vg.parseFile(tempEditorHistoryLC, true);
if (!parseResult) {
return;
}
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
int storeFileSum = 0;
ap.selectXPath("count(/editors/editor)");
storeFileSum = (int) ap.evalXPathToNumber();
IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 9, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
subMonitor.beginTask("", storeFileSum);
ap.selectXPath("/editors/editor");
String editorId = null;
String localFilePath = null;
boolean activate = false;
IEditorReference activateEditorRefe = null;
IEditorReference firstEditor = null;
IFile curIFile = null;
int index = -1;
while (ap.evalXPath() != -1) {
activate = false;
editorId = null;
localFilePath = null;
if ((index = vn.getAttrVal("id")) != -1) {
editorId = vn.toRawString(index);
}
if ((index = vn.getAttrVal("path")) != -1) {
localFilePath = vn.toRawString(index);
}
if (editorId == null || editorId.trim().length() <= 0 || localFilePath == null || localFilePath.trim().length() <= 0) {
continue;
}
if ((index = vn.getAttrVal("active")) != -1) {
if ("true".equals(vn.toRawString(index))) {
activate = true;
}
}
curIFile = root.getFileForLocation(new Path(localFilePath));
if (!curIFile.exists()) {
subMonitor.worked(1);
continue;
}
if (activate) {
activateEditorRefe = page.getEditorManager().openEditor(editorId, new FileEditorInput(curIFile), false, null);
PlatformUI.getWorkbench().getActiveWorkbenchWindow().setActivePage(page);
} else {
if (firstEditor == null) {
firstEditor = page.getEditorManager().openEditor(editorId, new FileEditorInput(curIFile), false, null);
} else {
page.getEditorManager().openEditor(editorId, new FileEditorInput(curIFile), false, null);
}
PlatformUI.getWorkbench().getActiveWorkbenchWindow().setActivePage(page);
}
subMonitor.worked(1);
}
PlatformUI.getWorkbench().getActiveWorkbenchWindow().setActivePage(page);
if (activateEditorRefe != null) {
if (firstEditor != null) {
page.activate(firstEditor.getEditor(true));
}
page.activate(activateEditorRefe.getEditor(true));
}
subMonitor.done();
monitor.done();
} catch (Exception e) {
LOGGER.error("restore editor file error", e);
}
}
};
try {
new ProgressMonitorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()).run(true, true, runnable);
} catch (Exception e) {
LOGGER.error("restore editor file error", e);
}
}
use of org.eclipse.ui.internal.WorkbenchPage in project netxms by netxms.
the class ApplicationActionBarAdvisor method exportPerspective.
/**
* Export perspective
* @param window
*/
private void exportPerspective(IWorkbenchWindow window) {
try {
SelectPerspectiveDialog dlg = new SelectPerspectiveDialog(window.getShell(), window.getWorkbench().getPerspectiveRegistry());
if (dlg.open() == Window.OK) {
WorkbenchPage page = (WorkbenchPage) window.getActivePage();
Perspective p = page.findPerspective(dlg.getSelection());
// $NON-NLS-1$
final XMLMemento memento = XMLMemento.createWriteRoot("perspective");
p.saveState(memento);
FileDialog fd = new FileDialog(window.getShell());
// $NON-NLS-1$ //$NON-NLS-2$
fd.setFilterExtensions(new String[] { "*.xml", "*.*" });
fd.setFilterNames(new String[] { Messages.get().ApplicationActionBarAdvisor_XMLFiles, Messages.get().ApplicationActionBarAdvisor_AllFiles });
fd.setOverwrite(true);
fd.setText(Messages.get().ApplicationActionBarAdvisor_ExportPerspective);
final String fileName = fd.open();
if (fileName != null) {
new ConsoleJob(Messages.get().ApplicationActionBarAdvisor_ExportPerspective, null, Activator.PLUGIN_ID, null) {
@Override
protected void runInternal(IProgressMonitor monitor) throws Exception {
FileWriter writer = null;
try {
writer = new FileWriter(fileName);
memento.save(writer);
} finally {
if (writer != null)
writer.close();
}
}
@Override
protected String getErrorMessage() {
return Messages.get().ApplicationActionBarAdvisor_PerspectiveExportFailed;
}
}.start();
}
}
} catch (Exception e) {
// $NON-NLS-1$
Activator.logError("Exception in exportPerspective", e);
MessageDialogHelper.openError(window.getShell(), Messages.get().ApplicationActionBarAdvisor_Error, Messages.get().ApplicationActionBarAdvisor_PerspectiveExportFailed);
}
}
use of org.eclipse.ui.internal.WorkbenchPage in project netxms by netxms.
the class ApplicationActionBarAdvisor method importPerspective.
/**
* Import perspective
* @param window
*/
private void importPerspective(final IWorkbenchWindow window) {
FileDialog fd = new FileDialog(window.getShell());
// $NON-NLS-1$ //$NON-NLS-2$
fd.setFilterExtensions(new String[] { "*.xml", "*.*" });
fd.setFilterNames(new String[] { Messages.get().ApplicationActionBarAdvisor_XMLFiles, Messages.get().ApplicationActionBarAdvisor_AllFiles });
fd.setText(Messages.get().ApplicationActionBarAdvisor_ImportPerspective);
final String fileName = fd.open();
if (fileName == null)
return;
if (!MessageDialogHelper.openConfirm(window.getShell(), Messages.get().ApplicationActionBarAdvisor_ConfirmRestart, Messages.get().ApplicationActionBarAdvisor_RestartConfirmationMessage))
return;
new ConsoleJob(Messages.get().ApplicationActionBarAdvisor_ImportPerspective, null, Activator.PLUGIN_ID, null) {
@Override
protected void runInternal(IProgressMonitor monitor) throws Exception {
FileReader reader = null;
try {
reader = new FileReader(fileName);
final XMLMemento memento = XMLMemento.createReadRoot(reader);
runInUIThread(new Runnable() {
@Override
public void run() {
try {
Perspective p = ((WorkbenchImplementation) Tweaklets.get(WorkbenchImplementation.KEY)).createPerspective(null, (WorkbenchPage) window.getActivePage());
p.restoreState(memento);
PerspectiveRegistry reg = (PerspectiveRegistry) window.getWorkbench().getPerspectiveRegistry();
PerspectiveDescriptor pd = reg.createPerspective(p.getDesc().getLabel(), (PerspectiveDescriptor) p.getDesc());
WorkbenchPage page = (WorkbenchPage) window.getActivePage();
page.savePerspectiveAs(pd);
p = page.findPerspective(pd);
p.restoreState(memento);
p.restoreState();
window.getWorkbench().showPerspective(pd.getId(), window);
page.savePerspective();
PlatformUI.getWorkbench().restart();
} catch (Exception e) {
// $NON-NLS-1$
Activator.logError("Exception in importPerspective", e);
MessageDialogHelper.openError(window.getShell(), Messages.get().ApplicationActionBarAdvisor_Error, Messages.get().ApplicationActionBarAdvisor_PerspectiveImportFailed);
}
}
});
} finally {
if (reader != null)
reader.close();
}
}
@Override
protected String getErrorMessage() {
return Messages.get().ApplicationActionBarAdvisor_PerspectiveImportFailed;
}
}.start();
}
use of org.eclipse.ui.internal.WorkbenchPage in project yamcs-studio by yamcs.
the class OpenWithMenu method openEditor.
/**
* Opens the given editor on the selected file.
*
* @param editor
* the editor descriptor, or null for the system editor
* @param openUsingDescriptor
* use the descriptor's editor ID for opening if false (normal case), or use the descriptor itself if
* true (needed to fix bug 178235).
*/
private void openEditor(IEditorDescriptor editor, boolean openUsingDescriptor) {
var file = getFileResource();
if (file == null) {
return;
}
try {
if (openUsingDescriptor) {
((WorkbenchPage) page).openEditorFromDescriptor(new FileEditorInput(file), editor, true, null);
} else {
var editorId = editor == null ? IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID : editor.getId();
((WorkbenchPage) page).openEditor(new FileEditorInput(file), editorId, true, MATCH_BOTH);
// only remember the default editor if the open succeeds
IDE.setDefaultEditor(file, editorId);
}
} catch (PartInitException e) {
DialogUtil.openError(page.getWorkbenchWindow().getShell(), "Problems Opening Editor", e.getMessage(), e);
}
}
use of org.eclipse.ui.internal.WorkbenchPage in project eclipse-integration-commons by spring-projects.
the class PerspectiveMigrator method migratePerspective.
public IStatus migratePerspective(String oldPerspectiveId, String newPerspectiveId, IProgressMonitor monitor) {
try {
monitor = SubMonitor.convert(monitor, "Migrating legacy perspectives", 3);
IPerspectiveRegistry registry = PlatformUI.getWorkbench().getPerspectiveRegistry();
monitor.worked(1);
IWorkbenchPage page = null;
try {
page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
} catch (NullPointerException e) {
// something wasn't initialized...ignore
}
if (page != null) {
if (page.getPerspective() == null || page.getPerspective().getId().equals(oldPerspectiveId)) {
IPerspectiveDescriptor newPerspective = registry.findPerspectiveWithId(newPerspectiveId);
page.setPerspective(newPerspective);
IPerspectiveDescriptor oldPerspective = registry.findPerspectiveWithId(oldPerspectiveId);
monitor.worked(1);
if (oldPerspective != null) {
page.closePerspective(oldPerspective, false, false);
registry.deletePerspective(oldPerspective);
}
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=381473
if (page instanceof WorkbenchPage) {
try {
Method closePerspectiveMethod = WorkbenchPage.class.getDeclaredMethod("closePerspective", IPerspectiveDescriptor.class, String.class, boolean.class, boolean.class);
closePerspectiveMethod.invoke(page, null, IConversionConstants.GRAILS_OLD_PERSPECTIVE_ID, true, false);
} catch (Exception e) {
// this method doesn't exist on e37. OK to ignore
// FrameworkUIActivator.getDefault().getLog().log(new Status(IStatus.INFO, FrameworkUIActivator.PLUGIN_ID, "Cannot use reflection to close legacy perspective on Eclipse 3.7.", e));
}
}
}
monitor.worked(1);
monitor.done();
return new Status(IStatus.OK, FrameworkUIActivator.PLUGIN_ID, "Migrate legacy perspectives.");
} catch (Exception e) {
return new Status(IStatus.ERROR, FrameworkUIActivator.PLUGIN_ID, "Problem migrating legacy perspectives.", e);
}
}
Aggregations