KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > preferences > TextPreferencePage


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.ui.preferences;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.jface.dialogs.*;
20 import org.eclipse.jface.dialogs.Dialog;
21 import org.eclipse.jface.preference.PreferencePage;
22 import org.eclipse.jface.util.PropertyChangeEvent;
23 import org.eclipse.jface.viewers.*;
24 import org.eclipse.jface.window.Window;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.widgets.*;
28 import org.eclipse.team.core.*;
29 import org.eclipse.team.internal.ui.*;
30 import org.eclipse.team.internal.ui.preferences.FileTypeTable.Item;
31 import org.eclipse.team.ui.TeamUI;
32 import org.eclipse.ui.*;
33 /**
34  * This preference page displays all patterns which determine whether a resource
35  * is to be treated as a text file or not. The page allows the user to add or
36  * remove entries from this table, and change their values from Text to Binary.
37  */

38 public class TextPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
39     
40     private static final class ExtensionValidator implements IInputValidator {
41         public String JavaDoc isValid(String JavaDoc newText) {
42             if (newText.trim().length() == 0)
43                 return ""; //$NON-NLS-1$
44
if (!isComplete(newText)){
45                 return TeamUIMessages.TextPreferencePage_ExtensionNotCompleted;
46             }
47             newText = strip(newText);
48             if (newText.indexOf('*') >= 0)
49                 return TeamUIMessages.TextPreferencePage_2;
50             if (newText.indexOf('.') >= 0)
51                 return TeamUIMessages.TextPreferencePage_3;
52             return null;
53         }
54
55         /**
56          * @param newText
57          * @return the text
58          */

59         public String JavaDoc strip(String JavaDoc newText) {
60             newText= newText.trim();
61             if (newText.startsWith("*")) //$NON-NLS-1$
62
newText= newText.substring(1);
63             if (newText.startsWith(".")) //$NON-NLS-1$
64
newText= newText.substring(1);
65             return newText;
66         }
67         
68         public boolean isComplete(String JavaDoc text){
69             //Allowed formats of extension are:
70
// extension
71
// .extension
72
// *.extension
73
if (text.equals("*") || text.equals("*.") || text.equals(".")){ //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
74
return false;
75             }
76             return true;
77         }
78     }
79     
80     private static final class FilenameValidator implements IInputValidator {
81         public String JavaDoc isValid(String JavaDoc newText) {
82             if (newText.trim().length() == 0)
83                 return ""; //$NON-NLS-1$
84
if (newText.indexOf('*') >= 0)
85                 return TeamUIMessages.TextPreferencePage_5;
86             return null;
87         }
88     }
89     
90     // The input for the table viewer
91
private final List JavaDoc fItems;
92     
93     // Widgets
94
private Button fRemoveButton;
95     private Button fChangeButton;
96
97     protected FileTypeTable fTable;
98
99     public TextPreferencePage() {
100         fItems= new ArrayList JavaDoc();
101         initializeItems();
102     }
103     
104     private void initializeItems() {
105         
106         fItems.clear();
107         
108         final IFileContentManager manager= Team.getFileContentManager();
109
110         final IStringMapping [] extensionInfoArray= manager.getExtensionMappings();
111         final IStringMapping [] nameInfoArray= manager.getNameMappings();
112         
113         Set JavaDoc fPluginNames= makeSetOfStrings(manager.getDefaultNameMappings());
114         Set JavaDoc fPluginExtensions= makeSetOfStrings(manager.getDefaultExtensionMappings());
115         
116         for (int i = 0; i < extensionInfoArray.length; i++) {
117             final IStringMapping info= extensionInfoArray[i];
118             final FileTypeTable.Extension extension= new FileTypeTable.Extension(info.getString(), fPluginExtensions.contains(info.getString()));
119             extension.mode= info.getType();
120             fItems.add(extension);
121         }
122         
123         for (int i = 0; i < nameInfoArray.length; i++) {
124             final IStringMapping info= nameInfoArray[i];
125             final FileTypeTable.Name name= new FileTypeTable.Name(info.getString(), fPluginNames.contains(info.getString()));
126             name.mode= info.getType();
127             fItems.add(name);
128         }
129
130     }
131     
132     private static Set JavaDoc makeSetOfStrings(IStringMapping [] mappings) {
133         final Set JavaDoc set= new HashSet JavaDoc(mappings.length);
134         for (int i = 0; i < mappings.length; i++) {
135             set.add(mappings[i].getString());
136         }
137         return set;
138     }
139     
140     /* (non-Javadoc)
141      * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
142      */

143     public void init(IWorkbench workbench) {
144     }
145
146     /* (non-Javadoc)
147      * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
148      */

149     protected Control createContents(Composite parent) {
150         
151         initializeDialogUnits(parent);
152         
153         final PixelConverter converter= SWTUtils.createDialogPixelConverter(parent);
154         
155         final Composite composite= SWTUtils.createHVFillComposite(parent, SWTUtils.MARGINS_NONE, 2);
156         
157         fTable= new FileTypeTable(composite, fItems, false);
158
159         fTable.getViewer().getControl().addListener(SWT.Selection, new Listener() {
160             public void handleEvent(Event e) {
161                 handleSelection();
162             }
163         });
164
165         fTable.getViewer().addDoubleClickListener(new IDoubleClickListener() {
166             public void doubleClick(DoubleClickEvent event) {
167                 final ISelection selection = event.getSelection();
168                 if (selection == null || !(selection instanceof IStructuredSelection)) {
169                     return;
170                 }
171                 fTable.getViewer().editElement(((IStructuredSelection)selection).getFirstElement(), 1);
172             }
173         });
174
175         final Composite buttonsComposite = new Composite(composite, SWT.NONE);
176         buttonsComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
177         buttonsComposite.setLayout(SWTUtils.createGridLayout(1, converter, SWTUtils.MARGINS_NONE));
178         
179         final Button addExtensionButton = new Button(buttonsComposite, SWT.PUSH);
180         addExtensionButton.setText(TeamUIMessages.TextPreferencePage_add);
181         final Button addNameButton = new Button(buttonsComposite, SWT.PUSH);
182         addNameButton.setText(TeamUIMessages.TextPreferencePage_0);
183         fChangeButton = new Button(buttonsComposite, SWT.PUSH);
184         fChangeButton.setText(TeamUIMessages.TextPreferencePage_change);
185         fRemoveButton= new Button(buttonsComposite, SWT.PUSH);
186         fRemoveButton.setText(TeamUIMessages.TextPreferencePage_remove);
187         
188         SWTUtils.createLabel(composite, TeamUIMessages.TextPreferencePage_1, 2);
189         
190         /**
191          * Calculate and set the button size
192          */

193         applyDialogFont(composite);
194         final int buttonWidth= SWTUtils.calculateControlSize(converter, new Button [] { addExtensionButton, addNameButton, fChangeButton, fRemoveButton });
195         addExtensionButton.setLayoutData(SWTUtils.createGridData(buttonWidth, SWT.DEFAULT, SWT.FILL, SWT.FILL, false, false));
196         addNameButton.setLayoutData(SWTUtils.createGridData(buttonWidth, SWT.DEFAULT, SWT.FILL, SWT.FILL, false, false));
197         fChangeButton.setLayoutData(SWTUtils.createGridData(buttonWidth, SWT.DEFAULT, SWT.FILL, SWT.FILL, false, false));
198         fRemoveButton.setLayoutData(SWTUtils.createGridData(buttonWidth, SWT.DEFAULT, SWT.FILL, SWT.FILL, false, false));
199         
200         addExtensionButton.addListener(SWT.Selection, new Listener() {
201             public void handleEvent(Event e) {
202                 addExtension();
203             }
204         });
205         addNameButton.addListener(SWT.Selection, new Listener() {
206             public void handleEvent(Event e) {
207                 addName();
208             }
209         });
210
211         fChangeButton.setEnabled(false);
212         fChangeButton.addListener(SWT.Selection, new Listener() {
213             public void handleEvent(Event e) {
214                 changePattern();
215             }
216         });
217         
218         fRemoveButton.setEnabled(false);
219         fRemoveButton.addListener(SWT.Selection, new Listener() {
220             public void handleEvent(Event e) {
221                 removePattern();
222             }
223         });
224         
225         Dialog.applyDialogFont(parent);
226         
227         // set F1 help
228
PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IHelpContextIds.FILE_TYPE_PREFERENCE_PAGE);
229         
230         return composite;
231     }
232     
233     protected void performDefaults() {
234         super.performDefaults();
235         initializeItems();
236         if (fTable != null)
237             fTable.getViewer().refresh();
238     }
239     
240     /**
241      * Do anything necessary because the OK button has been pressed.
242      *
243      * @return whether it is okay to close the preference page
244      */

245     public boolean performOk() {
246         final ArrayList JavaDoc extensionsList= new ArrayList JavaDoc();
247         final ArrayList JavaDoc extensionsModesList= new ArrayList JavaDoc();
248         
249         final ArrayList JavaDoc namesList= new ArrayList JavaDoc();
250         final ArrayList JavaDoc namesModesList= new ArrayList JavaDoc();
251         
252         for (final Iterator JavaDoc iter = fItems.iterator(); iter.hasNext();) {
253             final FileTypeTable.Item item= (FileTypeTable.Item) iter.next();
254             
255             if (item instanceof FileTypeTable.Extension) {
256                 extensionsList.add(item.name);
257                 extensionsModesList.add(new Integer JavaDoc(item.mode));
258             } else if (item instanceof FileTypeTable.Name) {
259                 namesList.add(item.name);
260                 namesModesList.add(new Integer JavaDoc(item.mode));
261             }
262         }
263         
264         final String JavaDoc [] extensions= (String JavaDoc [])extensionsList.toArray(new String JavaDoc [extensionsList.size()]);
265         final String JavaDoc [] names= (String JavaDoc [])namesList.toArray(new String JavaDoc [namesList.size()]);
266         
267         final int [] extensionsModes= integerListToIntArray(extensionsModesList);
268         final int [] namesModes= integerListToIntArray(namesModesList);
269         
270         Team.getFileContentManager().setExtensionMappings(extensions, extensionsModes);
271         Team.getFileContentManager().setNameMappings(names, namesModes);
272         
273         TeamUIPlugin.broadcastPropertyChange(new PropertyChangeEvent(this, TeamUI.GLOBAL_FILE_TYPES_CHANGED, null, null));
274
275         return true;
276     }
277
278     private static int [] integerListToIntArray(List JavaDoc integers) {
279         final int [] array= new int [integers.size()];
280         int index= 0;
281         for (Iterator JavaDoc iter = integers.iterator(); iter.hasNext();)
282             array[index++]= ((Integer JavaDoc)iter.next()).intValue();
283         return array;
284     }
285
286     /**
287      * Add a new item to the table with the default type of Text.
288      */

289     void addExtension() {
290         final ExtensionValidator validator = new ExtensionValidator();
291         final InputDialog dialog = new InputDialog(getShell(), TeamUIMessages.TextPreferencePage_enterExtensionShort, TeamUIMessages.TextPreferencePage_enterExtensionLong, null, validator); //
292
if (dialog.open() != Window.OK)
293             return;
294         
295         final String JavaDoc extension = validator.strip(dialog.getValue());
296         
297         // Check if the item already exists
298
final Iterator JavaDoc it = fItems.iterator();
299         while (it.hasNext()) {
300             final FileTypeTable.Item item= (FileTypeTable.Item)it.next();
301             if (item instanceof FileTypeTable.Extension && item.name.equals(extension)) {
302                 MessageDialog.openWarning(getShell(), TeamUIMessages.TextPreferencePage_extensionExistsShort, TeamUIMessages.TextPreferencePage_extensionExistsLong); //
303
return;
304             }
305         }
306         final FileTypeTable.Item item= new FileTypeTable.Extension(extension, false);
307         fItems.add(item);
308         fTable.getViewer().refresh();
309     }
310     
311     /**
312      * Add a new item to the table with the default type of Text.
313      */

314     void addName() {
315         final InputDialog dialog = new InputDialog(getShell(), TeamUIMessages.TextPreferencePage_6, TeamUIMessages.TextPreferencePage_7, null, new FilenameValidator()); //
316
if (dialog.open() != Window.OK)
317             return;
318         
319         final String JavaDoc name = dialog.getValue();
320         
321         // Check if the item already exists
322
final Iterator JavaDoc it = fItems.iterator();
323         while (it.hasNext()) {
324             final FileTypeTable.Item item= (FileTypeTable.Item)it.next();
325             if (item instanceof FileTypeTable.Name && item.name.equals(name)) {
326                 MessageDialog.openWarning(getShell(), TeamUIMessages.TextPreferencePage_extensionExistsShort, TeamUIMessages.TextPreferencePage_extensionExistsLong); //
327
return;
328             }
329         }
330         final FileTypeTable.Item item= new FileTypeTable.Name(name, false);
331         fItems.add(item);
332         fTable.getViewer().refresh();
333     }
334     
335     /**
336      * Remove the selected items from the table
337      */

338     void removePattern() {
339         final IStructuredSelection selection = fTable.getSelection();
340         if (selection == null)
341             return;
342         
343         for (final Iterator JavaDoc it = selection.iterator(); it.hasNext(); ) {
344             final FileTypeTable.Item item= (FileTypeTable.Item)it.next();
345             if (item.contributed)
346                 continue;
347             fItems.remove(item);
348         }
349         fTable.getViewer().refresh();
350         handleSelection();
351     }
352     /**
353      * Toggle the selected items' content types
354      */

355     void changePattern() {
356         final IStructuredSelection selection = fTable.getSelection();
357         if (selection == null)
358             return;
359
360         for (final Iterator JavaDoc it = selection.iterator(); it.hasNext(); ) {
361             final FileTypeTable.Item item= (FileTypeTable.Item)it.next();
362             item.mode= item.mode == Team.TEXT ? Team.BINARY : Team.TEXT;
363             fTable.getViewer().refresh(item);
364         }
365     }
366     
367     /**
368      * The table viewer selection has changed. Update the remove and change button enablement.
369      */

370     void handleSelection() {
371         final boolean empty = fTable.getSelection().isEmpty();
372         FileTypeTable.Item selectedItem = (Item) fTable.getSelection().getFirstElement();
373         
374         fRemoveButton.setEnabled(!empty && !selectedItem.contributed);
375         fChangeButton.setEnabled(!empty);
376     }
377 }
378
Popular Tags