1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
40
41
42
43
44
45
46
47
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
64
65
66
67
68
69 public SilverpeasUploadPolicy(JUploadContext juploadContext)
70 throws JUploadException {
71 super(juploadContext);
72 setNbFilesPerRequest(20);
73 displayInfo("Loading SilverpeasUploadPolicy ...");
74 }
75
76
77
78
79
80
81
82 @Override
83 public void addComponentsToJUploadPanel(JUploadPanel jUploadPanel) {
84
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
125 jUploadPanel.getJComponent().add(jUploadPanel.getUploadProgressBar());
126
127 jUploadPanel.showOrHideLogWindow();
128 jUploadPanel.getJComponent().add(jUploadPanel.getJLogWindowPane());
129 displayInfo("Displaying SilverpeasUploadPolicy ...");
130 }
131
132
133
134
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
172
173
174
175 @Override
176 public void afterFileDropped(DropTargetDropEvent dropEvent) {
177 getContext().getUploadPanel().doStartUpload();
178 }
179
180
181
182
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 }