]> matita.cs.unibo.it Git - logicplayer.git/blob - mainActivity/src/com/example/furt/myapplication/RuleDialog.java
1cffd89af95e98ba5bc0271fc1e066f72115f011
[logicplayer.git] / mainActivity / src / com / example / furt / myapplication / RuleDialog.java
1 package com.example.furt.myapplication;
2
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;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 public class RuleDialog extends DialogFragment {
21     List<IntroductionRule> rules;
22     boolean showAllRules;
23     public RuleDialog(List<IntroductionRule> r)
24     {
25         rules=new ArrayList<IntroductionRule>();
26         rules.addAll(r);
27         showAllRules=false;
28     }
29     @Override
30     public Dialog onCreateDialog(final Bundle savedInstanceState) {
31         // Use the Builder class for convenient dialog construction
32         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
33         int prules=0;
34         for (IntroductionRule r:rules)
35             if (r.getPriority()>0)
36                 prules++;
37         if (prules==0 && !showAllRules)
38         {
39             showAllRules=true;
40             reboot();
41             dismiss();
42             return builder.create();
43         }
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);
47         int ruleInterval=50;
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);
70             }
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() {
74                         @Override
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
82                             drawNode.Draw();
83                         }
84                     });
85         }
86         builder.setView(view);
87         builder.setTitle("Scegli una regola:");
88         builder.setPositiveButton("Incolla sottoalbero",new DialogInterface.OnClickListener() {
89             @Override
90             public void onClick(DialogInterface dialog, int which) {
91                 if (DrawActivity.copiedNode == null) //non ci sono nodi da copiare
92                 {
93                     Toast.makeText(DrawActivity.rootNode.view.getContext(),"Impossibile incollare: nessun nodo copiato",Toast.LENGTH_LONG).show();
94                 }
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();
97                 else {
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();
105                 }
106             }
107         });
108         if (!showAllRules) {
109             builder.setNeutralButton("Mostra tutte", new DialogInterface.OnClickListener() {
110                 public void onClick(DialogInterface dialog, int id) {
111                     showAllRules=true;
112                     reboot();
113                 }
114             });
115         }
116         else {
117             builder.setNeutralButton("Nascondi", new DialogInterface.OnClickListener() {
118                 public void onClick(DialogInterface dialog, int id) {
119                     showAllRules=false;
120                     reboot();
121                 }
122             });
123         }
124         builder.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
125             public void onClick(DialogInterface dialog, int id) {
126             }
127         });
128         // Create the AlertDialog object and return it
129         return builder.create();
130     }
131     void reboot()
132     {
133         touchnodeHandler.ruleDialog=new RuleDialog(rules);
134         touchnodeHandler.ruleDialog.showAllRules=showAllRules;
135         touchnodeHandler.ruleDialog.show(DrawActivity.fragmentManager, "CIAO");
136     }
137
138     public boolean checkHP(Node source,Node target) //controlla se le ipotesi di source sono compatibili per una copia in target
139     {
140         boolean found=false;
141         if (!source.F.toString().equals(target.F.toString())) //se i nodi non hanno la stessa formula la copia è automaticamente impossibile
142             return false;
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).
146                     found = true;
147                     break;
148                 }
149             }
150             if (!found)
151                 return false;
152             else found=false;
153         }
154         return true;
155     }
156
157     public Node duplicateNode(Node src) //metodo di supporto che duplica un nodo
158     {
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));
164         }
165         return tmp;
166     }
167 }
168