KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > CompareDialog


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.compare.internal;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.compare.CompareConfiguration;
16 import org.eclipse.compare.CompareEditorInput;
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.jface.dialogs.*;
19 import org.eclipse.jface.operation.IRunnableWithProgress;
20 import org.eclipse.jface.preference.IPreferenceStore;
21 import org.eclipse.jface.util.IPropertyChangeListener;
22 import org.eclipse.jface.util.PropertyChangeEvent;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.graphics.Point;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.widgets.*;
27 import org.eclipse.ui.PlatformUI;
28
29 /**
30  * This is a dialog that can host a {@link CompareEditorInput}.
31  * <p>
32  * This class can be used as is or can be subclassed.
33  *
34  * @since 3.3
35  */

36 public class CompareDialog extends TrayDialog implements IPropertyChangeListener {
37     
38     private final CompareEditorInput fCompareEditorInput;
39     private Button fCommitButton;
40     private Label statusLabel;
41     boolean hasSettings = true;
42     private final DialogCompareContainer fContainer = new DialogCompareContainer();
43     
44     private class DialogCompareContainer extends CompareContainer {
45         
46         /* (non-Javadoc)
47          * @see org.eclipse.jface.operation.IRunnableContext#run(boolean, boolean, org.eclipse.jface.operation.IRunnableWithProgress)
48          */

49         public void run(boolean fork, boolean cancelable,
50                 IRunnableWithProgress runnable) throws InvocationTargetException JavaDoc,
51                 InterruptedException JavaDoc {
52             ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell());
53             dialog.run(fork, cancelable, runnable);
54         }
55         
56         /* (non-Javadoc)
57          * @see org.eclipse.compare.ICompareContainer#setStatusMessage(java.lang.String)
58          */

59         public void setStatusMessage(String JavaDoc message) {
60             if (statusLabel != null && !statusLabel.isDisposed()) {
61                 if (message == null) {
62                     statusLabel.setText(""); //$NON-NLS-1$
63
} else {
64                     statusLabel.setText(message);
65                 }
66             }
67         }
68     }
69     
70     /**
71      * Create a dialog to host the given input.
72      * @param shell a shell
73      * @param input the dialog input
74      */

75     public CompareDialog(Shell shell, CompareEditorInput input) {
76         super(shell);
77         setShellStyle(getShellStyle() | SWT.RESIZE | SWT.MAX);
78         Assert.isNotNull(input);
79         fCompareEditorInput= input;
80     }
81     
82     /* (non-Javadoc)
83      * @see org.eclipse.compare.internal.ResizableDialog#close()
84      */

85     public boolean close() {
86         if (super.close()) {
87             if (fCompareEditorInput != null)
88                 fCompareEditorInput.removePropertyChangeListener(this);
89             return true;
90         }
91         return false;
92     }
93     
94     /* (non-Javadoc)
95      * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
96      */

97     protected void createButtonsForButtonBar(Composite parent) {
98         fCommitButton= createButton(parent, IDialogConstants.OK_ID, getOKButtonLabel(), true);
99         fCommitButton.setEnabled(isOKEnabled());
100         if (isCancelable()) {
101             createButton(parent, IDialogConstants.CANCEL_ID, getCancelButtonLabel(), false);
102         }
103     }
104
105     private String JavaDoc getCancelButtonLabel() {
106         return fCompareEditorInput.getCancelButtonLabel();
107     }
108
109     private boolean isCancelable() {
110         return isInputEditable() || isElementSelectionDialog();
111     }
112
113     private String JavaDoc getOKButtonLabel() {
114         return fCompareEditorInput.getOKButtonLabel();
115     }
116
117     private boolean isElementSelectionDialog() {
118         return fCompareEditorInput.isEditionSelectionDialog();
119     }
120
121     /**
122      * Return whether the compare editor input of this dialog is editable.
123      * By default, the input is editable if the compare configuration
124      * indicates that either the left or right sides are editable.
125      * Subclasses may override.
126      * @return whether the compare editor input of this dialog is editable
127      * @see CompareConfiguration#isLeftEditable()
128      * @see CompareConfiguration#isRightEditable()
129      */

130     protected boolean isInputEditable() {
131         return fCompareEditorInput.getCompareConfiguration().isLeftEditable()
132             || fCompareEditorInput.getCompareConfiguration().isRightEditable();
133     }
134
135     /* (non-Javadoc)
136      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
137      */

138     public void propertyChange(PropertyChangeEvent event) {
139         if (event.getProperty().equals(CompareEditorInput.DIRTY_STATE)
140                 || event.getProperty().equals(CompareEditorInput.PROP_SELECTED_EDITION)) {
141             if (fCommitButton != null && fCompareEditorInput != null)
142                 fCommitButton.setEnabled(isOKEnabled());
143         } else if (event.getProperty().equals(CompareEditorInput.PROP_TITLE)) {
144             getShell().setText(fCompareEditorInput.getTitle());
145         } else if (event.getProperty().equals(CompareEditorInput.PROP_TITLE_IMAGE)) {
146             getShell().setImage(fCompareEditorInput.getTitleImage());
147         }
148     }
149
150     private boolean isOKEnabled() {
151         if (isInputEditable())
152             return fCompareEditorInput.isDirty();
153         if (isElementSelectionDialog())
154             return getSelectedElement() != null;
155         return true;
156     }
157     
158     private Object JavaDoc getSelectedElement() {
159         return fCompareEditorInput.getSelectedEdition();
160     }
161
162     /* (non-Javadoc)
163      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
164      */

165     protected Control createDialogArea(Composite parent2) {
166                         
167         Composite parent= (Composite) super.createDialogArea(parent2);
168
169         Control c= fCompareEditorInput.createContents(parent);
170         c.setLayoutData(new GridData(GridData.FILL_BOTH));
171         
172         IPreferenceStore store= fCompareEditorInput.getCompareConfiguration().getPreferenceStore();
173         if (store != null) {
174             if (store.getBoolean(ComparePreferencePage.SHOW_MORE_INFO)) {
175                 statusLabel = new Label(parent, SWT.NONE);
176                 statusLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
177             }
178         }
179         
180         Shell shell= c.getShell();
181         shell.setText(fCompareEditorInput.getTitle());
182         shell.setImage(fCompareEditorInput.getTitleImage());
183         applyDialogFont(parent);
184         return parent;
185     }
186     
187     /* (non-Javadoc)
188      * @see org.eclipse.jface.window.Window#open()
189      */

190     public int open() {
191         // Before opening, set the container of the input and listen
192
// for changes to the input
193
fCompareEditorInput.addPropertyChangeListener(this);
194         fCompareEditorInput.setContainer(fContainer);
195         return super.open();
196     }
197     
198     /* (non-Javadoc)
199      * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
200      */

201     protected void buttonPressed(int buttonId) {
202         if (buttonId == OK) {
203             if (!fCompareEditorInput.okPressed())
204                 return;
205         } else if (buttonId == CANCEL) {
206             fCompareEditorInput.cancelPressed();
207         }
208         super.buttonPressed(buttonId);
209     }
210
211     /* (non-Javadoc)
212      * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsSettings()
213      */

214     protected IDialogSettings getDialogBoundsSettings() {
215         IDialogSettings compareSettings = CompareUIPlugin.getDefault().getDialogSettings();
216         String JavaDoc sectionName = this.getClass().getName();
217         IDialogSettings dialogSettings = compareSettings.getSection(sectionName);
218         if (dialogSettings == null) {
219             hasSettings = false;
220             dialogSettings = compareSettings.addNewSection(sectionName);
221         }
222         return dialogSettings;
223     }
224     
225     /* (non-Javadoc)
226      * @see org.eclipse.compare.internal.ResizableDialog#configureShell(org.eclipse.swt.widgets.Shell)
227      */

228     protected void configureShell(Shell newShell) {
229         super.configureShell(newShell);
230         if (getHelpContextId() != null)
231             PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, getHelpContextId());
232     }
233
234     /**
235      * Return the help content id for this dialog or <code>null</code>.
236      * By default, a generic help content id is returned. Subclasses may
237      * override.
238      * @return the help content id for this dialog or <code>null</code>
239      */

240     public String JavaDoc getHelpContextId() {
241         return ICompareContextIds.COMPARE_DIALOG;
242     }
243     
244     /* (non-Javadoc)
245      * @see org.eclipse.jface.dialogs.Dialog#getInitialSize()
246      */

247     protected Point getInitialSize() {
248         Point initialSize = super.getInitialSize();
249         if (hasSettings) {
250             return initialSize;
251         }
252         return getDefaultSize();
253     }
254
255     /**
256      * If we don't have settings we need to come up with a reasonable default
257      * since we can't depend on the compare editor input layout returning a
258      * good default size.
259      * @return the default size of the dialog
260      */

261     protected Point getDefaultSize() {
262         int width= 0;
263         int height= 0;
264         Shell shell= getParentShell();
265         if (shell != null) {
266             Point parentSize= shell.getSize();
267             width= parentSize.x-100;
268             height= parentSize.y-100;
269         }
270         if (width < 700)
271             width= 700;
272         if (height < 500)
273             height= 500;
274         return new Point(width, height);
275     }
276
277     /**
278      * Return the compare editor input for this dialog.
279      * @return the compare editor input for this dialog
280      */

281     protected final CompareEditorInput getInput() {
282         return fCompareEditorInput;
283     }
284
285 }
286
Popular Tags