Package org.gephi.dynamic

Source Code of org.gephi.dynamic.DynamicControllerImpl$DynamicModelEventDispatchThread

/*
* Copyright 2008-2010 Gephi
* Authors : Cezary Bartosiak
*           Mathieu Bastian <mathieu.bastian@gephi.org>
* Website : http://www.gephi.org
*
* This file is part of Gephi.
*
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

Copyright 2011 Gephi Consortium. All rights reserved.

The contents of this file are subject to the terms of either the GNU
General Public License Version 3 only ("GPL") or the Common
Development and Distribution License("CDDL") (collectively, the
"License"). You may not use this file except in compliance with the
License. You can obtain a copy of the License at
http://gephi.org/about/legal/license-notice/
or /cddl-1.0.txt and /gpl-3.0.txt. See the License for the
specific language governing permissions and limitations under the
License.  When distributing the software, include this License Header
Notice in each file and include the License files at
/cddl-1.0.txt and /gpl-3.0.txt. If applicable, add the following below the
License Header, with the fields enclosed by brackets [] replaced by
your own identifying information:
"Portions Copyrighted [year] [name of copyright owner]"

If you wish your version of this file to be governed by only the CDDL
or only the GPL Version 3, indicate your decision by adding
"[Contributor] elects to include this software in this distribution
under the [CDDL or GPL Version 3] license." If you do not indicate a
single choice of license, a recipient has the option to distribute
your version of this file under either the CDDL, the GPL Version 3 or
to extend the choice of license to its licensees as provided above.
However, if you add GPL Version 3 code and therefore, elected the GPL
Version 3 license, then the option applies only if the new code is
made subject to such option by the copyright holder.

Contributor(s):

Portions Copyrighted 2011 Gephi Consortium.
*/
package org.gephi.dynamic;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicReference;
import org.gephi.data.attributes.api.Estimator;
import org.gephi.data.attributes.type.TimeInterval;
import org.gephi.dynamic.api.DynamicController;
import org.gephi.dynamic.api.DynamicModel.TimeFormat;
import org.gephi.dynamic.api.DynamicModelEvent;
import org.gephi.dynamic.api.DynamicModelListener;
import org.gephi.project.api.ProjectController;
import org.gephi.project.api.Workspace;
import org.gephi.project.api.WorkspaceListener;
import org.gephi.project.api.WorkspaceProvider;
import org.openide.util.Lookup;
import org.openide.util.lookup.ServiceProvider;

/**
* It is the default implementation of the {@code DynamicController} class.
*
* @author Cezary Bartosiak
* @author Mathieu Bastian
*/
@ServiceProvider(service = DynamicController.class)
public final class DynamicControllerImpl implements DynamicController {
   
    private DynamicModelImpl model;
    private List<DynamicModelListener> listeners;
    private DynamicModelEventDispatchThread eventThread;
    private SetVisibleIntervalThread setIntervalThread;

    /**
     * The default constructor.
     */
    public DynamicControllerImpl() {
        listeners = Collections.synchronizedList(new ArrayList<DynamicModelListener>());
        eventThread = new DynamicModelEventDispatchThread();
        eventThread.start();
        setIntervalThread = new SetVisibleIntervalThread();
        setIntervalThread.start();
       
        ProjectController projectController = Lookup.getDefault().lookup(ProjectController.class);
        projectController.addWorkspaceListener(new WorkspaceListener() {
            @Override
            public void initialize(Workspace workspace) {
                workspace.add(new DynamicModelImpl(DynamicControllerImpl.this, workspace));
            }
           
            @Override
            public void select(Workspace workspace) {
                model = workspace.getLookup().lookup(DynamicModelImpl.class);
                if (model == null) {
                    model = new DynamicModelImpl(DynamicControllerImpl.this, workspace);
                    workspace.add(model);
                }
            }
           
            @Override
            public void unselect(Workspace workspace) {
            }
           
            @Override
            public void close(Workspace workspace) {
            }
           
            @Override
            public void disable() {
                model = null;
            }
        });
        if (projectController.getCurrentProject() != null) {
            Workspace[] workspaces = projectController.getCurrentProject().getLookup().
                    lookup(WorkspaceProvider.class).getWorkspaces();
            for (Workspace workspace : workspaces) {
                DynamicModelImpl m = (DynamicModelImpl) workspace.getLookup().lookup(DynamicModelImpl.class);
                if (m == null) {
                    m = new DynamicModelImpl(this, workspace);
                    workspace.add(m);
                }
                if (workspace == projectController.getCurrentWorkspace()) {
                    model = m;
                }
            }
        }
    }
   
    @Override
    public synchronized DynamicModelImpl getModel() {
        if (model == null) {
            ProjectController projectController = Lookup.getDefault().lookup(ProjectController.class);
            if (projectController.getCurrentWorkspace() != null) {
                Workspace workspace = projectController.getCurrentWorkspace();
                return workspace.getLookup().lookup(DynamicModelImpl.class);
            }
        }
        return model;
    }
   
    @Override
    public synchronized DynamicModelImpl getModel(Workspace workspace) {
        if (workspace != null) {
            DynamicModelImpl m = workspace.getLookup().lookup(DynamicModelImpl.class);
            if (m != null) {
                return m;
            }
            m = new DynamicModelImpl(this, workspace);
            workspace.add(m);
            return m;
        }
        return null;
    }
   
    @Override
    public void setVisibleInterval(TimeInterval interval) {
        if (model != null) {
            setIntervalThread.setVisibleTimeInterval(interval);
            //System.out.println("set visible interval "+interval);
        }
    }
   
    @Override
    public void setVisibleInterval(double low, double high) {
        setVisibleInterval(new TimeInterval(low, high));
    }
   
    @Override
    public void setTimeFormat(TimeFormat timeFormat) {
        if (model != null) {
            model.setTimeFormat(timeFormat);
        }
    }
   
    @Override
    public void setTimeFormat(TimeFormat timeFormat, Workspace workspace) {
        DynamicModelImpl modelImpl = (DynamicModelImpl) getModel(workspace);
        if (modelImpl != null) {
            modelImpl.setTimeFormat(timeFormat);
        }
    }
   
    @Override
    public void setEstimator(Estimator estimator) {
        if (model != null) {
            model.setEstimator(estimator);
        }
    }
   
    @Override
    public void setEstimator(Estimator estimator, Workspace workspace) {
        DynamicModelImpl modelImpl = (DynamicModelImpl) getModel(workspace);
        if (modelImpl != null) {
            modelImpl.setEstimator(estimator);
        }
    }
   
    @Override
    public void setNumberEstimator(Estimator numberEstimator) {
        if (model != null) {
            model.setNumberEstimator(numberEstimator);
        }
    }
   
    @Override
    public void setNumberEstimator(Estimator numberEstimator, Workspace workspace) {
        DynamicModelImpl modelImpl = (DynamicModelImpl) getModel(workspace);
        if (modelImpl != null) {
            modelImpl.setNumberEstimator(numberEstimator);
        }
    }
   
    @Override
    public void addModelListener(DynamicModelListener listener) {
        if (!listeners.contains(listener)) {
            listeners.add(listener);
        }
    }
   
    @Override
    public void removeModelListener(DynamicModelListener listener) {
        listeners.remove(listener);
    }
   
    public void fireModelEvent(DynamicModelEvent event) {
        eventThread.fireEvent(event);
    }
   
    protected class SetVisibleIntervalThread extends Thread {
       
        private boolean stop;
        private final AtomicReference<TimeInterval> lastInterval = new AtomicReference<TimeInterval>();
        private final Object lock = new Object();
       
        public SetVisibleIntervalThread() {
            super("Dynamic Set Visible Interval Thread");
            setDaemon(true);
        }
       
        @Override
        public void run() {
            while (!stop) {
                TimeInterval interval;
                if ((interval = lastInterval.getAndSet(null)) != null) {
                    model.setVisibleTimeInterval(interval);
                }
               
                while (lastInterval.get() == null) {
                    try {
                        synchronized (lock) {
                            lock.wait();
                        }
                    } catch (InterruptedException e) {
                    }
                }
            }
        }
       
        public void stop(boolean stop) {
            this.stop = stop;
        }
       
        public void setVisibleTimeInterval(TimeInterval interval) {
            lastInterval.set(interval);
            synchronized (lock) {
                lock.notifyAll();
            }
        }
    }
   
    protected class DynamicModelEventDispatchThread extends Thread {
       
        private boolean stop;
        private final LinkedBlockingQueue<DynamicModelEvent> eventQueue;
        private final Object lock = new Object();
       
        public DynamicModelEventDispatchThread() {
            super("Dynamic Model EventDispatchThread");
            setDaemon(true);
            this.eventQueue = new LinkedBlockingQueue<DynamicModelEvent>();
        }
       
        @Override
        public void run() {
            while (!stop) {
                DynamicModelEvent evt;
                while ((evt = eventQueue.poll()) != null) {
                    for (DynamicModelListener l : listeners.toArray(new DynamicModelListener[0])) {
                        l.dynamicModelChanged(evt);
                    }
                }
               
                while (eventQueue.isEmpty()) {
                    try {
                        synchronized (lock) {
                            lock.wait();
                        }
                    } catch (InterruptedException e) {
                    }
                }
            }
        }
       
        public void stop(boolean stop) {
            this.stop = stop;
        }
       
        public void fireEvent(DynamicModelEvent event) {
            eventQueue.add(event);
            synchronized (lock) {
                lock.notifyAll();
            }
        }
    }
}
TOP

Related Classes of org.gephi.dynamic.DynamicControllerImpl$DynamicModelEventDispatchThread

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.