KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > wizards > CVSWizardPage


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  * Philippe Ombredanne - bug 84808
11  *******************************************************************************/

12 package org.eclipse.team.internal.ccvs.ui.wizards;
13
14
15 import java.util.ArrayList JavaDoc;
16
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.jface.dialogs.IDialogConstants;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.jface.viewers.*;
23 import org.eclipse.jface.wizard.*;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.*;
28 import org.eclipse.team.internal.ccvs.core.ICVSRemoteFolder;
29 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
30 import org.eclipse.team.internal.ccvs.ui.operations.RemoteProjectFolder;
31 import org.eclipse.ui.model.WorkbenchContentProvider;
32 import org.eclipse.ui.model.WorkbenchLabelProvider;
33 import org.eclipse.ui.views.navigator.ResourceSorter;
34
35 /**
36  * Common superclass for CVS wizard pages. Provides convenience methods
37  * for widget creation.
38  */

39 public abstract class CVSWizardPage extends WizardPage {
40     
41     protected static final int LABEL_WIDTH_HINT = 400;
42     protected static final int LABEL_INDENT_WIDTH = 32;
43     protected static final int LIST_HEIGHT_HINT = 100;
44     protected static final int SPACER_HEIGHT = 8;
45
46     private ICVSWizard wizard;
47     
48     /**
49      * CVSWizardPage constructor comment.
50      * @param pageName the name of the page
51      */

52     public CVSWizardPage(String JavaDoc pageName) {
53         super(pageName);
54     }
55     
56     /**
57      * CVSWizardPage constructor comment.
58      * @param pageName the name of the page
59      * @param title the title of the page
60      * @param titleImage the image for the page
61      */

62     public CVSWizardPage(String JavaDoc pageName, String JavaDoc title, ImageDescriptor titleImage) {
63         super(pageName, title, titleImage);
64     }
65     /**
66      * CVSWizardPage constructor comment.
67      * @param pageName the name of the page
68      * @param title the title of the page
69      * @param titleImage the image for the page
70      * @param description the description of the page
71      */

72     public CVSWizardPage(String JavaDoc pageName, String JavaDoc title, ImageDescriptor titleImage, String JavaDoc description) {
73         super(pageName, title, titleImage);
74         setDescription(description);
75     }
76     /**
77      * Creates a new checkbox instance and sets the default layout data.
78      *
79      * @param group the composite in which to create the checkbox
80      * @param label the string to set into the checkbox
81      * @return the new checkbox
82      */

83     protected Button createCheckBox(Composite group, String JavaDoc label) {
84         Button button = new Button(group, SWT.CHECK | SWT.LEFT);
85         button.setText(label);
86         GridData data = new GridData();
87         data.horizontalSpan = 2;
88         button.setLayoutData(data);
89         return button;
90     }
91     /**
92      * Utility method that creates a combo box
93      *
94      * @param parent the parent for the new label
95      * @return the new widget
96      */

97     protected Combo createCombo(Composite parent) {
98         Combo combo = new Combo(parent, SWT.READ_ONLY);
99         GridData data = new GridData(GridData.FILL_HORIZONTAL);
100         data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
101         combo.setLayoutData(data);
102         return combo;
103     }
104     /**
105      * Creates composite control and sets the default layout data.
106      * @param parent the parent of the new composite
107      * @param numColumns the number of columns for the new composite
108      * @param grabExcess TODO
109      *
110      * @return the newly-created coposite
111      */

112     protected Composite createComposite(Composite parent, int numColumns, boolean grabExcess) {
113         final Composite composite = new Composite(parent, SWT.NULL);
114         composite.setLayout(new GridLayout(numColumns, false));
115         composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, grabExcess, grabExcess));
116         return composite;
117     }
118     
119     /**
120      * Utility method that creates a label instance
121      * and sets the default layout data.
122      *
123      * @param parent the parent for the new label
124      * @param text the text for the new label
125      * @return the new label
126      */

127     public static Label createLabel(Composite parent, String JavaDoc text) {
128         return createIndentedLabel(parent, text, 0);
129     }
130     /**
131      * Utility method that creates a label instance indented by the specified
132      * number of pixels and sets the default layout data.
133      *
134      * @param parent the parent for the new label
135      * @param text the text for the new label
136      * @param indent the indent in pixels, or 0 for none
137      * @return the new label
138      */

139     public static Label createIndentedLabel(Composite parent, String JavaDoc text, int indent) {
140         Label label = new Label(parent, SWT.LEFT);
141         label.setText(text);
142         GridData data = new GridData();
143         data.horizontalSpan = 1;
144         data.horizontalAlignment = GridData.FILL;
145         data.horizontalIndent = indent;
146         label.setLayoutData(data);
147         return label;
148     }
149     /**
150      * Utility method that creates a label instance with word wrap and sets
151      * the default layout data.
152      *
153      * @param parent the parent for the new label
154      * @param text the text for the new label
155      * @param indent the indent in pixels, or 0 for none
156      * @param widthHint the nominal width of the label
157      * @return the new label
158      */

159     protected Label createWrappingLabel(Composite parent, String JavaDoc text, int indent) {
160         return createWrappingLabel(parent, text, indent, 1);
161     }
162     
163     protected Label createWrappingLabel(Composite parent, String JavaDoc text, int indent, int horizontalSpan) {
164         Label label = new Label(parent, SWT.LEFT | SWT.WRAP);
165         label.setText(text);
166         GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
167         data.horizontalIndent = indent;
168         data.horizontalSpan = horizontalSpan;
169         data.widthHint = LABEL_WIDTH_HINT;
170         label.setLayoutData(data);
171         return label;
172     }
173     
174     /**
175      * Create a text field specific for this application
176      *
177      * @param parent the parent of the new text field
178      * @return the new text field
179      */

180     static public Text createTextField(Composite parent) {
181         Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
182         return layoutTextField(text);
183     }
184     /**
185      * Create a password field specific for this application
186      *
187      * @param parent the parent of the new text field
188      * @return the new text field
189      */

190     static public Text createPasswordField(Composite parent) {
191         Text text = new Text(parent, SWT.SINGLE | SWT.BORDER | SWT.PASSWORD);
192         return layoutTextField(text);
193     }
194     /**
195      * Layout a text or password field specific for this application
196      *
197      * @param parent the parent of the new text field
198      * @return the new text field
199      */

200     static public Text layoutTextField(Text text) {
201         GridData data = new GridData(GridData.FILL_HORIZONTAL);
202         data.verticalAlignment = GridData.CENTER;
203         data.grabExcessVerticalSpace = false;
204         data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
205         text.setLayoutData(data);
206         return text;
207     }
208     
209     /**
210      * Utility method to create a radio button
211      *
212      * @param parent the parent of the radio button
213      * @param label the label of the radio button
214      * @param span the number of columns to span
215      * @return the created radio button
216      */

217     protected Button createRadioButton(Composite parent, String JavaDoc label, int span) {
218         Button button = new Button(parent, SWT.RADIO);
219         button.setText(label);
220         GridData data = new GridData();
221         data.horizontalSpan = span;
222         button.setLayoutData(data);
223         return button;
224     }
225
226     protected TreeViewer createResourceSelectionTree(Composite composite, int types, int span) {
227         TreeViewer tree = new TreeViewer(composite, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
228         tree.setUseHashlookup(true);
229         tree.setContentProvider(getResourceProvider(types));
230         tree.setLabelProvider(
231             new DecoratingLabelProvider(
232                 new WorkbenchLabelProvider(),
233                 CVSUIPlugin.getPlugin().getWorkbench().getDecoratorManager().getLabelDecorator()));
234         tree.setSorter(new ResourceSorter(ResourceSorter.NAME));
235         
236         GridData data = new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL);
237         data.heightHint = LIST_HEIGHT_HINT;
238         data.horizontalSpan = span;
239         tree.getControl().setLayoutData(data);
240         return tree;
241     }
242
243     /**
244      * Returns a content provider for <code>IResource</code>s that returns
245      * only children of the given resource type.
246      */

247     protected ITreeContentProvider getResourceProvider(final int resourceType) {
248         return new WorkbenchContentProvider() {
249             public Object JavaDoc[] getChildren(Object JavaDoc o) {
250                 if (o instanceof IContainer) {
251                     IResource[] members = null;
252                     try {
253                         members = ((IContainer)o).members();
254                     } catch (CoreException e) {
255                         //just return an empty set of children
256
return new Object JavaDoc[0];
257                     }
258     
259                     //filter out the desired resource types
260
ArrayList JavaDoc results = new ArrayList JavaDoc();
261                     for (int i = 0; i < members.length; i++) {
262                         //And the test bits with the resource types to see if they are what we want
263
if ((members[i].getType() & resourceType) > 0) {
264                             results.add(members[i]);
265                         }
266                     }
267                     return results.toArray();
268                 } else {
269                     return super.getChildren(o);
270                 }
271             }
272         };
273     }
274     
275     protected ICVSWizard getCVSWizard() {
276         if (wizard != null) {
277             return wizard;
278         }
279         IWizard wizard = getWizard();
280         if (wizard instanceof ICVSWizard) {
281             // This is the method that is invoked when the next button is pressed
282
// Hence, assume that the page s about to be shown
283
return ((ICVSWizard)wizard);
284         }
285         return null;
286     }
287     
288     public void setCVSWizard(ICVSWizard wizard) {
289         this.wizard = wizard;
290     }
291     
292     /* (non-Javadoc)
293      * @see org.eclipse.jface.wizard.WizardPage#getNextPage()
294      */

295     public IWizardPage getNextPage() {
296         ICVSWizard w = getCVSWizard();
297         if (w != null) {
298             // This is the method that is invoked when the next button is pressed
299
// Hence, assume that the page s about to be shown
300
return w.getNextPage(this, true /* about to show */);
301         }
302         return super.getNextPage();
303     }
304     
305     /* (non-Javadoc)
306      * @see org.eclipse.jface.wizard.WizardPage#canFlipToNextPage()
307      */

308     public boolean canFlipToNextPage() {
309         ICVSWizard w = getCVSWizard();
310         if (w != null) {
311             return isPageComplete() &&
312                 w.getNextPage(this, false /* about to show */) != null;
313         }
314         return super.canFlipToNextPage();
315     }
316
317     /**
318      * Utility method to get a folder name based on preferences.
319      * Returns the folder name or the project name retrieved from the project metafile
320      * @param the CVS remote folder
321      * @return a project name
322      */

323     static protected String JavaDoc getPreferredFolderName(ICVSRemoteFolder folder) {
324         if (CVSUIPlugin.getPlugin().isUseProjectNameOnCheckout() && folder instanceof RemoteProjectFolder ) {
325             RemoteProjectFolder rpf = (RemoteProjectFolder) folder;
326             if (rpf.hasProjectName()) {
327                 return rpf.getProjectName();
328             }
329         }
330         return folder.getName();
331     }
332 }
333
Popular Tags