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     */
016    package com.github.gwtbootstrap.client.ui.resources.prettify;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import com.github.gwtbootstrap.client.ui.resources.JavaScriptInjector;
022    import com.google.gwt.dom.client.StyleInjector;
023    import com.google.gwt.resources.client.TextResource;
024    import com.google.gwt.user.client.ui.Widget;
025    
026    /**
027     * A helper class to inject and configure Google Code Prettify in our code
028     * components.
029     * 
030     * @since 2.0.4.0
031     * 
032     * @author Carlos Alexandro Becker
033     * 
034     */
035    public class PrettifyHelper implements HasProgrammingLanguage {
036    
037            static {
038                    JavaScriptInjector.inject(PrettifyResources.RESOURCES.prettify_js()
039                                    .getText());
040                    StyleInjector.inject(PrettifyResources.RESOURCES.prettify_css()
041                                    .getText());
042            }
043    
044            static List<Integer> importedLangs = new ArrayList<Integer>();
045            private final Widget w;
046    
047            public PrettifyHelper(Widget w) {
048                    super();
049                    this.w = w;
050            }
051    
052            /**
053             * will import the language if needed.
054             */
055            public void setLang(String lang) {
056    
057                    int i = PrettifyResources.speciallangs.indexOf(lang);
058    
059                    // XXX thats very very very very ugly, sorry.
060    
061                    if (i > -1 && !importedLangs.contains(i)) {
062                            TextResource tr = null;
063                            switch (i) {
064                                    case 0:
065                                            tr = PrettifyResources.RESOURCES.apollo();
066                                            break;
067                                    case 1:
068                                            tr = PrettifyResources.RESOURCES.clj();
069                                            break;
070                                    case 2:
071                                            tr = PrettifyResources.RESOURCES.css();
072                                            break;
073                                    case 3:
074                                            tr = PrettifyResources.RESOURCES.go();
075                                            break;
076                                    case 4:
077                                            tr = PrettifyResources.RESOURCES.hs();
078                                            break;
079                                    case 5:
080                                            tr = PrettifyResources.RESOURCES.lisp();
081                                            break;
082                                    case 6:
083                                            tr = PrettifyResources.RESOURCES.lua();
084                                            break;
085                                    case 7:
086                                            tr = PrettifyResources.RESOURCES.ml();
087                                            break;
088                                    case 8:
089                                            tr = PrettifyResources.RESOURCES.ml();
090                                            break;
091                                    case 9:
092                                            tr = PrettifyResources.RESOURCES.n();
093                                            break;
094                                    case 10:
095                                            tr = PrettifyResources.RESOURCES.proto();
096                                            break;
097                                    case 11:
098                                            tr = PrettifyResources.RESOURCES.scala();
099                                            break;
100                                    case 12:
101                                            tr = PrettifyResources.RESOURCES.sql();
102                                            break;
103                                    case 13:
104                                            tr = PrettifyResources.RESOURCES.tex();
105                                            break;
106                                    case 14:
107                                            tr = PrettifyResources.RESOURCES.vb();
108                                            break;
109                                    case 15:
110                                            tr = PrettifyResources.RESOURCES.vhdl();
111                                            break;
112                                    case 16:
113                                            tr = PrettifyResources.RESOURCES.wiki();
114                                            break;
115                                    case 17:
116                                            tr = PrettifyResources.RESOURCES.xq();
117                                            break;
118    
119                                    default:
120                                            break;
121                            }
122                            if (tr != null) {
123                                    JavaScriptInjector.inject(tr.getText());
124                                    importedLangs.add(i);
125                            }
126                    }
127            }
128    
129            /**
130             * Configures prettyprint without line numbers.
131             */
132            public void configure() {
133                    configure(false);
134            }
135    
136            /**
137             * Configures prettyprint with optional line numbers.
138             * 
139             * @param showLineNumber
140             *            <code>true</code> if line numbers should be shown
141             */
142            public void configure(boolean showLineNumber) {
143                    w.setStyleName("prettyprint");
144                    if (showLineNumber)
145                            w.addStyleName("linenums");
146                    setup();
147            }
148    
149            private native void setup() /*-{
150                    $wnd.prettyPrint();
151            }-*/;
152    }