|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004, 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.tasks.ui; |
| 13 |
|
| 14 |
import java.io.File; |
| 15 |
|
| 16 |
import org.eclipse.core.runtime.Assert; |
| 17 |
import org.eclipse.core.runtime.CoreException; |
| 18 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 19 |
import org.eclipse.core.runtime.IStatus; |
| 20 |
import org.eclipse.core.runtime.Status; |
| 21 |
import org.eclipse.core.runtime.jobs.ISchedulingRule; |
| 22 |
import org.eclipse.mylyn.commons.core.StatusHandler; |
| 23 |
import org.eclipse.mylyn.context.core.AbstractContextListener; |
| 24 |
import org.eclipse.mylyn.context.core.ContextChangeEvent; |
| 25 |
import org.eclipse.mylyn.context.core.ContextCore; |
| 26 |
import org.eclipse.mylyn.context.core.IInteractionContext; |
| 27 |
import org.eclipse.mylyn.internal.context.core.ContextCorePlugin; |
| 28 |
import org.eclipse.mylyn.internal.monitor.ui.ActivityContextManager; |
| 29 |
import org.eclipse.mylyn.internal.monitor.ui.MonitorUiPlugin; |
| 30 |
import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants; |
| 31 |
import org.eclipse.mylyn.internal.tasks.core.externalization.AbstractExternalizationParticipant; |
| 32 |
import org.eclipse.mylyn.internal.tasks.core.externalization.ExternalizationManager; |
| 33 |
import org.eclipse.mylyn.internal.tasks.core.externalization.IExternalizationContext; |
| 34 |
import org.eclipse.mylyn.monitor.ui.IUserAttentionListener; |
| 35 |
import org.eclipse.mylyn.tasks.core.ITask; |
| 36 |
import org.eclipse.mylyn.tasks.core.ITaskActivityListener; |
| 37 |
import org.eclipse.mylyn.tasks.ui.TasksUi; |
| 38 |
|
| 39 |
/** |
| 40 |
* This externalization participant only handles saving the active context periodically. No snapshots are taken and task |
| 41 |
* activation and deactivation control the load and final write of the context in InteractionContextManager. |
| 42 |
* |
| 43 |
* @author Shawn Minto |
| 44 |
*/ |
| 45 |
@SuppressWarnings("restriction") |
| 46 |
public class ActiveContextExternalizationParticipant extends AbstractExternalizationParticipant implements |
| 47 |
ITaskActivityListener, IUserAttentionListener { |
| 48 |
private boolean isDirty = false; |
| 49 |
|
| 50 |
private final ExternalizationManager manager; |
| 51 |
|
| 52 |
private long lastUpdate; |
| 53 |
|
| 54 |
private IInteractionContext currentlyActiveContext; |
| 55 |
|
| 56 |
private final AbstractContextListener listener = new AbstractContextListener() { |
| 57 |
@Override |
| 58 |
public void contextChanged(ContextChangeEvent event) { |
| 59 |
switch (event.getEventKind()) { |
| 60 |
case ACTIVATED: |
| 61 |
currentlyActiveContext = event.getContext(); |
| 62 |
break; |
| 63 |
case DEACTIVATED: |
| 64 |
currentlyActiveContext = null; |
| 65 |
setDirty(false); |
| 66 |
break; |
| 67 |
} |
| 68 |
} |
| 69 |
}; |
| 70 |
|
| 71 |
public ActiveContextExternalizationParticipant(ExternalizationManager manager) { |
| 72 |
this.manager = manager; |
| 73 |
} |
| 74 |
|
| 75 |
public void registerListeners() { |
| 76 |
ContextCore.getContextManager().addListener(listener); |
| 77 |
TasksUi.getTaskActivityManager().addActivityListener(this); |
| 78 |
if (MonitorUiPlugin.getDefault().getActivityContextManager() != null) { |
| 79 |
((ActivityContextManager) MonitorUiPlugin.getDefault().getActivityContextManager()).addListener(this); |
| 80 |
} else { |
| 81 |
StatusHandler.log(new Status(IStatus.WARNING, TasksUiPlugin.ID_PLUGIN, |
| 82 |
"Unable to register user activity listener.", new Exception())); //$NON-NLS-1$ |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// currently not called since no way to remove a participant |
| 87 |
public void dispose() { |
| 88 |
ContextCore.getContextManager().removeListener(listener); |
| 89 |
TasksUi.getTaskActivityManager().removeActivityListener(this); |
| 90 |
if (MonitorUiPlugin.getDefault().getActivityContextManager() != null) { |
| 91 |
((ActivityContextManager) MonitorUiPlugin.getDefault().getActivityContextManager()).removeListener(this); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public void execute(IExternalizationContext context, IProgressMonitor monitor) throws CoreException { |
| 97 |
Assert.isNotNull(context); |
| 98 |
switch (context.getKind()) { |
| 99 |
case SAVE: |
| 100 |
if (shouldWriteContext()) { |
| 101 |
setDirty(false); |
| 102 |
ContextCorePlugin.getContextManager().saveContext(currentlyActiveContext); |
| 103 |
} |
| 104 |
break; |
| 105 |
case LOAD: |
| 106 |
// ignore loads since we will do this synchronously with task activation |
| 107 |
break; |
| 108 |
case SNAPSHOT: |
| 109 |
// ignore snapshots |
| 110 |
break; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public String getDescription() { |
| 116 |
return Messages.ActiveContextExternalizationParticipant_Active_Task_Context; |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public ISchedulingRule getSchedulingRule() { |
| 121 |
return ITasksCoreConstants.ACTIVE_CONTEXT_SCHEDULING_RULE; |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public boolean isDirty() { |
| 126 |
synchronized (this) { |
| 127 |
return isDirty; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
public void setDirty(boolean dirty) { |
| 132 |
synchronized (this) { |
| 133 |
isDirty = dirty; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public String getFileName() { |
| 139 |
// ignore |
| 140 |
return null; |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
public void load(File sourceFile, IProgressMonitor monitor) throws CoreException { |
| 145 |
// ignore see execute method |
| 146 |
} |
| 147 |
|
| 148 |
@Override |
| 149 |
public void save(File targetFile, IProgressMonitor monitor) throws CoreException { |
| 150 |
// ignore see execute method |
| 151 |
} |
| 152 |
|
| 153 |
public void elapsedTimeUpdated(ITask task, long newElapsedTime) { |
| 154 |
if (System.currentTimeMillis() - lastUpdate > 1000 * 60 * 3) { |
| 155 |
// TODO TYR TO CHECK IF IT IS DIRTY AND IT EXISTS |
| 156 |
setDirty(shouldWriteContext()); |
| 157 |
if (isDirty()) { |
| 158 |
manager.requestSave(); |
| 159 |
} |
| 160 |
lastUpdate = System.currentTimeMillis(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
private boolean shouldWriteContext() { |
| 165 |
if (ContextCorePlugin.getContextManager() != null && currentlyActiveContext != null |
| 166 |
&& currentlyActiveContext.getAllElements().size() > 0) { |
| 167 |
// we could add a check here for whether there were changes to the context |
| 168 |
return true; |
| 169 |
} |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
public void activityReset() { |
| 174 |
// ignore |
| 175 |
} |
| 176 |
|
| 177 |
public void userAttentionGained() { |
| 178 |
// ignore |
| 179 |
} |
| 180 |
|
| 181 |
public void userAttentionLost() { |
| 182 |
setDirty(shouldWriteContext()); |
| 183 |
if (isDirty()) { |
| 184 |
manager.requestSave(); |
| 185 |
} |
| 186 |
lastUpdate = System.currentTimeMillis(); |
| 187 |
} |
| 188 |
|
| 189 |
} |