1 11 package org.eclipse.jdt.internal.ui.preferences; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.Map ; 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 57 public class BuildPathsPropertyPage extends PropertyPage implements IStatusChangeListener { 58 59 public static final String PROP_ID= "org.eclipse.jdt.ui.propertyPages.BuildPathsPropertyPage"; 61 private static final String PAGE_SETTINGS= "BuildPathsPropertyPage"; private static final String INDEX= "pageIndex"; 64 public static final Object DATA_ADD_ENTRY= "add_classpath_entry"; 66 public static final Object DATA_REVEAL_ENTRY= "select_classpath_entry"; public static final Object DATA_REVEAL_ATTRIBUTE_KEY= "select_classpath_attribute_key"; 69 public static final Object DATA_BLOCK= "block_until_buildpath_applied"; 71 private BuildPathsBlock fBuildPathsBlock; 72 private boolean fBlockOnApply= false; 73 74 77 protected Control createContents(Composite parent) { 78 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 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 115 public void setVisible(boolean visible) { 116 if (fBuildPathsBlock != null) { 117 if (!visible) { 118 if (fBuildPathsBlock.hasChangesInDialog()) { 119 String title= PreferencesMessages.BuildPathsPropertyPage_unsavedchanges_title; 120 String message= PreferencesMessages.BuildPathsPropertyPage_unsavedchanges_message; 121 String [] buttonLabels= new String [] { 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 } 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 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 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 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 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 e) { 222 ExceptionHandler.handle(e, getShell(), PreferencesMessages.BuildPathsPropertyPage_error_title, PreferencesMessages.BuildPathsPropertyPage_error_message); 223 return false; 224 } catch (InterruptedException e) { 225 return false; 226 } 227 } else { 228 op.runAsUserJob(PreferencesMessages.BuildPathsPropertyPage_job_title, null); 229 } 230 } 231 } 232 return true; 233 } 234 235 238 public void statusChanged(IStatus status) { 239 setValid(!status.matches(IStatus.ERROR)); 240 StatusUtil.applyToStatusLine(this, status); 241 } 242 243 246 public void applyData(Object data) { 247 if (data instanceof Map ) { 248 Map map= (Map ) data; 249 Object selectedLibrary= map.get(DATA_REVEAL_ENTRY); 250 if (selectedLibrary instanceof IClasspathEntry) { 251 IClasspathEntry entry= (IClasspathEntry) selectedLibrary; 252 Object attr= map.get(DATA_REVEAL_ATTRIBUTE_KEY); 253 String attributeKey= attr instanceof String ? (String ) attr : null; 254 if (fBuildPathsBlock != null) { 255 fBuildPathsBlock.setElementToReveal(entry, attributeKey); 256 } 257 } 258 Object 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 |