1 12 package org.eclipse.jdt.internal.ui.wizards.buildpaths; 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.IPath; 19 import org.eclipse.core.runtime.Path; 20 21 import org.eclipse.core.resources.IContainer; 22 import org.eclipse.core.resources.IProject; 23 import org.eclipse.core.resources.IResource; 24 import org.eclipse.core.resources.IWorkspaceRoot; 25 26 import org.eclipse.swt.SWT; 27 import org.eclipse.swt.graphics.Image; 28 import org.eclipse.swt.layout.GridData; 29 import org.eclipse.swt.layout.GridLayout; 30 import org.eclipse.swt.widgets.Composite; 31 import org.eclipse.swt.widgets.Control; 32 import org.eclipse.swt.widgets.Shell; 33 34 import org.eclipse.jface.dialogs.StatusDialog; 35 import org.eclipse.jface.resource.ImageDescriptor; 36 import org.eclipse.jface.viewers.LabelProvider; 37 import org.eclipse.jface.viewers.ViewerComparator; 38 import org.eclipse.jface.window.Window; 39 40 import org.eclipse.ui.PlatformUI; 41 42 import org.eclipse.jdt.internal.corext.util.Messages; 43 44 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 45 import org.eclipse.jdt.internal.ui.JavaPlugin; 46 import org.eclipse.jdt.internal.ui.JavaPluginImages; 47 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry; 48 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 49 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; 50 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; 51 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter; 52 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil; 53 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField; 54 55 public class ExclusionInclusionDialog extends StatusDialog { 56 57 private static class ExclusionInclusionLabelProvider extends LabelProvider { 58 59 private Image fElementImage; 60 61 public ExclusionInclusionLabelProvider(ImageDescriptor descriptor) { 62 ImageDescriptorRegistry registry= JavaPlugin.getImageDescriptorRegistry(); 63 fElementImage= registry.get(descriptor); 64 } 65 66 public Image getImage(Object element) { 67 return fElementImage; 68 } 69 70 public String getText(Object element) { 71 return (String ) element; 72 } 73 74 } 75 76 private ListDialogField fInclusionPatternList; 77 private ListDialogField fExclusionPatternList; 78 private CPListElement fCurrElement; 79 private IProject fCurrProject; 80 81 private IContainer fCurrSourceFolder; 82 83 private static final int IDX_ADD= 0; 84 private static final int IDX_ADD_MULTIPLE= 1; 85 private static final int IDX_EDIT= 2; 86 private static final int IDX_REMOVE= 4; 87 88 89 public ExclusionInclusionDialog(Shell parent, CPListElement entryToEdit, boolean focusOnExcluded) { 90 super(parent); 91 setShellStyle(getShellStyle() | SWT.RESIZE); 92 93 fCurrElement= entryToEdit; 94 95 setTitle(NewWizardMessages.ExclusionInclusionDialog_title); 96 97 fCurrProject= entryToEdit.getJavaProject().getProject(); 98 IWorkspaceRoot root= fCurrProject.getWorkspace().getRoot(); 99 IResource res= root.findMember(entryToEdit.getPath()); 100 if (res instanceof IContainer) { 101 fCurrSourceFolder= (IContainer) res; 102 } 103 104 String excLabel= NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_label; 105 ImageDescriptor excDescriptor= JavaPluginImages.DESC_OBJS_EXCLUSION_FILTER_ATTRIB; 106 String [] excButtonLabels= new String [] { 107 NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_add, 108 NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_add_multiple, 109 NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_edit, 110 null, 111 NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_remove 112 }; 113 114 115 String incLabel= NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_label; 116 ImageDescriptor incDescriptor= JavaPluginImages.DESC_OBJS_INCLUSION_FILTER_ATTRIB; 117 String [] incButtonLabels= new String [] { 118 NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_add, 119 NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_add_multiple, 120 NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_edit, 121 null, 122 NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_remove 123 }; 124 125 fExclusionPatternList= createListContents(entryToEdit, CPListElement.EXCLUSION, excLabel, excDescriptor, excButtonLabels); 126 fInclusionPatternList= createListContents(entryToEdit, CPListElement.INCLUSION, incLabel, incDescriptor, incButtonLabels); 127 if (focusOnExcluded) { 128 fExclusionPatternList.postSetFocusOnDialogField(parent.getDisplay()); 129 } else { 130 fInclusionPatternList.postSetFocusOnDialogField(parent.getDisplay()); 131 } 132 } 133 134 135 private ListDialogField createListContents(CPListElement entryToEdit, String key, String label, ImageDescriptor descriptor, String [] buttonLabels) { 136 ExclusionPatternAdapter adapter= new ExclusionPatternAdapter(); 137 138 ListDialogField patternList= new ListDialogField(adapter, buttonLabels, new ExclusionInclusionLabelProvider(descriptor)); 139 patternList.setDialogFieldListener(adapter); 140 patternList.setLabelText(label); 141 patternList.setRemoveButtonIndex(IDX_REMOVE); 142 patternList.enableButton(IDX_EDIT, false); 143 144 IPath[] pattern= (IPath[]) entryToEdit.getAttribute(key); 145 146 ArrayList elements= new ArrayList (pattern.length); 147 for (int i= 0; i < pattern.length; i++) { 148 elements.add(pattern[i].toString()); 149 } 150 patternList.setElements(elements); 151 patternList.selectFirstElement(); 152 patternList.enableButton(IDX_ADD_MULTIPLE, fCurrSourceFolder != null); 153 patternList.setViewerComparator(new ViewerComparator()); 154 return patternList; 155 } 156 157 158 protected Control createDialogArea(Composite parent) { 159 Composite composite= (Composite) super.createDialogArea(parent); 160 161 Composite inner= new Composite(composite, SWT.NONE); 162 inner.setFont(parent.getFont()); 163 164 GridLayout layout= new GridLayout(); 165 layout.marginHeight= 0; 166 layout.marginWidth= 0; 167 layout.numColumns= 2; 168 inner.setLayout(layout); 169 inner.setLayoutData(new GridData(GridData.FILL_BOTH)); 170 171 DialogField labelField= new DialogField(); 172 String name= fCurrElement.getPath().makeRelative().toString(); 173 labelField.setLabelText(Messages.format(NewWizardMessages.ExclusionInclusionDialog_description, name)); 174 labelField.doFillIntoGrid(inner, 2); 175 176 fInclusionPatternList.doFillIntoGrid(inner, 3); 177 LayoutUtil.setHorizontalSpan(fInclusionPatternList.getLabelControl(null), 2); 178 LayoutUtil.setHorizontalGrabbing(fInclusionPatternList.getListControl(null)); 179 180 fExclusionPatternList.doFillIntoGrid(inner, 3); 181 LayoutUtil.setHorizontalSpan(fExclusionPatternList.getLabelControl(null), 2); 182 LayoutUtil.setHorizontalGrabbing(fExclusionPatternList.getListControl(null)); 183 184 applyDialogFont(composite); 185 return composite; 186 } 187 188 protected void doCustomButtonPressed(ListDialogField field, int index) { 189 if (index == IDX_ADD) { 190 addEntry(field); 191 } else if (index == IDX_EDIT) { 192 editEntry(field); 193 } else if (index == IDX_ADD_MULTIPLE) { 194 addMultipleEntries(field); 195 } 196 } 197 198 protected void doDoubleClicked(ListDialogField field) { 199 editEntry(field); 200 } 201 202 protected void doSelectionChanged(ListDialogField field) { 203 List selected= field.getSelectedElements(); 204 field.enableButton(IDX_EDIT, canEdit(selected)); 205 } 206 207 private boolean canEdit(List selected) { 208 return selected.size() == 1; 209 } 210 211 private void editEntry(ListDialogField field) { 212 List selElements= field.getSelectedElements(); 213 if (selElements.size() != 1) { 214 return; 215 } 216 List existing= field.getElements(); 217 String entry= (String ) selElements.get(0); 218 ExclusionInclusionEntryDialog dialog= new ExclusionInclusionEntryDialog(getShell(), isExclusion(field), entry, existing, fCurrElement); 219 if (dialog.open() == Window.OK) { 220 field.replaceElement(entry, dialog.getExclusionPattern()); 221 } 222 } 223 224 private boolean isExclusion(ListDialogField field) { 225 return field == fExclusionPatternList; 226 } 227 228 229 private void addEntry(ListDialogField field) { 230 List existing= field.getElements(); 231 ExclusionInclusionEntryDialog dialog= new ExclusionInclusionEntryDialog(getShell(), isExclusion(field), null, existing, fCurrElement); 232 if (dialog.open() == Window.OK) { 233 field.addElement(dialog.getExclusionPattern()); 234 } 235 } 236 237 238 239 241 private class ExclusionPatternAdapter implements IListAdapter, IDialogFieldListener { 242 245 public void customButtonPressed(ListDialogField field, int index) { 246 doCustomButtonPressed(field, index); 247 } 248 249 252 public void selectionChanged(ListDialogField field) { 253 doSelectionChanged(field); 254 } 255 258 public void doubleClicked(ListDialogField field) { 259 doDoubleClicked(field); 260 } 261 262 265 public void dialogFieldChanged(DialogField field) { 266 } 267 268 } 269 270 protected void doStatusLineUpdate() { 271 } 272 273 protected void checkIfPatternValid() { 274 } 275 276 277 private IPath[] getPattern(ListDialogField field) { 278 Object [] arr= field.getElements().toArray(); 279 Arrays.sort(arr); 280 IPath[] res= new IPath[arr.length]; 281 for (int i= 0; i < res.length; i++) { 282 res[i]= new Path((String ) arr[i]); 283 } 284 return res; 285 } 286 287 public IPath[] getExclusionPattern() { 288 return getPattern(fExclusionPatternList); 289 } 290 291 public IPath[] getInclusionPattern() { 292 return getPattern(fInclusionPatternList); 293 } 294 295 298 protected void configureShell(Shell newShell) { 299 super.configureShell(newShell); 300 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.EXCLUSION_PATTERN_DIALOG); 301 } 302 303 private void addMultipleEntries(ListDialogField field) { 304 String title, message; 305 if (isExclusion(field)) { 306 title= NewWizardMessages.ExclusionInclusionDialog_ChooseExclusionPattern_title; 307 message= NewWizardMessages.ExclusionInclusionDialog_ChooseExclusionPattern_description; 308 } else { 309 title= NewWizardMessages.ExclusionInclusionDialog_ChooseInclusionPattern_title; 310 message= NewWizardMessages.ExclusionInclusionDialog_ChooseInclusionPattern_description; 311 } 312 313 IPath[] res= ExclusionInclusionEntryDialog.chooseExclusionPattern(getShell(), fCurrSourceFolder, title, message, null, true); 314 if (res != null) { 315 for (int i= 0; i < res.length; i++) { 316 field.addElement(res[i].toString()); 317 } 318 } 319 } 320 } 321 | Popular Tags |