OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

package org.openconcerto.modules.label.graphicspl;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

public class GPLDemoFrame extends JFrame {
    public GPLDemoFrame() {
        JPanel p = new JPanel();

        p.add(new JLabel("Drop your graphicspl file here"));
        p.setDropTarget(new DropTarget() {
            public synchronized void drop(DropTargetDropEvent evt) {
                try {
                    evt.acceptDrop(DnDConstants.ACTION_COPY);
                    List<File> droppedFiles = (List<File>) evt.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
                    for (File file : droppedFiles) {
                        System.out.println("GPLDemoFrame.GPLDemoFrame().new DropTarget() {...}.drop()" + file.getAbsolutePath());
                        if (file.getName().endsWith(".graphicspl")) {
                            open(file);
                            break;
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        this.setContentPane(p);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    long last;

    protected void open(File file) throws Exception {
        loadFile(file);
        last = file.lastModified();
        Timer timer = new Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("check");
                long l = file.lastModified();
                if (l != last) {
                    last = l;
                    // reload
                    System.out.println("check reload");
                    try {
                        loadFile(file);
                    } catch (ParserConfigurationException | SAXException | IOException e1) {
                        e1.printStackTrace();
                    }

                }

            }
        });
        timer.setRepeats(true);
        timer.start();

    }

    public void loadFile(File file) throws ParserConfigurationException, SAXException, IOException {
        GraphicsPL g = new GraphicsPL();
        g.load(file);
        BufferedImage img = g.createImage(1);
        ImageIO.write(img, "png", new File(file.getParent(), file.getName() + ".png"));
        System.out.println("GPLDemoFrame.loadFile() " + img.getWidth() + " x " + img.getHeight());
        final Dimension dimension = new Dimension(img.getWidth(), img.getHeight());
        JPanel iPanel = new JPanel() {
            @Override
            public Dimension getMinimumSize() {

                return dimension;
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(img.getWidth(), img.getHeight());
            }

            @Override
            public Dimension getMaximumSize() {
                return new Dimension(img.getWidth(), img.getHeight());
            }

            @Override
            public void paint(Graphics g) {
                g.drawImage(img, 0, 0, null);
            }

        };
        this.invalidate();
        this.setContentPane(iPanel);
        this.setSize(dimension);
        this.revalidate();
        this.repaint();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e) {
                    e.printStackTrace();
                }

                GPLDemoFrame f = new GPLDemoFrame();
                f.setSize(300, 200);
                f.setVisible(true);
                f.setLocationRelativeTo(null);
            }
        });

    }

}