KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > properties > SelfHostingPropertyPage


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.properties;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.ProjectScope;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.jdt.core.IClasspathEntry;
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.IPackageFragmentRoot;
23 import org.eclipse.jdt.core.JavaCore;
24 import org.eclipse.jdt.core.JavaModelException;
25 import org.eclipse.jface.dialogs.Dialog;
26 import org.eclipse.jface.viewers.CheckboxTableViewer;
27 import org.eclipse.jface.viewers.LabelProvider;
28 import org.eclipse.jface.viewers.ViewerComparator;
29 import org.eclipse.pde.internal.core.ICoreConstants;
30 import org.eclipse.pde.internal.core.PDECore;
31 import org.eclipse.pde.internal.ui.IHelpContextIds;
32 import org.eclipse.pde.internal.ui.PDEPlugin;
33 import org.eclipse.pde.internal.ui.PDEPluginImages;
34 import org.eclipse.pde.internal.ui.PDEUIMessages;
35 import org.eclipse.pde.internal.ui.elements.DefaultTableProvider;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.graphics.Image;
38 import org.eclipse.swt.layout.GridData;
39 import org.eclipse.swt.layout.GridLayout;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Control;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.ui.PlatformUI;
44 import org.eclipse.ui.dialogs.PropertyPage;
45 import org.osgi.service.prefs.BackingStoreException;
46 import org.osgi.service.prefs.Preferences;
47
48 public class SelfHostingPropertyPage extends PropertyPage {
49     
50     private Image fImage;
51     private CheckboxTableViewer fViewer;
52     
53     class ContentProvider extends DefaultTableProvider {
54         public Object JavaDoc[] getElements(Object JavaDoc input) {
55             return getOutputFolders();
56         }
57     }
58     
59     class FolderLabelProvider extends LabelProvider {
60         /* (non-Javadoc)
61          * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
62          */

63         public Image getImage(Object JavaDoc element) {
64             return fImage;
65         }
66     }
67     
68     private String JavaDoc[] getOutputFolders() {
69         IProject project = (IProject)getElement().getAdapter(IProject.class);
70         ArrayList JavaDoc list = new ArrayList JavaDoc();
71         try {
72             if (project.hasNature(JavaCore.NATURE_ID)) {
73                 IJavaProject jProject = JavaCore.create(project);
74                 list.add(jProject.getOutputLocation().toString());
75                 IClasspathEntry[] entries = jProject.getRawClasspath();
76                 for (int i = 0; i < entries.length; i++) {
77                     IClasspathEntry entry = entries[i];
78                     if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
79                             && entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
80                         IPath path = entry.getOutputLocation();
81                         if (path != null)
82                             list.add(path.toString());
83                     }
84                 }
85             }
86         } catch (JavaModelException e) {
87         } catch (CoreException e) {
88         }
89         return (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
90     }
91     
92     public SelfHostingPropertyPage() {
93         fImage = PDEPluginImages.DESC_OUTPUT_FOLDER_OBJ.createImage();
94     }
95     
96     /* (non-Javadoc)
97      * @see org.eclipse.jface.dialogs.IDialogPage#dispose()
98      */

99     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.setText(PDEUIMessages.SelfHostingPropertyPage_viewerLabel);
122         
123         fViewer = CheckboxTableViewer.newCheckList(composite, SWT.BORDER);
124         fViewer.setContentProvider(new ContentProvider());
125         fViewer.setLabelProvider(new FolderLabelProvider());
126         fViewer.setInput(getElement());
127         fViewer.setComparator(new ViewerComparator());
128         fViewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
129         
130         initialize();
131
132         Dialog.applyDialogFont(composite);
133         return composite;
134     }
135     
136     /* (non-Javadoc)
137      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
138      */

139     public void createControl(Composite parent) {
140         super.createControl(parent);
141         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IHelpContextIds.SELFHOSTING_PROPERTY_PAGE);
142     }
143     
144     private void initialize() {
145         fViewer.setAllChecked(true);
146         Preferences pref = getPreferences((IProject)getElement().getAdapter(IProject.class));
147         if (pref != null) {
148             String JavaDoc binExcludes = pref.get(ICoreConstants.SELFHOSTING_BIN_EXCLUDES, ""); //$NON-NLS-1$
149
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(binExcludes, ","); //$NON-NLS-1$
150
while (tokenizer.hasMoreTokens()) {
151                 String JavaDoc token = tokenizer.nextToken().trim();
152                 fViewer.setChecked(token, false);
153             }
154         }
155     }
156     
157     private Preferences getPreferences(IProject project) {
158         return new ProjectScope(project).getNode(PDECore.PLUGIN_ID);
159     }
160     
161     /* (non-Javadoc)
162      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
163      */

164     protected void performDefaults() {
165         fViewer.setAllChecked(true);
166     }
167     
168     public boolean performOk() {
169         Preferences pref = getPreferences((IProject)getElement().getAdapter(IProject.class));
170         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
171         for (int i = 0; i < fViewer.getTable().getItemCount(); i++) {
172             Object JavaDoc object = fViewer.getElementAt(i);
173             if (!fViewer.getChecked(object)) {
174                 if (buffer.length() > 0)
175                     buffer.append(","); //$NON-NLS-1$
176
buffer.append(object.toString());
177             }
178         }
179         if (pref != null) {
180             if (buffer.length() > 0)
181                 pref.put(ICoreConstants.SELFHOSTING_BIN_EXCLUDES, buffer.toString());
182             else
183                 pref.remove(ICoreConstants.SELFHOSTING_BIN_EXCLUDES);
184             
185             try {
186                 pref.flush();
187             } catch (BackingStoreException e) {
188                 PDEPlugin.logException(e);
189             }
190         }
191         return super.performOk();
192     }
193
194 }
195
Popular Tags