/*
 * NewMarkerAction.java
 *
 * Created on June 9, 2008, 3:42 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package org.opensim.view.markerEditor;

import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle;
import org.opensim.logger.OpenSimLogger;
import org.opensim.modeling.Body;
import org.opensim.modeling.Marker;
import org.opensim.modeling.MarkerSet;
import org.opensim.modeling.Model;
import org.opensim.modeling.OpenSimContext;
import org.opensim.modeling.OpenSimObject;
import org.opensim.modeling.PhysicalFrame;
import org.opensim.modeling.Vec3;
import org.opensim.utils.FileUtils;
import org.opensim.view.ExplorerTopComponent;
import org.opensim.view.ObjectsAddedEvent;
import org.opensim.view.nodes.MarkersNode;
import org.opensim.view.pub.OpenSimDB;
import org.opensim.view.pub.ViewDB;

/**
 *
 * @author Ayman Habib
 */
public class MarkersLoadFromFileAction extends AbstractAction {

   /** Creates a new instance of NewMarkerAction */
   public MarkersLoadFromFileAction() {
      super(NbBundle.getMessage(MarkersLoadFromFileAction.class, "CTL_ImportMarkersAction"));
   }

   public void actionPerformed(ActionEvent evt) {
        Node[] selected = ExplorerTopComponent.findInstance().getExplorerManager().getSelectedNodes();
        // One MarkersNode must have been selected otherwise bail
        if (selected.length != 1 || !(selected[0] instanceof MarkersNode)) 
            return;
   
        MarkersNode markersNode = (MarkersNode) selected[0];
        Model model = markersNode.getModelForNode();
        MarkerSet markerset = model.getMarkerSet();
        // Browse for file for MarkerSet
        String fileName = FileUtils.getInstance().browseForFilename(".xml", "XML file containing markers");
        if (fileName==null) return;
        MarkerSet newMarkerSet;
        try {
            newMarkerSet = new MarkerSet(model, fileName);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
            return;
        }
        MarkerSet modelMarkerSet = model.getMarkerSet();
        OpenSimContext context = OpenSimDB.getInstance().getContext(model);
        for (int i=0; i<newMarkerSet.getSize(); i++){
            context.cacheModelAndState();
             Marker newMarker = newMarkerSet.get(i);
             // Ideally we use cloneAndAppend but the clone loses track of frame!
             modelMarkerSet.adoptAndAppend(newMarker);
            try {
                context.restoreStateFromCachedModel();
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
             // Notify the world so that navigator and visualization update
             new NewMarkerAction().addMarker(newMarker, true);
        }
        // This hack is so that the adopted Marker isn't gc'ed
        newMarkerSet.setMemoryOwner(false);
    }

}

@@ -461,24 +461,97 @@
 oException;%0A
+import org.openide.DialogDisplayer;%0Aimport org.openide.NotifyDescriptor;%0A
 import org.o
@@ -565,24 +565,24 @@
 nodes.Node;%0A
-
 import org.o
@@ -712,12 +712,89 @@
 ing.
-Body
+ArrayStr;%0Aimport org.opensim.modeling.Body;%0Aimport org.opensim.modeling.Component
 ;%0Aim
@@ -2629,148 +2629,640 @@
     
-for (int i=0; i%3CnewMarkerSet.getSize(); i++)%7B%0A            context.cacheModelAndState();%0A             Marker newMarker = newMarkerSet.get(i);
+ArrayStr existingNames = new ArrayStr();%0A        modelMarkerSet.getMarkerNames(existingNames);%0A        String errorMessages = %22%22;%0A        boolean errorsToReport = false;%0A        for (int i=0; i%3CnewMarkerSet.getSize(); i++)%7B%0A            context.cacheModelAndState();%0A             Marker newMarker = newMarkerSet.get(i);%0A             // Check name duplication%0A             if (existingNames.findIndex(newMarker.getName())!= -1)%7B%0A                 errorMessages = errorMessages.concat(%22Marker name %22+newMarker.getName()+ %22 already in use. Ignoring..%5Cn%22);%0A                 errorsToReport = true;%0A                 continue;%0A             %7D
 %0A   
@@ -3704,24 +3704,83 @@
 ker, true);%0A
+             modelMarkerSet.getMarkerNames(existingNames);%0A
         %7D%0A  
@@ -3880,21 +3880,214 @@
 ner(false);%0A
+        if (errorsToReport)%7B%0A            DialogDisplayer.getDefault().notify(%0A                    new NotifyDescriptor.Message(%22Following errors were encountered %22 + errorMessages));%0A        %7D%0A
     %7D%0A%0A%7D%0A

Check for name duplication of Markers when loading from file