Search in sources :

Example 1 with Perspective

use of org.eclipse.ui.internal.Perspective 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 2 with Perspective

use of org.eclipse.ui.internal.Perspective 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)

Aggregations

IOException (java.io.IOException)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 FileDialog (org.eclipse.swt.widgets.FileDialog)2 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)2 PartInitException (org.eclipse.ui.PartInitException)2 XMLMemento (org.eclipse.ui.XMLMemento)2 Perspective (org.eclipse.ui.internal.Perspective)2 WorkbenchPage (org.eclipse.ui.internal.WorkbenchPage)2 ConsoleJob (org.netxms.ui.eclipse.jobs.ConsoleJob)2 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 SelectPerspectiveDialog (org.eclipse.ui.internal.dialogs.SelectPerspectiveDialog)1 PerspectiveDescriptor (org.eclipse.ui.internal.registry.PerspectiveDescriptor)1 PerspectiveRegistry (org.eclipse.ui.internal.registry.PerspectiveRegistry)1 WorkbenchImplementation (org.eclipse.ui.internal.tweaklets.WorkbenchImplementation)1