1 | // $Id: SilverpeasUploadPolicy.java 143 2010-01-27 11:01:06 +0100 (mer. 27 janv. 2010) ehsavoie $ |
---|
2 | // |
---|
3 | // jupload - A file upload applet. |
---|
4 | // Copyright 2010 The JUpload Team |
---|
5 | // |
---|
6 | // Created: 2010-05-14 |
---|
7 | // Creator: ehsavoie |
---|
8 | // Last modified: $Date: 2010-01-27 11:01:06 +0100 (mer. 27 janv. 2010) $ |
---|
9 | // |
---|
10 | // This program is free software; you can redistribute it and/or modify it under |
---|
11 | // the terms of the GNU General Public License as published by the Free Software |
---|
12 | // Foundation; either version 2 of the License, or (at your option) any later |
---|
13 | // version. This program is distributed in the hope that it will be useful, but |
---|
14 | // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
15 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
16 | // details. You should have received a copy of the GNU General Public License |
---|
17 | // along with this program; if not, write to the Free Software Foundation, Inc., |
---|
18 | // 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
19 | package wjhk.jupload2.policies; |
---|
20 | |
---|
21 | import java.awt.Color; |
---|
22 | import java.awt.Component; |
---|
23 | import java.awt.Dimension; |
---|
24 | import java.awt.dnd.DropTargetDropEvent; |
---|
25 | import java.io.BufferedReader; |
---|
26 | import java.io.CharArrayWriter; |
---|
27 | import java.io.IOException; |
---|
28 | import java.io.InputStreamReader; |
---|
29 | import java.net.HttpURLConnection; |
---|
30 | import java.net.URL; |
---|
31 | import javax.swing.BoxLayout; |
---|
32 | import javax.swing.JLabel; |
---|
33 | import wjhk.jupload2.context.JUploadContext; |
---|
34 | import wjhk.jupload2.exception.JUploadException; |
---|
35 | import wjhk.jupload2.exception.JUploadExceptionTooBigFile; |
---|
36 | import wjhk.jupload2.gui.JUploadPanel; |
---|
37 | |
---|
38 | /** |
---|
39 | * Personnalised Policy with no button : use only DragAndDrop or Copy/Paste with automatic upload.<br/> |
---|
40 | * This is given as an example of a personnalized policy to show how JUpload is easy to adapt to your |
---|
41 | * specific needs. |
---|
42 | * <ul> |
---|
43 | * <li>support for updating the background color.</li> |
---|
44 | * <li>load an HTML page to be displayed as a message into the Applet: allows a 'richer' label.</li> |
---|
45 | * <li>pre-validation of files size (when they are added, instead of before upload).</li> |
---|
46 | * </ul> |
---|
47 | * @author ehsavoie |
---|
48 | */ |
---|
49 | public class SilverpeasUploadPolicy extends DefaultUploadPolicy { |
---|
50 | |
---|
51 | /** */ |
---|
52 | public static final String MESSAGE_URL = "message"; |
---|
53 | /** */ |
---|
54 | public static final String COLOR_RED = "bgcolor_r"; |
---|
55 | /** */ |
---|
56 | public static final String COLOR_BLUE = "bgcolor_b"; |
---|
57 | /** */ |
---|
58 | public static final String COLOR_GREEN = "bgcolor_g"; |
---|
59 | /** */ |
---|
60 | public static final String COLOR_ALPHA = "bgcolor_a"; |
---|
61 | |
---|
62 | /** |
---|
63 | * The JUpload constructor for this upload policy. Like all upload policies, |
---|
64 | * this constructor is called by the {@link UploadPolicyFactory} |
---|
65 | * |
---|
66 | * @param juploadContext |
---|
67 | * @throws JUploadException |
---|
68 | */ |
---|
69 | public SilverpeasUploadPolicy(JUploadContext juploadContext) |
---|
70 | throws JUploadException { |
---|
71 | super(juploadContext); |
---|
72 | setNbFilesPerRequest(20); |
---|
73 | displayInfo("Loading SilverpeasUploadPolicy ..."); |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * This methods allow the upload policy to override the default disposition |
---|
78 | * of the components on the applet. |
---|
79 | * |
---|
80 | * @see UploadPolicy#addComponentsToJUploadPanel(JUploadPanel) |
---|
81 | */ |
---|
82 | @Override |
---|
83 | public void addComponentsToJUploadPanel(JUploadPanel jUploadPanel) { |
---|
84 | // Set the global layout of the panel. |
---|
85 | displayInfo("Displaying SilverpeasUploadPolicy ..."); |
---|
86 | jUploadPanel.getJComponent().setLayout(new BoxLayout(jUploadPanel.getJComponent(), |
---|
87 | BoxLayout.Y_AXIS)); |
---|
88 | displayInfo("Accessing the message " + getMessage()); |
---|
89 | Color color = new Color(getContext().getParameter(COLOR_RED, Color.LIGHT_GRAY.getRed()), |
---|
90 | getContext().getParameter(COLOR_GREEN, Color.LIGHT_GRAY.getGreen()), |
---|
91 | getContext().getParameter(COLOR_BLUE, Color.LIGHT_GRAY.getBlue()), |
---|
92 | getContext().getParameter(COLOR_ALPHA, Color.LIGHT_GRAY.getAlpha())); |
---|
93 | jUploadPanel.getJComponent().setBackground(color); |
---|
94 | jUploadPanel.getStatusLabel().setBackground(color); |
---|
95 | jUploadPanel.getUploadProgressBar().setBackground(color); |
---|
96 | |
---|
97 | jUploadPanel.getStatusLabel().setText(getMessage()); |
---|
98 | jUploadPanel.getStatusLabel().setHorizontalAlignment(JLabel.CENTER); |
---|
99 | jUploadPanel.getStatusLabel().setVerticalAlignment(JLabel.CENTER); |
---|
100 | Dimension appletSize = getContext().getApplet().getSize(); |
---|
101 | Dimension preferredAppletSize = getContext().getApplet().getPreferredSize(); |
---|
102 | Dimension maxAppletSize = getContext().getApplet().getMaximumSize(); |
---|
103 | jUploadPanel.getStatusLabel().setAlignmentX(Component.CENTER_ALIGNMENT); |
---|
104 | jUploadPanel.getStatusLabel().setAlignmentY(Component.CENTER_ALIGNMENT); |
---|
105 | jUploadPanel.getStatusLabel().setPreferredSize(new Dimension(preferredAppletSize.width, preferredAppletSize.height |
---|
106 | - 25)); |
---|
107 | jUploadPanel.getStatusLabel().setSize(new Dimension(appletSize.width, appletSize.height - 25)); |
---|
108 | jUploadPanel.getStatusLabel().setMaximumSize(new Dimension(maxAppletSize.width, maxAppletSize.height |
---|
109 | - 25)); |
---|
110 | jUploadPanel.getStatusLabel().setMinimumSize(new Dimension(appletSize.width, appletSize.height |
---|
111 | - 25)); |
---|
112 | jUploadPanel.getStatusLabel().setBackground(new Color(DEFAULT_ALBUM_ID)); |
---|
113 | jUploadPanel.getJComponent().add(jUploadPanel.getStatusLabel()); |
---|
114 | jUploadPanel.getJComponent().setAlignmentX(Component.CENTER_ALIGNMENT); |
---|
115 | jUploadPanel.getUploadProgressBar().setAlignmentX(Component.CENTER_ALIGNMENT); |
---|
116 | jUploadPanel.getUploadProgressBar().setAlignmentY(Component.CENTER_ALIGNMENT); |
---|
117 | jUploadPanel.getUploadProgressBar().setPreferredSize( |
---|
118 | new Dimension(preferredAppletSize.width, 20)); |
---|
119 | jUploadPanel.getUploadProgressBar().setSize(new Dimension(appletSize.width, 20)); |
---|
120 | jUploadPanel.getUploadProgressBar().setMaximumSize(new Dimension(maxAppletSize.width, 20)); |
---|
121 | jUploadPanel.getUploadProgressBar().setMinimumSize(new Dimension(appletSize.width, 20)); |
---|
122 | jUploadPanel.getUploadProgressBar().setStringPainted(false); |
---|
123 | jUploadPanel.getUploadProgressBar().setBorderPainted(false); |
---|
124 | // Then, add on the screen of the only component that is visible. |
---|
125 | jUploadPanel.getJComponent().add(jUploadPanel.getUploadProgressBar()); |
---|
126 | // Now, we add the log window. |
---|
127 | jUploadPanel.showOrHideLogWindow(); |
---|
128 | jUploadPanel.getJComponent().add(jUploadPanel.getJLogWindowPane()); |
---|
129 | displayInfo("Displaying SilverpeasUploadPolicy ..."); |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * Loads the message from the specified URL. |
---|
134 | * @return the remote message. |
---|
135 | */ |
---|
136 | protected String getMessage() { |
---|
137 | displayInfo("The message to be displayed is " + juploadContext.getParameter(MESSAGE_URL, "")); |
---|
138 | String urlContent = juploadContext.getParameter(MESSAGE_URL, ""); |
---|
139 | String content = urlContent; |
---|
140 | HttpURLConnection connection = null; |
---|
141 | BufferedReader in = null; |
---|
142 | try { |
---|
143 | URL url = new URL(urlContent); |
---|
144 | connection = (HttpURLConnection) url.openConnection(); |
---|
145 | connection.connect(); |
---|
146 | in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); |
---|
147 | displayInfo("Getting data ..."); |
---|
148 | CharArrayWriter text = new CharArrayWriter(); |
---|
149 | int size = 0; |
---|
150 | char[] buffer = new char[8]; |
---|
151 | while ((size = in.read(buffer)) >= 0) { |
---|
152 | text.write(buffer, 0, size); |
---|
153 | } |
---|
154 | content = text.toString(); |
---|
155 | } catch (IOException ioex) { |
---|
156 | displayErr(ioex); |
---|
157 | } finally { |
---|
158 | try { |
---|
159 | if (in != null) { |
---|
160 | in.close(); |
---|
161 | } |
---|
162 | } catch (IOException ex) { |
---|
163 | displayErr(ex); |
---|
164 | } |
---|
165 | connection.disconnect(); |
---|
166 | } |
---|
167 | return content; |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * Default reaction after a successful drop operation: no action. |
---|
172 | * |
---|
173 | * @see UploadPolicy#afterFileDropped(DropTargetDropEvent) |
---|
174 | */ |
---|
175 | @Override |
---|
176 | public void afterFileDropped(DropTargetDropEvent dropEvent) { |
---|
177 | getContext().getUploadPanel().doStartUpload(); |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | * @param description |
---|
182 | * @see UploadPolicy#sendDebugInformation(String, Exception) */ |
---|
183 | @Override |
---|
184 | public void sendDebugInformation(String description, Exception exception) { |
---|
185 | if (exception instanceof JUploadExceptionTooBigFile) { |
---|
186 | getContext().getUploadPanel().getFilePanel().removeAll(); |
---|
187 | } |
---|
188 | super.sendDebugInformation(description, exception); |
---|
189 | } |
---|
190 | } |
---|