]> matita.cs.unibo.it Git - logicplayer.git/blob - mainActivity/src/com/example/furt/myapplication/parserDialog.java
JOURNAL updated
[logicplayer.git] / mainActivity / src / com / example / furt / myapplication / parserDialog.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.view.LayoutInflater;
9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.Button;
12 import android.widget.RelativeLayout;
13 import android.widget.TextView;
14 import android.widget.Toast;
15
16 import java.util.ArrayList;
17 import java.util.List;
18
19 public class parserDialog extends DialogFragment {
20     static Formula F=null; //formula che sto costruendo
21     static RelativeLayout formulaLayout;
22     static TextView FView; //View contenente la formula che sto visualizzando
23     static boolean reload=false;
24     static ArrayList<String> esliter=new ArrayList<String>();
25     static ArrayList<Formula> undo=new ArrayList<Formula>();
26     public parserDialog()
27     {
28         esliter.removeAll(esliter);
29         if(!reload)
30             undo.removeAll(undo);
31         reload=false;
32         if(F==null) {
33             F = new UndefinedFormula();
34             F.setCursor();
35         }
36         esliter.addAll(parser.getLiteral(DrawActivity.nomeEs));
37     }
38     @Override
39     public Dialog onCreateDialog(final Bundle savedInstanceState) {
40         // Use the Builder class for convenient dialog construction
41         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
42         LayoutInflater inflater=getActivity().getLayoutInflater();
43         View view=inflater.inflate(R.layout.string_layout, null);
44         RelativeLayout litersLayout=(RelativeLayout)((ViewGroup)((ViewGroup)view).getChildAt(0)).getChildAt(0);
45         RelativeLayout operatorsLayout=(RelativeLayout)(((ViewGroup)view).getChildAt(1));
46         formulaLayout=(RelativeLayout)((ViewGroup)((ViewGroup)view).getChildAt(2)).getChildAt(0);
47         FView=new TextView(formulaLayout.getContext());
48         FView.setText(F.toString());
49         FView.setTextSize(DrawActivity.textSize);
50         formulaLayout.addView(FView);
51         int leftId=0;
52         if(!esliter.isEmpty()) {
53             for (String liter : esliter) {
54                 Button lit = new Button(litersLayout.getContext());
55                 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
56                 lit.setText(liter);
57                 if (leftId != 0) {
58                     lp.addRule(RelativeLayout.RIGHT_OF, leftId);
59                 }
60                 lit.setLayoutParams(lp);
61                 lit.setId(DrawActivity.globalId++);
62                 leftId = lit.getId();
63                 litersLayout.addView(lit);
64                 lit.setOnClickListener(new touchParserHandler(4, liter.charAt(0)));
65             }
66         }
67         Button bImpl=new Button(operatorsLayout.getContext());
68         Button bAnd=new Button(operatorsLayout.getContext());
69         Button bOr=new Button(operatorsLayout.getContext());
70         Button bNot=new Button(operatorsLayout.getContext());
71         RelativeLayout.LayoutParams lpImpl=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
72         RelativeLayout.LayoutParams lpAnd=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
73         RelativeLayout.LayoutParams lpNot=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
74         RelativeLayout.LayoutParams lpOr=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
75
76         bImpl.setText("⇒");
77         bImpl.setLayoutParams(lpImpl);
78         bImpl.setId(DrawActivity.globalId++);
79         leftId=bImpl.getId();
80
81         bOr.setText("∨");
82         lpOr.addRule(RelativeLayout.RIGHT_OF, leftId);
83         bOr.setLayoutParams(lpOr);
84         bOr.setId(DrawActivity.globalId++);
85         leftId=bOr.getId();
86
87         bAnd.setText("∧");
88         lpAnd.addRule(RelativeLayout.RIGHT_OF, leftId);
89         bAnd.setLayoutParams(lpAnd);
90         bAnd.setId(DrawActivity.globalId++);
91         leftId=bAnd.getId();
92
93         bNot.setText("¬");
94         lpNot.addRule(RelativeLayout.RIGHT_OF, leftId);
95         bNot.setLayoutParams(lpNot);
96         bNot.setId(DrawActivity.globalId++);
97
98         operatorsLayout.addView(bImpl);
99         operatorsLayout.addView(bAnd);
100         operatorsLayout.addView(bNot);
101         operatorsLayout.addView(bOr);
102         bImpl.setOnClickListener(new touchParserHandler(2,' '));
103         bAnd.setOnClickListener(new touchParserHandler(0,' '));
104         bNot.setOnClickListener(new touchParserHandler(3, ' '));
105         bOr.setOnClickListener(new touchParserHandler(1, ' '));
106         builder.setView(view);
107         builder.setTitle("Inserisci una formula").setPositiveButton("Ok", new DialogInterface.OnClickListener() {
108             public void onClick(DialogInterface dialog, int id) {
109                 if(!F.toString().contains("_")) {
110                     List<EliminationRule> L = F.EliminationRules();
111                     if (L.size()==0){
112                         F=null;
113                         Toast.makeText(DrawActivity.rootNode.view.getContext(),"Impossibile scaricare questa ipotesi ora",Toast.LENGTH_LONG).show();
114                         return;
115                     }
116                     if (F.toString().equals(DrawActivity.selectedNode.F.toString())) //sto provando a scaricare il nodo selezionato
117                     {
118                         Toast.makeText(DrawActivity.rootNode.view.getContext(),"Impossibile scaricare un nodo aperto senza dimostrarlo",Toast.LENGTH_LONG).show();
119                         F=null;
120                         return;
121                     }
122                     touchHPHandler t = new touchHPHandler(L.get(0).createNodes(null, new askFormula()),new Hypothesis(F,true));
123                     F = null;
124                     t.discard=false;
125                     t.onClick(null);
126                 }
127                 else{
128                     Toast.makeText(DrawActivity.rootNode.view.getContext(),"Completa l'inserimento!",Toast.LENGTH_SHORT).show();
129                     reboot();
130                 }
131             }
132         });
133         builder.setNeutralButton("Undo", new DialogInterface.OnClickListener() {
134             public void onClick(DialogInterface dialog, int id) {
135                 if(undo.size()>1) {
136                     F = undo.remove(undo.size() - 1);
137                 }
138                 else
139                     F=null;
140                 reboot();
141             }
142         });
143         builder.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
144             public void onClick(DialogInterface dialog, int id) {
145                 F=null;
146             }
147         });
148         // Create the AlertDialog object and return it
149         return builder.create();
150     }
151     static void reboot()
152     {
153         reload=true;
154         parserDialog p=new parserDialog();
155         p.F=F;
156         p.show(DrawActivity.fragmentManager, "CIAO");
157     }
158
159 }