]> matita.cs.unibo.it Git - logicplayer.git/blobdiff - mainActivity/src/com/example/furt/myapplication/RuleDialog.java
New version (to be tested).
[logicplayer.git] / mainActivity / src / com / example / furt / myapplication / RuleDialog.java
index 6bcd7bb16026a4b798236fd5ca8750dd0425fed7..1cffd89af95e98ba5bc0271fc1e066f72115f011 100755 (executable)
@@ -12,17 +12,16 @@ 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;
-    static Node selectedRule;
     boolean showAllRules;
     public RuleDialog(List<IntroductionRule> r)
     {
-        selectedRule=null;
         rules=new ArrayList<IntroductionRule>();
         rules.addAll(r);
         showAllRules=false;
@@ -31,14 +30,23 @@ public class RuleDialog extends DialogFragment {
     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;
-        int i=0;
-        for (i=0;i<rules.size();i++) {
-            IntroductionRule rule=rules.get(i);
-            if (!showAllRules && rule.getPriority()==0)
+        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());
@@ -47,7 +55,7 @@ public class RuleDialog extends DialogFragment {
             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();
+            ruleInterval += 2 * drawNode.getMaxWidth();
             lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, drawNode.view.getId());
             drawNode.view.setLayoutParams(lp);
             drawNode.view.setBorders(DrawActivity.b);
@@ -56,8 +64,8 @@ public class RuleDialog extends DialogFragment {
             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) {
+            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
@@ -76,30 +84,31 @@ public class RuleDialog extends DialogFragment {
                     });
         }
         builder.setView(view);
-        builder.setTitle("Scegli una regola:")
-                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
-                    public void onClick(DialogInterface dialog, int id) {
-                        if (selectedRule == null) return;
-                        for (Node n : selectedRule.Children) {
-                            for (Hypothesis hp : n.NodeHP)
-                                hp.fromNode = DrawActivity.selectedNode; //le ipotesi dei nuovi figli provengono dal nodo in cui stanno per essere inserite
-                            n.addHPList(DrawActivity.selectedNode.NodeHP);
-                            n.handler = null;
-                            DrawActivity.selectedNode.addChild(n);
-                        }
-                        DrawActivity.selectedNode.hasFocus = false;
-                        DrawActivity.selectedNode.ruleName = selectedRule.Children.get(0).ruleName;
-                        DrawActivity.selectedNode.Children.get(0).hasFocus = true;
-                        DrawActivity.nmoves++;
-                        DrawActivity.rootNode.Clean();
-                        DrawActivity.startDraw();
-                        selectedRule=null;
-                    }
-                });
+        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;
+                    showAllRules=true;
                     reboot();
                 }
             });
@@ -107,14 +116,13 @@ public class RuleDialog extends DialogFragment {
         else {
             builder.setNeutralButton("Nascondi", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
-                    showAllRules = false;
+                    showAllRules=false;
                     reboot();
                 }
             });
         }
         builder.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {
-                selectedRule=null;
             }
         });
         // Create the AlertDialog object and return it
@@ -122,10 +130,39 @@ public class RuleDialog extends DialogFragment {
     }
     void reboot()
     {
-        selectedRule=null;
-        RuleDialog ruleDialog=new RuleDialog(rules);
-        ruleDialog.showAllRules=showAllRules;
-        ruleDialog.show(DrawActivity.fragmentManager, "CIAO");
+        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;
     }
 }