/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Copyright (C) 2011-2013 Marchand Eric <ricoh51@free.fr>
This file is part of Freegressi.
Freegressi 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.
Freegressi 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 Freegressi. If not, see <http://www.gnu.org/licenses/>.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package freegressi.graphics;
import freegressi.main.FreegressiFilter;
import freegressi.main.MainModel;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import net.sf.epsgraphics.ColorMode;
import net.sf.epsgraphics.EpsGraphics;
/**
* Classe qui contient la toolbar graphique et le graphique
* @author marchand
*/
public class JPanelGraphics extends JPanel implements ClipboardOwner{
private JPanelGraphic graphicPanel = null;
/** Action d'éditer les courbe */
private EditAction editAction = new EditAction(null,
new javax.swing.ImageIcon(getClass().getResource("/freegressi/img/editcurves_26x26.png")),
"Editer le graphique", 0);
/** Action de copier les courbe dans le presse papier (pas sous linux!!) */
private CopyAction copyAction = new CopyAction(null,
new javax.swing.ImageIcon(getClass().getResource("/freegressi/img/copy_26x26.png")),
"Copier le graphique dans le presse papier", 0);
/** Action de sauver les courbe sur le disque */
private ExportAction exportAction = new ExportAction(null,
new javax.swing.ImageIcon(getClass().getResource("/freegressi/img/saveas_26x26.png")),
"Exporter le graphique", 0);
private FreegressiFilter bitmapFilter, vectorialFilter;
public JPanelGraphics() {
creerUI();
}
private void creerUI() {
graphicPanel = new JPanelGraphic(this);
graphicPanel.addMouseMotionListener(graphicPanel);
graphicPanel.addMouseListener(graphicPanel);
GraphicModel.getInstance().addGraphicListener(graphicPanel);
this.setLayout(new BorderLayout());
JToolBar jToolbar = new JToolBar();
// mettre une taille mini pour pouvoir réduire le jpanelgraphique
jToolbar.setMinimumSize(new Dimension(1, 50));
jToolbar.setFloatable(false); // toolbar fixe
jToolbar.add(new JButton(editAction));
jToolbar.add(new JButton(copyAction));
jToolbar.add(new JButton(exportAction));
// Ajout de la toolbar, le BorderLayout est le layout par défaut
add(jToolbar, BorderLayout.PAGE_START);
add(graphicPanel, BorderLayout.CENTER);
bitmapFilter = new FreegressiFilter(
new String[]{"png","jpg","gif","bmp"},
"les fichiers images (*.png, *.jpg, *.gif, *.bmp)"
);
vectorialFilter = new FreegressiFilter(
new String[]{"eps"},
"les fichiers images vectorielles (*.eps)"
);
}
public void setToolBar(boolean yesno){
editAction.setEnabled(yesno);
copyAction.setEnabled(yesno);
exportAction.setEnabled(yesno);
}
@Override
public void lostOwnership(Clipboard clipboard, Transferable contents) {
System.out.println( "Lost Clipboard Ownership" );
}
private Image getImage(Component component){
if(component==null){return null;}
int width = component.getWidth();
int height = component.getHeight();
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
component.paintAll(g);
g.dispose();
return image;
}
/**
* Action "Editer les courbes"
*/
class EditAction extends AbstractAction {
public EditAction(String text, ImageIcon icon, String desc, Integer mnemonic) {
super(text, icon);
putValue(SHORT_DESCRIPTION, desc);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
JDialogEditCurves jdec = new JDialogEditCurves(null, GraphicModel.getInstance().getGraphicStyle());
jdec.setVisible(true);
if (jdec.isOk()) {
GraphicModel.getInstance().notifyGraphicStyleChanged(jdec.getNewGraphicStyle(), true);
}
jdec.dispose();
}
}
/**
* Action "Exporter le graphique"
*/
class ExportAction extends AbstractAction {
public ExportAction(String text, ImageIcon icon, String desc, Integer mnemonic) {
super(text, icon);
putValue(SHORT_DESCRIPTION, desc);
putValue(MNEMONIC_KEY, mnemonic);
}
private void saveAsBitmap(File file, String ext) throws IOException{
BufferedImage bi = (BufferedImage)getImage(graphicPanel);
ImageIO.write(bi, ext, file);
}
private void saveAsEPS(File file) throws IOException{
EpsGraphics eps = null;
try {
OutputStream output = new FileOutputStream(file);
eps = new EpsGraphics("Titre", output, 0, 0,
graphicPanel.getWidth(), graphicPanel.getHeight(),
ColorMode.COLOR_RGB);
graphicPanel.drawAll(eps);
//eps.close();
} catch (IOException er) {
System.err.println("Erreur durant l'écriture du fichier! " + er.getMessage());
} finally {
if (eps != null){
eps.close();
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser filechoose = new JFileChooser();
filechoose.addChoosableFileFilter(bitmapFilter);
filechoose.addChoosableFileFilter(vectorialFilter);
filechoose.setFileFilter(bitmapFilter);
filechoose.setCurrentDirectory(new File(MainModel.getInstance().getLastDir()));
String approve = "Exporter l'image";
int result = filechoose.showDialog(null, approve);
if (result == JFileChooser.APPROVE_OPTION) {
try {
File outputfile = filechoose.getSelectedFile();
String ext = MainModel.getFileExtensionName(outputfile);
ext = ext.toUpperCase();
switch (ext){
case "PNG": saveAsBitmap(outputfile, "png"); break;
case "JPG": saveAsBitmap(outputfile, "jpg"); break;
case "GIF": saveAsBitmap(outputfile, "gif"); break;
case "BMP": saveAsBitmap(outputfile, "bmp"); break;
case "EPS": saveAsEPS(outputfile); break;
default : saveAsBitmap(outputfile, "png"); break;
}
} catch (IOException exc) {
JOptionPane.showMessageDialog(null, "Le graphique n'a pas été exporté!",
"Freegressi, Erreur d'enregistrement", JOptionPane.ERROR_MESSAGE);
return;
}
JOptionPane.showMessageDialog(null, "Le graphique a été exporté",
"Freegressi, Enregistrement", JOptionPane.INFORMATION_MESSAGE);
}
}
}
/**
* Action "Copier le graphique dans le presse papier"
*/
class CopyAction extends AbstractAction {
public CopyAction(String text, ImageIcon icon, String desc, Integer mnemonic) {
super(text, icon);
putValue(SHORT_DESCRIPTION, desc);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
Image bf = getImage(graphicPanel);
TransferableImage it = new TransferableImage(bf);
try {
Clipboard clip=Toolkit.getDefaultToolkit().getSystemClipboard();
clip.setContents(it,null);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Le graphique n'a pas été copié dans le presse-papier",
"Freegressi, Erreur du presse-papier", JOptionPane.ERROR_MESSAGE);
return;
}
JOptionPane.showMessageDialog(null, "Le graphique a été copié dans le presse-papier",
"Freegressi, Presse-papier", JOptionPane.INFORMATION_MESSAGE);
}
}
private class TransferableImage implements Transferable {
Image img;
public TransferableImage( Image i ) {
this.img = i;
}
@Override
public Object getTransferData( DataFlavor flavor )
throws UnsupportedFlavorException, IOException {
if ( flavor.equals( DataFlavor.imageFlavor ) && img != null ) {
return img;
}
else {
throw new UnsupportedFlavorException( flavor );
}
}
@Override
public DataFlavor[] getTransferDataFlavors() {
DataFlavor[] flavors = new DataFlavor[ 1 ];
flavors[ 0 ] = DataFlavor.imageFlavor;
return flavors;
}
@Override
public boolean isDataFlavorSupported( DataFlavor flavor ) {
DataFlavor[] flavors = getTransferDataFlavors();
for ( int i = 0; i < flavors.length; i++ ) {
if ( flavor.equals( flavors[ i ] ) ) {
return true;
}
}
return false;
}
}
}