src/textarea/TextAreaDemo.java
author Matthias Mann
Thu Jun 23 21:23:21 2011 +0200 (11 months ago)
changeset 251 5a496842d3d1
parent 197 939d8ee43216
child 253 25120c47f789
permissions -rw-r--r--
simplify TextAreaDemo by using the new auto width feature for tables and floating <div>
     1 /*
     2  * Copyright (c) 2008-2010, Matthias Mann
     3  *
     4  * All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     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.
    17  *
    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.
    29  */
    30 package textarea;
    31 
    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;
    58 
    59 /**
    60  *
    61  * @author Matthias Mann
    62  */
    63 public class TextAreaDemo extends DesktopArea {
    64 
    65     public static void main(String[] args) {
    66         try {
    67             Display.setDisplayMode(new DisplayMode(800, 600));
    68             Display.create();
    69             Display.setTitle("TWL TextArea Demo");
    70             Display.setVSyncEnabled(true);
    71 
    72             LWJGLRenderer renderer = new LWJGLRenderer();
    73             TextAreaDemo demo = new TextAreaDemo();
    74             GUI gui = new GUI(demo, renderer);
    75 
    76             ThemeManager theme = ThemeManager.createThemeManager(
    77                     TextAreaDemo.class.getResource("demo.xml"), renderer);
    78             gui.applyTheme(theme);
    79 
    80             while(!Display.isCloseRequested() && !demo.quit) {
    81                 GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
    82 
    83                 gui.update();
    84 
    85                 /**
    86                  * requires LWJGL 2.4 - for 2.3 just call Display.update()
    87                  */
    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
    92             }
    93 
    94             gui.destroy();
    95             theme.destroy();
    96         } catch (Exception ex) {
    97             SimpleTest.showErrMsg(ex);
    98         }
    99         Display.destroy();
   100     }
   101 
   102     private final FPSCounter fpsCounter;
   103     private final TextFrame textFrame;
   104 
   105     public boolean quit;
   106 
   107     public TextAreaDemo() {
   108         fpsCounter = new FPSCounter();
   109         add(fpsCounter);
   110 
   111         textFrame = new TextFrame();
   112         add(textFrame);
   113 
   114         textFrame.setSize(600, 500);
   115         textFrame.setPosition(40, 20);
   116     }
   117 
   118     @Override
   119     protected void layout() {
   120         super.layout();
   121 
   122         // fpsCounter is bottom right
   123         fpsCounter.adjustSize();
   124         fpsCounter.setPosition(
   125                 getInnerWidth() - fpsCounter.getWidth(),
   126                 getInnerHeight() - fpsCounter.getHeight());
   127     }
   128 
   129     @Override
   130     protected boolean handleEvent(Event evt) {
   131         if(super.handleEvent(evt)) {
   132             return true;
   133         }
   134         switch (evt.getType()) {
   135             case KEY_PRESSED:
   136                 switch (evt.getKeyCode()) {
   137                     case Event.KEY_ESCAPE:
   138                         quit = true;
   139                         return true;
   140                 }
   141         }
   142         return false;
   143     }
   144 
   145     static class TextFrame extends ResizableFrame {
   146         private final HTMLTextAreaModel textAreaModel;
   147         private final TextArea textArea;
   148         private final ScrollPane scrollPane;
   149         private Timer timer;
   150         private int size;
   151         private int dir;
   152 
   153         private static final int MIN_SIZE = 128;
   154         private static final int MAX_SIZE = 256;
   155 
   156         public TextFrame() {
   157             setTitle("Text");
   158 
   159             this.textAreaModel = new HTMLTextAreaModel();
   160             this.textArea = new TextArea(textAreaModel);
   161 
   162             readFile("demo.html");
   163 
   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));
   170                         if(ankor != null) {
   171                             Rect rect = textArea.getElementRect(ankor);
   172                             if(rect != null) {
   173                                 scrollPane.setScrollPositionY(rect.getY());
   174                             }
   175                         }
   176                     } else {
   177                         readFile(href);
   178                     }
   179                 }
   180             });
   181 
   182             ValueAdjusterInt vai = new ValueAdjusterInt(new SimpleIntegerModel(0, 100, 50));
   183             vai.setTooltipContent("Select a nice value");
   184             textArea.registerWidget("niceValueSlider", vai);
   185             
   186             scrollPane = new ScrollPane(textArea);
   187             scrollPane.setFixed(ScrollPane.Fixed.HORIZONTAL);
   188 
   189             add(scrollPane);
   190         }
   191 
   192         @Override
   193         protected void afterAddToGUI(GUI gui) {
   194             super.afterAddToGUI(gui);
   195             timer = gui.createTimer();
   196             timer.setDelay(16);
   197             timer.setContinuous(true);
   198             timer.setCallback(new Runnable() {
   199                 public void run() {
   200                     animate();
   201                 }
   202             });
   203         }
   204 
   205         @Override
   206         protected void beforeRemoveFromGUI(GUI gui) {
   207             super.beforeRemoveFromGUI(gui);
   208             timer.stop();
   209             timer = null;
   210         }
   211 
   212         void readFile(String name) {
   213             try {
   214                 textAreaModel.readHTMLFromURL(TextAreaDemo.class.getResource(name));
   215 
   216                 StyleSheet styleSheet = new StyleSheet();
   217                 for(String styleSheetLink : textAreaModel.getStyleSheetLinks()) {
   218                     try {
   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);
   223                     }
   224                 }
   225                 textArea.setStyleClassResolver(styleSheet);
   226                 
   227                 setTitle(TextUtil.notNull(textAreaModel.getTitle()));
   228 
   229                 size = MIN_SIZE;
   230                 dir = -4;
   231             } catch(IOException ex) {
   232                 Logger.getLogger(TextAreaDemo.class.getName()).log(Level.SEVERE, "Can't read HTML: " + name, ex);
   233             }
   234         }
   235 
   236         void handleAction(String what) {
   237             if("zoomImage()".equals(what)) {
   238                 if(timer != null && !timer.isRunning()) {
   239                     dir = -dir;
   240                     timer.start();
   241                 }
   242             }
   243         }
   244 
   245         void animate() {
   246             size = Math.max(MIN_SIZE, Math.min(MAX_SIZE, size + dir));
   247             if(size == MIN_SIZE || size == MAX_SIZE) {
   248                 timer.stop();
   249             }
   250 
   251             TextAreaModel.Element e = textAreaModel.getElementById("portrait");
   252             if(e != null) {
   253                 e.setStyle(e.getStyle().with(StyleAttribute.WIDTH, new Value(size, Value.Unit.PX)));
   254                 textAreaModel.domModified();
   255             }
   256         }
   257     }
   258 }