Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9d42272f6cff8344ddecfc522b65bab6421823af (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
/*******************************************************************************
 * Copyright (c) 2004 - 2005 University Of British Columbia 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:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylar.tasklist.ui;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.mylar.core.MylarPlugin;
import org.eclipse.mylar.tasklist.IContextEditorFactory;
import org.eclipse.mylar.tasklist.ITask;
import org.eclipse.mylar.tasklist.MylarTasklistPlugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.MultiPageEditorPart;
import org.eclipse.ui.part.MultiPageSelectionProvider;

/**
 * @author Mik Kersten
 * @author Eric Booth (initial prototype)
 */
public class TaskEditor extends MultiPageEditorPart {

	private static final String TASK_INFO_PAGE_LABEL = "Task Info";
	private static final String ISSUE_WEB_PAGE_LABEL = "Web Link";
	protected ITask task;
	private TaskSummaryEditor taskSummaryEditor;
	private Browser webBrowser;
	private TaskEditorInput taskEditorInput;
	
	private static class TaskEditorSelectionProvider extends MultiPageSelectionProvider {
		private ISelection globalSelection;
		
		public TaskEditorSelectionProvider(TaskEditor taskEditor) {
			super(taskEditor);
		}
		
		public ISelection getSelection() {
			IEditorPart activeEditor = ((TaskEditor) getMultiPageEditor()).getActiveEditor();
			if (activeEditor != null && activeEditor.getSite() != null) {
				ISelectionProvider selectionProvider = activeEditor.getSite().getSelectionProvider();
				if (selectionProvider != null)
					return selectionProvider.getSelection();
			}
			return globalSelection;
		}

		public void setSelection(ISelection selection) {
			IEditorPart activeEditor = ((TaskEditor) getMultiPageEditor()).getActiveEditor();
			if (activeEditor != null && activeEditor.getSite() != null) {
				ISelectionProvider selectionProvider = activeEditor.getSite().getSelectionProvider();
				if (selectionProvider != null) selectionProvider.setSelection(selection);
			} else {
				this.globalSelection = selection;
				fireSelectionChanged(new SelectionChangedEvent(this, globalSelection));
			}
		}
	}

	public TaskEditor() {
		super();
		IWorkbench workbench = MylarTasklistPlugin.getDefault().getWorkbench();
		IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
		IWorkbenchPage activePage = window.getActivePage();
		TaskEditorListener listener = new TaskEditorListener();
		activePage.addPartListener(listener);
		taskSummaryEditor = new TaskSummaryEditor();
	}

	@Override
	protected void createPages() {
		try { 
			int index = createTaskSummaryPage();
			if(task.getIssueReportURL().length() > 9){
				createTaskIssueWebPage();
			}
			for (IContextEditorFactory factory : MylarTasklistPlugin.getDefault().getContextEditors()) {
				taskSummaryEditor.setParentEditor(this);
				IEditorPart editor = factory.createEditor();
				index = addPage(editor, factory.createEditorInput(MylarPlugin.getContextManager().getActiveContext()));
				setPageText(index++, factory.getTitle()); 
			}
		} catch (PartInitException e) {
			MylarPlugin.fail(e, "failed to create task editor pages", false);
		}
	}
	
	public IEditorPart getActiveEditor() {
		return super.getActiveEditor();
	}

	private int createTaskSummaryPage() throws PartInitException {
		try {
			taskSummaryEditor.createPartControl(getContainer());
			taskSummaryEditor.setParentEditor(this);
			int index = addPage(taskSummaryEditor.getControl());
			setPageText(index, TASK_INFO_PAGE_LABEL);	
			return index;
		} catch (RuntimeException e) {
			MylarPlugin.fail(e, "could not add task editor", false);
		}		
		return 0;
	}

	/**
	 * Creates page 2 of the multi-page editor,
	 * which displays the task issue web page
	 */
	private void createTaskIssueWebPage() {
		try {
			webBrowser = new Browser(getContainer(), SWT.NONE);
			int index = addPage(webBrowser);
			setPageText(index, ISSUE_WEB_PAGE_LABEL);
			webBrowser.setUrl(task.getIssueReportURL());
			if (task.isDirectlyModifiable()) setActivePage(index);
		} catch (RuntimeException e) {
			MylarPlugin.fail(e, "could not open issue report web page", false);
		}
	}	

	@Override
	public void doSave(IProgressMonitor monitor) {
		taskSummaryEditor.doSave(monitor);
		if (webBrowser != null){
			webBrowser.setUrl(task.getIssueReportURL());
		}
		else if(task.getIssueReportURL().length() > 9){
			createTaskIssueWebPage();
		}
	}

	/**
	 * Saves the multi-page editor's document as another file.
	 * Also updates the text for page 0's tab, and updates this multi-page editor's input
	 * to correspond to the nested editor's.
	 * 
	 * @see org.eclipse.ui.ISaveablePart#doSaveAs()
	 */
	@SuppressWarnings("deprecation")
	@Override
	public void doSaveAs() {
		IEditorPart editor = getEditor(0);
		editor.doSaveAs();
		setPageText(0, editor.getTitle());
		setInput(editor.getEditorInput());
	}

	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		taskEditorInput = (TaskEditorInput)input;
		super.init(site, input);

		setSite(site);
		site.setSelectionProvider(new TaskEditorSelectionProvider(this));
		
		/*
		 * The task data is saved only once, at the initialization of the editor.  This is
		 * then passed to each of the child editors.  This way, only one instance of 
		 * the task data is stored for each editor opened.
		 */
		task = taskEditorInput.getTask();		
		try {
			taskSummaryEditor.init(this.getEditorSite(), this.getEditorInput());
			taskSummaryEditor.setTask(task);
			// Set the title on the editor's tab
			this.setPartName(taskEditorInput.getLabel());
		} catch (Exception e) {
			throw new PartInitException(e.getMessage());
		}
	}
	
	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	@Override
	public boolean isDirty() {
		return taskSummaryEditor.isDirty();
	}
	/**
	 * Class to listen for editor events
	 */
	private class TaskEditorListener implements IPartListener
	{

		/**
		 * @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
		 */
		public void partActivated(IWorkbenchPart part) {
			// don't care about this event
		}

		/**
		 * @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
		 */
		public void partBroughtToTop(IWorkbenchPart part) {
			// don't care about this event
		}

		/**
		 * @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart)
		 */
		public void partClosed(IWorkbenchPart part) {
			// don't care about this event
		}

		/**
		 * @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
		 */
		public void partDeactivated(IWorkbenchPart part) {
			// don't care about this event
		}

		/**
		 * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
		 */
		public void partOpened(IWorkbenchPart part) {
			// don't care about this event
		}
	}
	
	public void updatePartName() {
		firePropertyChange(PROP_DIRTY);
		return;
	}

	@Override
	public void setFocus() {
//		taskSummaryEditor.setFocus();
	}

	public Browser getWebBrowser() {
		return webBrowser;
	}
}

Back to the top