Package kartoMNT

Source Code of kartoMNT.DocumentKarto

/**
* Document Karto:
* Un document est constitu� d'un ensemble de calques:
*    -Une image
*    -Des lignes de niveaux
*    -Des lignes de carroyage
*    -Un ensemble de points de calibration
*
* @author HEIT Sylvestre & Coustillac Laurent
*
*
* Programme kartoMNT : g�n�ration de MNT � partir d'une carte de lignes de niveaux
* 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 java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
import javax.swing.border.*;
import javax.swing.*;
import java.util.*;
import java.util.zip.DataFormatException;
import kartoMNT.karto.math.*;
import java.awt.geom.*;
import kartoMNT.karto.outils.MsgInfo;

/**
* Extension de JLayeredPane
* getPreferredSize() retourne une dimension qui est la taille minimale permettant de visualiser
* tout les composants en entier dans le JLayeredPane
*/
class JLayeredPane2 extends JLayeredPane{
   
    JLayeredPane2(){
        super();
    }

  public Dimension getPreferredSize(){
        int i,maxx,maxy;
        Component c;
        if(getComponentCount()==0){
            maxx=0;
            maxy=0;
      }else{
            c=getComponent(0);
            maxx=c.getX()+c.getWidth();
            maxy=c.getY()+c.getHeight();
        }
        for(i=1;i<getComponentCount();i++){
            c=getComponent(i);
            maxx=Math.max(c.getX()+c.getWidth(),maxx);
            maxy=Math.max(c.getY()+c.getHeight(),maxy);
        }

        return new Dimension(maxx,maxy);
    }
}



/**
* Modelise un document de Karto: carte, lignes de niveau,...
*/
public class DocumentKarto extends JPanel implements ComponentListener,PointCalibrationSetListener,Document {
 
  ImageCanvas carte;
    FloatDataViewer floatDataViewer;
    Carroyage carroyage;
    Proj calibration;

    public JLayeredPane2 layeredPane;
    //private JLayeredPane3 firstLayeredPane;
    private JPanel glass;
    protected Tool tool;
    protected ObjectReferential referential;   // referentiel: position et facteur d'echelle pour tout les objets

    Hashtable names;
    ToolkitKarto toolkit;
    String carteFileName;   // nom du fichier contenant la carte
    String fileName;        // nom du fichier contenant ce document
    String name;   // nom apparaissant dans la barre de titre (si fileName==NULL)

    protected DocumentKartoContainer documentKartoContainer;
   
    protected PointCalibrationSet pointsDeCalibration;
    Hashtable pointDeCalibration_model;

    Frame frameParent;
 
    public DocumentKarto(DocumentKartoContainer cont,Frame frame){
        super();
        frameParent=frame;

        names = new Hashtable();
        referential=new ObjectReferential();
        documentKartoContainer=cont;

        createInterface();
        int i;
        Component c;
        for(i=0;i<layeredPane.getComponentCount();i++){
            c=layeredPane.getComponent(i);
            ((GraphicObject)c).setReferential(referential);
    }
        pointsDeCalibration=new PointCalibrationSet();
        pointsDeCalibration.addSetListener(this);
        pointDeCalibration_model=new Hashtable();
        calibration=null;
        carroyage=null;
        calibrationPointsMoveable=false;
        name=ResourceManager.get("kartoDoc")+" ("+ResourceManager.get("noTitle")+")";
    }

    public void selected(){
        if(documentKartoContainer!=null)
            documentKartoContainer.notifySelectDocumentKartoListener(this);
    }

    public void pointCalibrationAdded(PointCalibrationModel p){
        PointCalibration pc=new PointCalibration();
        pc.setMoveable(calibrationPointsMoveable);
        pc.setReferential(referential);
        layeredPane.add(pc,new Integer(2));
        pc.setModel(p);
        pointDeCalibration_model.put(p,pc);
    }

    public void pointCalibrationRemoved(PointCalibrationModel p){
        PointCalibration pcal=(PointCalibration)pointDeCalibration_model.get(p);
        if(pcal!=null){
            layeredPane.remove(pcal);
            layeredPane.repaint();       // bug de java?      
         }
  }

    PointCalibrationSet getPointCalibrationSet(){
        return pointsDeCalibration;
    }

  public void write (DataOutputStream dos) throws IOException {
        dos.writeUTF("KartoMNT");
        if(getCarteFileName()!=null){
      if(getCarte()!=null){
              dos.writeUTF("ImageName");
        if(getFileName()!=null){
        // on ecrit le chemin relatif
                    File f=FileUtil.getRelativePath(new File(getFileName()),new File(getCarteFileName()));
                    dos.writeUTF(""+f);
                }else{
                    dos.writeUTF(getCarteFileName());
                }
            }
        }
        if(getLignesDeNiveaux()!=null){
            dos.writeUTF("LignesDeNiveaux");
            getLignesDeNiveaux().write(dos);
        }
       
        // sauvegarder aussi points de calibration
        if(pointsDeCalibration!=null){
            dos.writeUTF("PointCalibrationSet");
            pointsDeCalibration.write(dos);
        }

        dos.writeUTF("End");
    }

    public void read (DataInputStream dis) throws IOException,DocumentKartoException {
        String s;
        s=dis.readUTF();
        if(!s.equalsIgnoreCase("KARTOMNT"))
            throw new DocumentKartoException(DocumentKartoException.FORMAT_FICHIER_INVALIDE);

        s=dis.readUTF();
        while(!s.equalsIgnoreCase("End")){
            if(s.equalsIgnoreCase("ImageName")){
              String name=dis.readUTF();
        if(getFileName()!=null){
                    File f;
                    f=FileUtil.resolveRelativePath(new File(getFileName()),new File(name));
                    if(f!=null){
                      name=f.getCanonicalPath();
                    }
                }

                setCarte(BufferedImageIO.load(name));
                setCarteFileName(name);
            } else if(s.equalsIgnoreCase("LignesDeNiveaux")) {
                LignesDeNiveaux ldn=new LignesDeNiveaux();
                ldn.read(dis);
                setLignesDeNiveaux(ldn);
            } else if(s.equalsIgnoreCase("PointCalibrationSet")){
                pointsDeCalibration.read(dis);
                if(pointsDeCalibration.size()>=3)
                  try{
                      calibrer();
                    }catch (Exception ex){
                        throw new DocumentKartoException(DocumentKartoException.CALIBRATION_IMPOSSIBLE);
                    }
           } else throw new DocumentKartoException(DocumentKartoException.FORMAT_FICHIER_INVALIDE);
            s=dis.readUTF();
        }
    }

    /*
    #kartoMNT
    [image]
    fileName="z.gif"
    [pointsdecalibration]
    nombrePoint=11
    point1=10 30 50
    [lignes de niveaux]
    */

    public void write2 (BufferedWriter dos) throws IOException {
            dos.write("#KartoMNT");
            dos.newLine();
            if(getCarteFileName()!=null)
                {

                    if(getCarte()!=null)
                        {
                            dos.write("[Image]");
                            dos.newLine();

                            if(getFileName()!=null)
                                {// on ecrit le chemin relatif

                                    File f=FileUtil.getRelativePath(new File(getFileName()),new File(getCarteFileName()));
                                    dos.write("Name=\""+f+"\"");
                                    dos.newLine();
                                }
                            else
                                {
                                    dos.write("Name=\""+getCarteFileName()+"\"");
                                    dos.newLine();
                                }
                        }

                }
            // sauvegarder aussi points de calibration
            if(pointsDeCalibration!=null)
                {
                    dos.write("[Points de Calibration]");
                    dos.newLine();
                    pointsDeCalibration.write2(dos);
                }
            if(getLignesDeNiveaux()!=null)
                {
                    dos.write("[LignesDeNiveaux]");
                    dos.newLine();
                    //getLignesDeNiveaux().write(dos);
                }

        }



    public void calibrer()
    {
        Point2D.Double[] p1=new Point2D.Double[pointsDeCalibration.size()];
        Point2D.Double[] p2=new Point2D.Double[pointsDeCalibration.size()];

        ListIterator it=pointsDeCalibration.listIterator();
        int i=0;
        while(it.hasNext())
            {
                PointCalibrationModel p=(PointCalibrationModel)it.next();

                p1[i]=new Point2D.Double();
                p1[i].x=p.getImageX();
                p1[i].y=p.getImageY();

                p2[i]=new Point2D.Double();
                p2[i].x=p.getX();
                p2[i].y=p.getY();

                i++;
            }
        try
            {
                if(carroyage==null)
                    {
                        carroyage=new Carroyage();
                        addObject(3,carroyage);
                    }
                calibration=new Proj(p2,p1);
                carroyage.setProj(calibration,getLignesDeNiveaux().getXMax(),getLignesDeNiveaux().getYMax());
            }
        catch(Exception ex)
            {

                MsgInfo msg=new MsgInfo(frameParent,MsgInfo.ERROR,MsgInfo.OK,ResourceManager.get("calibError")
                                        +"\n("+ex.getMessage()+")");

                calibration=null;
            }
    }

    public boolean isCalibrated()
    {
        return (calibration!=null);
    }

    public Proj getProjection()
    {
        return calibration;
    }

    public Carroyage getCarroyage()
    {
        return carroyage;
    }

    /**
     * indique si on authorise le deplacement de points de calibration � la souris
     */
    void setCalibrationPointsMoveable(boolean b)
    {
        calibrationPointsMoveable=b;
        ListIterator it=pointsDeCalibration.listIterator();
        while(it.hasNext())
            {
                PointCalibrationModel p=(PointCalibrationModel)it.next();
                PointCalibration pc=(PointCalibration)pointDeCalibration_model.get(p);
                pc.setMoveable(b);
            }
    }
    private boolean calibrationPointsMoveable;

    /*public void setDocumentKartoContainer(DocumentKartoContainer cont)
    {
        container=cont;
        if(container!=null)
            {
                container.notifySelectDocumentListener(this);
                container.setDocumentName(this,fileName);
            }
  }*/

    public void setTool(Tool t)
    {
        if(tool!=null)
            tool.attach(null);
        tool=t;
        if(tool!=null)
            tool.attach(this);
    }

    public void setCarte(BufferedImage bufim)
    {
        carte.set(bufim);
        if(bufim!=null)
            {
                if(getLignesDeNiveaux()==null)
                    setLignesDeNiveaux(new LignesDeNiveaux(bufim.getWidth(),bufim.getHeight() ));
            }
        else
            setLignesDeNiveaux(null);

    }

    public void setCarteFileName(String name)
    {
        carteFileName=name;
    }

    public String getCarteFileName()
    {
        return carteFileName;
    }

    public void setFileName(String name)
    {
        fileName=name;
        if(documentKartoContainer!=null)
            {
                //documentKartoContainer.notifySelectDocumentKartoListener(this);
                documentKartoContainer.setDocumentName(this,name);
            }
    }

    public String getFileName()
    {
        return fileName;
    }

    public String getName()
    {
        if(fileName==null)
            return name;
        else
            return fileName;
    }

    public void setName(String n)
    {
        name=n;
    }
 
  /**
   * Adapte la taille des lignes de niveau � celle de la carte
   */
    public void adaptSizeLignesDeNiveaux()
    {
        if(getLignesDeNiveaux()!=null)
            {
                getLignesDeNiveaux().resize(getCarte().getWidth(),getCarte().getHeight());
            }
        else
            {
                setLignesDeNiveaux(new LignesDeNiveaux(getCarte().getWidth(),getCarte().getHeight()));
            }
    }

    public void setLignesDeNiveaux(LignesDeNiveaux l)
    {
        floatDataViewer.setLignesDeNiveaux(l);
    }

    public LignesDeNiveaux getLignesDeNiveaux()
    {
        return (LignesDeNiveaux)floatDataViewer.getData(); ///////////////////////////
    }

    public FloatDataViewer getFloatDataViewer()
    {
        return floatDataViewer;
    }

    public FloatData getFloatData()
    {
        return floatDataViewer.getData();
    }


    public BufferedImage getCarte()
    {
        return carte.get();
    }

    public ImageCanvas getCarteCanvas()
    {
        return carte;
    }

    public JLayeredPane2 getLayeredPane()
    {
        return layeredPane;
    }

    /*public void addCalibrationPoint(int x,int y)
    {
        PointCalibration pc=new PointCalibration(25);
        pc.setReferential(referential);
        layeredPane.add(pc,new Integer(2));
        pc.setObjectPosition(pc.canvas2ObjX(x),pc.canvas2ObjY(y));
  }*/

    /**
      * Ajoute un objet
      * Les couches 0,1 et 2 sont occup�es par la carte, les lignes de niveaux et les points de calibration
      * @param i numero de la couche dans laquelle on ajoute l'objet
      * @param object object � ajouter
      */
    public void addObject(int i,GraphicObject object)
    {
        object.setReferential(referential);
        layeredPane.add(object,new Integer(i));
    }

    /**
      * Enleve un objet
      * @param object object � enlever
      */
    public void removeObject(GraphicObject object)
    {
        if((object!=carte) && (object!=floatDataViewer))
            {
                layeredPane.remove(object);
                layeredPane.repaint();       // bug de java?
            }
    }

    public boolean containsObject(GraphicObject object)
    {
        return layeredPane.isAncestorOf(object);
    }

    // retourne le poprietes du calque
    public ProprietesCalque getProprietesCalque(int i)
    {
        return (ProprietesCalque)(names.get(new Integer(i)));
    }

    public int getNbNamedCalques()
    {
        return names.size();
    }

    public void setCalqueVisible(int i,boolean b)
    {
        ProprietesCalque p=getProprietesCalque(i);
        if(p!=null)
            p.visible=b;
        else
            return;
        Component[] c=layeredPane.getComponentsInLayer(i);
        for(int a=0;a<c.length;a++)
            {
                c[a].setVisible(b);
            }
    }

    public void setAlpha(int i,double alpha)
    {
        ProprietesCalque p=getProprietesCalque(i);
        if(p!=null)
            p.alpha=alpha;
        else
            return;
        Component[] c=layeredPane.getComponentsInLayer(i);
        for(int a=0;a<c.length;a++)
            {
                ((GraphicObject)c[a]).setAlpha(alpha);
            }
    }

    /*
     * definit la couleur d'arri�re plan (visible uniquement si les objets des calques ne le cachent pas)
     */
    public void setCalqueBackground(Color color)
    {
        layeredPane.setBackground(color);
    }

    public Color getCalqueBackground()
    {
        return layeredPane.getBackground();
    }


    public boolean isCalqueVisible(int i)
    {
        ProprietesCalque p=getProprietesCalque(i);
        if(p!=null)
            return p.visible;
        else
            return false;
    }

    public double getAlpha(int i)
    {
        ProprietesCalque p=getProprietesCalque(i);
        if(p!=null)
            return p.alpha;
        else
            return 1.0;
    }


    /* definit le type d'interpolation
     * ImageCanvas.TYPE_BILINEAR ou ImageCanvas.TYPE_NEAREST_NEIGHBOR
     */
    public void setInterpolationType(int t)
    {
        carte.setInterpolationType(t);
        floatDataViewer.setInterpolationType(t);
    }

    public int getInterpolationType()
    {
        if(carte!=null)
            return carte.getInterpolationType();
        else if(floatDataViewer!=null)
            return floatDataViewer.getInterpolationType();
        else
            return 0;
    }

    public int getNbCalques()
    {
        return layeredPane.highestLayer()+1;
    }

    public double getScaleX()
    {
        return referential.scalex;
    }

    public double getScaleY()
    {
        return referential.scaley;
    }


    public void setScale(double sx,double sy)
    {
        referential.scalex=sx;
        referential.scaley=sy;
        int i;
        GraphicObject c;
        for(i=0;i<layeredPane.getComponentCount();i++)
            {
                c=(GraphicObject)layeredPane.getComponent(i);
                c.updateReferential();
            }
    }


    private void createInterface()
    {
        //calque=new ImageCanvas[2];

        layeredPane = new JLayeredPane2();
        //firstLayeredPane = new JLayeredPane3();
        layeredPane.setOpaque(true);
        int i;

        names.put(new Integer(0),new ProprietesCalque(0,ResourceManager.get("map")));

        carte=new ImageCanvas();
        layeredPane.add(carte,new Integer(0));
        carte.setOpaque(false);

        floatDataViewer=new FloatDataViewer();
        layeredPane.add(floatDataViewer,new Integer(1));
        floatDataViewer.setOpaque(false);

        names.put(new Integer(1),new ProprietesCalque(1,ResourceManager.get("lines")));
        names.put(new Integer(2),new ProprietesCalque(2,ResourceManager.get("calibPoints")));
        names.put(new Integer(3),new ProprietesCalque(3,ResourceManager.get("carroyage")));

        //setViewportView(layeredPane);
        setLayout(new BorderLayout());
        add(layeredPane,BorderLayout.CENTER);
        setBackground(Color.black);
    
    
        setScale(1,1);
    }



    public void componentHidden(ComponentEvent e)
    {}

    public void componentMoved(ComponentEvent e)
    {}

    public void componentResized(ComponentEvent e)
    {
        /*glass.setPreferredSize(firstLayeredPane.getSize());
        glass.setSize(firstLayeredPane.getSize());*/
    }

    public void componentShown(ComponentEvent e)
    {}


   
}

TOP

Related Classes of kartoMNT.DocumentKarto

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.