Package kartoMNT

Source Code of kartoMNT.ActionSave

/**
* Ce fichier contient l'ensemble des actions qui peuvent s'appliquer sur un DocumentKarto
*
* @author HEIT Sylvestre & Coustillac Laurent
* module ActionsKarto.java
*
* Programme kartoMNT : cartographie 2D sur cartes num�riques
* Copyright (C) 2002  HEIT Sylvestre & Coustillac Laurent
*
* Ce programme est libre, vous pouvez le redistribuer et/ou le modifier selon les termes de la Licence
*Publique G�n�rale GNU publi�e par la Free Software Foundation (version 2).
*
*Ce programme est distribu� car potentiellement utile, mais SANS AUCUNE GARANTIE, ni explicite ni implicite,
*y compris les garanties de commercialisation ou d'adaptation dans un but sp�cifique. Reportez-vous � la
*Licence Publique G�n�rale GNU pour plus de d�tails.
*
*Vous devez avoir re�u une copie de la Licence Publique G�n�rale GNU en m�me temps que ce programme ; si
*ce n'est pas le cas, �crivez � la
*Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, �tats-Unis.
*/
package kartoMNT;

import javax.swing.AbstractAction;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.FileDialog;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.Rectangle;
import java.util.*;
import java.awt.*;
import java.beans.*;
import javax.swing.JFileChooser;
import java.io.*;
import java.util.zip.*;
import kartoMNT.karto.outils.MsgInfo;

/**
* Ouverture d'un document
* Cette action fait appel au gestionnaire de format (GestionnaireMntFormat)
* qui permet de d�t�cter les diff�rents formats disponibles.
*/
class ActionOpen extends AbstractAction
{
//  protected String dir;
  protected DocumentKartoContainer KmainWindow;
  protected DocumentMNTContainer MmainWindow;
  protected JFrame parent;
  protected ActionImportImage importIm;
 
  public ActionOpen(JFrame p,DocumentKartoContainer kw, DocumentMNTContainer mw,ActionImportImage imp)
  {
    super(ResourceManager.get("open"));
    setEnabled(true);
//    dir="";

    File f = new File (".");
/*    try
      {
        dir=f.getCanonicalPath();
      }
    catch(Exception ex)
      {
        System.out.println(ex.getMessage());
      }
*/    KmainWindow=kw;
    MmainWindow=mw;
    parent=p;
    importIm=imp;
  }

  public void documentSelected(Document doc)
  {}

  public void actionPerformed(ActionEvent e)
  {

    JFileChooser chooser = new JFileChooser(Options.getDirRead());
    ExampleFileFilter filterImg = new ExampleFileFilter();
    filterImg.addExtension("jpg");
    filterImg.addExtension("gif");
    filterImg.setDescription(ResourceManager.get("images"));
    ExampleFileFilter filterKarto = new ExampleFileFilter();
    filterKarto.addExtension("karto");
    filterKarto.setDescription(ResourceManager.get("kartoFile"));
   
    
    GestionnaireMntFormat.initFilter(chooser,MntFormat.IMPORT);
   
    chooser.addChoosableFileFilter(filterImg);
    chooser.addChoosableFileFilter(filterKarto);
   
    int returnVal = chooser.showOpenDialog(parent);

    if(returnVal == JFileChooser.APPROVE_OPTION) {   
     
      Options.setDirRead(chooser.getSelectedFile().getPath());
     
   
      MntFormat format = GestionnaireMntFormat.donneFormat(Options.getDirRead());
      if ((format==null)||(format.support()==MntFormat.EXPORT)){
        KartoLoader.load(parent,KmainWindow,Options.getDirRead(),importIm);
      }
      else{
        MNTLoader.load(parent,MmainWindow,Options.getDirRead(),format);
      }
             
      }
     
  }


  
  
}





////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////


/**
* Detection des contours sur la carte par application d'un filtre de Sobel
*/
class ActionSobel extends DocumentAction
  {

    public ActionSobel(DocumentKartoContainer c)
    {
      super(c,ResourceManager.get("sobelFilter"));
      mainWindow=c;
    }

    public void actionPerformed(ActionEvent e)
    {
      BufferedImage srcimg=(getCurrentDocument()).getCarte();
      if(srcimg==null)
        {
          return;
        }

      LignesDeNiveaux dst=(getCurrentDocument()).getLignesDeNiveaux();
      if(dst==null)
        {
          dst=new LignesDeNiveaux(srcimg.getWidth(),srcimg.getHeight());
          (getCurrentDocument()).setLignesDeNiveaux(dst);
        }

      //ImageOperator.sobel(dst,srcimg,70,70,70);
      //ImageOperator.sobel(dst,srcimg,60,110,130);
      //ImageOperator.sobel(dst,srcimg,70,70,70);

      ImageOperator.sobel2(dst,srcimg,100,100,100);
      //ImageOperator.sobel2(dst,srcimg,20,20,20);
      (getCurrentDocument()).setLignesDeNiveaux(dst);

    }

    protected DocumentKartoContainer mainWindow;
  }


/**
* Affichage d'un JDialog
*/
class ActionShowDialog extends DocumentAction
  {
    ActionShowDialog(JFrame parent,DocumentKartoContainer w,String name,JDialog _dialog)
    {
      //super(name);
      super(w,name);
      dialog=_dialog;
    }

    public void actionPerformed(ActionEvent e)
    {
      dialog.show();
    }

    JDialog dialog;
  };


/**
* Affichage d'un DocumentDialog
*/
class ActionShowDialog1 extends DocumentAction
  {
    DocumentDialog dialog;
   
    ActionShowDialog1(JFrame parent,DocumentKartoContainer w,String name,DocumentDialog _dialog)
    {
      super(w,name);
      dialog=_dialog;
      w.addSelectDocumentKartoListener(dialog);
    }

    public void actionPerformed(ActionEvent e)
    {
      dialog.show();
    }

   
  };

/**
* Affichage d'un JDialog
*/
class ActionShowDialog2 extends DocumentAction
  {
    ActionShowDialog2(JFrame _parent,DocumentKartoContainer w,String name,DocumentPanel _dialog,String _title)
    {
      super(w,name);
      //super(name);
      dialog=_dialog;
      parent=_parent;
      title=_title;
      w.addSelectDocumentKartoListener(dialog);
      dialog.setFrame(parent);
    }


    public void actionPerformed(ActionEvent e)
    {
      jDialog=new JDialog(parent);
      jDialog.setTitle(title);
      jDialog.getContentPane().add(dialog);
      jDialog.pack();

      //centre la fenetre
      Util.centrer(jDialog);
      jDialog.show();
      jDialog.addWindowListener(new java.awt.event.WindowAdapter()
                    {
                      public void windowClosing(java.awt.event.WindowEvent evt)
                      {
                        closeDialog(evt);
                      }
                    }
                   );
    }

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt)
    {
      jDialog.setVisible(false);
      jDialog.dispose();
      jDialog=null;
    }

    DocumentPanel dialog;
    JDialog jDialog;
    JFrame parent;
    String title;
  };



/**
  * ToolAction : selection d'un outil
  */
abstract class ActionTool extends DocumentAction
  {

    ActionTool(DocumentKartoContainer w,String name, Icon icon)
    {
      super(w,name,icon);
      t=null;
    }

    ActionTool(DocumentKartoContainer w,String name)
    {
      super(w,name);
    }

    ActionTool(DocumentKartoContainer w)
    {
      super(w);
    }

    public void documentSelected(Document new_doc)
    {
      if(doc!=null)
        {
          doc.setTool(null);
        }

      doc=(DocumentKarto)new_doc;
      if(doc==null)
        {
          setEnabled(false);
          return;
        }

      if(getSelected())
        {
          doc.setTool(t);
        }

      setEnabled(true);
    }

    public void actionPerformed(ActionEvent e)
    {
      if(doc!=null)
        {
          doc.setTool(null);
        }
      if(getCurrentDocument()!=null)
        {
          if(getSelected())
            (doc=getCurrentDocument()).setTool(t);
          else
            (doc=getCurrentDocument()).setTool(null);
        }

    }

    public Tool getTool()
    {
      return t;
    }

    protected Tool t;
    DocumentKarto doc;
  }


/**
* Action selection d'un outil contextuel : lien avec la barre d'outils
*/
class ActionToolContextual extends ActionTool
  {
    public ActionToolContextual(DocumentKartoContainer c,String name,ImageIcon icon,Tool _tool,DocumentPanel _contextualToolbar)
    {
      super(c,name,icon);
      contextualToolbar=_contextualToolbar;
      if(contextualToolbar!=null)
        c.addSelectDocumentKartoListener(contextualToolbar);
      tool=_tool;
    }

    public void documentSelected(Document doc)
    {
      t=tool;
      super.documentSelected(doc);
    }

    public void actionPerformed(ActionEvent e)
    {
      super.actionPerformed(e);
      if(getSelected())
        documentKartoContainer.setContextualToolbar(contextualToolbar);
    }

    public Tool getTool()
    {
      return tool;
    }

    DocumentPanel contextualToolbar;
    Tool tool;
  }


/**
* Selection d'un outil de dessin
*/
abstract class ActionGenericDraw extends ActionTool
  {
    ActionGenericDraw(DocumentKartoContainer w,String name,ImageIcon i,ToolkitKarto tk)
    {
      super(w,name,i);
      toolkit=tk;
    }


    public void actionPerformed(ActionEvent e)
    {
      if(getSelected())
        {
          if(doc!=null)
            {
              ((DocumentKarto)doc).getLignesDeNiveaux().setBrush(toolkit.getBrush());
            }
          BrushDialog bd;
          documentKartoContainer.setContextualToolbar(bd=new BrushDialog(documentKartoContainer));
          bd.setBrush(toolkit.getBrush());
        }
      else
        {
          documentKartoContainer.setContextualToolbar(null);
        }

      super.actionPerformed(e);
    }

    protected ToolkitKarto toolkit;
  }


/**
* Selection de l'outil "pinceau"
*/
class ActionBrush extends ActionGenericDraw
  {
    ActionBrush(DocumentKartoContainer w,ToolkitKarto tk)
    {
      super(w,ResourceManager.get("toolBrush"),new ImageIcon("icones/brush.gif"),tk);
      t=new ToolBrush();
      ((ToolBrush)t).setBrush(toolkit.getBrush());
      ((ToolBrush)t).setDiffuseAltitude(true);
    }
  }


/**
* Selection de l'outil "ligne"
*/
class ActionLine extends ActionGenericDraw
  {
    ActionLine(DocumentKartoContainer w,ToolkitKarto tk)
    {
      super(w,ResourceManager.get("toolLine"),new ImageIcon("icones/line.gif"),tk);
      t=new ToolLine();
      ((ToolLine)t).setBrush(toolkit.getBrush());
      ((ToolLine)t).setDiffuseAltitude(true);
    }

  }


/**
* Selection de l'outil "ligne bris�e", ou multi lignes
*/
class ActionMultiLine extends ActionGenericDraw
  {
    ActionMultiLine(DocumentKartoContainer w,ToolkitKarto tk)
    {
      super(w,ResourceManager.get("toolBrLine"),new ImageIcon("icones/multiline.gif"),tk);
      t=new ToolMultiLine();
      ((ToolMultiLine)t).setBrush(toolkit.getBrush());
      ((ToolMultiLine)t).setDiffuseAltitude(true);
    }

  }

/**
* Selection de l'outil "rabouteur semi automatique"
*/
class ActionSemiAuto extends ActionGenericDraw
  {
    ActionSemiAuto(DocumentKartoContainer w,ToolkitKarto tk)
    {
      super(w,ResourceManager.get("toolRabLine"),new ImageIcon("icones/semiauto.gif"),tk);
      t=new ToolSemiAuto();
      ((ToolSemiAuto)t).setBrush(toolkit.getBrush());
      ((ToolSemiAuto)t).setDiffuseAltitude(true);
    }

  }

/**
* Selection de la gomme
*/
class ActionEraser extends ActionTool
  {
    ActionEraser(DocumentKartoContainer w,ToolkitKarto tk)
    {
      super(w,ResourceManager.get("toolGom"),new ImageIcon("icones/eraser.gif"));
      toolkit=tk;
      t=new ToolBrush();
    }

    public void documentSelected(Document doc)
    {
      super.documentSelected(doc);
    }

    public void actionPerformed(ActionEvent e)
    {
      if(t!=null)
        {
          toolkit.getEraser().altitude=LignesDeNiveaux.EMPTY;
          ((ToolBrush)t).setBrush(toolkit.getEraser());
          BrushDialog bd;
          documentKartoContainer.setContextualToolbar(bd=new BrushDialog(documentKartoContainer));
          bd.setBrush(toolkit.getEraser());
          if(doc!=null)
            doc.getLignesDeNiveaux().setBrush(toolkit.getEraser());
        }
      else
        {
          documentKartoContainer.setContextualToolbar(null);
        }
      super.actionPerformed(e);
    }
    protected ToolkitKarto toolkit;
  }


/**
* Selection "placer un sommet"
*/
class ActionSommet extends ActionTool
  {
    ActionSommet(DocumentKartoContainer w)
    {
      super(w,ResourceManager.get("placeSommet"),new ImageIcon("icones/sommet.gif"));
      t=new ToolBrushPoint();
      brush=new Brush();
      brush.altitude=0.0f;
    }

    public void documentSelected(Document doc)
    {
      super.documentSelected(doc);
    }

    public void actionPerformed(ActionEvent e)
    {
      if(t!=null)
        {
          ((ToolBrushPoint)t).setBrush(brush);
          BrushDialog2 bd;
          documentKartoContainer.setContextualToolbar(bd=new BrushDialog2(documentKartoContainer));
          bd.setBrush(brush);
          if(doc!=null)
            doc.getLignesDeNiveaux().setBrush(brush);
        }
      else
        {
          documentKartoContainer.setContextualToolbar(null);
        }
      super.actionPerformed(e);
    }
    protected ToolkitKarto toolkit;
    Brush brush;
  }




/**
  * Selection "definir l'altitude d'une ligne"
  */
class ActionSelectAltitude extends ActionTool
  {
    ActionSelectAltitude(DocumentKartoContainer d,ToolkitKarto tk)
    {
      super(d,ResourceManager.get("defAltLine"),new ImageIcon("icones/selectaltitude.gif"));
      t=new ToolAltitude();
      brush=new Brush();
      brush.altitude=0;
      ((ToolAltitude)t).setBrush(brush);
    }

    public void documentSelected(Document doc)
    {
      super.documentSelected(doc);
      if((doc!=null)&&getSelected())
        ((DocumentKarto)doc).getLignesDeNiveaux().setBrush(brush);

    }

    public void actionPerformed(ActionEvent e)
    {
      if(getSelected())
        {
          if(bd==null)
            {
              bd=new BrushDialog2(documentKartoContainer);
              documentKartoContainer.addSelectDocumentKartoListener(bd);
              bd.documentSelected(documentKartoContainer.getCurrentDocument());
            }
          documentKartoContainer.setContextualToolbar(bd);
          bd.setBrush(brush);
          if(doc!=null)
            doc.getLignesDeNiveaux().setBrush(brush);
        }

      else
        documentKartoContainer.setContextualToolbar(null);
      /*if(doc!=null)
        ((DocumentKarto)doc).getLignesDeNiveaux().setBrush(toolkit.getCurrentBrush());*/

      super.actionPerformed(e);
    }
    Brush brush;
    BrushDialog2 bd;
  }


/**
  * Selection "definir l'altitude d'un groupe de lignes"
  */
class ActionSelectAltitudeMulti extends ActionTool
  {
    ActionSelectAltitudeMulti(DocumentKartoContainer d)
    {
      super(d,ResourceManager.get("defAltLines"),new ImageIcon("icones/selectaltitudemulti.gif"));
      t=new ToolAltitudeMulti();
      altitudeDialog=new AltitudeDialog(documentKartoContainer,(ToolAltitudeMulti)t);
    }

    public void actionPerformed(ActionEvent e)
    {
      super.actionPerformed(e);
      if(getSelected())
        {
          documentKartoContainer.setContextualToolbar(altitudeDialog);
          documentKartoContainer.addSelectDocumentKartoListener(altitudeDialog);
          altitudeDialog.documentSelected((getCurrentDocument()));
        }
      else
        {
          documentKartoContainer.setContextualToolbar(null);
          documentKartoContainer.removeSelectDocumentKartoListener(altitudeDialog);
        }
    }


    public Line getLine()
    {
      return ((ToolAltitudeMulti)t).getLine();
    }
    protected AltitudeDialog altitudeDialog;
  }





//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
/**
* Choix de la couleur de fond
*/
class ActionBackgroundColor extends DocumentAction
  {
    ActionBackgroundColor(DocumentKartoContainer w)
    {
      super(w,ResourceManager.get("bgColor"));
    }

    public void actionPerformed(ActionEvent e)
    {
      if(getCurrentDocument()!=null)
        {
          Color color=(getCurrentDocument()).getCalqueBackground();
          color=JColorChooser.showDialog(getCurrentDocument(),ResourceManager.get("bgColor"), color);
          if(color!=null)
            (getCurrentDocument()).setCalqueBackground(color);
        }
    }
  }

//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
/**
* Choix de la palette de couleurs pour les lignes
*/
class ActionLinesColor extends DocumentAction
  {
    ActionLinesColor(DocumentKartoContainer w)
    {
      super(w,ResourceManager.get("linesColor"));
    }

    public void actionPerformed(ActionEvent e)
    {
      if(getCurrentDocument()!=null)
        {
          Color color=(getCurrentDocument()).getFloatDataViewer().getPaletteColor(1);
          color=JColorChooser.showDialog(getCurrentDocument(),ResourceManager.get("linesColor"), color);
          if(color!=null)
            {
              (getCurrentDocument()).getFloatDataViewer().setSpecialColor(
                1,LignesDeNiveaux.LINE_WITH_NO_ALTITUDE,color);
            }

        }
    }
  }


//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////


/**
* Affinnage des lignes de niveau
*/
class ActionThinning extends DocumentAction
  {
    ActionThinning(DocumentKartoContainer w)
    {
      super(w,ResourceManager.get("affineLignes"));
    }

    public void actionPerformed(ActionEvent e)
    {
      if(getCurrentDocument()!=null)
        {
          LignesDeNiveaux l=(getCurrentDocument()).getLignesDeNiveaux();
          if(l!=null)
            {
              l.thinning();
            }
        }
    }
  }



//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
/**
* Effacer les points isol�s dans les lignes de niveau
*/
class ActionCleaning extends DocumentAction
  {
    ActionCleaning(DocumentKartoContainer w)
    {
      super(w,ResourceManager.get("supprPtIsol"));
    }

    public void actionPerformed(ActionEvent e)
    {
      if(getCurrentDocument()!=null)
        {
          LignesDeNiveaux l=(getCurrentDocument()).getLignesDeNiveaux();
          if(l!=null)
            {
              l.clearPointsIsoles();
            }
        }
    }
  }



/**
* Reinitialisation des lignes de niveau
*/
class ActionClearAll extends DocumentAction{
 
  protected Frame parent;
 
  ActionClearAll(Frame p,DocumentKartoContainer w){
    super(w,ResourceManager.get("clearAll"));
    parent = p;
  }

  public void actionPerformed(ActionEvent e){
    if(getCurrentDocument()!=null){
      // Message de confirmation
      MsgInfo msg=new MsgInfo(parent,MsgInfo.WARN,MsgInfo.OUI|MsgInfo.NON,ResourceManager.get("clearAllWarn"));
      if(msg.reponse()==MsgInfo.OUI){
        LignesDeNiveaux l=(getCurrentDocument()).getLignesDeNiveaux();
           int w = l.getXMax();
           int h = l.getYMax();
           // Nouvelles lignes de niveau vides aux dimensions des anciennes
           getCurrentDocument().setLignesDeNiveaux(new LignesDeNiveaux(w,h));
      }
    }
  }
}



/**
* Sauvegarde des lignes de niveaux dans un fichier ".ldn"
*/
class ActionSauvegarde extends DocumentAction
  {

    public ActionSauvegarde(Frame p,DocumentKartoContainer w)
    {
      super(w,ResourceManager.get("saveLines"));
      mainWindow=w;
      parent=p;

  //    dir="";
      File f = new File (".");
  /*    try
        {
          dir=f.getCanonicalPath();
        }
      catch(Exception ex)
        {
          System.out.println(ex.getMessage());
        }
  */  }

    public void actionPerformed(ActionEvent e)
    {
      if(getCurrentDocument()!=null)
        {
          LignesDeNiveaux l=(getCurrentDocument()).getLignesDeNiveaux();

          if(l!=null)
            {
              JFileChooser chooser = new JFileChooser(Options.getDirWrite());
              ExampleFileFilter filter = new ExampleFileFilter();
              filter.addExtension("ldn");
              filter.setDescription(ResourceManager.get("lines"));
              chooser.setFileFilter(filter);

              int returnVal = chooser.showSaveDialog(parent);

              if(returnVal == JFileChooser.APPROVE_OPTION)
                {
                  Options.setDirWrite(chooser.getSelectedFile().getPath()+".ldn");
                  try
                    {
                      DataOutputStream dos = new DataOutputStream (new FileOutputStream(Options.getDirWrite()));
                      l.write(dos);
                      dos.close();
                    }
                  catch(IOException ex)
                    {
                      MsgInfo msg=new MsgInfo(parent,MsgInfo.ERROR,MsgInfo.OK,ResourceManager.get("sgError")+"\n("+ex.getMessage()+")");
                    }
                }
            }
        }

    }


//    protected String dir;
    protected DocumentKartoContainer mainWindow;
    protected Frame parent;
  }


/**
* Importation d'un fichier lignes de niveau
*/
class ActionChargerLignes extends DocumentAction
  {

    ActionChargerLignes (Frame p,DocumentKartoContainer w)
    {
      super(w,ResourceManager.get("importLines"));
      mainWindow=w;
      parent=p;

//      dir="";
      File f = new File (".");
/*      try
        {
          dir=f.getCanonicalPath();
        }
      catch(Exception ex)
        {
          System.out.println(ex.getMessage());
        }
*/    }

    public void actionPerformed(ActionEvent e)
    {
      if(getCurrentDocument()!=null)
        {


          JFileChooser chooser = new JFileChooser(Options.getDirRead());
          ExampleFileFilter filter = new ExampleFileFilter();
          filter.addExtension("ldn");
          filter.setDescription(ResourceManager.get("lines"));
          chooser.setFileFilter(filter);

          int returnVal = chooser.showOpenDialog(parent);

          if(returnVal == JFileChooser.APPROVE_OPTION)
            {
              Options.setDirRead(chooser.getSelectedFile().getPath());
              try
                {
                  DataInputStream dis = new DataInputStream (new FileInputStream(Options.getDirRead()));
                  LignesDeNiveaux l = new LignesDeNiveaux();
                  l.read(dis);
                  dis.close();
                  (getCurrentDocument()).setLignesDeNiveaux(l);
                }
              catch(IOException ex)
                {
                  MsgInfo msg=new MsgInfo(parent,MsgInfo.ERROR,MsgInfo.OK,ResourceManager.get("loadLdnError")+"\n("+ex.getMessage()+")");
                }
            }
        }


    }

//    protected String dir;
    protected DocumentKartoContainer mainWindow;
    protected Frame parent;
  }



/**
* Enregistrement du document dans un nouveau fichier
*/
class ActionSaveAs extends DocumentAction
  {
   
//    protected String dir;
    protected DocumentKartoContainer mainWindow;
    protected Frame parent;
    protected ActionSave actionSave;
   
    public ActionSaveAs(Frame p,DocumentKartoContainer w,ActionSave as)
    {
      super(w,ResourceManager.get("saveAs"));
//      dir="";

      File f = new File (".");
/*      try
        {
          dir=f.getCanonicalPath();
        }
      catch(Exception ex)
        {
          System.out.println(ex.getMessage());
        }
*/      mainWindow=w;
      parent=p;
      actionSave=as;
    }


    public void actionPerformed(ActionEvent e)
    {

      JFileChooser chooser = new JFileChooser(Options.getDirWrite());
      ExampleFileFilter filter = new ExampleFileFilter();
      filter.addExtension("karto");
      filter.setDescription(ResourceManager.get("kartoDoc"));
      chooser.setFileFilter(filter);

      int returnVal = chooser.showSaveDialog(parent);


      if(returnVal == JFileChooser.APPROVE_OPTION)
        {
          String fich=chooser.getSelectedFile().getPath()+".karto";
          Options.setDirWrite(chooser.getSelectedFile().getPath());
          try
            {
              FileOutputStream fos=new FileOutputStream(fich);
              DataOutputStream dos = new DataOutputStream (fos);
              getCurrentDocument().write(dos);
              dos.close();

              /*BufferedWriter bw=new BufferedWriter(new FileWriter(chooser.getSelectedFile().getPath()));
               getCurrentDocument().write2(bw);
                bw.close();*/

              getCurrentDocument().setFileName(fich);//chooser.getSelectedFile().getPath());
              actionSave.documentSelected(getCurrentDocument());
            }
          catch(Exception ex)
            {
              getCurrentDocument().setFileName(null);
              MsgInfo msg=new MsgInfo(parent,MsgInfo.ERROR,MsgInfo.OK,ResourceManager.get("sgError")+"\n("+ex.getMessage()+")");
            }
        }
    }

    
  }

/**
* Sauvegarde du document (si il a d�j� un nom)
*/
class ActionSave extends DocumentAction
  {
    public ActionSave(Frame p,DocumentKartoContainer w)
    {
      super(w,ResourceManager.get("save"));
      mainWindow=w;
      parent=p;
    }

    public void documentSelected(Document doc)
    {
      super.documentSelected(doc);
      // action active seulement si le document a d�ja un nom
      if(doc!=null)
        {
          if(((DocumentKarto)doc).getFileName()!=null)
            {
              setEnabled(true);
            }
          else
            setEnabled(false);
        }
      else
        setEnabled(false);
    }

    public void actionPerformed(ActionEvent e)
    {
      DocumentKarto doc=(getCurrentDocument());
      try
        {

          if(doc!=null)
            if(doc.getFileName()!=null)
              {
                FileOutputStream fos=new FileOutputStream(doc.getFileName());
                DataOutputStream dos = new DataOutputStream (fos);
                doc.write(dos);
                dos.close();

                /*BufferedWriter bw=new BufferedWriter(new FileWriter(doc.getFileName()));
                doc.write(bw);
                bw.close();*/
              }
        }
      catch(Exception ex)
        {
          MsgInfo msg=new MsgInfo(parent,MsgInfo.ERROR,MsgInfo.OK,ResourceManager.get("sgError")
                      +ResourceManager.get("sgFail")+"\n("+ex.getMessage()+")");
        }
    }

    protected DocumentKartoContainer mainWindow;
    protected Frame parent;

  }


/**
* Importation d'une image de fond (carte)
*/
class ActionImportImage extends DocumentAction
  {
    public ActionImportImage(Frame p,DocumentKartoContainer w)
    {
      super(w,ResourceManager.get("importImg"));
//      dir="";

      File f = new File (".");
/*      try
        {
          dir=f.getCanonicalPath();
        }
      catch(Exception ex)
        {
          System.out.println(ex.getMessage());
        }
*/      mainWindow=w;
      parent=p;
    }


    public void actionPerformed(ActionEvent e)
    {
      DocumentKarto doc=(getCurrentDocument());
      if(doc==null)
        {
          return;
        }

      JFileChooser chooser = new JFileChooser(Options.getDirRead());
      ExampleFileFilter filter = new ExampleFileFilter();
      filter.addExtension("jpg");
      filter.addExtension("gif");
      filter.setDescription("JPG & GIF Images");
      chooser.setFileFilter(filter);

      int returnVal = chooser.showOpenDialog(parent);

      if(returnVal == JFileChooser.APPROVE_OPTION)
        {
          Options.setDirRead(chooser.getSelectedFile().getPath());
          String fich=chooser.getSelectedFile().getPath();
          BufferedImage bufim=BufferedImageIO.load(fich);

          if(bufim==null)
            {
              MsgInfo msg=new MsgInfo(parent,MsgInfo.ERROR,MsgInfo.OK,ResourceManager.get("imgLoadError")
                          +ResourceManager.get("imgLoadFail"));

            }
          else
            {
              doc.setCarte(bufim);
              doc.setCarteFileName(chooser.getSelectedFile().getPath());
            }
        }

    }

//    protected String dir;
    protected DocumentKartoContainer mainWindow;
    protected Frame parent;
  }


/**
* Adaptation de la taile des lignes de niveau avec celle de l'image de la carte
*/
class ActionAdaptSize extends DocumentAction
  {
    ActionAdaptSize(DocumentKartoContainer w)
    {
      super(w,ResourceManager.get("redimCalque"));
      mainWindow=w;
    }

    public void actionPerformed(ActionEvent e)
    {
      DocumentKarto doc=(getCurrentDocument());
      if(doc!=null)
        {
          if((doc.getCarteCanvas()!=null)&&(doc.getFloatDataViewer()!=null))
            {
              doc.adaptSizeLignesDeNiveaux();
            }
        }
    }

    protected DocumentKartoContainer mainWindow;
  };


/**
* Calibration du document
*/
class ActionCalibrate extends DocumentAction
  {
    ActionCalibrate(DocumentKartoContainer w)
    {
      super(w,ResourceManager.get("calib"));
    }

    public void actionPerformed(ActionEvent e)
    {
      DocumentKarto doc=(getCurrentDocument());
      if(doc!=null)
        {

          doc.calibrer();
        }
    }

    protected DocumentKartoContainer mainWindow;
  };
TOP

Related Classes of kartoMNT.ActionSave

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.
.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-20639858-1', 'auto'); ga('send', 'pageview');