KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > tags > TagSelectionDialog


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.ccvs.ui.tags;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.core.runtime.jobs.Job;
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.jface.dialogs.TrayDialog;
19 import org.eclipse.jface.operation.IRunnableContext;
20 import org.eclipse.jface.operation.IRunnableWithProgress;
21 import org.eclipse.jface.util.IPropertyChangeListener;
22 import org.eclipse.jface.util.PropertyChangeEvent;
23 import org.eclipse.jface.window.Window;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.DisposeEvent;
26 import org.eclipse.swt.events.DisposeListener;
27 import org.eclipse.swt.graphics.Cursor;
28 import org.eclipse.swt.graphics.Point;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.widgets.*;
31 import org.eclipse.team.internal.ccvs.core.CVSStatus;
32 import org.eclipse.team.internal.ccvs.core.CVSTag;
33 import org.eclipse.team.internal.ccvs.ui.*;
34
35 /**
36  * Dialog to prompt the user to choose a tag for a selected resource
37  */

38 public class TagSelectionDialog extends TrayDialog implements IPropertyChangeListener {
39     
40     private TagSelectionArea tagSelectionArea;
41     private Cursor appBusyCursor;
42     
43     public static final int INCLUDE_HEAD_TAG = TagSourceWorkbenchAdapter.INCLUDE_HEAD_TAG;
44     public static final int INCLUDE_BASE_TAG = TagSourceWorkbenchAdapter.INCLUDE_BASE_TAG;
45     public static final int INCLUDE_BRANCHES = TagSourceWorkbenchAdapter.INCLUDE_BRANCHES;
46     public static final int INCLUDE_VERSIONS = TagSourceWorkbenchAdapter.INCLUDE_VERSIONS;
47     public static final int INCLUDE_DATES = TagSourceWorkbenchAdapter.INCLUDE_DATES;
48     public static final int INCLUDE_ALL_TAGS = TagSourceWorkbenchAdapter.INCLUDE_ALL_TAGS;
49     
50     private Button okButton;
51     
52     // dialog title, should indicate the action in which the tag selection
53
// dialog is being shown
54
private String JavaDoc title;
55     
56     private boolean recurse = true;
57     
58     // constants
59
private static final int SIZING_DIALOG_WIDTH = 90;
60     private static final int SIZING_DIALOG_HEIGHT = 25;
61
62     private CVSTag selection;
63
64     private TagSource tagSource;
65
66     private String JavaDoc message;
67
68     private int includeFlags;
69
70     private String JavaDoc helpContext;
71
72     private boolean showRecurse;
73         
74     public static CVSTag getTagToCompareWith(Shell shell, TagSource tagSource, int includeFlags) {
75         TagSelectionDialog dialog = new TagSelectionDialog(shell, tagSource,
76             CVSUIMessages.CompareWithTagAction_message,
77             CVSUIMessages.TagSelectionDialog_Select_a_Tag_1,
78             includeFlags,
79             false, /* show recurse*/
80             IHelpContextIds.COMPARE_TAG_SELECTION_DIALOG);
81         dialog.setBlockOnOpen(true);
82         int result = dialog.open();
83         if (result == Window.CANCEL) {
84             return null;
85         }
86         return dialog.getResult();
87     }
88     
89     /**
90      * Creates a new TagSelectionDialog.
91      * @param resource The resource to select a version for.
92      */

93     public TagSelectionDialog(Shell parentShell, TagSource tagSource, String JavaDoc title, String JavaDoc message, int includeFlags, final boolean showRecurse, String JavaDoc helpContext) {
94         super(parentShell);
95         
96         // Create a tag selection area with a custom recurse option
97
this.tagSource = tagSource;
98         this.message = message;
99         this.includeFlags = includeFlags;
100         this.helpContext = helpContext;
101         this.showRecurse = showRecurse;
102         this.title = title;
103         setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.APPLICATION_MODAL);
104     }
105     
106     /* (non-Javadoc)
107      * Method declared on Window.
108      */

109     protected void configureShell(Shell newShell) {
110         super.configureShell(newShell);
111         newShell.setText(title);
112     }
113     
114     /* (non-Javadoc)
115      * @see org.eclipse.jface.window.Window#getInitialSize()
116      */

117     protected Point getInitialSize() {
118         final Point size= super.getInitialSize();
119         size.x= convertWidthInCharsToPixels(SIZING_DIALOG_WIDTH);
120         size.y= convertHeightInCharsToPixels(SIZING_DIALOG_HEIGHT);
121         return size;
122     }
123     
124     /**
125      * Creates this window's widgetry.
126      * <p>
127      * The default implementation of this framework method
128      * creates this window's shell (by calling <code>createShell</code>),
129      * its control (by calling <code>createContents</code>),
130      * and initializes this window's shell bounds
131      * (by calling <code>initializeBounds</code>).
132      * This framework method may be overridden; however,
133      * <code>super.create</code> must be called.
134      * </p>
135      */

136     public void create() {
137         super.create();
138         initialize();
139     }
140     
141     /**
142      * Add buttons to the dialog's button bar.
143      *
144      * @param parent the button bar composite
145      */

146     protected void createButtonsForButtonBar(Composite parent) {
147         // create OK and Cancel buttons by default
148
okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
149         okButton.setEnabled(false);
150         createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
151     }
152     
153     /**
154      * Creates and returns the contents of the upper part
155      * of this dialog (above the button bar).
156      * <p>
157      * The default implementation of this framework method
158      * creates and returns a new <code>Composite</code> with
159      * standard margins and spacing.
160      * Subclasses should override.
161      * </p>
162      *
163      * @param parent the parent composite to contain the dialog area
164      * @return the dialog area control
165      */

166     protected Control createDialogArea(Composite parent) {
167         
168         applyDialogFont(parent);
169         initializeDialogUnits(parent);
170         
171         final Composite top = (Composite)super.createDialogArea(parent);
172         
173         // Delegate most of the dialog to the tag selection area
174
tagSelectionArea = new TagSelectionArea(getShell(), tagSource, includeFlags, helpContext) {
175             protected void createCustomArea(Composite parent) {
176                 if(showRecurse) {
177                     final Button recurseCheck = new Button(parent, SWT.CHECK);
178                     recurseCheck.setText(CVSUIMessages.TagSelectionDialog_recurseOption);
179                     recurseCheck.addListener(SWT.Selection, new Listener() {
180                         public void handleEvent(Event event) {
181                             recurse = recurseCheck.getSelection();
182                         }
183                     });
184                     recurseCheck.setSelection(true);
185                 }
186             }
187         };
188         if (message != null)
189             tagSelectionArea.setTagAreaLabel(message);
190         tagSelectionArea.addPropertyChangeListener(this);
191         tagSelectionArea.createArea(top);
192         tagSelectionArea.setRunnableContext(getRunnableContext());
193         
194         // Create a separator between the tag area and the button area
195
final Label seperator = new Label(top, SWT.SEPARATOR | SWT.HORIZONTAL);
196         final GridData data = new GridData (GridData.FILL_HORIZONTAL);
197         data.horizontalSpan = 2;
198         seperator.setLayoutData(data);
199         
200         updateEnablement();
201         applyDialogFont(parent);
202         
203         return top;
204     }
205     
206     
207     /**
208      * Utility method that creates a label instance
209      * and sets the default layout data.
210      *
211      * @param parent the parent for the new label
212      * @param text the text for the new label
213      * @return the new label
214      */

215     protected Label createLabel(Composite parent, String JavaDoc text) {
216         Label label = new Label(parent, SWT.LEFT);
217         label.setText(text);
218         GridData data = new GridData();
219         data.horizontalSpan = 1;
220         data.horizontalAlignment = GridData.FILL;
221         label.setLayoutData(data);
222         return label;
223     }
224     
225     /**
226      * Returns the selected tag.
227      */

228     public CVSTag getResult() {
229         return selection;
230     }
231     
232     public boolean getRecursive() {
233         return recurse;
234     }
235
236     /**
237      * Initializes the dialog contents.
238      */

239     protected void initialize() {
240         okButton.setEnabled(false);
241         if (CVSUIPlugin.getPlugin().getPreferenceStore().getBoolean(ICVSUIConstants.PREF_AUTO_REFRESH_TAGS_IN_TAG_SELECTION_DIALOG))
242             tagSelectionArea.refreshTagList();
243     }
244
245     
246     /**
247      * Updates the dialog enablement.
248      */

249     protected void updateEnablement() {
250         if(okButton!=null) {
251             okButton.setEnabled(selection != null);
252         }
253     }
254
255     /* (non-Javadoc)
256      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
257      */

258     public void propertyChange(PropertyChangeEvent event) {
259         String JavaDoc property = event.getProperty();
260         if (property.equals(TagSelectionArea.SELECTED_TAG)) {
261             selection = (CVSTag)event.getNewValue();
262             updateEnablement();
263         } else if (property.equals(TagSelectionArea.OPEN_SELECTED_TAG)) {
264             okPressed();
265         }
266     }
267     
268     /**
269      * Creates a runnable context that allows refreshing the tags in the background.
270      *
271      * @since 3.1
272      */

273     private IRunnableContext getRunnableContext() {
274         return new IRunnableContext() {
275             public void run(boolean fork, boolean cancelable, final IRunnableWithProgress runnable) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
276                 final Job refreshJob = new Job(CVSUIMessages.TagSelectionDialog_7) {
277                     protected IStatus run(IProgressMonitor monitor) {
278                             if (monitor.isCanceled())
279                             return Status.CANCEL_STATUS;
280                         try {
281                             setBusy(true);
282                             runnable.run(monitor);
283                         } catch (InvocationTargetException JavaDoc e) {
284                             return new CVSStatus(IStatus.ERROR, CVSUIMessages.TagSelectionDialog_8, e);
285                         } catch (InterruptedException JavaDoc e) {
286                             return new CVSStatus(IStatus.ERROR, CVSUIMessages.TagSelectionDialog_8, e);
287                         } finally {
288                             setBusy(false);
289                         }
290                         if (monitor.isCanceled())
291                             return Status.CANCEL_STATUS;
292                         else
293                             return Status.OK_STATUS;
294                     }
295                 };
296                 refreshJob.setUser(false);
297                 refreshJob.setPriority(Job.DECORATE);
298                 getShell().addDisposeListener(new DisposeListener() {
299                     public void widgetDisposed(DisposeEvent e) {
300                         refreshJob.cancel();
301                     }
302                 });
303                 refreshJob.schedule();
304             }
305         };
306     }
307     
308     private void setBusy(final boolean busy) {
309         final Shell shell = getShell();
310         if (shell != null && !shell.isDisposed()) {
311             shell.getDisplay().asyncExec(new Runnable JavaDoc() {
312                 public void run() {
313                     if (!shell.isDisposed()) {
314                         Cursor cursor = null;
315                         if (busy) {
316                             if (appBusyCursor == null)
317                                 appBusyCursor = new Cursor(shell.getDisplay(), SWT.CURSOR_APPSTARTING);
318                             cursor = appBusyCursor;
319                         }
320                         shell.setCursor(cursor);
321                     }
322                 }
323             });
324         }
325     }
326         
327     public boolean close() {
328         if(appBusyCursor != null)
329             appBusyCursor.dispose();
330         return super.close();
331     }
332 }
333
Popular Tags