Skip to main content
summaryrefslogtreecommitdiffstats
blob: dffd2d6a4c942745d3aa8ec6fbf324309b442352 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*******************************************************************************
 * Copyright (c) 2004, 2010 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *     Ken Sueda - initial prototype
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui.editors;

import java.util.Collection;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.ControlContribution;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.internal.provisional.commons.ui.CommonImages;
import org.eclipse.mylyn.internal.provisional.commons.ui.CommonTextSupport;
import org.eclipse.mylyn.internal.provisional.commons.ui.SelectionProviderAdapter;
import org.eclipse.mylyn.internal.tasks.core.AbstractTask;
import org.eclipse.mylyn.internal.tasks.core.ITaskListChangeListener;
import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants;
import org.eclipse.mylyn.internal.tasks.core.LocalTask;
import org.eclipse.mylyn.internal.tasks.core.TaskContainerDelta;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.actions.NewSubTaskAction;
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.ITaskActivityListener;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.ui.ITasksUiConstants;
import org.eclipse.mylyn.tasks.ui.TasksUi;
import org.eclipse.mylyn.tasks.ui.editors.TaskEditor;
import org.eclipse.mylyn.tasks.ui.editors.TaskEditorInput;
import org.eclipse.mylyn.tasks.ui.editors.TaskFormPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IPropertyListener;
import org.eclipse.ui.IWorkbenchPartConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.IFormPart;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.handlers.IHandlerService;

/**
 * @author Mik Kersten
 * @author Rob Elves
 * @author Shawn Minto
 * @author Steffen Pingel
 */
public class TaskPlanningEditor extends TaskFormPage {

	private Composite editorComposite;

	private TaskRepository repository;

	private AbstractTask task;

	private Button saveButton;

	private final IPropertyListener dirtyStateListener = new IPropertyListener() {

		public void propertyChanged(Object source, int propId) {
			if (propId == IWorkbenchPartConstants.PROP_DIRTY && saveButton != null) {
				saveButton.setEnabled(getEditor().isDirty());
			}
		}
	};

	private final ITaskListChangeListener taskListChangeListener = new TaskListChangeAdapter() {
		@Override
		public void containersChanged(Set<TaskContainerDelta> containers) {
			for (TaskContainerDelta taskContainerDelta : containers) {
				if (taskContainerDelta.getElement() instanceof ITask) {
					final AbstractTask updateTask = (AbstractTask) taskContainerDelta.getElement();
					if (updateTask != null && task != null
							&& updateTask.getHandleIdentifier().equals(task.getHandleIdentifier())) {
						if (PlatformUI.getWorkbench() != null && !PlatformUI.getWorkbench().isClosing()) {
							PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
								public void run() {
									refresh();
								}
							});
						}
						break;
					}
				}
			}
		}

	};

	private CommonTextSupport textSupport;

	private ITaskActivityListener timingListener;

	private FormToolkit toolkit;

	private FocusTracker focusTracker;

	public TaskPlanningEditor(TaskEditor editor) {
		super(editor, ITasksUiConstants.ID_PAGE_PLANNING, Messages.TaskPlanningEditor_Planning);
		TasksUiInternal.getTaskList().addChangeListener(taskListChangeListener);
	}

	private void createContributions(final Composite editorComposite) {
		Collection<LocalTaskEditorContributionDescriptor> localEditorContributions = TaskEditorContributionExtensionReader.getLocalEditorContributions();
		for (final LocalTaskEditorContributionDescriptor descriptor : localEditorContributions) {
			SafeRunner.run(new ISafeRunnable() {
				public void handleException(Throwable e) {
					StatusHandler.log(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN,
							"Error creating task editor contribution: \"" + descriptor.getId() + "\"", e)); //$NON-NLS-1$ //$NON-NLS-2$
				}

				public void run() throws Exception {
					AbstractLocalEditorPart part = descriptor.createPart();
					initializePart(editorComposite, part);
				}

			});
		}
	}

	@Override
	protected void createFormContent(IManagedForm managedForm) {
		super.createFormContent(managedForm);

		TaskEditorInput taskEditorInput = (TaskEditorInput) getEditorInput();
		task = (AbstractTask) taskEditorInput.getTask();
		repository = TasksUi.getRepositoryManager().getRepository(task.getConnectorKind(), task.getRepositoryUrl());

		toolkit = managedForm.getToolkit();

		editorComposite = managedForm.getForm().getBody();
		GridLayout layout = new GridLayout();
		layout.verticalSpacing = 0;
		editorComposite.setLayout(layout);

		if (task instanceof LocalTask) {
			SummaryPart part = new SummaryPart();
			part.setTextSupport(textSupport);
			initializePart(editorComposite, part);
			initializePart(editorComposite, new AttributePart());
			// currently there is only one location for extensions
			createContributions(editorComposite);
		}

		PlanningPart planningPart = new PlanningPart(SWT.NONE);
		// disable due date picker if it's a repository due date
		boolean needsDueDate = true;
		if (task != null) {
			try {
				TaskData taskData = TasksUi.getTaskDataManager().getTaskData(task);
				if (taskData != null) {
					AbstractRepositoryConnector connector = TasksUi.getRepositoryConnector(taskData.getConnectorKind());
					TaskRepository taskRepository = TasksUi.getRepositoryManager().getRepository(
							taskData.getConnectorKind(), taskData.getRepositoryUrl());
					if (connector != null && taskRepository != null
							&& connector.hasRepositoryDueDate(taskRepository, task, taskData)) {
						needsDueDate = false;
					}
				}
			} catch (CoreException e) {
				// ignore
			}
		}
		planningPart.initialize(getManagedForm(), repository, task, needsDueDate, this, textSupport);
		planningPart.createControl(editorComposite, toolkit);
		planningPart.getSection().setLayoutData(new GridData(GridData.FILL_BOTH));
		getManagedForm().addPart(planningPart);

		focusTracker = new FocusTracker();
		focusTracker.track(editorComposite);
	}

	@Override
	public void dispose() {
		getEditor().removePropertyListener(dirtyStateListener);
		if (timingListener != null) {
			TasksUiPlugin.getTaskActivityManager().removeActivityListener(timingListener);
		}
		TasksUiInternal.getTaskList().removeChangeListener(taskListChangeListener);
		super.dispose();
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		super.doSave(monitor);
		// update task title
		TasksUiInternal.getTaskList().notifyElementChanged(task);
	}

	@Override
	public void doSaveAs() {
		// don't support saving as
	}

	/**
	 * Override for customizing the tool bar.
	 */
	@Override
	public void fillToolBar(IToolBarManager toolBarManager) {
		TaskEditorInput taskEditorInput = (TaskEditorInput) getEditorInput();
		ITask task = taskEditorInput.getTask();
		if (task instanceof LocalTask
				&& task.getAttribute(ITasksCoreConstants.ATTRIBUTE_OUTGOING_NEW_CONNECTOR_KIND) == null) {

			NewSubTaskAction newSubTaskAction = new NewSubTaskAction();
			newSubTaskAction.selectionChanged(newSubTaskAction, new StructuredSelection(task));
			if (newSubTaskAction.isEnabled()) {
				toolBarManager.add(newSubTaskAction);
			}
		}
	}

	@Override
	public TaskEditor getEditor() {
		return (TaskEditor) super.getEditor();
	}

	@Override
	public void init(IEditorSite site, IEditorInput input) {
		super.init(site, input);
		this.textSupport = new CommonTextSupport((IHandlerService) getSite().getService(IHandlerService.class));
		this.textSupport.setSelectionChangedListener((TaskEditorActionContributor) getEditorSite().getActionBarContributor());

		site.setSelectionProvider(new SelectionProviderAdapter(new StructuredSelection(
				((TaskEditorInput) input).getTask())));
	}

	private void initializePart(final Composite editorComposite, final AbstractLocalEditorPart part) {
		part.initialize(getManagedForm(), repository, task);
		Control control = part.createControl(editorComposite, toolkit);
		part.setControl(control);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(control);
		getManagedForm().addPart(part);
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	@Override
	public void refresh() {
		if (getManagedForm() == null || getManagedForm().getForm().isDisposed()) {
			// editor possibly closed
			return;
		}

		getEditor().updateHeaderToolBar();
		IFormPart[] parts = getManagedForm().getParts();
		// refresh will not be invoked unless parts are stale
		for (IFormPart part : parts) {
			if (part instanceof AbstractLocalEditorPart) {
				((AbstractLocalEditorPart) part).refresh(false);
			} else {
				part.refresh();
			}
		}
		getManagedForm().reflow(true);
	}

	public void fillLeftHeaderToolBar(IToolBarManager toolBarManager) {
		if (getEditorInput() instanceof TaskEditorInput
				&& ((TaskEditorInput) getEditorInput()).getTask() instanceof LocalTask) {
			ControlContribution submitButtonContribution = new ControlContribution(
					"org.eclipse.mylyn.tasks.toolbars.save") { //$NON-NLS-1$

				@Override
				protected Control createControl(Composite parent) {
					saveButton = new Button(parent, SWT.FLAT);
					saveButton.setText(Messages.TaskPlanningEditor_Save);
					saveButton.setImage(CommonImages.getImage(CommonImages.SAVE));
					saveButton.setBackground(null);
					saveButton.addListener(SWT.Selection, new Listener() {
						public void handleEvent(Event e) {
							doSave(new NullProgressMonitor());
						}
					});
					saveButton.setEnabled(getEditor().isDirty());

					return saveButton;
				}
			};
			getEditor().addPropertyListener(dirtyStateListener);
			toolBarManager.add(submitButtonContribution);
		}
	}

	@Override
	public void setFocus() {
		if (focusTracker.setFocus()) {
			return;
		} else {
			IFormPart[] parts = getManagedForm().getParts();
			if (parts.length > 0) {
				parts[0].setFocus();
				return;
			}
		}
		super.setFocus();
	}

}

Back to the top