KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > IgnoreResourcesDialog


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;
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.resource.JFaceColors;
17 import org.eclipse.osgi.util.NLS;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.*;
22 import org.eclipse.team.internal.ccvs.core.util.FileNameMatcher;
23 import org.eclipse.ui.PlatformUI;
24
25 public class IgnoreResourcesDialog extends TrayDialog {
26     // resources that should be ignored
27
private IResource[] resources;
28
29     // preference keys
30
private final String JavaDoc ACTION_KEY = "Action"; //$NON-NLS-1$
31
private static final int ADD_NAME_ENTRY = 0;
32     private static final int ADD_EXTENSION_ENTRY = 1;
33     private static final int ADD_CUSTOM_ENTRY = 2;
34
35     // dialogs settings that are persistent between workbench sessions
36
private IDialogSettings settings;
37
38     // buttons
39
private Button addNameEntryButton;
40     private Button addExtensionEntryButton;
41     private Button addCustomEntryButton;
42     private Text customEntryText;
43     private Label statusMessageLabel;
44     
45     private int selectedAction;
46     private String JavaDoc customPattern;
47     
48     // layout controls
49
private static final int LABEL_INDENT_WIDTH = 32;
50
51     /**
52      * Creates a new dialog for ignoring resources.
53      * @param shell the parent shell
54      * @param resources the array of resources to be ignored
55      */

56     public IgnoreResourcesDialog(Shell shell, IResource[] resources) {
57         super(shell);
58         this.resources = resources;
59
60         IDialogSettings workbenchSettings = CVSUIPlugin.getPlugin().getDialogSettings();
61         this.settings = workbenchSettings.getSection("IgnoreResourcesDialog");//$NON-NLS-1$
62
if (settings == null) {
63             this.settings = workbenchSettings.addNewSection("IgnoreResourcesDialog");//$NON-NLS-1$
64
}
65         
66         try {
67             selectedAction = settings.getInt(ACTION_KEY);
68         } catch (NumberFormatException JavaDoc e) {
69             selectedAction = ADD_NAME_ENTRY;
70         }
71     }
72     
73     /**
74      * Determines the ignore pattern to use for a resource given the selected action.
75      *
76      * @param resource the resource
77      * @return the ignore pattern for the specified resource
78      */

79     public String JavaDoc getIgnorePatternFor(IResource resource) {
80         switch (selectedAction) {
81             case ADD_NAME_ENTRY:
82                 return resource.getName();
83             case ADD_EXTENSION_ENTRY: {
84                 String JavaDoc extension = resource.getFileExtension();
85                 return (extension == null) ? resource.getName() : "*." + extension; //$NON-NLS-1$
86
}
87             case ADD_CUSTOM_ENTRY:
88                 return customPattern;
89         }
90         throw new IllegalStateException JavaDoc();
91     }
92
93     /* (non-Javadoc)
94      * Method declared on Dialog.
95      */

96     protected void createButtonsForButtonBar(Composite parent) {
97         createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
98         createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
99     }
100
101     /* (non-Javadoc)
102      * Method declared on Dialog.
103      */

104     protected void configureShell(Shell newShell) {
105         super.configureShell(newShell);
106         if (resources.length == 1) {
107             newShell.setText(NLS.bind(CVSUIMessages.IgnoreResourcesDialog_titleSingle, new String JavaDoc[] { resources[0].getName() }));
108         } else {
109             newShell.setText(NLS.bind(CVSUIMessages.IgnoreResourcesDialog_titleMany, new String JavaDoc[] { Integer.toString(resources.length) }));
110         }
111     }
112     
113     /* (non-Javadoc)
114      * Method declared on Dialog.
115      */

116     protected Control createContents(Composite parent) {
117         Control control = super.createContents(parent);
118         updateEnablements();
119         return control;
120     }
121
122     /* (non-Javadoc)
123      * Method declared on Dialog.
124      */

125     protected Control createDialogArea(Composite parent) {
126         Composite top = new Composite(parent, SWT.NONE);
127         top.setLayout(new GridLayout());
128         
129         PlatformUI.getWorkbench().getHelpSystem().setHelp(top, IHelpContextIds.ADD_TO_CVSIGNORE);
130         
131         createIndentedLabel(top, CVSUIMessages.IgnoreResourcesDialog_prompt, 0);
132         
133         Listener selectionListener = new Listener() {
134             public void handleEvent(Event event) {
135                 updateEnablements();
136             }
137         };
138         Listener modifyListener = new Listener() {
139             public void handleEvent(Event event) {
140                 validate();
141             }
142         };
143         
144         addNameEntryButton = createRadioButton(top, CVSUIMessages.IgnoreResourcesDialog_addNameEntryButton);
145         addNameEntryButton.addListener(SWT.Selection, selectionListener);
146         addNameEntryButton.setSelection(selectedAction == ADD_NAME_ENTRY);
147         createIndentedLabel(top, CVSUIMessages.IgnoreResourcesDialog_addNameEntryExample, LABEL_INDENT_WIDTH);
148         
149         addExtensionEntryButton = createRadioButton(top, CVSUIMessages.IgnoreResourcesDialog_addExtensionEntryButton);
150         addExtensionEntryButton.addListener(SWT.Selection, selectionListener);
151         addExtensionEntryButton.setSelection(selectedAction == ADD_EXTENSION_ENTRY);
152         createIndentedLabel(top, CVSUIMessages.IgnoreResourcesDialog_addExtensionEntryExample, LABEL_INDENT_WIDTH);
153
154         addCustomEntryButton = createRadioButton(top, CVSUIMessages.IgnoreResourcesDialog_addCustomEntryButton);
155         addCustomEntryButton.addListener(SWT.Selection, selectionListener);
156         addCustomEntryButton.setSelection(selectedAction == ADD_CUSTOM_ENTRY);
157         createIndentedLabel(top, CVSUIMessages.IgnoreResourcesDialog_addCustomEntryExample, LABEL_INDENT_WIDTH);
158         
159         customEntryText = createIndentedText(top, resources[0].getName(), LABEL_INDENT_WIDTH);
160         customEntryText.addListener(SWT.Modify, modifyListener);
161
162         statusMessageLabel = createIndentedLabel(top, "", 0); //$NON-NLS-1$
163
Dialog.applyDialogFont(parent);
164         return top;
165     }
166
167     /* (non-Javadoc)
168      * Method declared on Dialog.
169      */

170     protected void okPressed() {
171         settings.put(ACTION_KEY, selectedAction);
172         super.okPressed();
173     }
174
175     private Label createIndentedLabel(Composite parent, String JavaDoc text, int indent) {
176         Label label = new Label(parent, SWT.LEFT);
177         label.setText(text);
178         GridData data = new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.FILL_HORIZONTAL);
179         data.horizontalIndent = indent;
180         label.setLayoutData(data);
181         return label;
182     }
183
184     private Text createIndentedText(Composite parent, String JavaDoc text, int indent) {
185         Text textbox = new Text(parent, SWT.BORDER);
186         textbox.setText(text);
187         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
188         data.horizontalIndent = indent;
189         textbox.setLayoutData(data);
190         return textbox;
191     }
192     
193     private Button createRadioButton(Composite parent, String JavaDoc text) {
194         Button button = new Button(parent, SWT.RADIO);
195         button.setText(text);
196         button.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.FILL_HORIZONTAL));
197         return button;
198     }
199
200     private void updateEnablements() {
201         if (addNameEntryButton.getSelection()) {
202             selectedAction = ADD_NAME_ENTRY;
203         } else if (addExtensionEntryButton.getSelection()) {
204             selectedAction = ADD_EXTENSION_ENTRY;
205         } else if (addCustomEntryButton.getSelection()) {
206             selectedAction = ADD_CUSTOM_ENTRY;
207         }
208         customEntryText.setEnabled(selectedAction == ADD_CUSTOM_ENTRY);
209         validate();
210     }
211     
212     private void validate() {
213         if (selectedAction == ADD_CUSTOM_ENTRY) {
214             customPattern = customEntryText.getText();
215             if (customPattern.length() == 0) {
216                 setError(CVSUIMessages.IgnoreResourcesDialog_patternMustNotBeEmpty);
217                 return;
218             }
219             FileNameMatcher matcher = new FileNameMatcher(new String JavaDoc[] { customPattern });
220             for (int i = 0; i < resources.length; i++) {
221                 String JavaDoc name = resources[i].getName();
222                 if (! matcher.match(name)) {
223                     setError(NLS.bind(CVSUIMessages.IgnoreResourcesDialog_patternDoesNotMatchFile, new String JavaDoc[] { name }));
224                     return;
225                 }
226             }
227         }
228         setError(null);
229     }
230     
231     private void setError(String JavaDoc text) {
232         if (text == null) {
233             statusMessageLabel.setText(""); //$NON-NLS-1$
234
getButton(IDialogConstants.OK_ID).setEnabled(true);
235         } else {
236             statusMessageLabel.setText(text);
237             statusMessageLabel.setForeground(JFaceColors.getErrorText(getShell().getDisplay()));
238             getButton(IDialogConstants.OK_ID).setEnabled(false);
239         }
240     }
241 }
242
Popular Tags