001/*
002 *  Copyright 2012 GWT-Bootstrap
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.github.gwtbootstrap.client.ui;
017
018import com.github.gwtbootstrap.client.ui.base.AbstractTypography;
019import com.github.gwtbootstrap.client.ui.resources.prettify.HasProgrammingLanguage;
020import com.github.gwtbootstrap.client.ui.resources.prettify.PrettifyHelper;
021import com.google.gwt.safehtml.shared.SafeHtmlUtils;
022import com.google.gwt.user.client.DOM;
023import com.google.gwt.user.client.ui.HasHTML;
024
025//@formatter:off
026/**
027 * Simple inline code with syntax highlighting.
028 * 
029 * <p>
030 * <h3>UiBinder Usage:</h3>
031 * 
032 * <pre>
033 * {@code 
034 * <b:Code><a href="http://gwtbootstrap.github.com">Link</a></b:Code>}
035 * </pre>
036 * </p>
037 * 
038 * @since 2.0.4.0
039 * 
040 * @author Dominik Mayer
041 * 
042 * @author Carlos Alexandro Becker
043 * 
044 * @see <a href="http://twitter.github.com/bootstrap/base-css.html#code">Bootstrap documentation</a>
045 * @see CodeBlock
046 */
047//@formatter:on
048public class Code extends AbstractTypography implements HasProgrammingLanguage,
049                HasHTML {
050
051        private final PrettifyHelper helper;
052
053        /**
054         * Creates an empty widget.
055         */
056        public Code() {
057                setElement(DOM.createElement("code"));
058                helper = new PrettifyHelper(this);
059        }
060
061        /**
062         * {@inheritDoc}
063         */
064        @Override
065        protected void onLoad() {
066                super.onLoad();
067                helper.configure();
068        }
069
070        //TODO
071        /**
072         * {@inheritDoc}
073         */
074        public void setLang(String programmingLanguage) {
075                helper.setLang(programmingLanguage);
076        }
077
078        /**
079         * {@inheritDoc}
080         */
081        public String getHTML() {
082                return getText();
083        }
084
085        /**
086         * Sets the widget's text.
087         * <p>
088         * Any HTML content is escaped and displayed as text.
089         * 
090         * @param html
091         *            the text to be set
092         */
093        public void setHTML(String html) {
094                getElement().setInnerHTML(SafeHtmlUtils.htmlEscapeAllowEntities(html));
095        }
096}