KickJava   Java API By Example, From Geeks To Geeks.

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


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.team.internal.ccvs.ui.tags;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.jface.dialogs.Dialog;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.operation.IRunnableContext;
20 import org.eclipse.jface.operation.IRunnableWithProgress;
21 import org.eclipse.osgi.util.NLS;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.widgets.*;
24 import org.eclipse.team.core.TeamException;
25 import org.eclipse.team.internal.ccvs.core.CVSTag;
26 import org.eclipse.team.internal.ccvs.ui.*;
27 import org.eclipse.team.internal.ui.PixelConverter;
28 import org.eclipse.team.internal.ui.SWTUtils;
29 import org.eclipse.team.internal.ui.dialogs.DialogArea;
30 import org.eclipse.ui.PlatformUI;
31
32 /**
33  * An area that displays the Refresh and Configure Tags buttons
34  */

35 public class TagRefreshButtonArea extends DialogArea {
36     
37     private TagSource tagSource;
38     private final Shell shell;
39     private Button refreshButton;
40     private IRunnableContext context;
41     private Label fMessageLabel;
42     private final Listener addDateTagListener;
43
44     public TagRefreshButtonArea(Shell shell, TagSource tagSource, Listener addDateTagListener) {
45         this.addDateTagListener = addDateTagListener;
46         Assert.isNotNull(shell);
47         Assert.isNotNull(tagSource);
48         this.shell = shell;
49         this.tagSource = tagSource;
50     }
51     
52     /* (non-Javadoc)
53      * @see org.eclipse.team.internal.ui.dialogs.DialogArea#createArea(org.eclipse.swt.widgets.Composite)
54      */

55     public void createArea(Composite parent) {
56         
57         final PixelConverter converter= SWTUtils.createDialogPixelConverter(parent);
58         
59         final Composite buttonComp = new Composite(parent, SWT.NONE);
60         buttonComp.setLayoutData(SWTUtils.createHFillGridData());//SWT.DEFAULT, SWT.DEFAULT, SWT.END, SWT.TOP, false, false));
61
buttonComp.setLayout(SWTUtils.createGridLayout(4, converter, SWTUtils.MARGINS_NONE));
62         
63         fMessageLabel= SWTUtils.createLabel(buttonComp, null);
64         refreshButton = new Button(buttonComp, SWT.PUSH);
65         refreshButton.setText (CVSUIMessages.TagConfigurationDialog_20);
66         
67         final Button configureTagsButton = new Button(buttonComp, SWT.PUSH);
68         configureTagsButton.setText (CVSUIMessages.TagConfigurationDialog_21);
69         
70         Button addDateTagButton = null;
71         int buttonWidth;
72         if (addDateTagListener != null) {
73             addDateTagButton = new Button(buttonComp, SWT.PUSH);
74             addDateTagButton.setText (CVSUIMessages.TagConfigurationDialog_AddDateTag);
75             Dialog.applyDialogFont(buttonComp);
76             buttonWidth= SWTUtils.calculateControlSize(converter, new Button [] { addDateTagButton, configureTagsButton, refreshButton });
77             addDateTagButton.setLayoutData(SWTUtils.createGridData(buttonWidth, SWT.DEFAULT, SWT.END, SWT.CENTER, false, false));
78             addDateTagButton.addListener(SWT.Selection, addDateTagListener);
79         } else {
80             Dialog.applyDialogFont(buttonComp);
81             buttonWidth= SWTUtils.calculateControlSize(converter, new Button [] { configureTagsButton, refreshButton });
82         }
83         
84         refreshButton.setLayoutData(SWTUtils.createGridData(buttonWidth, SWT.DEFAULT, SWT.END, SWT.CENTER, false, false));
85         configureTagsButton.setLayoutData(SWTUtils.createGridData(buttonWidth, SWT.DEFAULT, SWT.END, SWT.CENTER, false, false));
86         
87         refreshButton.addListener(SWT.Selection, new Listener() {
88             public void handleEvent(Event event) {
89                 refresh(false);
90             }
91         });
92         
93         configureTagsButton.addListener(SWT.Selection, new Listener() {
94                 public void handleEvent(Event event) {
95                     TagConfigurationDialog d = new TagConfigurationDialog(shell, tagSource);
96                     d.open();
97                 }
98             });
99         
100         PlatformUI.getWorkbench().getHelpSystem().setHelp(refreshButton, IHelpContextIds.TAG_CONFIGURATION_REFRESHACTION);
101         PlatformUI.getWorkbench().getHelpSystem().setHelp(configureTagsButton, IHelpContextIds.TAG_CONFIGURATION_OVERVIEW);
102         Dialog.applyDialogFont(buttonComp);
103     }
104     
105     
106     public void refresh(final boolean background) {
107         try {
108             getRunnableContext().run(true, true, new IRunnableWithProgress() {
109                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
110                     try {
111                         setBusy(true);
112                         Display.getDefault().asyncExec(new Runnable JavaDoc() {
113                             public void run() {
114                                 fMessageLabel.setText(CVSUIMessages.TagRefreshButtonArea_6);
115                             }
116                         });
117                         monitor.beginTask(CVSUIMessages.TagRefreshButtonArea_5, 100);
118                         final CVSTag[] tags = tagSource.refresh(false, Policy.subMonitorFor(monitor, 70));
119                         Display.getDefault().asyncExec(new Runnable JavaDoc() {
120                             public void run() {
121                                 fMessageLabel.setText(background && tags.length == 0 ? CVSUIMessages.TagRefreshButtonArea_7 : ""); //$NON-NLS-1$
122
}
123                         });
124                         if (!background && tags.length == 0 && promptForBestEffort()) {
125                             tagSource.refresh(true, Policy.subMonitorFor(monitor, 30));
126                         }
127                     } catch (TeamException e) {
128                         throw new InvocationTargetException JavaDoc(e);
129                     } finally {
130                         setBusy(false);
131                         monitor.done();
132                     }
133                 }
134             });
135         } catch (InterruptedException JavaDoc e) {
136             // operation cancelled
137
} catch (InvocationTargetException JavaDoc e) {
138             CVSUIPlugin.openError(shell, CVSUIMessages.TagConfigurationDialog_14, null, e);
139         }
140     }
141     
142     private void setBusy(final boolean busy) {
143         if (shell != null && !shell.isDisposed())
144             shell.getDisplay().asyncExec(new Runnable JavaDoc() {
145                 public void run() {
146                     refreshButton.setEnabled(!busy);
147                 }
148             });
149     }
150     
151     private boolean promptForBestEffort() {
152         final boolean[] prompt = new boolean[] { false };
153         shell.getDisplay().syncExec(new Runnable JavaDoc() {
154             public void run() {
155                 MessageDialog dialog = new MessageDialog(shell, CVSUIMessages.TagRefreshButtonArea_0, null,
156                         getNoTagsFoundMessage(),
157                         MessageDialog.INFORMATION,
158                         new String JavaDoc[] {
159                             CVSUIMessages.TagRefreshButtonArea_1,
160                             CVSUIMessages.TagRefreshButtonArea_2,
161                             CVSUIMessages.TagRefreshButtonArea_3
162                         }, 1);
163                 int code = dialog.open();
164                 if (code == 0) {
165                     prompt[0] = true;
166                 } else if (code == 1) {
167                     TagConfigurationDialog d = new TagConfigurationDialog(shell, tagSource);
168                     d.open();
169                 }
170
171             }
172         });
173         return prompt[0];
174     }
175     
176     private String JavaDoc getNoTagsFoundMessage() {
177         return NLS.bind(CVSUIMessages.TagRefreshButtonArea_4, new String JavaDoc[] { tagSource.getShortDescription() });
178     }
179     
180     public void setTagSource(TagSource tagSource) {
181         Assert.isNotNull(tagSource);
182         this.tagSource = tagSource;
183     }
184
185     public IRunnableContext getRunnableContext() {
186         if (context == null)
187             return PlatformUI.getWorkbench().getProgressService();
188         return context;
189     }
190     
191     public void setRunnableContext(IRunnableContext context) {
192         this.context = context;
193     }
194 }
195
Popular Tags