001    package org.gwtbootstrap3.extras.summernote.client.ui.base;
002    
003    /*
004     * #%L
005     * GwtBootstrap3
006     * %%
007     * Copyright (C) 2013 - 2014 GwtBootstrap3
008     * %%
009     * Licensed under the Apache License, Version 2.0 (the "License");
010     * you may not use this file except in compliance with the License.
011     * You may obtain a copy of the License at
012     * 
013     *      http://www.apache.org/licenses/LICENSE-2.0
014     * 
015     * Unless required by applicable law or agreed to in writing, software
016     * distributed under the License is distributed on an "AS IS" BASIS,
017     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018     * See the License for the specific language governing permissions and
019     * limitations under the License.
020     * #L%
021     */
022    
023    import com.google.gwt.core.client.JavaScriptObject;
024    import com.google.gwt.core.client.JsArray;
025    
026    /**
027     * Custom toolbar builder for the summernote editor
028     * <p/>
029     * See: http://hackerwins.github.io/summernote/
030     *
031     * @author godi
032     */
033    public class Toolbar {
034        private static final String STYLE = "style";
035        private static final String FONT_SIZE = "fontsize";
036        private static final String COLOR = "color";
037        private static final String PARA = "para";
038        private static final String HEIGHT = "height";
039        private static final String INSERT = "insert";
040        private static final String TABLE = "table";
041        private static final String VIEW = "view";
042        private static final String HELP = "help";
043        private static final String BOLD = "bold";
044        private static final String ITALIC = "italic";
045        private static final String UNDERLINE = "underline";
046        private static final String CLEAR = "clear";
047        private static final String UL = "ul";
048        private static final String OL = "ol";
049        private static final String PARAGRAPH = "paragraph";
050        private static final String PICTURE = "picture";
051        private static final String LINK = "link";
052        private static final String VIDEO = "video";
053        private static final String FULL_SCREEN = "fullscreen";
054        private static final String CODE_VIEW = "codeview";
055    
056        private boolean showStyleButton;
057        private boolean showBoldButton;
058        private boolean showItalicButton;
059        private boolean showUnderlineButton;
060        private boolean showClearButton;
061        private boolean showFontSizeButton;
062        private boolean showColorButton;
063        private boolean showUnorderedListButton;
064        private boolean showOrderedListButton;
065        private boolean showParagraphButton;
066        private boolean showLineHeightButton;
067        private boolean showInsertPictureButton;
068        private boolean showInsertLinkButton;
069        private boolean showInsertTableButton;
070        private boolean showInsertVideoButton;
071        private boolean showFullScreenButton;
072        private boolean showCodeViewButton;
073        private boolean showHelpButton;
074    
075        public Toolbar setShowStyleButton(final boolean showStyleButton) {
076            this.showStyleButton = showStyleButton;
077            return this;
078        }
079    
080        public Toolbar setShowBoldButton(final boolean showBoldButton) {
081            this.showBoldButton = showBoldButton;
082            return this;
083        }
084    
085        public Toolbar setShowItalicButton(final boolean showItalicButton) {
086            this.showItalicButton = showItalicButton;
087            return this;
088        }
089    
090        public Toolbar setShowUnderlineButton(final boolean showUnderlineButton) {
091            this.showUnderlineButton = showUnderlineButton;
092            return this;
093        }
094    
095        public Toolbar setShowClearButton(final boolean showClearButton) {
096            this.showClearButton = showClearButton;
097            return this;
098        }
099    
100        public Toolbar setShowFontSizeButton(final boolean showFontSizeButton) {
101            this.showFontSizeButton = showFontSizeButton;
102            return this;
103        }
104    
105        public Toolbar setShowColorButton(final boolean showColorButton) {
106            this.showColorButton = showColorButton;
107            return this;
108        }
109    
110        public Toolbar setShowUnorderedListButton(final boolean showUnorderedListButton) {
111            this.showUnorderedListButton = showUnorderedListButton;
112            return this;
113        }
114    
115        public Toolbar setShowOrderedListButton(final boolean showOrderedListButton) {
116            this.showOrderedListButton = showOrderedListButton;
117            return this;
118        }
119    
120        public Toolbar setShowParagraphButton(final boolean showParagraphButton) {
121            this.showParagraphButton = showParagraphButton;
122            return this;
123        }
124    
125        public Toolbar setShowLineHeightButton(final boolean showLineHeightButton) {
126            this.showLineHeightButton = showLineHeightButton;
127            return this;
128        }
129    
130        public Toolbar setShowInsertPictureButton(final boolean showInsertPictureButton) {
131            this.showInsertPictureButton = showInsertPictureButton;
132            return this;
133        }
134    
135        public Toolbar setShowInsertLinkButton(final boolean showInsertLinkButton) {
136            this.showInsertLinkButton = showInsertLinkButton;
137            return this;
138        }
139    
140        public Toolbar setShowInsertTableButton(final boolean showInsertTableButton) {
141            this.showInsertTableButton = showInsertTableButton;
142            return this;
143        }
144    
145        public Toolbar setShowHelpButton(final boolean showHelpButton) {
146            this.showHelpButton = showHelpButton;
147            return this;
148        }
149    
150        public Toolbar setShowInsertVideoButton(final boolean showInsertVideoButton) {
151            this.showInsertVideoButton = showInsertVideoButton;
152            return this;
153        }
154    
155        public Toolbar setShowFullScreenButton(final boolean showFullScreenButton) {
156            this.showFullScreenButton = showFullScreenButton;
157            return this;
158        }
159    
160        public Toolbar setShowCodeViewButton(final boolean showCodeViewButton) {
161            this.showCodeViewButton = showCodeViewButton;
162            return this;
163        }
164    
165        /**
166         * Quick toggle to use everything or nothing
167         *
168         * @param visible toggle visible
169         * @return Toolbar
170         */
171        public Toolbar toggleAll(final boolean visible) {
172            showStyleButton = visible;
173            showBoldButton = visible;
174            showItalicButton = visible;
175            showUnderlineButton = visible;
176            showClearButton = visible;
177            showFontSizeButton = visible;
178            showColorButton = visible;
179            showUnorderedListButton = visible;
180            showOrderedListButton = visible;
181            showParagraphButton = visible;
182            showLineHeightButton = visible;
183            showInsertPictureButton = visible;
184            showInsertLinkButton = visible;
185            showInsertTableButton = visible;
186            showInsertVideoButton = visible;
187            showCodeViewButton = visible;
188            showFullScreenButton = visible;
189            showHelpButton = visible;
190            return this;
191        }
192    
193        public JsArray build() {
194            final JsArray array = JavaScriptObject.createArray().cast();
195    
196            buildArray(showStyleButton, array, STYLE);
197            buildStyles(array);
198            buildArray(showFontSizeButton, array, FONT_SIZE);
199            buildArray(showColorButton, array, COLOR);
200            buildPara(array);
201            buildArray(showLineHeightButton, array, HEIGHT);
202            buildInsert(array);
203            buildArray(showInsertTableButton, array, TABLE);
204            buildView(array);
205            buildArray(showHelpButton, array, HELP);
206    
207            return array;
208        }
209    
210        private void buildArray(final boolean toggle, final JsArray array, final String value) {
211            if (toggle) {
212                array.push(toJSArray(value, toValueArray(value)));
213            }
214        }
215    
216        private void addToValueArray(final boolean toggle, final JavaScriptObject object, final String value) {
217            if (toggle) {
218                pushToValueArray(object, value);
219            }
220        }
221    
222        private void buildView(final JsArray array) {
223            final JsArray view = JavaScriptObject.createArray().cast();
224    
225            addToValueArray(showFullScreenButton, view, FULL_SCREEN);
226            addToValueArray(showCodeViewButton, view, CODE_VIEW);
227    
228            if (!view.toString().isEmpty()) {
229                array.push(toJSArray(VIEW, view));
230            }
231        }
232    
233        private void buildInsert(final JsArray array) {
234            final JsArray insert = JavaScriptObject.createArray().cast();
235    
236            addToValueArray(showInsertPictureButton, insert, PICTURE);
237            addToValueArray(showInsertLinkButton, insert, LINK);
238            addToValueArray(showInsertVideoButton, insert, VIDEO);
239    
240            if (!insert.toString().isEmpty()) {
241                array.push(toJSArray(INSERT, insert));
242            }
243        }
244    
245        private void buildPara(final JsArray array) {
246            final JsArray para = JavaScriptObject.createArray().cast();
247    
248            addToValueArray(showUnorderedListButton, para, UL);
249            addToValueArray(showOrderedListButton, para, OL);
250            addToValueArray(showParagraphButton, para, PARAGRAPH);
251    
252            if (!para.toString().isEmpty()) {
253                array.push(toJSArray(PARA, para));
254            }
255        }
256    
257        private void buildStyles(final JsArray array) {
258            final JsArray styles = JavaScriptObject.createArray().cast();
259    
260            addToValueArray(showBoldButton, styles, BOLD);
261            addToValueArray(showItalicButton, styles, ITALIC);
262            addToValueArray(showUnderlineButton, styles, UNDERLINE);
263            addToValueArray(showClearButton, styles, CLEAR);
264    
265            if (!styles.toString().isEmpty()) {
266                array.push(toJSArray(STYLE, styles));
267            }
268        }
269    
270        private native JavaScriptObject toJSArray(String key, JavaScriptObject value)/*-{
271            return [key, value];
272        }-*/;
273    
274        private native JavaScriptObject toValueArray(String value1)/*-{
275            return [value1];
276        }-*/;
277    
278        private native void pushToValueArray(JavaScriptObject object, String value)/*-{
279            object.push(value);
280        }-*/;
281    }
282