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/07/19 16:31:51 UTC

svn commit: r965501 - in /pivot/trunk: demos/src/org/apache/pivot/demos/richtexteditor/ wtk/src/org/apache/pivot/wtk/skin/ wtk/src/org/apache/pivot/wtk/text/

Author: noelgrandin
Date: Mon Jul 19 14:31:51 2010
New Revision: 965501

URL: http://svn.apache.org/viewvc?rev=965501&view=rev
Log:
add horizontal alignment style to TextArea

Added:
    pivot/trunk/wtk/src/org/apache/pivot/wtk/text/BlockListener.java
Modified:
    pivot/trunk/demos/src/org/apache/pivot/demos/richtexteditor/RichTextEditorDemo.java
    pivot/trunk/demos/src/org/apache/pivot/demos/richtexteditor/richtexteditor.bxml
    pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkinParagraphView.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Block.java

Modified: pivot/trunk/demos/src/org/apache/pivot/demos/richtexteditor/RichTextEditorDemo.java
URL: http://svn.apache.org/viewvc/pivot/trunk/demos/src/org/apache/pivot/demos/richtexteditor/RichTextEditorDemo.java?rev=965501&r1=965500&r2=965501&view=diff
==============================================================================
--- pivot/trunk/demos/src/org/apache/pivot/demos/richtexteditor/RichTextEditorDemo.java (original)
+++ pivot/trunk/demos/src/org/apache/pivot/demos/richtexteditor/RichTextEditorDemo.java Mon Jul 19 14:31:51 2010
@@ -41,6 +41,7 @@ import org.apache.pivot.wtk.ColorChooser
 import org.apache.pivot.wtk.DesktopApplicationContext;
 import org.apache.pivot.wtk.Display;
 import org.apache.pivot.wtk.FileBrowserSheet;
+import org.apache.pivot.wtk.HorizontalAlignment;
 import org.apache.pivot.wtk.ListButton;
 import org.apache.pivot.wtk.ListButtonSelectionListener;
 import org.apache.pivot.wtk.ListView;
@@ -55,6 +56,7 @@ import org.apache.pivot.wtk.content.Nume
 import org.apache.pivot.wtk.text.Document;
 import org.apache.pivot.wtk.text.Element;
 import org.apache.pivot.wtk.text.Node;
+import org.apache.pivot.wtk.text.Paragraph;
 import org.apache.pivot.wtk.text.TextNode;
 
 /**
@@ -86,6 +88,12 @@ public class RichTextEditorDemo implemen
     private ListButton fontSizeListButton = null;
     @BXML
     private Checkbox wrapTextCheckbox = null;
+    @BXML
+    private PushButton alignLeftButton= null;
+    @BXML
+    private PushButton alignCentreButton= null;
+    @BXML
+    private PushButton alignRightButton= null;
 
     private File loadedFile = null;
 
@@ -330,6 +338,28 @@ public class RichTextEditorDemo implemen
                 textarea.getStyles().put("wrapText", wrapTextCheckbox.isSelected());
             }
         });
+        
+        alignLeftButton.getButtonPressListeners().add(new ButtonPressListener() {
+            @Override
+            public void buttonPressed(Button button) {
+                applyAlignmentStyle(HorizontalAlignment.LEFT);
+            }
+        });
+        
+        alignCentreButton.getButtonPressListeners().add(new ButtonPressListener() {
+            @Override
+            public void buttonPressed(Button button) {
+                applyAlignmentStyle(HorizontalAlignment.CENTER);
+            }
+        });
+        
+        alignRightButton.getButtonPressListeners().add(new ButtonPressListener() {
+            @Override
+            public void buttonPressed(Button button) {
+                applyAlignmentStyle(HorizontalAlignment.RIGHT);
+            }
+        });
+        
         window.open(display);
         textarea.requestFocus();
     }
@@ -338,6 +368,17 @@ public class RichTextEditorDemo implemen
         void apply(org.apache.pivot.wtk.text.Span span);
     }
 
+    private void applyAlignmentStyle(HorizontalAlignment horizontalAlignment) {
+        Node node = textarea.getDocument().getDescendantAt(textarea.getSelectionStart());
+        while (node != null && !(node instanceof Paragraph)) {
+            node = node.getParent();
+        }
+        if (node != null) {
+            Paragraph paragraph = (Paragraph) node;
+            paragraph.setHorizontalAlignment(horizontalAlignment);
+        }
+    }
+    
     /** debugging tools */
     @SuppressWarnings("unused")
     private void dumpDocument() {

Modified: pivot/trunk/demos/src/org/apache/pivot/demos/richtexteditor/richtexteditor.bxml
URL: http://svn.apache.org/viewvc/pivot/trunk/demos/src/org/apache/pivot/demos/richtexteditor/richtexteditor.bxml?rev=965501&r1=965500&r2=965501&view=diff
==============================================================================
--- pivot/trunk/demos/src/org/apache/pivot/demos/richtexteditor/richtexteditor.bxml (original)
+++ pivot/trunk/demos/src/org/apache/pivot/demos/richtexteditor/richtexteditor.bxml Mon Jul 19 14:31:51 2010
@@ -45,6 +45,12 @@ limitations under the License.
                     <ListButton bxml:id="fontFamilyListButton"/>
                     <ListButton bxml:id="fontSizeListButton"/>
                     <Checkbox bxml:id="wrapTextCheckbox" buttonData="Wrap Text" selected="false"/>
+	                <FlowPane>
+                    	<Label text="Horizontal Align"/>
+		                <PushButton bxml:id="alignLeftButton" buttonData="Left"/>
+		                <PushButton bxml:id="alignCentreButton" buttonData="Centre"/>
+		                <PushButton bxml:id="alignRightButton" buttonData="Right"/>
+    	            </FlowPane>
                 </FlowPane>
             </TablePane.Row>
             <TablePane.Row height="-1">

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkinParagraphView.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkinParagraphView.java?rev=965501&r1=965500&r2=965501&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkinParagraphView.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/TextAreaSkinParagraphView.java Mon Jul 19 14:31:51 2010
@@ -22,11 +22,14 @@ import java.awt.font.LineMetrics;
 import org.apache.pivot.collections.ArrayList;
 import org.apache.pivot.wtk.Bounds;
 import org.apache.pivot.wtk.FocusTraversalDirection;
+import org.apache.pivot.wtk.HorizontalAlignment;
 import org.apache.pivot.wtk.Platform;
+import org.apache.pivot.wtk.text.Block;
+import org.apache.pivot.wtk.text.BlockListener;
 import org.apache.pivot.wtk.text.Node;
 import org.apache.pivot.wtk.text.Paragraph;
 
-class TextAreaSkinParagraphView extends TextAreaSkinElementView {
+class TextAreaSkinParagraphView extends TextAreaSkinElementView implements BlockListener {
 
     private static final int PARAGRAPH_TERMINATOR_WIDTH = 4;
 
@@ -49,6 +52,14 @@ class TextAreaSkinParagraphView extends 
     }
 
     @Override
+    protected void attach() {
+        super.attach();
+        
+        Block block = (Block)getNode();
+        block.getBlockListeners().add(this);
+    }
+    
+    @Override
     public void invalidate() {
         super.invalidate();
         terminatorBounds = null;
@@ -126,9 +137,14 @@ class TextAreaSkinParagraphView extends 
                     row.height = Math.max(row.height, nodeView.getHeight());
                 }
 
-                // TODO Align horizontally when Elements support a horizontal
-                // alignment property
-                x = 0;
+                if (paragraph.getHorizontalAlignment() == HorizontalAlignment.LEFT) {
+                    x = 0;
+                } else if (paragraph.getHorizontalAlignment() == HorizontalAlignment.CENTER) {
+                    x = (width - row.width) / 2;
+                } else {
+                    // right alignment
+                    x = width - row.width;
+                }
                 for (TextAreaSkinNodeView nodeView : row.nodeViews) {
                     // TODO Align to baseline
                     int y = row.height - nodeView.getHeight();
@@ -338,4 +354,9 @@ class TextAreaSkinParagraphView extends 
 
         return bounds;
     }
+    
+    @Override
+    public void horizontalAlignmentChanged(Block block, HorizontalAlignment previousHorizontalAlignment) {
+        invalidate();
+    }
 }
\ No newline at end of file

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Block.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Block.java?rev=965501&r1=965500&r2=965501&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Block.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/text/Block.java Mon Jul 19 14:31:51 2010
@@ -16,12 +16,30 @@
  */
 package org.apache.pivot.wtk.text;
 
+import org.apache.pivot.util.ListenerList;
+import org.apache.pivot.wtk.HorizontalAlignment;
+
 /**
  * Abstract base class for block elements.
  * <p>
- * TODO Add horizontal alignment, margin, and line spacing properties.
+ * TODO Add margin, and line spacing properties.
  */
 public abstract class Block extends Element {
+
+    private static class BlockListenerList extends ListenerList<BlockListener> implements
+        BlockListener {
+        @Override
+        public void horizontalAlignmentChanged(Block block, HorizontalAlignment previousHorizontalAlignment) {
+            for (BlockListener listener : this) {
+                listener.horizontalAlignmentChanged(block, previousHorizontalAlignment);
+            }
+        }
+    }
+
+    private HorizontalAlignment horizontalAlignment = HorizontalAlignment.LEFT;
+
+    private BlockListenerList blockListeners = new BlockListenerList();
+
     public Block() {
         super();
     }
@@ -29,4 +47,24 @@ public abstract class Block extends Elem
     public Block(Block blockElement, boolean recursive) {
         super(blockElement, recursive);
     }
+
+    public HorizontalAlignment getHorizontalAlignment() {
+        return horizontalAlignment;
+    }
+
+    public void setHorizontalAlignment(HorizontalAlignment horizontalAlignment) {
+        if (horizontalAlignment == null) {
+            throw new IllegalArgumentException("horizontalAlignment is null.");
+        }
+        
+        HorizontalAlignment previousHorizontalAlignment = this.horizontalAlignment;
+        if (previousHorizontalAlignment != horizontalAlignment) {
+            this.horizontalAlignment = horizontalAlignment;
+            blockListeners.horizontalAlignmentChanged(this, previousHorizontalAlignment);
+        }
+    }
+
+    public ListenerList<BlockListener> getBlockListeners() {
+        return blockListeners;
+    }
 }

Added: pivot/trunk/wtk/src/org/apache/pivot/wtk/text/BlockListener.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/text/BlockListener.java?rev=965501&view=auto
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/text/BlockListener.java (added)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/text/BlockListener.java Mon Jul 19 14:31:51 2010
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pivot.wtk.text;
+
+import org.apache.pivot.wtk.HorizontalAlignment;
+
+/**
+ * Block listener interface.
+ */
+public interface BlockListener {
+
+    public class Adapter implements BlockListener {
+        @Override
+        public void horizontalAlignmentChanged(Block block,
+            HorizontalAlignment previousHorizontalAlignment) {
+        }
+    }
+
+    /**
+     * Called when the horizontal alignment has changed.
+     * 
+     * @param block
+     * @param previousHorizontalAlignment
+     */
+    public void horizontalAlignmentChanged(Block block,
+        HorizontalAlignment previousHorizontalAlignment);
+}