View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.util;
21  
22  
23  import java.awt.Component;
24  import java.awt.Graphics2D;
25  import java.awt.Toolkit;
26  import java.awt.datatransfer.Clipboard;
27  import java.awt.image.BufferedImage;
28  import java.io.File;
29  
30  import javax.imageio.ImageIO;
31  import javax.swing.Icon;
32  import javax.swing.ImageIcon;
33  import javax.swing.JFileChooser;
34  import javax.swing.JLabel;
35  import javax.swing.TransferHandler;
36  
37  import com.cosylab.gui.components.customizer.ImageFilter;
38  
39  /**
40   * @author tomo
41   * 
42   * TODO To change the template for this generated type comment go to Window -
43   * Preferences - Java - Code Style - Code Templates
44   */
45  public class ScreenCapturer {
46  
47  	private Component component = null;
48  
49  	/**
50  	 * String that holds path(full path: example "C:\something\gauger.png") ,
51  	 * where to create image
52  	 */
53  	private String path;
54  
55  	/**
56  	 * File type : .png , .jpeg ...
57  	 */
58  	private String fileExt;
59  
60  	/**
61  	 * ScreenCapturer supportable formats
62  	 */
63  	private String[] formats = { "png", "jpeg", "jpg" };
64  
65  	/**
66  	 * A varuble that holds JFileChooser-s action(like what button was
67  	 * pressed...)
68  	 */
69  	private int retval;
70  
71  	/**
72  	 * Creates new object
73  	 * 
74  	 * @param com
75  	 *            Component
76  	 */
77  	public ScreenCapturer(Component com) {
78  		if (com != null) {
79  			component = com;
80  		} else {
81  			//TODO throw NullPointer...
82  		}
83  	}
84  
85  	/**
86  	 * Creates new object
87  	 * 
88  	 * @param com
89  	 *            Component to paint
90  	 * @param p
91  	 *            String path , where to save file
92  	 */
93  	public ScreenCapturer(Component com, String p) {
94  
95  		component = com;
96  		path = p;
97  	}
98  
99  	/**
100 	 * Creates new object
101 	 *  
102 	 */
103 	public ScreenCapturer() {
104 	}
105 
106 	/**
107 	 * Shows JFileChooser, sets filters for supportable file types , and saves
108 	 * screen image
109 	 *  
110 	 */
111 	public void showScreenDialog() {
112 
113 		JFileChooser chooser = new JFileChooser();
114 		ImageFilter pngFilter = new ImageFilter() {
115 			public boolean accept(File f) {
116 				if (f.isDirectory())
117 					return true;
118 				String extension = getExtension(f);
119 				if (extension.equals("png"))
120 					return true;
121 				return false;
122 			}
123 
124 			public String getDescription() {
125 				return "png";
126 			}
127 		};
128 
129 		ImageFilter jpegFilter = new ImageFilter() {
130 			public boolean accept(File f) {
131 				if (f.isDirectory())
132 					return true;
133 				String extension = getExtension(f);
134 				if (extension.equals("jpeg"))
135 					return true;
136 				return false;
137 			}
138 
139 			public String getDescription() {
140 				return "jpeg";
141 			}
142 		};
143 
144 		ImageFilter jpgFilter = new ImageFilter() {
145 			public boolean accept(File f) {
146 				if (f.isDirectory())
147 					return true;
148 				String extension = getExtension(f);
149 				if (extension.equals("jpg"))
150 					return true;
151 				return false;
152 			}
153 
154 			public String getDescription() {
155 				return "jpg";
156 			}
157 		};
158 
159 		chooser.setFileFilter(jpgFilter);
160 		chooser.addChoosableFileFilter(jpgFilter);
161 		chooser.setFileFilter(jpegFilter);
162 		chooser.addChoosableFileFilter(jpegFilter);
163 		chooser.setFileFilter(pngFilter);
164 		chooser.addChoosableFileFilter(pngFilter);
165 
166 		retval = chooser.showSaveDialog(component);
167 		if (retval == JFileChooser.APPROVE_OPTION) {
168 			path = chooser.getSelectedFile().getAbsolutePath();
169 			if (chooser.getFileFilter() == jpegFilter) {
170 				path += ".jpeg";
171 			} else if (chooser.getFileFilter() == jpgFilter) {
172 				path += ".jpg";
173 			} else {
174 				path += ".png";
175 			}
176 
177 			saveImage();
178 
179 		} else {
180 			return;
181 		}
182 
183 	}
184 
185 	/**
186 	 * returns file type
187 	 * 
188 	 * @return String
189 	 */
190 	public String getFormat() {
191 		String ext[] = path.split("\\.");
192 
193 		if (ext[ext.length - 1] == formats[2]
194 				|| ext[ext.length - 1] == formats[1]) {
195 
196 			return ext[ext.length - 1];
197 		} else
198 			return formats[0];
199 
200 	}
201 
202 	/**
203 	 * returns file path
204 	 * 
205 	 * @return String
206 	 */
207 	public String getFile() {
208 		return path;
209 	}
210 
211 	/**
212 	 * set the file path
213 	 * 
214 	 * @param path
215 	 *            String
216 	 */
217 	public void setFile(String path) {
218 		this.path = path;
219 	}
220 
221 	/**
222 	 * Saves image to file ...
223 	 *  
224 	 */
225 	public void saveImage() {
226 		if (component != null) {
227 
228 			int w = component.getWidth();
229 			int h = component.getHeight();
230 			BufferedImage bi = new BufferedImage(w, h,
231 					BufferedImage.TYPE_INT_RGB);
232 			Graphics2D g2 = bi.createGraphics();
233 			component.paint(g2);
234 
235 			g2.dispose();
236 			try {
237 			  ImageIO.write(bi, getFormat(), new File(path));
238 				
239 			} catch (Exception ioe) {
240 				System.out.println("IOE: " + ioe.getMessage());
241 			}
242 		}
243 	}
244 	
245 	public void saveImageToSysClipBoard(Component componentToCopy){	     
246 	    if (componentToCopy != null) {
247 			int w = componentToCopy.getWidth();
248 			int h = componentToCopy.getHeight();
249 			BufferedImage bi = new BufferedImage(w, h,
250 					BufferedImage.TYPE_INT_RGB);
251 			Graphics2D g2 = bi.createGraphics();
252 			componentToCopy.paint(g2);
253 
254 			g2.dispose();
255 			try {
256 			    Toolkit kit = Toolkit.getDefaultToolkit();
257 			    final Clipboard clipboard = kit.getSystemClipboard();
258 
259 		        Icon icon = new ImageIcon(bi);
260 		        final JLabel label = new JLabel(icon);
261 		        label.setTransferHandler(new ImageClipboardHelper());
262 
263 			    TransferHandler transferHandler = label.getTransferHandler();
264                 transferHandler.exportToClipboard(label, clipboard,
265                     TransferHandler.COPY);
266 			    
267 				
268 				
269 			} catch (Exception ioe) {
270 				System.out.println("IOE: " + ioe.getMessage());
271 			}
272 		}
273 	}
274 	
275 	
276 
277 }