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