|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009 Tasktop Technologies 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 |
* Tasktop Technologies - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.mylyn.internal.context.ui.wizards; |
| 13 |
|
| 14 |
import java.util.Collections; |
| 15 |
import java.util.Date; |
| 16 |
import java.util.List; |
| 17 |
import java.util.Map; |
| 18 |
|
| 19 |
import org.eclipse.jface.dialogs.IDialogConstants; |
| 20 |
import org.eclipse.jface.dialogs.MessageDialog; |
| 21 |
import org.eclipse.jface.layout.GridDataFactory; |
| 22 |
import org.eclipse.jface.layout.GridLayoutFactory; |
| 23 |
import org.eclipse.jface.wizard.ProgressMonitorPart; |
| 24 |
import org.eclipse.jface.wizard.WizardDialog; |
| 25 |
import org.eclipse.mylyn.internal.provisional.commons.ui.WorkbenchUtil; |
| 26 |
import org.eclipse.mylyn.internal.tasks.ui.util.AttachmentUtil; |
| 27 |
import org.eclipse.mylyn.tasks.core.ITask; |
| 28 |
import org.eclipse.mylyn.tasks.core.ITaskAttachment; |
| 29 |
import org.eclipse.mylyn.tasks.core.TaskRepository; |
| 30 |
import org.eclipse.mylyn.tasks.ui.TasksUi; |
| 31 |
import org.eclipse.osgi.util.NLS; |
| 32 |
import org.eclipse.swt.SWT; |
| 33 |
import org.eclipse.swt.events.SelectionAdapter; |
| 34 |
import org.eclipse.swt.events.SelectionEvent; |
| 35 |
import org.eclipse.swt.graphics.Image; |
| 36 |
import org.eclipse.swt.widgets.Composite; |
| 37 |
import org.eclipse.swt.widgets.Control; |
| 38 |
import org.eclipse.swt.widgets.Link; |
| 39 |
import org.eclipse.swt.widgets.Shell; |
| 40 |
|
| 41 |
import com.ibm.icu.text.DateFormat; |
| 42 |
|
| 43 |
/** |
| 44 |
* @author Steffen Pingel |
| 45 |
*/ |
| 46 |
public class RetrieveLatestContextDialog extends MessageDialog { |
| 47 |
|
| 48 |
public static boolean openQuestion(Shell shell, ITask task) { |
| 49 |
String title = Messages.RetrieveLatestContextDialog_Task_Activation0; |
| 50 |
TaskRepository repository = TasksUi.getRepositoryManager().getRepository(task.getConnectorKind(), |
| 51 |
task.getRepositoryUrl()); |
| 52 |
List<ITaskAttachment> contextAttachments = AttachmentUtil.getContextAttachments(repository, task); |
| 53 |
Collections.sort(contextAttachments, new TaskAttachmentComparator()); |
| 54 |
if (contextAttachments.size() > 0) { |
| 55 |
ITaskAttachment attachment = contextAttachments.get(0); |
| 56 |
String author = null; |
| 57 |
if (attachment.getAuthor() != null) { |
| 58 |
author = (attachment.getAuthor().getName()) != null ? attachment.getAuthor().getName() |
| 59 |
: attachment.getAuthor().getPersonId(); |
| 60 |
} |
| 61 |
if (author == null) { |
| 62 |
author = Messages.RetrieveLatestContextDialog_Task_ActivationUnknown; |
| 63 |
} |
| 64 |
Date date = attachment.getCreationDate(); |
| 65 |
String dateString = null; |
| 66 |
if (date != null) { |
| 67 |
dateString = DateFormat.getDateInstance(DateFormat.LONG).format(date); |
| 68 |
} |
| 69 |
if (dateString == null) { |
| 70 |
dateString = Messages.RetrieveLatestContextDialog_Task_ActivationUnknown; |
| 71 |
} |
| 72 |
String message = NLS.bind(Messages.RetrieveLatestContextDialog_Task_ActivationNo_local_context, author, |
| 73 |
dateString); |
| 74 |
int kind = QUESTION; |
| 75 |
int style = SWT.NONE; |
| 76 |
|
| 77 |
RetrieveLatestContextDialog dialog = new RetrieveLatestContextDialog(shell, title, null, message, kind, |
| 78 |
new String[] { IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL }, 0, task, attachment); |
| 79 |
style &= SWT.SHEET; |
| 80 |
dialog.setShellStyle(dialog.getShellStyle() | style); |
| 81 |
return dialog.open() == 0; |
| 82 |
} |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
private final ITaskAttachment attachment; |
| 87 |
|
| 88 |
private Link link; |
| 89 |
|
| 90 |
private ProgressContainer progressContainer; |
| 91 |
|
| 92 |
private ProgressMonitorPart progressMonitorPart; |
| 93 |
|
| 94 |
private final ITask task; |
| 95 |
|
| 96 |
public RetrieveLatestContextDialog(Shell parentShell, String dialogTitle, Image dialogTitleImage, |
| 97 |
String dialogMessage, int dialogImageType, String[] dialogButtonLabels, int defaultIndex, ITask task, |
| 98 |
ITaskAttachment attachment) { |
| 99 |
super(parentShell, dialogTitle, dialogTitleImage, dialogMessage, dialogImageType, dialogButtonLabels, |
| 100 |
defaultIndex); |
| 101 |
this.task = task; |
| 102 |
this.attachment = attachment; |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
protected void buttonPressed(int buttonId) { |
| 107 |
if (progressContainer.isActive()) { |
| 108 |
return; |
| 109 |
} |
| 110 |
if (buttonId == 0) { |
| 111 |
if (!AttachmentUtil.downloadContext(task, attachment, progressContainer)) { |
| 112 |
// failed |
| 113 |
return; |
| 114 |
} |
| 115 |
} |
| 116 |
super.buttonPressed(buttonId); |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
protected Control createButtonBar(Composite parent) { |
| 121 |
Composite composite = new Composite(parent, SWT.NONE); |
| 122 |
composite.setLayout(GridLayoutFactory.fillDefaults().numColumns(3).create()); |
| 123 |
GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(composite); |
| 124 |
Control control = createLink(composite); |
| 125 |
GridDataFactory.fillDefaults().grab(true, false).applyTo(control); |
| 126 |
super.createButtonBar(composite); |
| 127 |
return composite; |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
protected Control createContents(Composite parent) { |
| 132 |
Control control = super.createContents(parent); |
| 133 |
progressContainer.setCancelButton(getButton(1)); |
| 134 |
getButton(0).setFocus(); |
| 135 |
return control; |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
protected Control createCustomArea(Composite parent) { |
| 140 |
progressMonitorPart = new ProgressMonitorPart(parent, null); |
| 141 |
GridDataFactory.fillDefaults().grab(true, false).applyTo(progressMonitorPart); |
| 142 |
progressContainer = new ProgressContainer(getShell(), progressMonitorPart) { |
| 143 |
@Override |
| 144 |
protected void restoreUiState(Map<Object, Object> state) { |
| 145 |
link.setEnabled(true); |
| 146 |
getButton(0).setEnabled(true); |
| 147 |
getButton(1).setEnabled(true); |
| 148 |
}; |
| 149 |
|
| 150 |
@Override |
| 151 |
protected void saveUiState(Map<Object, Object> savedState) { |
| 152 |
link.setEnabled(false); |
| 153 |
getButton(0).setEnabled(false); |
| 154 |
}; |
| 155 |
}; |
| 156 |
return progressMonitorPart; |
| 157 |
} |
| 158 |
|
| 159 |
protected Control createLink(Composite parent) { |
| 160 |
link = new Link(parent, SWT.NONE); |
| 161 |
link.setText(Messages.RetrieveLatestContextDialog_Task_ActivationSelect_from_history); |
| 162 |
link.addSelectionListener(new SelectionAdapter() { |
| 163 |
@Override |
| 164 |
public void widgetSelected(SelectionEvent e) { |
| 165 |
close(); |
| 166 |
|
| 167 |
ContextRetrieveWizard wizard = new ContextRetrieveWizard(task); |
| 168 |
WizardDialog dialog = new WizardDialog(WorkbenchUtil.getShell(), wizard); |
| 169 |
dialog.create(); |
| 170 |
dialog.setBlockOnOpen(true); |
| 171 |
setReturnCode(dialog.open()); |
| 172 |
} |
| 173 |
}); |
| 174 |
return link; |
| 175 |
} |
| 176 |
|
| 177 |
} |