Package gestFile

Source Code of gestFile.PreviewAll

package gestFile;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileSystemView;

import org.apache.commons.lang3.StringUtils;

import utilitaires.Extensions;

public class PreviewAll extends JPanel implements PropertyChangeListener {

  private JFileChooser chooser;
  private JLabel nom, taille, date, icone, description;
  ImageIcon thumbnail = null;
  File file = null;

  public PreviewAll() {
    // TODO Auto-generated constructor stub
  }

  public PreviewAll(JFileChooser chooser) {
    this.chooser = chooser;
    chooser.addPropertyChangeListener(this);
    setPreferredSize(new Dimension(150, 100));
    initFileDetails();

  }

  // private void ImagePreview(JFileChooser fc) {
  // setPreferredSize(new Dimension(150, 100));
  // fc.addPropertyChangeListener(this);
  // }

  private void initFileDetails() {

    add(icone = new JLabel("Icone du fichier"));
    add(description = new JLabel("Description du fichier"));
    add(nom = new JLabel("Nom du fichier"));
    add(taille = new JLabel("Taille du fichier"));
    add(date = new JLabel("Derni�re mod. du fichier"));
    this.chooser.addPropertyChangeListener(this);
    setBorder(new TitledBorder("Preview"));

  }

  public void propertyChange(PropertyChangeEvent e) {
    boolean update = false;
    boolean isFile = true;
    String prop = e.getPropertyName();

    // If the directory changed, don't show an image.
    if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
      file = null;
      clear();
      update = true;
      isFile = false;
      System.out.println("Change Dir");
      // If a file became selected, find out which one.
    } else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
      System.out.println("Change file");
      file = (File) e.getNewValue();

      if (StringUtils.equals(Extensions.getFileExtensions(file), "image")) {
        System.out.println("une image");
        update = true;
        isFile = false;
      } else {
        removeAll();
        System.out.println("Un fichier");
        initFileDetails();
        update = false;
      }

    }

    // Update the preview accordingly.
    if (update) {
      System.out.println("Dans update");
      thumbnail = null;
      if (isShowing()) {
        loadImage();
        repaint();
      }
    } else if (isFile && file != null) {

      System.out.println("Dans info File");
      FileSystemView vueSysteme = FileSystemView.getFileSystemView();
      Locale locale = Locale.getDefault();
      NumberFormat nf = NumberFormat.getInstance(locale);
      DateFormat dateFormat = DateFormat.getDateInstance(
          DateFormat.SHORT, locale);

      icone.setIcon(vueSysteme.getSystemIcon(file));

      nom.setText(vueSysteme.getSystemDisplayName(file));

      description.setText(vueSysteme.getSystemTypeDescription(file));

      String tailleFile = nf.format(file.length() / 1024.0) + " Kb";
      taille.setText(tailleFile);

      String dateFile = dateFormat.format(new Date(file.lastModified()));
      date.setText("Derni�re mod : " + dateFile);
      date.setVisible(true);

    }
  }

  private void clear() {
    icone.setIcon(null);
    nom.setText("Nom du fichier");
    description.setText("Description fichier");
    taille.setText("Taille du fichier");
    date.setText("Derni�re mod. du fichier");
  }

  public void loadImage() {
    if (file == null) {
      thumbnail = null;
      return;
    }

    // Don't use createImageIcon (which is a wrapper for getResource)
    // because the image we're trying to load is probably not one
    // of this program's own resources.
    ImageIcon tmpIcon = new ImageIcon(file.getPath());
    if (tmpIcon != null) {
      if (tmpIcon.getIconWidth() > 90) {
        thumbnail = new ImageIcon(tmpIcon.getImage().getScaledInstance(
            120, -1, Image.SCALE_DEFAULT));
      } else { // no need to miniaturize
        thumbnail = tmpIcon;
      }
    }
  }

  protected void paintComponent(Graphics g) {

    if (thumbnail == null) {
      loadImage();
    }
    if (thumbnail != null) {
      int x = getWidth() / 2 - thumbnail.getIconWidth() / 2;
      int y = getHeight() / 2 - thumbnail.getIconHeight() / 2;

      if (y < 0) {
        y = 0;
      }

      if (x < 5) {
        x = 5;
      }
      thumbnail.paintIcon(this, g, x, y);
    }
  }

}
TOP

Related Classes of gestFile.PreviewAll

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.