/**
* Copyright: t3am_C9 (Alexander Schäffer, Johannes Ebersold, Sebastian Geib, Thomas Kisiel)
*
* This file is part of GifToApngConverter
*
* GifToApngConverter is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GifToApngConverter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GifToApngConverter. If not, see <a href="http://www.gnu.org/licenses/">here</a>
*/
package gui;
import java.awt.GridLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
* has the Textfields, Filechooser and Source-/Destinationbutton.
*
* @author Sebastian Geib
* @author Alexander Schäffer
*/
class PathJPanel extends JPanel {
private static final long serialVersionUID = 1L;
/**
* png filter for a JFileChooser(mFileChooser)
*/
protected FileNameExtensionFilter filterPng;
/**
* png filter for a JFileChooser(mFileChooser)
*/
protected FileNameExtensionFilter filterAPng;
/**
* gif filter for a JFileChooser(mFileChooser)
*/
protected FileNameExtensionFilter filterGif;
/**
* the Textfield which contains the Source-Filepath.
*/
private JTextField mTextFieldSource = new JTextField(20);
/**
* the Button to open mFileChooser for choosing the Source-File.
*/
private JButton mButtonSource = new JButton("Select Source File");
/**
* the Textfield which contains the Destination-Filepath.
*/
private JTextField mTextFieldDestination = new JTextField(20);
/**
* the FileChooser which is called by click of mButtonSource or mButtonDestination.
*/
private JFileChooser mFileChooser = new JFileChooser();
/**
* the Button to open the FileChooser for choosing the Destination-File.
*/
private JButton mButtonDestination = new JButton("Select Target File");
/**
* Constructs a new PathJPanel<br><br>
*
* This constructor sets a GridLayout (2,2) and adds mTextFieldSource, mButtonSource,
* mTextFieldDestination, mButtonDestination and mFileChooser to the Panel.<br><br>
*
* Also an ActionListener(to open the FileChooser by a click)
* is added to mButtonSource and to mButtonDestination .
*
*/
PathJPanel(){
this.setLayout(new GridLayout(2,2));
filterAPng = new FileNameExtensionFilter("APng-Images","apng");
mFileChooser.addChoosableFileFilter(filterAPng);
filterPng = new FileNameExtensionFilter("Png-Images","png");
mFileChooser.addChoosableFileFilter(filterPng);
filterGif = new FileNameExtensionFilter("Gif-Images","gif");
mFileChooser.addChoosableFileFilter(filterGif);
mFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
mButtonSource.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
mFileChooser.setFileFilter(filterGif);
mFileChooser.setSelectedFile(new File(mTextFieldSource.getText()));
mFileChooser.setVisible(true);
int iRetVal = mFileChooser.showOpenDialog(EJFrame.getEJFrame());
if(iRetVal == JFileChooser.CANCEL_OPTION){
//do nothing
}
else if(iRetVal == JFileChooser.APPROVE_OPTION){
setFileToTextfields(mFileChooser.getSelectedFile());
}
}
});
DropTargetListener dropTargetSourceListener =
new DropTargetListener() {
public void dragEnter(DropTargetDragEvent e) {}
public void dragExit(DropTargetEvent e) {}
public void dragOver(DropTargetDragEvent e) {}
@SuppressWarnings("unchecked")
public void drop(DropTargetDropEvent e) {
try {
Transferable tr = e.getTransferable();
DataFlavor[] flavors = tr.getTransferDataFlavors();
for (int i = 0; i < flavors.length; i++)
if (flavors[i].isFlavorJavaFileListType()) {
// accept drop
e.acceptDrop (e.getDropAction());
List<File> files = (List<File>) tr.getTransferData(flavors[i]);
File dropFile = files.get(0);
setFileToTextfields(dropFile);
e.dropComplete(true);
return;
}
} catch (Throwable t) {}
e.rejectDrop();
}
public void dropActionChanged(
DropTargetDragEvent e) {}
};
DropTargetListener dropTargetDestinationListener =
new DropTargetListener() {
public void dragEnter(DropTargetDragEvent e) {}
public void dragExit(DropTargetEvent e) {}
public void dragOver(DropTargetDragEvent e) {}
@SuppressWarnings("unchecked")
public void drop(DropTargetDropEvent e) {
try {
Transferable tr = e.getTransferable();
DataFlavor[] flavors = tr.getTransferDataFlavors();
for (int i = 0; i < flavors.length; i++)
if (flavors[i].isFlavorJavaFileListType()) {
// accept drop
e.acceptDrop (e.getDropAction());
List<File> files = (List<File>) tr.getTransferData(flavors[i]);
File dropFile = files.get(0);
mTextFieldDestination.setText(dropFile.getAbsolutePath());
e.dropComplete(true);
return;
}
} catch (Throwable t) {}
e.rejectDrop();
}
public void dropActionChanged(
DropTargetDragEvent e) {}
};
new DropTarget(mTextFieldSource, dropTargetSourceListener);
new DropTarget(mTextFieldDestination,dropTargetDestinationListener);
mButtonDestination.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
mFileChooser.setFileFilter(filterAPng);
mFileChooser.setSelectedFile(new File(mTextFieldDestination.getText()));
mFileChooser.setVisible(true);
int iRetVal = mFileChooser.showSaveDialog(EJFrame.getEJFrame());
if(iRetVal == JFileChooser.CANCEL_OPTION){
//do nothing
}
else if(iRetVal == JFileChooser.APPROVE_OPTION){
mTextFieldDestination.setText(mFileChooser.getSelectedFile().getAbsolutePath());
}
}
});
this.add(mTextFieldSource);
this.add(mButtonSource);
this.add(mTextFieldDestination);
this.add(mButtonDestination);
this.setVisible(true);
}
/**
* returns the text of the Source TextField.
* @return the text entered in mTextFieldSource
*/
String getSourcePath(){
return mTextFieldSource.getText();
}
/**
* returns the text of the Destination TextField.
* @return the text entered in mTextFieldDestination
*/
String getDestinationPath(){
return mTextFieldDestination.getText();
}
/**
* // i don't think that we have to force a png extension
* tests if the file represented by f is a .png/.PNG.
* @param f the File Object which represents the Destination(.png) File
* @return true, if f represents a .png or .PNG file
*/
static boolean isPng(File f){
if(!f.isDirectory() && f.getName().length()>=4){
StringBuffer sb = new StringBuffer(f.getName());
//returns only true if the pathname represents a png file
return sb.substring(sb.length()-3).toLowerCase().equals("png");
} else {
return false;//no, directories are no png-files
}
}
/**
* tests if the file represented by f is a .gif/.GIF.
* @param f the File Object which represents the Source(.gif) File
* @return true, if f represents a .gif or .GIF file
*/
static boolean isGif(File f){
if(!f.isDirectory() && f.getName().length()>=4){
StringBuffer sb = new StringBuffer(f.getName());
//returns only true if the pathname represents a gif file
return sb.substring(sb.length()-3).toLowerCase().equals("gif");
} else {
return false;//no, directories are no gif-files
}
}
/**
* set text of source-textfield AND of the destination-textfield with .png
* according to the given File <i>toSet</i>
* @param toSet
*/
private void setFileToTextfields(File toSet){
mTextFieldSource.setText(toSet.getAbsolutePath());
if(!toSet.isDirectory()){
StringBuffer sb = new StringBuffer(
toSet.getAbsolutePath());
mTextFieldDestination.setText(sb.substring(0, sb.length()-4)+".apng");
}
else{
mTextFieldDestination.setText(mTextFieldSource.getText());
}
}
}