KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > SavePerspectiveAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
23  * Action to save the layout of the active perspective.
24  */

25 public class SavePerspectiveAction extends PerspectiveAction {
26
27     /**
28      * Creates an instance of this class.
29      *
30      * @param window the workbench window in which this action appears
31      */

32     public SavePerspectiveAction(IWorkbenchWindow window) {
33         super(window);
34         setText(WorkbenchMessages.SavePerspective_text);
35         setActionDefinitionId("org.eclipse.ui.window.savePerspective"); //$NON-NLS-1$
36
// @issue missing action id
37
setToolTipText(WorkbenchMessages.SavePerspective_toolTip);
38         window.getWorkbench().getHelpSystem().setHelp(this,
39                 IWorkbenchHelpContextIds.SAVE_PERSPECTIVE_ACTION);
40     }
41
42     /* (non-Javadoc)
43      * Method declared on PerspectiveAction.
44      */

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     /**
57      * Save a singleton over itself.
58      */

59     private void saveSingleton(IWorkbenchPage page) {
60         String JavaDoc[] buttons = new String JavaDoc[] { 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     /**
72      * Save a singleton over the user selection.
73      */

74     private void saveNonSingleton(IWorkbenchPage page, PerspectiveDescriptor oldDesc) {
75         // Get reg.
76
PerspectiveRegistry reg = (PerspectiveRegistry) WorkbenchPlugin
77                 .getDefault().getPerspectiveRegistry();
78
79         // Get persp name.
80
SavePerspectiveDialog dlg = new SavePerspectiveDialog(page.getWorkbenchWindow()
81                 .getShell(), reg);
82         // Look up the descriptor by id again to ensure it is still valid.
83
IPerspectiveDescriptor description = reg.findPerspectiveWithId(oldDesc.getId());
84         dlg.setInitialSelection(description);
85         if (dlg.open() != IDialogConstants.OK_ID) {
86             return;
87         }
88
89         // Create descriptor.
90
PerspectiveDescriptor newDesc = (PerspectiveDescriptor) dlg.getPersp();
91         if (newDesc == null) {
92             String JavaDoc 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         // Save state.
103
page.savePerspectiveAs(newDesc);
104     }
105
106 }
107
Popular Tags