All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xhtmlrenderer.demo.browser.swt.DemosNavigation Maven / Gradle / Ivy

There is a newer version: 9.9.4
Show newest version
/*
 * {{{ header & license
 * Copyright (c) 2007 Vianney le Clément
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * }}}
 */
package org.xhtmlrenderer.demo.browser.swt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class DemosNavigation {

    private static final String FILE_LIST_URL = "demo:/demos/file-list.txt";

    private final List _demos = new ArrayList<>();
    private int _current;
    private boolean _lock;

    public DemosNavigation(BrowserUserAgent uac) {
        try {
            URL url = new URL(uac.resolveFullURI(FILE_LIST_URL));
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    String[] fields = line.split(",");
                    _demos.add(new Demo(fields[1], fields[0]));
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        _current = -1;
    }

    public int size() {
        return _demos.size();
    }

    public Iterable demos() {
        return _demos;
    }

    public Demo getCurrent() {
        if (_current == -1) {
            return null;
        }
        return _demos.get(_current);
    }

    public void setCurrent(int index) {
        if (index < 0 || index >= _demos.size()) {
            throw new IndexOutOfBoundsException(String.format("Index %s is out of range [%s, %s)", index, 0, _demos.size()));
        }
        if (!_lock) {
            _current = index;
            _lock = true;
        }
    }

    public Demo next() {
        if (_demos.isEmpty()) {
            return null;
        }
        if (!_lock) {
            _current++;
            if (_current >= _demos.size()) {
                _current = 0;
            }
            _lock = true;
        }
        return getCurrent();
    }

    public Demo previous() {
        if (_demos.isEmpty()) {
            return null;
        }
        if (!_lock) {
            _current--;
            if (_current < 0) {
                _current = _demos.size() - 1;
            }
            _lock = true;
        }
        return getCurrent();
    }

    public void setCurrent(Demo demo) {
        setCurrent(_demos.indexOf(demo));
    }

    /**
     * Permit navigation again (e.g. when the document has loaded)
     */
    public void unlock() {
        _lock = false;
    }

    public static class Demo {
        private final String _url;
        private final String _name;

        public Demo(String url, String name) {
            _url = url;
            _name = name;
        }

        public String getUrl() {
            return _url;
        }

        public String getName() {
            return _name;
        }

        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Demo demo)) return false;

            return _name.equals(demo._name) && _url.equals(demo._url);
        }

        public int hashCode() {
            int result = _url.hashCode();
            result = 31 * result + _name.hashCode();
            return result;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy