mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
majour redesign of the Phrasebook UI to support recursive phrases
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<CheckBox
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Activate Advanced Options"
|
||||
android:id="@+id/checkBox" />
|
||||
|
||||
</LinearLayout>
|
||||
@@ -6,42 +6,55 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@drawable/grid_phrasebook_button"
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:layout_marginRight="10dp"
|
||||
android:id="@+id/fragment_translation">
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/containerfor_translation"
|
||||
android:layout_marginLeft="0dp"
|
||||
android:layout_marginTop="0dp">
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="New Text"
|
||||
android:textSize="24dp"
|
||||
android:id="@+id/origin_phrase"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:layout_marginRight="10dp"
|
||||
android:layout_marginTop="16dp"
|
||||
android:layout_marginBottom="16dp"
|
||||
android:maxLines="4"
|
||||
android:scrollHorizontally="false"
|
||||
/>
|
||||
|
||||
</FrameLayout>
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="New Text"
|
||||
android:textSize="24dp"
|
||||
android:id="@+id/target_phrase"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:layout_marginRight="10dp"
|
||||
android:maxLines="4"
|
||||
android:scrollHorizontally="false"
|
||||
/>
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/containerfor_options"
|
||||
android:layout_marginLeft="0dp"
|
||||
android:layout_marginTop="0dp">
|
||||
<ImageView
|
||||
android:id="@+id/button3"
|
||||
android:layout_width="30dp"
|
||||
android:layout_height="30dp"
|
||||
android:layout_gravity="right"
|
||||
android:src="@drawable/ic_volume_up_black_18dp"/>
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/dots"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
android:layout_gravity="center|bottom">
|
||||
|
||||
<ImageView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/firstDot"
|
||||
android:src="@drawable/ic_dictionary" />
|
||||
|
||||
<ImageView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/secondDot"
|
||||
android:src="@drawable/ic_dictionary" />
|
||||
</LinearLayout>
|
||||
</FrameLayout>
|
||||
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/input_holder"
|
||||
android:divider="@android:color/transparent"
|
||||
android:padding="8dp"/>
|
||||
</LinearLayout>
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical" android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/input_holder"
|
||||
android:padding="8dp"
|
||||
>
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="New Text"
|
||||
android:textSize="25dp"
|
||||
android:id="@+id/holderOptionText" />
|
||||
|
||||
</LinearLayout>
|
||||
@@ -1,59 +0,0 @@
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="16dp"
|
||||
tools:context=".gui.smallFragments.OptionsFragment"
|
||||
>
|
||||
|
||||
<ScrollView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/scrollView" >
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/child_fragment1">
|
||||
</FrameLayout>
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/child_fragment2">
|
||||
</FrameLayout>
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/child_fragment3">
|
||||
</FrameLayout>
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/child_fragment4">
|
||||
</FrameLayout>
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/child_fragment5">
|
||||
</FrameLayout>
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/child_fragment6">
|
||||
</FrameLayout>
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
@@ -1,15 +0,0 @@
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".gui.smallFragments.SwipeFragment"
|
||||
android:id="@+id/fragment_swipe">
|
||||
|
||||
|
||||
<android.support.v4.view.ViewPager
|
||||
android:id="@+id/vpPager"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
</android.support.v4.view.ViewPager>
|
||||
|
||||
</FrameLayout>
|
||||
@@ -1,45 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@drawable/grid_phrasebook_button"
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:layout_marginRight="10dp"
|
||||
android:id="@+id/fragment_translation">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="New Text"
|
||||
android:textSize="24dp"
|
||||
android:id="@+id/origin_phrase"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:layout_marginRight="10dp"
|
||||
android:layout_marginTop="16dp"
|
||||
android:layout_marginBottom="16dp"
|
||||
android:maxLines="4"
|
||||
android:scrollHorizontally="false"
|
||||
/>
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="New Text"
|
||||
android:textSize="24dp"
|
||||
android:id="@+id/target_phrase"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:layout_marginRight="10dp"
|
||||
android:maxLines="4"
|
||||
android:scrollHorizontally="false"
|
||||
/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/button3"
|
||||
android:layout_width="30dp"
|
||||
android:layout_height="30dp"
|
||||
android:layout_gravity="right"
|
||||
android:src="@drawable/ic_volume_up_black_18dp"/>
|
||||
|
||||
</LinearLayout>
|
||||
@@ -2,8 +2,7 @@
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/spinner_input">
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
@@ -17,4 +16,4 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/choice_spinner" />
|
||||
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
@@ -1,39 +1,36 @@
|
||||
package se.chalmers.phrasebook.backend;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import org.w3c.dom.*;
|
||||
import org.xml.sax.*;
|
||||
import org.w3c.dom.ls.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import javax.xml.parsers.*;
|
||||
|
||||
import org.grammaticalframework.pgf.Expr;
|
||||
import org.grammaticalframework.ui.android.R;
|
||||
import org.grammaticalframework.ui.android.GFTranslator;
|
||||
import se.chalmers.phrasebook.backend.syntax.SyntaxNodeList;
|
||||
import se.chalmers.phrasebook.backend.syntax.SyntaxTree;
|
||||
import se.chalmers.phrasebook.backend.syntax.*;
|
||||
|
||||
/**
|
||||
* Created by Björn on 2016-03-03.
|
||||
*/
|
||||
public class Model {
|
||||
private static Model model;
|
||||
|
||||
private XMLParser parser;
|
||||
private ArrayList<SyntaxTree> phrases;
|
||||
private SyntaxTree currentPhrase;
|
||||
private List<SyntaxTree> phrases;
|
||||
|
||||
private Model() {
|
||||
try {
|
||||
InputStream phrases = GFTranslator.get().getAssets().open("phrases.xml");
|
||||
parser = new XMLParser(phrases);
|
||||
phrases.close();
|
||||
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
InputStream is = GFTranslator.get().getAssets().open("phrases.xml");
|
||||
Document document = documentBuilder.parse(is);
|
||||
phrases = parseSentencesData(document);
|
||||
is.close();
|
||||
} catch (ParserConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
} catch (SAXException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
phrases = new ArrayList<SyntaxTree>();
|
||||
for (Map.Entry<String,String> entry : parser.getSentencesData()) {
|
||||
phrases.add(parser.getSyntaxTree(entry.getKey()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,39 +44,111 @@ public class Model {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void update(int optionIndex, SyntaxNodeList target, int childIndex, boolean isAdvanced) {
|
||||
currentPhrase.setSelectedChild(optionIndex, target, childIndex, isAdvanced);
|
||||
}
|
||||
public List<SyntaxTree> getSentences() {
|
||||
return phrases;
|
||||
}
|
||||
|
||||
public List<String> getSentencesInCurrentPhrasebook() {
|
||||
ArrayList<String> sentences = new ArrayList<String>();
|
||||
for (Map.Entry<String,String> entry : parser.getSentencesData()) {
|
||||
sentences.add(entry.getValue());
|
||||
private List<SyntaxTree> parseSentencesData(Document document) {
|
||||
List<SyntaxTree> sentences = new ArrayList<SyntaxTree>();
|
||||
Map<String,SyntaxNode> ids = new HashMap<String,SyntaxNode>();
|
||||
List<SyntaxNodeCall> calls = new ArrayList<SyntaxNodeCall>();
|
||||
|
||||
NodeList nodesList = document.getDocumentElement().getChildNodes();
|
||||
for (int i = 0; i < nodesList.getLength(); i++) {
|
||||
Node node = nodesList.item(i);
|
||||
|
||||
if (node != null &&
|
||||
node.getNodeType() == Node.ELEMENT_NODE &&
|
||||
node.getNodeName().equals("sentence")) {
|
||||
NamedNodeMap attributes = node.getAttributes();
|
||||
|
||||
if (attributes == null)
|
||||
continue;
|
||||
|
||||
String desc = "";
|
||||
if (attributes.getNamedItem("desc") != null) {
|
||||
desc = attributes.getNamedItem("desc").getNodeValue();
|
||||
}
|
||||
|
||||
SyntaxNode[] nodes = constructSyntaxNodeList(node, ids, calls);
|
||||
if (nodes.length > 0)
|
||||
sentences.add(new SyntaxTree(desc, nodes[0]));
|
||||
} else if (node.getAttributes() != null && node.getAttributes().getNamedItem("id") != null) {
|
||||
String id = node.getAttributes().getNamedItem("id").getNodeValue();
|
||||
SyntaxNode snode = constructSyntaxNode(node, ids, calls);
|
||||
if (snode == null) {
|
||||
throw new IllegalArgumentException(outerXml(node));
|
||||
}
|
||||
ids.put(id,snode);
|
||||
}
|
||||
}
|
||||
|
||||
for (SyntaxNodeCall call : calls) {
|
||||
call.bind(ids);
|
||||
}
|
||||
return sentences;
|
||||
}
|
||||
|
||||
public void setCurrentPhrase(int position) {
|
||||
SyntaxTree choosenPhrase = phrases.get(position);
|
||||
currentPhrase = parser.getSyntaxTree(choosenPhrase.getId());
|
||||
boolean status = currentPhrase.replicate(choosenPhrase);
|
||||
}
|
||||
private SyntaxNode constructSyntaxNode(Node node, Map<String,SyntaxNode> ids, List<SyntaxNodeCall> calls) {
|
||||
NamedNodeMap attributes = node.getAttributes();
|
||||
if (attributes == null)
|
||||
return null;
|
||||
|
||||
public String getDescFromPos(int pos) {
|
||||
return parser.getSentencesData().get(pos).getValue();
|
||||
}
|
||||
String desc = "";
|
||||
if (attributes.getNamedItem("desc") != null) {
|
||||
desc = attributes.getNamedItem("desc").getNodeValue();
|
||||
}
|
||||
|
||||
public void setNumeralCurrentPhrase() {
|
||||
for (int i = 0; i < parser.getSentencesData().size(); i++) {
|
||||
String key = parser.getSentencesData().get(i).getKey();
|
||||
if (key.equals("NNumeral")) {
|
||||
currentPhrase = parser.getSyntaxTree(key);
|
||||
if (node.getNodeName().equals("function")) {
|
||||
String function = attributes.getNamedItem("name").getNodeValue();
|
||||
SyntaxNode[] arguments = constructSyntaxNodeList(node, ids, calls);
|
||||
return new SyntaxNodeFunction(desc, function, arguments);
|
||||
} else if (node.getNodeName().equals("numeral")) {
|
||||
return new SyntaxNodeNumeral(desc, 1, 100);
|
||||
} else if (node.getNodeName().equals("option")) {
|
||||
SyntaxNode[] options = constructSyntaxNodeList(node, ids, calls);
|
||||
return new SyntaxNodeOption(desc, options);
|
||||
} else if (node.getNodeName().equals("call")) {
|
||||
if (attributes.getNamedItem("ref") == null)
|
||||
return null;
|
||||
|
||||
String ref = attributes.getNamedItem("ref").getNodeValue();
|
||||
SyntaxNode[] arguments = constructSyntaxNodeList(node, ids, calls);
|
||||
SyntaxNodeCall call = new SyntaxNodeCall(desc, ref, arguments);
|
||||
calls.add(call);
|
||||
return call;
|
||||
} else if (node.getNodeName().equals("argument")) {
|
||||
int d = 0;
|
||||
if (attributes.getNamedItem("index") != null)
|
||||
d = Integer.parseInt(attributes.getNamedItem("index").getNodeValue());
|
||||
return new SyntaxNodeArgument(desc, d);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private SyntaxNode[] constructSyntaxNodeList(Node root, Map<String,SyntaxNode> ids, List<SyntaxNodeCall> calls) {
|
||||
NodeList nl = root.getChildNodes();
|
||||
|
||||
int index = 0;
|
||||
SyntaxNode[] list = new SyntaxNode[nl.getLength()];
|
||||
for (int i = 0; i < nl.getLength(); i++) {
|
||||
Node node = nl.item(i);
|
||||
if (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
list[index] = constructSyntaxNode(node, ids, calls);
|
||||
if (list[index] == null)
|
||||
throw new IllegalArgumentException(outerXml(node));
|
||||
index++;
|
||||
}
|
||||
}
|
||||
return Arrays.copyOf(list, index);
|
||||
}
|
||||
|
||||
public SyntaxTree getCurrentPhrase() {
|
||||
return currentPhrase;
|
||||
}
|
||||
|
||||
private String outerXml(Node node) {
|
||||
DOMImplementationLS lsImpl = (DOMImplementationLS)node.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
|
||||
LSSerializer lsSerializer = lsImpl.createLSSerializer();
|
||||
lsSerializer.getDomConfig().setParameter("xml-declaration", false);
|
||||
return lsSerializer.writeToString(node);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,195 +0,0 @@
|
||||
package se.chalmers.phrasebook.backend;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
import se.chalmers.phrasebook.backend.syntax.NumeralSyntaxNode;
|
||||
import se.chalmers.phrasebook.backend.syntax.SyntaxNode;
|
||||
import se.chalmers.phrasebook.backend.syntax.SyntaxNodeList;
|
||||
import se.chalmers.phrasebook.backend.syntax.SyntaxTree;
|
||||
|
||||
/**
|
||||
* Created by David on 2016-02-19.
|
||||
*/
|
||||
public class XMLParser {
|
||||
|
||||
private DocumentBuilder documentBuilder;
|
||||
private Document document;
|
||||
private String currentId;
|
||||
|
||||
public XMLParser(InputStream is) {
|
||||
try {
|
||||
documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
document = documentBuilder.parse(is);
|
||||
} catch (ParserConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
} catch (SAXException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Map.Entry<String, String>> getSentencesData() {
|
||||
String[] result;
|
||||
List<Map.Entry<String, String>> sentences = new ArrayList<Map.Entry<String, String>>();
|
||||
|
||||
NodeList sentencesList = document.getElementsByTagName("sentence");
|
||||
int nbrOfSentences = sentencesList.getLength();
|
||||
|
||||
for (int i = 0; i < nbrOfSentences; i++) {
|
||||
String desc = sentencesList.item(i).getAttributes().getNamedItem("desc").getNodeValue();
|
||||
String id = sentencesList.item(i).getAttributes().getNamedItem("id").getNodeValue();
|
||||
if (desc != null && id != null)
|
||||
sentences.add(new HashMap.SimpleEntry<String,String>(id, desc));
|
||||
}
|
||||
return sentences;
|
||||
}
|
||||
|
||||
public SyntaxTree getAdvancedOptionSyntaxTree() {
|
||||
NodeList advSentence = document.getElementsByTagName("advanced");
|
||||
advSentence = advSentence.item(0).getChildNodes();
|
||||
SyntaxTree s = new SyntaxTree(constructSyntaxNodeList(advSentence, new SyntaxNode("Root"), new SyntaxNodeList(), null, 1));
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
public SyntaxTree getSyntaxTree(String sentenceTitle) {
|
||||
NodeList result = null;
|
||||
NodeList nl = document.getElementsByTagName("sentence");
|
||||
boolean isAdvanced = false;
|
||||
String id = "";
|
||||
for (int i = 0; i < nl.getLength(); i++) {
|
||||
NamedNodeMap attr = nl.item(i).getAttributes();
|
||||
id = attr.getNamedItem("id").getNodeValue();
|
||||
if (nl.item(i).getNodeType() == Node.ELEMENT_NODE && sentenceTitle.equals(id)) {
|
||||
result = nl.item(i).getChildNodes();
|
||||
if (attr.getNamedItem("advanced") != null) isAdvanced = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
SyntaxTree s = buildSyntaxTree(result);
|
||||
s.setId(id);
|
||||
if (isAdvanced) {
|
||||
s.setAdvancedTree(getAdvancedOptionSyntaxTree());
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
private SyntaxTree buildSyntaxTree(NodeList currentRoot) {
|
||||
SyntaxTree s = new SyntaxTree(constructSyntaxNodeList(currentRoot, new SyntaxNode("Root"), new SyntaxNodeList(), null, 1));
|
||||
return s;
|
||||
}
|
||||
|
||||
/*
|
||||
"Abandon all hope, ye who enter here
|
||||
*/
|
||||
private SyntaxNode constructSyntaxNodeList(NodeList nl, SyntaxNode parent, SyntaxNodeList list, SyntaxNode nextSequence, int nbrOfArgs) {
|
||||
if (nl == null || nl.getLength() < 1) {
|
||||
if (nextSequence != null && !(parent.getData().isEmpty() && nextSequence.getSyntaxNodes().isEmpty())) {
|
||||
list.add(nextSequence);
|
||||
parent.getSyntaxNodes().add(list);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
int length = nl.getLength();
|
||||
|
||||
//CurrentArgs counts the number of arguments for the current NodeList
|
||||
int currentArgs = 0;
|
||||
|
||||
//If the parent node, or previous "recursion", calls for multiple args
|
||||
if (nbrOfArgs > 1) {
|
||||
currentArgs = nbrOfArgs;////Update currentArgs
|
||||
nbrOfArgs = 0;//Reset nbrOfArgs, important due to the other recursive calls which will happen before nbrOfArgs is actually used.
|
||||
}
|
||||
|
||||
int args = 0;
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
if (nl.item(i) != null && (nl.item(i).getNodeType() == Node.ELEMENT_NODE) && nl.item(i).getAttributes() != null) {
|
||||
String syntax = "", desc = "", option = "", question = "";
|
||||
|
||||
NamedNodeMap attributes = nl.item(i).getAttributes();
|
||||
if (attributes.getNamedItem("syntax") != null) {
|
||||
syntax = attributes.getNamedItem("syntax").getNodeValue();
|
||||
}
|
||||
|
||||
if (attributes.getNamedItem("desc") != null) {
|
||||
desc = attributes.getNamedItem("desc").getNodeValue();
|
||||
}
|
||||
|
||||
if (attributes.getNamedItem("args") != null) {
|
||||
args = Integer.parseInt(attributes.getNamedItem("args").getNodeValue());
|
||||
nbrOfArgs = args;
|
||||
}
|
||||
|
||||
if (attributes.getNamedItem("option") != null) {
|
||||
question = attributes.getNamedItem("option").getNodeValue();
|
||||
list.setQuestion(question);
|
||||
constructSyntaxNodeList(nl.item(i).getChildNodes(), parent, list, nextSequence, nbrOfArgs);
|
||||
}
|
||||
|
||||
if (attributes.getNamedItem("child") != null) {
|
||||
option = attributes.getNamedItem("child").getNodeValue();
|
||||
|
||||
SyntaxNode mNextSequence = new SyntaxNode("");//This is a "holder" node to bind the child function calls nodes, contains no useful information in its syntax.
|
||||
SyntaxNodeList mList = new SyntaxNodeList();
|
||||
constructSyntaxNodeList(nl.item(i).getChildNodes(), mNextSequence, mList, nextSequence, nbrOfArgs);
|
||||
|
||||
constructSyntaxNodeList(jumpToChild("child", option), parent, list, mNextSequence, nbrOfArgs);
|
||||
}
|
||||
if (!syntax.isEmpty()) {
|
||||
SyntaxNode node;
|
||||
|
||||
if (syntax.equals("NNumeral")) {
|
||||
node = new NumeralSyntaxNode();
|
||||
} else {
|
||||
node = new SyntaxNode(syntax);
|
||||
node.setDesc(desc);
|
||||
}
|
||||
|
||||
|
||||
list.add(node);
|
||||
SyntaxNodeList mList = new SyntaxNodeList();
|
||||
|
||||
constructSyntaxNodeList(nl.item(i).getChildNodes(), node, mList, nextSequence, nbrOfArgs);
|
||||
}
|
||||
|
||||
//Add the list to the current parent node list
|
||||
if (!list.getChildren().isEmpty() && !parent.getSyntaxNodes().contains(list)) {
|
||||
parent.getSyntaxNodes().add(list);
|
||||
}
|
||||
//Check if current node is multiple arg nodes i.e. add another list to its syntaxNodes.
|
||||
if (currentArgs > 1 && parent.getSyntaxNodes().size() < currentArgs) {
|
||||
list = new SyntaxNodeList();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return parent;
|
||||
|
||||
}
|
||||
|
||||
public NodeList jumpToChild(String tag, String id) {
|
||||
NodeList result = null;
|
||||
NodeList nl = document.getElementsByTagName(tag);
|
||||
|
||||
for (int i = 0; i < nl.getLength(); i++) {
|
||||
String s = nl.item(i).getFirstChild().getNodeValue();
|
||||
if (nl.item(i).getNodeType() == Node.ELEMENT_NODE && nl.item(i).getAttributes().getNamedItem("id").getNodeValue().equals(id)) {
|
||||
result = nl.item(i).getChildNodes();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package se.chalmers.phrasebook.backend.syntax;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class ChoiceContext {
|
||||
private int pos;
|
||||
private List<SyntacticChoice> choices;
|
||||
private List<SyntaxNode[]> stack;
|
||||
|
||||
public ChoiceContext() {
|
||||
this.pos = pos;
|
||||
this.choices = new ArrayList<SyntacticChoice>();
|
||||
this.stack = new ArrayList<SyntaxNode[]>();
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
pos = 0;
|
||||
stack.clear();
|
||||
}
|
||||
|
||||
public int choose(SyntaxNode node) {
|
||||
SyntacticChoice choice = null;
|
||||
|
||||
if (pos < choices.size()) {
|
||||
if (choices.get(pos).getNode().unlink() == node.unlink()) {
|
||||
choice = choices.get(pos);
|
||||
} else {
|
||||
while (pos < choices.size())
|
||||
choices.remove(choices.size()-1);
|
||||
}
|
||||
}
|
||||
|
||||
if (choice == null) {
|
||||
choice = new SyntacticChoice(node);
|
||||
choices.add(choice);
|
||||
}
|
||||
|
||||
pos++;
|
||||
return choice.getChoice();
|
||||
}
|
||||
|
||||
public void push(SyntaxNode[] args) {
|
||||
stack.add(args);
|
||||
}
|
||||
|
||||
public SyntaxNode[] pop() {
|
||||
return stack.remove(stack.size()-1);
|
||||
}
|
||||
|
||||
public SyntaxNode getArgument(int i) {
|
||||
return stack.get(stack.size()-1)[i];
|
||||
}
|
||||
|
||||
public List<SyntacticChoice> getChoices() {
|
||||
return choices;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package se.chalmers.phrasebook.backend.syntax;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class SyntacticChoice implements Serializable {
|
||||
private int choice;
|
||||
private SyntaxNode node;
|
||||
|
||||
public SyntacticChoice(SyntaxNode node) {
|
||||
this.node = node;
|
||||
this.choice = node.getDefaultChoice();
|
||||
}
|
||||
|
||||
public SyntaxNode getNode() {
|
||||
return node;
|
||||
}
|
||||
|
||||
public int getChoice() {
|
||||
return choice;
|
||||
}
|
||||
|
||||
public void setChoice(int choice) {
|
||||
this.choice = choice;
|
||||
}
|
||||
}
|
||||
@@ -1,66 +1,32 @@
|
||||
package se.chalmers.phrasebook.backend.syntax;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by Björn on 2016-03-03.
|
||||
*/
|
||||
|
||||
public class SyntaxNode implements Serializable {
|
||||
private String data;
|
||||
private String desc;
|
||||
private ArrayList<SyntaxNodeList> syntaxNodes;
|
||||
|
||||
public SyntaxNode(String data) {
|
||||
syntaxNodes = new ArrayList<SyntaxNodeList>();
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void setSelectedChild(int listIndex, SyntaxNode newChild) {
|
||||
syntaxNodes.get(listIndex).setSelectedChild(newChild);
|
||||
}
|
||||
|
||||
public ArrayList<SyntaxNodeList> getSyntaxNodes() {
|
||||
return syntaxNodes;
|
||||
}
|
||||
|
||||
public ArrayList<SyntaxNodeList> getModularSyntaxNodes() {
|
||||
ArrayList<SyntaxNodeList> result = new ArrayList<SyntaxNodeList>();
|
||||
for (SyntaxNodeList snl : syntaxNodes) {
|
||||
if (snl.getChildren().size() > 1) result.add(snl);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public boolean isModular() {
|
||||
for (SyntaxNodeList s : syntaxNodes) if (s.getChildren().size() > 1) return true;
|
||||
return false;
|
||||
public SyntaxNode(String desc) {
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
|
||||
public void setDesc(String desc) {
|
||||
this.desc = desc;
|
||||
}
|
||||
public String getAbstractSyntax(ChoiceContext context) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
public int getDefaultChoice() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof SyntaxNode)) {
|
||||
return false;
|
||||
}
|
||||
SyntaxNode n = (SyntaxNode) o;
|
||||
|
||||
return this.data.equals(n.data);
|
||||
}
|
||||
|
||||
}
|
||||
public SyntaxNode unlink() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package se.chalmers.phrasebook.backend.syntax;
|
||||
|
||||
public class SyntaxNodeArgument extends SyntaxNode {
|
||||
private int index;
|
||||
|
||||
public SyntaxNodeArgument(String desc, int index) {
|
||||
super(desc);
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAbstractSyntax(ChoiceContext context) {
|
||||
return context.getArgument(index).getAbstractSyntax(context);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package se.chalmers.phrasebook.backend.syntax;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class SyntaxNodeCall extends SyntaxNode {
|
||||
private String id;
|
||||
private SyntaxNode ref;
|
||||
private SyntaxNode[] arguments;
|
||||
|
||||
public SyntaxNodeCall(String desc, String id, SyntaxNode[] arguments) {
|
||||
super(desc);
|
||||
this.id = id;
|
||||
this.ref = null;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
String desc = super.getDesc();
|
||||
if (desc == null || desc.isEmpty())
|
||||
return ref.getDesc();
|
||||
return desc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAbstractSyntax(ChoiceContext context) {
|
||||
context.push(arguments);
|
||||
String res = ref.getAbstractSyntax(context);
|
||||
context.pop();
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SyntaxNode unlink() {
|
||||
return ref.unlink();
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public SyntaxNode getRef() {
|
||||
return ref;
|
||||
}
|
||||
|
||||
public void bind(Map<String,SyntaxNode> ids) {
|
||||
if (ref == null) {
|
||||
ref = ids.get(id);
|
||||
if (ref == null)
|
||||
throw new IllegalArgumentException("Missing reference to "+id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package se.chalmers.phrasebook.backend.syntax;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class SyntaxNodeFunction extends SyntaxNode {
|
||||
private String function;
|
||||
private SyntaxNode[] arguments;
|
||||
|
||||
public SyntaxNodeFunction(String desc, String function, SyntaxNode[] arguments) {
|
||||
super(desc);
|
||||
this.function = function;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
public String getFunction() {
|
||||
return function;
|
||||
}
|
||||
|
||||
public SyntaxNode[] getArguments() {
|
||||
return arguments;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAbstractSyntax(ChoiceContext context) {
|
||||
if (arguments == null || arguments.length == 0)
|
||||
return function;
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(function);
|
||||
for (SyntaxNode argument : arguments) {
|
||||
builder.append(' ');
|
||||
builder.append('(');
|
||||
builder.append(argument.getAbstractSyntax(context));
|
||||
builder.append(')');
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package se.chalmers.phrasebook.backend.syntax;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import se.chalmers.phrasebook.backend.syntax.SyntaxNode;
|
||||
|
||||
/**
|
||||
* Created by David on 2016-04-01.
|
||||
*/
|
||||
public class SyntaxNodeList implements Serializable {
|
||||
private SyntaxNode selectedChild;
|
||||
private ArrayList<SyntaxNode> children;
|
||||
private String question;
|
||||
|
||||
|
||||
public SyntaxNodeList() {
|
||||
children = new ArrayList<SyntaxNode>();
|
||||
}
|
||||
|
||||
public ArrayList<SyntaxNode> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public boolean add(SyntaxNode object) {
|
||||
if (selectedChild == null)
|
||||
selectedChild = object;
|
||||
return children.add(object);
|
||||
}
|
||||
|
||||
public SyntaxNode getSelectedChild() {
|
||||
return selectedChild;
|
||||
}
|
||||
|
||||
public boolean setSelectedChild(SyntaxNode selectedChild) {
|
||||
if(children.contains(selectedChild)) {
|
||||
this.selectedChild = selectedChild;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean selectChild(String description) {
|
||||
for (SyntaxNode s : children) {
|
||||
if (s.getDesc().equals(description)) {
|
||||
setSelectedChild(s);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public String getQuestion() {
|
||||
return question;
|
||||
}
|
||||
|
||||
public void setQuestion(String question) {
|
||||
this.question = question;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,54 +1,42 @@
|
||||
package se.chalmers.phrasebook.backend.syntax;
|
||||
|
||||
/**
|
||||
* Created by Björn on 2016-04-04.
|
||||
*/
|
||||
public class NumeralSyntaxNode extends SyntaxNode {
|
||||
public class SyntaxNodeNumeral extends SyntaxNode {
|
||||
private int min;
|
||||
private int max;
|
||||
|
||||
private int number = 1;
|
||||
|
||||
public NumeralSyntaxNode() {
|
||||
super("NNumeral");
|
||||
public SyntaxNodeNumeral(String desc, int min, int max) {
|
||||
super(desc);
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isModular() {
|
||||
return true;
|
||||
}
|
||||
public String getAbstractSyntax(ChoiceContext context) {
|
||||
return nbrToSyntax(context.choose(this));
|
||||
}
|
||||
|
||||
public String getData() {
|
||||
try {
|
||||
return nbrToSyntax(number);
|
||||
}catch(IllegalArgumentException e) {
|
||||
//Returns the syntax for "1" in case of erroneous input.
|
||||
return "(NNumeral(num (pot2as3 (pot1as2 (pot0as1 pot01)))))";
|
||||
}
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return Integer.toString(number);
|
||||
}
|
||||
|
||||
public void setDesc(String number) {
|
||||
this.number = Integer.parseInt(number);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setSelectedChild(int listIndex, SyntaxNode newChild) {
|
||||
setDesc(Integer.toString(listIndex));
|
||||
}
|
||||
@Override
|
||||
public int getDefaultChoice() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
public int getMin() {
|
||||
return min;
|
||||
}
|
||||
|
||||
public int getMax() {
|
||||
return max;
|
||||
}
|
||||
|
||||
private String nbrToSyntax(int nbr) throws IllegalArgumentException {
|
||||
String syntax = "";
|
||||
if(nbr < 1000000 && nbr > 0) {
|
||||
if (nbr <=999) {
|
||||
syntax = "(NNumeral(num(pot2as3 " + subs1000(nbr) + ")))";
|
||||
syntax = "(num (pot2as3 " + subs1000(nbr) + "))";
|
||||
} else if(nbr % 1000 == 0) {
|
||||
syntax = "(NNumeral(num(pot3 " + subs1000(nbr/1000) + ")))";
|
||||
syntax = "(num (pot3 " + subs1000(nbr/1000) + "))";
|
||||
} else if(nbr > 1000 && nbr%1000 != 0) {
|
||||
syntax = "(NNumeral(num(pot3plus " + subs1000(nbr/1000) + " " +
|
||||
subs1000(nbr%1000) + ")))";
|
||||
syntax = "(num (pot3plus " + subs1000(nbr/1000) + " " +
|
||||
subs1000(nbr%1000) + "))";
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("Input must be between 1 and 999999");
|
||||
@@ -93,12 +81,4 @@ public class NumeralSyntaxNode extends SyntaxNode {
|
||||
}
|
||||
return syntax;
|
||||
}
|
||||
|
||||
public int getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void setNumber(int number) {
|
||||
this.number = number;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package se.chalmers.phrasebook.backend.syntax;
|
||||
|
||||
import java.util.*;
|
||||
import se.chalmers.phrasebook.backend.*;
|
||||
|
||||
public class SyntaxNodeOption extends SyntaxNode {
|
||||
private SyntaxNode[] options;
|
||||
|
||||
public SyntaxNodeOption(String desc, SyntaxNode[] options) {
|
||||
super(desc);
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
public SyntaxNode[] getOptions() {
|
||||
return options;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAbstractSyntax(ChoiceContext context) {
|
||||
return options[context.choose(this)].getAbstractSyntax(context);
|
||||
}
|
||||
}
|
||||
@@ -1,224 +1,31 @@
|
||||
package se.chalmers.phrasebook.backend.syntax;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import org.grammaticalframework.pgf.Expr;
|
||||
|
||||
/**
|
||||
* Created by Björn on 2016-02-26.
|
||||
*/
|
||||
public class SyntaxTree implements Serializable {
|
||||
|
||||
private String id;
|
||||
private String desc;
|
||||
private SyntaxNode root;
|
||||
private boolean advActivated;
|
||||
private boolean favorite;
|
||||
|
||||
private ArrayList<SyntaxNodeList> options = new ArrayList<SyntaxNodeList>();
|
||||
|
||||
private SyntaxTree advancedTree; //Realy ugly solution but the only one we can think of as
|
||||
|
||||
public SyntaxTree getAdvancedTree() {
|
||||
return advancedTree;
|
||||
}
|
||||
|
||||
public void setAdvancedTree(SyntaxTree advancedTree) {
|
||||
this.advancedTree = advancedTree;
|
||||
}
|
||||
|
||||
public SyntaxTree(SyntaxNode root) {
|
||||
public SyntaxTree(String desc, SyntaxNode root) {
|
||||
this.desc = desc;
|
||||
this.root = root;
|
||||
advActivated = false;
|
||||
initializeOptions(this.root);
|
||||
}
|
||||
|
||||
public ArrayList<SyntaxNodeList> getOptions() {
|
||||
return options;
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
|
||||
public ArrayList<SyntaxNodeList> getAdvOptions() {
|
||||
if(advancedTree != null) {
|
||||
return advancedTree.getOptions();
|
||||
}
|
||||
return new ArrayList<SyntaxNodeList>();
|
||||
}
|
||||
|
||||
public boolean getFavorite() {
|
||||
return favorite;
|
||||
}
|
||||
|
||||
public void setFavorite(boolean fav) {
|
||||
favorite = fav;
|
||||
}
|
||||
public boolean replicate(SyntaxTree tree) {
|
||||
if(this.getOptions() == null) {
|
||||
return false;
|
||||
} else {
|
||||
for(int i = 0; i < getOptions().size(); i++) {
|
||||
for(SyntaxNode n: getOptions().get(i).getChildren()) {
|
||||
|
||||
if(n.getData().equals(tree.getOptions().get(i).getSelectedChild().getData())) {
|
||||
this.setSelectedChild(getOptions().get(i), n);
|
||||
} else if(n instanceof NumeralSyntaxNode) {
|
||||
((NumeralSyntaxNode) n).setDesc
|
||||
(Integer.toString(((NumeralSyntaxNode)tree.getOptions()
|
||||
.get(i).getSelectedChild()).getNumber()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
tree.setFavorite(getFavorite());
|
||||
if(advancedTree != null) {
|
||||
if(tree.advActivated) {
|
||||
setAdvActivated(true);
|
||||
return advancedTree.replicate(tree.getAdvancedTree());
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
//creates an ArrayList of LinkedHashMaps, each representing
|
||||
//a currently available option to be customized.
|
||||
private void initializeOptions(SyntaxNode currentRoot) {
|
||||
if (currentRoot == null) return;
|
||||
if (currentRoot.isModular()) {
|
||||
for (SyntaxNodeList l : currentRoot.getSyntaxNodes()) {
|
||||
if (l.getQuestion() != null && !options.contains(l)) {
|
||||
options.add(l);
|
||||
}
|
||||
initializeOptions(l.getSelectedChild());
|
||||
}
|
||||
} else if (currentRoot.getSyntaxNodes() != null && currentRoot.getSyntaxNodes().size() > 0) {
|
||||
for (SyntaxNodeList n : currentRoot.getSyntaxNodes()) {
|
||||
initializeOptions(n.getSelectedChild());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public boolean setSelectedChild(int optionIndex, int childIndex, boolean isAdvanced) {
|
||||
boolean status = false;
|
||||
if(!isAdvanced) {
|
||||
if (options.get(optionIndex).getSelectedChild() instanceof NumeralSyntaxNode) {
|
||||
options.get(optionIndex).getSelectedChild().setSelectedChild(childIndex, null);
|
||||
status = true;
|
||||
} else if (options.get(optionIndex) != null) {
|
||||
if (options.get(optionIndex).getChildren().get(childIndex) != null)
|
||||
status = options.get(optionIndex).setSelectedChild(options.get(optionIndex).getChildren().get(childIndex));
|
||||
}
|
||||
} else {
|
||||
advancedTree.setSelectedChild(optionIndex,childIndex,false);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
public void setSelectedChild(int optionIndex, SyntaxNodeList snl, int childIndex, boolean isAdvanced) {
|
||||
if(!isAdvanced) {
|
||||
if (snl != null) {
|
||||
SyntaxNodeList nodeList = options.get(optionIndex);
|
||||
setRecursiveSelectedChild(nodeList, snl, childIndex);
|
||||
} else {
|
||||
setSelectedChild(optionIndex, childIndex,false);
|
||||
}
|
||||
} else {
|
||||
advancedTree.setSelectedChild(optionIndex,snl,childIndex,false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void setRecursiveSelectedChild(SyntaxNodeList nodeList, SyntaxNodeList optionTarget, int childIndex) {
|
||||
|
||||
if (nodeList.equals(optionTarget)) {
|
||||
setSelectedChild(nodeList, nodeList.getChildren().get(childIndex));
|
||||
} else {
|
||||
for (SyntaxNodeList list : nodeList.getSelectedChild().getSyntaxNodes())
|
||||
setRecursiveSelectedChild(list, optionTarget, childIndex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public boolean setSelectedChild(SyntaxNodeList l, SyntaxNode s) {
|
||||
return l.setSelectedChild(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the selected children into a text syntax usable by the grammar to
|
||||
* generate a translation. Builds recursivly.
|
||||
*
|
||||
* @return The syntax usable by the GF-grammar to generate a translation
|
||||
*/
|
||||
public String getSyntax() {
|
||||
return parseSentenceSyntax(getSentenceHead());
|
||||
}
|
||||
|
||||
//TODO A really ugly hack, should DEFINITIVELY BE FIXED
|
||||
public Expr getAdvSyntax() {
|
||||
String syntax = getSyntax();
|
||||
if(advActivated) {
|
||||
String advSyntax = advancedTree.getSyntax();
|
||||
if(!advSyntax.isEmpty()) {
|
||||
String test = advSyntax.substring(0, advSyntax.indexOf("AKnow") + 5) +
|
||||
syntax.substring(1, 9) + "(" +
|
||||
advSyntax.substring(advSyntax.indexOf("AKnow") + 6, advSyntax.length() - 3) +
|
||||
syntax.substring(9, syntax.length()) + ")))";
|
||||
return Expr.readExpr(test);
|
||||
}
|
||||
}
|
||||
return Expr.readExpr(syntax);
|
||||
}
|
||||
|
||||
public boolean hasAdvOptions() {
|
||||
return (advancedTree != null);
|
||||
}
|
||||
|
||||
// Builds recursively from root node to parse syntax
|
||||
//the getSyntax() method acts as a wrapper
|
||||
private String parseSentenceSyntax(SyntaxNode node) {
|
||||
|
||||
if (node.getSyntaxNodes().size() < 1) {
|
||||
return node.getData();
|
||||
} else {
|
||||
String syntax = node.getData();
|
||||
for (int i = 0; i < node.getSyntaxNodes().size(); i++) {
|
||||
if (node.getSyntaxNodes().get(i).getSelectedChild().getData().isEmpty()) {
|
||||
syntax += parseSentenceSyntax(node.getSyntaxNodes().get(i).getSelectedChild());
|
||||
} else {
|
||||
syntax = syntax + "(" + parseSentenceSyntax(node.getSyntaxNodes().get(i).getSelectedChild()) + ")";
|
||||
if (node.getSyntaxNodes().size() > 1) {
|
||||
syntax += " ";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return syntax;
|
||||
}
|
||||
}
|
||||
|
||||
private SyntaxNode getSentenceHead() {
|
||||
if (root.getSyntaxNodes().get(0) != null)
|
||||
return root.getSyntaxNodes().get(0).getSelectedChild();//TODO Might cause bugs
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isAdvActivated(){
|
||||
return advActivated;
|
||||
}
|
||||
|
||||
public void setAdvActivated(boolean activated){
|
||||
advActivated = activated;
|
||||
|
||||
public SyntaxNode getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
public Expr getAbstractSyntax(ChoiceContext choices) {
|
||||
return Expr.readExpr(root.getAbstractSyntax(choices));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package se.chalmers.phrasebook.gui;
|
||||
|
||||
import se.chalmers.phrasebook.backend.syntax.SyntaxNodeList;
|
||||
|
||||
/**
|
||||
* An interface for communication between a fragment to an activity
|
||||
* Created by David on 2016-04-13.
|
||||
*/
|
||||
public interface FragmentCommunicator {
|
||||
void updateSyntax(int optionIndex, SyntaxNodeList l, int childIndex, boolean isAdvanced);
|
||||
|
||||
void pageChanged();
|
||||
|
||||
void setToTranslationFragment(int id);
|
||||
|
||||
void updateTranslation();
|
||||
}
|
||||
@@ -16,13 +16,11 @@ import android.support.v4.widget.DrawerLayout;
|
||||
|
||||
import org.grammaticalframework.ui.android.R;
|
||||
import se.chalmers.phrasebook.backend.Model;
|
||||
import se.chalmers.phrasebook.backend.syntax.SyntaxNodeList;
|
||||
import se.chalmers.phrasebook.gui.FragmentCommunicator;
|
||||
import se.chalmers.phrasebook.gui.fragments.NumeralTranslatorFragment;
|
||||
import se.chalmers.phrasebook.backend.syntax.*;
|
||||
import se.chalmers.phrasebook.gui.fragments.PhraseListFragment;
|
||||
import se.chalmers.phrasebook.gui.fragments.TranslatorFragment;
|
||||
|
||||
public class NavigationActivity extends FragmentActivity implements FragmentCommunicator {
|
||||
public class NavigationActivity extends FragmentActivity {
|
||||
|
||||
/**
|
||||
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
|
||||
@@ -30,11 +28,6 @@ public class NavigationActivity extends FragmentActivity implements FragmentComm
|
||||
private CharSequence mTitle;
|
||||
private Fragment mContent;
|
||||
|
||||
public void pageChanged() {
|
||||
if (mContent instanceof TranslatorFragment)
|
||||
((TranslatorFragment) mContent).displayDots();
|
||||
}
|
||||
|
||||
private Model mModel;
|
||||
|
||||
@Override
|
||||
@@ -80,21 +73,7 @@ public class NavigationActivity extends FragmentActivity implements FragmentComm
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateSyntax(int optionIndex, SyntaxNodeList l, int childIndex, boolean isAdvanced) {
|
||||
mModel.update(optionIndex, l, childIndex, isAdvanced);
|
||||
updateTranslation();
|
||||
}
|
||||
|
||||
public void updateTranslation() {
|
||||
if (mContent instanceof TranslatorFragment) {
|
||||
TranslatorFragment fragment = (TranslatorFragment) mContent;
|
||||
fragment.updateTranslation();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setToTranslationFragment(int id) {
|
||||
switchContent(TranslatorFragment.newInstance(id + ""), "");
|
||||
public void setToTranslationFragment(SyntaxTree phrase) {
|
||||
switchContent(TranslatorFragment.newInstance(phrase), "");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
package se.chalmers.phrasebook.gui.adapters;
|
||||
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentPagerAdapter;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import se.chalmers.phrasebook.backend.Model;
|
||||
import se.chalmers.phrasebook.gui.FragmentCommunicator;
|
||||
import se.chalmers.phrasebook.gui.smallFragments.OptionsFragment;
|
||||
|
||||
/**
|
||||
* Created by matilda on 15/03/16.
|
||||
*/
|
||||
public class SwipeAdapter extends FragmentPagerAdapter {
|
||||
|
||||
private int pages;
|
||||
private FragmentCommunicator mCallback;
|
||||
private Model model;
|
||||
|
||||
public SwipeAdapter(FragmentManager fragmentManager, FragmentCommunicator mCallback) {
|
||||
super(fragmentManager);
|
||||
this.mCallback = mCallback;
|
||||
model = Model.getInstance();
|
||||
if(model.getCurrentPhrase().hasAdvOptions()) {
|
||||
pages = 2;
|
||||
} else {
|
||||
pages = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Returns total number of pages
|
||||
@Override
|
||||
public int getCount() {
|
||||
return pages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startUpdate(ViewGroup container) {
|
||||
mCallback.pageChanged();
|
||||
}
|
||||
|
||||
// Returns the fragment to display for that page
|
||||
@Override
|
||||
public Fragment getItem(int position) {
|
||||
|
||||
switch(position){
|
||||
case 0:
|
||||
return OptionsFragment.newInstance(1, false);
|
||||
case 1:
|
||||
return OptionsFragment.newInstance(2, model.getCurrentPhrase().isAdvActivated());
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package se.chalmers.phrasebook.gui.fragments;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import se.chalmers.phrasebook.backend.Model;
|
||||
|
||||
/**
|
||||
* Created by Björn on 2016-04-25.
|
||||
*/
|
||||
public class NumeralTranslatorFragment extends TranslatorFragment {
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
model = Model.getInstance();
|
||||
}
|
||||
|
||||
public static NumeralTranslatorFragment newInstance() {
|
||||
NumeralTranslatorFragment fragment = new NumeralTranslatorFragment();
|
||||
Bundle args = new Bundle();
|
||||
args.putString("phrase", "NNumeral");
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceStat) {
|
||||
View view = super.onCreateView(inflater, container,
|
||||
savedInstanceStat);
|
||||
super.model.setNumeralCurrentPhrase();
|
||||
return view;
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import android.os.Bundle;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
@@ -22,7 +21,6 @@ import java.util.ArrayList;
|
||||
import org.grammaticalframework.ui.android.R;
|
||||
import se.chalmers.phrasebook.backend.Model;
|
||||
import se.chalmers.phrasebook.backend.syntax.SyntaxTree;
|
||||
import se.chalmers.phrasebook.gui.FragmentCommunicator;
|
||||
import se.chalmers.phrasebook.gui.activities.NavigationActivity;
|
||||
|
||||
|
||||
@@ -32,12 +30,8 @@ import se.chalmers.phrasebook.gui.activities.NavigationActivity;
|
||||
public class PhraseListFragment extends Fragment {
|
||||
|
||||
protected Model model;
|
||||
protected ArrayList<String> phrases;
|
||||
Context context;
|
||||
private String title;
|
||||
|
||||
private FragmentCommunicator mCallback;
|
||||
|
||||
public static PhraseListFragment newInstance(String title) {
|
||||
PhraseListFragment fragment = new PhraseListFragment();
|
||||
Bundle args = new Bundle();
|
||||
@@ -46,37 +40,12 @@ public class PhraseListFragment extends Fragment {
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Activity activity) {
|
||||
super.onAttach(activity);
|
||||
|
||||
// This makes sure that the container activity has implemented
|
||||
// the callback interface. If not, it throws an exception
|
||||
try {
|
||||
mCallback = (FragmentCommunicator) activity;
|
||||
} catch (ClassCastException e) {
|
||||
throw new ClassCastException(activity.toString()
|
||||
+ " must implement OnHeadlineSelectedListener");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
model = Model.getInstance();
|
||||
context = getActivity().getApplicationContext();
|
||||
|
||||
title = getArguments().getString("title");
|
||||
phrases = new ArrayList<String>();
|
||||
phrases.addAll(model.getSentencesInCurrentPhrasebook());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
phrases.clear();
|
||||
phrases.addAll(model.getSentencesInCurrentPhrasebook());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -85,7 +54,8 @@ public class PhraseListFragment extends Fragment {
|
||||
|
||||
View view = inflater.inflate(R.layout.fragment_phrase_list, container, false);
|
||||
getActivity().getActionBar().setTitle(title);
|
||||
ArrayAdapter adapter = new ArrayAdapter<String>(context, R.layout.phrase_list_item, phrases);
|
||||
|
||||
ArrayAdapter<SyntaxTree> adapter = new ArrayAdapter<SyntaxTree>(getActivity(), R.layout.phrase_list_item, model.getSentences());
|
||||
|
||||
final ListView phraseListView = (ListView) view.findViewById(R.id.phrase_listView);
|
||||
phraseListView.setAdapter(adapter);
|
||||
@@ -93,19 +63,12 @@ public class PhraseListFragment extends Fragment {
|
||||
phraseListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
|
||||
model.setCurrentPhrase(position);
|
||||
((NavigationActivity)mCallback).getActionBar().setTitle(model.getDescFromPos(position));
|
||||
sendMessage(position);
|
||||
SyntaxTree phrase = model.getSentences().get(position);
|
||||
getActivity().getActionBar().setTitle(phrase.getDesc());
|
||||
((NavigationActivity) getActivity()).setToTranslationFragment(phrase);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
|
||||
private void sendMessage(int position) {
|
||||
mCallback.setToTranslationFragment(position);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,81 +1,226 @@
|
||||
package se.chalmers.phrasebook.gui.fragments;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.content.Context;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.view.*;
|
||||
import android.widget.*;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.text.method.PasswordTransformationMethod;
|
||||
|
||||
import org.grammaticalframework.pgf.Expr;
|
||||
import org.grammaticalframework.ui.android.Translator;
|
||||
import org.grammaticalframework.ui.android.GFTranslator;
|
||||
import org.grammaticalframework.ui.android.TTS;
|
||||
import org.grammaticalframework.ui.android.R;
|
||||
import se.chalmers.phrasebook.backend.Model;
|
||||
import se.chalmers.phrasebook.gui.smallFragments.SwipeFragment;
|
||||
import se.chalmers.phrasebook.gui.smallFragments.TranslationFragment;
|
||||
import se.chalmers.phrasebook.backend.*;
|
||||
import se.chalmers.phrasebook.backend.syntax.*;
|
||||
|
||||
/**
|
||||
* Created by matilda on 04/04/16.
|
||||
*/
|
||||
public class TranslatorFragment extends Fragment {
|
||||
protected Model model;
|
||||
private SwipeFragment swiper;
|
||||
private View view;
|
||||
Translator mTranslator;
|
||||
private TTS mTts;
|
||||
|
||||
public static TranslatorFragment newInstance(String phrase) {
|
||||
TranslatorFragment fragment = new TranslatorFragment();
|
||||
private TextView origin,target;
|
||||
private ListView list;
|
||||
SyntaxTree phrase;
|
||||
|
||||
ChoiceContext mContext;
|
||||
ArrayAdapter<SyntacticChoice> mAdapter;
|
||||
|
||||
public static TranslatorFragment newInstance(SyntaxTree phrase) {
|
||||
TranslatorFragment translatorFragment = new TranslatorFragment();
|
||||
Bundle args = new Bundle();
|
||||
args.putString("phrase", phrase);
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
args.putSerializable("phrase", phrase);
|
||||
translatorFragment.setArguments(args);
|
||||
return translatorFragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
phrase = (SyntaxTree) getArguments().getSerializable("phrase");
|
||||
model = Model.getInstance();
|
||||
}
|
||||
mTranslator = ((GFTranslator) getContext().getApplicationContext()).getTranslator();
|
||||
mTts = new TTS(getActivity());
|
||||
mContext = new ChoiceContext();
|
||||
|
||||
|
||||
mAdapter =
|
||||
new ArrayAdapter<SyntacticChoice>(getActivity(), R.layout.spinner_input_list_item, mContext.getChoices()) {
|
||||
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
|
||||
public void displayDots() {
|
||||
if(swiper.isAdvanced(view.findViewById(R.id.containerfor_options))) {
|
||||
((ImageView)view.findViewById(R.id.firstDot))
|
||||
.setImageResource(R.drawable.ic_dictionary);
|
||||
((ImageView)view.findViewById(R.id.secondDot))
|
||||
.setImageResource(R.drawable.ic_dictionary);
|
||||
} else {
|
||||
((ImageView)view.findViewById(R.id.secondDot))
|
||||
.setImageResource(R.drawable.ic_dictionary);
|
||||
((ImageView)view.findViewById(R.id.firstDot))
|
||||
.setImageResource(R.drawable.ic_dictionary);
|
||||
}
|
||||
@Override
|
||||
public View getView (int position, View convertView, ViewGroup parent) {
|
||||
SyntacticChoice choice = mContext.getChoices().get(position);
|
||||
View view = null;
|
||||
if (choice.getNode() instanceof SyntaxNodeOption) {
|
||||
view = createSpinnerInputView(inflater, choice, (SyntaxNodeOption) choice.getNode(), parent);
|
||||
} else if (choice.getNode() instanceof SyntaxNodeNumeral) {
|
||||
view = createNumeralInputView(inflater, choice, (SyntaxNodeNumeral) choice.getNode(), parent);
|
||||
}
|
||||
return view;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.fragment_translator, container, false);
|
||||
origin = (TextView) view.findViewById(R.id.origin_phrase);
|
||||
target = (TextView) view.findViewById(R.id.target_phrase);
|
||||
list = (ListView) view.findViewById(R.id.input_holder);
|
||||
list.setAdapter(mAdapter);
|
||||
|
||||
view = inflater.inflate(R.layout.fragment_translator, container, false);
|
||||
|
||||
final FragmentTransaction fm = getChildFragmentManager().beginTransaction();
|
||||
|
||||
swiper = new SwipeFragment();
|
||||
fm.replace(R.id.containerfor_translation, new TranslationFragment());
|
||||
fm.replace(R.id.containerfor_options, new SwipeFragment());
|
||||
|
||||
fm.commit();
|
||||
|
||||
if(!model.getCurrentPhrase().hasAdvOptions()) {
|
||||
view.findViewById(R.id.firstDot).setVisibility(View.GONE);
|
||||
view.findViewById(R.id.secondDot).setVisibility(View.GONE);
|
||||
}
|
||||
ImageView button = (ImageView) view.findViewById(R.id.button3);
|
||||
button.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
mTts.speak(mTranslator.getTargetLanguage().getLangCode(), (String) target.getText());
|
||||
}
|
||||
});
|
||||
|
||||
updateSyntax();
|
||||
return view;
|
||||
}
|
||||
|
||||
private View createSpinnerInputView(LayoutInflater inflater, final SyntacticChoice choice, final SyntaxNodeOption options, ViewGroup parent) {
|
||||
View view = inflater.inflate(R.layout.spinner_input_list_item, parent, false);
|
||||
TextView viewLabel = (TextView) view.findViewById(R.id.text_view_spinner);
|
||||
Spinner spinner = (Spinner) view.findViewById(R.id.choice_spinner);
|
||||
|
||||
public void updateTranslation() {
|
||||
TranslationFragment translationFragment = (TranslationFragment) getChildFragmentManager().findFragmentById(R.id.containerfor_translation);
|
||||
if (translationFragment != null)
|
||||
translationFragment.updateData();
|
||||
String label = options.getDesc();
|
||||
if (label == null || label.isEmpty()) {
|
||||
viewLabel.setVisibility(View.GONE);
|
||||
} else {
|
||||
viewLabel.setText(label);
|
||||
}
|
||||
|
||||
final ArrayAdapter<SyntaxNode> adapter = new ArrayAdapter<SyntaxNode>(getActivity(), android.R.layout.simple_list_item_1,options.getOptions());
|
||||
adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
|
||||
spinner.setAdapter(adapter);
|
||||
|
||||
spinner.setSelection(choice.getChoice());
|
||||
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
||||
|
||||
@Override
|
||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||
if (position != choice.getChoice()) {
|
||||
choice.setChoice(position);
|
||||
updateSyntax();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNothingSelected(AdapterView<?> parent) {
|
||||
}
|
||||
});
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
private class NumericKeyBoardTransformationMethod extends PasswordTransformationMethod {
|
||||
@Override
|
||||
public CharSequence getTransformation(CharSequence source, View view) {
|
||||
return source;
|
||||
}
|
||||
}
|
||||
|
||||
private View createNumeralInputView(LayoutInflater inflater, final SyntacticChoice choice, final SyntaxNodeNumeral numeral, ViewGroup parent) {
|
||||
View view = inflater.inflate(R.layout.number_input_list_item, parent, false);
|
||||
TextView viewLabel = (TextView) view.findViewById(R.id.textView_number);
|
||||
final SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekBar);
|
||||
final EditText editNumber = (EditText) view.findViewById(R.id.editNumber);
|
||||
|
||||
String label = choice.getNode().getDesc();
|
||||
if (label == null || label.isEmpty()) {
|
||||
viewLabel.setVisibility(View.GONE);
|
||||
} else {
|
||||
viewLabel.setText(label);
|
||||
}
|
||||
|
||||
seekBar.setProgress(choice.getChoice());
|
||||
editNumber.setText(Integer.toString(choice.getChoice()));
|
||||
|
||||
editNumber.setTransformationMethod(new NumericKeyBoardTransformationMethod());
|
||||
editNumber.requestFocus();
|
||||
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
|
||||
@Override
|
||||
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||
editNumber.setText(Integer.toString(progress+1));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartTrackingTouch(SeekBar seekBar) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStopTrackingTouch(SeekBar seekBar) {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
editNumber.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
int number;
|
||||
boolean update = false;
|
||||
try {
|
||||
if(editNumber.getText().toString().equals("")) {
|
||||
number = numeral.getMin();
|
||||
} else {
|
||||
number = Integer.parseInt(editNumber.getText().toString());
|
||||
if (number < numeral.getMin()) {
|
||||
number = numeral.getMin();
|
||||
update = true;
|
||||
}
|
||||
if (number > numeral.getMax()) {
|
||||
number = numeral.getMax();
|
||||
update = true;
|
||||
}
|
||||
editNumber.setInputType(0);
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
number = choice.getChoice();
|
||||
update = true;
|
||||
}
|
||||
choice.setChoice(number);
|
||||
if (update)
|
||||
editNumber.setText(Integer.toString(number));
|
||||
seekBar.setProgress(number-numeral.getMin());
|
||||
updateSyntax();
|
||||
}
|
||||
});
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
public void updateSyntax() {
|
||||
mContext.reset();
|
||||
|
||||
Expr expr = phrase.getAbstractSyntax(mContext);
|
||||
origin.setText(mTranslator.linearizeSource(expr));
|
||||
target.setText(mTranslator.linearize(expr));
|
||||
mAdapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
package se.chalmers.phrasebook.gui.smallFragments;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.CheckBox;
|
||||
|
||||
import org.grammaticalframework.ui.android.R;
|
||||
import se.chalmers.phrasebook.backend.Model;
|
||||
import se.chalmers.phrasebook.gui.FragmentCommunicator;
|
||||
import se.chalmers.phrasebook.gui.fragments.TranslatorFragment;
|
||||
|
||||
|
||||
/**
|
||||
* Created by matilda on 02/05/16.
|
||||
*/
|
||||
public class AdvancedOptionsButtonFragment extends Fragment{
|
||||
|
||||
private boolean active;
|
||||
private Model model;
|
||||
private FragmentCommunicator mCallback;
|
||||
|
||||
public static Fragment newInstance(boolean active) {
|
||||
AdvancedOptionsButtonFragment advancedOptionsButtonFragment = new AdvancedOptionsButtonFragment();
|
||||
|
||||
|
||||
Bundle args = new Bundle();
|
||||
args.putBoolean("active", active);
|
||||
advancedOptionsButtonFragment.setArguments(args);
|
||||
|
||||
return advancedOptionsButtonFragment;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
model = Model.getInstance();
|
||||
this.active = getArguments().getBoolean("active");
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.advanced_options_button, container, false);
|
||||
|
||||
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox);
|
||||
|
||||
checkBox.setText("Use reported speech ('I Know that...')");
|
||||
|
||||
if(active != true){
|
||||
checkBox.setChecked(false);
|
||||
} else {
|
||||
checkBox.setChecked(true);
|
||||
}
|
||||
|
||||
checkBox.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (((CheckBox) v).isChecked()) {
|
||||
model.getCurrentPhrase().setAdvActivated(true);
|
||||
((OptionsFragment)getParentFragment()).update(true);
|
||||
} else {
|
||||
model.getCurrentPhrase().setAdvActivated(false);
|
||||
((OptionsFragment)getParentFragment()).update(false);
|
||||
}
|
||||
mCallback.updateTranslation();
|
||||
}
|
||||
});
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Activity activity) {
|
||||
super.onAttach(activity);
|
||||
// This makes sure that the container activity has implemented
|
||||
// the callback interface. If not, it throws an exception
|
||||
try {
|
||||
mCallback = (FragmentCommunicator) activity;
|
||||
} catch (ClassCastException e) {
|
||||
throw new ClassCastException(activity.toString()
|
||||
+ " must implement OnHeadlineSelectedListener");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,167 +0,0 @@
|
||||
package se.chalmers.phrasebook.gui.smallFragments;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.grammaticalframework.ui.android.R;
|
||||
import se.chalmers.phrasebook.backend.Model;
|
||||
import se.chalmers.phrasebook.backend.syntax.NumeralSyntaxNode;
|
||||
import se.chalmers.phrasebook.backend.syntax.SyntaxNodeList;
|
||||
import se.chalmers.phrasebook.gui.FragmentCommunicator;
|
||||
|
||||
/**
|
||||
* Created by David on 2016-04-13.
|
||||
*/
|
||||
|
||||
|
||||
public class InputHolderFragment extends Fragment {
|
||||
private Model model;
|
||||
|
||||
private int optionIndex;
|
||||
private SyntaxNodeList guiOptions;
|
||||
private FragmentManager fragmentManager;
|
||||
private ArrayList<String> fragmentTags;
|
||||
private boolean isAdvanced;
|
||||
|
||||
private FragmentCommunicator mCallback;
|
||||
|
||||
public static Fragment newInstance(int optionIndex, boolean isAdvanced) {
|
||||
InputHolderFragment inputHolderFragment = new InputHolderFragment();
|
||||
|
||||
Bundle args = new Bundle();
|
||||
args.putInt("index", optionIndex);
|
||||
args.putBoolean("advanced", isAdvanced);
|
||||
inputHolderFragment.setArguments(args);
|
||||
|
||||
return inputHolderFragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Activity activity) {
|
||||
super.onAttach(activity);
|
||||
|
||||
// This makes sure that the container activity has implemented
|
||||
// the callback interface. If not, it throws an exception
|
||||
try {
|
||||
mCallback = (FragmentCommunicator) activity;
|
||||
} catch (ClassCastException e) {
|
||||
throw new ClassCastException(activity.toString()
|
||||
+ " must implement OnHeadlineSelectedListener");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
this.isAdvanced = getArguments().getBoolean("advanced");
|
||||
model = Model.getInstance();
|
||||
optionIndex = getArguments().getInt("index");
|
||||
fragmentManager = getChildFragmentManager();
|
||||
if (isAdvanced) {
|
||||
guiOptions = model.getCurrentPhrase().getAdvOptions().get(optionIndex);
|
||||
} else {
|
||||
guiOptions = model.getCurrentPhrase().getOptions().get(optionIndex);
|
||||
}
|
||||
fragmentTags = new ArrayList<String>();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.small_fragment_input_holder, container, false);
|
||||
TextView textView = (TextView) view.findViewById(R.id.holderOptionText);
|
||||
|
||||
textView.setText(guiOptions.getQuestion());
|
||||
addInputFragments(guiOptions);
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
private void addListInputFragment(SyntaxNodeList l) {
|
||||
FragmentTransaction transaction = fragmentManager.beginTransaction();
|
||||
String s = "Input: " + l.toString();
|
||||
|
||||
transaction.add(R.id.input_holder, SpinnerInputFragment.newInstance(optionIndex, null, l), s);
|
||||
fragmentTags.add(s);
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
private void addNumberInputFragment(SyntaxNodeList l) {
|
||||
FragmentTransaction transaction = fragmentManager.beginTransaction();
|
||||
String s = "Input: " + l.toString();
|
||||
|
||||
int defaultIntValue = 0;
|
||||
// String title = l.getQuestion();
|
||||
String title = "";
|
||||
NumeralSyntaxNode nsn = (NumeralSyntaxNode) l.getSelectedChild();
|
||||
defaultIntValue = nsn.getNumber();
|
||||
|
||||
transaction.replace(R.id.input_holder, NumberInputFragment.newInstance(optionIndex, title, defaultIntValue), s);
|
||||
|
||||
fragmentTags.add(s);
|
||||
transaction.commit();
|
||||
|
||||
}
|
||||
|
||||
private void redrawInputGUI() {
|
||||
clearInputs();
|
||||
addInputFragments(guiOptions);
|
||||
}
|
||||
|
||||
private void addInputFragments(SyntaxNodeList snl) {
|
||||
if (snl != null) {
|
||||
|
||||
if (snl.getSelectedChild() instanceof NumeralSyntaxNode) {
|
||||
addNumberInputFragment(snl);
|
||||
} else {
|
||||
addListInputFragment(snl);
|
||||
}
|
||||
|
||||
|
||||
if (snl.getSelectedChild().isModular()) {
|
||||
ArrayList<SyntaxNodeList> modularLists = snl.getSelectedChild().getModularSyntaxNodes();
|
||||
for (SyntaxNodeList nodeList : modularLists)
|
||||
addInputFragments(nodeList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void clearInputs() {
|
||||
FragmentTransaction transaction = fragmentManager.beginTransaction();
|
||||
for (String tag : fragmentTags) {
|
||||
Fragment fragment = fragmentManager.findFragmentByTag(tag);
|
||||
transaction.remove(fragment);
|
||||
}
|
||||
fragmentTags.clear();
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
|
||||
public void updateSyntax(int optionIndex, SyntaxNodeList l, int childIndex) {
|
||||
if (this.optionIndex == optionIndex) {
|
||||
mCallback.updateSyntax(optionIndex, l, childIndex, isAdvanced);
|
||||
this.redrawInputGUI();
|
||||
}
|
||||
}
|
||||
|
||||
public void updateNumeralSyntax(int optionIndex, int childIndex) {
|
||||
|
||||
if (this.optionIndex == optionIndex) {
|
||||
mCallback.updateSyntax(optionIndex, null, childIndex, false);
|
||||
}
|
||||
this.redrawInputGUI();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,147 +0,0 @@
|
||||
package se.chalmers.phrasebook.gui.smallFragments;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.text.method.PasswordTransformationMethod;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.EditText;
|
||||
import android.widget.SeekBar;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.grammaticalframework.ui.android.R;
|
||||
import se.chalmers.phrasebook.backend.Model;
|
||||
import se.chalmers.phrasebook.backend.syntax.SyntaxNode;
|
||||
import se.chalmers.phrasebook.backend.syntax.SyntaxNodeList;
|
||||
|
||||
/**
|
||||
* Created by David on 2016-04-07.
|
||||
*/
|
||||
public class NumberInputFragment extends Fragment {
|
||||
|
||||
private Model model;
|
||||
private int spinnerIndex;
|
||||
private SyntaxNodeList options;
|
||||
|
||||
private int optionIndex;
|
||||
private String label;
|
||||
private int defaultInt;
|
||||
private EditText editNumber;
|
||||
private int currentNumber;
|
||||
|
||||
public static NumberInputFragment newInstance(int optionIndex, String title, int defaultInt) {
|
||||
NumberInputFragment numberInputFragment = new NumberInputFragment();
|
||||
Bundle args = new Bundle();
|
||||
|
||||
args.putInt("optionIndex", optionIndex);
|
||||
args.putString("title", title);
|
||||
args.putInt("defaultInt", defaultInt);
|
||||
|
||||
numberInputFragment.setArguments(args);
|
||||
return numberInputFragment;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
model = Model.getInstance();
|
||||
optionIndex = getArguments().getInt("optionIndex");
|
||||
label = getArguments().getString("title");
|
||||
defaultInt = getArguments().getInt("defaultInt");
|
||||
}
|
||||
|
||||
private class NumericKeyBoardTransformationMethod extends PasswordTransformationMethod {
|
||||
@Override
|
||||
public CharSequence getTransformation(CharSequence source, View view) {
|
||||
return source;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.small_fragment_number, container, false);
|
||||
TextView viewLabel = (TextView) view.findViewById(R.id.textView_number);
|
||||
final SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekBar);
|
||||
editNumber = (EditText) view.findViewById(R.id.editNumber);
|
||||
|
||||
viewLabel.setText(label);
|
||||
seekBar.setProgress(defaultInt);
|
||||
editNumber.setText(""+defaultInt);
|
||||
|
||||
editNumber.setTransformationMethod(new NumericKeyBoardTransformationMethod());
|
||||
editNumber.requestFocus();
|
||||
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
|
||||
@Override
|
||||
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||
|
||||
currentNumber = progress;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartTrackingTouch(SeekBar seekBar) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStopTrackingTouch(SeekBar seekBar) {
|
||||
editNumber.setText(""+currentNumber);
|
||||
|
||||
sendMessage(optionIndex, currentNumber);
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
editNumber.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
if(editNumber.getText().toString().equals("")) {
|
||||
currentNumber = 0;
|
||||
} else if(editNumber.getText().toString().length() < 7){
|
||||
currentNumber = Integer.parseInt(editNumber.getText().toString());
|
||||
editNumber.setInputType(0);
|
||||
sendMessage(optionIndex, currentNumber);
|
||||
|
||||
} else {
|
||||
editNumber.setText(""+editNumber.getText().toString()
|
||||
.substring(0,editNumber.getText().toString().length()-1));
|
||||
}
|
||||
seekBar.setProgress(currentNumber);
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
private void sendMessage(int optionIndex, int childIndex) {
|
||||
|
||||
InputHolderFragment fragment = (InputHolderFragment) getParentFragment();
|
||||
//options är ju tom i det här fragmentet, hur ska vi lösa det?
|
||||
fragment.updateSyntax(optionIndex, options,childIndex);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
package se.chalmers.phrasebook.gui.smallFragments;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.grammaticalframework.ui.android.R;
|
||||
import se.chalmers.phrasebook.backend.Model;
|
||||
import se.chalmers.phrasebook.backend.syntax.SyntaxNodeList;
|
||||
|
||||
public class OptionsFragment extends Fragment {
|
||||
|
||||
private Model model;
|
||||
|
||||
private ArrayList<SyntaxNodeList> options;
|
||||
private ArrayList<SyntaxNodeList> advancedOptions;
|
||||
|
||||
private int type;
|
||||
private boolean advActive;
|
||||
|
||||
private int[] containers;
|
||||
|
||||
public static OptionsFragment newInstance(int type, boolean advActive) {
|
||||
OptionsFragment optionsFragment = new OptionsFragment();
|
||||
Bundle args = new Bundle();
|
||||
args.putInt("index", type);
|
||||
args.putBoolean("advActive", advActive);
|
||||
optionsFragment.setArguments(args);
|
||||
return optionsFragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
model = Model.getInstance();
|
||||
|
||||
type = getArguments().getInt("index");
|
||||
advActive = getArguments().getBoolean("advActive");
|
||||
|
||||
options = model.getCurrentPhrase().getOptions();
|
||||
advancedOptions = model.getCurrentPhrase().getAdvOptions();
|
||||
containers = new int[6];
|
||||
|
||||
addContainers();
|
||||
addFragments();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
|
||||
return inflater.inflate(R.layout.small_fragment_options, container, false);
|
||||
|
||||
}
|
||||
|
||||
private void addFragments() {
|
||||
|
||||
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
|
||||
|
||||
if (type == 1) {
|
||||
for (int i = 0; i < options.size(); i++) {
|
||||
if (options.get(i) != null) {
|
||||
transaction.replace(containers[i], (InputHolderFragment.newInstance(i, false)));
|
||||
}
|
||||
}
|
||||
} else if (type == 2) {
|
||||
transaction.replace(containers[0], AdvancedOptionsButtonFragment.newInstance(advActive));
|
||||
if (advActive) {
|
||||
for (int i = 0; i < advancedOptions.size(); i++) {
|
||||
if (advancedOptions.get(i) != null)
|
||||
transaction.replace(containers[i + 1], InputHolderFragment.newInstance(i, true));
|
||||
}
|
||||
} else {
|
||||
//Replaces with empty fragments
|
||||
for (int i = 0; i < advancedOptions.size(); i++) {
|
||||
if (advancedOptions.get(i) != null)
|
||||
transaction.replace(containers[i + 1], new Fragment());
|
||||
}
|
||||
}
|
||||
}
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
public void update(boolean advActive) {
|
||||
this.advActive = advActive;
|
||||
addFragments();
|
||||
}
|
||||
|
||||
private void addContainers() {
|
||||
|
||||
containers[0] = R.id.child_fragment1;
|
||||
containers[1] = R.id.child_fragment2;
|
||||
containers[2] = R.id.child_fragment3;
|
||||
containers[3] = R.id.child_fragment4;
|
||||
containers[4] = R.id.child_fragment5;
|
||||
containers[5] = R.id.child_fragment6;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,124 +0,0 @@
|
||||
package se.chalmers.phrasebook.gui.smallFragments;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.grammaticalframework.ui.android.R;
|
||||
import se.chalmers.phrasebook.backend.Model;
|
||||
import se.chalmers.phrasebook.backend.syntax.SyntaxNode;
|
||||
import se.chalmers.phrasebook.backend.syntax.SyntaxNodeList;
|
||||
import se.chalmers.phrasebook.gui.FragmentCommunicator;
|
||||
|
||||
/**
|
||||
* Created by matilda on 14/03/16.
|
||||
*/
|
||||
public class SpinnerInputFragment extends Fragment {
|
||||
|
||||
private Model model;
|
||||
private int spinnerIndex;
|
||||
private SyntaxNodeList options;
|
||||
|
||||
private String label;
|
||||
private String currentChoice;
|
||||
private Spinner spinner;
|
||||
|
||||
|
||||
public static SpinnerInputFragment newInstance(int optionIndex, String title, SyntaxNodeList options) {
|
||||
SpinnerInputFragment spinnerInputFragment = new SpinnerInputFragment();
|
||||
Bundle args = new Bundle();
|
||||
|
||||
args.putInt("index", optionIndex);
|
||||
args.putString("title", title);
|
||||
args.putSerializable("spinner_options", options);
|
||||
|
||||
spinnerInputFragment.setArguments(args);
|
||||
return spinnerInputFragment;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
model = Model.getInstance();
|
||||
|
||||
label = getArguments().getString("title");
|
||||
spinnerIndex = getArguments().getInt("index");
|
||||
options = (SyntaxNodeList) getArguments().getSerializable("spinner_options");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.small_fragment_spinner, container, false);
|
||||
TextView viewLabel = (TextView) view.findViewById(R.id.text_view_spinner);
|
||||
spinner = (Spinner) view.findViewById(R.id.choice_spinner);
|
||||
|
||||
|
||||
if (label == null) {
|
||||
viewLabel.setVisibility(View.GONE);
|
||||
} else {
|
||||
viewLabel.setText(label);
|
||||
}
|
||||
|
||||
String[] children = new String[options.getChildren().size()];
|
||||
|
||||
int i = 0;
|
||||
int selectedIndex = 0;
|
||||
for (SyntaxNode s : options.getChildren()) {
|
||||
if(s == options.getSelectedChild())
|
||||
selectedIndex = i;
|
||||
children[i] = s.getDesc();
|
||||
i++;
|
||||
}
|
||||
|
||||
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,children);
|
||||
adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
|
||||
spinner.setAdapter(adapter);
|
||||
spinner.setSelection(selectedIndex);
|
||||
currentChoice = spinner.getSelectedItem().toString();
|
||||
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
||||
|
||||
@Override
|
||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||
if (!spinner.getSelectedItem().toString().equals(currentChoice)) {
|
||||
sendMessage(spinnerIndex, spinner.getSelectedItemPosition());
|
||||
currentChoice = spinner.getSelectedItem().toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNothingSelected(AdapterView<?> parent) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
return view;
|
||||
|
||||
}
|
||||
|
||||
private void sendMessage(int optionIndex, int childIndex) {
|
||||
|
||||
InputHolderFragment fragment = (InputHolderFragment) getParentFragment();
|
||||
|
||||
fragment.updateSyntax(optionIndex, options,childIndex);
|
||||
|
||||
|
||||
// Intent intent = new Intent();
|
||||
// intent.setAction("gui_update");
|
||||
// intent.putExtra("optionIndex", optionIndex);
|
||||
// intent.putExtra("childIndex", childIndex);
|
||||
//
|
||||
// LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).sendBroadcast(intent);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package se.chalmers.phrasebook.gui.smallFragments;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import org.grammaticalframework.ui.android.R;
|
||||
import se.chalmers.phrasebook.gui.FragmentCommunicator;
|
||||
import se.chalmers.phrasebook.gui.adapters.SwipeAdapter;
|
||||
|
||||
|
||||
public class SwipeFragment extends Fragment {
|
||||
|
||||
private ViewPager pager;
|
||||
private SwipeAdapter swipeAdapter;
|
||||
private FragmentCommunicator mCallback;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
|
||||
View view = inflater.inflate(R.layout.small_fragment_swipe, container, false);
|
||||
|
||||
pager = (ViewPager)view.findViewById(R.id.vpPager);
|
||||
|
||||
swipeAdapter = new SwipeAdapter(getChildFragmentManager(), mCallback);
|
||||
|
||||
pager.setAdapter(swipeAdapter);
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Activity activity) {
|
||||
super.onAttach(activity);
|
||||
System.out.println("Attaching");
|
||||
// This makes sure that the container activity has implemented
|
||||
// the callback interface. If not, it throws an exception
|
||||
try {
|
||||
mCallback = (FragmentCommunicator) activity;
|
||||
} catch (ClassCastException e) {
|
||||
throw new ClassCastException(activity.toString()
|
||||
+ " must implement OnHeadlineSelectedListener");
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isAdvanced(View view) {
|
||||
pager = (ViewPager)view.findViewById(R.id.vpPager);
|
||||
try {
|
||||
return ((ViewPager)view.findViewById(R.id.vpPager)).getCurrentItem() == 1;
|
||||
}catch(NullPointerException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
package se.chalmers.phrasebook.gui.smallFragments;
|
||||
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import org.grammaticalframework.pgf.Expr;
|
||||
import org.grammaticalframework.ui.android.Translator;
|
||||
import org.grammaticalframework.ui.android.GFTranslator;
|
||||
import org.grammaticalframework.ui.android.TTS;
|
||||
import org.grammaticalframework.ui.android.R;
|
||||
|
||||
import se.chalmers.phrasebook.backend.Model;
|
||||
|
||||
/**
|
||||
* Created by matilda on 10/03/16.
|
||||
*/
|
||||
public class TranslationFragment extends Fragment {
|
||||
|
||||
private View translateView;
|
||||
private Model mModel;
|
||||
private TextView origin,target;
|
||||
Translator mTranslator;
|
||||
|
||||
private TTS mTts;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
mModel = Model.getInstance();
|
||||
mTranslator = ((GFTranslator) getContext().getApplicationContext()).getTranslator();
|
||||
mTts = new TTS(getActivity());
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
|
||||
translateView = inflater.inflate(R.layout.small_fragment_translation, container, false);
|
||||
|
||||
origin = (TextView) translateView.findViewById(R.id.origin_phrase);
|
||||
target = (TextView) translateView.findViewById(R.id.target_phrase);
|
||||
|
||||
ImageView button = (ImageView) translateView.findViewById(R.id.button3);
|
||||
button.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
mTts.speak(mTranslator.getTargetLanguage().getLangCode(), getTargetTranslation());
|
||||
}
|
||||
});
|
||||
|
||||
return translateView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
updateData();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
if (mTts != null) {
|
||||
mTts.destroy();
|
||||
mTts = null;
|
||||
}
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
public String getTargetTranslation() {
|
||||
return target.getText().toString();
|
||||
}
|
||||
|
||||
public void updateData() {
|
||||
Expr expr = mModel.getCurrentPhrase().getAdvSyntax();
|
||||
origin.setText(mTranslator.linearizeSource(expr));
|
||||
target.setText(mTranslator.linearize(expr));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user