Source Code Cross Referenced for RssHelper.java in  » Groupware » lucane » org » jperdian » rss2 » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Groupware » lucane » org.jperdian.rss2 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.