001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.views.markers.internal;
011:
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: import org.eclipse.core.commands.ExecutionException;
016: import org.eclipse.core.commands.operations.IUndoContext;
017: import org.eclipse.core.commands.operations.IUndoableOperation;
018: import org.eclipse.core.resources.IMarker;
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.jface.action.IMenuManager;
021: import org.eclipse.jface.action.IToolBarManager;
022: import org.eclipse.jface.action.Separator;
023: import org.eclipse.jface.dialogs.ErrorDialog;
024: import org.eclipse.jface.dialogs.IDialogSettings;
025: import org.eclipse.jface.viewers.CellEditor;
026: import org.eclipse.jface.viewers.CheckboxCellEditor;
027: import org.eclipse.jface.viewers.ComboBoxCellEditor;
028: import org.eclipse.jface.viewers.ICellModifier;
029: import org.eclipse.jface.viewers.IStructuredSelection;
030: import org.eclipse.jface.viewers.TextCellEditor;
031: import org.eclipse.jface.viewers.TreeViewer;
032: import org.eclipse.swt.SWT;
033: import org.eclipse.swt.widgets.Composite;
034: import org.eclipse.swt.widgets.Item;
035: import org.eclipse.swt.widgets.TreeColumn;
036: import org.eclipse.ui.PlatformUI;
037: import org.eclipse.ui.ide.undo.UpdateMarkersOperation;
038: import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
039: import org.eclipse.ui.internal.ide.IDEInternalPreferences;
040: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
041: import org.eclipse.ui.part.CellEditorActionHandler;
042:
043: /**
044: * The TaskView is the view for displaying task markers.
045: */
046: public class TaskView extends MarkerView {
047:
048: private static final String COMPLETION = "completion"; //$NON-NLS-1$
049:
050: private final IField[] HIDDEN_FIELDS = { new FieldCreationTime() };
051:
052: private final static String[] ROOT_TYPES = { IMarker.TASK };
053:
054: private final static String[] TABLE_COLUMN_PROPERTIES = {
055: COMPLETION, IMarker.PRIORITY, IMarker.MESSAGE,
056: Util.EMPTY_STRING, Util.EMPTY_STRING, Util.EMPTY_STRING };
057:
058: private final static String TAG_DIALOG_SECTION = "org.eclipse.ui.views.task"; //$NON-NLS-1$
059:
060: private final IField[] VISIBLE_FIELDS = { new FieldDone(),
061: new FieldPriority(), new FieldMessage(),
062: new FieldResource(), new FieldFolder(),
063: new FieldLineNumber() };
064:
065: private ICellModifier cellModifier = new ICellModifier() {
066: public Object getValue(Object element, String property) {
067: if (element instanceof ConcreteMarker) {
068: IMarker marker = ((ConcreteMarker) element).getMarker();
069:
070: if (COMPLETION.equals(property)) {
071: return marker.getAttribute(IMarker.DONE, false) ? Boolean.TRUE
072: : Boolean.FALSE;
073: }
074:
075: if (IMarker.PRIORITY.equals(property)) {
076: return new Integer(IMarker.PRIORITY_HIGH
077: - marker.getAttribute(IMarker.PRIORITY,
078: IMarker.PRIORITY_NORMAL));
079: }
080:
081: if (IMarker.MESSAGE.equals(property)) {
082: return marker.getAttribute(IMarker.MESSAGE, ""); //$NON-NLS-1$
083: }
084: }
085:
086: return null;
087: }
088:
089: public boolean canModify(Object element, String property) {
090: return Util.isEditable(((ConcreteMarker) element)
091: .getMarker());
092: }
093:
094: public void modify(Object element, String property, Object value) {
095: if (element instanceof Item) {
096: Item item = (Item) element;
097: Object data = item.getData();
098:
099: if (data instanceof ConcreteMarker) {
100: ConcreteMarker concreteMarker = (ConcreteMarker) data;
101:
102: IMarker marker = concreteMarker.getMarker();
103:
104: try {
105: Object oldValue = getValue(data, property);
106: if (oldValue != null && !oldValue.equals(value)) {
107: Map attrs = new HashMap();
108: if (COMPLETION.equals(property))
109: attrs.put(IMarker.DONE, value);
110: else if (IMarker.PRIORITY.equals(property))
111: attrs
112: .put(
113: IMarker.PRIORITY,
114: new Integer(
115: IMarker.PRIORITY_HIGH
116: - ((Integer) value)
117: .intValue()));
118: else if (IMarker.MESSAGE.equals(property))
119: attrs.put(IMarker.MESSAGE, value);
120: if (!attrs.isEmpty()) {
121: IUndoableOperation op = new UpdateMarkersOperation(
122: marker,
123: attrs,
124: MarkerMessages.modifyTask_title,
125: true);
126: PlatformUI
127: .getWorkbench()
128: .getOperationSupport()
129: .getOperationHistory()
130: .execute(
131: op,
132: null,
133: WorkspaceUndoUtil
134: .getUIInfoAdapter(getSite()
135: .getShell()));
136: }
137: }
138: concreteMarker.refresh();
139: } catch (ExecutionException e) {
140: if (e.getCause() instanceof CoreException) {
141: ErrorDialog.openError(getSite().getShell(),
142: MarkerMessages.errorModifyingTask,
143: null,
144: ((CoreException) e.getCause())
145: .getStatus());
146: } else {
147: // something rather unexpected occurred.
148: IDEWorkbenchPlugin.log(
149: MarkerMessages.errorModifyingTask,
150: e);
151: }
152: }
153: }
154: }
155: }
156: };
157:
158: private CellEditorActionHandler cellEditorActionHandler;
159:
160: private ActionAddGlobalTask addGlobalTaskAction;
161:
162: private ActionDeleteCompleted deleteCompletedAction;
163:
164: private ActionMarkCompleted markCompletedAction;
165:
166: public void createPartControl(Composite parent) {
167: super .createPartControl(parent);
168:
169: TreeViewer treeViewer = getViewer();
170: CellEditor cellEditors[] = new CellEditor[treeViewer.getTree()
171: .getColumnCount()];
172: cellEditors[0] = new CheckboxCellEditor(treeViewer.getTree());
173:
174: String[] priorities = new String[] {
175: MarkerMessages.priority_high,
176: MarkerMessages.priority_normal,
177: MarkerMessages.priority_low };
178:
179: cellEditors[1] = new ComboBoxCellEditor(treeViewer.getTree(),
180: priorities, SWT.READ_ONLY);
181: CellEditor descriptionCellEditor = new TextCellEditor(
182: treeViewer.getTree());
183: cellEditors[2] = descriptionCellEditor;
184: treeViewer.setCellEditors(cellEditors);
185: treeViewer.setCellModifier(cellModifier);
186: treeViewer.setColumnProperties(TABLE_COLUMN_PROPERTIES);
187:
188: cellEditorActionHandler = new CellEditorActionHandler(
189: getViewSite().getActionBars());
190: cellEditorActionHandler.addCellEditor(descriptionCellEditor);
191: cellEditorActionHandler.setCopyAction(copyAction);
192: cellEditorActionHandler.setPasteAction(pasteAction);
193: cellEditorActionHandler.setDeleteAction(deleteAction);
194: cellEditorActionHandler.setSelectAllAction(selectAllAction);
195: cellEditorActionHandler.setUndoAction(undoAction);
196: cellEditorActionHandler.setRedoAction(redoAction);
197:
198: }
199:
200: public void dispose() {
201: if (cellEditorActionHandler != null) {
202: cellEditorActionHandler.dispose();
203: }
204:
205: if (markCompletedAction != null) {
206: markCompletedAction.dispose();
207: }
208:
209: super .dispose();
210: }
211:
212: /*
213: * (non-Javadoc)
214: *
215: * @see org.eclipse.ui.views.markers.internal.TableView#getDialogSettings()
216: */
217: protected IDialogSettings getDialogSettings() {
218: IDialogSettings workbenchSettings = IDEWorkbenchPlugin
219: .getDefault().getDialogSettings();
220: IDialogSettings settings = workbenchSettings
221: .getSection(TAG_DIALOG_SECTION);
222:
223: if (settings == null) {
224: settings = workbenchSettings
225: .addNewSection(TAG_DIALOG_SECTION);
226: }
227:
228: return settings;
229: }
230:
231: protected void createActions() {
232: super .createActions();
233:
234: addGlobalTaskAction = new ActionAddGlobalTask(this );
235: deleteCompletedAction = new ActionDeleteCompleted(this ,
236: getViewer());
237: markCompletedAction = new ActionMarkCompleted(getViewer());
238: propertiesAction = new ActionTaskProperties(this , getViewer());
239: }
240:
241: protected void fillContextMenu(IMenuManager manager) {
242: manager.add(addGlobalTaskAction);
243: manager.add(new Separator());
244: super .fillContextMenu(manager);
245: }
246:
247: protected void fillContextMenuAdditions(IMenuManager manager) {
248: manager.add(new Separator());
249: manager.add(markCompletedAction);
250: manager.add(deleteCompletedAction);
251: }
252:
253: /*
254: * (non-Javadoc)
255: *
256: * @see org.eclipse.ui.views.markers.internal.TableView#getSortingFields()
257: */
258: protected IField[] getSortingFields() {
259: IField[] all = new IField[VISIBLE_FIELDS.length
260: + HIDDEN_FIELDS.length];
261:
262: System.arraycopy(VISIBLE_FIELDS, 0, all, 0,
263: VISIBLE_FIELDS.length);
264: System.arraycopy(HIDDEN_FIELDS, 0, all, VISIBLE_FIELDS.length,
265: HIDDEN_FIELDS.length);
266:
267: return all;
268: }
269:
270: /*
271: * (non-Javadoc)
272: *
273: * @see org.eclipse.ui.views.markers.internal.TableView#getAllFields()
274: */
275: protected IField[] getAllFields() {
276: return getSortingFields();
277: }
278:
279: protected String[] getRootTypes() {
280: return ROOT_TYPES;
281: }
282:
283: protected void initToolBar(IToolBarManager toolBarManager) {
284: toolBarManager.add(addGlobalTaskAction);
285: super .initToolBar(toolBarManager);
286: }
287:
288: public void setSelection(IStructuredSelection structuredSelection,
289: boolean reveal) {
290: // TODO: added because nick doesn't like public API inherited from
291: // internal classes
292: super .setSelection(structuredSelection, reveal);
293: }
294:
295: /*
296: * (non-Javadoc)
297: *
298: * @see org.eclipse.ui.views.markers.internal.MarkerView#getMarkerTypes()
299: */
300: protected String[] getMarkerTypes() {
301: return new String[] { IMarker.TASK };
302: }
303:
304: protected String getStaticContextId() {
305: return PlatformUI.PLUGIN_ID + ".task_list_view_context"; //$NON-NLS-1$
306: }
307:
308: /*
309: * (non-Javadoc)
310: *
311: * @see org.eclipse.ui.views.markers.internal.MarkerView#createFiltersDialog()
312: */
313: protected DialogMarkerFilter createFiltersDialog() {
314:
315: MarkerFilter[] filters = getUserFilters();
316: TaskFilter[] taskFilters = new TaskFilter[filters.length];
317: System.arraycopy(filters, 0, taskFilters, 0, filters.length);
318: return new DialogTaskFilter(getSite().getShell(), taskFilters);
319: }
320:
321: /*
322: * (non-Javadoc)
323: *
324: * @see org.eclipse.ui.views.markers.internal.MarkerView#createFilter(java.lang.String)
325: */
326: protected MarkerFilter createFilter(String name) {
327: return new TaskFilter(name);
328: }
329:
330: /*
331: * (non-Javadoc)
332: *
333: * @see org.eclipse.ui.views.markers.internal.MarkerView#getSectionTag()
334: */
335: protected String getSectionTag() {
336: return TAG_DIALOG_SECTION;
337: }
338:
339: /*
340: * (non-Javadoc)
341: *
342: * @see org.eclipse.ui.views.markers.internal.MarkerView#getMarkerEnablementPreferenceName()
343: */
344: String getMarkerEnablementPreferenceName() {
345: return IDEInternalPreferences.LIMIT_TASKS;
346: }
347:
348: /*
349: * (non-Javadoc)
350: *
351: * @see org.eclipse.ui.views.markers.internal.MarkerView#getMarkerLimitPreferenceName()
352: */
353: String getMarkerLimitPreferenceName() {
354: return IDEInternalPreferences.TASKS_LIMIT;
355: }
356:
357: /*
358: * (non-Javadoc)
359: *
360: * @see org.eclipse.ui.views.markers.internal.MarkerView#getFiltersPreferenceName()
361: */
362: String getFiltersPreferenceName() {
363: return IDEInternalPreferences.TASKS_FILTERS;
364: }
365:
366: /* (non-Javadoc)
367: * @see org.eclipse.ui.views.markers.internal.TableView#updateDirectionIndicator(org.eclipse.swt.widgets.TreeColumn)
368: */
369: void updateDirectionIndicator(TreeColumn column) {
370: // Do nothing due to images being obscured
371: }
372:
373: /*
374: * (non-Javadoc)
375: *
376: * @see org.eclipse.ui.views.markers.internal.MarkerView#getMarkerName()
377: */
378: protected String getMarkerName() {
379: return MarkerMessages.task_title;
380: }
381:
382: /*
383: * (non-Javadoc)
384: * @see org.eclipse.ui.views.markers.internal.MarkerView#getUndoContext()
385: */
386: protected IUndoContext getUndoContext() {
387: return WorkspaceUndoUtil.getTasksUndoContext();
388: }
389: }
|