KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jperdian > rss2 > RssHelper


1 /**
2  * RSS framework and reader
3  * Copyright (C) 2004 Christian Robert
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package org.jperdian.rss2;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Image JavaDoc;
25 import java.awt.MediaTracker JavaDoc;
26 import java.awt.Toolkit JavaDoc;
27 import java.io.BufferedReader JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.text.DecimalFormat JavaDoc;
33 import java.text.NumberFormat JavaDoc;
34
35 import javax.swing.Action JavaDoc;
36 import javax.swing.Icon JavaDoc;
37 import javax.swing.ImageIcon JavaDoc;
38 import javax.swing.JButton JavaDoc;
39 import javax.swing.JLabel JavaDoc;
40 import javax.swing.JMenuItem JavaDoc;
41
42 /**
43  * Several small helpers
44  *
45  * @author Christian Robert
46  */

47
48 public class RssHelper {
49
50   private static final NumberFormat JavaDoc DECIMAL_FORMAT = new DecimalFormat JavaDoc("#,##0");
51   private static final NumberFormat JavaDoc FLOAT_FORMAT = new DecimalFormat JavaDoc("#,##0.0");
52   private static final Component JavaDoc DUMMY_COMP = new JLabel JavaDoc();
53   private static final String JavaDoc TEXT_START = "org/jperdian/rss2/res/texts/";
54   private static final String JavaDoc RESOURCE_START = "org/jperdian/rss2/res/images/";
55   
56   /**
57    * Creates an image icon for the given name. The image for this icon
58    * must be located in the resource path
59    * <code>"org/jperdian/rss2/res/images"</code>
60    */

61   public static String JavaDoc getResourceText(String JavaDoc imageName) {
62     ClassLoader JavaDoc loader = RssHelper.class.getClassLoader();
63     URL JavaDoc resourceURL = loader.getResource(TEXT_START + imageName);
64     if(resourceURL == null) {
65       throw new IllegalArgumentException JavaDoc("Cannot find text resource: " + TEXT_START + imageName);
66     } else {
67       try {
68         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(resourceURL.openStream()));
69         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
70         for(String JavaDoc line = reader.readLine(); line != null; line = reader.readLine()) {
71           buffer.append(line).append('\n');
72         }
73         return buffer.toString();
74       } catch(IOException JavaDoc e) {
75         return "";
76       }
77     }
78   }
79   
80   /**
81    * Creates a text for the given name. The text must be located in the
82    * resource path <code>"braintags/elacarte/imageassist/res/texts"</code>
83    */

84   public static Icon JavaDoc getResourceImageIcon(String JavaDoc imageName) {
85     ClassLoader JavaDoc loader = RssHelper.class.getClassLoader();
86     URL JavaDoc resourceURL = loader.getResource(RESOURCE_START + imageName);
87     if(resourceURL == null) {
88       throw new IllegalArgumentException JavaDoc("Cannot find image resource: " + RESOURCE_START + imageName);
89     } else {
90       return new ImageIcon JavaDoc(resourceURL);
91     }
92   }
93   
94   /**
95    * Creates an image icon for the given name. The image for this icon
96    * must be located in the resource path
97    * <code>"org/jperdian/rss2/res/images"</code>
98    */

99   public static Image JavaDoc getResourceImage(String JavaDoc imageName) {
100     ClassLoader JavaDoc loader = RssHelper.class.getClassLoader();
101     URL JavaDoc resourceURL = loader.getResource(RESOURCE_START + imageName);
102     if(resourceURL == null) {
103       throw new IllegalArgumentException JavaDoc("Cannot find image resource: " + RESOURCE_START + imageName);
104     } else {
105       Image JavaDoc img = Toolkit.getDefaultToolkit().createImage(resourceURL);
106       MediaTracker JavaDoc imgTracker = new MediaTracker JavaDoc(DUMMY_COMP);
107       imgTracker.addImage(img, 0);
108       try {
109         imgTracker.waitForAll();
110       } catch(Exception JavaDoc e) {
111         // Ignore here
112
}
113       return img;
114     }
115   }
116   
117   /**
118    * Gets the image from the given File
119    */

120   public static Image JavaDoc getImage(File JavaDoc file) throws IOException JavaDoc {
121     Image JavaDoc image = Toolkit.getDefaultToolkit().createImage(file.getAbsolutePath());
122     return RssHelper.waitFor(image);
123   }
124   
125   public static Image JavaDoc scaleImageProportional(Image JavaDoc sourceImage, Dimension JavaDoc maxDimension, boolean highQuality) {
126     
127     double targetWidth = maxDimension.width;
128     double targetHeight = maxDimension.height;
129     double currentWidth = sourceImage.getWidth(DUMMY_COMP);
130     double currentHeight = sourceImage.getHeight(DUMMY_COMP);
131     if(currentWidth > targetWidth || currentHeight > targetHeight) {
132       
133       double scaleX = 1 / currentWidth * targetWidth;
134       double scaleY = 1 / currentHeight * targetHeight;
135       double scale = Math.min(scaleX, scaleY);
136       int newWidth = (int)(currentWidth * scale);
137       int newHeight = (int)(currentHeight * scale);
138       Image JavaDoc newImage = sourceImage.getScaledInstance(newWidth, newHeight, highQuality ? Image.SCALE_SMOOTH : Image.SCALE_FAST);
139       return RssHelper.waitFor(newImage);
140       
141     } else {
142       return sourceImage;
143     }
144   }
145   
146   /**
147    * Wait until the image has been loaded completely
148    */

149   public static Image JavaDoc waitFor(Image JavaDoc image) {
150     try {
151       MediaTracker JavaDoc tracker = new MediaTracker JavaDoc(DUMMY_COMP);
152       tracker.addImage(image, 0);
153       tracker.waitForAll();
154     } catch(InterruptedException JavaDoc e) {
155       // Do nothing
156
}
157     return image;
158   }
159   
160   /**
161    * Computes the title for the given file size
162    */

163   public static String JavaDoc computeFileSizeString(long bytes) {
164     if(bytes < 1024) {
165       return DECIMAL_FORMAT.format(bytes) + " B";
166     } else if(bytes < 1024 * 1024) {
167       return DECIMAL_FORMAT.format(bytes / 1024) + " KB";
168     } else {
169       return FLOAT_FORMAT.format(bytes / (1024 * 1024)) + " MB";
170     }
171   }
172   
173   /**
174    * Formats the given time span
175    */

176   public static String JavaDoc computeTimeString(long time) {
177     if(time < 1000) {
178       return "0sec";
179     } else if(time < 1000 * 60) {
180       return (time/1000) + "sec";
181     } else {
182       long minutes = time / (1000 * 60);
183       long seconds = (time / 1000) % 60;
184       return minutes + "min" + seconds + "sec";
185     }
186   }
187   
188   /**
189    * Creates a <code>JMenuItem</code>
190    * @param action
191    * the action to be performed
192    * @param icon
193    * the icon to be used
194    */

195   public static JMenuItem JavaDoc createMenuItem(Action JavaDoc action, Icon JavaDoc icon) {
196     JMenuItem JavaDoc item = new JMenuItem JavaDoc(action);
197     if(icon != null) {
198       item.setIcon(icon);
199     }
200     return item;
201   }
202   
203   /**
204    * Creates a <code>JButton</code>
205    * @param action
206    * the action to be performed
207    * @param icon
208    * the icon to be used
209    */

210   public static JButton JavaDoc createButton(char mnemonic, Action JavaDoc action, Icon JavaDoc icon) {
211     JButton JavaDoc button = new JButton JavaDoc(action);
212     if(icon != null) {
213       button.setIcon(icon);
214     }
215     button.setMnemonic(mnemonic);
216     return button;
217   }
218   
219 }
Popular Tags