KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > BuildPathsPropertyPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.internal.ui.preferences;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.OperationCanceledException;
21
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.IWorkspaceRunnable;
24
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Label;
29
30 import org.eclipse.jface.dialogs.Dialog;
31 import org.eclipse.jface.dialogs.IDialogSettings;
32 import org.eclipse.jface.dialogs.MessageDialog;
33 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
34 import org.eclipse.jface.preference.IPreferencePageContainer;
35
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.dialogs.PropertyPage;
38 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
39
40 import org.eclipse.jdt.core.IClasspathEntry;
41 import org.eclipse.jdt.core.IJavaElement;
42 import org.eclipse.jdt.core.IJavaProject;
43 import org.eclipse.jdt.core.JavaCore;
44
45 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
46 import org.eclipse.jdt.internal.ui.JavaPlugin;
47 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
48 import org.eclipse.jdt.internal.ui.dialogs.StatusUtil;
49 import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext;
50 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
51 import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
52 import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathsBlock;
53
54 /**
55  * Property page for configuring the Java build path
56  */

57 public class BuildPathsPropertyPage extends PropertyPage implements IStatusChangeListener {
58     
59     public static final String JavaDoc PROP_ID= "org.eclipse.jdt.ui.propertyPages.BuildPathsPropertyPage"; //$NON-NLS-1$
60

61     private static final String JavaDoc PAGE_SETTINGS= "BuildPathsPropertyPage"; //$NON-NLS-1$
62
private static final String JavaDoc INDEX= "pageIndex"; //$NON-NLS-1$
63

64     public static final Object JavaDoc DATA_ADD_ENTRY= "add_classpath_entry"; //$NON-NLS-1$
65

66     public static final Object JavaDoc DATA_REVEAL_ENTRY= "select_classpath_entry"; //$NON-NLS-1$
67
public static final Object JavaDoc DATA_REVEAL_ATTRIBUTE_KEY= "select_classpath_attribute_key"; //$NON-NLS-1$
68

69     public static final Object JavaDoc DATA_BLOCK= "block_until_buildpath_applied"; //$NON-NLS-1$
70

71     private BuildPathsBlock fBuildPathsBlock;
72     private boolean fBlockOnApply= false;
73     
74     /*
75      * @see PreferencePage#createControl(Composite)
76      */

77     protected Control createContents(Composite parent) {
78         // ensure the page has no special buttons
79
noDefaultAndApplyButton();
80         
81         IProject project= getProject();
82         Control result;
83         if (project == null || !isJavaProject(project)) {
84             result= createWithoutJava(parent);
85         } else if (!project.isOpen()) {
86             result= createForClosedProject(parent);
87         } else {
88             result= createWithJava(parent, project);
89         }
90         Dialog.applyDialogFont(result);
91         return result;
92     }
93     
94     /*
95      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
96      */

97     public void createControl(Composite parent) {
98         super.createControl(parent);
99         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.BUILD_PATH_PROPERTY_PAGE);
100     }
101     
102     private IDialogSettings getSettings() {
103         IDialogSettings javaSettings= JavaPlugin.getDefault().getDialogSettings();
104         IDialogSettings pageSettings= javaSettings.getSection(PAGE_SETTINGS);
105         if (pageSettings == null) {
106             pageSettings= javaSettings.addNewSection(PAGE_SETTINGS);
107             pageSettings.put(INDEX, 3);
108         }
109         return pageSettings;
110     }
111     
112     /* (non-Javadoc)
113      * @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
114      */

115     public void setVisible(boolean visible) {
116         if (fBuildPathsBlock != null) {
117             if (!visible) {
118                 if (fBuildPathsBlock.hasChangesInDialog()) {
119                     String JavaDoc title= PreferencesMessages.BuildPathsPropertyPage_unsavedchanges_title;
120                     String JavaDoc message= PreferencesMessages.BuildPathsPropertyPage_unsavedchanges_message;
121                     String JavaDoc[] buttonLabels= new String JavaDoc[] {
122                             PreferencesMessages.BuildPathsPropertyPage_unsavedchanges_button_save,
123                             PreferencesMessages.BuildPathsPropertyPage_unsavedchanges_button_discard,
124                             PreferencesMessages.BuildPathsPropertyPage_unsavedchanges_button_ignore
125                     };
126                     MessageDialog dialog= new MessageDialog(getShell(), title, null, message, MessageDialog.QUESTION, buttonLabels, 0);
127                     int res= dialog.open();
128                     if (res == 0) {
129                         performOk();
130                     } else if (res == 1) {
131                         fBuildPathsBlock.init(JavaCore.create(getProject()), null, null);
132                     } else {
133                         // keep unsaved
134
}
135                 }
136             } else {
137                 if (!fBuildPathsBlock.hasChangesInDialog() && fBuildPathsBlock.hasChangesInClasspathFile()) {
138                     fBuildPathsBlock.init(JavaCore.create(getProject()), null, null);
139                 }
140             }
141         }
142         super.setVisible(visible);
143     }
144     
145     
146     /*
147      * Content for valid projects.
148      */

149     private Control createWithJava(Composite parent, IProject project) {
150         IWorkbenchPreferenceContainer pageContainer= null;
151         IPreferencePageContainer container= getContainer();
152         if (container instanceof IWorkbenchPreferenceContainer) {
153             pageContainer= (IWorkbenchPreferenceContainer) container;
154         }
155         
156         fBuildPathsBlock= new BuildPathsBlock(new BusyIndicatorRunnableContext(), this, getSettings().getInt(INDEX), false, pageContainer);
157         fBuildPathsBlock.init(JavaCore.create(project), null, null);
158         return fBuildPathsBlock.createControl(parent);
159     }
160
161     /*
162      * Content for non-Java projects.
163      */

164     private Control createWithoutJava(Composite parent) {
165         Label label= new Label(parent, SWT.LEFT);
166         label.setText(PreferencesMessages.BuildPathsPropertyPage_no_java_project_message);
167         
168         fBuildPathsBlock= null;
169         setValid(true);
170         return label;
171     }
172
173     /*
174      * Content for closed projects.
175      */

176     private Control createForClosedProject(Composite parent) {
177         Label label= new Label(parent, SWT.LEFT);
178         label.setText(PreferencesMessages.BuildPathsPropertyPage_closed_project_message);
179         
180         fBuildPathsBlock= null;
181         setValid(true);
182         return label;
183     }
184     
185     private IProject getProject() {
186         IAdaptable adaptable= getElement();
187         if (adaptable != null) {
188             IJavaElement elem= (IJavaElement) adaptable.getAdapter(IJavaElement.class);
189             if (elem instanceof IJavaProject) {
190                 return ((IJavaProject) elem).getProject();
191             }
192         }
193         return null;
194     }
195     
196     private boolean isJavaProject(IProject proj) {
197         try {
198             return proj.hasNature(JavaCore.NATURE_ID);
199         } catch (CoreException e) {
200             JavaPlugin.log(e);
201         }
202         return false;
203     }
204     
205     /*
206      * @see IPreferencePage#performOk
207      */

208     public boolean performOk() {
209         if (fBuildPathsBlock != null) {
210             getSettings().put(INDEX, fBuildPathsBlock.getPageIndex());
211             if (fBuildPathsBlock.hasChangesInDialog() || fBuildPathsBlock.isClassfileMissing()) {
212                 IWorkspaceRunnable runnable= new IWorkspaceRunnable() {
213                     public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
214                         fBuildPathsBlock.configureJavaProject(monitor);
215                     }
216                 };
217                 WorkbenchRunnableAdapter op= new WorkbenchRunnableAdapter(runnable);
218                 if (fBlockOnApply) {
219                     try {
220                         new ProgressMonitorDialog(getShell()).run(true, true, op);
221                     } catch (InvocationTargetException JavaDoc e) {
222                         ExceptionHandler.handle(e, getShell(), PreferencesMessages.BuildPathsPropertyPage_error_title, PreferencesMessages.BuildPathsPropertyPage_error_message);
223                         return false;
224                     } catch (InterruptedException JavaDoc e) {
225                         return false;
226                     }
227                 } else {
228                     op.runAsUserJob(PreferencesMessages.BuildPathsPropertyPage_job_title, null);
229                 }
230             }
231         }
232         return true;
233     }
234     
235     /* (non-Javadoc)
236      * @see IStatusChangeListener#statusChanged
237      */

238     public void statusChanged(IStatus status) {
239         setValid(!status.matches(IStatus.ERROR));
240         StatusUtil.applyToStatusLine(this, status);
241     }
242     
243     /* (non-Javadoc)
244      * @see org.eclipse.jface.preference.PreferencePage#applyData(java.lang.Object)
245      */

246     public void applyData(Object JavaDoc data) {
247         if (data instanceof Map JavaDoc) {
248             Map JavaDoc map= (Map JavaDoc) data;
249             Object JavaDoc selectedLibrary= map.get(DATA_REVEAL_ENTRY);
250             if (selectedLibrary instanceof IClasspathEntry) {
251                 IClasspathEntry entry= (IClasspathEntry) selectedLibrary;
252                 Object JavaDoc attr= map.get(DATA_REVEAL_ATTRIBUTE_KEY);
253                 String JavaDoc attributeKey= attr instanceof String JavaDoc ? (String JavaDoc) attr : null;
254                 if (fBuildPathsBlock != null) {
255                     fBuildPathsBlock.setElementToReveal(entry, attributeKey);
256                 }
257             }
258             Object JavaDoc entryToAdd= map.get(DATA_ADD_ENTRY);
259             if (entryToAdd instanceof IClasspathEntry) {
260                 if (fBuildPathsBlock != null) {
261                     fBuildPathsBlock.addElement((IClasspathEntry) entryToAdd);
262                 }
263             }
264             fBlockOnApply= Boolean.TRUE.equals(map.get(DATA_BLOCK));
265         }
266     }
267
268 }
269
Popular Tags