| /******************************************************************************* |
| * Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency |
| * 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: |
| * Pierre Allard, |
| * Regent L'Archeveque, |
| * Olivier L. Larouche - initial API and implementation |
| * SPDX-License-Identifier: EPL-1.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.apogy.common.emf.impl; |
| |
| import java.util.Date; |
| |
| import org.eclipse.apogy.common.emf.ApogyCommonEMFPackage; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.core.runtime.Status; |
| import org.eclipse.core.runtime.jobs.Job; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class CurrentTimeSourceCustomImpl extends CurrentTimeSourceImpl { |
| |
| private static final Logger Logger = LoggerFactory.getLogger(CurrentTimeSourceImpl.class); |
| |
| private boolean disposed = false; |
| |
| /** |
| * Job used to update the current time. |
| */ |
| private Job updateJob = null; |
| |
| @Override |
| public void pause() { |
| Logger.info("Pausing"); |
| basicSet(this, ApogyCommonEMFPackage.Literals.CURRENT_TIME_SOURCE__PAUSED, true); |
| } |
| |
| @Override |
| public void resume() { |
| Logger.info("Resuming."); |
| |
| // Force a time update right away. |
| Date now = new Date(); |
| updateTime(now); |
| |
| // Force the job to start if not already started. |
| getUpdateJob(); |
| |
| // Resume updates. |
| basicSet(this, ApogyCommonEMFPackage.Literals.CURRENT_TIME_SOURCE__PAUSED, false); |
| } |
| |
| @Override |
| public void dispose() { |
| this.disposed = true; |
| if (this.updateJob != null) { |
| /* Cancel the update job. */ |
| getUpdateJob().cancel(); |
| |
| /* Clears our reference to the Job. */ |
| setUpdateJob(null); |
| } |
| } |
| |
| protected void setUpdateJob(Job updateJob) { |
| this.updateJob = updateJob; |
| } |
| |
| /** |
| * Returns the Job used to update the time. |
| * |
| * @return |
| */ |
| protected Job getUpdateJob() { |
| |
| if (this.updateJob == null) { |
| this.updateJob = new Job("CurrentTimeSource update") { |
| |
| @Override |
| protected IStatus run(IProgressMonitor monitor) { |
| |
| if (!monitor.isCanceled() && !CurrentTimeSourceCustomImpl.this.disposed) { |
| try { |
| // Update time if we are not paused. |
| if (!isPaused()) { |
| Date now = new Date(); |
| updateTime(now); |
| |
| } |
| // Schedule again. |
| CurrentTimeSourceCustomImpl.this.updateJob.schedule(getUpdatePeriod()); |
| } catch (Exception e) { |
| Logger.error(e.getMessage(), e); |
| } |
| } |
| return Status.OK_STATUS; |
| } |
| }; |
| |
| // Starts the job. |
| this.updateJob.schedule(); |
| } |
| |
| |
| return this.updateJob; |
| } |
| } // CurrentTimeSourceImpl |