001/*
002 * #%L
003 * GwtMaterial
004 * %%
005 * Copyright (C) 2015 - 2017 GwtMaterialDesign
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 * 
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 * 
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package gwt.material.design.addins.client.docviewer;
021
022import com.google.gwt.dom.client.Document;
023import com.google.gwt.dom.client.Element;
024import gwt.material.design.client.base.AbstractValueWidget;
025import gwt.material.design.client.base.MaterialWidget;
026
027//@formatter:off
028
029/**
030 * A document viewer for your word, excel, powerpoint, pdf and other <a href='http://wiki.mobileread.com/wiki/Google_Docs_Viewer'></a> supported
031 * file types </a>. <br/>
032 * Note that this viewer only work with public files.
033 * <p>
034 * <h3>XML Namespace Declaration</h3>
035 * <pre>
036 * {@code
037 * xmlns:ma='urn:import:gwt.material.design.addins.client'
038 * }
039 * </pre>
040 * <p>
041 * <h3>UiBinder Usage:</h3>
042 * <pre>
043 * {@code
044 * <ma:docviewer.MaterialDocViewer url="http://infolab.stanford.edu/pub/papers/google.pdf" embedded="true"/>
045 * }
046 * </pre>
047 *
048 * @author kevzlou7979
049 * @see <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/#docviewer">Doc Viewer</a>
050 */
051//@formatter:on
052public class MaterialDocViewer extends AbstractValueWidget<String> {
053
054    private String url;
055    private boolean embedded = true;
056
057    public MaterialDocViewer() {
058        super(Document.get().createIFrameElement());
059    }
060
061    public MaterialDocViewer(String url) {
062        this();
063        setUrl(url);
064    }
065
066    @Override
067    protected void onLoad() {
068        super.onLoad();
069
070        updateSrc();
071    }
072
073    @Override
074    public String getValue() {
075        return url;
076    }
077
078    @Override
079    public void setValue(String value, boolean fireEvents) {
080        this.url = value;
081        updateSrc();
082        super.setValue(value, fireEvents);
083    }
084
085    /**
086     * Get the url of the Iframe component.
087     */
088    public String getUrl() {
089        return getValue();
090    }
091
092    /**
093     * Set the url of the public document.
094     */
095    public void setUrl(String url) {
096        setValue(url, true);
097    }
098
099    /**
100     * Check whether the iframe is embedded or not.
101     */
102    public boolean isEmbedded() {
103        return embedded;
104    }
105
106    /**
107     * Set the embedded value of the iframe.
108     */
109    public void setEmbedded(boolean embedded) {
110        this.embedded = embedded;
111        updateSrc();
112    }
113
114    protected void updateSrc() {
115        Element element = getElement();
116        if(element != null) {
117            element.setAttribute("src", "https://docs.google.com/gview?url=" + url + "&embedded=" + embedded);
118        }
119    }
120}