summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ui/android/src/se/chalmers/phrasebook/backend/Model.java59
-rw-r--r--src/ui/android/src/se/chalmers/phrasebook/gui/fragments/PhraseListFragment.java22
2 files changed, 71 insertions, 10 deletions
diff --git a/src/ui/android/src/se/chalmers/phrasebook/backend/Model.java b/src/ui/android/src/se/chalmers/phrasebook/backend/Model.java
index b5ebf430d..8e4b07b21 100644
--- a/src/ui/android/src/se/chalmers/phrasebook/backend/Model.java
+++ b/src/ui/android/src/se/chalmers/phrasebook/backend/Model.java
@@ -17,13 +17,14 @@ public class Model {
private static Model model;
private List<SyntaxTree> phrases;
+ private Map<String,List<SyntaxTree>> groups;
private Model() {
try {
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputStream is = GFTranslator.get().getAssets().open("phrases.xml");
Document document = documentBuilder.parse(is);
- phrases = parseSentencesData(document);
+ parseSentencesData(document);
is.close();
} catch (ParserConfigurationException e) {
e.printStackTrace();
@@ -48,8 +49,13 @@ public class Model {
return phrases;
}
- private List<SyntaxTree> parseSentencesData(Document document) {
- List<SyntaxTree> sentences = new ArrayList<SyntaxTree>();
+ public List<SyntaxTree> getGroup(String id) {
+ return groups.get(id);
+ }
+
+ private void parseSentencesData(Document document) {
+ phrases = new ArrayList<SyntaxTree>();
+ groups = new HashMap<String,List<SyntaxTree>>();
Map<String,SyntaxNode> ids = new HashMap<String,SyntaxNode>();
List<SyntaxNodeCall> calls = new ArrayList<SyntaxNodeCall>();
@@ -72,7 +78,51 @@ public class Model {
SyntaxNode[] nodes = constructSyntaxNodeList(node, ids, calls);
if (nodes.length > 0)
- sentences.add(new SyntaxTree(desc, nodes[0]));
+ phrases.add(new SyntaxTree(desc, nodes[0]));
+ } else if (node != null &&
+ node.getNodeType() == Node.ELEMENT_NODE &&
+ node.getNodeName().equals("group")) {
+
+ NamedNodeMap attributes = node.getAttributes();
+ if (attributes == null)
+ continue;
+
+ String id = null;
+ if (attributes.getNamedItem("id") != null) {
+ id = attributes.getNamedItem("id").getNodeValue();
+ }
+ if (id == null)
+ continue;
+
+ List<SyntaxTree> group_phrases = new ArrayList<SyntaxTree>();
+
+ NodeList nodesList2 = node.getChildNodes();
+ for (int j = 0; j < nodesList2.getLength(); j++) {
+ node = nodesList2.item(j);
+
+ if (node != null &&
+ node.getNodeType() == Node.ELEMENT_NODE &&
+ node.getNodeName().equals("sentence")) {
+ 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) {
+ SyntaxTree tree = new SyntaxTree(desc, nodes[0]);
+ phrases.add(tree);
+ group_phrases.add(tree);
+ }
+ }
+ }
+
+ groups.put(id, group_phrases);
} else if (node.getAttributes() != null && node.getAttributes().getNamedItem("id") != null) {
String id = node.getAttributes().getNamedItem("id").getNodeValue();
SyntaxNode snode = constructSyntaxNode(node, ids, calls);
@@ -86,7 +136,6 @@ public class Model {
for (SyntaxNodeCall call : calls) {
call.bind(ids);
}
- return sentences;
}
private SyntaxNode constructSyntaxNode(Node node, Map<String,SyntaxNode> ids, List<SyntaxNodeCall> calls) {
diff --git a/src/ui/android/src/se/chalmers/phrasebook/gui/fragments/PhraseListFragment.java b/src/ui/android/src/se/chalmers/phrasebook/gui/fragments/PhraseListFragment.java
index 2328f340d..7e3c09ada 100644
--- a/src/ui/android/src/se/chalmers/phrasebook/gui/fragments/PhraseListFragment.java
+++ b/src/ui/android/src/se/chalmers/phrasebook/gui/fragments/PhraseListFragment.java
@@ -2,7 +2,7 @@ package se.chalmers.phrasebook.gui.fragments;
import android.os.Bundle;
-import java.util.ArrayList;
+import java.util.*;
import android.app.Activity;
import android.content.Intent;
@@ -31,11 +31,21 @@ public class PhraseListFragment extends Fragment {
protected Model model;
private String title;
+ private String id;
+
+ public static PhraseListFragment newInstance(String title) {
+ PhraseListFragment fragment = new PhraseListFragment();
+ Bundle args = new Bundle();
+ args.putString("title", title);
+ fragment.setArguments(args);
+ return fragment;
+ }
- public static PhraseListFragment newInstance(String title) {
+ public static PhraseListFragment newInstance(String title, String id) {
PhraseListFragment fragment = new PhraseListFragment();
Bundle args = new Bundle();
args.putString("title", title);
+ args.putString("id", id);
fragment.setArguments(args);
return fragment;
}
@@ -46,6 +56,7 @@ public class PhraseListFragment extends Fragment {
model = Model.getInstance();
title = getArguments().getString("title");
+ id = getArguments().getString("id");
}
@Override
@@ -55,15 +66,16 @@ public class PhraseListFragment extends Fragment {
View view = inflater.inflate(R.layout.fragment_phrase_list, container, false);
getActivity().getActionBar().setTitle(title);
- ArrayAdapter<SyntaxTree> adapter = new ArrayAdapter<SyntaxTree>(getActivity(), R.layout.phrase_list_item, model.getSentences());
+ final List<SyntaxTree> sentences = (id == null) ? model.getSentences() : model.getGroup(id);
+ ArrayAdapter<SyntaxTree> adapter = new ArrayAdapter<SyntaxTree>(getActivity(), R.layout.phrase_list_item, sentences);
final ListView phraseListView = (ListView) view.findViewById(R.id.phrase_listView);
phraseListView.setAdapter(adapter);
phraseListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- SyntaxTree phrase = model.getSentences().get(position);
+ public void onItemClick(AdapterView<?> parent, View view, int position, long item_id) {
+ SyntaxTree phrase = sentences.get(position);
getActivity().getActionBar().setTitle(phrase.getDesc());
((NavigationActivity) getActivity()).setToTranslationFragment(phrase);
}