KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > nls > NLSAccessorConfigurationDialog


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.jdt.internal.ui.refactoring.nls;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Path;
19
20 import org.eclipse.core.resources.IFile;
21
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.graphics.GC;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.swt.widgets.Text;
30
31 import org.eclipse.jface.dialogs.Dialog;
32 import org.eclipse.jface.dialogs.StatusDialog;
33 import org.eclipse.jface.window.Window;
34
35 import org.eclipse.ui.PlatformUI;
36 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
37 import org.eclipse.ui.progress.IProgressService;
38
39 import org.eclipse.jdt.core.ICompilationUnit;
40 import org.eclipse.jdt.core.IJavaElement;
41 import org.eclipse.jdt.core.IJavaProject;
42 import org.eclipse.jdt.core.IPackageFragment;
43 import org.eclipse.jdt.core.IPackageFragmentRoot;
44 import org.eclipse.jdt.core.IType;
45 import org.eclipse.jdt.core.JavaConventions;
46 import org.eclipse.jdt.core.JavaModelException;
47 import org.eclipse.jdt.core.search.IJavaSearchConstants;
48 import org.eclipse.jdt.core.search.IJavaSearchScope;
49 import org.eclipse.jdt.core.search.SearchEngine;
50
51 import org.eclipse.jdt.internal.corext.refactoring.nls.NLSRefactoring;
52 import org.eclipse.jdt.internal.corext.util.Messages;
53
54 import org.eclipse.jdt.ui.JavaElementLabelProvider;
55
56 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
57 import org.eclipse.jdt.internal.ui.dialogs.FilteredTypesSelectionDialog;
58 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
59 import org.eclipse.jdt.internal.ui.dialogs.StatusUtil;
60 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
61 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
62 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
63 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
64 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
65 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
66 import org.eclipse.jdt.internal.ui.wizards.dialogfields.Separator;
67 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
68 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
69
70 public class NLSAccessorConfigurationDialog extends StatusDialog {
71
72
73     private SourceFirstPackageSelectionDialogField fResourceBundlePackage;
74     private StringButtonDialogField fResourceBundleFile;
75     private SourceFirstPackageSelectionDialogField fAccessorPackage;
76     private StringDialogField fAccessorClassName;
77     private StringDialogField fSubstitutionPattern;
78
79     private NLSRefactoring fRefactoring;
80     
81     private IStatus[] fStati;
82     
83     private static final int IDX_ACCESSOR_CLASS= 0;
84     private static final int IDX_ACCESSOR_PACKAGE= 1;
85     private static final int IDX_SUBST_PATTERN= 2;
86     private static final int IDX_BUNDLE_NAME= 3;
87     private static final int IDX_BUNDLE_PACKAGE= 4;
88
89     private class AccessorAdapter implements IDialogFieldListener, IStringButtonAdapter {
90         public void dialogFieldChanged(DialogField field) {
91             validateAll();
92         }
93
94         public void changeControlPressed(DialogField field) {
95             if (field == fResourceBundleFile) {
96                 browseForPropertyFile();
97             } else if (field == fAccessorClassName) {
98                 browseForAccessorClass();
99             }
100         }
101     }
102     
103     
104     public NLSAccessorConfigurationDialog(Shell parent, NLSRefactoring refactoring) {
105         super(parent);
106         setShellStyle(getShellStyle() | SWT.RESIZE);
107         
108         fRefactoring= refactoring;
109         fStati= new IStatus[] { StatusInfo.OK_STATUS, StatusInfo.OK_STATUS, StatusInfo.OK_STATUS, StatusInfo.OK_STATUS, StatusInfo.OK_STATUS };
110         
111         setTitle(NLSUIMessages.NLSAccessorConfigurationDialog_title);
112
113         AccessorAdapter updateListener= new AccessorAdapter();
114
115         ICompilationUnit cu= refactoring.getCu();
116
117         fAccessorPackage= new SourceFirstPackageSelectionDialogField(NLSUIMessages.NLSAccessorConfigurationDialog_accessor_path,
118                 NLSUIMessages.NLSAccessorConfigurationDialog_accessor_package,
119                 NLSUIMessages.NLSAccessorConfigurationDialog_browse1,
120                 NLSUIMessages.NLSAccessorConfigurationDialog_browse2,
121                 NLSUIMessages.NLSAccessorConfigurationDialog_default_package,
122                 NLSUIMessages.NLSAccessorConfigurationDialog_accessor_dialog_title,
123                 NLSUIMessages.NLSAccessorConfigurationDialog_accessor_dialog_message,
124                 NLSUIMessages.NLSAccessorConfigurationDialog_accessor_dialog_emtpyMessage,
125                 cu, updateListener, refactoring.getAccessorClassPackage());
126
127         fAccessorClassName= createStringButtonField(NLSUIMessages.NLSAccessorConfigurationDialog_className,
128                 NLSUIMessages.NLSAccessorConfigurationDialog_browse6, updateListener);
129         fSubstitutionPattern= createStringField(NLSUIMessages.NLSAccessorConfigurationDialog_substitutionPattern, updateListener);
130
131         fResourceBundlePackage= new SourceFirstPackageSelectionDialogField(NLSUIMessages.NLSAccessorConfigurationDialog_property_path,
132                 NLSUIMessages.NLSAccessorConfigurationDialog_property_package,
133                 NLSUIMessages.NLSAccessorConfigurationDialog_browse3,
134                 NLSUIMessages.NLSAccessorConfigurationDialog_browse4,
135                 NLSUIMessages.NLSAccessorConfigurationDialog_default_package,
136                 NLSUIMessages.NLSAccessorConfigurationDialog_property_dialog_title,
137                 NLSUIMessages.NLSAccessorConfigurationDialog_property_dialog_message,
138                 NLSUIMessages.NLSAccessorConfigurationDialog_property_dialog_emptyMessage,
139                 cu, updateListener, fRefactoring.getResourceBundlePackage());
140
141         fResourceBundleFile= createStringButtonField(NLSUIMessages.NLSAccessorConfigurationDialog_property_file_name,
142                 NLSUIMessages.NLSAccessorConfigurationDialog_browse5, updateListener);
143
144         initFields();
145     }
146
147     private void initFields() {
148         initAccessorClassFields();
149         String JavaDoc resourceBundleName= fRefactoring.getResourceBundleName();
150         fResourceBundleFile.setText(resourceBundleName != null ? resourceBundleName : (NLSRefactoring.DEFAULT_PROPERTY_FILENAME + NLSRefactoring.PROPERTY_FILE_EXT));
151     }
152
153     private void initAccessorClassFields() {
154         String JavaDoc accessorClassName= fRefactoring.getAccessorClassName();
155
156         if (accessorClassName == null) {
157             accessorClassName= NLSRefactoring.DEFAULT_ACCESSOR_CLASSNAME;
158         }
159         fAccessorClassName.setText(accessorClassName);
160
161         fSubstitutionPattern.setText(fRefactoring.getSubstitutionPattern());
162     }
163
164     protected Control createDialogArea(Composite ancestor) {
165         Composite parent= (Composite) super.createDialogArea(ancestor);
166
167         final int nOfColumns= 4;
168
169         initializeDialogUnits(ancestor);
170
171         GridLayout layout= (GridLayout) parent.getLayout();
172         layout.numColumns= nOfColumns;
173         parent.setLayout(layout);
174
175         createAccessorPart(parent, nOfColumns, convertWidthInCharsToPixels(40));
176
177         Separator s= new Separator(SWT.SEPARATOR | SWT.HORIZONTAL);
178         s.doFillIntoGrid(parent, nOfColumns);
179
180         createPropertyPart(parent, nOfColumns, convertWidthInCharsToPixels(40));
181
182         Dialog.applyDialogFont(parent);
183         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IJavaHelpContextIds.EXTERNALIZE_WIZARD_PROPERTIES_FILE_PAGE);
184         validateAll();
185         return parent;
186     }
187
188
189     private void createAccessorPart(Composite parent, final int nOfColumns, int textWidth) {
190         createLabel(parent, NLSUIMessages.NLSAccessorConfigurationDialog_resourceBundle_title, nOfColumns);
191         fAccessorPackage.createControl(parent, nOfColumns, textWidth);
192
193         fAccessorClassName.doFillIntoGrid(parent, nOfColumns);
194         Text accessorClassText= fAccessorClassName.getTextControl(null);
195         LayoutUtil.setWidthHint(accessorClassText, convertWidthInCharsToPixels(60));
196         TextFieldNavigationHandler.install(accessorClassText);
197         
198         fSubstitutionPattern.doFillIntoGrid(parent, nOfColumns);
199         Text substitutionPatternText= fSubstitutionPattern.getTextControl(null);
200         LayoutUtil.setWidthHint(substitutionPatternText, convertWidthInCharsToPixels(60));
201         TextFieldNavigationHandler.install(substitutionPatternText);
202         
203         fSubstitutionPattern.setEnabled(!fRefactoring.isEclipseNLS());
204     }
205
206     private void createPropertyPart(Composite parent, final int nOfColumns, final int textWidth) {
207         createLabel(parent, NLSUIMessages.NLSAccessorConfigurationDialog_property_location, nOfColumns);
208         fResourceBundlePackage.createControl(parent, nOfColumns, textWidth);
209
210         fResourceBundleFile.doFillIntoGrid(parent, nOfColumns);
211         LayoutUtil.setWidthHint(fResourceBundleFile.getTextControl(null), convertWidthInCharsToPixels(60));
212     }
213
214     private void createLabel(Composite parent, final String JavaDoc text, final int N_OF_COLUMNS) {
215         Separator label= new Separator(SWT.NONE);
216         ((Label) label.getSeparator(parent)).setText(text);
217         GC gc= new GC(parent);
218         int height= gc.stringExtent(text).y;
219         gc.dispose();
220         label.doFillIntoGrid(parent, N_OF_COLUMNS, height);
221     }
222
223     private void browseForPropertyFile() {
224         ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), new JavaElementLabelProvider());
225         dialog.setIgnoreCase(false);
226         dialog.setTitle(NLSUIMessages.NLSAccessorConfigurationDialog_Property_File_Selection);
227         dialog.setMessage(NLSUIMessages.NLSAccessorConfigurationDialog_Choose_the_property_file);
228         dialog.setElements(createFileListInput());
229         dialog.setFilter('*' + NLSRefactoring.PROPERTY_FILE_EXT);
230         if (dialog.open() == Window.OK) {
231             IFile selectedFile= (IFile) dialog.getFirstResult();
232             if (selectedFile != null)
233                 fResourceBundleFile.setText(selectedFile.getName());
234         }
235     }
236
237     protected void browseForAccessorClass() {
238         IProgressService service= PlatformUI.getWorkbench().getProgressService();
239         IPackageFragmentRoot root= fAccessorPackage.getSelectedFragmentRoot();
240         
241         IJavaSearchScope scope= root != null ? SearchEngine.createJavaSearchScope(new IJavaElement[] { root }) : SearchEngine.createWorkspaceScope();
242         
243         FilteredTypesSelectionDialog dialog= new FilteredTypesSelectionDialog (getShell(), false,
244             service, scope, IJavaSearchConstants.CLASS);
245         dialog.setTitle(NLSUIMessages.NLSAccessorConfigurationDialog_Accessor_Selection);
246         dialog.setMessage(NLSUIMessages.NLSAccessorConfigurationDialog_Choose_the_accessor_file);
247         dialog.setInitialPattern("*Messages"); //$NON-NLS-1$
248
if (dialog.open() == Window.OK) {
249             IType selectedType= (IType) dialog.getFirstResult();
250             if (selectedType != null) {
251                 fAccessorClassName.setText(selectedType.getElementName());
252                 fAccessorPackage.setSelected(selectedType.getPackageFragment());
253             }
254         }
255
256
257     }
258
259     private Object JavaDoc[] createFileListInput() {
260         try {
261
262             IPackageFragment fPkgFragment= fResourceBundlePackage.getSelected();
263             if (fPkgFragment == null)
264                 return new Object JavaDoc[0];
265             List JavaDoc result= new ArrayList JavaDoc(1);
266             Object JavaDoc[] nonjava= fPkgFragment.getNonJavaResources();
267             for (int i= 0; i < nonjava.length; i++) {
268                 if (isPropertyFile(nonjava[i]))
269                     result.add(nonjava[i]);
270             }
271             return result.toArray();
272
273         } catch (JavaModelException e) {
274             ExceptionHandler.handle(e, NLSUIMessages.NLSAccessorConfigurationDialog_externalizing, NLSUIMessages .NLSAccessorConfigurationDialog_exception);
275             return new Object JavaDoc[0];
276         }
277     }
278
279     private static boolean isPropertyFile(Object JavaDoc o) {
280         if (!(o instanceof IFile))
281             return false;
282         IFile file= (IFile) o;
283         return (NLSRefactoring.PROPERTY_FILE_EXT.equals('.' + file.getFileExtension()));
284     }
285
286     /**
287      * checks all entered values delegates to the specific validate methods these methods
288      * update the refactoring
289      */

290     private void validateAll() {
291         validateSubstitutionPattern();
292
293         validateAccessorClassName();
294         checkPackageFragment();
295
296         validatePropertyFilename();
297         validatePropertyPackage();
298         
299         updateStatus(StatusUtil.getMostSevere(fStati));
300     }
301
302     private void validateAccessorClassName() {
303         String JavaDoc className= fAccessorClassName.getText();
304
305         IStatus status= JavaConventions.validateJavaTypeName(className);
306         if (status.getSeverity() == IStatus.ERROR) {
307             setInvalid(IDX_ACCESSOR_CLASS, status.getMessage());
308             return;
309         }
310
311         if (className.indexOf('.') != -1) {
312             setInvalid(IDX_ACCESSOR_CLASS, NLSUIMessages.NLSAccessorConfigurationDialog_no_dot);
313             return;
314         }
315
316         setValid(IDX_ACCESSOR_CLASS);
317     }
318
319     private void validatePropertyFilename() {
320         String JavaDoc fileName= fResourceBundleFile.getText();
321         if ((fileName == null) || (fileName.length() == 0)) {
322             setInvalid(IDX_BUNDLE_NAME, NLSUIMessages.NLSAccessorConfigurationDialog_enter_name);
323             return;
324         }
325
326         if (!fileName.endsWith(NLSRefactoring.PROPERTY_FILE_EXT)) {
327             setInvalid(IDX_BUNDLE_NAME, Messages.format(NLSUIMessages.NLSAccessorConfigurationDialog_file_name_must_end, NLSRefactoring.PROPERTY_FILE_EXT));
328             return;
329         }
330
331         setValid(IDX_BUNDLE_NAME);
332     }
333
334     private void validatePropertyPackage() {
335         
336         IPackageFragmentRoot root= fResourceBundlePackage.getSelectedFragmentRoot();
337         if ((root == null) || !root.exists()) {
338             setInvalid(IDX_BUNDLE_PACKAGE, NLSUIMessages.NLSAccessorConfigurationDialog_property_package_root_invalid);
339             return;
340         }
341
342         IPackageFragment fragment= fResourceBundlePackage.getSelected();
343         if ((fragment == null) || !fragment.exists()) {
344             setInvalid(IDX_BUNDLE_PACKAGE, NLSUIMessages.NLSAccessorConfigurationDialog_property_package_invalid);
345             return;
346         }
347         
348         String JavaDoc pkgName= fragment.getElementName();
349
350         IStatus status= JavaConventions.validatePackageName(pkgName);
351         if ((pkgName.length() > 0) && (status.getSeverity() == IStatus.ERROR)) {
352             setInvalid(IDX_BUNDLE_PACKAGE, status.getMessage());
353             return;
354         }
355
356         IPath pkgPath= new Path(pkgName.replace('.', IPath.SEPARATOR)).makeRelative();
357
358         IJavaProject project= fRefactoring.getCu().getJavaProject();
359         try {
360             IJavaElement element= project.findElement(pkgPath);
361             if (element == null || !element.exists()) {
362                 setInvalid(IDX_BUNDLE_PACKAGE, NLSUIMessages.NLSAccessorConfigurationDialog_must_exist);
363                 return;
364             }
365             IPackageFragment fPkgFragment= (IPackageFragment) element;
366             if (!PackageBrowseAdapter.canAddPackage(fPkgFragment)) {
367                 setInvalid(IDX_BUNDLE_PACKAGE, NLSUIMessages.NLSAccessorConfigurationDialog_incorrect_package);
368                 return;
369             }
370             if (!PackageBrowseAdapter.canAddPackageRoot((IPackageFragmentRoot) fPkgFragment.getParent())) {
371                 setInvalid(IDX_BUNDLE_PACKAGE, NLSUIMessages.NLSAccessorConfigurationDialog_incorrect_package);
372                 return;
373             }
374         } catch (JavaModelException e) {
375             setInvalid(IDX_BUNDLE_PACKAGE, e.getStatus().getMessage());
376             return;
377         }
378
379         setValid(IDX_BUNDLE_PACKAGE);
380     }
381
382     private void checkPackageFragment() {
383         IPackageFragmentRoot root= fAccessorPackage.getSelectedFragmentRoot();
384         if ((root == null) || !root.exists()) {
385             setInvalid(IDX_ACCESSOR_PACKAGE, NLSUIMessages.NLSAccessorConfigurationDialog_accessor_package_root_invalid);
386             return;
387         }
388
389         IPackageFragment fragment= fAccessorPackage.getSelected();
390         if ((fragment == null) || !fragment.exists()) {
391             setInvalid(IDX_ACCESSOR_PACKAGE, NLSUIMessages.NLSAccessorConfigurationDialog_accessor_package_invalid);
392             return;
393         }
394         setValid(IDX_ACCESSOR_PACKAGE);
395     }
396
397     private void validateSubstitutionPattern() {
398         if ((fSubstitutionPattern.getText() == null) || (fSubstitutionPattern.getText().length() == 0)) {
399             setInvalid(IDX_SUBST_PATTERN, NLSUIMessages.NLSAccessorConfigurationDialog_substitution_pattern_missing);
400         } else {
401             setValid(IDX_SUBST_PATTERN);
402         }
403     }
404
405     private void setInvalid(int idx, String JavaDoc msg) {
406         fStati[idx]= new StatusInfo(IStatus.ERROR, msg);
407     }
408
409     private void setValid(int idx) {
410         fStati[idx]= StatusInfo.OK_STATUS;
411     }
412
413     /* (non-Javadoc)
414      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
415      */

416     protected void okPressed() {
417         updateRefactoring();
418         super.okPressed();
419     }
420
421
422     void updateRefactoring() {
423         fRefactoring.setAccessorClassPackage(fAccessorPackage.getSelected());
424         fRefactoring.setAccessorClassName(fAccessorClassName.getText());
425
426         fRefactoring.setResourceBundleName(fResourceBundleFile.getText());
427         fRefactoring.setResourceBundlePackage(fResourceBundlePackage.getSelected());
428
429         if (!fRefactoring.isEclipseNLS())
430             fRefactoring.setSubstitutionPattern(fSubstitutionPattern.getText());
431         
432         fRefactoring.setIsEclipseNLS(fRefactoring.detectIsEclipseNLS());
433     }
434
435     private StringDialogField createStringField(String JavaDoc label, AccessorAdapter updateListener) {
436         StringDialogField field= new StringDialogField();
437         field.setDialogFieldListener(updateListener);
438         field.setLabelText(label);
439         return field;
440     }
441
442     private StringButtonDialogField createStringButtonField(String JavaDoc label, String JavaDoc button, AccessorAdapter adapter) {
443         StringButtonDialogField field= new StringButtonDialogField(adapter);
444         field.setDialogFieldListener(adapter);
445         field.setLabelText(label);
446         field.setButtonLabel(button);
447         return field;
448     }
449
450 }
451
Popular Tags