KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > ui > SaveablePartDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.team.ui;
12
13 import org.eclipse.compare.CompareEditorInput;
14 import org.eclipse.compare.CompareUI;
15 import org.eclipse.core.runtime.NullProgressMonitor;
16 import org.eclipse.jface.dialogs.*;
17 import org.eclipse.jface.dialogs.Dialog;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.custom.BusyIndicator;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.widgets.*;
22 import org.eclipse.team.internal.ui.TeamUIMessages;
23 import org.eclipse.team.internal.ui.TeamUIPlugin;
24 import org.eclipse.ui.PlatformUI;
25
26 /**
27  * A dialog that displays a {@link org.eclipse.team.ui.ISaveableWorkbenchPart} and
28  * ensures that changes made to the input are saved when the dialog is closed.
29  *
30  * @see ISaveableWorkbenchPart
31  * @see SaveablePartAdapter
32  * @since 3.0
33  * @deprecated Clients should use a subclass of {@link CompareEditorInput}
34  * and {@link CompareUI#openCompareDialog(org.eclipse.compare.CompareEditorInput)}
35  */

36 public class SaveablePartDialog extends TrayDialog {
37         
38     private ISaveableWorkbenchPart input;
39     private String JavaDoc fContextId;
40     private boolean hasSettings = true;
41
42     /**
43      * Creates a dialog with the given title and input. The input is not created until the dialog
44      * is opened.
45      *
46      * @param shell the parent shell or <code>null</code> to create a top level shell.
47      * @param input the part to show in the dialog.
48      */

49     public SaveablePartDialog(Shell shell, ISaveableWorkbenchPart input) {
50         super(shell);
51         setShellStyle(getShellStyle() | SWT.RESIZE | SWT.MAX);
52         this.input = input;
53     }
54     
55     /* (non-Javadoc)
56      * Method declared on Dialog.
57      */

58     protected void createButtonsForButtonBar(Composite parent) {
59         createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
60     }
61     
62     /* (non-Javadoc)
63      * Method declared on Dialog.
64      */

65     protected Control createDialogArea(Composite parent2) {
66         Composite parent = (Composite) super.createDialogArea(parent2);
67         input.createPartControl(parent);
68         Shell shell = getShell();
69         shell.setText(input.getTitle());
70         shell.setImage(input.getTitleImage());
71         Dialog.applyDialogFont(parent2);
72         return parent;
73     }
74         
75     /* (non-Javadoc)
76      * @see org.eclipse.compare.internal.ResizableDialog#close()
77      */

78     public boolean close() {
79         saveChanges();
80         return super.close();
81     }
82     
83     /**
84      * Save any changes to the compare editor.
85      */

86     private void saveChanges() {
87         MessageDialog dialog = new MessageDialog(
88                 getShell(), TeamUIMessages.ParticipantCompareDialog_2, null,
89                 TeamUIMessages.ParticipantCompareDialog_3, MessageDialog.QUESTION, new String JavaDoc[]{IDialogConstants.YES_LABEL,
90                 IDialogConstants.NO_LABEL}, 0); // YES is the default
91

92         if (input.isDirty() && dialog.open() == IDialogConstants.OK_ID) {
93             BusyIndicator.showWhile(null, new Runnable JavaDoc() {
94                 public void run() {
95                     input.doSave(new NullProgressMonitor());
96                 }
97             });
98         }
99     }
100
101     /**
102      * Return the input to the dialog.
103      * @return the input to the dialog
104      * @since 3.2
105      */

106     protected ISaveableWorkbenchPart getInput() {
107         return input;
108     }
109     
110     /* (non-Javadoc)
111      * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsSettings()
112      */

113     protected IDialogSettings getDialogBoundsSettings() {
114         IDialogSettings compareSettings = TeamUIPlugin.getPlugin().getDialogSettings();
115         String JavaDoc sectionName = this.getClass().getName();
116         IDialogSettings dialogSettings = compareSettings.getSection(sectionName);
117         if (dialogSettings == null) {
118             hasSettings = false;
119             dialogSettings = compareSettings.addNewSection(sectionName);
120         }
121         return dialogSettings;
122     }
123     
124     /**
125      * Set the help content id of this dialog.
126      * @param contextId the help context id
127      */

128     public void setHelpContextId(String JavaDoc contextId) {
129         fContextId= contextId;
130     }
131     
132     /*
133      * @see org.eclipse.jface.window.Window#configureShell(Shell)
134      */

135     protected void configureShell(Shell newShell) {
136         super.configureShell(newShell);
137         if (fContextId != null)
138             PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, fContextId);
139     }
140     
141     /* (non-Javadoc)
142      * @see org.eclipse.jface.dialogs.Dialog#getInitialSize()
143      */

144     protected Point getInitialSize() {
145         Point initialSize = super.getInitialSize();
146         if (hasSettings) {
147             return initialSize;
148         }
149         // If we don't have settings we need to come up with a reasonable default
150
// since we can't depend on the compare editor input layout returning a good default size
151
int width= 0;
152         int height= 0;
153         Shell shell= getParentShell();
154         if (shell != null) {
155             Point parentSize= shell.getSize();
156             width= parentSize.x-100;
157             height= parentSize.y-100;
158         }
159         if (width < 700)
160             width= 700;
161         if (height < 500)
162             height= 500;
163         return new Point(width, height);
164     }
165 }
166
Popular Tags