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