KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > preferences > AntPropertiesBlock


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.ant.internal.ui.preferences;
12
13 import com.ibm.icu.text.MessageFormat;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.ant.core.Property;
20 import org.eclipse.ant.internal.ui.AntUIPlugin;
21 import org.eclipse.ant.internal.ui.AntUtil;
22 import org.eclipse.ant.internal.ui.IAntUIConstants;
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.core.variables.VariablesPlugin;
28 import org.eclipse.jface.dialogs.IDialogConstants;
29 import org.eclipse.jface.dialogs.IDialogSettings;
30 import org.eclipse.jface.dialogs.MessageDialog;
31 import org.eclipse.jface.viewers.ColumnLayoutData;
32 import org.eclipse.jface.viewers.ColumnWeightData;
33 import org.eclipse.jface.viewers.DoubleClickEvent;
34 import org.eclipse.jface.viewers.IDoubleClickListener;
35 import org.eclipse.jface.viewers.ISelectionChangedListener;
36 import org.eclipse.jface.viewers.IStructuredSelection;
37 import org.eclipse.jface.viewers.SelectionChangedEvent;
38 import org.eclipse.jface.viewers.TableLayout;
39 import org.eclipse.jface.viewers.TableViewer;
40 import org.eclipse.jface.window.Window;
41 import org.eclipse.swt.SWT;
42 import org.eclipse.swt.events.KeyAdapter;
43 import org.eclipse.swt.events.KeyEvent;
44 import org.eclipse.swt.events.SelectionAdapter;
45 import org.eclipse.swt.events.SelectionEvent;
46 import org.eclipse.swt.graphics.Font;
47 import org.eclipse.swt.layout.GridData;
48 import org.eclipse.swt.layout.GridLayout;
49 import org.eclipse.swt.widgets.Button;
50 import org.eclipse.swt.widgets.Composite;
51 import org.eclipse.swt.widgets.FileDialog;
52 import org.eclipse.swt.widgets.Label;
53 import org.eclipse.swt.widgets.Table;
54 import org.eclipse.swt.widgets.TableColumn;
55
56 public class AntPropertiesBlock {
57     
58     private IAntBlockContainer container;
59     
60     private Button editButton;
61     private Button removeButton;
62     private Button addButton;
63     
64     private Button addFileButton;
65     private Button addExternalFileButton;
66     private Button removeFileButton;
67
68     private TableViewer propertyTableViewer;
69     private TableViewer fileTableViewer;
70
71     private final AntObjectLabelProvider labelProvider = new AntObjectLabelProvider();
72
73     private IDialogSettings dialogSettings;
74     
75     private boolean tablesEnabled= true;
76     
77     private final String JavaDoc[] fTableColumnHeaders= {
78             AntPreferencesMessages.AntPropertiesBlock_0, AntPreferencesMessages.AntPropertiesBlock_5, AntPreferencesMessages.AntPropertiesBlock_6
79     };
80     
81     private final ColumnLayoutData[] fTableColumnLayouts= {
82             new ColumnWeightData(30),
83             new ColumnWeightData(40),
84             new ColumnWeightData(30)
85     };
86     
87     /**
88      * Button listener that delegates for widget selection events.
89      */

90     private SelectionAdapter buttonListener= new SelectionAdapter() {
91         public void widgetSelected(SelectionEvent event) {
92             if (event.widget == addButton) {
93                 addProperty();
94             } else if (event.widget == editButton) {
95                 edit();
96             } else if (event.widget == removeButton) {
97                 remove(propertyTableViewer);
98             } else if (event.widget == addFileButton) {
99                 addPropertyFile();
100             } else if (event.widget == addExternalFileButton) {
101                 addExternalPropertyFile();
102             } else if (event.widget == removeFileButton) {
103                 remove(fileTableViewer);
104             }
105         }
106     };
107     
108     /**
109      * Key listener that delegates for key pressed events.
110      */

111     private KeyAdapter keyListener= new KeyAdapter() {
112         public void keyPressed(KeyEvent event) {
113             if (event.getSource() == propertyTableViewer) {
114                 if (removeButton.isEnabled() && event.character == SWT.DEL && event.stateMask == 0) {
115                     remove(propertyTableViewer);
116                 }
117             } else if (event.getSource() == fileTableViewer) {
118                 if (removeFileButton.isEnabled() && event.character == SWT.DEL && event.stateMask == 0) {
119                     remove(fileTableViewer);
120                 }
121             }
122         }
123     };
124     
125     /**
126      * Selection changed listener that delegates selection events.
127      */

128     private ISelectionChangedListener tableListener= new ISelectionChangedListener() {
129         public void selectionChanged(SelectionChangedEvent event) {
130             if (tablesEnabled) {
131                 if (event.getSource() == propertyTableViewer) {
132                     propertyTableSelectionChanged((IStructuredSelection) event.getSelection());
133                 } else if (event.getSource() == fileTableViewer) {
134                     fileTableSelectionChanged((IStructuredSelection) event.getSelection());
135                 }
136             }
137         }
138     };
139     
140     public AntPropertiesBlock(IAntBlockContainer container) {
141         this.container= container;
142     }
143
144     private void addPropertyFile() {
145         String JavaDoc title= AntPreferencesMessages.AntPropertiesFileSelectionDialog_12;
146         String JavaDoc message= AntPreferencesMessages.AntPropertiesFileSelectionDialog_13;
147         String JavaDoc filterExtension= "properties"; //$NON-NLS-1$
148
String JavaDoc filterMessage= AntPreferencesMessages.AntPropertiesFileSelectionDialog_14;
149         
150         Object JavaDoc[] existingFiles= getPropertyFiles();
151         List JavaDoc propFiles= new ArrayList JavaDoc(existingFiles.length);
152         for (int j = 0; j < existingFiles.length; j++) {
153             String JavaDoc file = (String JavaDoc)existingFiles[j];
154             try {
155                 propFiles.add(AntUtil.getFileForLocation(VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(file), null));
156             } catch (CoreException e) {
157                 AntUIPlugin.log(e.getStatus());
158             }
159         }
160             
161         FileSelectionDialog dialog= new FileSelectionDialog(propertyTableViewer.getControl().getShell(), propFiles, title, message, filterExtension, filterMessage);
162         if (dialog.open() == Window.OK) {
163             Object JavaDoc[] elements= dialog.getResult();
164             for (int i = 0; i < elements.length; i++) {
165                 IFile file = (IFile)elements[i];
166                 String JavaDoc varExpression= VariablesPlugin.getDefault().getStringVariableManager().generateVariableExpression("workspace_loc", file.getFullPath().toString()); //$NON-NLS-1$
167
((AntContentProvider)fileTableViewer.getContentProvider()).add(varExpression);
168             }
169             container.update();
170         }
171     }
172     
173     public void createControl(Composite top, String JavaDoc propertyLabel, String JavaDoc propertyFileLabel) {
174         Font font= top.getFont();
175         dialogSettings= AntUIPlugin.getDefault().getDialogSettings();
176
177         Label label = new Label(top, SWT.NONE);
178         GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
179         gd.horizontalSpan =2;
180         label.setLayoutData(gd);
181         label.setFont(font);
182         label.setText(propertyLabel);
183
184         propertyTableViewer= createTableViewer(top, true);
185         propertyTableViewer.addDoubleClickListener(new IDoubleClickListener() {
186             public void doubleClick(DoubleClickEvent event) {
187                 if (!event.getSelection().isEmpty() && editButton.isEnabled()) {
188                     edit();
189                 }
190             }
191         });
192         
193         propertyTableViewer.getTable().addKeyListener(keyListener);
194         
195         createButtonGroup(top);
196
197         label = new Label(top, SWT.NONE);
198         gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
199         gd.horizontalSpan =2;
200         label.setLayoutData(gd);
201         label.setFont(font);
202         label.setText(propertyFileLabel);
203
204         fileTableViewer= createTableViewer(top, false);
205         fileTableViewer.getTable().addKeyListener(keyListener);
206         createButtonGroup(top);
207     }
208     
209     /**
210      * Creates the group which will contain the buttons.
211      */

212     private void createButtonGroup(Composite top) {
213         Composite buttonGroup = new Composite(top, SWT.NONE);
214         GridLayout layout = new GridLayout();
215         layout.marginHeight = 0;
216         layout.marginWidth = 0;
217         buttonGroup.setLayout(layout);
218         buttonGroup.setLayoutData(new GridData(GridData.FILL_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL));
219         buttonGroup.setFont(top.getFont());
220
221         addButtonsToButtonGroup(buttonGroup);
222     }
223     
224     /**
225      * Creates and returns a configured table viewer in the given parent
226      */

227     private TableViewer createTableViewer(Composite parent, boolean setColumns) {
228         Table table = new Table(parent, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
229         GridData data= new GridData(GridData.FILL_BOTH);
230         int availableRows= availableRows(parent);
231         if (setColumns) {
232             data.heightHint = table.getItemHeight() * (availableRows / 10);
233         }
234         data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
235         table.setLayoutData(data);
236         table.setFont(parent.getFont());
237         
238         TableViewer tableViewer= new TableViewer(table);
239         tableViewer.setContentProvider(new AntContentProvider());
240         tableViewer.setLabelProvider(labelProvider);
241         tableViewer.addSelectionChangedListener(tableListener);
242         
243         if (setColumns) {
244             TableLayout tableLayout = new TableLayout();
245             table.setLayout(tableLayout);
246             table.setHeaderVisible(true);
247             table.setLinesVisible(true);
248             
249             for (int i = 0; i < fTableColumnHeaders.length; i++) {
250                 tableLayout.addColumnData(fTableColumnLayouts[i]);
251                 TableColumn column = new TableColumn(table, SWT.NONE, i);
252                 column.setResizable(fTableColumnLayouts[i].resizable);
253                 column.setText(fTableColumnHeaders[i]);
254             }
255         }
256             
257         return tableViewer;
258     }
259     
260     /**
261      * Return the number of rows available in the current display using the
262      * current font.
263      * @param parent The Composite whose Font will be queried.
264      * @return The result of the display size divided by the font size.
265      */

266     private int availableRows(Composite parent) {
267         int fontHeight = (parent.getFont().getFontData())[0].getHeight();
268         int displayHeight = parent.getDisplay().getClientArea().height;
269         return displayHeight / fontHeight;
270     }
271     
272     /* (non-Javadoc)
273      * Method declared on AntPage.
274      */

275     protected void addButtonsToButtonGroup(Composite parent) {
276         if (editButton == null) {
277             addButton= createPushButton(parent, AntPreferencesMessages.AntPropertiesBlock_1);
278             editButton= createPushButton(parent, AntPreferencesMessages.AntPropertiesBlock_2);
279             removeButton= createPushButton(parent, AntPreferencesMessages.AntPropertiesBlock_3);
280         } else {
281             addFileButton= createPushButton(parent, AntPreferencesMessages.AntPropertiesBlock_4);
282             addExternalFileButton= createPushButton(parent, AntPreferencesMessages.AntPropertiesBlock_14);
283             removeFileButton= createPushButton(parent, AntPreferencesMessages.AntPropertiesBlock_removeFileButton);
284         }
285     }
286     
287     /**
288      * Creates and returns a configured button in the given composite with the given
289      * label. Widget selection callbacks for the returned button will be processed
290      * by the <code>buttonListener</code>
291      */

292     private Button createPushButton(Composite parent, String JavaDoc label) {
293         Button button= container.createPushButton(parent, label);
294         button.addSelectionListener(buttonListener);
295         GridData gridData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.FILL_HORIZONTAL);
296         button.setLayoutData(gridData);
297         return button;
298     }
299     
300     /**
301      * Allows the user to enter external property files
302      */

303     private void addExternalPropertyFile() {
304         String JavaDoc lastUsedPath;
305         lastUsedPath= dialogSettings.get(IAntUIConstants.DIALOGSTORE_LASTEXTFILE);
306         if (lastUsedPath == null) {
307             lastUsedPath= ""; //$NON-NLS-1$
308
}
309         FileDialog dialog = new FileDialog(fileTableViewer.getControl().getShell(), SWT.MULTI);
310         dialog.setFilterExtensions(new String JavaDoc[] { "*.properties", "*.*" }); //$NON-NLS-1$ //$NON-NLS-2$;
311
dialog.setFilterPath(lastUsedPath);
312
313         String JavaDoc result = dialog.open();
314         if (result == null) {
315             return;
316         }
317         IPath filterPath= new Path(dialog.getFilterPath());
318         String JavaDoc[] results= dialog.getFileNames();
319         for (int i = 0; i < results.length; i++) {
320             String JavaDoc fileName = results[i];
321             IPath path= filterPath.append(fileName).makeAbsolute();
322             ((AntContentProvider)fileTableViewer.getContentProvider()).add(path.toOSString());
323         }
324     
325         dialogSettings.put(IAntUIConstants.DIALOGSTORE_LASTEXTFILE, filterPath.toOSString());
326         container.update();
327     }
328     
329     private void remove(TableViewer viewer) {
330         AntContentProvider antContentProvider= (AntContentProvider)viewer.getContentProvider();
331         IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
332         antContentProvider.remove(sel);
333         container.update();
334     }
335     
336     /**
337      * Allows the user to enter a user property
338      */

339     private void addProperty() {
340         String JavaDoc title = AntPreferencesMessages.AntPropertiesBlock_Add_Property_2;
341         AddPropertyDialog dialog = new AddPropertyDialog(propertyTableViewer.getControl().getShell(), title, new String JavaDoc[]{"", ""}); //$NON-NLS-1$ //$NON-NLS-2$
342
if (dialog.open() == Window.CANCEL) {
343             return;
344         }
345         
346         String JavaDoc[] pair= dialog.getNameValuePair();
347         String JavaDoc name= pair[0];
348         if (!overwrite(name)) {
349             return;
350         }
351         Property prop = new Property();
352         prop.setName(name);
353         prop.setValue(pair[1]);
354         ((AntContentProvider)propertyTableViewer.getContentProvider()).add(prop);
355         container.update();
356     }
357
358     private void edit() {
359         IStructuredSelection selection= (IStructuredSelection) propertyTableViewer.getSelection();
360         Property prop = (Property) selection.getFirstElement();
361         
362         String JavaDoc originalName= prop.getName();
363         String JavaDoc title = AntPreferencesMessages.AntPropertiesBlock_Edit_User_Property_5;
364         AddPropertyDialog dialog = new AddPropertyDialog(propertyTableViewer.getControl().getShell(), title, new String JavaDoc[]{prop.getName(), prop.getValue(false)});
365     
366         if (dialog.open() == Window.CANCEL) {
367             return;
368         }
369
370         String JavaDoc[] pair= dialog.getNameValuePair();
371         String JavaDoc name= pair[0];
372         if (!name.equals(originalName)) {
373             if (!overwrite(name)){
374                 return;
375             }
376         }
377         prop.setName(name);
378         prop.setValue(pair[1]);
379         //trigger a resort
380
propertyTableViewer.refresh();
381         container.update();
382     }
383     
384     private boolean overwrite(String JavaDoc name) {
385         Object JavaDoc[] properties= getProperties();
386         for (int i = 0; i < properties.length; i++) {
387             Property property = (Property)properties[i];
388             String JavaDoc propertyName = property.getName();
389             if (propertyName.equals(name)) {
390                 if (property.isDefault()) {
391                     MessageDialog.openError(propertyTableViewer.getControl().getShell(), AntPreferencesMessages.AntPropertiesBlock_17, MessageFormat.format(AntPreferencesMessages.AntPropertiesBlock_18, new String JavaDoc[]{propertyName, property.getPluginLabel()}));
392                     return false;
393                 }
394                 boolean overWrite= MessageDialog.openQuestion(propertyTableViewer.getControl().getShell(), AntPreferencesMessages.AntPropertiesBlock_15, MessageFormat.format(AntPreferencesMessages.AntPropertiesBlock_16, new String JavaDoc[] {name}));
395                 if (!overWrite) {
396                     return false;
397                 }
398                 ((AntContentProvider)propertyTableViewer.getContentProvider()).remove(property);
399                 break;
400             }
401         }
402         return true;
403     }
404     
405     /**
406      * Handles selection changes in the Property file table viewer.
407      */

408     private void fileTableSelectionChanged(IStructuredSelection newSelection) {
409         removeFileButton.setEnabled(newSelection.size() > 0);
410     }
411
412     /**
413      * Handles selection changes in the Property table viewer.
414      */

415     private void propertyTableSelectionChanged(IStructuredSelection newSelection) {
416         int size = newSelection.size();
417         boolean enabled= true;
418
419         Iterator JavaDoc itr= newSelection.iterator();
420         while (itr.hasNext()) {
421             Object JavaDoc element = itr.next();
422             if (element instanceof Property) {
423                 Property property= (Property)element;
424                 if (property.isDefault()) {
425                     enabled= false;
426                     break;
427                 }
428             }
429         }
430         editButton.setEnabled(enabled && size == 1);
431         removeButton.setEnabled(enabled && size > 0);
432         
433     }
434     
435     public void populatePropertyViewer(Map JavaDoc properties) {
436         if (properties == null) {
437             propertyTableViewer.setInput(new Property[0]);
438             return;
439         }
440         Property[] result = new Property[properties.size()];
441         Iterator JavaDoc entries= properties.entrySet().iterator();
442         int i= 0;
443         while (entries.hasNext()) {
444             Map.Entry JavaDoc element = (Map.Entry JavaDoc) entries.next();
445             Property property = new Property();
446             property.setName((String JavaDoc)element.getKey());
447             property.setValue((String JavaDoc)element.getValue());
448             result[i]= property;
449             i++;
450         }
451         propertyTableViewer.setInput(result);
452     }
453     
454     public void setPropertiesInput(Property[] properties) {
455         propertyTableViewer.setInput(properties);
456     }
457         
458     public void setPropertyFilesInput(String JavaDoc[] files) {
459         fileTableViewer.setInput(files);
460     }
461     
462     public void update() {
463         propertyTableSelectionChanged((IStructuredSelection) propertyTableViewer.getSelection());
464         fileTableSelectionChanged((IStructuredSelection)fileTableViewer.getSelection());
465     }
466     
467     public Object JavaDoc[] getProperties() {
468         return ((AntContentProvider)propertyTableViewer.getContentProvider()).getElements(null);
469     }
470     
471     public Object JavaDoc[] getPropertyFiles() {
472         return ((AntContentProvider)fileTableViewer.getContentProvider()).getElements(null);
473     }
474     
475     public void setEnabled(boolean enable) {
476         setTablesEnabled(enable);
477         addButton.setEnabled(enable);
478         addExternalFileButton.setEnabled(enable);
479         addFileButton.setEnabled(enable);
480         editButton.setEnabled(enable);
481         removeButton.setEnabled(enable);
482         removeFileButton.setEnabled(enable);
483         
484         if (enable) {
485             propertyTableViewer.setSelection(propertyTableViewer.getSelection());
486             fileTableViewer.setSelection(fileTableViewer.getSelection());
487         }
488     }
489     
490     public void setTablesEnabled(boolean tablesEnabled) {
491         this.tablesEnabled= tablesEnabled;
492     }
493 }
494
Popular Tags