|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004, 2008 Mylyn project committers 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 |
|
| 9 |
package org.eclipse.mylyn.internal.tasks.ui.wizards; |
| 10 |
|
| 11 |
import java.lang.reflect.InvocationTargetException; |
| 12 |
import java.util.ArrayList; |
| 13 |
import java.util.Arrays; |
| 14 |
import java.util.Collections; |
| 15 |
import java.util.Comparator; |
| 16 |
import java.util.List; |
| 17 |
|
| 18 |
import org.eclipse.core.runtime.IConfigurationElement; |
| 19 |
import org.eclipse.core.runtime.IExtension; |
| 20 |
import org.eclipse.core.runtime.IExtensionPoint; |
| 21 |
import org.eclipse.core.runtime.IExtensionRegistry; |
| 22 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 23 |
import org.eclipse.core.runtime.IStatus; |
| 24 |
import org.eclipse.core.runtime.Platform; |
| 25 |
import org.eclipse.core.runtime.Status; |
| 26 |
import org.eclipse.core.runtime.SubProgressMonitor; |
| 27 |
import org.eclipse.jface.dialogs.IMessageProvider; |
| 28 |
import org.eclipse.jface.layout.GridDataFactory; |
| 29 |
import org.eclipse.jface.operation.IRunnableWithProgress; |
| 30 |
import org.eclipse.jface.wizard.WizardPage; |
| 31 |
import org.eclipse.mylyn.commons.core.StatusHandler; |
| 32 |
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin; |
| 33 |
import org.eclipse.mylyn.tasks.core.TaskRepository; |
| 34 |
import org.eclipse.mylyn.tasks.ui.wizards.ITaskRepositoryPage; |
| 35 |
import org.eclipse.mylyn.tasks.ui.wizards.ITaskRepositoryPageContribution; |
| 36 |
import org.eclipse.mylyn.tasks.ui.wizards.ITaskRepositoryPageContributor; |
| 37 |
import org.eclipse.mylyn.tasks.ui.wizards.ITaskRepositoryPageContribution.Listener; |
| 38 |
import org.eclipse.swt.SWT; |
| 39 |
import org.eclipse.swt.layout.GridLayout; |
| 40 |
import org.eclipse.swt.widgets.Composite; |
| 41 |
import org.eclipse.ui.forms.events.ExpansionAdapter; |
| 42 |
import org.eclipse.ui.forms.events.ExpansionEvent; |
| 43 |
import org.eclipse.ui.forms.widgets.ExpandableComposite; |
| 44 |
import org.eclipse.ui.forms.widgets.FormToolkit; |
| 45 |
|
| 46 |
public abstract class AbstractExtensibleRepositorySettingsPage extends WizardPage implements ITaskRepositoryPage { |
| 47 |
|
| 48 |
private static final String KIND = "connectorKind"; |
| 49 |
|
| 50 |
private static final String TASK_REPOSITORY_PAGE_CONTRIBUTOR = "taskRepositoryPageContributor"; |
| 51 |
|
| 52 |
private static final String TASK_REPOSITORY_PAGE_CONTRIBUTOR_EXTENSION = "org.eclipse.mylyn.tasks.ui.taskRepositoryPageContributor"; |
| 53 |
|
| 54 |
private static final Comparator<ITaskRepositoryPageContribution> CONTRIBUTION_COMPARATOR = new ContributionComparator(); |
| 55 |
|
| 56 |
protected final TaskRepository repository; |
| 57 |
|
| 58 |
private final List<ITaskRepositoryPageContribution> contributions = new ArrayList<ITaskRepositoryPageContribution>(); |
| 59 |
|
| 60 |
protected FormToolkit toolkit; |
| 61 |
|
| 62 |
protected Composite compositeContainer; |
| 63 |
|
| 64 |
private final Listener contributionListener = new ITaskRepositoryPageContribution.Listener() { |
| 65 |
public void validationRequired(ITaskRepositoryPageContribution contribution) { |
| 66 |
validateSettings(); |
| 67 |
} |
| 68 |
}; |
| 69 |
|
| 70 |
public AbstractExtensibleRepositorySettingsPage(String title, String description, TaskRepository repository) { |
| 71 |
super(title); |
| 72 |
this.repository = repository; |
| 73 |
setTitle(title); |
| 74 |
setDescription(description); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void dispose() { |
| 79 |
if (toolkit != null) { |
| 80 |
toolkit.dispose(); |
| 81 |
toolkit = null; |
| 82 |
} |
| 83 |
super.dispose(); |
| 84 |
} |
| 85 |
|
| 86 |
public void createControl(Composite parent) { |
| 87 |
toolkit = new FormToolkit(TasksUiPlugin.getDefault().getFormColors(parent.getDisplay())); |
| 88 |
|
| 89 |
compositeContainer = new Composite(parent, SWT.NULL); |
| 90 |
GridLayout layout = new GridLayout(1, true); |
| 91 |
compositeContainer.setLayout(layout); |
| 92 |
|
| 93 |
createSettingControls(compositeContainer); |
| 94 |
|
| 95 |
addContributions(compositeContainer); |
| 96 |
|
| 97 |
setControl(compositeContainer); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* create the controls of this page |
| 102 |
*/ |
| 103 |
protected abstract void createSettingControls(Composite parent); |
| 104 |
|
| 105 |
@Override |
| 106 |
public boolean isPageComplete() { |
| 107 |
return super.isPageComplete() && conributionsIsPageComplete(); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public boolean canFlipToNextPage() { |
| 112 |
return super.canFlipToNextPage() && contributionsCanFlipToNextPage(); |
| 113 |
} |
| 114 |
|
| 115 |
private boolean contributionsCanFlipToNextPage() { |
| 116 |
for (ITaskRepositoryPageContribution contribution : contributions) { |
| 117 |
if (!contribution.canFlipToNextPage()) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
} |
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
private boolean conributionsIsPageComplete() { |
| 125 |
for (ITaskRepositoryPageContribution contribution : contributions) { |
| 126 |
if (!contribution.isPageComplete()) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
} |
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
protected void addContributions(Composite parentControl) { |
| 134 |
List<ITaskRepositoryPageContributor> contributors = findApplicableContributors(); |
| 135 |
for (ITaskRepositoryPageContributor contributor : contributors) { |
| 136 |
ITaskRepositoryPageContribution contribution = contributor.createContribution(repository); |
| 137 |
if (contribution != null) { |
| 138 |
contributions.add(contribution); |
| 139 |
contribution.addListener(contributionListener); |
| 140 |
} |
| 141 |
} |
| 142 |
if (!contributions.isEmpty()) { |
| 143 |
Collections.sort(contributions, CONTRIBUTION_COMPARATOR); |
| 144 |
|
| 145 |
for (ITaskRepositoryPageContribution contribution : contributions) { |
| 146 |
|
| 147 |
ExpandableComposite section = toolkit.createExpandableComposite(compositeContainer, |
| 148 |
ExpandableComposite.COMPACT | ExpandableComposite.TWISTIE | ExpandableComposite.TITLE_BAR); |
| 149 |
section.clientVerticalSpacing = 0; |
| 150 |
section.setBackground(parentControl.getBackground()); |
| 151 |
section.setFont(parentControl.getFont()); |
| 152 |
section.addExpansionListener(new ExpansionAdapter() { |
| 153 |
@Override |
| 154 |
public void expansionStateChanged(ExpansionEvent e) { |
| 155 |
getControl().getShell().pack(); |
| 156 |
} |
| 157 |
}); |
| 158 |
section.setText(contribution.getTitle()); |
| 159 |
section.setToolTipText(contribution.getDescription()); |
| 160 |
|
| 161 |
GridDataFactory.fillDefaults().grab(true, false).applyTo(section); |
| 162 |
|
| 163 |
Composite sectionContentsContainer = toolkit.createComposite(section); |
| 164 |
sectionContentsContainer.setBackground(parentControl.getBackground()); |
| 165 |
contribution.createControl(sectionContentsContainer, toolkit); |
| 166 |
|
| 167 |
section.setClient(sectionContentsContainer); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Validate the settings of this page, not including contributions. This method should not be called directly by |
| 174 |
* page implementations |
| 175 |
* |
| 176 |
* @return an array of statuses, or null if there are no messages. |
| 177 |
* |
| 178 |
* @see #validateSettings() |
| 179 |
*/ |
| 180 |
protected abstract IStatus[] validate(IProgressMonitor monitor); |
| 181 |
|
| 182 |
/** |
| 183 |
* Overriding methods should call <code>super.applyTo(repository)</code> |
| 184 |
*/ |
| 185 |
public void applyTo(TaskRepository repository) { |
| 186 |
applyContributionSettingsTo(repository); |
| 187 |
} |
| 188 |
|
| 189 |
private void applyContributionSettingsTo(TaskRepository repository) { |
| 190 |
for (ITaskRepositoryPageContribution contribution : contributions) { |
| 191 |
contribution.applyTo(repository); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
private IStatus[] computeValidation(IProgressMonitor monitor) { |
| 196 |
int factor = 100; |
| 197 |
monitor.beginTask("Validating settings", (contributions.size() + 1) * factor); |
| 198 |
|
| 199 |
List<IStatus> statuses = null; |
| 200 |
|
| 201 |
// validate the page |
| 202 |
{ |
| 203 |
SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, factor); |
| 204 |
IStatus[] result = validate(subMonitor); |
| 205 |
if (result != null && result.length > 0) { |
| 206 |
if (statuses == null) { |
| 207 |
statuses = new ArrayList<IStatus>(result.length); |
| 208 |
} |
| 209 |
statuses.addAll(Arrays.asList(result)); |
| 210 |
} |
| 211 |
subMonitor.done(); |
| 212 |
} |
| 213 |
|
| 214 |
// validate contributions |
| 215 |
for (ITaskRepositoryPageContribution contribution : contributions) { |
| 216 |
SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, factor); |
| 217 |
IStatus[] result = contribution.validate(subMonitor); |
| 218 |
if (result != null && result.length > 0) { |
| 219 |
if (statuses == null) { |
| 220 |
statuses = new ArrayList<IStatus>(result.length); |
| 221 |
} |
| 222 |
statuses.addAll(Arrays.asList(result)); |
| 223 |
} |
| 224 |
subMonitor.done(); |
| 225 |
} |
| 226 |
monitor.done(); |
| 227 |
return statuses == null ? null : statuses.toArray(new IStatus[statuses.size()]); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Validate all settings in the page including contributions. This method should be called whenever a setting is |
| 232 |
* changed on the page. |
| 233 |
* |
| 234 |
* @see #validate(IProgressMonitor) |
| 235 |
*/ |
| 236 |
protected void validateSettings() { |
| 237 |
final Object[] result = new Object[1]; |
| 238 |
try { |
| 239 |
getWizard().getContainer().run(true, true, new IRunnableWithProgress() { |
| 240 |
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { |
| 241 |
result[0] = computeValidation(monitor); |
| 242 |
} |
| 243 |
}); |
| 244 |
} catch (InvocationTargetException e) { |
| 245 |
StatusHandler.fail(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, |
| 246 |
"Internal error validating repository", e.getCause())); |
| 247 |
return; |
| 248 |
} catch (InterruptedException e) { |
| 249 |
// canceled |
| 250 |
return; |
| 251 |
} |
| 252 |
applyValidationResult((IStatus[]) result[0]); |
| 253 |
getWizard().getContainer().updateButtons(); |
| 254 |
} |
| 255 |
|
| 256 |
protected void applyValidationResult(IStatus[] statuses) { |
| 257 |
if (statuses == null || statuses.length == 0) { |
| 258 |
setMessage(null, IMessageProvider.INFORMATION); |
| 259 |
setErrorMessage(null); |
| 260 |
} else { |
| 261 |
// find the most severe status |
| 262 |
IStatus status = statuses[0]; |
| 263 |
for (IStatus s : statuses) { |
| 264 |
if (status == null || s.getSeverity() > status.getSeverity()) { |
| 265 |
status = s; |
| 266 |
} |
| 267 |
} |
| 268 |
int messageType; |
| 269 |
switch (status.getSeverity()) { |
| 270 |
case IStatus.OK: |
| 271 |
case IStatus.INFO: |
| 272 |
messageType = IMessageProvider.INFORMATION; |
| 273 |
break; |
| 274 |
case IStatus.WARNING: |
| 275 |
messageType = IMessageProvider.WARNING; |
| 276 |
break; |
| 277 |
case IStatus.ERROR: |
| 278 |
default: |
| 279 |
messageType = IMessageProvider.ERROR; |
| 280 |
break; |
| 281 |
} |
| 282 |
setErrorMessage(null); |
| 283 |
setMessage(status.getMessage(), messageType); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
private List<ITaskRepositoryPageContributor> findApplicableContributors() { |
| 288 |
List<ITaskRepositoryPageContributor> contributors = new ArrayList<ITaskRepositoryPageContributor>(); |
| 289 |
|
| 290 |
IExtensionRegistry registry = Platform.getExtensionRegistry(); |
| 291 |
|
| 292 |
IExtensionPoint editorExtensionPoint = registry.getExtensionPoint(TASK_REPOSITORY_PAGE_CONTRIBUTOR_EXTENSION); |
| 293 |
IExtension[] editorExtensions = editorExtensionPoint.getExtensions(); |
| 294 |
for (IExtension extension : editorExtensions) { |
| 295 |
IConfigurationElement[] elements = extension.getConfigurationElements(); |
| 296 |
for (IConfigurationElement element : elements) { |
| 297 |
if (element.getName().equals(TASK_REPOSITORY_PAGE_CONTRIBUTOR)) { |
| 298 |
String kind = element.getAttribute(KIND); |
| 299 |
if ("*".equals(kind) || repository.getConnectorKind().equals(kind)) { |
| 300 |
try { |
| 301 |
Object contributor = element.createExecutableExtension("class"); |
| 302 |
contributors.add((ITaskRepositoryPageContributor) contributor); |
| 303 |
} catch (Exception e) { |
| 304 |
StatusHandler.log(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, "Could not load " |
| 305 |
+ TASK_REPOSITORY_PAGE_CONTRIBUTOR, e)); |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
return contributors; |
| 313 |
} |
| 314 |
|
| 315 |
private static class ContributionComparator implements Comparator<ITaskRepositoryPageContribution> { |
| 316 |
|
| 317 |
public int compare(ITaskRepositoryPageContribution o1, ITaskRepositoryPageContribution o2) { |
| 318 |
if (o1 == o2) { |
| 319 |
return 0; |
| 320 |
} |
| 321 |
String s1 = o1.getTitle(); |
| 322 |
String s2 = o2.getTitle(); |
| 323 |
int i = s1.compareTo(s2); |
| 324 |
if (i == 0) { |
| 325 |
i = new Integer(System.identityHashCode(s1)).compareTo(System.identityHashCode(s2)); |
| 326 |
} |
| 327 |
return i; |
| 328 |
} |
| 329 |
|
| 330 |
} |
| 331 |
} |