jaroslav@309: /**
jaroslav@309: * HTML via Java(tm) Language Bindings
jaroslav@309: * Copyright (C) 2013 Jaroslav Tulach
jaroslav@309: *
jaroslav@309: * This program is free software: you can redistribute it and/or modify
jaroslav@309: * it under the terms of the GNU General Public License as published by
jaroslav@309: * the Free Software Foundation, version 2 of the License.
jaroslav@309: *
jaroslav@309: * This program is distributed in the hope that it will be useful,
jaroslav@309: * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@309: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
jaroslav@309: * GNU General Public License for more details. apidesign.org
jaroslav@309: * designates this particular file as subject to the
jaroslav@309: * "Classpath" exception as provided by apidesign.org
jaroslav@309: * in the License file that accompanied this code.
jaroslav@309: *
jaroslav@309: * You should have received a copy of the GNU General Public License
jaroslav@309: * along with this program. Look for COPYING file in the top folder.
jaroslav@309: * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
jaroslav@309: */
jaroslav@309: package org.apidesign.html.boot.impl;
jaroslav@309:
jaroslav@322: import java.io.Closeable;
jaroslav@322: import java.io.IOException;
jaroslav@322: import java.util.logging.Logger;
jaroslav@309: import org.apidesign.html.boot.spi.Fn;
jaroslav@309:
jaroslav@309: /**
jaroslav@309: *
jaroslav@309: * @author Jaroslav Tulach
jaroslav@309: */
jaroslav@322: public final class FnContext implements Closeable {
jaroslav@322: private static final Logger LOG = Logger.getLogger(FnContext.class.getName());
jaroslav@322:
jaroslav@323: private Object prev;
jaroslav@322: private FnContext(Fn.Presenter p) {
jaroslav@322: this.prev = p;
jaroslav@309: }
jaroslav@322:
jaroslav@322: @Override
jaroslav@322: public void close() throws IOException {
jaroslav@323: if (prev != this) {
jaroslav@323: currentPresenter((Fn.Presenter)prev);
jaroslav@323: prev = this;
jaroslav@322: }
jaroslav@322: }
jaroslav@322: /*
jaroslav@322: @Override
jaroslav@322: protected void finalize() throws Throwable {
jaroslav@322: if (prev != null) {
jaroslav@322: LOG.warning("Unclosed context!");
jaroslav@322: }
jaroslav@322: }
jaroslav@322: */
jaroslav@322: public static Closeable activate(Fn.Presenter newP) {
jaroslav@322: return new FnContext(currentPresenter(newP));
jaroslav@322: }
jaroslav@322:
jaroslav@309:
jaroslav@309: private static final ThreadLocal CURRENT = new ThreadLocal();
jaroslav@309:
jaroslav@309: public static Fn.Presenter currentPresenter(Fn.Presenter p) {
jaroslav@309: Fn.Presenter prev = CURRENT.get();
jaroslav@309: CURRENT.set(p);
jaroslav@309: return prev;
jaroslav@309: }
jaroslav@309:
jaroslav@309: public static Fn.Presenter currentPresenter() {
jaroslav@309: Fn.Presenter p = CURRENT.get();
jaroslav@309: if (p == null) {
jaroslav@309: throw new IllegalStateException("No current WebView context around!");
jaroslav@309: }
jaroslav@309: return p;
jaroslav@309: }
jaroslav@309:
jaroslav@309: }