Search in sources :

Example 1 with WorkbenchPage

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);
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) VTDGen(com.ximpleware.VTDGen) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) WorkbenchPage(org.eclipse.ui.internal.WorkbenchPage) IEditorReference(org.eclipse.ui.IEditorReference) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) AutoPilot(com.ximpleware.AutoPilot) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IFile(org.eclipse.core.resources.IFile) File(java.io.File) VTDNav(com.ximpleware.VTDNav)

Example 2 with WorkbenchPage

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);
    }
}
Also used : Perspective(org.eclipse.ui.internal.Perspective) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) WorkbenchPage(org.eclipse.ui.internal.WorkbenchPage) XMLMemento(org.eclipse.ui.XMLMemento) FileWriter(java.io.FileWriter) SelectPerspectiveDialog(org.eclipse.ui.internal.dialogs.SelectPerspectiveDialog) ConsoleJob(org.netxms.ui.eclipse.jobs.ConsoleJob) FileDialog(org.eclipse.swt.widgets.FileDialog) PartInitException(org.eclipse.ui.PartInitException) IOException(java.io.IOException)

Example 3 with WorkbenchPage

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();
}
Also used : PerspectiveRegistry(org.eclipse.ui.internal.registry.PerspectiveRegistry) PartInitException(org.eclipse.ui.PartInitException) IOException(java.io.IOException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Perspective(org.eclipse.ui.internal.Perspective) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) WorkbenchPage(org.eclipse.ui.internal.WorkbenchPage) XMLMemento(org.eclipse.ui.XMLMemento) WorkbenchImplementation(org.eclipse.ui.internal.tweaklets.WorkbenchImplementation) FileReader(java.io.FileReader) ConsoleJob(org.netxms.ui.eclipse.jobs.ConsoleJob) FileDialog(org.eclipse.swt.widgets.FileDialog) PerspectiveDescriptor(org.eclipse.ui.internal.registry.PerspectiveDescriptor)

Example 4 with WorkbenchPage

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);
    }
}
Also used : WorkbenchPage(org.eclipse.ui.internal.WorkbenchPage) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) FileEditorInput(org.eclipse.ui.part.FileEditorInput) PartInitException(org.eclipse.ui.PartInitException)

Example 5 with WorkbenchPage

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);
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) WorkbenchPage(org.eclipse.ui.internal.WorkbenchPage) IPerspectiveRegistry(org.eclipse.ui.IPerspectiveRegistry) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IPerspectiveDescriptor(org.eclipse.ui.IPerspectiveDescriptor) Method(java.lang.reflect.Method)

Aggregations

IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)5 WorkbenchPage (org.eclipse.ui.internal.WorkbenchPage)5 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 PartInitException (org.eclipse.ui.PartInitException)3 IOException (java.io.IOException)2 FileDialog (org.eclipse.swt.widgets.FileDialog)2 XMLMemento (org.eclipse.ui.XMLMemento)2 Perspective (org.eclipse.ui.internal.Perspective)2 FileEditorInput (org.eclipse.ui.part.FileEditorInput)2 ConsoleJob (org.netxms.ui.eclipse.jobs.ConsoleJob)2 AutoPilot (com.ximpleware.AutoPilot)1 VTDGen (com.ximpleware.VTDGen)1 VTDNav (com.ximpleware.VTDNav)1 File (java.io.File)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 IFile (org.eclipse.core.resources.IFile)1 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)1