001/*
002 * Copyright 2006 - 2013
003 *     Stefan Balev     <stefan.balev@graphstream-project.org>
004 *     Julien Baudry    <julien.baudry@graphstream-project.org>
005 *     Antoine Dutot    <antoine.dutot@graphstream-project.org>
006 *     Yoann Pigné      <yoann.pigne@graphstream-project.org>
007 *     Guilhelm Savin   <guilhelm.savin@graphstream-project.org>
008 * 
009 * This file is part of GraphStream <http://graphstream-project.org>.
010 * 
011 * GraphStream is a library whose purpose is to handle static or dynamic
012 * graph, create them from scratch, file or any source and display them.
013 * 
014 * This program is free software distributed under the terms of two licenses, the
015 * CeCILL-C license that fits European law, and the GNU Lesser General Public
016 * License. You can  use, modify and/ or redistribute the software under the terms
017 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
018 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
019 * the Free Software Foundation, either version 3 of the License, or (at your
020 * option) any later version.
021 * 
022 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
024 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
025 * 
026 * You should have received a copy of the GNU Lesser General Public License
027 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
028 * 
029 * The fact that you are presently reading this means that you have had
030 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
031 */
032package org.graphstream.ui.layout;
033
034import java.security.AccessControlException;
035
036/**
037 * A factory in charge or creating various layout implementations.
038 * 
039 * This class is mainly used to create the default layout for the graph viewer.
040 * You can also use layouts directly on your graphs, but in this case you do not
041 * need this factory.
042 * 
043 * This class looks at the "gs.ui.layout" system property to create a layout
044 * class. You can change this property using
045 * <code>System.setProperty("gs.ui.layout", you_layout_class_name)</code>.
046 */
047public class Layouts {
048        /**
049         * Creates a layout according to the "org.graphstream.ui.layout" system property.
050         * 
051         * @return The new layout or the default GraphStream "Spring-Box" layout if
052         *         the "gs.ui.layout" system property is either not set or contains
053         *         a class that cannot be found.
054         */
055        public static Layout newLayoutAlgorithm() {
056                String layoutClassName;
057
058                try {
059                        layoutClassName = System.getProperty("gs.ui.layout");
060
061                        if (layoutClassName != null) {
062                                System.err.printf("\"gs.ui.layout\" is deprecated,");
063                                System.err.printf("use \"org.graphstream.ui.layout\""
064                                                + " instead\n");
065                        } else {
066                                layoutClassName = System
067                                                .getProperty("org.graphstream.ui.layout");
068                        }
069                } catch (AccessControlException e) {
070                        layoutClassName = null;
071                }
072
073                if (layoutClassName != null) {
074                        try {
075                                Class<?> c = Class.forName(layoutClassName);
076                                Object object = c.newInstance();
077
078                                if (object instanceof Layout) {
079                                        return (Layout) object;
080                                } else {
081                                        System.err.printf("class '%s' is not a 'GraphRenderer'%n",
082                                                        object);
083                                }
084                        } catch (ClassNotFoundException e) {
085                                e.printStackTrace();
086                                System.err
087                                                .printf("Cannot create layout, 'GraphRenderer' class not found : "
088                                                                + e.getMessage());
089                        } catch (InstantiationException e) {
090                                e.printStackTrace();
091                                System.err.printf("Cannot create layout, class '"
092                                                + layoutClassName + "' error : " + e.getMessage());
093                        } catch (IllegalAccessException e) {
094                                e.printStackTrace();
095                                System.err.printf("Cannot create layout, class '"
096                                                + layoutClassName + "' illegal access : "
097                                                + e.getMessage());
098                        }
099                }
100
101                return new org.graphstream.ui.layout.springbox.implementations.SpringBox(
102                                false);
103        }
104}