You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by no...@apache.org on 2010/06/08 14:41:18 UTC

svn commit: r952639 - in /pivot/trunk: tests/src/org/apache/pivot/tests/text/ wtk/src/org/apache/pivot/wtk/skin/ wtk/src/org/apache/pivot/wtk/text/

Author: noelgrandin
Date: Tue Jun  8 12:41:17 2010
New Revision: 952639

URL: http://svn.apache.org/viewvc?rev=952639&view=rev
Log:
PIVOT-31 Add rich text support to TextArea
add support for background color, foreground color, and fonts on text

Modified:
    pivot/trunk/tests/src/org/apache/pivot/tests/text/ElementAdapter.java
    pivot/trunk/tests/src/org/apache/pivot/tests/text/document.wtkx
    pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Element.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/text/ElementListener.java

Modified: pivot/trunk/tests/src/org/apache/pivot/tests/text/ElementAdapter.java
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/text/ElementAdapter.java?rev=952639&r1=952638&r2=952639&view=diff
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/text/ElementAdapter.java (original)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/text/ElementAdapter.java Tue Jun  8 12:41:17 2010
@@ -33,7 +33,7 @@ import org.apache.pivot.wtk.text.TextNod
 
 
 public class ElementAdapter extends NodeAdapter implements List<NodeAdapter> {
-    private ElementListener elementListener = new ElementListener() {
+    private ElementListener elementListener = new ElementListener.Adapter() {
         @Override
         public void nodeInserted(Element element, int index) {
             // Insert/attach node

Modified: pivot/trunk/tests/src/org/apache/pivot/tests/text/document.wtkx
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/text/document.wtkx?rev=952639&r1=952638&r2=952639&view=diff
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/text/document.wtkx (original)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/text/document.wtkx Tue Jun  8 12:41:17 2010
@@ -51,17 +51,20 @@ limitations under the License.
     </Paragraph>
     <Paragraph></Paragraph>
     <Paragraph>1234</Paragraph>
+    <Paragraph font="Arial BOLD 24">abcd</Paragraph>
+    <Paragraph foregroundColor="red">abcd</Paragraph>
+    <Paragraph backgroundColor="red">abcd</Paragraph>
     <Paragraph>abcd</Paragraph>
     <Paragraph>Pivot is an open-source platform for building rich internet applications in Java. It combines the enhanced productivity and usability features of a modern RIA toolkit with the robustness of the industry-standard Java platform.</Paragraph>
     <Paragraph></Paragraph>
     <Paragraph>Pivot applications are written using a combination of Java and XML and can be run either as an applet or as a standalone (optionally offline) desktop application. While Pivot was designed to be familiar to web developers who have experience building AJAX applications using HTML, CSS, and JavaScript, it provides a much richer set of standard widgets than HTML, and allows developers to create sophisticated user experiences much more quickly and easily. Pivot will also seem familiar to Swing developers, as both Swing and Pivot are based on Java2D and employ a model-view-controller (MVC) architecture to separate component data from presentation. However, Pivot includes additional features that make building modern GUI applications much easier, including declarative UI, data binding, effects and transitions, and web services integration.</Paragraph>
-    <Paragraph>EFGH</Paragraph>
+    <Paragraph font="Arial BOLD 24" foregroundColor="green" backgroundColor="black">EFGH</Paragraph>
     <Paragraph>Pivot is an open-source platform for building rich internet applications in Java. It combines the enhanced productivity and usability features of a modern RIA toolkit with the robustness of the industry-standard Java platform.</Paragraph>
     <Paragraph>Pivot applications are written using a combination of Java and XML and can be run either as an applet or as a standalone (optionally offline) desktop application. While Pivot was designed to be familiar to web developers who have experience building AJAX applications using HTML, CSS, and JavaScript, it provides a much richer set of standard widgets than HTML, and allows developers to create sophisticated user experiences much more quickly and easily. Pivot will also seem familiar to Swing developers, as both Swing and Pivot are based on Java2D and employ a model-view-controller (MVC) architecture to separate component data from presentation. However, Pivot includes additional features that make building modern GUI applications much easier, including declarative UI, data binding, effects and transitions, and web services integration.</Paragraph>
     <Paragraph>IJKL</Paragraph>
     <Paragraph>Pivot is an open-source platform for building rich internet applications in Java. It combines the enhanced productivity and usability features of a modern RIA toolkit with the robustness of the industry-standard Java platform.</Paragraph>
     <Paragraph>PivotapplicationsarewrittenusingacombinationofJavaandXMLandcanberuneitherasanappletorasastandaloneoptionallyofflinedesktopapplicationWhilePivotwasdesignedtobefamiliartowebdeveloperswhohaveexperiencebuildingAJAXapplicationsusingHTMLCSSandJavaScriptitprovidesamuchrichersetofstandardwidgetsthanHTMLandallowsdeveloperstocreatesophisticateduserexperiencesmuchmorequicklyandeasily. Pivot will also seem familiar to Swing developers, as both Swing and Pivot are based on Java2D and employ a model-view-controller (MVC) architecture to separate component data from presentation. However, Pivot includes additional features that make building modern GUI applications much easier, including declarative UI, data binding, effects and transitions, and web services integration.</Paragraph>
-    <Paragraph>MNOP</Paragraph>
+    <Paragraph font="Arial ITALIC 24">MNOP</Paragraph>
     <Paragraph>Pivot is an open-source platform for building rich internet applications in Java. It combines the enhanced productivity and usability features of a modern RIA toolkit with the robustness of the industry-standard Java platform.</Paragraph>
     <Paragraph>PivotapplicationsarewrittenusingacombinationofJavaandXMLandcanberuneitherasanappletorasastandaloneoptionallyofflinedesktopapplicationWhilePivotwasdesignedtobefamiliartowebdeveloperswhohaveexperiencebuildingAJAXapplicationsusingHTMLCSSandJavaScriptitprovidesamuchrichersetofstandardwidgetsthanHTMLandallowsdeveloperstocreatesophisticateduserexperiencesmuchmorequicklyandeasily. Pivot will also seem familiar to Swing developers, as both Swing and Pivot are based on Java2D and employ a model-view-controller (MVC) architecture to separate component data from presentation. However, Pivot includes additional features that make building modern GUI applications much easier, including declarative UI, data binding, effects and transitions, and web services integration.</Paragraph>
     <Paragraph>QRST</Paragraph>

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java?rev=952639&r1=952638&r2=952639&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkin.java Tue Jun  8 12:41:17 2010
@@ -391,6 +391,12 @@ public class TextAreaSkin extends Contai
                     Graphics2D nodeViewGraphics = (Graphics2D)graphics.create();
                     nodeViewGraphics.translate(nodeViewBounds.x, nodeViewBounds.y);
 
+                    Color styledBackgroundColor = getStyledBackgroundColor();
+                    if (styledBackgroundColor != null) {
+                        nodeViewGraphics.setColor(styledBackgroundColor);
+                        nodeViewGraphics.fillRect(nodeViewBounds.x, nodeViewBounds.y, nodeViewBounds.width, nodeViewBounds.height);
+                    }
+                    
                     // NOTE We don't clip here because views should generally
                     // not overlap and clipping would impose an unnecessary
                     // performance penalty
@@ -404,6 +410,22 @@ public class TextAreaSkin extends Contai
             }
         }
 
+        private Color getStyledBackgroundColor() {
+            Color backgroundColor = null;
+            Node node = getNode();
+            // run up the tree until we find a Element's style to apply
+            while (node != null) {
+                if (node instanceof Element) {
+                    backgroundColor = ((Element) node).getBackgroundColor();
+                    if (backgroundColor != null) {
+                        break;
+                    }
+                }
+                node = node.getParent();
+            }
+            return backgroundColor;
+        }
+        
         @Override
         public Bounds getCharacterBounds(int offset) {
             Bounds characterBounds = null;
@@ -443,6 +465,21 @@ public class TextAreaSkin extends Contai
         }
 
         @Override
+        public void fontChanged(Element element, Font previousFont) {
+            invalidate();
+        }
+        
+        @Override
+        public void backgroundColorChanged(Element element, Color previousBackgroundColor) {
+            invalidate();
+        }
+        
+        @Override
+        public void foregroundColorChanged(Element element, Color previousForegroundColor) {
+            invalidate();
+        }
+        
+        @Override
         public Iterator<NodeView> iterator() {
             return new ImmutableIterator<NodeView>(nodeViews.iterator());
         }
@@ -1021,6 +1058,7 @@ public class TextAreaSkin extends Contai
                 float lineWidth = 0;
                 int lastWhitespaceIndex = -1;
 
+                Font effectiveFont = getEffectiveFont();
                 char c = ci.first();
                 while (c != CharacterIterator.DONE
                     && lineWidth < breakWidth) {
@@ -1029,7 +1067,7 @@ public class TextAreaSkin extends Contai
                     }
 
                     int i = ci.getIndex();
-                    Rectangle2D characterBounds = font.getStringBounds(ci, i, i + 1, fontRenderContext);
+                    Rectangle2D characterBounds = effectiveFont.getStringBounds(ci, i, i + 1, fontRenderContext);
                     lineWidth += characterBounds.getWidth();
 
                     c = ci.current();
@@ -1057,7 +1095,7 @@ public class TextAreaSkin extends Contai
                     end = ci.getEndIndex();
                 }
 
-                glyphVector = font.createGlyphVector(fontRenderContext,
+                glyphVector = getEffectiveFont().createGlyphVector(fontRenderContext,
                     textNode.getCharacterIterator(start, end));
 
                 if (end < ci.getEndIndex()) {
@@ -1085,10 +1123,10 @@ public class TextAreaSkin extends Contai
                 TextArea textArea = (TextArea)getComponent();
 
                 FontRenderContext fontRenderContext = Platform.getFontRenderContext();
-                LineMetrics lm = font.getLineMetrics("", fontRenderContext);
+                LineMetrics lm = getEffectiveFont().getLineMetrics("", fontRenderContext);
                 float ascent = lm.getAscent();
 
-                graphics.setFont(font);
+                graphics.setFont(getEffectiveFont());
 
                 int selectionStart = textArea.getSelectionStart();
                 int selectionLength = textArea.getSelectionLength();
@@ -1127,7 +1165,7 @@ public class TextAreaSkin extends Contai
                     unselectedArea.subtract(new Area(selection));
 
                     Graphics2D textGraphics = (Graphics2D)graphics.create();
-                    textGraphics.setColor(color);
+                    textGraphics.setColor(getEffectiveForegroundColor());
                     textGraphics.clip(unselectedArea);
                     textGraphics.drawGlyphVector(glyphVector, 0, ascent);
                     textGraphics.dispose();
@@ -1148,7 +1186,7 @@ public class TextAreaSkin extends Contai
                     selectedTextGraphics.dispose();
                 } else {
                     // Draw the text
-                    graphics.setColor(color);
+                    graphics.setColor(getEffectiveForegroundColor());
                     graphics.drawGlyphVector(glyphVector, 0, ascent);
                 }
             }
@@ -1172,7 +1210,7 @@ public class TextAreaSkin extends Contai
         @Override
         public int getInsertionPoint(int x, int y) {
             FontRenderContext fontRenderContext = Platform.getFontRenderContext();
-            LineMetrics lm = font.getLineMetrics("", fontRenderContext);
+            LineMetrics lm = getEffectiveFont().getLineMetrics("", fontRenderContext);
             float ascent = lm.getAscent();
 
             int n = glyphVector.getNumGlyphs();
@@ -1199,7 +1237,47 @@ public class TextAreaSkin extends Contai
 
             return i;
         }
-
+        
+        private Font getEffectiveFont() {
+            Font font = null;
+            Node node = getNode().getParent();
+            // run up the tree until we find a Element's style to apply
+            while (node != null) {
+                if (node instanceof Element) {
+                    font = ((Element) node).getFont();
+                    if (font != null) {
+                        break;
+                    }
+                }
+                node = node.getParent();
+            }
+            // if we find nothing, use the default font
+            if (node == null) {
+                font = TextAreaSkin.this.font;
+            }
+            return font;
+        }
+
+        private Color getEffectiveForegroundColor() {
+            Color foregroundColor = null;
+            Node node = getNode().getParent();
+            // run up the tree until we find a Element's style to apply
+            while (node != null) {
+                if (node instanceof Element) {
+                    foregroundColor = ((Element) node).getForegroundColor();
+                    if (foregroundColor != null) {
+                        break;
+                    }
+                }
+                node = node.getParent();
+            }
+            // if we find nothing, use the default color
+            if (node == null) {
+                foregroundColor = TextAreaSkin.this.color;
+            }
+            return foregroundColor;
+        }
+        
         @Override
         public int getNextInsertionPoint(int x, int from, FocusTraversalDirection direction) {
             int offset = -1;
@@ -2519,6 +2597,8 @@ public class TextAreaSkin extends Contai
             nodeView = new TextNodeView((TextNode)node);
         } else if (node instanceof ImageNode) {
             nodeView = new ImageNodeView((ImageNode)node);
+        } else if (node instanceof ComponentNode) {
+            nodeView = new ComponentNodeView((ComponentNode)node);
         } else {
             throw new IllegalArgumentException("Unsupported node type: "
                 + node.getClass().getName());

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Element.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Element.java?rev=952639&r1=952638&r2=952639&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Element.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Element.java Tue Jun  8 12:41:17 2010
@@ -16,12 +16,18 @@
  */
 package org.apache.pivot.wtk.text;
 
+import java.awt.Color;
+import java.awt.Font;
 import java.util.Iterator;
 
 import org.apache.pivot.collections.ArrayList;
 import org.apache.pivot.collections.Sequence;
+import org.apache.pivot.json.JSONSerializer;
+import org.apache.pivot.serialization.SerializationException;
 import org.apache.pivot.util.ImmutableIterator;
 import org.apache.pivot.util.ListenerList;
+import org.apache.pivot.wtk.GraphicsUtilities;
+import org.apache.pivot.wtk.Theme;
 
 /**
  * Abstract base class for elements.
@@ -48,10 +54,32 @@ public abstract class Element extends No
                 listener.nodesRemoved(element, index, nodes);
             }
         }
+        
+        @Override
+        public void fontChanged(Element element, Font previousFont) {
+            for (ElementListener listener : this) {
+                listener.fontChanged(element, previousFont);
+            }
+        }
+        @Override
+        public void backgroundColorChanged(Element element, Color previousBackgroundColor) {
+            for (ElementListener listener : this) {
+                listener.backgroundColorChanged(element, previousBackgroundColor);
+            }
+        }
+        @Override
+        public void foregroundColorChanged(Element element, Color previousForegroundColor) {
+            for (ElementListener listener : this) {
+                listener.foregroundColorChanged(element, previousForegroundColor);
+            }
+        }
     }
 
     private int characterCount = 0;
     private ArrayList<Node> nodes = new ArrayList<Node>();
+    private java.awt.Font font;
+    private Color foregroundColor;
+    private Color backgroundColor;
 
     private ElementListenerList elementListeners = new ElementListenerList();
 
@@ -569,7 +597,114 @@ public abstract class Element extends No
         System.out.println();
     }
 
+    public java.awt.Font getFont() {
+        return font;
+    }
+
+    public void setFont(Font font) {
+        if (font == null) {
+            throw new IllegalArgumentException("font is null.");
+        }
+
+        Font previousFont = this.font;
+        if (previousFont != font) {
+            this.font = font;
+            elementListeners.fontChanged(this, previousFont);
+        }
+    }
+
+    public final void setFont(String font) {
+        if (font == null) {
+            throw new IllegalArgumentException("font is null.");
+        }
+
+        if (font.startsWith("{")) {
+            try {
+                setFont(Theme.deriveFont(JSONSerializer.parseMap(font)));
+            } catch (SerializationException exception) {
+                throw new IllegalArgumentException(exception);
+            }
+        } else {
+            setFont(Font.decode(font));
+        }
+    }
+    
+    /**
+     * Gets the currently foreground color, or <tt>null</tt> if no color is
+     * foreground.
+     */
+    public Color getForegroundColor() {
+        return foregroundColor;
+    }
+
+    /**
+     * Sets the currently foreground color.
+     *
+     * @param foregroundColor
+     * The foreground color, or <tt>null</tt> to specify no selection
+     */
+    public void setForegroundColor(Color foregroundColor) {
+        Color previousForegroundColor = this.foregroundColor;
+
+        if (foregroundColor != previousForegroundColor) {
+            this.foregroundColor = foregroundColor;
+            elementListeners.foregroundColorChanged(this, previousForegroundColor);
+        }
+    }
+
+    /**
+     * Sets the currently foreground color.
+     *
+     * @param foregroundColor
+     * The foreground color
+     */
+    public void setForegroundColor(String foregroundColor) {
+        if (foregroundColor == null) {
+            throw new IllegalArgumentException("foregroundColor is null.");
+        }
+
+        setForegroundColor(GraphicsUtilities.decodeColor(foregroundColor));
+    }
+    
+    /**
+     * Gets the currently background color, or <tt>null</tt> if no color is
+     * background.
+     */
+    public Color getBackgroundColor() {
+        return backgroundColor;
+    }
+
+    /**
+     * Sets the currently background color.
+     *
+     * @param backgroundColor
+     * The background color, or <tt>null</tt> to specify no selection
+     */
+    public void setBackgroundColor(Color backgroundColor) {
+        Color previousBackgroundColor = this.backgroundColor;
+
+        if (backgroundColor != previousBackgroundColor) {
+            this.backgroundColor = backgroundColor;
+            elementListeners.backgroundColorChanged(this, previousBackgroundColor);
+        }
+    }
+
+    /**
+     * Sets the currently background color.
+     *
+     * @param backgroundColor
+     * The background color
+     */
+    public void setBackgroundColor(String backgroundColor) {
+        if (backgroundColor == null) {
+            throw new IllegalArgumentException("backgroundColor is null.");
+        }
+
+        setBackgroundColor(GraphicsUtilities.decodeColor(backgroundColor));
+    }
+    
     public ListenerList<ElementListener> getElementListeners() {
         return elementListeners;
     }
+    
 }

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/text/ElementListener.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/text/ElementListener.java?rev=952639&r1=952638&r2=952639&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/text/ElementListener.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/text/ElementListener.java Tue Jun  8 12:41:17 2010
@@ -16,12 +16,32 @@
  */
 package org.apache.pivot.wtk.text;
 
+import java.awt.Color;
+import java.awt.Font;
+
 import org.apache.pivot.collections.Sequence;
 
 /**
  * Element listener interface.
  */
 public interface ElementListener {
+    public class Adapter implements ElementListener {
+        @Override
+        public void nodeInserted(Element element, int index) {
+        }
+        @Override
+        public void nodesRemoved(Element element, int index, Sequence<Node> nodes) {
+        }
+        @Override
+        public void fontChanged(Element element, Font previousFont) {
+        }
+        @Override
+        public void backgroundColorChanged(Element element, Color previousBackgroundColor) {
+        }
+        @Override
+        public void foregroundColorChanged(Element element, Color previousForegroundColor) {
+        }
+    }
     /**
      * Called when a node has been inserted into an element.
      *
@@ -38,4 +58,30 @@ public interface ElementListener {
      * @param nodes
      */
     public void nodesRemoved(Element element, int index, Sequence<Node> nodes);
+    
+    /**
+     * Called when the font has changed.
+     *
+     * @param element
+     * @param previousFont
+     */
+    public void fontChanged(Element element, java.awt.Font previousFont);
+    
+    
+    /**
+     * Called when the background color has changed.
+     *
+     * @param element
+     * @param previousBackgroundColor
+     */
+    public void backgroundColorChanged(Element element, Color previousBackgroundColor);
+    
+    /**
+     * Called when the foreground color has changed.
+     *
+     * @param element
+     * @param previousForegroundColor
+     */
+    public void foregroundColorChanged(Element element, Color previousForegroundColor);
+    
 }