001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.jface.dialogs.IDialogConstants;
013: import org.eclipse.jface.dialogs.MessageDialog;
014: import org.eclipse.ui.IPerspectiveDescriptor;
015: import org.eclipse.ui.IWorkbenchPage;
016: import org.eclipse.ui.IWorkbenchWindow;
017: import org.eclipse.ui.internal.dialogs.SavePerspectiveDialog;
018: import org.eclipse.ui.internal.registry.PerspectiveDescriptor;
019: import org.eclipse.ui.internal.registry.PerspectiveRegistry;
020:
021: /**
022: * Action to save the layout of the active perspective.
023: */
024: public class SavePerspectiveAction extends PerspectiveAction {
025:
026: /**
027: * Creates an instance of this class.
028: *
029: * @param window the workbench window in which this action appears
030: */
031: public SavePerspectiveAction(IWorkbenchWindow window) {
032: super (window);
033: setText(WorkbenchMessages.SavePerspective_text);
034: setActionDefinitionId("org.eclipse.ui.window.savePerspective"); //$NON-NLS-1$
035: // @issue missing action id
036: setToolTipText(WorkbenchMessages.SavePerspective_toolTip);
037: window.getWorkbench().getHelpSystem().setHelp(this ,
038: IWorkbenchHelpContextIds.SAVE_PERSPECTIVE_ACTION);
039: }
040:
041: /* (non-Javadoc)
042: * Method declared on PerspectiveAction.
043: */
044: protected void run(IWorkbenchPage page, IPerspectiveDescriptor persp) {
045: PerspectiveDescriptor desc = (PerspectiveDescriptor) persp;
046: if (desc != null) {
047: if (desc.isSingleton()) {
048: saveSingleton(page);
049: } else {
050: saveNonSingleton(page, desc);
051: }
052: }
053: }
054:
055: /**
056: * Save a singleton over itself.
057: */
058: private void saveSingleton(IWorkbenchPage page) {
059: String[] buttons = new String[] { IDialogConstants.OK_LABEL,
060: IDialogConstants.CANCEL_LABEL };
061: MessageDialog d = new MessageDialog(page.getWorkbenchWindow()
062: .getShell(),
063: WorkbenchMessages.SavePerspective_overwriteTitle, null,
064: WorkbenchMessages.SavePerspective_singletonQuestion,
065: MessageDialog.QUESTION, buttons, 0);
066: if (d.open() == 0) {
067: page.savePerspective();
068: }
069: }
070:
071: /**
072: * Save a singleton over the user selection.
073: */
074: private void saveNonSingleton(IWorkbenchPage page,
075: PerspectiveDescriptor oldDesc) {
076: // Get reg.
077: PerspectiveRegistry reg = (PerspectiveRegistry) WorkbenchPlugin
078: .getDefault().getPerspectiveRegistry();
079:
080: // Get persp name.
081: SavePerspectiveDialog dlg = new SavePerspectiveDialog(page
082: .getWorkbenchWindow().getShell(), reg);
083: // Look up the descriptor by id again to ensure it is still valid.
084: IPerspectiveDescriptor description = reg
085: .findPerspectiveWithId(oldDesc.getId());
086: dlg.setInitialSelection(description);
087: if (dlg.open() != IDialogConstants.OK_ID) {
088: return;
089: }
090:
091: // Create descriptor.
092: PerspectiveDescriptor newDesc = (PerspectiveDescriptor) dlg
093: .getPersp();
094: if (newDesc == null) {
095: String name = dlg.getPerspName();
096: newDesc = reg.createPerspective(name,
097: (PerspectiveDescriptor) description);
098: if (newDesc == null) {
099: MessageDialog.openError(dlg.getShell(),
100: WorkbenchMessages.SavePerspective_errorTitle,
101: WorkbenchMessages.SavePerspective_errorMessage);
102: return;
103: }
104: }
105:
106: // Save state.
107: page.savePerspectiveAs(newDesc);
108: }
109:
110: }
|