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 2011/11/14 12:38:27 UTC

svn commit: r1201670 - in /pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer: BXmlExplorerDocument.java BXmlExplorerWindow.java CreateHighlightedXml.bxml CreateHighlightedXml.java builder-test1.bxml bxml_explorer_document.bxml

Author: noelgrandin
Date: Mon Nov 14 11:38:26 2011
New Revision: 1201670

URL: http://svn.apache.org/viewvc?rev=1201670&view=rev
Log:
syntax highlight the loaded BXML file

Added:
    pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/CreateHighlightedXml.bxml
    pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/CreateHighlightedXml.java
    pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/builder-test1.bxml
Modified:
    pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/BXmlExplorerDocument.java
    pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/BXmlExplorerWindow.java
    pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/bxml_explorer_document.bxml

Modified: pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/BXmlExplorerDocument.java
URL: http://svn.apache.org/viewvc/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/BXmlExplorerDocument.java?rev=1201670&r1=1201669&r2=1201670&view=diff
==============================================================================
--- pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/BXmlExplorerDocument.java (original)
+++ pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/BXmlExplorerDocument.java Mon Nov 14 11:38:26 2011
@@ -19,11 +19,12 @@ package org.apache.pivot.tutorials.bxmle
 import java.awt.Color;
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileReader;
 import java.io.IOException;
 import java.net.URL;
 import java.util.HashMap;
 
+import javax.xml.parsers.ParserConfigurationException;
+
 import org.apache.pivot.beans.BXML;
 import org.apache.pivot.beans.BXMLSerializer;
 import org.apache.pivot.beans.BeanAdapter;
@@ -49,7 +50,7 @@ import org.apache.pivot.wtk.Mouse.Button
 import org.apache.pivot.wtk.PushButton;
 import org.apache.pivot.wtk.RadioButton;
 import org.apache.pivot.wtk.TablePane;
-import org.apache.pivot.wtk.TextArea;
+import org.apache.pivot.wtk.TextPane;
 import org.apache.pivot.wtk.TreeView;
 import org.apache.pivot.wtk.TreeView.SelectMode;
 import org.apache.pivot.wtk.TreeViewSelectionListener;
@@ -59,6 +60,7 @@ import org.apache.pivot.wtk.content.Tree
 import org.apache.pivot.wtk.content.TreeViewNodeRenderer;
 import org.apache.pivot.wtk.effects.Decorator;
 import org.apache.pivot.wtk.effects.ShadeDecorator;
+import org.xml.sax.SAXException;
 
 public class BXmlExplorerDocument extends CardPane implements Bindable {
     @BXML
@@ -66,7 +68,7 @@ public class BXmlExplorerDocument extend
     @BXML
     private CardPane playgroundCardPane;
     @BXML
-    private TextArea bxmlSourceTextArea;
+    private TextPane bxmlSourceTextPane;
     @BXML
     private ComponentPropertyInspector componentPropertyInspector;
     @BXML
@@ -168,6 +170,12 @@ public class BXmlExplorerDocument extend
                 } catch (SerializationException exception) {
                     exception.printStackTrace();
                     BXmlExplorer.displayLoadException(exception, BXmlExplorerDocument.this.getWindow());
+                } catch (ParserConfigurationException exception) {
+                    exception.printStackTrace();
+                    BXmlExplorer.displayLoadException(exception, BXmlExplorerDocument.this.getWindow());
+                } catch (SAXException exception) {
+                    exception.printStackTrace();
+                    BXmlExplorer.displayLoadException(exception, BXmlExplorerDocument.this.getWindow());
                 }
             }
         });
@@ -190,7 +198,7 @@ public class BXmlExplorerDocument extend
         return file;
     }
 
-    public void load(File f) throws IOException, SerializationException {
+    public void load(File f) throws IOException, SerializationException, ParserConfigurationException, SAXException {
         BXMLSerializer serializer = new BXMLSerializer();
         serializer.setLocation(f.toURI().toURL());
         final FileInputStream in = new FileInputStream(f);
@@ -222,9 +230,10 @@ public class BXmlExplorerDocument extend
         } finally {
             in.close();
         }
-        final FileReader in2 = new FileReader(f);
+        final FileInputStream in2 = new FileInputStream(f);
         try {
-            bxmlSourceTextArea.setText(in2);
+            CreateHighlightedXml xml = new CreateHighlightedXml();
+            bxmlSourceTextPane.setDocument(xml.prettyPrint(in2));
         } finally {
             in2.close();
         }

Modified: pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/BXmlExplorerWindow.java
URL: http://svn.apache.org/viewvc/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/BXmlExplorerWindow.java?rev=1201670&r1=1201669&r2=1201670&view=diff
==============================================================================
--- pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/BXmlExplorerWindow.java (original)
+++ pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/BXmlExplorerWindow.java Mon Nov 14 11:38:26 2011
@@ -20,6 +20,8 @@ import java.io.File;
 import java.io.IOException;
 import java.net.URL;
 
+import javax.xml.parsers.ParserConfigurationException;
+
 import org.apache.pivot.beans.BXML;
 import org.apache.pivot.beans.BXMLSerializer;
 import org.apache.pivot.beans.Bindable;
@@ -45,6 +47,7 @@ import org.apache.pivot.wtk.TextInput;
 import org.apache.pivot.wtk.TextInputContentListener;
 import org.apache.pivot.wtk.TextInputSelectionListener;
 import org.apache.pivot.wtk.Window;
+import org.xml.sax.SAXException;
 
 public class BXmlExplorerWindow extends Window implements Bindable {
     @BXML
@@ -178,6 +181,14 @@ public class BXmlExplorerWindow extends 
                             exception.printStackTrace();
                             BXmlExplorer.displayLoadException(exception, BXmlExplorerWindow.this);
                             return;
+                        } catch (ParserConfigurationException exception) {
+                            exception.printStackTrace();
+                            BXmlExplorer.displayLoadException(exception, BXmlExplorerWindow.this);
+                            return;
+                        } catch (SAXException exception) {
+                            exception.printStackTrace();
+                            BXmlExplorer.displayLoadException(exception, BXmlExplorerWindow.this);
+                            return;
                         }
 
                         tabPane.getTabs().add(tab);

Added: pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/CreateHighlightedXml.bxml
URL: http://svn.apache.org/viewvc/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/CreateHighlightedXml.bxml?rev=1201670&view=auto
==============================================================================
--- pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/CreateHighlightedXml.bxml (added)
+++ pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/CreateHighlightedXml.bxml Mon Nov 14 11:38:26 2011
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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.
+-->
+
+<!--  Used to test the XML highlighting -->
+<Window title="Text Areas" maximized="true"
+    xmlns:bxml="http://pivot.apache.org/bxml"
+    xmlns="org.apache.pivot.wtk">
+    <Border styles="{padding:6}">
+        <TablePane>
+            <columns>
+                <TablePane.Column width="1*"/>
+            </columns>
+            <TablePane.Row height="1*">
+                <Border styles="{color:10}">
+                    <ScrollPane horizontalScrollBarPolicy="fill"
+                        verticalScrollBarPolicy="fill_to_capacity"
+                        preferredHeight="240">
+                        <TextPane bxml:id="textPane" styles="{margin:10, wrapText:true}">
+                        </TextPane>
+                    </ScrollPane>
+                </Border>
+            </TablePane.Row>
+        </TablePane>
+    </Border>
+</Window>

Added: pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/CreateHighlightedXml.java
URL: http://svn.apache.org/viewvc/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/CreateHighlightedXml.java?rev=1201670&view=auto
==============================================================================
--- pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/CreateHighlightedXml.java (added)
+++ pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/CreateHighlightedXml.java Mon Nov 14 11:38:26 2011
@@ -0,0 +1,160 @@
+package org.apache.pivot.tutorials.bxmlexplorer;
+
+import java.awt.Color;
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.apache.pivot.beans.BXMLSerializer;
+import org.apache.pivot.collections.Map;
+import org.apache.pivot.wtk.Application;
+import org.apache.pivot.wtk.DesktopApplicationContext;
+import org.apache.pivot.wtk.Display;
+import org.apache.pivot.wtk.TextPane;
+import org.apache.pivot.wtk.Window;
+import org.apache.pivot.wtk.text.Document;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+/**
+ * Create a nicely syntax highlighted version of the BXML document.
+ */
+public class CreateHighlightedXml {
+
+    private static final Color ELEMENT_COLOR = new Color(63, 127, 127);
+    private static final Color ATTR_NAME_COLOR = new Color(127, 0, 127);
+    private static final Color ATTR_VALUE_COLOR = new Color(42, 0, 255);
+    private static final Color ATTR_EQUALS_COLOR = new Color(0, 0, 0);
+
+    private final org.apache.pivot.wtk.text.Document textPaneDocument = new org.apache.pivot.wtk.text.Document();
+    private org.apache.pivot.wtk.text.Paragraph currentParagraph = new org.apache.pivot.wtk.text.Paragraph();
+
+    public org.apache.pivot.wtk.text.Document prettyPrint(InputStream reader)
+        throws ParserConfigurationException, SAXException, IOException {
+        final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
+        final DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
+        org.w3c.dom.Document doc = documentBuilder.parse(reader);
+
+        return prettyPrint(doc);
+    }
+
+    private org.apache.pivot.wtk.text.Document prettyPrint(final org.w3c.dom.Document domDocument) {
+        org.w3c.dom.Element domElement = domDocument.getDocumentElement();
+        prettyPrint(domElement, 0);
+        textPaneDocument.add(currentParagraph);
+        return textPaneDocument;
+    }
+
+    private void prettyPrint(org.w3c.dom.Element domElement, int indent) {
+        newLine();
+        indent++;
+        add(createIndent(indent));
+
+        add(ELEMENT_COLOR, "<" + domElement.getNodeName());
+
+        final NamedNodeMap attributes = domElement.getAttributes();
+        if (attributes != null) {
+            for (int i = 0; i < attributes.getLength(); i++) {
+                org.w3c.dom.Attr attr = (org.w3c.dom.Attr) attributes.item(i);
+                String attributeName = "";
+                String prefix2 = attr.getPrefix();
+                if (prefix2 != null && prefix2.length() > 0) {
+                    attributeName += prefix2 + ":";
+                }
+                attributeName += attr.getNodeName();
+                add(ATTR_NAME_COLOR, " " + attributeName);
+                add(ATTR_EQUALS_COLOR, "=");
+                add(ATTR_VALUE_COLOR, "\"" + attr.getValue() + "\"");
+            }
+        }
+
+        if (!domElement.hasChildNodes()) {
+            add(ELEMENT_COLOR, "/>");
+        } else {
+            add(ELEMENT_COLOR, ">");
+            final NodeList childNodes = domElement.getChildNodes();
+            for (int i = 0; i < childNodes.getLength(); i++) {
+                org.w3c.dom.Node node = childNodes.item(i);
+                if (node instanceof org.w3c.dom.Element) {
+                    prettyPrint((org.w3c.dom.Element) node, indent);
+                }
+            }
+            newLine();
+            add(createIndent(indent));
+            add(ELEMENT_COLOR, "</" + domElement.getNodeName() + ">");
+        }
+        indent--;
+    }
+
+    private void newLine() {
+        textPaneDocument.add(currentParagraph);
+        currentParagraph = new org.apache.pivot.wtk.text.Paragraph();
+    }
+
+    private void add(Color color, String buf) {
+        final org.apache.pivot.wtk.text.TextSpan textSpan = new org.apache.pivot.wtk.text.TextSpan();
+        textSpan.setForegroundColor(color);
+        textSpan.add(buf);
+        currentParagraph.add(textSpan);
+    }
+
+    private void add(String buf) {
+        final org.apache.pivot.wtk.text.TextSpan textSpan = new org.apache.pivot.wtk.text.TextSpan();
+        textSpan.add(buf);
+        currentParagraph.add(textSpan);
+    }
+
+    private static String createIndent(int indent) {
+        StringBuilder buf = new StringBuilder();
+        for (int i = 0; i < indent; i++) {
+            buf.append("  ");
+        }
+        return buf.toString();
+    }
+
+    public static void main(String[] args) throws Exception {
+        DesktopApplicationContext.main(TestApplication.class, args);
+    }
+
+    public static final class TestApplication implements Application {
+        private Window window = null;
+
+        @Override
+        public void startup(Display display, Map<String, String> properties) throws Exception {
+
+            CreateHighlightedXml xml = new CreateHighlightedXml();
+            Document doc = xml.prettyPrint(CreateHighlightedXml.class.getResourceAsStream("builder-test1.bxml"));
+
+            BXMLSerializer bxmlSerializer = new BXMLSerializer();
+            window = (Window) bxmlSerializer.readObject(BXmlExplorer.class,
+                "CreateHighlightedXml.bxml", true);
+
+            TextPane textPane = (TextPane) bxmlSerializer.getNamespace().get("textPane");
+            textPane.setDocument(doc);
+
+            window.open(display);
+        }
+
+        @Override
+        public boolean shutdown(boolean optional) {
+            if (window != null) {
+                window.close();
+            }
+
+            return false;
+        }
+
+        @Override
+        public void resume() throws Exception {
+        }
+
+        @Override
+        public void suspend() throws Exception {
+        }
+    }
+
+}

Added: pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/builder-test1.bxml
URL: http://svn.apache.org/viewvc/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/builder-test1.bxml?rev=1201670&view=auto
==============================================================================
--- pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/builder-test1.bxml (added)
+++ pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/builder-test1.bxml Mon Nov 14 11:38:26 2011
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<TablePane xmlns:bxml="http://pivot.apache.org/bxml" xmlns="org.apache.pivot.wtk" bxml:id="tablepane1"
+  styles="{verticalSpacing:1, showHorizontalGridLines:true, horizontalSpacing:1, showVerticalGridLines:true, horizontalGridColor:'#ff0000', verticalGridColor:'#ff0000', backgroundColor:'#ffffff'}">
+  <columns>
+    <TablePane.Column width="1*" />
+    <TablePane.Column width="1*" />
+  </columns>
+  <TablePane.Row height="60">
+    <TablePane bxml:id="tablepane2"
+      styles="{verticalSpacing:1, showHorizontalGridLines:true, horizontalSpacing:1, showVerticalGridLines:true, horizontalGridColor:'#00ff00', verticalGridColor:'#00ff00'}">
+      <columns>
+        <TablePane.Column width="60" />
+        <TablePane.Column width="1*" />
+      </columns>
+      <TablePane.Row>
+        <ImageView image="@test1.jpg" styles="{fill:true}" />
+      </TablePane.Row>
+    </TablePane>
+    <ImageView image="@test1.jpg" styles="{fill:true}" />
+  </TablePane.Row>
+  <TablePane.Row height="60">
+    <TablePane
+      styles="{verticalSpacing:1, showHorizontalGridLines:true, horizontalSpacing:1, showVerticalGridLines:true, horizontalGridColor:'#0000ff', verticalGridColor:'#0000ff'}">
+      <columns>
+        <TablePane.Column width="60" />
+        <TablePane.Column width="1*" />
+      </columns>
+      <TablePane.Row>
+        <ImageView image="@test2.jpg" styles="{fill:true}" />
+      </TablePane.Row>
+    </TablePane>
+    <ImageView image="@test2.jpg" styles="{fill:true}" />
+  </TablePane.Row>
+  <TablePane.Row>
+    <Label TablePane.columnSpan="2" text="The bottom" />
+  </TablePane.Row>
+</TablePane>

Modified: pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/bxml_explorer_document.bxml
URL: http://svn.apache.org/viewvc/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/bxml_explorer_document.bxml?rev=1201670&r1=1201669&r2=1201670&view=diff
==============================================================================
--- pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/bxml_explorer_document.bxml (original)
+++ pivot/trunk/tutorials/src/org/apache/pivot/tutorials/bxmlexplorer/bxml_explorer_document.bxml Mon Nov 14 11:38:26 2011
@@ -43,8 +43,8 @@
             </Border>
             <Border TabPane.tabData="BXML Source">
               <ScrollPane horizontalScrollBarPolicy="fill_to_capacity">
-                <TextArea bxml:id="bxmlSourceTextArea" editable="false">
-                </TextArea>
+                <TextPane bxml:id="bxmlSourceTextPane" editable="false" styles="{wrapText:true}">
+                </TextPane>
               </ScrollPane>
             </Border>
             <corner>