1
0
forked from GitHub/gf-core

some extensions in the conceptual editor needed for the SBAR app

This commit is contained in:
krasimir
2017-05-06 07:46:39 +00:00
parent d620c62d0b
commit 7d0779dfcc
2 changed files with 71 additions and 10 deletions

View File

@@ -17,13 +17,14 @@ public class Model {
private static Model model; private static Model model;
private List<SyntaxTree> phrases; private List<SyntaxTree> phrases;
private Map<String,List<SyntaxTree>> groups;
private Model() { private Model() {
try { try {
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputStream is = GFTranslator.get().getAssets().open("phrases.xml"); InputStream is = GFTranslator.get().getAssets().open("phrases.xml");
Document document = documentBuilder.parse(is); Document document = documentBuilder.parse(is);
phrases = parseSentencesData(document); parseSentencesData(document);
is.close(); is.close();
} catch (ParserConfigurationException e) { } catch (ParserConfigurationException e) {
e.printStackTrace(); e.printStackTrace();
@@ -48,8 +49,13 @@ public class Model {
return phrases; return phrases;
} }
private List<SyntaxTree> parseSentencesData(Document document) { public List<SyntaxTree> getGroup(String id) {
List<SyntaxTree> sentences = new ArrayList<SyntaxTree>(); 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>(); Map<String,SyntaxNode> ids = new HashMap<String,SyntaxNode>();
List<SyntaxNodeCall> calls = new ArrayList<SyntaxNodeCall>(); List<SyntaxNodeCall> calls = new ArrayList<SyntaxNodeCall>();
@@ -72,7 +78,51 @@ public class Model {
SyntaxNode[] nodes = constructSyntaxNodeList(node, ids, calls); SyntaxNode[] nodes = constructSyntaxNodeList(node, ids, calls);
if (nodes.length > 0) 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) { } else if (node.getAttributes() != null && node.getAttributes().getNamedItem("id") != null) {
String id = node.getAttributes().getNamedItem("id").getNodeValue(); String id = node.getAttributes().getNamedItem("id").getNodeValue();
SyntaxNode snode = constructSyntaxNode(node, ids, calls); SyntaxNode snode = constructSyntaxNode(node, ids, calls);
@@ -86,7 +136,6 @@ public class Model {
for (SyntaxNodeCall call : calls) { for (SyntaxNodeCall call : calls) {
call.bind(ids); call.bind(ids);
} }
return sentences;
} }
private SyntaxNode constructSyntaxNode(Node node, Map<String,SyntaxNode> ids, List<SyntaxNodeCall> calls) { private SyntaxNode constructSyntaxNode(Node node, Map<String,SyntaxNode> ids, List<SyntaxNodeCall> calls) {

View File

@@ -2,7 +2,7 @@ package se.chalmers.phrasebook.gui.fragments;
import android.os.Bundle; import android.os.Bundle;
import java.util.ArrayList; import java.util.*;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
@@ -31,6 +31,7 @@ public class PhraseListFragment extends Fragment {
protected Model model; protected Model model;
private String title; private String title;
private String id;
public static PhraseListFragment newInstance(String title) { public static PhraseListFragment newInstance(String title) {
PhraseListFragment fragment = new PhraseListFragment(); PhraseListFragment fragment = new PhraseListFragment();
@@ -40,12 +41,22 @@ public class PhraseListFragment extends Fragment {
return fragment; return fragment;
} }
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;
}
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
model = Model.getInstance(); model = Model.getInstance();
title = getArguments().getString("title"); title = getArguments().getString("title");
id = getArguments().getString("id");
} }
@Override @Override
@@ -55,15 +66,16 @@ public class PhraseListFragment extends Fragment {
View view = inflater.inflate(R.layout.fragment_phrase_list, container, false); View view = inflater.inflate(R.layout.fragment_phrase_list, container, false);
getActivity().getActionBar().setTitle(title); 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); final ListView phraseListView = (ListView) view.findViewById(R.id.phrase_listView);
phraseListView.setAdapter(adapter); phraseListView.setAdapter(adapter);
phraseListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { phraseListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long item_id) {
SyntaxTree phrase = model.getSentences().get(position); SyntaxTree phrase = sentences.get(position);
getActivity().getActionBar().setTitle(phrase.getDesc()); getActivity().getActionBar().setTitle(phrase.getDesc());
((NavigationActivity) getActivity()).setToTranslationFragment(phrase); ((NavigationActivity) getActivity()).setToTranslationFragment(phrase);
} }