001: /*******************************************************************************
002: * Copyright (c) 2003, 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.pde.internal.ui.properties;
011:
012: import java.util.ArrayList;
013: import java.util.StringTokenizer;
014:
015: import org.eclipse.core.resources.IProject;
016: import org.eclipse.core.resources.ProjectScope;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IPath;
019: import org.eclipse.jdt.core.IClasspathEntry;
020: import org.eclipse.jdt.core.IJavaProject;
021: import org.eclipse.jdt.core.IPackageFragmentRoot;
022: import org.eclipse.jdt.core.JavaCore;
023: import org.eclipse.jdt.core.JavaModelException;
024: import org.eclipse.jface.dialogs.Dialog;
025: import org.eclipse.jface.viewers.CheckboxTableViewer;
026: import org.eclipse.jface.viewers.LabelProvider;
027: import org.eclipse.jface.viewers.ViewerComparator;
028: import org.eclipse.pde.internal.core.ICoreConstants;
029: import org.eclipse.pde.internal.core.PDECore;
030: import org.eclipse.pde.internal.ui.IHelpContextIds;
031: import org.eclipse.pde.internal.ui.PDEPlugin;
032: import org.eclipse.pde.internal.ui.PDEPluginImages;
033: import org.eclipse.pde.internal.ui.PDEUIMessages;
034: import org.eclipse.pde.internal.ui.elements.DefaultTableProvider;
035: import org.eclipse.swt.SWT;
036: import org.eclipse.swt.graphics.Image;
037: import org.eclipse.swt.layout.GridData;
038: import org.eclipse.swt.layout.GridLayout;
039: import org.eclipse.swt.widgets.Composite;
040: import org.eclipse.swt.widgets.Control;
041: import org.eclipse.swt.widgets.Label;
042: import org.eclipse.ui.PlatformUI;
043: import org.eclipse.ui.dialogs.PropertyPage;
044: import org.osgi.service.prefs.BackingStoreException;
045: import org.osgi.service.prefs.Preferences;
046:
047: public class SelfHostingPropertyPage extends PropertyPage {
048:
049: private Image fImage;
050: private CheckboxTableViewer fViewer;
051:
052: class ContentProvider extends DefaultTableProvider {
053: public Object[] getElements(Object input) {
054: return getOutputFolders();
055: }
056: }
057:
058: class FolderLabelProvider extends LabelProvider {
059: /* (non-Javadoc)
060: * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
061: */
062: public Image getImage(Object element) {
063: return fImage;
064: }
065: }
066:
067: private String[] getOutputFolders() {
068: IProject project = (IProject) getElement().getAdapter(
069: IProject.class);
070: ArrayList list = new ArrayList();
071: try {
072: if (project.hasNature(JavaCore.NATURE_ID)) {
073: IJavaProject jProject = JavaCore.create(project);
074: list.add(jProject.getOutputLocation().toString());
075: IClasspathEntry[] entries = jProject.getRawClasspath();
076: for (int i = 0; i < entries.length; i++) {
077: IClasspathEntry entry = entries[i];
078: if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
079: && entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
080: IPath path = entry.getOutputLocation();
081: if (path != null)
082: list.add(path.toString());
083: }
084: }
085: }
086: } catch (JavaModelException e) {
087: } catch (CoreException e) {
088: }
089: return (String[]) list.toArray(new String[list.size()]);
090: }
091:
092: public SelfHostingPropertyPage() {
093: fImage = PDEPluginImages.DESC_OUTPUT_FOLDER_OBJ.createImage();
094: }
095:
096: /* (non-Javadoc)
097: * @see org.eclipse.jface.dialogs.IDialogPage#dispose()
098: */
099: public void dispose() {
100: if (fImage != null)
101: fImage.dispose();
102: }
103:
104: /**
105: * @see PreferencePage#createContents(Composite)
106: */
107: protected Control createContents(Composite parent) {
108: Composite composite = new Composite(parent, SWT.NONE);
109: composite.setLayout(new GridLayout());
110: composite.setLayoutData(new GridData(GridData.FILL_BOTH));
111:
112: Label label = new Label(composite, SWT.WRAP);
113: label.setText(PDEUIMessages.SelfHostingPropertyPage_label);
114: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
115: gd.widthHint = 300;
116: label.setLayoutData(gd);
117:
118: new Label(composite, SWT.NONE);
119:
120: label = new Label(composite, SWT.WRAP);
121: label
122: .setText(PDEUIMessages.SelfHostingPropertyPage_viewerLabel);
123:
124: fViewer = CheckboxTableViewer.newCheckList(composite,
125: SWT.BORDER);
126: fViewer.setContentProvider(new ContentProvider());
127: fViewer.setLabelProvider(new FolderLabelProvider());
128: fViewer.setInput(getElement());
129: fViewer.setComparator(new ViewerComparator());
130: fViewer.getControl().setLayoutData(
131: new GridData(GridData.FILL_BOTH));
132:
133: initialize();
134:
135: Dialog.applyDialogFont(composite);
136: return composite;
137: }
138:
139: /* (non-Javadoc)
140: * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
141: */
142: public void createControl(Composite parent) {
143: super .createControl(parent);
144: PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
145: IHelpContextIds.SELFHOSTING_PROPERTY_PAGE);
146: }
147:
148: private void initialize() {
149: fViewer.setAllChecked(true);
150: Preferences pref = getPreferences((IProject) getElement()
151: .getAdapter(IProject.class));
152: if (pref != null) {
153: String binExcludes = pref.get(
154: ICoreConstants.SELFHOSTING_BIN_EXCLUDES, ""); //$NON-NLS-1$
155: StringTokenizer tokenizer = new StringTokenizer(
156: binExcludes, ","); //$NON-NLS-1$
157: while (tokenizer.hasMoreTokens()) {
158: String token = tokenizer.nextToken().trim();
159: fViewer.setChecked(token, false);
160: }
161: }
162: }
163:
164: private Preferences getPreferences(IProject project) {
165: return new ProjectScope(project).getNode(PDECore.PLUGIN_ID);
166: }
167:
168: /* (non-Javadoc)
169: * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
170: */
171: protected void performDefaults() {
172: fViewer.setAllChecked(true);
173: }
174:
175: public boolean performOk() {
176: Preferences pref = getPreferences((IProject) getElement()
177: .getAdapter(IProject.class));
178: StringBuffer buffer = new StringBuffer();
179: for (int i = 0; i < fViewer.getTable().getItemCount(); i++) {
180: Object object = fViewer.getElementAt(i);
181: if (!fViewer.getChecked(object)) {
182: if (buffer.length() > 0)
183: buffer.append(","); //$NON-NLS-1$
184: buffer.append(object.toString());
185: }
186: }
187: if (pref != null) {
188: if (buffer.length() > 0)
189: pref.put(ICoreConstants.SELFHOSTING_BIN_EXCLUDES,
190: buffer.toString());
191: else
192: pref.remove(ICoreConstants.SELFHOSTING_BIN_EXCLUDES);
193:
194: try {
195: pref.flush();
196: } catch (BackingStoreException e) {
197: PDEPlugin.logException(e);
198: }
199: }
200: return super.performOk();
201: }
202:
203: }
|