1 11 package org.eclipse.team.internal.ui.preferences; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Set ; 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 38 public class TextPreferencePage extends PreferencePage implements IWorkbenchPreferencePage { 39 40 private static final class ExtensionValidator implements IInputValidator { 41 public String isValid(String newText) { 42 if (newText.trim().length() == 0) 43 return ""; 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 59 public String strip(String newText) { 60 newText= newText.trim(); 61 if (newText.startsWith("*")) newText= newText.substring(1); 63 if (newText.startsWith(".")) newText= newText.substring(1); 65 return newText; 66 } 67 68 public boolean isComplete(String text){ 69 if (text.equals("*") || text.equals("*.") || text.equals(".")){ return false; 75 } 76 return true; 77 } 78 } 79 80 private static final class FilenameValidator implements IInputValidator { 81 public String isValid(String newText) { 82 if (newText.trim().length() == 0) 83 return ""; if (newText.indexOf('*') >= 0) 85 return TeamUIMessages.TextPreferencePage_5; 86 return null; 87 } 88 } 89 90 private final List fItems; 92 93 private Button fRemoveButton; 95 private Button fChangeButton; 96 97 protected FileTypeTable fTable; 98 99 public TextPreferencePage() { 100 fItems= new ArrayList (); 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 fPluginNames= makeSetOfStrings(manager.getDefaultNameMappings()); 114 Set 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 makeSetOfStrings(IStringMapping [] mappings) { 133 final Set set= new HashSet (mappings.length); 134 for (int i = 0; i < mappings.length; i++) { 135 set.add(mappings[i].getString()); 136 } 137 return set; 138 } 139 140 143 public void init(IWorkbench workbench) { 144 } 145 146 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 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 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 245 public boolean performOk() { 246 final ArrayList extensionsList= new ArrayList (); 247 final ArrayList extensionsModesList= new ArrayList (); 248 249 final ArrayList namesList= new ArrayList (); 250 final ArrayList namesModesList= new ArrayList (); 251 252 for (final Iterator 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 (item.mode)); 258 } else if (item instanceof FileTypeTable.Name) { 259 namesList.add(item.name); 260 namesModesList.add(new Integer (item.mode)); 261 } 262 } 263 264 final String [] extensions= (String [])extensionsList.toArray(new String [extensionsList.size()]); 265 final String [] names= (String [])namesList.toArray(new String [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 integers) { 279 final int [] array= new int [integers.size()]; 280 int index= 0; 281 for (Iterator iter = integers.iterator(); iter.hasNext();) 282 array[index++]= ((Integer )iter.next()).intValue(); 283 return array; 284 } 285 286 289 void addExtension() { 290 final ExtensionValidator validator = new ExtensionValidator(); 291 final InputDialog dialog = new InputDialog(getShell(), TeamUIMessages.TextPreferencePage_enterExtensionShort, TeamUIMessages.TextPreferencePage_enterExtensionLong, null, validator); if (dialog.open() != Window.OK) 293 return; 294 295 final String extension = validator.strip(dialog.getValue()); 296 297 final Iterator 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); return; 304 } 305 } 306 final FileTypeTable.Item item= new FileTypeTable.Extension(extension, false); 307 fItems.add(item); 308 fTable.getViewer().refresh(); 309 } 310 311 314 void addName() { 315 final InputDialog dialog = new InputDialog(getShell(), TeamUIMessages.TextPreferencePage_6, TeamUIMessages.TextPreferencePage_7, null, new FilenameValidator()); if (dialog.open() != Window.OK) 317 return; 318 319 final String name = dialog.getValue(); 320 321 final Iterator 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); return; 328 } 329 } 330 final FileTypeTable.Item item= new FileTypeTable.Name(name, false); 331 fItems.add(item); 332 fTable.getViewer().refresh(); 333 } 334 335 338 void removePattern() { 339 final IStructuredSelection selection = fTable.getSelection(); 340 if (selection == null) 341 return; 342 343 for (final Iterator 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 355 void changePattern() { 356 final IStructuredSelection selection = fTable.getSelection(); 357 if (selection == null) 358 return; 359 360 for (final Iterator 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 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 |