1 package com.example.furt.myapplication;
3 import android.app.AlertDialog;
4 import android.app.Dialog;
5 import android.app.DialogFragment;
6 import android.content.DialogInterface;
7 import android.os.Bundle;
8 import android.util.TypedValue;
9 import android.view.Gravity;
10 import android.view.LayoutInflater;
11 import android.view.View;
12 import android.view.ViewGroup;
13 import android.view.ViewTreeObserver;
14 import android.widget.RelativeLayout;
15 import android.widget.Toast;
17 import java.util.ArrayList;
18 import java.util.List;
20 public class RuleDialog extends DialogFragment {
21 List<IntroductionRule> rules;
23 public RuleDialog(List<IntroductionRule> r)
25 rules=new ArrayList<IntroductionRule>();
30 public Dialog onCreateDialog(final Bundle savedInstanceState) {
31 // Use the Builder class for convenient dialog construction
32 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
34 for (IntroductionRule r:rules)
35 if (r.getPriority()>0)
37 if (prules==0 && !showAllRules)
42 return builder.create();
44 LayoutInflater inflater=getActivity().getLayoutInflater();
45 View view=inflater.inflate(R.layout.ruledialog,null);
46 RelativeLayout layout=(RelativeLayout)((ViewGroup)((ViewGroup)view).getChildAt(0)).getChildAt(0);
48 for (IntroductionRule rule : rules) {
49 if (!showAllRules && rule.getPriority() == 0)
50 continue; //only high priority: skip this node
51 final Node drawNode = rule.createNodes(new askFormula());
52 BorderedTextView t = new BorderedTextView(layout.getContext());
53 t.setId(DrawActivity.globalId);
54 DrawActivity.globalId++;
55 drawNode.setView(t, layout);
56 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
57 lp.setMargins(ruleInterval, 0, 0, 0);
58 ruleInterval += 2 * drawNode.getMaxWidth();
59 lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, drawNode.view.getId());
60 drawNode.view.setLayoutParams(lp);
61 drawNode.view.setBorders(DrawActivity.b);
62 drawNode.view.setTextSize(TypedValue.COMPLEX_UNIT_PX, DrawActivity.textSize);
63 drawNode.view.setWidth(Math.round(drawNode.getLineWidth())); //setto la larghezza della view al contenuto della linea
64 drawNode.view.setGravity(Gravity.CENTER); //formula al centro della sua overline
65 drawNode.view.setText(drawNode.F.toString()); //setta il contenuto della formula
66 drawNode.global.addView(drawNode.view);
67 drawNode.handler = new DialogTouchHandler(drawNode);
68 for (Node n : drawNode.Children) {
69 n.handler = new DialogTouchHandler(drawNode);
71 //inserisco un listener a rootView da attivare quando sono state fissate le coordinate nel RelativeLayout
72 drawNode.view.getViewTreeObserver().addOnGlobalLayoutListener(
73 new ViewTreeObserver.OnGlobalLayoutListener() {
75 public void onGlobalLayout() {
76 drawNode.global.setPadding(0, 0, (int) Math.max(drawNode.getRightOffset(), 50), 0); //MAXWIDTH non provoca lo scroll involontario dello schermo.
77 drawNode.global.getLayoutParams().height = (int) Math.max(100, drawNode.getMaxHeight());
78 drawNode.global.requestLayout();
79 drawNode.view.setOnClickListener(drawNode.handler);
80 drawNode.hasFocus = true;
81 drawNode.view.getViewTreeObserver().removeOnGlobalLayoutListener(this); //rimuove il listener per evitare che l'albero sia creato ad ogni modifica del layout
86 builder.setView(view);
87 builder.setTitle("Scegli una regola:");
88 builder.setPositiveButton("Incolla sottoalbero",new DialogInterface.OnClickListener() {
90 public void onClick(DialogInterface dialog, int which) {
91 if (DrawActivity.copiedNode == null) //non ci sono nodi da copiare
93 Toast.makeText(DrawActivity.rootNode.view.getContext(),"Impossibile incollare: nessun nodo copiato",Toast.LENGTH_LONG).show();
95 else if (!checkHP(DrawActivity.copiedNode, DrawActivity.selectedNode)) //ipotesi incompatibili: impossibile incollare in questo punto
96 Toast.makeText(DrawActivity.rootNode.view.getContext(),"Impossibile incollare: sottoalberi incompatibili",Toast.LENGTH_LONG).show();
98 Node tmp = duplicateNode(DrawActivity.copiedNode); //duplica il nodo copiato
99 DrawActivity.selectedNode.ruleName=tmp.ruleName; //eredita il nome della regola
100 DrawActivity.selectedNode.Children = new ArrayList<Node>();
101 for (Node c:tmp.Children)
102 DrawActivity.selectedNode.addChild(c); //incolla il sottoalbero del nodo copiato nella posizione richiesta
103 DrawActivity.rootNode.Clean();
104 DrawActivity.startDraw();
109 builder.setNeutralButton("Mostra tutte", new DialogInterface.OnClickListener() {
110 public void onClick(DialogInterface dialog, int id) {
117 builder.setNeutralButton("Nascondi", new DialogInterface.OnClickListener() {
118 public void onClick(DialogInterface dialog, int id) {
124 builder.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
125 public void onClick(DialogInterface dialog, int id) {
128 // Create the AlertDialog object and return it
129 return builder.create();
133 touchnodeHandler.ruleDialog=new RuleDialog(rules);
134 touchnodeHandler.ruleDialog.showAllRules=showAllRules;
135 touchnodeHandler.ruleDialog.show(DrawActivity.fragmentManager, "CIAO");
138 public boolean checkHP(Node source,Node target) //controlla se le ipotesi di source sono compatibili per una copia in target
141 if (!source.F.toString().equals(target.F.toString())) //se i nodi non hanno la stessa formula la copia è automaticamente impossibile
143 for (Hypothesis sourceHp:source.NodeHP) {
144 for (Hypothesis destHp : target.NodeHP) {
145 if (destHp.HP.toString().equals(sourceHp.HP.toString())) { //per ogni ipotesi nel nodo sorgente cerco se è disponibile nel nodo destinazione: in caso affermativo, proseguo il ciclo (found=true).
157 public Node duplicateNode(Node src) //metodo di supporto che duplica un nodo
159 Node tmp=new Node(src.F);
160 tmp.addHPList(src.NodeHP);
161 tmp.ruleName=src.ruleName;
162 for (Node n:src.Children) {
163 tmp.addChild(duplicateNode(n));