1 11 package org.eclipse.ui.internal.ide.dialogs; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.resources.IProject; 17 import org.eclipse.core.resources.IProjectDescription; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.resources.IWorkspace; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.core.runtime.Status; 24 import org.eclipse.jface.dialogs.ErrorDialog; 25 import org.eclipse.jface.operation.IRunnableWithProgress; 26 import org.eclipse.jface.preference.PreferencePage; 27 import org.eclipse.jface.viewers.CheckStateChangedEvent; 28 import org.eclipse.jface.viewers.CheckboxTableViewer; 29 import org.eclipse.jface.viewers.ICheckStateListener; 30 import org.eclipse.jface.viewers.IStructuredContentProvider; 31 import org.eclipse.jface.viewers.ViewerComparator; 32 import org.eclipse.osgi.util.NLS; 33 import org.eclipse.swt.SWT; 34 import org.eclipse.swt.graphics.Font; 35 import org.eclipse.swt.graphics.FontData; 36 import org.eclipse.swt.layout.GridData; 37 import org.eclipse.swt.layout.GridLayout; 38 import org.eclipse.swt.widgets.Composite; 39 import org.eclipse.swt.widgets.Control; 40 import org.eclipse.swt.widgets.Label; 41 import org.eclipse.ui.PlatformUI; 42 import org.eclipse.ui.dialogs.PropertyPage; 43 import org.eclipse.ui.internal.ide.DialogUtil; 44 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 45 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 46 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 47 import org.eclipse.ui.internal.progress.ProgressMonitorJobsDialog; 48 import org.eclipse.ui.model.WorkbenchContentProvider; 49 import org.eclipse.ui.model.WorkbenchLabelProvider; 50 51 55 public class ProjectReferencePage extends PropertyPage { 56 private IProject project; 57 58 private boolean modified = false; 59 60 private CheckboxTableViewer listViewer; 62 63 private static final int PROJECT_LIST_MULTIPLIER = 30; 64 65 68 public ProjectReferencePage() { 69 } 71 72 75 protected Control createContents(Composite parent) { 76 77 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), 78 IIDEHelpContextIds.PROJECT_REFERENCE_PROPERTY_PAGE); 79 Font font = parent.getFont(); 80 81 Composite composite = new Composite(parent, SWT.NONE); 82 GridLayout layout = new GridLayout(); 83 composite.setLayout(layout); 84 composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 85 composite.setFont(font); 86 87 initialize(); 88 89 Label description = createDescriptionLabel(composite); 90 description.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); 91 92 listViewer = CheckboxTableViewer.newCheckList(composite, SWT.TOP 93 | SWT.BORDER); 94 listViewer.getTable().setFont(font); 95 GridData data = new GridData(GridData.FILL_BOTH); 96 data.grabExcessHorizontalSpace = true; 97 98 if(!project.isOpen()) 99 listViewer.getControl().setEnabled(false); 100 101 if (DialogUtil.inRegularFontMode(parent)) { 103 data.heightHint = getDefaultFontHeight(listViewer.getTable(), 104 PROJECT_LIST_MULTIPLIER); 105 } 106 listViewer.getTable().setLayoutData(data); 107 listViewer.getTable().setFont(font); 108 109 listViewer.setLabelProvider(WorkbenchLabelProvider 110 .getDecoratingWorkbenchLabelProvider()); 111 listViewer.setContentProvider(getContentProvider(project)); 112 listViewer.setComparator(new ViewerComparator()); 113 listViewer.setInput(project.getWorkspace()); 114 try { 115 listViewer.setCheckedElements(project.getDescription() 116 .getReferencedProjects()); 117 } catch (CoreException e) { 118 } 120 121 listViewer.addCheckStateListener(new ICheckStateListener() { 123 public void checkStateChanged(CheckStateChangedEvent event) { 124 modified = true; 125 } 126 }); 127 128 return composite; 129 } 130 131 140 protected IStructuredContentProvider getContentProvider( 141 final IProject project) { 142 return new WorkbenchContentProvider() { 143 public Object [] getChildren(Object o) { 144 if (!(o instanceof IWorkspace)) { 145 return new Object [0]; 146 } 147 148 IProject[] projects = ((IWorkspace) o).getRoot().getProjects(); 150 ArrayList referenced = new ArrayList (projects.length); 151 boolean found = false; 152 for (int i = 0; i < projects.length; i++) { 153 if (!found && projects[i].equals(project)) { 154 found = true; 155 continue; 156 } 157 referenced.add(projects[i]); 158 } 159 160 try { 162 projects = project.getDescription().getReferencedProjects(); 163 for (int i = 0; i < projects.length; i++) { 164 if (!referenced.contains(projects[i])) { 165 referenced.add(projects[i]); 166 } 167 } 168 } catch (CoreException e) { 169 } 171 172 return referenced.toArray(); 173 } 174 }; 175 } 176 177 183 private static int getDefaultFontHeight(Control control, int lines) { 184 FontData[] viewerFontData = control.getFont().getFontData(); 185 int fontHeight = 10; 186 187 if (viewerFontData.length > 0) { 189 fontHeight = viewerFontData[0].getHeight(); 190 } 191 return lines * fontHeight; 192 193 } 194 195 199 protected void handle(InvocationTargetException e) { 200 IStatus error; 201 Throwable target = e.getTargetException(); 202 if (target instanceof CoreException) { 203 error = ((CoreException) target).getStatus(); 204 } else { 205 String msg = target.getMessage(); 206 if (msg == null) { 207 msg = IDEWorkbenchMessages.Internal_error; 208 } 209 error = new Status(IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH, 210 1, msg, target); 211 } 212 ErrorDialog.openError(getControl().getShell(), null, null, error); 213 } 214 215 218 private void initialize() { 219 project = (IProject) getElement().getAdapter(IResource.class); 220 noDefaultAndApplyButton(); 221 setDescription(NLS.bind(IDEWorkbenchMessages.ProjectReferencesPage_label, project.getName())); 222 } 223 224 227 public boolean performOk() { 228 if (!modified) { 229 return true; 230 } 231 Object [] checked = listViewer.getCheckedElements(); 232 final IProject[] refs = new IProject[checked.length]; 233 System.arraycopy(checked, 0, refs, 0, checked.length); 234 IRunnableWithProgress runnable = new IRunnableWithProgress() { 235 public void run(IProgressMonitor monitor) 236 throws InvocationTargetException { 237 238 try { 239 IProjectDescription description = project.getDescription(); 240 description.setReferencedProjects(refs); 241 project.setDescription(description, monitor); 242 } catch (CoreException e) { 243 throw new InvocationTargetException (e); 244 } 245 } 246 }; 247 try { 248 new ProgressMonitorJobsDialog(getControl().getShell()).run(true, 249 true, runnable); 250 } catch (InterruptedException e) { 251 } catch (InvocationTargetException e) { 253 handle(e); 254 return false; 255 } 256 return true; 257 } 258 } 259 | Popular Tags |