1 15 package org.eclipse.ui.dialogs; 16 17 import java.util.ArrayList ; 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.StringTokenizer ; 22 23 import org.eclipse.jface.dialogs.IDialogConstants; 24 import org.eclipse.jface.viewers.CheckboxTableViewer; 25 import org.eclipse.jface.viewers.ViewerComparator; 26 import org.eclipse.swt.SWT; 27 import org.eclipse.swt.events.SelectionAdapter; 28 import org.eclipse.swt.events.SelectionEvent; 29 import org.eclipse.swt.events.SelectionListener; 30 import org.eclipse.swt.graphics.Font; 31 import org.eclipse.swt.layout.GridData; 32 import org.eclipse.swt.layout.GridLayout; 33 import org.eclipse.swt.widgets.Button; 34 import org.eclipse.swt.widgets.Composite; 35 import org.eclipse.swt.widgets.Control; 36 import org.eclipse.swt.widgets.Label; 37 import org.eclipse.swt.widgets.Shell; 38 import org.eclipse.swt.widgets.Text; 39 import org.eclipse.ui.IFileEditorMapping; 40 import org.eclipse.ui.PlatformUI; 41 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 42 import org.eclipse.ui.internal.WorkbenchMessages; 43 import org.eclipse.ui.internal.registry.EditorRegistry; 44 45 49 public class TypeFilteringDialog extends SelectionDialog { 50 Button addTypesButton; 51 52 Collection initialSelections; 53 54 CheckboxTableViewer listViewer; 56 57 private final static int SIZING_SELECTION_WIDGET_HEIGHT = 250; 59 60 private final static int SIZING_SELECTION_WIDGET_WIDTH = 300; 61 62 private final static String TYPE_DELIMITER = WorkbenchMessages.TypesFiltering_typeDelimiter; 63 64 private String filterTitle = WorkbenchMessages.TypesFiltering_otherExtensions; 66 67 Text userDefinedText; 68 69 IFileEditorMapping[] currentInput; 70 71 79 public TypeFilteringDialog(Shell parentShell, Collection preselections) { 80 super(parentShell); 81 setTitle(WorkbenchMessages.TypesFiltering_title); 82 this.initialSelections = preselections; 83 setMessage(WorkbenchMessages.TypesFiltering_message); 84 } 85 86 97 public TypeFilteringDialog(Shell parentShell, Collection preselections, 98 String filterText) { 99 this(parentShell, preselections); 100 this.filterTitle = filterText; 101 } 102 103 109 private void addSelectionButtons(Composite composite) { 110 Composite buttonComposite = new Composite(composite, SWT.RIGHT); 111 GridLayout layout = new GridLayout(); 112 layout.numColumns = 2; 113 buttonComposite.setLayout(layout); 114 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END 115 | GridData.GRAB_HORIZONTAL); 116 data.grabExcessHorizontalSpace = true; 117 composite.setData(data); 118 Button selectButton = createButton(buttonComposite, 119 IDialogConstants.SELECT_ALL_ID, WorkbenchMessages.WizardTransferPage_selectAll, false); 120 SelectionListener listener = new SelectionAdapter() { 121 public void widgetSelected(SelectionEvent e) { 122 listViewer.setAllChecked(true); 123 } 124 }; 125 selectButton.addSelectionListener(listener); 126 Button deselectButton = createButton(buttonComposite, 127 IDialogConstants.DESELECT_ALL_ID, WorkbenchMessages.WizardTransferPage_deselectAll, false); 128 listener = new SelectionAdapter() { 129 public void widgetSelected(SelectionEvent e) { 130 listViewer.setAllChecked(false); 131 } 132 }; 133 deselectButton.addSelectionListener(listener); 134 } 135 136 140 private void addUserDefinedEntries(List result) { 141 StringTokenizer tokenizer = new StringTokenizer (userDefinedText 142 .getText(), TYPE_DELIMITER); 143 while (tokenizer.hasMoreTokens()) { 145 String currentExtension = tokenizer.nextToken().trim(); 146 if (!currentExtension.equals("")) { if (currentExtension.startsWith("*.")) { result.add(currentExtension.substring(2)); 149 } else { 150 if (currentExtension.startsWith(".")) { result.add(currentExtension.substring(1)); 152 } else { 153 result.add(currentExtension); 154 } 155 } 156 } 157 } 158 } 159 160 164 private void checkInitialSelections() { 165 IFileEditorMapping editorMappings[] = ((EditorRegistry) PlatformUI 166 .getWorkbench().getEditorRegistry()).getUnifiedMappings(); 167 ArrayList selectedMappings = new ArrayList (); 168 for (int i = 0; i < editorMappings.length; i++) { 169 IFileEditorMapping mapping = editorMappings[i]; 170 if (this.initialSelections.contains(mapping.getExtension())) { 172 listViewer.setChecked(mapping, true); 173 selectedMappings.add(mapping.getExtension()); 174 } else { 175 if (this.initialSelections.contains(mapping.getLabel())) { 176 listViewer.setChecked(mapping, true); 177 selectedMappings.add(mapping.getLabel()); 178 } 179 } 180 } 181 Iterator initialIterator = this.initialSelections.iterator(); 183 StringBuffer entries = new StringBuffer (); 184 while (initialIterator.hasNext()) { 185 String nextExtension = (String ) initialIterator.next(); 186 if (!selectedMappings.contains(nextExtension)) { 187 if (entries.length() != 0) { 188 entries.append(','); 189 } 190 entries.append(nextExtension); 191 } 192 } 193 this.userDefinedText.setText(entries.toString()); 194 } 195 196 200 protected void configureShell(Shell shell) { 201 super.configureShell(shell); 202 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, 203 IWorkbenchHelpContextIds.TYPE_FILTERING_DIALOG); 204 } 205 206 210 protected Control createDialogArea(Composite parent) { 211 Composite composite = (Composite) super.createDialogArea(parent); 213 createMessageArea(composite); 214 listViewer = CheckboxTableViewer.newCheckList(composite, SWT.BORDER); 215 GridData data = new GridData(GridData.FILL_BOTH); 216 data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT; 217 data.widthHint = SIZING_SELECTION_WIDGET_WIDTH; 218 listViewer.getTable().setLayoutData(data); 219 listViewer.getTable().setFont(parent.getFont()); 220 listViewer.setLabelProvider(FileEditorMappingLabelProvider.INSTANCE); 221 listViewer 222 .setContentProvider(FileEditorMappingContentProvider.INSTANCE); 223 listViewer.setComparator(new ViewerComparator()); 224 addSelectionButtons(composite); 225 createUserEntryGroup(composite); 226 initializeViewer(); 227 if (this.initialSelections != null && !this.initialSelections.isEmpty()) { 229 checkInitialSelections(); 230 } 231 return composite; 232 } 233 234 240 private void createUserEntryGroup(Composite parent) { 241 Font font = parent.getFont(); 242 Composite userDefinedGroup = new Composite(parent, SWT.NONE); 244 GridLayout layout = new GridLayout(); 245 layout.numColumns = 2; 246 userDefinedGroup.setLayout(layout); 247 userDefinedGroup.setLayoutData(new GridData( 248 GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL)); 249 Label fTitle = new Label(userDefinedGroup, SWT.NONE); 250 fTitle.setFont(font); 251 fTitle.setText(filterTitle); 252 userDefinedText = new Text(userDefinedGroup, SWT.SINGLE | SWT.BORDER); 254 userDefinedText.setFont(font); 255 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL 256 | GridData.GRAB_HORIZONTAL); 257 userDefinedText.setLayoutData(data); 258 } 259 260 264 private IFileEditorMapping[] getInput() { 265 if (currentInput == null) { 267 List wildcardEditors = new ArrayList (); 268 IFileEditorMapping[] allMappings = ((EditorRegistry)PlatformUI.getWorkbench() 269 .getEditorRegistry()).getUnifiedMappings(); 270 for (int i = 0; i < allMappings.length; i++) { 271 if (allMappings[i].getName().equals("*")) { wildcardEditors.add(allMappings[i]); 273 } 274 } 275 currentInput = new IFileEditorMapping[wildcardEditors.size()]; 276 wildcardEditors.toArray(currentInput); 277 } 278 return currentInput; 279 } 280 281 284 private void initializeViewer() { 285 listViewer.setInput(getInput()); 286 } 287 288 293 protected void okPressed() { 294 IFileEditorMapping[] children = getInput(); 296 List list = new ArrayList (); 297 for (int i = 0; i < children.length; ++i) { 299 IFileEditorMapping element = children[i]; 300 if (listViewer.getChecked(element)) { 301 list.add(element.getExtension()); 302 } 303 } 304 addUserDefinedEntries(list); 305 setResult(list); 306 super.okPressed(); 307 } 308 } 309 | Popular Tags |