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 9376ccfe62
commit a3768695b3
2 changed files with 71 additions and 10 deletions

View File

@@ -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) {

View File

@@ -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,8 +31,9 @@ public class PhraseListFragment extends Fragment {
protected Model model;
private String title;
private String id;
public static PhraseListFragment newInstance(String title) {
public static PhraseListFragment newInstance(String title) {
PhraseListFragment fragment = new PhraseListFragment();
Bundle args = new Bundle();
args.putString("title", title);
@@ -40,12 +41,22 @@ public class PhraseListFragment extends 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
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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);
}