001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.wizards.buildpaths;
011:
012: import java.util.ArrayList;
013: import java.util.Arrays;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.IPath;
017: import org.eclipse.core.runtime.Path;
018:
019: import org.eclipse.core.resources.IContainer;
020: import org.eclipse.core.resources.IProject;
021: import org.eclipse.core.resources.IResource;
022: import org.eclipse.core.resources.IWorkspaceRoot;
023:
024: import org.eclipse.swt.SWT;
025: import org.eclipse.swt.graphics.Image;
026: import org.eclipse.swt.layout.GridData;
027: import org.eclipse.swt.layout.GridLayout;
028: import org.eclipse.swt.widgets.Composite;
029: import org.eclipse.swt.widgets.Shell;
030:
031: import org.eclipse.jface.dialogs.Dialog;
032: import org.eclipse.jface.resource.ImageDescriptor;
033: import org.eclipse.jface.viewers.LabelProvider;
034: import org.eclipse.jface.viewers.ViewerComparator;
035: import org.eclipse.jface.window.Window;
036:
037: import org.eclipse.ui.PlatformUI;
038:
039: import org.eclipse.jdt.core.IJavaModelStatus;
040: import org.eclipse.jdt.core.JavaConventions;
041:
042: import org.eclipse.jdt.ui.wizards.NewElementWizardPage;
043:
044: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
045: import org.eclipse.jdt.internal.ui.JavaPlugin;
046: import org.eclipse.jdt.internal.ui.JavaPluginImages;
047: import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
048: import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry;
049: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
050: import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
051: import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
052: import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter;
053: import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
054: import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
055:
056: public class SetFilterWizardPage extends NewElementWizardPage {
057:
058: private static final String PAGE_NAME = "SetFilterWizardPage"; //$NON-NLS-1$
059:
060: private ListDialogField fInclusionPatternList;
061: private ListDialogField fExclusionPatternList;
062: private CPListElement fCurrElement;
063: private IProject fCurrProject;
064:
065: private IContainer fCurrSourceFolder;
066:
067: private static final int IDX_ADD = 0;
068: private static final int IDX_ADD_MULTIPLE = 1;
069: private static final int IDX_EDIT = 2;
070: private static final int IDX_REMOVE = 4;
071:
072: private final ArrayList fExistingEntries;
073:
074: private final IPath fOutputLocation;
075:
076: public SetFilterWizardPage(CPListElement entryToEdit,
077: ArrayList existingEntries, IPath outputLocation) {
078: super (PAGE_NAME);
079: fExistingEntries = existingEntries;
080: fOutputLocation = outputLocation;
081:
082: setTitle(NewWizardMessages.ExclusionInclusionDialog_title);
083: setDescription(NewWizardMessages.ExclusionInclusionDialog_description2);
084:
085: fCurrElement = entryToEdit;
086: fCurrProject = entryToEdit.getJavaProject().getProject();
087: IWorkspaceRoot root = fCurrProject.getWorkspace().getRoot();
088: IResource res = root.findMember(entryToEdit.getPath());
089: if (res instanceof IContainer) {
090: fCurrSourceFolder = (IContainer) res;
091: }
092:
093: String excLabel = NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_label;
094: ImageDescriptor excDescriptor = JavaPluginImages.DESC_OBJS_EXCLUSION_FILTER_ATTRIB;
095: String[] excButtonLabels = new String[] {
096: NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_add,
097: NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_add_multiple,
098: NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_edit,
099: null,
100: NewWizardMessages.ExclusionInclusionDialog_exclusion_pattern_remove };
101:
102: String incLabel = NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_label;
103: ImageDescriptor incDescriptor = JavaPluginImages.DESC_OBJS_INCLUSION_FILTER_ATTRIB;
104: String[] incButtonLabels = new String[] {
105: NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_add,
106: NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_add_multiple,
107: NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_edit,
108: null,
109: NewWizardMessages.ExclusionInclusionDialog_inclusion_pattern_remove };
110:
111: fExclusionPatternList = createListContents(entryToEdit,
112: CPListElement.EXCLUSION, excLabel, excDescriptor,
113: excButtonLabels);
114: fInclusionPatternList = createListContents(entryToEdit,
115: CPListElement.INCLUSION, incLabel, incDescriptor,
116: incButtonLabels);
117: }
118:
119: /* (non-Javadoc)
120: * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
121: */
122: public void createControl(Composite parent) {
123: Composite inner = new Composite(parent, SWT.NONE);
124: inner.setFont(parent.getFont());
125:
126: GridLayout layout = new GridLayout();
127: layout.marginHeight = 0;
128: layout.marginWidth = 0;
129: layout.numColumns = 2;
130: inner.setLayout(layout);
131: inner.setLayoutData(new GridData(GridData.FILL_BOTH));
132:
133: fInclusionPatternList.doFillIntoGrid(inner, 3);
134: LayoutUtil.setHorizontalSpan(fInclusionPatternList
135: .getLabelControl(null), 2);
136: LayoutUtil.setHorizontalGrabbing(fInclusionPatternList
137: .getListControl(null));
138:
139: fExclusionPatternList.doFillIntoGrid(inner, 3);
140: LayoutUtil.setHorizontalSpan(fExclusionPatternList
141: .getLabelControl(null), 2);
142: LayoutUtil.setHorizontalGrabbing(fExclusionPatternList
143: .getListControl(null));
144:
145: setControl(inner);
146: Dialog.applyDialogFont(inner);
147: PlatformUI.getWorkbench().getHelpSystem().setHelp(inner,
148: IJavaHelpContextIds.INCLUSION_EXCLUSION_WIZARD_PAGE);
149: }
150:
151: private static class ExclusionInclusionLabelProvider extends
152: LabelProvider {
153:
154: private Image fElementImage;
155:
156: public ExclusionInclusionLabelProvider(
157: ImageDescriptor descriptor) {
158: ImageDescriptorRegistry registry = JavaPlugin
159: .getImageDescriptorRegistry();
160: fElementImage = registry.get(descriptor);
161: }
162:
163: public Image getImage(Object element) {
164: return fElementImage;
165: }
166:
167: public String getText(Object element) {
168: return (String) element;
169: }
170:
171: }
172:
173: private ListDialogField createListContents(
174: CPListElement entryToEdit, String key, String label,
175: ImageDescriptor descriptor, String[] buttonLabels) {
176: ExclusionPatternAdapter adapter = new ExclusionPatternAdapter();
177:
178: ListDialogField patternList = new ListDialogField(adapter,
179: buttonLabels, new ExclusionInclusionLabelProvider(
180: descriptor));
181: patternList.setDialogFieldListener(adapter);
182: patternList.setLabelText(label);
183: patternList.enableButton(IDX_EDIT, false);
184:
185: IPath[] pattern = (IPath[]) entryToEdit.getAttribute(key);
186:
187: ArrayList elements = new ArrayList(pattern.length);
188: for (int i = 0; i < pattern.length; i++) {
189: String patternName = pattern[i].toString();
190: if (patternName.length() > 0)
191: elements.add(patternName);
192: }
193: patternList.setElements(elements);
194: patternList.selectFirstElement();
195: patternList.enableButton(IDX_ADD_MULTIPLE,
196: fCurrSourceFolder != null);
197: patternList.setViewerComparator(new ViewerComparator());
198: return patternList;
199: }
200:
201: protected void doCustomButtonPressed(ListDialogField field,
202: int index) {
203: if (index == IDX_ADD) {
204: addEntry(field);
205: } else if (index == IDX_EDIT) {
206: editEntry(field);
207: } else if (index == IDX_ADD_MULTIPLE) {
208: addMultipleEntries(field);
209: } else if (index == IDX_REMOVE) {
210: field.removeElements(field.getSelectedElements());
211: }
212: updateStatus();
213: }
214:
215: private void updateStatus() {
216: fCurrElement.setAttribute(CPListElement.INCLUSION,
217: getInclusionPattern());
218: fCurrElement.setAttribute(CPListElement.EXCLUSION,
219: getExclusionPattern());
220: IJavaModelStatus status = JavaConventions.validateClasspath(
221: fCurrElement.getJavaProject(), CPListElement
222: .convertToClasspathEntries(fExistingEntries),
223: fOutputLocation);
224: if (!status.isOK()) {
225: StatusInfo statusInfo = new StatusInfo();
226: statusInfo.setError(status.getMessage());
227: updateStatus(statusInfo);
228: } else {
229: StatusInfo statusInfo = new StatusInfo();
230: statusInfo.setOK();
231: updateStatus(statusInfo);
232: }
233: }
234:
235: protected void doDoubleClicked(ListDialogField field) {
236: editEntry(field);
237: updateStatus();
238: }
239:
240: protected void doSelectionChanged(ListDialogField field) {
241: List selected = field.getSelectedElements();
242: field.enableButton(IDX_EDIT, canEdit(selected));
243: }
244:
245: private boolean canEdit(List selected) {
246: return selected.size() == 1;
247: }
248:
249: private void editEntry(ListDialogField field) {
250: List selElements = field.getSelectedElements();
251: if (selElements.size() != 1) {
252: return;
253: }
254: List existing = field.getElements();
255: String entry = (String) selElements.get(0);
256: ExclusionInclusionEntryDialog dialog = new ExclusionInclusionEntryDialog(
257: getShell(), isExclusion(field), entry, existing,
258: fCurrElement);
259: if (dialog.open() == Window.OK) {
260: field.replaceElement(entry, dialog.getExclusionPattern());
261: }
262: }
263:
264: private boolean isExclusion(ListDialogField field) {
265: return field == fExclusionPatternList;
266: }
267:
268: private void addEntry(ListDialogField field) {
269: List existing = field.getElements();
270: ExclusionInclusionEntryDialog dialog = new ExclusionInclusionEntryDialog(
271: getShell(), isExclusion(field), null, existing,
272: fCurrElement);
273: if (dialog.open() == Window.OK) {
274: field.addElement(dialog.getExclusionPattern());
275: }
276: }
277:
278: // -------- ExclusionPatternAdapter --------
279:
280: private class ExclusionPatternAdapter implements IListAdapter,
281: IDialogFieldListener {
282: /**
283: * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#customButtonPressed(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField, int)
284: */
285: public void customButtonPressed(ListDialogField field, int index) {
286: doCustomButtonPressed(field, index);
287: }
288:
289: /**
290: * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#selectionChanged(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField)
291: */
292: public void selectionChanged(ListDialogField field) {
293: doSelectionChanged(field);
294: }
295:
296: /**
297: * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter#doubleClicked(org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField)
298: */
299: public void doubleClicked(ListDialogField field) {
300: doDoubleClicked(field);
301: }
302:
303: /**
304: * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener#dialogFieldChanged(org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField)
305: */
306: public void dialogFieldChanged(DialogField field) {
307: }
308:
309: }
310:
311: protected void doStatusLineUpdate() {
312: }
313:
314: protected void checkIfPatternValid() {
315: }
316:
317: private IPath[] getPattern(ListDialogField field) {
318: Object[] arr = field.getElements().toArray();
319: Arrays.sort(arr);
320: IPath[] res = new IPath[arr.length];
321: for (int i = 0; i < res.length; i++) {
322: res[i] = new Path((String) arr[i]);
323: }
324: return res;
325: }
326:
327: public IPath[] getExclusionPattern() {
328: return getPattern(fExclusionPatternList);
329: }
330:
331: public IPath[] getInclusionPattern() {
332: return getPattern(fInclusionPatternList);
333: }
334:
335: /*
336: * @see org.eclipse.jface.window.Window#configureShell(Shell)
337: */
338: protected void configureShell(Shell newShell) {
339: PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell,
340: IJavaHelpContextIds.EXCLUSION_PATTERN_DIALOG);
341: }
342:
343: private void addMultipleEntries(ListDialogField field) {
344: String title, message;
345: if (isExclusion(field)) {
346: title = NewWizardMessages.ExclusionInclusionDialog_ChooseExclusionPattern_title;
347: message = NewWizardMessages.ExclusionInclusionDialog_ChooseExclusionPattern_description;
348: } else {
349: title = NewWizardMessages.ExclusionInclusionDialog_ChooseInclusionPattern_title;
350: message = NewWizardMessages.ExclusionInclusionDialog_ChooseInclusionPattern_description;
351: }
352:
353: IPath[] res = ExclusionInclusionEntryDialog
354: .chooseExclusionPattern(getShell(), fCurrSourceFolder,
355: title, message, null, true);
356: if (res != null) {
357: for (int i = 0; i < res.length; i++) {
358: field.addElement(res[i].toString());
359: }
360: }
361: }
362:
363: }
|