
package yi.iu.processor.clc;
import yi.iu.processor.ort.*;

import java.awt.*;
import java.beans.*;
import symantec.itools.multimedia.ImageViewer;
import java.awt.image.MemoryImageSource;
import java.util.Vector;
import java.io.*;

public class LineClusterFrame extends java.awt.Frame
{
        public LineCluster[] _lcs;
        public int width,height;

        public LineClusterFrame()
        {
                //{{INIT_CONTROLS
                setLayout(null);
                setSize(402,312);
                setVisible(false);
                add(imageViewer);
                imageViewer.setBounds(12,12,300,200);
                infLabel.setAlignment(java.awt.Label.CENTER);
                add(infLabel);
                infLabel.setBounds(84,276,227,23);
                setTitle("Process Result");
                //}}

                //{{REGISTER_LISTENERS
                SymWindow aSymWindow = new SymWindow();
                this.addWindowListener(aSymWindow);
                SymMouseMotion aSymMouseMotion = new SymMouseMotion();
                imageViewer.addMouseMotionListener(aSymMouseMotion);
                SymAction lSymAction = new SymAction();
                SaveAsMenuItem.addActionListener(lSymAction);
                SymMouse aSymMouse = new SymMouse();
                imageViewer.addMouseListener(aSymMouse);
                //}}

                //{{INIT_MENUS
                file.setLabel("File");
                file.add(SaveAsMenuItem);
                SaveAsMenuItem.setLabel("Save As");
                menuBar1.add(file);
                //$$ menuBar1.move(0,360);
                setMenuBar(menuBar1);
                //}}
        }

        // initialize the display frame with the image data and image width & height
        public LineClusterFrame(String name, Dimension d, LineCluster[] lcs)
        {
            this();
            _lcs= lcs;
            width=d.width; height=d.height;
            int[] data= new int[width*height];

        int i, j;
        int[] label= new int[d.width*d.height];
        for (i=0;i<lcs.length;i++) {
            if (lcs[i]==null) continue;
            for (j=0;j<lcs[i].size();j++) {
                ORTLine line= lcs[i].getLineAt(j);
                DrawUtil.ort2image(label, d, line, i+1, 1);
            }
        }
        int[] cluster_img= yi.util.ImageUtil.makeNewImageFromLabel(label);
            Image img = createImage(new MemoryImageSource
                (width, height, cluster_img, 0, width));

                try {
                    imageViewer.setStyle(ImageViewer.IMAGE_SCALED_TO_FIT);
                    imageViewer.setImage(img);
                }
                catch (PropertyVetoException e) {
                    System.err.println("can't set image to viewer!");
                }

            // change the default set of the image viewer
                int dw=0;
                if (width<128) dw=(128-width)/2;
                setSize(width+24+dw*2,height+60);
                imageViewer.setBounds(12+dw,12,width,height);
                infLabel.setBounds(width/2+12-112+dw,height+36,224,24);
        setTitle(name);
        }

        public void setVisible(boolean b)
        {
                if(b)
                {
                        setLocation(50, 50);
                }
            super.setVisible(b);
        }

        public void addNotify()
        {
                // Record the size of the window prior to calling parents addNotify.
                Dimension d = getSize();

                super.addNotify();

                if (fComponentsAdjusted)
                        return;

                // Adjust components according to the insets
                Insets ins = getInsets();
                setSize(ins.left + ins.right + d.width, ins.top + ins.bottom + d.height);
                Component components[] = getComponents();
                for (int i = 0; i < components.length; i++)
                        {
                        Point p = components[i].getLocation();
                        p.translate(ins.left, ins.top);
                        components[i].setLocation(p);
                }
                fComponentsAdjusted = true;
        }

        // Used for addNotify check.
        boolean fComponentsAdjusted = false;

        class SymWindow extends java.awt.event.WindowAdapter
        {
                public void windowClosing(java.awt.event.WindowEvent event)
                {
                Object object = event.getSource();
                if (object == LineClusterFrame.this)
                        LineClusterFrame_WindowClosing(event);
                }
        }

        void LineClusterFrame_WindowClosing(java.awt.event.WindowEvent event)
        {
                dispose();		 // dispose of the Frame.
        }
        //{{DECLARE_CONTROLS
        symantec.itools.multimedia.ImageViewer imageViewer = new symantec.itools.multimedia.ImageViewer();
        java.awt.Label infLabel = new java.awt.Label();
        //}}

        //{{DECLARE_MENUS
        java.awt.MenuBar menuBar1 = new java.awt.MenuBar();
        java.awt.Menu file = new java.awt.Menu();
        java.awt.MenuItem SaveAsMenuItem = new java.awt.MenuItem();
        //}}


        class SymMouseMotion extends java.awt.event.MouseMotionAdapter
        {
                public void mouseMoved(java.awt.event.MouseEvent event)
                {
                        Object object = event.getSource();
                        if (object == imageViewer)
                                imageViewer_mouseMoved(event);
                }
        }

        void imageViewer_mouseMoved(java.awt.event.MouseEvent event)
        {
                // get the mouse position based on the image coordination
                int x= event.getX(),y=event.getY();
                if (x>=width) x=width-1;
                if (y>=height) y=height-1;
                if (x<0) x=0;
                if (y<0) y=0;

        // display the information based on the position of mouse
                displayInfo(x,y);
        }

        class SymAction implements java.awt.event.ActionListener
        {
                public void actionPerformed(java.awt.event.ActionEvent event)
                {
                        Object object = event.getSource();
                        if (object == SaveAsMenuItem)
                                SaveAsMenuItem_ActionPerformed(event);
                }
        }

        void SaveAsMenuItem_ActionPerformed(java.awt.event.ActionEvent event)
        {
            /*
                FileDialog fd= new FileDialog(this, "Save the result", FileDialog.SAVE);
                fd.show();
                String fileName=fd.getDirectory()+fd.getFile();
                if (fileName.endsWith("ppm")) yi.util.io.OutputResult.outputColorImage(fileName, imageData, width, height);
                else if (fileName.endsWith("pgm")) yi.util.io.OutputResult.outputGrayImage(fileName, imageData, width, height);
                else System.err.println("Can't save image format other than ppm and pgm");
                */
        }

        class SymMouse extends java.awt.event.MouseAdapter
        {
                public void mouseReleased(java.awt.event.MouseEvent event)
                {
                        Object object = event.getSource();
                        if (object == imageViewer)
                                imageViewer_mouseReleased(event);
                }

                public void mouseExited(java.awt.event.MouseEvent event)
                {
                        Object object = event.getSource();
                        if (object == imageViewer)
                                imageViewer_mouseExited(event);
                }

                public void mousePressed(java.awt.event.MouseEvent event)
                {
                        Object object = event.getSource();
                        if (object == imageViewer)
                                imageViewer_mousePressed(event);
                }
        }

        int press_x;
        int press_y;
        void imageViewer_mousePressed(java.awt.event.MouseEvent event)
        {
                // get the mouse position based on the image coordination
                int x= event.getX(),y=event.getY();
                if (x>=width) x=width-1;
                if (y>=height) y=height-1;
                if (x<0) x=0;
                if (y<0) y=0;

        press_x=x; press_y=y;
        // display the information based on the position of mouse
                pressInfo(x,y);
        }

        public void pressInfo(int x, int y)
        {
/*
                if (seg==null) return;
                int label=seg[y*width+x];

                // mask the image and only the segment with number of blob counter is remained
                int[] display= new int[width*height];
                int area=0;
                for (int i=0; i<seg.length; i++) {
                    if (seg[i]==label) {display[i]=imageData[i];area++;}
                    else display[i]=0xff000000;
                }
        //if (VERBOSE) System.err.println(area);

                //display the masked image
            Image im = createImage(new MemoryImageSource(width, height, display, 0, width));
                try {
                    imageViewer.setImage(im);
                }
                catch (Exception e) {
                    System.err.println("Can't set image!");
                }
                imageViewer.repaint();
*/
        }

        public void displayInfo(int x, int y)
        {
            int i,j;
            boolean[] yes= new boolean[_lcs.length];
            for (i=0;i<yes.length;i++) yes[i]=false;
            for (i=0;i<_lcs.length;i++) {
                if (_lcs[i]==null) continue;
                for (j=0;j<_lcs[j].size();j++) {
                    ORTLine l= _lcs[j].getLineAt(j);
                    if (l.cross(width, height, x, y)) {
                        yes[i]= true;
                        break;
                    }
                }
            }

            String s= new String();
            for (i=0;i<yes.length;i++) {
                if (yes[i]) s+="("+i+") ";
            }

                infLabel.setText("("+x+","+y+"): "+s);
        }

    public void mouseExit()
    {
        /*
        //display the masked image
            Image im = createImage(new MemoryImageSource(width, height, imageData, 0, width));
                try {
                    imageViewer.setImage(im);
                }
                catch (Exception e) {
                    System.err.println("Can't set image!");
                }
                imageViewer.repaint();
                */
    }


        void imageViewer_mouseExited(java.awt.event.MouseEvent event)
        {
                mouseExit();
        }

        void imageViewer_mouseReleased(java.awt.event.MouseEvent event)
        {
                // get the mouse position based on the image coordination
                int x= event.getX(),y=event.getY();
                if (x>=width) x=width-1;
                if (y>=height) y=height-1;
                if (x<0) x=0;
                if (y<0) y=0;

        if (press_x!=x||press_y!=y)
        // display the information based on the position of mouse
                selectInfo(press_x, press_y,x,y);
        }

        public void selectInfo(int x1, int y1, int x2, int y2)
        {
                return;
        }

}