KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > nls > ExternalizeStringsWizardPage


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.pde.internal.ui.nls;
12
13 import java.util.Properties JavaDoc;
14
15 import org.eclipse.core.filebuffers.FileBuffers;
16 import org.eclipse.core.filebuffers.ITextFileBufferManager;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.NullProgressMonitor;
20 import org.eclipse.jface.dialogs.Dialog;
21 import org.eclipse.jface.resource.JFaceResources;
22 import org.eclipse.jface.text.Document;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.source.SourceViewer;
25 import org.eclipse.jface.viewers.CellEditor;
26 import org.eclipse.jface.viewers.CheckStateChangedEvent;
27 import org.eclipse.jface.viewers.CheckboxTableViewer;
28 import org.eclipse.jface.viewers.ICellModifier;
29 import org.eclipse.jface.viewers.ICheckStateListener;
30 import org.eclipse.jface.viewers.IContentProvider;
31 import org.eclipse.jface.viewers.ISelectionChangedListener;
32 import org.eclipse.jface.viewers.IStructuredContentProvider;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.jface.viewers.ITreeContentProvider;
35 import org.eclipse.jface.viewers.SelectionChangedEvent;
36 import org.eclipse.jface.viewers.StructuredSelection;
37 import org.eclipse.jface.viewers.TextCellEditor;
38 import org.eclipse.jface.viewers.Viewer;
39 import org.eclipse.jface.viewers.ViewerFilter;
40 import org.eclipse.jface.wizard.WizardPage;
41 import org.eclipse.pde.core.plugin.IPluginModelBase;
42 import org.eclipse.pde.internal.ui.IHelpContextIds;
43 import org.eclipse.pde.internal.ui.PDEPlugin;
44 import org.eclipse.pde.internal.ui.PDEUIMessages;
45 import org.eclipse.pde.internal.ui.editor.context.ManifestDocumentSetupParticipant;
46 import org.eclipse.pde.internal.ui.editor.context.XMLDocumentSetupParticpant;
47 import org.eclipse.pde.internal.ui.editor.text.ColorManager;
48 import org.eclipse.pde.internal.ui.editor.text.IColorManager;
49 import org.eclipse.pde.internal.ui.editor.text.ManifestConfiguration;
50 import org.eclipse.pde.internal.ui.editor.text.XMLConfiguration;
51 import org.eclipse.pde.internal.ui.wizards.ListUtil;
52 import org.eclipse.swt.SWT;
53 import org.eclipse.swt.custom.SashForm;
54 import org.eclipse.swt.events.ModifyEvent;
55 import org.eclipse.swt.events.ModifyListener;
56 import org.eclipse.swt.events.SelectionAdapter;
57 import org.eclipse.swt.events.SelectionEvent;
58 import org.eclipse.swt.layout.GridData;
59 import org.eclipse.swt.layout.GridLayout;
60 import org.eclipse.swt.widgets.Button;
61 import org.eclipse.swt.widgets.Composite;
62 import org.eclipse.swt.widgets.Control;
63 import org.eclipse.swt.widgets.Label;
64 import org.eclipse.swt.widgets.Table;
65 import org.eclipse.swt.widgets.TableColumn;
66 import org.eclipse.swt.widgets.TableItem;
67 import org.eclipse.swt.widgets.Text;
68 import org.eclipse.swt.widgets.TreeItem;
69 import org.eclipse.text.edits.MalformedTreeException;
70 import org.eclipse.ui.PlatformUI;
71 import org.eclipse.ui.dialogs.ContainerCheckedTreeViewer;
72
73 public class ExternalizeStringsWizardPage extends WizardPage {
74
75     public static final String JavaDoc PAGE_NAME = "ExternalizeStringsWizardPage"; //$NON-NLS-1$
76

77     public static final int EXTERN = 0;
78     public static final int VALUE = 1;
79     public static final int KEY = 2;
80     private static final int SIZE = 3; // column counter
81
private static final String JavaDoc[] TABLE_PROPERTIES = new String JavaDoc[SIZE];
82     private static final String JavaDoc[] TABLE_COLUMNS = new String JavaDoc[SIZE];
83     
84     static {
85         TABLE_PROPERTIES[EXTERN] = "extern"; //$NON-NLS-1$
86
TABLE_PROPERTIES[VALUE] = "value"; //$NON-NLS-1$
87
TABLE_PROPERTIES[KEY] = "key"; //$NON-NLS-1$
88
TABLE_COLUMNS[EXTERN] = ""; //$NON-NLS-1$
89
TABLE_COLUMNS[VALUE] = PDEUIMessages.ExternalizeStringsWizardPage_value;
90         TABLE_COLUMNS[KEY] = PDEUIMessages.ExternalizeStringsWizardPage_subKey;
91     }
92
93     private class ModelChangeContentProvider implements ITreeContentProvider, IContentProvider {
94         
95         public Object JavaDoc[] getElements(Object JavaDoc parent) {
96             return fModelChangeTable.getAllModelChanges().toArray();
97         }
98
99         public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
100             if (!(parentElement instanceof ModelChange))
101                 return new Object JavaDoc[0];
102             return ((ModelChange)parentElement).getModelChangeFiles();
103         }
104
105         public Object JavaDoc getParent(Object JavaDoc element) {
106             if (element instanceof ModelChangeFile) {
107                 return ((ModelChangeFile)element).getModel();
108             }
109             return null;
110         }
111
112         public boolean hasChildren(Object JavaDoc element) {
113             return element instanceof ModelChange;
114         }
115
116         public void dispose() {
117         }
118
119         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
120         }
121     }
122
123     private class ExternalizeStringsCellModifier implements ICellModifier {
124
125         public boolean canModify(Object JavaDoc element, String JavaDoc property) {
126             return (property != null &&
127                     (element instanceof ModelChangeElement) &&
128                     !TABLE_PROPERTIES[VALUE].equals(property) &&
129                     (isPageComplete() || element.equals(fErrorElement)) &&
130                     (TABLE_PROPERTIES[KEY].equals(property) && ((ModelChangeElement)element).isExternalized()));
131
132         }
133
134         public Object JavaDoc getValue(Object JavaDoc element, String JavaDoc property) {
135             if (element instanceof ModelChangeElement) {
136                 ModelChangeElement changeElement = (ModelChangeElement) element;
137                 if (TABLE_PROPERTIES[KEY].equals(property)) {
138                     return StringHelper.unwindEscapeChars(changeElement.getKey());
139                 }
140             }
141             return ""; //$NON-NLS-1$
142
}
143
144         public void modify(Object JavaDoc element, String JavaDoc property, Object JavaDoc value) {
145             if (element instanceof TableItem) {
146                 Object JavaDoc data = ((TableItem) element).getData();
147                 if (data instanceof ModelChangeElement) {
148                     ModelChangeElement changeElement = (ModelChangeElement) data;
149                     if (TABLE_PROPERTIES[KEY].equals(property)) {
150                         String JavaDoc newKey = StringHelper.windEscapeChars((String JavaDoc)value);
151                         validateKey(newKey, changeElement);
152                         changeElement.setKey(newKey);
153                         fPropertiesViewer.update(data, null);
154                     }
155                 }
156             }
157         }
158     }
159     
160     
161     
162     private ModelChangeTable fModelChangeTable;
163     
164     private ContainerCheckedTreeViewer fInputViewer;
165     private Button fSelectAll;
166     private Button fDeselectAll;
167     private Label fProjectLabel;
168     private Text fLocalizationText;
169     private CheckboxTableViewer fPropertiesViewer;
170     private Table fTable;
171     private SourceViewer fSourceViewer;
172     
173     private ViewerFilter fErrorElementFilter;
174     private ModifyListener fModifyListener;
175     
176     private Object JavaDoc fCurrSelection;
177     private ModelChangeElement fErrorElement;
178     private String JavaDoc fPreErrorKey;
179
180     private IDocument fEmptyDoc;
181     private IColorManager fColorManager;
182     private XMLConfiguration fXMLConfig;
183     private XMLDocumentSetupParticpant fXMLSetupParticipant;
184     private ManifestDocumentSetupParticipant fManifestSetupParticipant;
185
186     private ManifestConfiguration fManifestConfig;
187     
188     protected ExternalizeStringsWizardPage(ModelChangeTable changeTable) {
189         super(PAGE_NAME);
190         setTitle(PDEUIMessages.ExternalizeStringsWizardPage_pageTitle);
191         setDescription(PDEUIMessages.ExternalizeStringsWizardPage_pageDescription);
192         fModelChangeTable = changeTable;
193         fErrorElementFilter = new ViewerFilter() {
194             public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
195                 if (!(element instanceof ModelChangeElement))
196                     return false;
197                 ModelChangeElement change = (ModelChangeElement) element;
198                 return change.equals(fErrorElement);
199             }
200         };
201         fModifyListener = new ModifyListener() {
202             public void modifyText(ModifyEvent e) {
203                 String JavaDoc localization = fLocalizationText.getText();
204                 if (StringHelper.isValidLocalization(localization)) {
205                     setEnabled(fLocalizationText, true);
206                     setPageComplete(hasCheckedElements());
207                     setErrorMessage(null);
208                     if (fCurrSelection instanceof ModelChange) {
209                         ((ModelChange)fCurrSelection).setBundleLocalization(fLocalizationText.getText());
210                     } else if (fCurrSelection instanceof ModelChangeFile) {
211                         ((ModelChangeFile)fCurrSelection).getModel().setBundleLocalization(fLocalizationText.getText());
212                     }
213                 } else {
214                     setEnabled(fLocalizationText, false);
215                     fLocalizationText.setEditable(true);
216                     setPageComplete(false);
217                     setErrorMessage(PDEUIMessages.ExternalizeStringsWizardPage_badLocalizationError);
218                 }
219             }
220         };
221         fColorManager = ColorManager.getDefault();
222         fXMLConfig = new XMLConfiguration(fColorManager);
223         fXMLSetupParticipant = new XMLDocumentSetupParticpant();
224         fManifestConfig = new ManifestConfiguration(fColorManager);
225         fManifestSetupParticipant = new ManifestDocumentSetupParticipant();
226     }
227     
228     public void dispose() {
229         fColorManager.dispose();
230         super.dispose();
231     }
232     
233     public void createControl(Composite parent) {
234
235         SashForm superSash = new SashForm(parent, SWT.HORIZONTAL);
236         superSash.setFont(parent.getFont());
237         superSash.setLayoutData(new GridData(GridData.FILL_BOTH));
238         
239         createInputContents(superSash);
240         
241         SashForm sash = new SashForm(superSash, SWT.VERTICAL);
242         sash.setFont(superSash.getFont());
243         sash.setLayoutData(new GridData(GridData.FILL_BOTH));
244
245         createTableViewer(sash);
246         createSourceViewer(sash);
247         initialize();
248         
249         setPageComplete(hasCheckedElements());
250         
251         superSash.setWeights(new int[] {4,7});
252         setControl(superSash);
253         Dialog.applyDialogFont(superSash);
254
255         PlatformUI.getWorkbench().getHelpSystem().setHelp(superSash, IHelpContextIds.EXTERNALIZE_STRINGS_PAGE);
256     }
257
258     private void createInputContents(Composite composite) {
259         Composite fileComposite = new Composite(composite, SWT.NONE);
260         fileComposite.setLayout(new GridLayout());
261         fileComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
262
263         Label label = new Label(fileComposite, SWT.NONE);
264         label.setText(PDEUIMessages.ExternalizeStringsWizardPage_resourcelabel);
265         fInputViewer = new ContainerCheckedTreeViewer(fileComposite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE | SWT.BORDER);
266         fInputViewer.setContentProvider(new ModelChangeContentProvider());
267         fInputViewer.setLabelProvider(new ModelChangeLabelProvider());
268         GridData gd = new GridData(GridData.FILL_BOTH);
269         gd.heightHint = 250;
270         fInputViewer.getTree().setLayoutData(gd);
271         fInputViewer.addSelectionChangedListener(new ISelectionChangedListener() {
272             public void selectionChanged(SelectionChangedEvent event) {
273                 handleSelectionChanged(event);
274             }
275         });
276         fInputViewer.addCheckStateListener(new ICheckStateListener() {
277             public void checkStateChanged(CheckStateChangedEvent event) {
278                 setPageComplete(hasCheckedElements());
279             }
280         });
281         fInputViewer.setComparator(ListUtil.PLUGIN_COMPARATOR);
282         
283         Composite buttonComposite = new Composite(fileComposite, SWT.NONE);
284         GridLayout layout = new GridLayout(2, true);
285         layout.marginHeight = layout.marginWidth = 0;
286         buttonComposite.setLayout(layout);
287         buttonComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
288         
289         fSelectAll = new Button(buttonComposite, SWT.PUSH);
290         fSelectAll.setText(PDEUIMessages.ExternalizeStringsWizardPage_selectAllButton);
291         fSelectAll.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
292         fSelectAll.addSelectionListener(new SelectionAdapter() {
293             public void widgetSelected(SelectionEvent e) {
294                 fInputViewer.setCheckedElements(fModelChangeTable.getAllModelChanges().toArray());
295                 setPageComplete(hasCheckedElements());
296             }
297         });
298         fDeselectAll = new Button(buttonComposite, SWT.PUSH);
299         fDeselectAll.setText(PDEUIMessages.ExternalizeStringsWizardPage_deselectAllButton);
300         fDeselectAll.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
301         fDeselectAll.addSelectionListener(new SelectionAdapter() {
302             public void widgetSelected(SelectionEvent e) {
303                 fInputViewer.setCheckedElements(new Object JavaDoc[0]);
304                 setPageComplete(hasCheckedElements());
305             }
306         });
307         
308         Composite infoComposite = new Composite(fileComposite, SWT.NONE);
309         layout = new GridLayout();
310         layout.marginHeight = 0;
311         infoComposite.setLayout(layout);
312         infoComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
313         
314         Label project = new Label(infoComposite, SWT.NONE);
315         project.setText(PDEUIMessages.ExternalizeStringsWizardPage_projectLabel);
316         fProjectLabel = new Label(infoComposite, SWT.NONE);
317         gd = new GridData(GridData.FILL_HORIZONTAL);
318         gd.horizontalIndent = 10;
319         fProjectLabel.setLayoutData(gd);
320         fProjectLabel.setText(PDEUIMessages.ExternalizeStringsWizardPage_noUnderlyingResource);
321         
322         Label properties = new Label(infoComposite, SWT.NONE);
323         properties.setText(PDEUIMessages.ExternalizeStringsWizardPage_localizationLabel);
324         fLocalizationText = new Text(infoComposite, SWT.BORDER);
325         gd = new GridData(GridData.FILL_HORIZONTAL);
326         gd.horizontalIndent = 10;
327         fLocalizationText.setLayoutData(gd);
328         fLocalizationText.setText(PDEUIMessages.ExternalizeStringsWizardPage_noUnderlyingResource);
329         fLocalizationText.addModifyListener(fModifyListener);
330         
331         fInputViewer.setInput(PDEPlugin.getDefault());
332     }
333
334     private void createTableViewer(Composite parent) {
335         Composite composite = new Composite(parent, SWT.NONE);
336         composite.setFont(parent.getFont());
337         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
338         composite.setLayout(new GridLayout());
339                 
340         Label label = new Label(composite, SWT.NONE);
341         label.setText(PDEUIMessages.ExternalizeStringsWizardPage_propertiesLabel);
342         label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
343         
344         fPropertiesViewer = CheckboxTableViewer.newCheckList(composite,
345                 SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION | SWT.BORDER);
346         fTable = fPropertiesViewer.getTable();
347         fTable.setFont(composite.getFont());
348         fTable.setLayoutData(new GridData(GridData.FILL_BOTH));
349         fTable.setLayout(new GridLayout());
350         fTable.setLinesVisible(true);
351         fTable.setHeaderVisible(true);
352
353         for (int i= 0; i < TABLE_COLUMNS.length; i++) {
354             TableColumn tc = new TableColumn(fTable, SWT.NONE);
355             tc.setText(TABLE_COLUMNS[i]);
356             tc.setResizable(i != 0);
357             tc.setWidth(i == 0 ? 20 : 200);
358         }
359         
360         fPropertiesViewer.setUseHashlookup(true);
361         fPropertiesViewer.setCellEditors(createCellEditors());
362         fPropertiesViewer.setColumnProperties(TABLE_PROPERTIES);
363         fPropertiesViewer.setCellModifier(new ExternalizeStringsCellModifier());
364         fPropertiesViewer.setContentProvider(new IStructuredContentProvider() {
365             public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
366                 if (fInputViewer.getSelection() instanceof IStructuredSelection) {
367                     Object JavaDoc selection = ((IStructuredSelection)fInputViewer.getSelection()).getFirstElement();
368                     if (selection instanceof ModelChangeFile) {
369                         ModelChangeFile cf = (ModelChangeFile)selection;
370                         return (cf).getModel().getChangesInFile(cf.getFile()).toArray();
371                     }
372                 }
373                 return new Object JavaDoc[0];
374             }
375             public void dispose() {
376             }
377             public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
378             }
379         });
380         fPropertiesViewer.setLabelProvider(new ExternalizeStringsLabelProvider());
381         fPropertiesViewer.addSelectionChangedListener(new ISelectionChangedListener() {
382             public void selectionChanged(SelectionChangedEvent event) {
383                 handlePropertySelection();
384             }
385         });
386         fPropertiesViewer.addCheckStateListener(new ICheckStateListener() {
387             public void checkStateChanged(CheckStateChangedEvent event) {
388                 Object JavaDoc element = event.getElement();
389                 if (element instanceof ModelChangeElement) {
390                     ((ModelChangeElement)element).setExternalized(event.getChecked());
391                     fPropertiesViewer.update(element, null);
392                 }
393             }
394         });
395         fPropertiesViewer.setInput(new Object JavaDoc());
396     }
397
398     private void createSourceViewer(Composite parent) {
399         Composite composite = new Composite(parent, SWT.NONE);
400         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
401         composite.setLayout(new GridLayout());
402
403         Label label = new Label(composite, SWT.NONE);
404         label.setText(PDEUIMessages.ExternalizeStringsWizardPage_sourceLabel);
405         label.setLayoutData(new GridData());
406
407         fSourceViewer = new SourceViewer(composite, null, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
408         fSourceViewer.setEditable(false);
409         fSourceViewer.getTextWidget().setFont(JFaceResources.getTextFont());
410         fSourceViewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
411         
412         fEmptyDoc = new Document();
413         fSourceViewer.setDocument(fEmptyDoc);
414     }
415     
416     // must set selection after source viewer is created, otherwise you get an NPE.
417
private void initialize() {
418         Object JavaDoc[] preSelect = fModelChangeTable.getPreSelected();
419         fInputViewer.setSelection(new StructuredSelection(preSelect));
420         fInputViewer.setCheckedElements(fModelChangeTable.getPreSelected());
421     }
422     
423     private void handleSelectionChanged(SelectionChangedEvent event) {
424         if (!(event.getSelection() instanceof IStructuredSelection))
425             return;
426         Object JavaDoc selection = (((IStructuredSelection)event.getSelection()).getFirstElement());
427         if (selection == null) {
428             fCurrSelection = null;
429             fSourceViewer.setDocument(fEmptyDoc);
430         } else if (selection.equals(fCurrSelection)) {
431             return;
432         } else if (selection instanceof ModelChangeFile) {
433             fCurrSelection = selection;
434             IFile file = ((ModelChangeFile)fCurrSelection).getFile();
435             NullProgressMonitor monitor = new NullProgressMonitor();
436             ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
437             try {
438                 try {
439                     manager.connect(file.getFullPath(), monitor);
440                     updateSourceViewer(manager, file);
441                 } catch (MalformedTreeException e) {
442                 } finally {
443                     manager.disconnect(file.getFullPath(), monitor);
444                 }
445             } catch (CoreException e) {
446             }
447         } else if (selection instanceof ModelChange) {
448             fCurrSelection = selection;
449             fSourceViewer.setDocument(fEmptyDoc);
450             updatePropertiesLabel(((ModelChange)fCurrSelection).getParentModel());
451         }
452         refreshPropertiesViewer(false);
453     }
454     
455     private void refreshPropertiesViewer(boolean updateLabels) {
456         fPropertiesViewer.refresh(updateLabels);
457         TableItem[] items = fTable.getItems();
458         for (int i = 0; i < items.length; i++) {
459             if (!(items[i].getData() instanceof ModelChangeElement)) continue;
460             ModelChangeElement element = (ModelChangeElement)items[i].getData();
461             fPropertiesViewer.setChecked(element, element.isExternalized());
462         }
463     }
464     
465     private void updateSourceViewer(ITextFileBufferManager manager, IFile sourceFile) {
466         IDocument document = manager.getTextFileBuffer(sourceFile.getFullPath()).getDocument();
467         TreeItem item = fInputViewer.getTree().getSelection()[0];
468         IPluginModelBase model = ((ModelChange)item.getParentItem().getData()).getParentModel();
469         
470         if (fSourceViewer.getDocument() != null)
471             fSourceViewer.unconfigure();
472         if (sourceFile.getFileExtension().equalsIgnoreCase("xml")) { //$NON-NLS-1$
473
fSourceViewer.configure(fXMLConfig);
474             fXMLSetupParticipant.setup(document);
475         } else {
476             fSourceViewer.configure(fManifestConfig);
477             fManifestSetupParticipant.setup(document);
478         }
479         
480         fSourceViewer.setDocument(document);
481         updatePropertiesLabel(model);
482     }
483     
484     
485     private void updatePropertiesLabel(IPluginModelBase model) {
486         ModelChange modelChange = fModelChangeTable.getModelChange(model);
487         fProjectLabel.setText(model.getUnderlyingResource().getProject().getName());
488         fLocalizationText.setEditable(!modelChange.localizationSet());
489         fLocalizationText.setText(modelChange.getBundleLocalization());
490     }
491     
492     private void handlePropertySelection() {
493         if (!(fPropertiesViewer.getSelection() instanceof IStructuredSelection)) return;
494         Object JavaDoc selection = (((IStructuredSelection)fPropertiesViewer.getSelection()).getFirstElement());
495         if (selection instanceof ModelChangeElement && fSourceViewer.getDocument() != null) {
496             ModelChangeElement element = (ModelChangeElement) selection;
497             int offset = element.getOffset();
498             int length = element.getLength();
499             fSourceViewer.setSelectedRange(offset, length);
500             fSourceViewer.revealRange(offset, length);
501         }
502     }
503     
504     private CellEditor[] createCellEditors() {
505         final CellEditor editors[] = new CellEditor[SIZE];
506         editors[EXTERN] = null;
507         editors[VALUE] = null;
508         editors[KEY] = new TextCellEditor(fTable);
509         return editors;
510     }
511
512     private void validateKey(String JavaDoc key, ModelChangeElement element) {
513         ModelChange modelChange = ((ModelChangeFile)fCurrSelection).getModel();
514         Properties JavaDoc properties = modelChange.getProperties();
515         String JavaDoc error = null;
516         String JavaDoc oldKey = (fPreErrorKey != null) ? fPreErrorKey : element.getKey();
517         if (key.equals(fPreErrorKey)) {
518             error = null;
519         } else if (key.trim().length() < 1) {
520             error = getErrorMessage(PDEUIMessages.ExternalizeStringsWizardPage_keyEmptyError, oldKey);
521         } else if (key.charAt(0) == '#' || key.charAt(0) == '!' || key.charAt(0) == '%') {
522             error = getErrorMessage(PDEUIMessages.ExternalizeStringsWizardPage_keyCommentError, oldKey);
523         } else if (key.indexOf(':') != -1 || key.indexOf('=') != -1 || key.indexOf(' ') != -1) {
524             error = getErrorMessage(PDEUIMessages.ExternalizeStringsWizardPage_keyError, oldKey);
525         } else if ((!key.equals(oldKey) || fPreErrorKey != null) &&
526                 properties.containsKey(key)) {
527             error = getErrorMessage(PDEUIMessages.ExternalizeStringsWizardPage_keyDuplicateError, oldKey);
528         }
529
530         setErrorMessage(error);
531         setPageComplete(error == null && hasCheckedElements());
532         if (error == null) {
533             fErrorElement = null;
534             fPreErrorKey = null;
535             setEnabled(fPropertiesViewer.getControl(), true);
536             fPropertiesViewer.removeFilter(fErrorElementFilter);
537             refreshPropertiesViewer(true);
538             properties.setProperty(key, element.getValue());
539         } else if (fPreErrorKey == null) {
540             fErrorElement = element;
541             fPreErrorKey = oldKey;
542             setEnabled(fPropertiesViewer.getControl(), false);
543             fPropertiesViewer.addFilter(fErrorElementFilter);
544         }
545     }
546     
547     private String JavaDoc getErrorMessage(String JavaDoc error, String JavaDoc suggestion) {
548         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(error);
549         if (suggestion != null) {
550             sb.append(PDEUIMessages.ExternalizeStringsWizardPage_keySuggested);
551             sb.append(suggestion);
552         }
553         return sb.toString();
554     }
555     
556     public Object JavaDoc[] getChangeFiles() {
557         return fInputViewer.getCheckedElements();
558     }
559     
560     private boolean hasCheckedElements() {
561         return fInputViewer.getCheckedElements().length > 0;
562     }
563     
564     private void setEnabled(Control exception, boolean enabled) {
565         if (!exception.equals(fInputViewer.getControl()))
566             fInputViewer.getControl().setEnabled(enabled);
567         if (!exception.equals(fPropertiesViewer.getControl()))
568             fPropertiesViewer.getControl().setEnabled(enabled);
569         if (!exception.equals(fLocalizationText))
570             fLocalizationText.setEnabled(enabled);
571         if (!exception.equals(fSelectAll))
572             fSelectAll.setEnabled(enabled);
573         if (!exception.equals(fDeselectAll))
574             fDeselectAll.setEnabled(enabled);
575     }
576 }
577
Popular Tags