[brailleblaster] [brailleblaster.newdesign] push by brandon....@xxxxxxxxx - Improved perfromance when loading a file/Added a normalizing class on 2013-07-10 12:40 GMT

  • From: brailleblaster@xxxxxxxxxxxxxx
  • To: brailleblaster@xxxxxxxxxxxxx
  • Date: Wed, 10 Jul 2013 12:41:16 +0000

Revision: 285a3db353ba
Branch:   WithBrailleFormatting
Author:   Brandon Roller <brandon.r.roller@xxxxxxxxx>
Date:     Wed Jul 10 05:34:57 2013
Log:      Improved perfromance when loading a file/Added a normalizing class
http://code.google.com/p/brailleblaster/source/detail?r=285a3db353ba&repo=newdesign

Added:
 /src/main/org/brailleblaster/wordprocessor/Normalizer.java
Modified:
 /src/main/org/brailleblaster/abstractClasses/AbstractView.java
 /src/main/org/brailleblaster/printers/PrintPreview.java
 /src/main/org/brailleblaster/views/BrailleView.java
 /src/main/org/brailleblaster/views/TextView.java
 /src/main/org/brailleblaster/views/TreeView.java
 /src/main/org/brailleblaster/wordprocessor/BBDocument.java
 /src/main/org/brailleblaster/wordprocessor/BBSemanticsTable.java
 /src/main/org/brailleblaster/wordprocessor/DocumentManager.java

=======================================
--- /dev/null
+++ /src/main/org/brailleblaster/wordprocessor/Normalizer.java Wed Jul 10 05:34:57 2013
@@ -0,0 +1,103 @@
+package org.brailleblaster.wordprocessor;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
+import org.w3c.dom.Document;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Node;
+import org.w3c.dom.Element;
+import org.w3c.dom.Text;
+import org.xml.sax.SAXException;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.ParserConfigurationException;
+
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+public class Normalizer {
+       File f;
+       Document doc;
+
+       public Normalizer(String path){
+               this.f = new File(path);
+               DocumentBuilderFactory dbFactory = 
DocumentBuilderFactory.newInstance();
+               DocumentBuilder dBuilder;
+               try {
+                       dBuilder = dbFactory.newDocumentBuilder();
+                       this.doc = dBuilder.parse(this.f);
+               }
+               catch (ParserConfigurationException e) {
+                       e.printStackTrace();
+               }
+               catch (SAXException e) {
+                       e.printStackTrace();
+               }
+               catch (IOException e) {
+                       e.printStackTrace();
+               }
+       }
+
+       public void createNewNormalizedFile(String path){
+               normalize();
+               write(this.doc, path);
+       }
+
+       private void normalize(){
+               doc.normalize();
+               removeEscapeChars(doc.getDocumentElement());
+       }
+
+       private void removeEscapeChars(Element e){
+               NodeList list = e.getChildNodes();
+
+               for(int i = 0; i < list.getLength(); i++){
+                       if(list.item(i) instanceof Element){
+                               removeEscapeChars((Element)list.item(i));
+                       }
+                       else if(list.item(i) instanceof Text){
+                               Text t = (Text)list.item(i);
+                               String text = t.getTextContent();
+                               text = text.replace("\n", "");
+                               t.setTextContent(text);
+                       }
+               }
+       }
+       private boolean onlyWhitespace(String text){
+               for(int j = 0; j < text.length(); j++){
+                       if(!Character.isWhitespace(text.charAt(j))){
+                               return false;
+                       }
+               }
+               return true;
+       }
+
+       public void write(Document document, String path) {
+               try {
+ TransformerFactory transformerFactory = TransformerFactory.newInstance();
+                       Transformer transformer;
+                       transformer = transformerFactory.newTransformer();
+                       DOMSource source = new DOMSource(doc);
+                       StreamResult result = new StreamResult(new File(path));
+                       transformer.transform(source, result);
+               } catch (TransformerConfigurationException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
+               catch (TransformerException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
+
+
+    }
+}
=======================================
--- /src/main/org/brailleblaster/abstractClasses/AbstractView.java Tue Jul 2 09:14:48 2013 +++ /src/main/org/brailleblaster/abstractClasses/AbstractView.java Wed Jul 10 05:34:57 2013
@@ -49,6 +49,7 @@
        public boolean hasFocus = false;
        public boolean hasChanged = false;
        protected int total;
+       protected int charWidth;
        protected int spaceBeforeText, spaceAfterText;
        public int positionFromStart, cursorOffset, words;
        public static int currentLine;
@@ -60,16 +61,10 @@
        }

public AbstractView(Group group, int left, int right, int top, int bottom) { - view = new StyledText(group, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.WRAP);
-
-               FormData location = new FormData();
-               location.left = new FormAttachment(left);
-               location.right = new FormAttachment(right);
-               location.top = new FormAttachment(top);
-               location.bottom = new FormAttachment(bottom);
-               view.setLayoutData(location);
-
+               view = new StyledText(group, SWT.BORDER | SWT.H_SCROLL | 
SWT.V_SCROLL);
+               setLayout(left, right, top, bottom);
                view.addModifyListener(viewMod);
+               this.charWidth = getFontWidth();
        }

        // Better use a ModifyListener to set the change flag.
@@ -79,6 +74,15 @@
                }
        };

+       protected void setLayout(int left, int right, int top, int bottom){
+               FormData location = new FormData();
+               location.left = new FormAttachment(left);
+               location.right = new FormAttachment(right);
+               location.top = new FormAttachment(top);
+               location.bottom = new FormAttachment(bottom);
+               view.setLayoutData(location);
+       }
+
        protected void sendIncrementCurrent(DocumentManager dm){
                Message message = new Message(BBEvent.INCREMENT);
                dm.dispatch(message);
@@ -229,8 +233,23 @@
                        this.view.setTopIndex(line);
                        topIndex = line;
                setListenerLock(false);
+       }
+
+       public void resetCursor(int pos){
+               setListenerLock(true);
+               view.setFocus();
+               view.setCaretOffset(pos);
+               setListenerLock(false);
+       }
+
+ protected void recreateView(Group group, int left, int right, int top, int bottom){
+               view.dispose();
+               view = new StyledText(group, SWT.BORDER | SWT.H_SCROLL | 
SWT.V_SCROLL);
+               setLayout(left, right, top, bottom);
+               view.getParent().layout();
        }

        protected abstract void setViewData(Message message);
-       public abstract void resetView();
+       public abstract void resetView(Group group);
+       public abstract void initializeListeners(final DocumentManager dm);
 }
=======================================
--- /src/main/org/brailleblaster/printers/PrintPreview.java Fri Jun 21 07:40:34 2013 +++ /src/main/org/brailleblaster/printers/PrintPreview.java Wed Jul 10 05:34:57 2013
@@ -8,6 +8,7 @@
 import org.brailleblaster.abstractClasses.AbstractView;
 import org.brailleblaster.util.Notify;
 import org.brailleblaster.wordprocessor.BBDocument;
+import org.brailleblaster.wordprocessor.DocumentManager;
 import org.brailleblaster.wordprocessor.FontManager;
 import org.brailleblaster.wordprocessor.Message;
 import org.eclipse.swt.SWT;
@@ -23,6 +24,7 @@
 import org.eclipse.swt.widgets.Listener;
 import org.eclipse.swt.widgets.Shell;

+
 public class PrintPreview {
        Shell shell;
        BBDocument doc;
@@ -75,10 +77,16 @@
                        // TODO Auto-generated method stub
                }

-               @Override
-               public void resetView() {
+
+               public void resetView(Group group) {
                        // TODO Auto-generated method stub
                }
+
+               @Override
+               public void initializeListeners(DocumentManager dm) {
+                       // TODO Auto-generated method stub
+
+               }
        }

        public PrintPreview(Display display, BBDocument doc){
=======================================
--- /src/main/org/brailleblaster/views/BrailleView.java Tue Jul 2 09:14:48 2013 +++ /src/main/org/brailleblaster/views/BrailleView.java Wed Jul 10 05:34:57 2013
@@ -29,6 +29,7 @@
 package org.brailleblaster.views;

 import java.util.ArrayList;
+import java.util.Map.Entry;

 import nu.xom.Element;
 import nu.xom.Elements;
@@ -49,6 +50,7 @@
 import org.eclipse.swt.custom.CaretEvent;
 import org.eclipse.swt.custom.CaretListener;
 import org.eclipse.swt.custom.StyleRange;
+import org.eclipse.swt.custom.StyledText;
 import org.eclipse.swt.custom.VerifyKeyListener;
 import org.eclipse.swt.events.FocusEvent;
 import org.eclipse.swt.events.FocusListener;
@@ -59,15 +61,30 @@
 import org.eclipse.swt.events.TraverseEvent;
 import org.eclipse.swt.events.TraverseListener;
 import org.eclipse.swt.events.VerifyEvent;
+import org.eclipse.swt.layout.FormAttachment;
+import org.eclipse.swt.layout.FormData;
 import org.eclipse.swt.widgets.Group;

+
 public class BrailleView extends AbstractView {
+       private final static int LEFT_MARGIN = 58;
+       private final static int RIGHT_MARGIN = 100;
+       private final static int TOP_MARGIN = 0;
+       private final static int BOTTOM_MARGIN = 100;
+
        private int currentStart, currentEnd, nextStart, previousEnd;
        private BBSemanticsTable stylesTable;
        private int oldCursorPosition = -1;
private ArrayList<BrailleMapElement> pageRanges = new ArrayList<BrailleMapElement>();
        private String charAtOffset;

+       private VerifyKeyListener verifyListener;
+       private FocusListener focusListener;
+       private MouseListener mouseListener;
+       private CaretListener caretListener;
+       private TraverseListener traverseListener;
+       private SelectionListener selectionListener;
+
        public BrailleView(Group documentWindow, BBSemanticsTable table) {
                super(documentWindow, 58, 100, 0, 100);
                this.total = 0;
@@ -77,7 +94,7 @@
        }

        public void initializeListeners(final DocumentManager dm){
-               view.addVerifyKeyListener(new VerifyKeyListener(){
+               view.addVerifyKeyListener(verifyListener = new 
VerifyKeyListener(){
                        @Override
                        public void verifyKey(VerifyEvent e) {
                                oldCursorPosition = view.getCaretOffset();
@@ -85,7 +102,7 @@

                });

-               view.addFocusListener(new FocusListener(){
+               view.addFocusListener(focusListener = new FocusListener(){
                        @Override
                        public void focusGained(FocusEvent e) {
                                Message message = new 
Message(BBEvent.GET_CURRENT);
@@ -107,7 +124,7 @@
                        }
                });

-               view.addMouseListener(new MouseListener(){
+               view.addMouseListener(mouseListener = new MouseListener(){
                        @Override
                        public void mouseDoubleClick(MouseEvent e) {
                                // TODO Auto-generated method stub
@@ -126,7 +143,7 @@
                        }
                });

-               view.addCaretListener(new CaretListener(){
+               view.addCaretListener(caretListener = new CaretListener(){
                        @Override
                        public void caretMoved(CaretEvent e) {
                                if(!getLock()){
@@ -148,7 +165,7 @@
                        }
                });

-               view.addTraverseListener(new TraverseListener(){
+               view.addTraverseListener(traverseListener = new 
TraverseListener(){
                        @Override
                        public void keyTraversed(TraverseEvent e) {
if(e.stateMask == SWT.CONTROL && e.keyCode == SWT.ARROW_DOWN && nextStart != -1){
@@ -164,7 +181,7 @@
                        }
                });

-               view.getVerticalBar().addSelectionListener(new 
SelectionListener(){
+ view.getVerticalBar().addSelectionListener(selectionListener = new SelectionListener(){
                        @Override
                        public void widgetDefaultSelected(SelectionEvent arg0) {
                                // TODO Auto-generated method stub
@@ -188,6 +205,14 @@
                setListenerLock(false);
        }

+       public void removeListeners(){
+               view.removeVerifyKeyListener(verifyListener);
+               view.removeFocusListener(focusListener);
+               view.removeMouseListener(mouseListener);
+               view.removeCaretListener(caretListener);
+               view.removeTraverseListener(traverseListener);
+               
view.getVerticalBar().removeSelectionListener(selectionListener);
+       }

        private void setCurrent(DocumentManager dm){
                Message message = new Message(BBEvent.SET_CURRENT);
@@ -196,6 +221,7 @@
                message.put("offset", view.getCaretOffset());
                if(charAtOffset != null)
                        message.put("char", charAtOffset);
+
                dm.dispatch(message);
                setViewData(message);
                charAtOffset = null;
@@ -220,22 +246,26 @@
        }

        public void setBraille(Node n, TextMapElement t){
-               String text = n.getValue();
                setListenerLock(true);
String key = this.stylesTable.getKeyFromAttribute((Element)t.n.getParent());
                Styles style = this.stylesTable.makeStylesElement(key, n);
-
+               String textBefore = "";
+               String text = n.getValue();
+               int textLength = text.length();
+
                if(insertNewLine(n)){
-                       view.append("\n");
-                       this.total++;
+               //      view.append("\n");
+               //      this.total++;
+                       textBefore = "\n";
+                       this.spaceBeforeText++;
                }

-               view.append(n.getValue());
+               view.append(textBefore + text);
                handleStyle(style, n, (Element)t.n.getParent());
 //             checkFinalNewline(n);

- t.brailleList.add(new BrailleMapElement(this.spaceBeforeText + this.total, this.spaceBeforeText + this.total + n.getValue().length(), n)); - this.total += this.spaceBeforeText + n.getValue().length() + this.spaceAfterText; + t.brailleList.add(new BrailleMapElement(this.spaceBeforeText + this.total, this.spaceBeforeText + this.total + textLength, n));
+               this.total += this.spaceBeforeText + textLength + 
this.spaceAfterText;
                this.spaceBeforeText = 0;
                this.spaceAfterText = 0;
                setListenerLock(false);
@@ -267,8 +297,8 @@

        private void handleStyle(Styles style, Node n, Element parent){
                String viewText = n.getValue();
-               Element brailleParent = (Element)n.getParent();
-               int index = parent.indexOf(brailleParent);
+       //      Element brailleParent = (Element)n.getParent();
+       //      int index = parent.indexOf(brailleParent);
        /*
                if(index > 1){
if(parent.getChild(index - 2) instanceof Element && ((Element)parent.getChild(index - 2)).getLocalName().equals("br")){
@@ -279,41 +309,42 @@
                        checkForLineBreak((Element)parent.getParent(), parent);
                }
        */
-               for (StylesType styleType : style.getKeySet()) {
-                       switch(styleType){
+               for (Entry<StylesType, String> entry : style.getEntrySet()) {
+                       switch(entry.getKey()){
                                case linesBefore:
                                        if(isFirst(n)){
- String textBefore = makeInsertionString(Integer.valueOf((String)style.get(styleType)),'\n'); + String textBefore = makeInsertionString(Integer.valueOf(entry.getValue()),'\n');
                                                insertBefore(this.total + 
this.spaceBeforeText, textBefore);
                                        }
                                        break;
                                case linesAfter:
                                        if(isLast(n)){
- String textAfter = makeInsertionString(Integer.valueOf((String)style.get(styleType)),'\n'); + String textAfter = makeInsertionString(Integer.valueOf(entry.getValue()),'\n'); insertAfter(this.spaceBeforeText + this.total + viewText.length() + this.spaceAfterText, textAfter);
                                        }
                                        break;
                                case firstLineIndent:
-                                       if(isFirst(n) && 
Integer.valueOf((String)style.get(styleType)) != -2){
-                                               int spaces = 
Integer.valueOf((String)style.get(styleType));
- this.view.setLineIndent(this.view.getLineAtOffset(this.spaceBeforeText + this.total) , 1, spaces * getFontWidth());
+                                       if(isFirst(n) && 
Integer.valueOf(entry.getValue()) != -2){
+                                               int spaces = 
Integer.valueOf(entry.getValue());
+ this.view.setLineIndent(this.view.getLineAtOffset(this.spaceBeforeText + this.total) , 1, spaces * this.charWidth);
                                        }
                                        break;
                                case format:
- this.view.setLineAlignment(this.view.getLineAtOffset(this.spaceBeforeText + this.total + this.spaceAfterText), 1, Integer.valueOf((String)style.get(styleType))); + this.view.setLineAlignment(this.view.getLineAtOffset(this.spaceBeforeText + this.total + this.spaceAfterText), 1, Integer.valueOf(entry.getValue()));
                                        break;
                                case Font:
- setFontRange(this.total, this.spaceBeforeText + n.getValue().length(), Integer.valueOf((String)style.get(styleType))); + setFontRange(this.total, this.spaceBeforeText + n.getValue().length(), Integer.valueOf(entry.getValue()));
                                         break;
                                case leftMargin:
                                        if(!isFirst(n) && followsNewLine(n))
- this.view.setLineIndent(this.view.getLineAtOffset(this.spaceBeforeText + this.total), 1, this.view.getLineIndent(this.view.getLineAtOffset(this.spaceBeforeText + this.total))+ (Integer.valueOf((String)style.get(styleType)) * getFontWidth())); + this.view.setLineIndent(this.view.getLineAtOffset(this.spaceBeforeText + this.total), 1, this.view.getLineIndent(this.view.getLineAtOffset(this.spaceBeforeText + this.total))+ (Integer.valueOf(entry.getValue()) * getFontWidth()));
                                        break;
                                default:
-                                       System.out.println(styleType);
+                                       System.out.println(entry.getKey());
                        }
                }

+               /*
                if(parent.getAttributeValue("semantics").contains("action")){
                        Element grandParent = (Element)parent.getParent();
                        
while(grandParent.getAttributeValue("semantics").contains("action")){
@@ -334,6 +365,7 @@
// if(els.size() > 0 && els.get(els.size() - 1).getLocalName().equals("brl")) // insertAfter(this.spaceBeforeText + this.total + n.getValue().length() + this.spaceAfterText, "\n");
                }
+               */
        }

        private boolean followsNewLine(Node n){
@@ -457,12 +489,12 @@
                StyleRange range = null;
                int total = (Integer)message.getValue("brailleLength");
                System.out.println("Value: " + t.n.getValue());
-
+
                String insertionString = 
(String)message.getValue("newBrailleText");
                if(t.brailleList.getFirst().start != -1){
                        setListenerLock(true);
                        if(t.brailleList.getFirst().start < view.getCharCount())
-                               range = 
view.getStyleRangeAtOffset(t.brailleList.getFirst().start);
+ range = view.getStyleRangeAtOffset(t.brailleList.getFirst().start + ((t.brailleList.getLast().end - t.brailleList.getFirst().start) / 2));

int startLine = this.view.getLineAtOffset(t.brailleList.getFirst().start);
                        int lineIndent = this.view.getLineIndent(startLine);
@@ -508,7 +540,7 @@
                setListenerLock(false);
        }

-       private void setPositionFromStart(){
+       public void setPositionFromStart(){
                int count = 0;
                positionFromStart = view.getCaretOffset() - currentStart;
if(positionFromStart > 0 && currentStart + positionFromStart <= currentEnd){
@@ -590,10 +622,9 @@
                this.words = words;
        }

-       @Override
-       public void resetView() {
+       public void resetView(Group group) {
                setListenerLock(true);
-               view.setText("");
+ recreateView(group, LEFT_MARGIN, RIGHT_MARGIN, TOP_MARGIN, BOTTOM_MARGIN);
                this.total = 0;
                this.spaceBeforeText = 0;
                this.spaceAfterText = 0;
=======================================
--- /src/main/org/brailleblaster/views/TextView.java Tue Jul 2 09:14:48 2013 +++ /src/main/org/brailleblaster/views/TextView.java Wed Jul 10 05:34:57 2013
@@ -29,6 +29,7 @@
 package org.brailleblaster.views;

 import java.util.LinkedList;
+import java.util.Map.Entry;

 import nu.xom.Element;
 import nu.xom.Elements;
@@ -39,16 +40,17 @@
 import org.brailleblaster.mapping.TextMapElement;
 import org.brailleblaster.wordprocessor.BBEvent;
 import org.brailleblaster.wordprocessor.BBSemanticsTable;
-import org.brailleblaster.wordprocessor.BBSemanticsTable.Styles;
-import org.brailleblaster.wordprocessor.BBSemanticsTable.StylesType;
 import org.brailleblaster.wordprocessor.DocumentManager;
 import org.brailleblaster.wordprocessor.Message;
+import org.brailleblaster.wordprocessor.BBSemanticsTable.Styles;
+import org.brailleblaster.wordprocessor.BBSemanticsTable.StylesType;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.CaretEvent;
 import org.eclipse.swt.custom.CaretListener;
 import org.eclipse.swt.custom.ExtendedModifyEvent;
 import org.eclipse.swt.custom.ExtendedModifyListener;
 import org.eclipse.swt.custom.StyleRange;
+import org.eclipse.swt.custom.StyledText;
 import org.eclipse.swt.custom.VerifyKeyListener;
 import org.eclipse.swt.events.FocusEvent;
 import org.eclipse.swt.events.FocusListener;
@@ -59,9 +61,18 @@
 import org.eclipse.swt.events.TraverseEvent;
 import org.eclipse.swt.events.TraverseListener;
 import org.eclipse.swt.events.VerifyEvent;
+import org.eclipse.swt.layout.FormAttachment;
+import org.eclipse.swt.layout.FormData;
+import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Group;

+
 public class TextView extends AbstractView {
+       private final static int LEFT_MARGIN = 16;
+       private final static int RIGHT_MARGIN = 57;
+       private final static int TOP_MARGIN = 0;
+       private final static int BOTTOM_MARGIN = 100;
+
        private int oldCursorPosition = -1;
        private int currentChar;
private int currentStart, currentEnd, previousEnd, nextStart, selectionStart, selectionLength;
@@ -82,7 +93,7 @@
        private String charAtOffset;

        public TextView (Group documentWindow, BBSemanticsTable table) {
-               super (documentWindow, 16, 57, 0, 100);
+ super (documentWindow, LEFT_MARGIN, RIGHT_MARGIN, TOP_MARGIN, BOTTOM_MARGIN);
                this.stylesTable = table;
                this.total = 0;
                this.spaceBeforeText = 0;
@@ -417,6 +428,7 @@

                //String text = n.getValue().replace("\n","");
                String newText = appendToView(n);
+               int textLength = newText.length();
                //System.out.println("Length: " + newText.length());
                //System.out.println(newText);
                view.append(newText);
@@ -424,8 +436,8 @@
        //      for(int i = 0; i < this.spaceAfterText; i++)
        //              view.append("\n");

- list.add(new TextMapElement(this.spaceBeforeText + this.total, this.spaceBeforeText + this.total + newText.length() + this.spaceAfterText,n)); - this.total += this.spaceBeforeText + newText.length() + this.spaceAfterText; + list.add(new TextMapElement(this.spaceBeforeText + this.total, this.spaceBeforeText + this.total + textLength + this.spaceAfterText,n));
+               this.total += this.spaceBeforeText + textLength + 
this.spaceAfterText;

        //      if(view.getCharCount() != this.total){
        //              System.out.println(view.getCharCount() + " " + 
this.total);
@@ -570,7 +582,6 @@
                                                else {
                                                        String brltext = 
brl.getChild(i).getValue();
                                                        totalLength += 
brltext.length();
- // System.out.println("Array length " + indexes.length + " text Lwenvb: " + brltext.length());
                                                        end = 
indexes[totalLength - 1];
                                                        if(totalLength == 
indexes.length){
                                                                text += 
n.getValue().substring(start);
@@ -604,45 +615,44 @@
        }

        private void handleStyle(Styles style, Node n, String viewText){
-               Element parent = (Element)n.getParent();
+       //      Element parent = (Element)n.getParent();
                //checkForLineBreak(parent, n);
-
-               for (StylesType styleType : style.getKeySet()) {
-                       switch(styleType){
+               for (Entry<StylesType, String> entry : style.getEntrySet()) {
+                       switch(entry.getKey()){
                                case linesBefore:
                                        if(isFirst(n)){
- String textBefore = makeInsertionString(Integer.valueOf((String)style.get(styleType)),'\n'); + String textBefore = makeInsertionString(Integer.valueOf(entry.getValue()),'\n');
                                                insertBefore(this.total + 
this.spaceBeforeText, textBefore);
                                        }
                                        break;
                                case linesAfter:
                                        if(isLast(n)){
- String textAfter = makeInsertionString(Integer.valueOf((String)style.get(styleType)), '\n'); + String textAfter = makeInsertionString(Integer.valueOf(entry.getValue()), '\n'); insertAfter(this.spaceBeforeText + this.total + viewText.length(), textAfter);
                                        }
                                        break;
                                case firstLineIndent:
-                                       if(isFirst(n) && 
Integer.valueOf((String)style.get(styleType)) != -2){
-                                               int spaces = 
Integer.valueOf((String)style.get(styleType));
- this.view.setLineIndent(this.view.getLineAtOffset(this.spaceBeforeText + this.total) , 1, spaces * getFontWidth());
+                                       if(isFirst(n) && 
Integer.valueOf(entry.getValue()) != -2){
+                                               int spaces = 
Integer.valueOf(entry.getValue());
+ this.view.setLineIndent(this.view.getLineAtOffset(this.spaceBeforeText + this.total) , 1, spaces * this.charWidth);
                                        }
                                        break;
                                case format:
- this.view.setLineAlignment(this.view.getLineAtOffset(this.spaceBeforeText + this.total + this.spaceAfterText), getLineNumber(this.spaceBeforeText + this.total, viewText), Integer.valueOf((String)style.get(styleType))); + this.view.setLineAlignment(this.view.getLineAtOffset(this.spaceBeforeText + this.total + this.spaceAfterText), getLineNumber(this.spaceBeforeText + this.total, viewText), Integer.valueOf(entry.getValue()));
                                        break;
                                case Font:
- setFontRange(this.total, this.spaceBeforeText + viewText.length(), Integer.valueOf((String)style.get(styleType))); + setFontRange(this.total, this.spaceBeforeText + viewText.length(), Integer.valueOf(entry.getValue()));
                                         break;
                                case leftMargin:
//System.out.println(this.view.getLineAtOffset(this.spaceBeforeText + this.total)); //this.view.setLineWrapIndent(this.view.getLineAtOffset(this.spaceBeforeText + this.total), 1, this.view.getLineIndent(this.view.getLineAtOffset(this.spaceBeforeText + this.total))+ (2 * getFontWidth())); - handleLineWrap( viewText, Integer.valueOf((String)style.get(styleType)));
+                                       handleLineWrap( viewText, 
Integer.valueOf(entry.getValue()));
                                        break;
                                default:
-                                       System.out.println(styleType);
+                                       System.out.println(entry.getKey());
                        }
                }
-
+       /*
                if(parent.getAttributeValue("semantics").contains("action")){
                        Element grandParent = (Element)parent.getParent();
                        
while(grandParent.getAttributeValue("semantics").contains("action")){
@@ -659,6 +669,7 @@
// if(els.size() > 0 && els.get(els.size() - 1).getLocalName().equals("brl")) // insertAfter(this.spaceBeforeText + this.total + viewText.length() + this.spaceAfterText, "\n");
                }
+               */
        }

        private int getLineNumber(int startOffset, String text){
@@ -690,6 +701,7 @@

                                if(selectionStart < currentStart){
                                        sendAdjustRangeMessage(dm, "start", 
currentStart - selectionStart);
+                                       updateRange(range, currentStart, 
e.length);
                                }
                                else if(selectionStart > currentEnd){
                                        sendAdjustRangeMessage(dm, "end", 
selectionStart - currentEnd);
@@ -1057,10 +1069,10 @@
                selectionLength = length;
        }

-       @Override
-       public void resetView() {
+
+       public void resetView(Group group) {
                setListenerLock(true);
-               view.setText("");
+ recreateView(group, LEFT_MARGIN, RIGHT_MARGIN, TOP_MARGIN, BOTTOM_MARGIN);
                this.total = 0;
                this.spaceBeforeText = 0;
                this.spaceAfterText = 0;
=======================================
--- /src/main/org/brailleblaster/views/TreeView.java Tue Jun 11 12:01:30 2013 +++ /src/main/org/brailleblaster/views/TreeView.java Wed Jul 10 05:34:57 2013
@@ -67,14 +67,25 @@
                        this.textMapList = new ArrayList<TextMapElement>();
                }
        }
+
+       private final static int LEFT_MARGIN = 0;
+       private final static int RIGHT_MARGIN = 15;
+       private final static int TOP_MARGIN = 0;
+       private final static int BOTTOM_MARGIN = 100;

        public Tree tree;
        private TreeItem root, previousItem;

+       private FocusListener treeFocusListener;
+       private SelectionListener selectionListener;
+       private TraverseListener traverseListener;
+
        public TreeView(final DocumentManager dm, Group documentWindow){
-               super(documentWindow, 0, 15, 0, 100);
+ super(documentWindow, LEFT_MARGIN, RIGHT_MARGIN, TOP_MARGIN, BOTTOM_MARGIN);
                this.tree = new Tree(view, SWT.VIRTUAL | SWT.NONE);
-
+
+               view.setLayout(new FillLayout());
+
                view.addFocusListener(new FocusListener(){
                        @Override
                        public void focusGained(FocusEvent e) {
@@ -82,14 +93,17 @@
                        }
                        @Override
                        public void focusLost(FocusEvent e) {
-
+
                        }
-               });
-
-               this.tree.addSelectionListener(new SelectionListener(){
+               });
+               this.tree.pack();
+       }
+
+       public void initializeListeners(final DocumentManager dm){
+ this.tree.addSelectionListener(selectionListener = new SelectionListener(){
                        @Override
                        public void widgetDefaultSelected(SelectionEvent e) {
-                               // TODO Auto-generated method stub
+
                        }

                        @Override
@@ -140,7 +154,7 @@
                        }
                });

-               this.tree.addFocusListener(new FocusListener(){
+               this.tree.addFocusListener(treeFocusListener = new 
FocusListener(){
                        @Override
                        public void focusGained(FocusEvent arg0) {
                                // TODO Auto-generated method stub
@@ -156,7 +170,7 @@
                        }
                });

-               this.tree.addTraverseListener(new TraverseListener(){
+               this.tree.addTraverseListener(traverseListener = new 
TraverseListener(){
                        @Override
                        public void keyTraversed(TraverseEvent e) {
                                if(e.stateMask == SWT.MOD1 && e.keyCode == 
SWT.ARROW_DOWN){
@@ -175,12 +189,14 @@
                                }
                        }
                });
-
-               view.setLayout(new FillLayout());
-
-               this.tree.pack();
                setListenerLock(false);
        }
+
+       public void removeListeners(){
+               tree.removeSelectionListener(selectionListener);
+               tree.removeFocusListener(treeFocusListener);
+               tree.removeTraverseListener(traverseListener);
+       }

private void populateItemChildren(TreeItem item, Element e, DocumentManager dm){
                ArrayList<Text>textList = new ArrayList<Text>();
@@ -437,8 +453,8 @@
                // TODO Auto-generated method stub
        }

-       @Override
-       public void resetView() {
+
+       public void resetView(Group group) {
                setListenerLock(true);
                this.root.setExpanded(false);
                depopulateItemChildren(this.root);
=======================================
--- /src/main/org/brailleblaster/wordprocessor/BBDocument.java Tue Jul 2 09:14:48 2013 +++ /src/main/org/brailleblaster/wordprocessor/BBDocument.java Wed Jul 10 05:34:57 2013
@@ -189,7 +189,6 @@

        private int changeBrailleNodes(TextMapElement t, Message message){
Document d = getStringTranslation(t, (String)message.getValue("newText"));
-       //      System.out.println(d.toXML().toString());
                int total = 0;
                int startOffset = 0;
                String insertionString = "";
=======================================
--- /src/main/org/brailleblaster/wordprocessor/BBSemanticsTable.java Tue Jul 2 09:14:48 2013 +++ /src/main/org/brailleblaster/wordprocessor/BBSemanticsTable.java Wed Jul 10 05:34:57 2013
@@ -1,23 +1,20 @@
 package org.brailleblaster.wordprocessor;

 import java.io.BufferedReader;
-import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
 import java.util.HashMap;
+import java.util.Map.Entry;
 import java.util.Set;

 import org.brailleblaster.BBIni;
 import org.brailleblaster.util.Notify;
 import org.eclipse.swt.SWT;

-import nu.xom.Builder;
 import nu.xom.Document;
 import nu.xom.Element;
-import nu.xom.Elements;
 import nu.xom.Node;
-import nu.xom.ParsingException;
-import nu.xom.ValidityException;
+

 public class BBSemanticsTable {
        public enum StylesType{
@@ -58,6 +55,10 @@
                public Set<StylesType> getKeySet(){
                        return this.map.keySet();
                }
+
+               public Set<Entry<StylesType, String>> getEntrySet(){
+                       return this.map.entrySet();
+               }

                public boolean contains(StylesType key){
                        return this.map.containsKey(key);
=======================================
--- /src/main/org/brailleblaster/wordprocessor/DocumentManager.java Tue Jul 2 09:14:48 2013 +++ /src/main/org/brailleblaster/wordprocessor/DocumentManager.java Wed Jul 10 05:34:57 2013
@@ -33,8 +33,10 @@

 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;

 import java.util.ArrayList;
 import java.util.Properties;
@@ -67,6 +69,7 @@
 import org.eclipse.swt.widgets.Group;
 import org.eclipse.swt.widgets.TabItem;

+
//This class manages each document in an MDI environment. It controls the braille View and the daisy View.
 public class DocumentManager {
        WPManager wp;
@@ -105,12 +108,8 @@
                this.text = new TextView(this.group, this.styles);
                this.braille = new BrailleView(this.group, this.styles);
                this.item.setControl(this.group);
-
+               initializeDocumentTab();
                this.document = new BBDocument(this);
-               FontManager.setShellFonts(this.wp.getShell(), this);
-
- this.tabList = new Control[]{this.treeView.view, this.text.view, this.braille.view};
-               this.group.setTabList(this.tabList);
        //      this.wp.getStatusBar().setText("Words: " + 0);

                logger = BBIni.getLogger();
@@ -125,17 +124,20 @@
                }
        }

+       private void initializeDocumentTab(){
+               FontManager.setShellFonts(this.wp.getShell(), this);
+ this.tabList = new Control[]{this.treeView.view, this.text.view, this.braille.view};
+               this.group.setTabList(this.tabList);
+               wp.getShell().layout();
+       }
+
        public void fileSave(){

                // Borrowed from Save As function. Different document types 
require
                // different save methods.
                try {
                        if(workingFilePath.endsWith("xml")){
-                               Document newDoc = this.document.getNewXML();
-                               FileOutputStream os = new 
FileOutputStream(workingFilePath);
-                           Serializer serializer = new Serializer(os, "UTF-8");
-                           serializer.write(newDoc);
-                           os.close();
+                           createXMLFile(workingFilePath);
                        }
                        else if(workingFilePath.endsWith("utd")) {
                                FileOutputStream os = new 
FileOutputStream(workingFilePath);
@@ -194,7 +196,6 @@

                // Update file we're about to work on.
                workingFilePath = fileName;
-
                ////////////////////////
                // Zip and Recent Files.

@@ -245,13 +246,24 @@

                // Zip and Recent Files.
                ////////////////////////
-
-               initializeAllViews(fileName, workingFilePath);
+
+ String tempPath = BBIni.getTempFilesPath() + BBIni.getFileSep() + workingFilePath.substring(fileName.lastIndexOf(BBIni.getFileSep()), fileName.lastIndexOf(".")) + "_temp.xml";
+               normalizeFile(workingFilePath, tempPath);
+               initializeAllViews(fileName, tempPath);
        }

+       private void normalizeFile(String originalFilePath, String 
tempFilePath){
+               Normalizer n = new Normalizer(originalFilePath);
+               n.createNewNormalizedFile(tempFilePath);
+       }
+
        private void initializeAllViews(String fileName, String filePath){
+               //long start = System.currentTimeMillis();
                try{
if(this.document.startDocument(filePath, BBIni.getDefaultConfigFile(), null)){
+                               this.group.setRedraw(false);
+                               this.text.view.setWordWrap(false);
+                               this.braille.view.setWordWrap(false);
                                this.wp.getStatusBar().resetLocation(6,100,100);
                                this.wp.getStatusBar().setText("Loading...");
                                this.wp.getProgressBar().start();
@@ -262,6 +274,7 @@
                                this.document.notifyUser();
                                this.text.initializeListeners(this);
                                this.braille.initializeListeners(this);
+                               this.treeView.initializeListeners(this);
                                this.text.hasChanged = false;
                                this.braille.hasChanged = false;
                                this.wp.checkToolbarSettings();
@@ -269,6 +282,9 @@
                                this.wp.getProgressBar().stop();
                                this.wp.getStatusBar().setText("Words: " + 
this.text.words);
                                this.braille.setWords(this.text.words);
+                               this.text.view.setWordWrap(true);
+                               this.braille.view.setWordWrap(true);
+                               this.group.setRedraw(true);
                        }
                        else {
                                System.out.println("The Document Base document tree 
is empty");
@@ -278,6 +294,8 @@
                catch(Exception e){
                        e.printStackTrace();
                }
+               //long end = System.currentTimeMillis();
+               //System.out.println("TOTAL: " + (end - start));
        }

        private void initializeViews(Node current){
@@ -477,11 +495,7 @@
                                        }
                                }
                                else if(ext.equals("xml")){
-                                       Document newDoc = 
this.document.getNewXML();
-                                       FileOutputStream os = new 
FileOutputStream(filePath);
-                                   Serializer serializer = new Serializer(os, 
"UTF-8");
-                                   serializer.write(newDoc);
-                                   os.close();
+                                   createXMLFile(filePath);
                                    setTabTitle(filePath);
                                    this.documentName = filePath;
                                }
@@ -560,56 +574,118 @@
                        PrintPreview pv = new PrintPreview(this.getDisplay(), 
this.document);
                }
        }
+
+       private void setCurrentOnRefresh(String sender, int offset){
+               Message m = new Message(BBEvent.SET_CURRENT);
+               if(sender != null){
+                       m.put("sender", sender);
+               }
+               m.put("offset", offset);
+               dispatch(m);
+       }

        public void refresh(){
                int currentOffset;
-               Message m = new Message(BBEvent.SET_CURRENT);

                if(this.text.view.isFocusControl()){
                        currentOffset = this.text.view.getCaretOffset();
                        resetViews();
-                       if(currentOffset < this.text.view.getCharCount())
+                       initializeDocumentTab();
+
+                       if(currentOffset < this.text.view.getCharCount()){
                                this.text.view.setCaretOffset(currentOffset);
+                       }
                        else
                                this.text.view.setCaretOffset(0);
-                       m.put("sender", "text");
-                       m.put("offset", currentOffset);
-                       this.dispatch(m);
+
+                       setCurrentOnRefresh("text",currentOffset);
+                       this.text.setPositionFromStart();
+                       this.text.view.setFocus();
                }
                else if(this.braille.view.isFocusControl()){
                        currentOffset = this.braille.view.getCaretOffset();
                        resetViews();
+                       initializeDocumentTab();
+
                        this.braille.view.setCaretOffset(currentOffset);
-                       m.put("sender", "braille");
-                       m.put("offset", currentOffset);
-                       this.dispatch(m);
+                       setCurrentOnRefresh("braille",currentOffset);
+                       this.braille.setPositionFromStart();
+                       this.braille.view.setFocus();
                }
                else if(this.treeView.tree.isFocusControl()){
-                       currentOffset = list.getCurrent().start;
+                       if(this.text.view.getCaretOffset() > 0)
+                               currentOffset = this.text.view.getCaretOffset();
+                       else
+                               currentOffset = list.getCurrent().start;
+
                        resetViews();
-                       m.put("offset", currentOffset);
-                       this.dispatch(m);
+                       initializeDocumentTab();
+
+
+                       setCurrentOnRefresh(null,currentOffset);
+                       this.text.view.setCaretOffset(currentOffset);
+                       this.text.setPositionFromStart();
                }
                else {
                        currentOffset = this.text.view.getCaretOffset();
-                       resetViews();
-                       m.put("offset", currentOffset);
-                       this.dispatch(m);
+
+                       resetViews();
+                       initializeDocumentTab();
+
+                       setCurrentOnRefresh(null,currentOffset);
                        this.text.view.setCaretOffset(currentOffset);
                        this.text.setPositionFromStart();
+                       //this.text.view.setFocus();
                }
        }

        private void resetViews(){
-               list.clear();
-               this.text.removeListeners();
-               this.text.resetView();
-               this.braille.resetView();
-               this.treeView.resetView();
-               this.text.words = 0;
-               updateTempFile();
-               this.document.deleteDOM();
-               initializeAllViews(this.documentName, 
this.document.getOutfile());
+               try {
+ String path = BBIni.getTempFilesPath() + BBIni.getFileSep() + "temp.xml";
+                       File f = new File(path);
+                       f.createNewFile();
+                       createXMLFile(path);
+                       list.clear();
+                       this.text.removeListeners();
+                       this.text.resetView(this.group);
+                       this.braille.removeListeners();
+                       this.braille.resetView(this.group);
+                       this.treeView.removeListeners();
+                       this.treeView.resetView(this.group);
+                       this.text.words = 0;
+                       updateTempFile();
+                       this.document.deleteDOM();
+                       initializeAllViews(this.documentName, path);
+                       f.delete();
+
+               } catch (IOException e) {
+ new Notify("An error occurred while refreshing the document. Please save your work and try again.");
+                       e.printStackTrace();
+               }
+       }
+
+       private boolean createXMLFile(String path){
+               try {
+                       Document newDoc = this.document.getNewXML();
+                       FileOutputStream os;
+                       os = new FileOutputStream(path);
+                       Serializer serializer;
+                       serializer = new Serializer(os, "UTF-8");
+                       serializer.write(newDoc);
+                       os.close();
+               } catch (FileNotFoundException e) {
+                       e.printStackTrace();
+                       return false;
+               }
+               catch (UnsupportedEncodingException e) {
+                       e.printStackTrace();
+                       return false;
+               }
+               catch (IOException e) {
+                       e.printStackTrace();
+                       return false;
+               }
+               return true;
        }

        private void updateTempFile(){

Other related posts:

  • » [brailleblaster] [brailleblaster.newdesign] push by brandon....@xxxxxxxxx - Improved perfromance when loading a file/Added a normalizing class on 2013-07-10 12:40 GMT - brailleblaster