1 11 package org.eclipse.ui.internal; 12 13 import org.eclipse.jface.dialogs.IDialogConstants; 14 import org.eclipse.jface.dialogs.MessageDialog; 15 import org.eclipse.ui.IPerspectiveDescriptor; 16 import org.eclipse.ui.IWorkbenchPage; 17 import org.eclipse.ui.IWorkbenchWindow; 18 import org.eclipse.ui.internal.dialogs.SavePerspectiveDialog; 19 import org.eclipse.ui.internal.registry.PerspectiveDescriptor; 20 import org.eclipse.ui.internal.registry.PerspectiveRegistry; 21 22 25 public class SavePerspectiveAction extends PerspectiveAction { 26 27 32 public SavePerspectiveAction(IWorkbenchWindow window) { 33 super(window); 34 setText(WorkbenchMessages.SavePerspective_text); 35 setActionDefinitionId("org.eclipse.ui.window.savePerspective"); setToolTipText(WorkbenchMessages.SavePerspective_toolTip); 38 window.getWorkbench().getHelpSystem().setHelp(this, 39 IWorkbenchHelpContextIds.SAVE_PERSPECTIVE_ACTION); 40 } 41 42 45 protected void run(IWorkbenchPage page, IPerspectiveDescriptor persp) { 46 PerspectiveDescriptor desc = (PerspectiveDescriptor) persp; 47 if (desc != null) { 48 if (desc.isSingleton()) { 49 saveSingleton(page); 50 } else { 51 saveNonSingleton(page, desc); 52 } 53 } 54 } 55 56 59 private void saveSingleton(IWorkbenchPage page) { 60 String [] buttons = new String [] { IDialogConstants.OK_LABEL, 61 IDialogConstants.CANCEL_LABEL }; 62 MessageDialog d = new MessageDialog(page.getWorkbenchWindow().getShell(), 63 WorkbenchMessages.SavePerspective_overwriteTitle, 64 null, WorkbenchMessages.SavePerspective_singletonQuestion, 65 MessageDialog.QUESTION, buttons, 0); 66 if (d.open() == 0) { 67 page.savePerspective(); 68 } 69 } 70 71 74 private void saveNonSingleton(IWorkbenchPage page, PerspectiveDescriptor oldDesc) { 75 PerspectiveRegistry reg = (PerspectiveRegistry) WorkbenchPlugin 77 .getDefault().getPerspectiveRegistry(); 78 79 SavePerspectiveDialog dlg = new SavePerspectiveDialog(page.getWorkbenchWindow() 81 .getShell(), reg); 82 IPerspectiveDescriptor description = reg.findPerspectiveWithId(oldDesc.getId()); 84 dlg.setInitialSelection(description); 85 if (dlg.open() != IDialogConstants.OK_ID) { 86 return; 87 } 88 89 PerspectiveDescriptor newDesc = (PerspectiveDescriptor) dlg.getPersp(); 91 if (newDesc == null) { 92 String name = dlg.getPerspName(); 93 newDesc = reg.createPerspective(name, 94 (PerspectiveDescriptor) description); 95 if (newDesc == null) { 96 MessageDialog.openError(dlg.getShell(), WorkbenchMessages.SavePerspective_errorTitle, 97 WorkbenchMessages.SavePerspective_errorMessage); 98 return; 99 } 100 } 101 102 page.savePerspectiveAs(newDesc); 104 } 105 106 } 107 | Popular Tags |