]> matita.cs.unibo.it Git - logicplayer.git/blob - mainActivity/src/com/example/furt/myapplication/RuleDialog.java
6bcd7bb16026a4b798236fd5ca8750dd0425fed7
[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
16 import java.util.ArrayList;
17 import java.util.List;
18
19 public class RuleDialog extends DialogFragment {
20     List<IntroductionRule> rules;
21     static Node selectedRule;
22     boolean showAllRules;
23     public RuleDialog(List<IntroductionRule> r)
24     {
25         selectedRule=null;
26         rules=new ArrayList<IntroductionRule>();
27         rules.addAll(r);
28         showAllRules=false;
29     }
30     @Override
31     public Dialog onCreateDialog(final Bundle savedInstanceState) {
32         // Use the Builder class for convenient dialog construction
33         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
34         LayoutInflater inflater=getActivity().getLayoutInflater();
35         View view=inflater.inflate(R.layout.ruledialog,null);
36         RelativeLayout layout=(RelativeLayout)((ViewGroup)((ViewGroup)view).getChildAt(0)).getChildAt(0);
37         int ruleInterval=50;
38         int i=0;
39         for (i=0;i<rules.size();i++) {
40             IntroductionRule rule=rules.get(i);
41             if (!showAllRules && rule.getPriority()==0)
42                 continue; //only high priority: skip this node
43             final Node drawNode = rule.createNodes(new askFormula());
44             BorderedTextView t = new BorderedTextView(layout.getContext());
45             t.setId(DrawActivity.globalId);
46             DrawActivity.globalId++;
47             drawNode.setView(t, layout);
48             RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
49             lp.setMargins(ruleInterval, 0, 0, 0);
50             ruleInterval+=2*drawNode.getMaxWidth();
51             lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, drawNode.view.getId());
52             drawNode.view.setLayoutParams(lp);
53             drawNode.view.setBorders(DrawActivity.b);
54             drawNode.view.setTextSize(TypedValue.COMPLEX_UNIT_PX, DrawActivity.textSize);
55             drawNode.view.setWidth(Math.round(drawNode.getLineWidth())); //setto la larghezza della view al contenuto della linea
56             drawNode.view.setGravity(Gravity.CENTER); //formula al centro della sua overline
57             drawNode.view.setText(drawNode.F.toString()); //setta il contenuto della formula
58             drawNode.global.addView(drawNode.view);
59             drawNode.handler=new DialogTouchHandler(drawNode);
60             for (Node n:drawNode.Children) {
61                 n.handler = new DialogTouchHandler(drawNode);
62             }
63             //inserisco un listener a rootView da attivare quando sono state fissate le coordinate nel RelativeLayout
64             drawNode.view.getViewTreeObserver().addOnGlobalLayoutListener(
65                     new ViewTreeObserver.OnGlobalLayoutListener() {
66                         @Override
67                         public void onGlobalLayout() {
68                             drawNode.global.setPadding(0, 0, (int) Math.max(drawNode.getRightOffset(), 50), 0); //MAXWIDTH non provoca lo scroll involontario dello schermo.
69                             drawNode.global.getLayoutParams().height = (int) Math.max(100, drawNode.getMaxHeight());
70                             drawNode.global.requestLayout();
71                             drawNode.view.setOnClickListener(drawNode.handler);
72                             drawNode.hasFocus = true;
73                             drawNode.view.getViewTreeObserver().removeOnGlobalLayoutListener(this); //rimuove il listener per evitare che l'albero sia creato ad ogni modifica del layout
74                             drawNode.Draw();
75                         }
76                     });
77         }
78         builder.setView(view);
79         builder.setTitle("Scegli una regola:")
80                 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
81                     public void onClick(DialogInterface dialog, int id) {
82                         if (selectedRule == null) return;
83                         for (Node n : selectedRule.Children) {
84                             for (Hypothesis hp : n.NodeHP)
85                                 hp.fromNode = DrawActivity.selectedNode; //le ipotesi dei nuovi figli provengono dal nodo in cui stanno per essere inserite
86                             n.addHPList(DrawActivity.selectedNode.NodeHP);
87                             n.handler = null;
88                             DrawActivity.selectedNode.addChild(n);
89                         }
90                         DrawActivity.selectedNode.hasFocus = false;
91                         DrawActivity.selectedNode.ruleName = selectedRule.Children.get(0).ruleName;
92                         DrawActivity.selectedNode.Children.get(0).hasFocus = true;
93                         DrawActivity.nmoves++;
94                         DrawActivity.rootNode.Clean();
95                         DrawActivity.startDraw();
96                         selectedRule=null;
97                     }
98                 });
99         if (!showAllRules) {
100             builder.setNeutralButton("Mostra tutte", new DialogInterface.OnClickListener() {
101                 public void onClick(DialogInterface dialog, int id) {
102                     showAllRules = true;
103                     reboot();
104                 }
105             });
106         }
107         else {
108             builder.setNeutralButton("Nascondi", new DialogInterface.OnClickListener() {
109                 public void onClick(DialogInterface dialog, int id) {
110                     showAllRules = false;
111                     reboot();
112                 }
113             });
114         }
115         builder.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
116             public void onClick(DialogInterface dialog, int id) {
117                 selectedRule=null;
118             }
119         });
120         // Create the AlertDialog object and return it
121         return builder.create();
122     }
123     void reboot()
124     {
125         selectedRule=null;
126         RuleDialog ruleDialog=new RuleDialog(rules);
127         ruleDialog.showAllRules=showAllRules;
128         ruleDialog.show(DrawActivity.fragmentManager, "CIAO");
129     }
130 }
131