001: /**
002: * RSS framework and reader
003: * Copyright (C) 2004 Christian Robert
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */package org.jperdian.rss2;
019:
020: import java.awt.Component;
021: import java.awt.Dimension;
022: import java.awt.Image;
023: import java.awt.MediaTracker;
024: import java.awt.Toolkit;
025: import java.io.BufferedReader;
026: import java.io.File;
027: import java.io.IOException;
028: import java.io.InputStreamReader;
029: import java.net.URL;
030: import java.text.DecimalFormat;
031: import java.text.NumberFormat;
032:
033: import javax.swing.Action;
034: import javax.swing.Icon;
035: import javax.swing.ImageIcon;
036: import javax.swing.JButton;
037: import javax.swing.JLabel;
038: import javax.swing.JMenuItem;
039:
040: /**
041: * Several small helpers
042: *
043: * @author Christian Robert
044: */
045:
046: public class RssHelper {
047:
048: private static final NumberFormat DECIMAL_FORMAT = new DecimalFormat(
049: "#,##0");
050: private static final NumberFormat FLOAT_FORMAT = new DecimalFormat(
051: "#,##0.0");
052: private static final Component DUMMY_COMP = new JLabel();
053: private static final String TEXT_START = "org/jperdian/rss2/res/texts/";
054: private static final String RESOURCE_START = "org/jperdian/rss2/res/images/";
055:
056: /**
057: * Creates an image icon for the given name. The image for this icon
058: * must be located in the resource path
059: * <code>"org/jperdian/rss2/res/images"</code>
060: */
061: public static String getResourceText(String imageName) {
062: ClassLoader loader = RssHelper.class.getClassLoader();
063: URL resourceURL = loader.getResource(TEXT_START + imageName);
064: if (resourceURL == null) {
065: throw new IllegalArgumentException(
066: "Cannot find text resource: " + TEXT_START
067: + imageName);
068: } else {
069: try {
070: BufferedReader reader = new BufferedReader(
071: new InputStreamReader(resourceURL.openStream()));
072: StringBuffer buffer = new StringBuffer();
073: for (String line = reader.readLine(); line != null; line = reader
074: .readLine()) {
075: buffer.append(line).append('\n');
076: }
077: return buffer.toString();
078: } catch (IOException e) {
079: return "";
080: }
081: }
082: }
083:
084: /**
085: * Creates a text for the given name. The text must be located in the
086: * resource path <code>"braintags/elacarte/imageassist/res/texts"</code>
087: */
088: public static Icon getResourceImageIcon(String imageName) {
089: ClassLoader loader = RssHelper.class.getClassLoader();
090: URL resourceURL = loader
091: .getResource(RESOURCE_START + imageName);
092: if (resourceURL == null) {
093: throw new IllegalArgumentException(
094: "Cannot find image resource: " + RESOURCE_START
095: + imageName);
096: } else {
097: return new ImageIcon(resourceURL);
098: }
099: }
100:
101: /**
102: * Creates an image icon for the given name. The image for this icon
103: * must be located in the resource path
104: * <code>"org/jperdian/rss2/res/images"</code>
105: */
106: public static Image getResourceImage(String imageName) {
107: ClassLoader loader = RssHelper.class.getClassLoader();
108: URL resourceURL = loader
109: .getResource(RESOURCE_START + imageName);
110: if (resourceURL == null) {
111: throw new IllegalArgumentException(
112: "Cannot find image resource: " + RESOURCE_START
113: + imageName);
114: } else {
115: Image img = Toolkit.getDefaultToolkit().createImage(
116: resourceURL);
117: MediaTracker imgTracker = new MediaTracker(DUMMY_COMP);
118: imgTracker.addImage(img, 0);
119: try {
120: imgTracker.waitForAll();
121: } catch (Exception e) {
122: // Ignore here
123: }
124: return img;
125: }
126: }
127:
128: /**
129: * Gets the image from the given File
130: */
131: public static Image getImage(File file) throws IOException {
132: Image image = Toolkit.getDefaultToolkit().createImage(
133: file.getAbsolutePath());
134: return RssHelper.waitFor(image);
135: }
136:
137: public static Image scaleImageProportional(Image sourceImage,
138: Dimension maxDimension, boolean highQuality) {
139:
140: double targetWidth = maxDimension.width;
141: double targetHeight = maxDimension.height;
142: double currentWidth = sourceImage.getWidth(DUMMY_COMP);
143: double currentHeight = sourceImage.getHeight(DUMMY_COMP);
144: if (currentWidth > targetWidth || currentHeight > targetHeight) {
145:
146: double scaleX = 1 / currentWidth * targetWidth;
147: double scaleY = 1 / currentHeight * targetHeight;
148: double scale = Math.min(scaleX, scaleY);
149: int newWidth = (int) (currentWidth * scale);
150: int newHeight = (int) (currentHeight * scale);
151: Image newImage = sourceImage.getScaledInstance(newWidth,
152: newHeight, highQuality ? Image.SCALE_SMOOTH
153: : Image.SCALE_FAST);
154: return RssHelper.waitFor(newImage);
155:
156: } else {
157: return sourceImage;
158: }
159: }
160:
161: /**
162: * Wait until the image has been loaded completely
163: */
164: public static Image waitFor(Image image) {
165: try {
166: MediaTracker tracker = new MediaTracker(DUMMY_COMP);
167: tracker.addImage(image, 0);
168: tracker.waitForAll();
169: } catch (InterruptedException e) {
170: // Do nothing
171: }
172: return image;
173: }
174:
175: /**
176: * Computes the title for the given file size
177: */
178: public static String computeFileSizeString(long bytes) {
179: if (bytes < 1024) {
180: return DECIMAL_FORMAT.format(bytes) + " B";
181: } else if (bytes < 1024 * 1024) {
182: return DECIMAL_FORMAT.format(bytes / 1024) + " KB";
183: } else {
184: return FLOAT_FORMAT.format(bytes / (1024 * 1024)) + " MB";
185: }
186: }
187:
188: /**
189: * Formats the given time span
190: */
191: public static String computeTimeString(long time) {
192: if (time < 1000) {
193: return "0sec";
194: } else if (time < 1000 * 60) {
195: return (time / 1000) + "sec";
196: } else {
197: long minutes = time / (1000 * 60);
198: long seconds = (time / 1000) % 60;
199: return minutes + "min" + seconds + "sec";
200: }
201: }
202:
203: /**
204: * Creates a <code>JMenuItem</code>
205: * @param action
206: * the action to be performed
207: * @param icon
208: * the icon to be used
209: */
210: public static JMenuItem createMenuItem(Action action, Icon icon) {
211: JMenuItem item = new JMenuItem(action);
212: if (icon != null) {
213: item.setIcon(icon);
214: }
215: return item;
216: }
217:
218: /**
219: * Creates a <code>JButton</code>
220: * @param action
221: * the action to be performed
222: * @param icon
223: * the icon to be used
224: */
225: public static JButton createButton(char mnemonic, Action action,
226: Icon icon) {
227: JButton button = new JButton(action);
228: if (icon != null) {
229: button.setIcon(icon);
230: }
231: button.setMnemonic(mnemonic);
232: return button;
233: }
234:
235: }
|