From 7d0779dfcc5a3c017c8c25860abd6573148ccdcb Mon Sep 17 00:00:00 2001 From: krasimir Date: Sat, 6 May 2017 07:46:39 +0000 Subject: some extensions in the conceptual editor needed for the SBAR app --- .../src/se/chalmers/phrasebook/backend/Model.java | 59 ++++++++++++++++++++-- .../gui/fragments/PhraseListFragment.java | 22 ++++++-- 2 files changed, 71 insertions(+), 10 deletions(-) (limited to 'src') 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 phrases; + private Map> 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 parseSentencesData(Document document) { - List sentences = new ArrayList(); + public List getGroup(String id) { + return groups.get(id); + } + + private void parseSentencesData(Document document) { + phrases = new ArrayList(); + groups = new HashMap>(); Map ids = new HashMap(); List calls = new ArrayList(); @@ -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 group_phrases = new ArrayList(); + + 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 ids, List 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 adapter = new ArrayAdapter(getActivity(), R.layout.phrase_list_item, model.getSentences()); + final List sentences = (id == null) ? model.getSentences() : model.getGroup(id); + ArrayAdapter adapter = new ArrayAdapter(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); } -- cgit v1.2.3