--- /dev/null
+package com.example.furt.myapplication;
+
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.app.DialogFragment;
+import android.content.DialogInterface;
+import android.os.Bundle;
+import android.util.TypedValue;
+import android.view.Gravity;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.ViewTreeObserver;
+import android.widget.RelativeLayout;
+import android.widget.Toast;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class RuleDialog extends DialogFragment {
+ List<IntroductionRule> rules;
+ boolean showAllRules;
+ public RuleDialog(List<IntroductionRule> r)
+ {
+ rules=new ArrayList<IntroductionRule>();
+ rules.addAll(r);
+ showAllRules=false;
+ }
+ @Override
+ public Dialog onCreateDialog(final Bundle savedInstanceState) {
+ // Use the Builder class for convenient dialog construction
+ AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
+ int prules=0;
+ for (IntroductionRule r:rules)
+ if (r.getPriority()>0)
+ prules++;
+ if (prules==0 && !showAllRules)
+ {
+ showAllRules=true;
+ reboot();
+ dismiss();
+ return builder.create();
+ }
+ LayoutInflater inflater=getActivity().getLayoutInflater();
+ View view=inflater.inflate(R.layout.ruledialog,null);
+ RelativeLayout layout=(RelativeLayout)((ViewGroup)((ViewGroup)view).getChildAt(0)).getChildAt(0);
+ int ruleInterval=50;
+ for (IntroductionRule rule : rules) {
+ if (!showAllRules && rule.getPriority() == 0)
+ continue; //only high priority: skip this node
+ final Node drawNode = rule.createNodes(new askFormula());
+ BorderedTextView t = new BorderedTextView(layout.getContext());
+ t.setId(DrawActivity.globalId);
+ DrawActivity.globalId++;
+ drawNode.setView(t, layout);
+ RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
+ lp.setMargins(ruleInterval, 0, 0, 0);
+ ruleInterval += 2 * drawNode.getMaxWidth();
+ lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, drawNode.view.getId());
+ drawNode.view.setLayoutParams(lp);
+ drawNode.view.setBorders(DrawActivity.b);
+ drawNode.view.setTextSize(TypedValue.COMPLEX_UNIT_PX, DrawActivity.textSize);
+ drawNode.view.setWidth(Math.round(drawNode.getLineWidth())); //setto la larghezza della view al contenuto della linea
+ drawNode.view.setGravity(Gravity.CENTER); //formula al centro della sua overline
+ drawNode.view.setText(drawNode.F.toString()); //setta il contenuto della formula
+ drawNode.global.addView(drawNode.view);
+ drawNode.handler = new DialogTouchHandler(drawNode);
+ for (Node n : drawNode.Children) {
+ n.handler = new DialogTouchHandler(drawNode);
+ }
+ //inserisco un listener a rootView da attivare quando sono state fissate le coordinate nel RelativeLayout
+ drawNode.view.getViewTreeObserver().addOnGlobalLayoutListener(
+ new ViewTreeObserver.OnGlobalLayoutListener() {
+ @Override
+ public void onGlobalLayout() {
+ drawNode.global.setPadding(0, 0, (int) Math.max(drawNode.getRightOffset(), 50), 0); //MAXWIDTH non provoca lo scroll involontario dello schermo.
+ drawNode.global.getLayoutParams().height = (int) Math.max(100, drawNode.getMaxHeight());
+ drawNode.global.requestLayout();
+ drawNode.view.setOnClickListener(drawNode.handler);
+ drawNode.hasFocus = true;
+ drawNode.view.getViewTreeObserver().removeOnGlobalLayoutListener(this); //rimuove il listener per evitare che l'albero sia creato ad ogni modifica del layout
+ drawNode.Draw();
+ }
+ });
+ }
+ builder.setView(view);
+ builder.setTitle("Scegli una regola:");
+ builder.setPositiveButton("Incolla sottoalbero",new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ if (DrawActivity.copiedNode == null) //non ci sono nodi da copiare
+ {
+ Toast.makeText(DrawActivity.rootNode.view.getContext(),"Impossibile incollare: nessun nodo copiato",Toast.LENGTH_LONG).show();
+ }
+ else if (!checkHP(DrawActivity.copiedNode, DrawActivity.selectedNode)) //ipotesi incompatibili: impossibile incollare in questo punto
+ Toast.makeText(DrawActivity.rootNode.view.getContext(),"Impossibile incollare: sottoalberi incompatibili",Toast.LENGTH_LONG).show();
+ else {
+ Node tmp = duplicateNode(DrawActivity.copiedNode); //duplica il nodo copiato
+ DrawActivity.selectedNode.ruleName=tmp.ruleName; //eredita il nome della regola
+ DrawActivity.selectedNode.Children = new ArrayList<Node>();
+ for (Node c:tmp.Children)
+ DrawActivity.selectedNode.addChild(c); //incolla il sottoalbero del nodo copiato nella posizione richiesta
+ DrawActivity.rootNode.Clean();
+ DrawActivity.startDraw();
+ }
+ }
+ });
+ if (!showAllRules) {
+ builder.setNeutralButton("Mostra tutte", new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int id) {
+ showAllRules=true;
+ reboot();
+ }
+ });
+ }
+ else {
+ builder.setNeutralButton("Nascondi", new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int id) {
+ showAllRules=false;
+ reboot();
+ }
+ });
+ }
+ builder.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int id) {
+ }
+ });
+ // Create the AlertDialog object and return it
+ return builder.create();
+ }
+ void reboot()
+ {
+ touchnodeHandler.ruleDialog=new RuleDialog(rules);
+ touchnodeHandler.ruleDialog.showAllRules=showAllRules;
+ touchnodeHandler.ruleDialog.show(DrawActivity.fragmentManager, "CIAO");
+ }
+
+ public boolean checkHP(Node source,Node target) //controlla se le ipotesi di source sono compatibili per una copia in target
+ {
+ boolean found=false;
+ if (!source.F.toString().equals(target.F.toString())) //se i nodi non hanno la stessa formula la copia è automaticamente impossibile
+ return false;
+ for (Hypothesis sourceHp:source.NodeHP) {
+ for (Hypothesis destHp : target.NodeHP) {
+ 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).
+ found = true;
+ break;
+ }
+ }
+ if (!found)
+ return false;
+ else found=false;
+ }
+ return true;
+ }
+
+ public Node duplicateNode(Node src) //metodo di supporto che duplica un nodo
+ {
+ Node tmp=new Node(src.F);
+ tmp.addHPList(src.NodeHP);
+ tmp.ruleName=src.ruleName;
+ for (Node n:src.Children) {
+ tmp.addChild(duplicateNode(n));
+ }
+ return tmp;
+ }
+}
+