KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > dialogs > DetailsDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.internal.ui.dialogs;
12
13 import org.eclipse.jface.dialogs.*;
14 import org.eclipse.jface.dialogs.Dialog;
15 import org.eclipse.jface.resource.JFaceResources;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.swt.graphics.Point;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.*;
22 import org.eclipse.ui.PlatformUI;
23
24 /**
25  * A simple superclass for detail button dialogs.
26  */

27 abstract public class DetailsDialog extends TrayDialog {
28     /**
29      * The Details button.
30      */

31     private Button detailsButton;
32
33     /**
34      * The Ok button.
35      */

36     private Button okButton;
37
38     /**
39      * The title of the dialog.
40      */

41     private String JavaDoc title;
42     
43     /**
44      * The error message
45      */

46     private Label errorMessageLabel;
47
48     /**
49      * The SWT list control that displays the error details.
50      */

51     private Composite detailsComposite;
52
53     /**
54      * Indicates whether the error details viewer is currently created.
55      */

56     private boolean detailsCreated = false;
57     
58     /**
59      * The key for the image to be displayed (one of the image constants on Dialog)
60      */

61     private String JavaDoc imageKey = null;
62     
63     /**
64      * Creates a details pane dialog.
65      * Note that the dialog will have no visual representation (no widgets)
66      * until it is told to open.
67      *
68      * @param parentShell the shell under which to create this dialog
69      * @param dialogTitle the title to use for this dialog
70      * @see org.eclipse.core.runtime.IStatus#matches
71      */

72     public DetailsDialog(Shell parentShell, String JavaDoc dialogTitle) {
73         super(parentShell);
74         this.title = dialogTitle;
75         initializeStyle();
76     }
77
78     protected void initializeStyle() {
79         setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.APPLICATION_MODAL);
80     }
81
82     /* (non-Javadoc)
83      * Method declared on Dialog.
84      * Handles the pressing of the Ok or Details button in this dialog.
85      * If the Ok button was pressed then close this dialog. If the Details
86      * button was pressed then toggle the displaying of the error details area.
87      * Note that the Details button will only be visible if the error being
88      * displayed specifies child details.
89      */

90     protected void buttonPressed(int id) {
91         if (id == IDialogConstants.DETAILS_ID) { // was the details button pressed?
92
toggleDetailsArea();
93         } else {
94             super.buttonPressed(id);
95         }
96     }
97
98     /* (non-Javadoc)
99      * Method declared in Window.
100      */

101     protected void configureShell(Shell shell) {
102         super.configureShell(shell);
103         shell.setText(title);
104         String JavaDoc helpContextId = getHelpContextId();
105         if (helpContextId != null) {
106             PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, helpContextId);
107         }
108     }
109
110     /* (non-Javadoc)
111      * Method declared on Dialog.
112      */

113     protected void createButtonsForButtonBar(Composite parent) {
114         // create OK and Details buttons
115
if(includeOkButton()) {
116             okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
117         }
118         if (includeCancelButton()) {
119             createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
120         }
121         if(includeDetailsButton()) {
122             detailsButton = createButton(parent, IDialogConstants.DETAILS_ID, getDetailsButtonLabelShow(), false);
123         }
124         updateEnablements();
125     }
126
127     protected String JavaDoc getDetailsButtonLabelShow() {
128         return IDialogConstants.SHOW_DETAILS_LABEL;
129     }
130     
131     protected String JavaDoc getDetailsButtonLabelHide() {
132         return IDialogConstants.HIDE_DETAILS_LABEL;
133     }
134     
135     /* (non-Javadoc)
136      * Method declared on Dialog.
137      * Creates and returns the contents of the upper part
138      * of the dialog (above the button bar).
139      */

140     final protected Control createDialogArea(Composite parent) {
141         
142         applyDialogFont(parent);
143         initializeDialogUnits(parent);
144         
145         // create composite
146
Composite composite = (Composite)super.createDialogArea(parent);
147         if (!isMainGrabVertical()) {
148             composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
149         }
150         
151         String JavaDoc helpContextId = getHelpContextId();
152         if (helpContextId != null) {
153             PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, helpContextId);
154         }
155             
156         // create image
157
String JavaDoc key = getImageKey();
158         Image image = null;
159         if (key != null) {
160             image = JFaceResources.getImageRegistry().get(key);
161         }
162         if (image != null) {
163             // create a composite to split the dialog area in two
164
Composite top = new Composite(composite, SWT.NONE);
165             GridLayout layout = new GridLayout();
166             layout.marginHeight = 0;
167             layout.marginWidth = 0;
168             layout.verticalSpacing = 0;
169             layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
170             layout.numColumns = 2;
171             top.setLayout(layout);
172             top.setLayoutData(new GridData(GridData.FILL_BOTH));
173         
174             // add the image to the left of the composite
175
Label label = new Label(top, 0);
176             image.setBackground(label.getBackground());
177             label.setImage(image);
178             label.setLayoutData(new GridData(
179                 GridData.HORIZONTAL_ALIGN_CENTER |
180                 GridData.VERTICAL_ALIGN_BEGINNING));
181                 
182             // add a composite to the right to contain the custom components
183
Composite right = new Composite(top, SWT.NONE);
184             layout = new GridLayout();
185             layout.marginHeight = 0;
186             layout.marginWidth = 0;
187             layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
188             layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
189             right.setLayout(layout);
190             right.setLayoutData(new GridData(GridData.FILL_BOTH));
191             createMainDialogArea(right);
192         } else {
193             createMainDialogArea(composite);
194         }
195         
196         if(includeErrorMessage()) {
197             errorMessageLabel = new Label(composite, SWT.NONE);
198             errorMessageLabel.setLayoutData(new GridData(
199                 GridData.GRAB_HORIZONTAL |
200                 GridData.HORIZONTAL_ALIGN_FILL));
201             errorMessageLabel.setForeground(getShell().getDisplay().getSystemColor(SWT.COLOR_RED));
202         }
203         
204         Dialog.applyDialogFont(parent);
205         return composite;
206     }
207     
208     /**
209      * Return the help context id to be used for the dialog.
210      * This context Id will be registered by this class.
211      * By default, this method returns <code>null</code>.
212      * @return the help context id to be used for the dialog.
213      */

214     protected String JavaDoc getHelpContextId() {
215         return null;
216     }
217
218     /**
219      * Return whether the main area should grab excess vertical space.
220      * The default is <code>true</code> but subclasses can override
221      * in cases where the main is more or less fixed but the details
222      * needs to grab.
223      * @return whether the main area should grab excess vertical space
224      */

225     protected boolean isMainGrabVertical() {
226         return true;
227     }
228
229     /**
230      * Creates the dialog's top composite
231      *
232      * @param parent the parent composite
233      */

234     abstract protected void createMainDialogArea(Composite parent);
235
236     /**
237      * Create this dialog's drop-down list component.
238      *
239      * @param parent the parent composite
240      * @return the drop-down list component
241      */

242     abstract protected Composite createDropDownDialogArea(Composite parent);
243     
244     /**
245      * Toggles the unfolding of the details area. This is triggered by
246      * the user pressing the details button.
247      */

248     private void toggleDetailsArea() {
249         Point windowSize = getShell().getSize();
250         Point oldSize = getContents().computeSize(SWT.DEFAULT, SWT.DEFAULT);
251         
252         if (detailsCreated) {
253             detailsComposite.dispose();
254             detailsCreated = false;
255             detailsButton.setText(getDetailsButtonLabelShow());
256         } else {
257             detailsComposite = createDropDownDialogArea((Composite)getContents());
258             detailsCreated = true;
259             detailsButton.setText(getDetailsButtonLabelHide());
260         }
261         Dialog.applyDialogFont(getContents());
262         Point newSize = getContents().computeSize(SWT.DEFAULT, SWT.DEFAULT);
263     
264         getShell().setSize(new Point(windowSize.x, windowSize.y + (newSize.y - oldSize.y)));
265     }
266     
267     final protected void setErrorMessage(String JavaDoc error) {
268         if(errorMessageLabel != null) {
269             if(error == null || error.length() == 0) {
270                 errorMessageLabel.setText(""); //$NON-NLS-1$
271
} else {
272                 errorMessageLabel.setText(error);
273             }
274             errorMessageLabel.update();
275         }
276     }
277     
278     final protected void setPageComplete(boolean complete) {
279         if(okButton != null ) {
280             okButton.setEnabled(complete);
281         }
282     }
283     
284     abstract protected void updateEnablements();
285     
286     protected boolean includeCancelButton() {
287         return true;
288     }
289     
290     protected boolean includeOkButton() {
291         return true;
292     }
293     
294     /**
295      * Returns the imageKey.
296      * @return String
297      */

298     protected String JavaDoc getImageKey() {
299         return imageKey;
300     }
301
302
303     /**
304      * Sets the imageKey.
305      * @param imageKey The imageKey to set
306      */

307     protected void setImageKey(String JavaDoc imageKey) {
308         this.imageKey = imageKey;
309     }
310
311     protected Label createWrappingLabel(Composite parent, String JavaDoc text) {
312         Label label = new Label(parent, SWT.LEFT | SWT.WRAP);
313         label.setText(text);
314         GridData data = new GridData();
315         data.horizontalSpan = 1;
316         data.horizontalAlignment = GridData.FILL;
317         data.horizontalIndent = 0;
318         data.grabExcessHorizontalSpace = true;
319         data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
320         label.setLayoutData(data);
321         label.setFont(parent.getFont());
322         return label;
323     }
324     
325     protected Composite createComposite(Composite parent) {
326         Composite composite = new Composite(parent, SWT.NONE);
327         GridLayout layout = new GridLayout();
328         composite.setLayout(layout);
329         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
330         composite.setFont(parent.getFont());
331         return composite;
332     }
333     
334     protected boolean isDetailsVisible() {
335         return detailsCreated;
336     }
337     
338     protected boolean includeErrorMessage() {
339         return true;
340     }
341     
342     protected boolean includeDetailsButton() {
343         return true;
344     }
345 }
346
Popular Tags