KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > subscriber > CommitSetDialog


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.team.internal.ccvs.ui.subscriber;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.jface.dialogs.*;
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.util.IPropertyChangeListener;
17 import org.eclipse.jface.util.PropertyChangeEvent;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.*;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.layout.*;
22 import org.eclipse.swt.widgets.*;
23 import org.eclipse.team.internal.ccvs.ui.*;
24 import org.eclipse.team.internal.core.subscribers.ActiveChangeSet;
25 import org.eclipse.ui.PlatformUI;
26
27 /**
28  * Dialog for creating and editing commit set
29  * title and comment
30  */

31 public class CommitSetDialog extends TitleAreaDialog {
32
33     public final static short NEW = 0;
34     public final static short EDIT = 1;
35     
36     private static final int DEFAULT_WIDTH_IN_CHARS= 80;
37     
38     private final ActiveChangeSet set;
39     private CommitCommentArea commitCommentArea;
40     private Text nameText;
41     private Button useTitleButton;
42     private Button enterCommentButton;
43     private final String JavaDoc title;
44     private final String JavaDoc description;
45     private String JavaDoc comment;
46     private short mode;
47
48     public CommitSetDialog(Shell parentShell, ActiveChangeSet set, IResource[] files, short mode) {
49         super(parentShell);
50         this.set = set;
51         this.mode = mode;
52         this.title = mode == NEW ? CVSUIMessages.WorkspaceChangeSetCapability_2
53                 : CVSUIMessages.WorkspaceChangeSetCapability_7;
54         this.description = mode == NEW ? CVSUIMessages.WorkspaceChangeSetCapability_3
55                 : CVSUIMessages.WorkspaceChangeSetCapability_8;
56         
57         if (files == null) {
58             files = set.getResources();
59         }
60         
61         int shellStyle = getShellStyle();
62         setShellStyle(shellStyle | SWT.RESIZE | SWT.MAX);
63         commitCommentArea = new CommitCommentArea();
64         // Get a project from which the commit template can be obtained
65
if (files.length > 0)
66             commitCommentArea.setProject(files[0].getProject());
67     }
68     
69     /* (non-Javadoc)
70      * @see org.eclipse.jface.dialogs.TitleAreaDialog#createContents(org.eclipse.swt.widgets.Composite)
71      */

72     protected Control createContents(Composite parent) {
73         Control contents = super.createContents(parent);
74         setTitle(title);
75         setMessage(description);
76         return contents;
77     }
78     
79     /* (non-Javadoc)
80      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
81      */

82     protected Control createDialogArea(Composite parent) {
83         Composite parentComposite = (Composite) super.createDialogArea(parent);
84
85         // create a composite with standard margins and spacing
86
Composite composite = new Composite(parentComposite, SWT.NONE);
87         GridLayout layout = new GridLayout();
88         layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
89         layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
90         layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
91         layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
92         composite.setLayout(layout);
93         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
94         composite.setFont(parentComposite.getFont());
95         
96         createNameArea(composite);
97         
98         if (hasCommitTemplate()) {
99             if (set.hasComment()) {
100                 // Only set the comment if the set has a custom comment.
101
// Otherwise, the template should be used
102
comment = set.getComment();
103                 commitCommentArea.setProposedComment(comment);
104             }
105         } else {
106             comment = set.getComment();
107             commitCommentArea.setProposedComment(comment);
108             createOptionsArea(composite);
109         }
110         
111         commitCommentArea.createArea(composite);
112         commitCommentArea.addPropertyChangeListener(new IPropertyChangeListener() {
113
114             public void propertyChange(PropertyChangeEvent event) {
115                 if (event.getProperty() == CommitCommentArea.OK_REQUESTED) {
116                     okPressed();
117                 } else if (event.getProperty() == CommitCommentArea.COMMENT_MODIFIED) {
118                     comment = (String JavaDoc)event.getNewValue();
119                     updateEnablements();
120                 }
121             }
122         });
123         
124         initializeValues();
125         updateEnablements();
126         
127         Dialog.applyDialogFont(parent);
128         return composite;
129     }
130     
131     /* (non-Javadoc)
132      * @see org.eclipse.jface.window.Window#getInitialSize()
133      */

134     protected Point getInitialSize() {
135         final Point size= super.getInitialSize();
136         size.x= convertWidthInCharsToPixels(DEFAULT_WIDTH_IN_CHARS);
137         size.y += convertHeightInCharsToPixels(8);
138         return size;
139     }
140
141     private void createNameArea(Composite parent) {
142         Composite composite = new Composite(parent, SWT.NONE);
143         GridLayout layout = new GridLayout();
144         layout.marginHeight = 0;
145         layout.marginWidth = 0;
146         layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
147         layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
148         layout.numColumns = 2;
149         composite.setLayout(layout);
150         composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
151         composite.setFont(parent.getFont());
152         
153         Label label = new Label(composite, SWT.NONE);
154         label.setText(CVSUIMessages.CommitSetDialog_0);
155         label.setLayoutData(new GridData(GridData.BEGINNING));
156         
157         nameText = new Text(composite, SWT.BORDER);
158         nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
159         nameText.addModifyListener(new ModifyListener() {
160             public void modifyText(ModifyEvent e) {
161                 updateEnablements();
162             }
163         });
164     }
165
166     private void initializeValues() {
167         String JavaDoc initialText = set.getTitle();
168         if (initialText == null) initialText = ""; //$NON-NLS-1$
169
nameText.setText(initialText);
170         nameText.setSelection(0, initialText.length());
171         
172         if (useTitleButton != null) {
173             useTitleButton.setSelection(!set.hasComment());
174             enterCommentButton.setSelection(set.hasComment());
175         }
176     }
177     
178     private void createOptionsArea(Composite composite) {
179         Composite radioArea = new Composite(composite, SWT.NONE);
180         RowLayout radioAreaLayout = new RowLayout(SWT.VERTICAL);
181         radioAreaLayout.marginLeft = 0;
182         radioAreaLayout.marginRight = 0;
183         radioAreaLayout.marginTop = 0;
184         radioAreaLayout.marginBottom = 0;
185         radioArea.setLayout(radioAreaLayout);
186         
187         useTitleButton = createRadioButton(radioArea, CVSUIMessages.CommitSetDialog_2);
188         enterCommentButton = createRadioButton(radioArea, CVSUIMessages.CommitSetDialog_3);
189         SelectionAdapter listener = new SelectionAdapter() {
190             public void widgetSelected(SelectionEvent e) {
191                 updateEnablements();
192             }
193         };
194         useTitleButton.addSelectionListener(listener);
195         enterCommentButton.addSelectionListener(listener);
196         
197     }
198     
199     private Button createRadioButton(Composite parent, String JavaDoc label) {
200         Button button = new Button(parent, SWT.RADIO);
201         button.setText(label);
202         return button;
203     }
204     
205     private void updateEnablements() {
206         commitCommentArea.setEnabled(isUseCustomComment());
207         setErrorMessage(null);
208         String JavaDoc name = nameText.getText();
209         if (name.length() == 0) {
210             setPageComplete(false);
211             return;
212         }
213         
214         // check if the new change set already exists
215
if (mode == NEW
216                 && CVSUIPlugin.getPlugin().getChangeSetManager().getSet(name) != null) {
217             setPageComplete(false);
218             setErrorMessage(CVSUIMessages.WorkspaceChangeSetCapability_9);
219             return;
220         }
221         
222         // check if the edited change set already exists, do not display the
223
// error message when new the name is the same as the old one
224
if (mode == EDIT && !name.equals(set.getName())
225                 && CVSUIPlugin.getPlugin().getChangeSetManager().getSet(name) != null) {
226             setPageComplete(false);
227             setErrorMessage(CVSUIMessages.WorkspaceChangeSetCapability_9);
228             return;
229         }
230
231         
232         if (isUseCustomComment()) {
233             if (comment == null || comment.length() == 0) {
234                setPageComplete(false);
235                return;
236             }
237         }
238         setPageComplete(true);
239     }
240     
241     final protected void setPageComplete(boolean complete) {
242         Button okButton = getButton(IDialogConstants.OK_ID);
243         if(okButton != null ) {
244             okButton.setEnabled(complete);
245         }
246     }
247     
248     private boolean hasCommitTemplate() {
249         return commitCommentArea.hasCommitTemplate();
250     }
251     
252     /* (non-Javadoc)
253      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
254      */

255     protected void okPressed() {
256         set.setTitle(nameText.getText());
257         if (isUseCustomComment()) {
258             // Call getComment so the comment gets saved
259
set.setComment(commitCommentArea.getComment(true));
260         } else {
261             set.setComment(null);
262         }
263         super.okPressed();
264     }
265
266     private boolean isUseCustomComment() {
267         return enterCommentButton == null || enterCommentButton.getSelection();
268     }
269
270     protected Label createWrappingLabel(Composite parent, String JavaDoc text) {
271         Label label = new Label(parent, SWT.LEFT | SWT.WRAP);
272         label.setText(text);
273         GridData data = new GridData();
274         data.horizontalSpan = 1;
275         data.horizontalAlignment = GridData.FILL;
276         data.horizontalIndent = 0;
277         data.grabExcessHorizontalSpace = true;
278         data.widthHint = 200;
279         label.setLayoutData(data);
280         return label;
281     }
282     
283     /* (non-Javadoc)
284      * @see org.eclipse.jface.dialogs.Dialog#createButtonBar(org.eclipse.swt.widgets.Composite)
285      */

286     protected Control createButtonBar(Composite parent) {
287         Control control = super.createButtonBar(parent);
288         updateEnablements();
289         return control;
290     }
291     
292     /* (non-Javadoc)
293      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
294      */

295     protected void configureShell(Shell shell) {
296         super.configureShell(shell);
297         shell.setText(title);
298         // set F1 help
299
PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
300                 IHelpContextIds.COMMIT_SET_DIALOG);
301
302     }
303 }
304
Popular Tags