/* * Projecte Fressa a JAVA * Tools.java * Created on 11 / novembre / 2008, 15:40 * * @author Jordi Lagares Roset "jlagares@xtec.cat - www.lagares.org" * amb el suport del Departament d'Educacio de la Generalitat de Catalunya * * This program 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 2 of the License, or * (at your option) any later version. * * This program 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 (see the LICENSE file). */ import java.awt.*; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.IOException; import java.awt.image.BufferedImage; import java.io.File; //import javax.imageio.ImageIO; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.FileInputStream; import java.io.FileOutputStream; public class Tools { public int ErrorCode; public Tools() { } /**************************************************************/ /*BEGIN TOOLS**************************************************/ /**************************************************************/ public boolean not(boolean b){ if (b) { return false; } else { return true; } } public boolean Not(boolean b){ if (b) { return false; } else { return true; } } /*STRINGS******************************************************/ public String TreureFinalsLinea(String s) { int i; i=0; while (iLength(s)) { return s.substring(i-1,Length(s)); } else { return s.substring(i-1,i+c-1); } } else { return ""; } } public String LeftS(String s,int nombre) { if (nombre>s.length()) { return s; } else { return s.substring(0,nombre); } } public String RightS(String s,int nombre) { if (nombre>s.length()) { return s; } else { return s.substring(s.length()-nombre,s.length()); } } public String Insert(String s1,String s,int i) { return LeftS(s,i-1)+s1+RightS(s,s.length()-i+1); } public String Delete(String s,int i,int c) { return LeftS(s,i-1)+RightS(s,s.length()-i-c+1); } public String CanviText(String TextACanviar,String FindText,String ReplaceText) { int P; P=Pos(FindText,TextACanviar); while (P>0) { TextACanviar=Delete(TextACanviar,P,Length(FindText)); TextACanviar=Insert(ReplaceText,TextACanviar,P); P=Pos(FindText,TextACanviar); } return TextACanviar; } public String Arredonir(double x) { String s; s=String.valueOf(Math.round(x*10000)/10000.); s=CanviText(s,",","."); return s; } public String RealToString(double v) { String s; //return String.valueOf(v); //return Arredonir(v); s=Arredonir(v); if (RightS(s,2).equals(".0")) { s=Delete(s,Length(s)-1,2); } return s; } public String IntToStr(int i) { return String.valueOf(i); } public String LongToStr(long i) { return String.valueOf(i); } /*NUMERIC******************************************************/ public static double Abs(double s) { return Math.abs(s); } public static int Abs(int s) { return Math.abs(s); } public int Round(double v) { return (int)Math.round(v); } public long RoundLong(double v) { return (long)Math.round(v); } public int StrToInt(String s){ ErrorCode=0; try { s=CanviText(s,",","."); return Round(Double.valueOf(s).doubleValue()); } catch (Exception e) { ErrorCode=1; return 0; } } public long StrToLong(String s){ ErrorCode=0; try { s=CanviText(s,",","."); return RoundLong(Double.valueOf(s).doubleValue()); } catch (Exception e) { ErrorCode=1; return 0; } } public double StrToReal(String s){ ErrorCode=0; try { s=CanviText(s,",","."); return Double.valueOf(s).doubleValue(); } catch (Exception e) { ErrorCode=1; return 0; } } public double StrToFloat(String s){ ErrorCode=0; try { s=CanviText(s,",","."); return Double.valueOf(s).doubleValue(); } catch (Exception e) { ErrorCode=1; return 0; } } /*ARXIUS*******************************************************/ public static String getExtension(File f) { String ext = null; String s = f.getName(); int i = s.lastIndexOf('.'); if (i > 0 && i < s.length() - 1) { ext = s.substring(i+1).toLowerCase(); } return ext; } public static String getExtension(String f) { String ext = null; String s = f; int i = s.lastIndexOf('.'); if (i > 0 && i < s.length() - 1) { ext = s.substring(i+1).toLowerCase(); } return ext; } //http://www.java2s.com/Code/Java/File-Input-Output/CopyfilesusingJavaIOAPI.htm public static void FileCopy(String fromFileName, String toFileName) { File fromFile = new File(fromFileName); File toFile = new File(toFileName); if (toFile.exists()) toFile.delete(); try { if (!fromFile.exists()) throw new IOException("FileCopy: " + "no such source file: "+ fromFileName); if (!fromFile.isFile()) throw new IOException("FileCopy: " + "can't copy directory: "+ fromFileName); if (!fromFile.canRead()) throw new IOException("FileCopy: " + "source file is unreadable: "+ fromFileName); if (toFile.isDirectory()) toFile = new File(toFile, fromFile.getName()); if (toFile.exists()) { if (!toFile.canWrite()) throw new IOException("FileCopy: "+ "destination file is unwriteable: " + toFileName); System.out.print("Overwrite existing file " + toFile.getName()+ "? (Y/N): "); System.out.flush(); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String response = in.readLine(); if (!response.equals("Y") && !response.equals("y")) throw new IOException("FileCopy: "+ "existing file was not overwritten."); } else { String parent = toFile.getParent(); if (parent == null) parent = System.getProperty("user.dir"); File dir = new File(parent); if (!dir.exists()) throw new IOException("FileCopy: "+ "destination directory doesn't exist: " + parent); if (dir.isFile()) throw new IOException("FileCopy: "+ "destination is not a directory: " + parent); if (!dir.canWrite()) throw new IOException("FileCopy: "+ "destination directory is unwriteable: " + parent); } FileInputStream from = null; FileOutputStream to = null; try { from = new FileInputStream(fromFile); to = new FileOutputStream(toFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = from.read(buffer)) != -1) to.write(buffer, 0, bytesRead); // write } finally { if (from != null) try { from.close(); } catch (IOException e) { ; } if (to != null) try { to.close(); } catch (IOException e) { ; } } } catch ( java.io.IOException e ) { } } /*CLIPBOARD****************************************************/ public static void CopyStringToClipboard(String writeMe) { //Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); //Transferable transferableText = new StringSelection(writeMe); //systemClipboard.setContents(transferableText,null); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(writeMe),null); } public static String PasteStringFromClipboard() { //Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); //Transferable clipboardContents = systemClipboard.getContents(null); Transferable clipboardContents = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); if (clipboardContents==null) { return("NO PLAPHOONS DATA"); } else { try { if (clipboardContents.isDataFlavorSupported(DataFlavor.stringFlavor)) { String returnText = (String) clipboardContents.getTransferData(DataFlavor.stringFlavor); return returnText; } } catch (UnsupportedFlavorException ufe) { ufe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } } return("NO PLAPHOONS DATA"); } public static void CopiarAlaCarpeta(Rectangle screenArea,Dimension screenshotFinalDimension,String pictureName,String compressionType) { BufferedImage buf = null; BufferedImage bufFinal = null; try { buf = new Robot().createScreenCapture(screenArea); } catch (AWTException e) { e.printStackTrace(); } ImageSelection imageSelection = new ImageSelection(buf); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imageSelection, null); /* bufFinal = new BufferedImage(screenshotFinalDimension.width,screenshotFinalDimension.height, BufferedImage.TYPE_INT_RGB); // Redimensionnement de la capture originale Graphics2D g = (Graphics2D) bufFinal.getGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(buf, 0, 0, screenshotFinalDimension.width,screenshotFinalDimension.height, null); g.dispose(); //ImageSelection imageSelection = new ImageSelection(bufFinal); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imageSelection, null); try { ImageIO.write(bufFinal, compressionType, new File(pictureName)); } catch (IOException e) { e.printStackTrace(); } */ } // Inner class is used to hold an image while on the clipboard. public static class ImageSelection implements Transferable { // the Image object which will be housed by the ImageSelection private Image image; public ImageSelection(Image image) { this.image = image; } // Returns the supported flavors of our implementation public DataFlavor[] getTransferDataFlavors() { return new DataFlavor[] {DataFlavor.imageFlavor}; } // Returns true if flavor is supported public boolean isDataFlavorSupported(DataFlavor flavor) { return DataFlavor.imageFlavor.equals(flavor); } // Returns Image object housed by Transferable object public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException,IOException { if (!DataFlavor.imageFlavor.equals(flavor)) { throw new UnsupportedFlavorException(flavor); } // else return the payload return image; } } /**************************************************************/ /*END TOOLS****************************************************/ /**************************************************************/ }