Permalink
Browse files

Muta metodele pentru actiuni pe text in parser.TextActions

  • Loading branch information...
1 parent e8ffd5d commit b4cb81aa50f407c303337e3fdc2ed9ff9a48b5e4 @dianaprajescu dianaprajescu committed Jan 5, 2013
Showing with 211 additions and 199 deletions.
  1. +3 −199 src/element_actions/ViewText.java
  2. +208 −0 src/parser/TextActions.java
@@ -7,18 +7,14 @@
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
-import parser.LayoutParserTreeElement;
-import tree.GenericTreeNode;
+import parser.TextActions;
/**
* @author Unknown-Revengers
@@ -89,7 +85,7 @@ private void addButton() {
@Override
public void actionPerformed(ActionEvent e) {
// Save text.
- ViewText.this.changeText(elementPanel.element,
+ TextActions.saveText(elementPanel.element,
descriptionArea.getText());
frame.dispose();
@@ -101,136 +97,13 @@ public void actionPerformed(ActionEvent e) {
}
/**
- * Salveaza modificarile facute in arbore.
- *
- * @param text
- * Textul de salvat in arbore.
- */
- private void changeText(GenericTreeNode<LayoutParserTreeElement> element,
- String text) {
- /*
- * Daca elementul curent este nod frunza, modifica atributul text.
- */
- if (element.getData().elementType.toString().compareTo("String") == 0) {
- element.getData().text = text;
- return;
- }
-
- /*
- * Daca TextBlock sau TextLine are doar o componenta
- * (textComponents.length = 0) atunci aceasta este text.
- */
- String[] textComponents = { text };
-
- if (element.getData().elementType.toString().compareTo("TextBlock") == 0
- && text.contains("\n")) {
- // Split text in lines.
- textComponents = text.split("\n");
-
- } else if (element.getData().elementType.toString().compareTo(
- "TextLine") == 0) {
-
- if (text.contains("\n")) {
- text = text.replace("\n", " ");
- }
-
- text = text.trim();
- text = text.replaceAll("( )+", " ");
- textComponents = text.split(" ");
- }
-
- // Ia copii elementului curent.
- List<GenericTreeNode<LayoutParserTreeElement>> children = element
- .getChildren();
-
- // Sorteaza dupa top.
- Collections.sort(children, new ElementComparator());
-
- // Adauga si modifica text in arbore.
- for (int i = 0; i < textComponents.length; i++) {
- if (children.size() == i + 1
- && element.getData().elementType.toString().compareTo(
- "TextBlock") == 0) {
- /*
- * Daca numarul de noduri TextLine din TextBlock este mai mare
- * decat numarul de noduri TextLine existente in arbore, adauga
- * ultimile TextLine la ultimul nod TextLine din arbore.
- */
- for (int j = i + 1; j < textComponents.length; j++) {
- textComponents[i] = textComponents[i].concat(" ")
- .concat(textComponents[j]);
- }
-
- textComponents[i] = textComponents[i].trim();
- textComponents[i] = textComponents[i].replaceAll("( )+", " ");
-
- this.changeText(children.get(i), textComponents[i]);
- break;
-
- } else if (children.size() > i) {
- // Schimba textul din nodul existent children.get(i).
- this.changeText(children.get(i), textComponents[i]);
-
- } else if (element.getData().elementType.toString().compareTo(
- "TextLine") == 0) {
- // Creaza frunze (elemente de tipul String).
- LayoutParserTreeElement frunza = new LayoutParserTreeElement();
- frunza.text = textComponents[i];
- frunza.elementType = parser.LayoutParserTreeElement.ElementType.STRING;
-
- GenericTreeNode<LayoutParserTreeElement> x = new GenericTreeNode<LayoutParserTreeElement>();
- x.setData(frunza);
-
- element.addChild(x);
- }
- }
-
- // Sterge text din arbore.
- if (children.size() > textComponents.length) {
- this.deleteText(element, textComponents.length);
- }
- }
-
- /**
- * Sterge noduri frunza daca la editare s-au sters cuvinte.
- *
- * @param element
- * Elementul din care s-au sters cuvinte.
- * @param textLength
- * Numarul de elemente ramase.
- */
- private void deleteText(GenericTreeNode<LayoutParserTreeElement> element,
- int textLength) {
- // Ia copii elementului curent.
- List<GenericTreeNode<LayoutParserTreeElement>> children = element
- .getChildren();
-
- // Sorteaza dupa top.
- Collections.sort(children, new ElementComparator());
-
- for (int i = textLength; i < children.size(); i++) {
- // Daca este frunza, sterge nodul
- if (children.get(i).getData().elementType.toString().compareTo(
- "String") == 0) {
- element.removeChildAt(i);
-
- // One child removed, size drops by 1.
- i--;
-
- } else {
- this.deleteText(children.get(i), 0);
- }
- }
- }
-
- /**
* Adauga text la fereastra.
*/
private void addText() {
// New Panel.
JPanel panel = new JPanel();
- String text = this.generateText(elementPanel.element);
+ String text = TextActions.getText(elementPanel.element);
// Creaza Text Area pentru text.
descriptionArea = new JTextArea(text);
@@ -251,75 +124,6 @@ private void addText() {
}
/**
- * Genereaza textul pentru elementul curent.
- *
- * @param element
- * Elementul curent.
- * @return String Textul elementului.
- */
- @SuppressWarnings("unchecked")
- private String generateText(
- GenericTreeNode<LayoutParserTreeElement> element) {
-
- // Daca elementul este nod frunza returneaza textul.
- if (element.getData().elementType.toString().compareTo("String") == 0) {
- return element.getData().text;
- }
-
- // Ia copii elementului curent.
- List<GenericTreeNode<LayoutParserTreeElement>> children = element
- .getChildren();
-
- // Sorteaza dupa top.
- Collections.sort(children, new ElementComparator());
-
- String text = "";
- for (GenericTreeNode<LayoutParserTreeElement> child : children) {
- text = text.concat(this.generateText(child));
- if (child.getData().elementType.toString().compareTo("TextLine") == 0) {
- text = text.concat("\n");
- }
- else if (child.getData().elementType.toString().compareTo("String") == 0) {
- text = text.concat(" ");
- }
- }
-
- // Remove space and new line added only after last component.
- text = text.replaceAll(" $", "");
- if (element.getData().elementType.toString().compareTo("TextBlock") == 0) {
- text = text.substring(0, text.length() - 1);
- }
- return text;
- }
-
- /**
- * @author Unknown-Revengers
- *
- * Comparator Class for Elements.
- */
- @SuppressWarnings("rawtypes")
- public class ElementComparator implements Comparator {
- @SuppressWarnings("unchecked")
- @Override
- public int compare(Object node1, Object node2) {
- // Nu compara nodurile frunza, ele nu au top.
- if (((GenericTreeNode<LayoutParserTreeElement>) node1).getData().elementType
- .toString().compareTo("String") != 0) {
- int top1 = ((GenericTreeNode<LayoutParserTreeElement>) node1)
- .getData().top;
- int top2 = ((GenericTreeNode<LayoutParserTreeElement>) node2)
- .getData().top;
-
- if (top1 != top2) {
- return top1 > top2 ? -1 : 1;
- }
- }
-
- return 0;
- }
- }
-
- /**
* Initializeaza frame.
*/
private void initFrame() {
Oops, something went wrong.

0 comments on commit b4cb81a

Please sign in to comment.