Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editare text #48

Merged
merged 4 commits into from Jan 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/element_actions/PopupListener.java
Expand Up @@ -14,9 +14,9 @@
*/
public class PopupListener implements ActionListener {

private final GElement panel;
private GElement panel;

private final LayoutParser layoutParser;
private LayoutParser layoutParser;

/**
* Constructor.
Expand Down
14 changes: 8 additions & 6 deletions src/element_actions/ViewText.java
Expand Up @@ -14,6 +14,8 @@
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import parser.TextActions;

/**
* @author Unknown-Revengers
*
Expand Down Expand Up @@ -59,7 +61,7 @@ public ViewText(GElement panel) {
contentPanel = this.getContentPane();

// Adauga textul elementului la frame.
this.addText(elementPanel.element.getData().text);
this.addText();

// Adauga buton.
this.addButton();
Expand All @@ -83,7 +85,8 @@ private void addButton() {
@Override
public void actionPerformed(ActionEvent e) {
// Save text.
elementPanel.element.getData().text = descriptionArea.getText();
TextActions.saveText(elementPanel.element,
descriptionArea.getText());

frame.dispose();
}
Expand All @@ -95,14 +98,13 @@ public void actionPerformed(ActionEvent e) {

/**
* Adauga text la fereastra.
*
* @param text
* Textul de adaugat.
*/
private void addText(String text) {
private void addText() {
// New Panel.
JPanel panel = new JPanel();

String text = TextActions.getText(elementPanel.element);

// Creaza Text Area pentru text.
descriptionArea = new JTextArea(text);
descriptionArea.setLineWrap(true);
Expand Down
12 changes: 6 additions & 6 deletions src/gui/LayoutGUI.java
Expand Up @@ -47,12 +47,12 @@ public class LayoutGUI extends JFrame {
/**
* Layout parser.
*/
public final LayoutParser layoutParser;
public LayoutParser layoutParser;

/**
* Imaginea ce este incarcata.
*/
private final BufferedImage image;
private BufferedImage image;

/**
* Panelul in care se deseneaza imaginea.
Expand Down Expand Up @@ -93,8 +93,8 @@ public LayoutGUI(LayoutParser layoutParser) throws IOException {
// Incarca elementele din pagina.
this.loadElements(VisibleElements.S_BLOCK);

// Actiuni finale asupra documentului.
this.addFinalActions();
// Actiuni e asupra documentului.
this.addActions();

// Afiseaza fereastra.
this.setVisible(true);
Expand Down Expand Up @@ -310,7 +310,7 @@ private void addFilters() {
/**
* TODO Buton pentru salvat schimbarile facute intr-un fisier de output.
*/
private void addFinalActions() {
private void addActions() {
// TODO Ruleaza modul de numerotare a paginii.
JButton btnNumeroteaza = new JButton("Numeroteaza pagina");
btnNumeroteaza.setBounds(this.getMaximizedBounds().width - 290,
Expand All @@ -327,4 +327,4 @@ private void addFinalActions() {
btnSalveaza.addActionListener(new SalveazaButtonListener());
getContentPane().add(btnSalveaza);
}
}
}
208 changes: 208 additions & 0 deletions src/parser/TextActions.java
@@ -0,0 +1,208 @@
package parser;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import tree.GenericTreeNode;

/**
* @author diana
*
*/
public class TextActions {
/**
* Genereaza textul pentru elementul curent.
*
* @param element
* Elementul curent.
* @return String Textul elementului.
*/
@SuppressWarnings("unchecked")
public static String getText(
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(getText(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;
}

/**
* Salveaza modificarile facute in arbore.
*
* @param text
* Textul de salvat in arbore.
*/
public static void saveText(
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 atunci aceasta este
* text.
*/
String[] textComponents = { text };

// Sparge textul dupa delimitatori.
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("( )+", " ");

saveText(children.get(i), textComponents[i]);
break;

} else if (children.size() > i) {
// Schimba textul din nodul existent children.get(i).
saveText(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) {
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 static 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 {
deleteText(children.get(i), 0);
}
}
}

/**
* @author Unknown-Revengers
*
* Comparator Class for Elements.
*/
@SuppressWarnings("rawtypes")
public static 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;
}
}
}