simplify TextAreaDemo by using the new auto width feature for tables and floating <div>
2 * Copyright (c) 2008-2010, Matthias Mann
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of Matthias Mann nor the names of its contributors may
15 * be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 import de.matthiasmann.twl.DesktopArea;
33 import de.matthiasmann.twl.Event;
34 import de.matthiasmann.twl.FPSCounter;
35 import de.matthiasmann.twl.GUI;
36 import de.matthiasmann.twl.Rect;
37 import de.matthiasmann.twl.ResizableFrame;
38 import de.matthiasmann.twl.ScrollPane;
39 import de.matthiasmann.twl.TextArea;
40 import de.matthiasmann.twl.Timer;
41 import de.matthiasmann.twl.ValueAdjusterInt;
42 import de.matthiasmann.twl.textarea.HTMLTextAreaModel;
43 import de.matthiasmann.twl.model.SimpleIntegerModel;
44 import de.matthiasmann.twl.renderer.lwjgl.LWJGLRenderer;
45 import de.matthiasmann.twl.textarea.StyleAttribute;
46 import de.matthiasmann.twl.textarea.StyleSheet;
47 import de.matthiasmann.twl.textarea.TextAreaModel;
48 import de.matthiasmann.twl.textarea.Value;
49 import de.matthiasmann.twl.theme.ThemeManager;
50 import de.matthiasmann.twl.utils.TextUtil;
51 import java.io.IOException;
52 import java.util.logging.Level;
53 import java.util.logging.Logger;
54 import org.lwjgl.opengl.Display;
55 import org.lwjgl.opengl.DisplayMode;
56 import org.lwjgl.opengl.GL11;
57 import test.SimpleTest;
61 * @author Matthias Mann
63 public class TextAreaDemo extends DesktopArea {
65 public static void main(String[] args) {
67 Display.setDisplayMode(new DisplayMode(800, 600));
69 Display.setTitle("TWL TextArea Demo");
70 Display.setVSyncEnabled(true);
72 LWJGLRenderer renderer = new LWJGLRenderer();
73 TextAreaDemo demo = new TextAreaDemo();
74 GUI gui = new GUI(demo, renderer);
76 ThemeManager theme = ThemeManager.createThemeManager(
77 TextAreaDemo.class.getResource("demo.xml"), renderer);
78 gui.applyTheme(theme);
80 while(!Display.isCloseRequested() && !demo.quit) {
81 GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
86 * requires LWJGL 2.4 - for 2.3 just call Display.update()
88 Display.update(false);
89 GL11.glGetError(); // force sync with multi threaded GL driver
90 Display.sync(60); // ensure 60Hz even without vsync
91 Display.processMessages(); // now process inputs
96 } catch (Exception ex) {
97 SimpleTest.showErrMsg(ex);
102 private final FPSCounter fpsCounter;
103 private final TextFrame textFrame;
107 public TextAreaDemo() {
108 fpsCounter = new FPSCounter();
111 textFrame = new TextFrame();
114 textFrame.setSize(600, 500);
115 textFrame.setPosition(40, 20);
119 protected void layout() {
122 // fpsCounter is bottom right
123 fpsCounter.adjustSize();
124 fpsCounter.setPosition(
125 getInnerWidth() - fpsCounter.getWidth(),
126 getInnerHeight() - fpsCounter.getHeight());
130 protected boolean handleEvent(Event evt) {
131 if(super.handleEvent(evt)) {
134 switch (evt.getType()) {
136 switch (evt.getKeyCode()) {
137 case Event.KEY_ESCAPE:
145 static class TextFrame extends ResizableFrame {
146 private final HTMLTextAreaModel textAreaModel;
147 private final TextArea textArea;
148 private final ScrollPane scrollPane;
153 private static final int MIN_SIZE = 128;
154 private static final int MAX_SIZE = 256;
159 this.textAreaModel = new HTMLTextAreaModel();
160 this.textArea = new TextArea(textAreaModel);
162 readFile("demo.html");
164 textArea.addCallback(new TextArea.Callback() {
165 public void handleLinkClicked(String href) {
166 if(href.startsWith("javascript:")) {
167 handleAction(href.substring(11));
168 } else if(href.startsWith("#")) {
169 TextAreaModel.Element ankor = textAreaModel.getElementById(href.substring(1));
171 Rect rect = textArea.getElementRect(ankor);
173 scrollPane.setScrollPositionY(rect.getY());
182 ValueAdjusterInt vai = new ValueAdjusterInt(new SimpleIntegerModel(0, 100, 50));
183 vai.setTooltipContent("Select a nice value");
184 textArea.registerWidget("niceValueSlider", vai);
186 scrollPane = new ScrollPane(textArea);
187 scrollPane.setFixed(ScrollPane.Fixed.HORIZONTAL);
193 protected void afterAddToGUI(GUI gui) {
194 super.afterAddToGUI(gui);
195 timer = gui.createTimer();
197 timer.setContinuous(true);
198 timer.setCallback(new Runnable() {
206 protected void beforeRemoveFromGUI(GUI gui) {
207 super.beforeRemoveFromGUI(gui);
212 void readFile(String name) {
214 textAreaModel.readHTMLFromURL(TextAreaDemo.class.getResource(name));
216 StyleSheet styleSheet = new StyleSheet();
217 for(String styleSheetLink : textAreaModel.getStyleSheetLinks()) {
219 styleSheet.parse(TextAreaDemo.class.getResource(styleSheetLink));
220 } catch(IOException ex) {
221 Logger.getLogger(TextAreaDemo.class.getName()).log(Level.SEVERE,
222 "Can't parse style sheet: " + styleSheetLink, ex);
225 textArea.setStyleClassResolver(styleSheet);
227 setTitle(TextUtil.notNull(textAreaModel.getTitle()));
231 } catch(IOException ex) {
232 Logger.getLogger(TextAreaDemo.class.getName()).log(Level.SEVERE, "Can't read HTML: " + name, ex);
236 void handleAction(String what) {
237 if("zoomImage()".equals(what)) {
238 if(timer != null && !timer.isRunning()) {
246 size = Math.max(MIN_SIZE, Math.min(MAX_SIZE, size + dir));
247 if(size == MIN_SIZE || size == MAX_SIZE) {
251 TextAreaModel.Element e = textAreaModel.getElementById("portrait");
253 e.setStyle(e.getStyle().with(StyleAttribute.WIDTH, new Value(size, Value.Unit.PX)));
254 textAreaModel.domModified();