⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 layouter.java

📁 JPowerGraph is a Java library for creating directed graphs for SWT. It supports graph movement, sele
💻 JAVA
字号:
package net.sourceforge.jpowergraph.layout;

import java.util.Collection;

import net.sourceforge.jpowergraph.Graph;
import net.sourceforge.jpowergraph.GraphListener;

/**
 * The layouter applies the layout strategy to the graph in a separate thread,
 */
public class Layouter {
    /** The graph that this layouter takes care of. */
    protected Graph m_graph;
    /** The layout strategy for this layouter. */
    protected LayoutStrategy m_layoutStrategy;
    /** The thread of the layouter. */
    protected Thread m_layouterThread;

    /**
     * Creates the layouter.
     *
     * @param layoutStrategy            the layout strategy
     */
    public Layouter(LayoutStrategy layoutStrategy) {
        m_layoutStrategy=layoutStrategy;
        m_graph=m_layoutStrategy.getGraph();
        m_graph.addGraphListener(new GraphHandler());
    }
    /**
     * Starts the layouter thread.
     */
    public synchronized void start() {
        if (m_layouterThread==null) {
            m_layouterThread=new LayouterThread();
            m_layouterThread.start();
        }
    }
    /**
     * Stops the layouter thread.
     */
    public void stop() {
        Thread layouterThread;
        synchronized (this) {
            layouterThread=m_layouterThread;
            m_layouterThread=null;
        }
        if (layouterThread!=null) {
            layouterThread.interrupt();
            try {
                layouterThread.join();
            }
            catch (InterruptedException ignored) {
            }
        }
    }

    /**
     * The layouter thread.
     */
    protected class LayouterThread extends Thread {
        public LayouterThread() {
            super("GraphLayouter");
            setDaemon(true);
        }
        public void run() {
            synchronized (m_graph) {
                m_layoutStrategy.graphContentsChanged();
                m_layoutStrategy.notifyGraphLayoutUpdated();
            }
            while (true) {
                try {
                    boolean hasMoreSteps=false;
                    synchronized (m_graph) {
                        hasMoreSteps=m_layoutStrategy.shouldExecuteStep();
                        if (hasMoreSteps) {
                            m_layoutStrategy.executeGraphLayoutStep();
                            m_graph.notifyLayoutUpdated();
                        }
                    }
                    if (hasMoreSteps)
                        Thread.sleep(20);
                    else
                        synchronized (Layouter.this) {
                            Layouter.this.wait();
                        }
                }
                catch (InterruptedException e) {
                    break;
                }
                catch (Throwable shouldntHappen) {
                    shouldntHappen.printStackTrace();
                }
            }
        }
    }

    /**
     * The handler for graph events.
     */
    protected class GraphHandler implements GraphListener {
        public void graphLayoutUpdated(Graph graph) {
            synchronized (m_graph) {
                synchronized (Layouter.this) {
                    if (m_layouterThread!=Thread.currentThread()) {
                        m_layoutStrategy.notifyGraphLayoutUpdated();
                        Layouter.this.notifyAll();
                    }
                }
            }
        }
        public void graphUpdated(Graph graph) {
        }
        public void graphContentsChanged(Graph graph) {
            synchronized (m_graph) {
                m_layoutStrategy.graphContentsChanged();
                notifyGraphChanged();
            }
        }
        public void elementsAdded(Graph graph,Collection nodes,Collection edges) {
            synchronized (m_graph) {
                m_layoutStrategy.elementsAdded(nodes,edges);
                notifyGraphChanged();
            }
        }
        public void elementsRemoved(Graph graph,Collection nodes,Collection edges) {
            synchronized (m_graph) {
                m_layoutStrategy.elementsRemoved(nodes,edges);
                notifyGraphChanged();
            }
        }
        protected void notifyGraphChanged() {
            synchronized (Layouter.this) {
                m_layoutStrategy.notifyGraphLayoutUpdated();
                Layouter.this.notifyAll();
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -