From 8e72a001e435902ce707519995f774cced1eada2 Mon Sep 17 00:00:00 2001
From: Lukas Ladenberger <lukas.ladenberger@googlemail.com>
Date: Tue, 21 Aug 2012 17:53:50 +0200
Subject: [PATCH] working on table + corresponding observer

---
 de.bmotionstudio.gef.editor/plugin.xml        |  9 +++
 .../gef/editor/observer/ColumnObserver.java   | 36 +++++++--
 .../gef/editor/observer/TableObserver.java    | 77 ++++++++++++++++---
 .../observer/wizard/WizardTableObserver.java  | 17 +++-
 4 files changed, 122 insertions(+), 17 deletions(-)

diff --git a/de.bmotionstudio.gef.editor/plugin.xml b/de.bmotionstudio.gef.editor/plugin.xml
index 2c44c36d..599ff15f 100644
--- a/de.bmotionstudio.gef.editor/plugin.xml
+++ b/de.bmotionstudio.gef.editor/plugin.xml
@@ -384,6 +384,9 @@
             <control
                   id="de.bmotionstudio.gef.editor.table">
             </control>
+            <control
+                  id="de.bmotionstudio.gef.editor.tablecolumn">
+            </control>
          </observer>
          <observer
                id="de.bmotionstudio.gef.editor.observer.SwitchCoordinates">
@@ -474,6 +477,9 @@
             <control
                   id="de.bmotionstudio.gef.editor.table">
             </control>
+            <control
+                  id="de.bmotionstudio.gef.editor.tablecolumn">
+            </control>
          </observer>
          <observer
                id="de.bmotionstudio.gef.editor.observer.SwitchChildCoordinates">
@@ -522,6 +528,9 @@
             <control
                   id="de.bmotionstudio.gef.editor.table">
             </control>
+            <control
+                  id="de.bmotionstudio.gef.editor.tablecolumn">
+            </control>
          </observer>
          <observer
                id="de.bmotionstudio.gef.editor.observer.SwitchImage">
diff --git a/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/observer/ColumnObserver.java b/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/observer/ColumnObserver.java
index 078476c6..04309d36 100644
--- a/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/observer/ColumnObserver.java
+++ b/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/observer/ColumnObserver.java
@@ -1,17 +1,38 @@
 package de.bmotionstudio.gef.editor.observer;
 
+import java.util.LinkedList;
+import java.util.List;
+
 import de.bmotionstudio.gef.editor.Animation;
 import de.bmotionstudio.gef.editor.AttributeConstants;
 import de.bmotionstudio.gef.editor.attribute.AbstractAttribute;
 import de.bmotionstudio.gef.editor.model.BControl;
 import de.bmotionstudio.gef.editor.observer.wizard.WizardColumnObserver;
 import de.bmotionstudio.gef.editor.util.BMSUtil;
+import de.prob.unicode.UnicodeTranslator;
 
 public class ColumnObserver extends Observer {
 
 	private String expression;
 	private String predicate;
 
+	public static List<String> split(String input, char tempReplacement) {
+		while (input.matches(".*\"[^\\{\\}]+,[^\\{\\}]+.*")) {
+			input = input.replaceAll("(\"[^\\{\\}]+),([^\\{\\}]+)", "$1"
+					+ tempReplacement + "$2");
+		}
+		while (input.matches(".*\\{[^\\}]+,[^\\}]+\\}.*")) {
+			input = input.replaceAll("(\\{[^\\}]+),([^\\}]+\\})", "$1"
+					+ tempReplacement + "$2");
+		}
+		String[] split = input.split(",");
+		List<String> output = new LinkedList<String>();
+		for (String s : split) {
+			output.add(s.replaceAll(tempReplacement + "", ",").trim());
+		}
+		return output;
+	}
+
 	@Override
 	public void check(Animation animation, BControl control) {
 
@@ -25,8 +46,10 @@ public class ColumnObserver extends Observer {
 
 			String fEval = BMSUtil.parseExpression(expression, control,
 					animation);
-			fEval = fEval.replace("}", "").replace("{", "");
-			String[] splitArray = fEval.split(",");
+			fEval = UnicodeTranslator.toAscii(fEval);
+			fEval = fEval.replaceAll("^\\{", "");
+			fEval = fEval.replaceAll("\\}$", "");
+			List<String> output = split(fEval, '#');
 
 			AbstractAttribute attributeRows = control.getParent().getAttribute(
 					AttributeConstants.ATTRIBUTE_ROWS);
@@ -36,13 +59,16 @@ public class ColumnObserver extends Observer {
 
 			control.getParent().setAttributeValue(
 					AttributeConstants.ATTRIBUTE_ROWS,
-					defaultRows + splitArray.length, true, false);
+					defaultRows + output.size(), true, false);
 
-			for (int i = defaultRows; i < splitArray.length + defaultRows; i++) {
+			for (int i = defaultRows; i < output.size() + defaultRows; i++) {
+				String val = output.get(i - defaultRows);
+				if (val != null && val.length() > 0)
+					val = UnicodeTranslator.toUnicode(val);
 				control.getChildrenArray()
 						.get(i)
 						.setAttributeValue(AttributeConstants.ATTRIBUTE_TEXT,
-								splitArray[i - defaultRows]);
+								val);
 			}
 
 		}
diff --git a/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/observer/TableObserver.java b/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/observer/TableObserver.java
index 436098c6..5377db99 100644
--- a/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/observer/TableObserver.java
+++ b/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/observer/TableObserver.java
@@ -1,5 +1,12 @@
 package de.bmotionstudio.gef.editor.observer;
 
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.regex.MatchResult;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 import de.bmotionstudio.gef.editor.Animation;
 import de.bmotionstudio.gef.editor.AttributeConstants;
 import de.bmotionstudio.gef.editor.attribute.AbstractAttribute;
@@ -13,6 +20,41 @@ public class TableObserver extends Observer {
 	private String expression;
 	private String predicate;
 
+	public static List<String> split(String input, char tempReplacement) {
+		while (input.matches(".*\"[^\\{\\}]+,[^\\{\\}]+.*")) {
+			input = input.replaceAll("([^\\{\\}]+),([^\\{\\}]+)", "$1"
+					+ tempReplacement + "$2");
+		}
+		while (input.matches(".*\\{[^\\}]+,[^\\}]+\\}.*")) {
+			input = input.replaceAll("(\\{[^\\}]+),([^\\}]+\\})", "$1"
+					+ tempReplacement + "$2");
+		}
+		String[] split = input.split(",");
+		List<String> output = new LinkedList<String>();
+		for (String s : split) {
+			output.add(s.replaceAll(tempReplacement + "", ",").trim());
+		}
+		return output;
+	}
+
+	public static List<String> split2(String input, char tempReplacement) {
+		while (input.matches(".*\"[^\\(\\)]+\\|->[^\\(\\)]+.*")) {
+			input = input.replaceAll("(\"[^\\(\\)]+)\\|->([^\\(\\)]+)", "$1"
+					+ tempReplacement + "$2");
+		}
+		while (input.matches(".*\\([^\\)]+\\|->[^\\)]+\\).*")) {
+			input = input.replaceAll("(\\([^\\)]+)\\|->([^\\)]+\\))", "$1"
+					+ tempReplacement + "$2");
+		}
+		String[] split = input.split("\\|->");
+		List<String> output = new LinkedList<String>();
+		for (String s : split) {
+			output.add(s.replaceAll(tempReplacement + "", "\\|->").trim());
+		}
+		return output;
+	}
+
+
 	@Override
 	public void check(Animation animation, BControl control) {
 
@@ -26,12 +68,15 @@ public class TableObserver extends Observer {
 
 			String fEval = BMSUtil.parseExpression(expression, control,
 					animation);
+			fEval = UnicodeTranslator.toAscii(fEval);
+			fEval = fEval.replaceAll("^\\{", "");
+			fEval = fEval.replaceAll("\\}$", "");
 
-			fEval = fEval.replace("}", "").replace("{", "").replace(")", "")
-					.replace("(", "");
-			String[] splitArray = fEval.split(",");
+			// System.out.println(fEval);
 
-			// ---------------------------------------------------------------
+			// String input = "aa, a, aa, {bb, 1, 2}, {cc}, {dd,5}";
+			String input = fEval;
+			List<String> rows = split(input, '#');
 
 			AbstractAttribute attributeRows = control
 					.getAttribute(AttributeConstants.ATTRIBUTE_ROWS);
@@ -43,7 +88,7 @@ public class TableObserver extends Observer {
 			Integer numberOfOldColumns = Integer.valueOf(attributeColumns
 					.getInitValue().toString());
 
-			int numberOfNewRows = splitArray.length;
+			int numberOfNewRows = rows.size();
 
 			// Set the correct number of rows
 			control.setAttributeValue(AttributeConstants.ATTRIBUTE_ROWS,
@@ -54,11 +99,14 @@ public class TableObserver extends Observer {
 			// Set content and the correct number of columns
 			for (int i = numberOfOldRows; i < numberOfNewRows + numberOfOldRows; i++) {
 
-				String content = UnicodeTranslator.toAscii(
-						splitArray[i - numberOfOldRows]).replace("|->", ",");
+				String content = UnicodeTranslator.toAscii(rows.get(i
+						- numberOfOldRows));
 
-				String[] vals = content.split(",");
-				int numberOfNewColumns = vals.length;
+				content = content.replaceAll("^\\(", "");
+				content = content.replaceAll("\\)$", "");
+
+				List<String> columns = split2(content, '#');
+				int numberOfNewColumns = columns.size();
 
 				// Set only one time the number of columns!
 				if (!setColumns) {
@@ -72,9 +120,11 @@ public class TableObserver extends Observer {
 				}
 
 				for (int z = 0; z < numberOfNewColumns; z++) {
-					String val = vals[z];
+					String val = columns.get(z);
 					BControl column = control.getChildrenArray().get(z);
 					BControl cell = column.getChildrenArray().get(i);
+					if (val != null && val.length() > 0)
+						val = UnicodeTranslator.toUnicode(val);
 					cell.setAttributeValue(AttributeConstants.ATTRIBUTE_TEXT,
 							val);
 				}
@@ -85,6 +135,13 @@ public class TableObserver extends Observer {
 
 	}
 
+	private Iterable<MatchResult> findMatches(String pattern, CharSequence s) {
+		List<MatchResult> results = new ArrayList<MatchResult>();
+		for (Matcher m = Pattern.compile(pattern).matcher(s); m.find();)
+			results.add(m.toMatchResult());
+		return results;
+	}
+
 	public void setExpression(String expression) {
 		this.expression = expression;
 	}
diff --git a/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/observer/wizard/WizardTableObserver.java b/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/observer/wizard/WizardTableObserver.java
index cf0f44ba..6d5a7a66 100644
--- a/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/observer/wizard/WizardTableObserver.java
+++ b/de.bmotionstudio.gef.editor/src/de/bmotionstudio/gef/editor/observer/wizard/WizardTableObserver.java
@@ -26,6 +26,8 @@ import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.List;
 import org.eclipse.swt.widgets.Text;
+import org.eventb.core.ast.Expression;
+import org.eventb.core.ast.FormulaFactory;
 import org.eventb.core.ast.PowerSetType;
 
 import de.bmotionstudio.gef.editor.eventb.EventBHelper;
@@ -34,6 +36,7 @@ import de.bmotionstudio.gef.editor.model.BControl;
 import de.bmotionstudio.gef.editor.observer.Observer;
 import de.bmotionstudio.gef.editor.observer.ObserverWizard;
 import de.bmotionstudio.gef.editor.observer.TableObserver;
+import de.prob.unicode.UnicodeTranslator;
 
 public class WizardTableObserver extends ObserverWizard {
 
@@ -90,15 +93,25 @@ public class WizardTableObserver extends ObserverWizard {
 			java.util.List<MachineContentObject> constants = EventBHelper
 					.getConstants(getBControl().getVisualization());
 			for (MachineContentObject mobj : constants) {
-				if (mobj.getType() instanceof PowerSetType)
+				if (mobj.getType() instanceof PowerSetType) {
+					Expression expression = ((PowerSetType) mobj.getType())
+							.toExpression(FormulaFactory.getDefault());
+					System.out.println(UnicodeTranslator.toAscii(expression
+							.toString()));
 					relationList.add(mobj.getLabel());
+				}
 			}
 
 			java.util.List<MachineContentObject> variables = EventBHelper
 					.getVariables(getBControl().getVisualization());
 			for (MachineContentObject mobj : variables) {
-				if (mobj.getType() instanceof PowerSetType)
+				if (mobj.getType() instanceof PowerSetType) {
+					Expression expression = ((PowerSetType) mobj.getType())
+							.toExpression(FormulaFactory.getDefault());
+					System.out.println(UnicodeTranslator.toAscii(expression
+							.toString()));
 					relationList.add(mobj.getLabel());
+				}
 			}
 
 			final List list = new List(conRight, SWT.SINGLE | SWT.BORDER);
-- 
GitLab