1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.telscenter.pas.steps.quickEditors.html;
24
25 import java.awt.BorderLayout;
26 import java.awt.Dimension;
27 import java.awt.FlowLayout;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.event.FocusEvent;
31 import java.awt.event.FocusListener;
32 import java.io.BufferedReader;
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.InputStreamReader;
38 import java.net.URL;
39 import java.net.URLConnection;
40 import java.util.jar.JarOutputStream;
41
42 import javax.swing.Action;
43 import javax.swing.JButton;
44 import javax.swing.JComponent;
45 import javax.swing.JFileChooser;
46 import javax.swing.JFrame;
47 import javax.swing.JLabel;
48 import javax.swing.JPanel;
49 import javax.swing.JTextArea;
50 import javax.swing.JTextField;
51 import javax.swing.undo.UndoManager;
52
53 import net.sf.sail.core.beans.Pod;
54 import net.sf.sail.core.util.BinaryUtils;
55 import net.sf.sail.core.util.PodUtils;
56
57 import org.telscenter.pas.common.ui.panel.TextEditorPanel;
58 import org.telscenter.pas.steps.BrowseWebUI;
59 import org.telscenter.pas.steps.DisplayPage;
60 import org.telscenter.pas.steps.BrowseWeb.PopupPreviewAction;
61
62 /***
63 * @author aperritano
64 *
65 */
66 public class EditPageQuickEditorUI extends HtmlQuickEditorUI {
67
68 public static final String VALUE_LOCAL_FILE_URL = "local";
69 private static final int TEXT_BOX_LEN = 25;
70
71 private JTextArea htmlArea;
72
73 private JButton browseButton;
74 private JTextField localUrlText;
75 private JButton previewButton;
76 private UndoManager undoManager;
77
78
79 public EditPageQuickEditorUI(UndoManager undoManager) {
80 this.undoManager = undoManager;
81 }
82
83
84
85
86
87 protected void createUI() {
88 this.browseWebBean.setIsAuthoringHTMLPage(true);
89
90 this.setOpaque(false);
91 this.setLayout(new BorderLayout(0,0));
92 JPanel titlePane = new JPanel(new FlowLayout(FlowLayout.LEFT));
93 titlePane.setOpaque(false);
94 JLabel label = new JLabel("HTML Source code:");
95 label.setOpaque(false);
96
97 titlePane.add(label);
98
99
100 this.add(titlePane,BorderLayout.NORTH);
101
102
103 htmlArea = new JTextArea();
104
105 if( this.undoManager != null)
106 htmlArea.getDocument().addUndoableEditListener(this.undoManager);
107
108
109 browseWebBean.getWebBrowserComponent();
110
111 if( browseWebBean.getBrowser() != null && browseWebBean.getBrowser().getContent() != null ) {
112
113 URL url = browseWebBean.getBrowser().getUrl();
114
115 try {
116 if( url != null) {
117
118 htmlArea.setText(readTextFromURL(url.toString()));
119 } else {
120 htmlArea.setText(browseWebBean.getContent());
121 }
122 } catch (IOException e) {
123
124 e.printStackTrace();
125 }
126
127 }
128
129
130 TextEditorPanel ppanel = new TextEditorPanel(htmlArea,new Dimension(400, 125));
131
132 htmlArea.setBackground(this.getBackground());
133 JButton jb = ppanel.getEditButton();
134
135 if( jb != null ) {
136
137 jb.addActionListener(new ActionListener() {
138
139 public void actionPerformed(ActionEvent e) {
140
141
142 JButton source = (JButton) e.getSource();
143 String clientProperty = (String) source.getClientProperty(TextEditorPanel.MODE);
144
145 if (clientProperty.equals(TextEditorPanel.SAVE_MODE)) {
146
147
148
149 final String text = htmlArea.getText();
150 if( text == null) {
151 return;
152 }
153 if( text != null ) {
154 htmlArea.setText(text);
155 browseWebBean.setContent(text);
156
157 }
158 }
159 }
160 });
161
162 }
163
164 this.add(createPreviewButton(), BorderLayout.EAST);
165 this.add(ppanel,BorderLayout.CENTER);
166
167
168
169 }
170
171 private JPanel createPreviewButton() {
172 JPanel previewButtonPanel = new JPanel(new BorderLayout(0,0));
173
174
175 previewButtonPanel.setOpaque(false);
176 BrowseWebUI ui = (BrowseWebUI) browseWebBean.getComponent();
177 PopupPreviewAction previewAction = (PopupPreviewAction) browseWebBean.getPreviewAction(false);
178
179 previewAction.putValue(Action.NAME, "Preview Step");
180
181
182
183 previewButton = new JButton(previewAction);
184 previewButton.setOpaque(false);
185
186
187 previewButtonPanel.add(previewButton,BorderLayout.NORTH);
188 return previewButtonPanel;
189 }
190
191 private JComponent createLocalLabelPanel(JPanel formPanel) {
192 JLabel local = new JLabel("File:");
193 return local;
194 }
195
196 private JComponent createLocalTextPanel(JPanel formPanel) {
197 localUrlText = new JTextField(TEXT_BOX_LEN);
198
199
200 if( browseWebBean.getUrl() != null ) {
201
202
203 String url = browseWebBean.getUrl().toString();
204
205 localUrlText.setText(url);
206
207
208 }
209
210 localUrlText.setEditable(false);
211 localUrlText.addFocusListener(new FocusListener() {
212
213 public void focusGained(FocusEvent e) {
214
215 }
216
217 public void focusLost(FocusEvent e) {
218
219
220 }});
221
222 localUrlText.addActionListener(new ActionListener() {
223
224 public void actionPerformed(ActionEvent e) {
225 previewButton.setEnabled(true);
226 }});
227 return localUrlText;
228 }
229
230 /***
231 * @param object
232 * @return
233 */
234 private JButton createLocalButtonPanel(JPanel formPanel) {
235 browseButton = new JButton("Select File");
236
237 browseButton.addActionListener(new ActionListener() {
238
239 public void actionPerformed(ActionEvent e) {
240 browseForBean();
241 }
242 });
243 browseButton.setToolTipText("Import a HTML from a local file.");
244
245 return browseButton;
246 }
247
248 /***
249 *
250 */
251 protected void browseForBean() {
252 final File selectedFile = showChooserAndReturnFile();
253
254 if( selectedFile == null )
255 return;
256
257 Pod browseWebStep = PodUtils.resolvePod(browseWebBean);
258 URL resURL = null;
259 try {
260
261
262 JarOutputStream podResources = BinaryUtils
263 .newPodArchiveFor(browseWebStep);
264
265 resURL = BinaryUtils.addToArchive(browseWebStep,
266 podResources, selectedFile.getName(),
267 new FileInputStream(selectedFile));
268 podResources.close();
269
270
271 localUrlText.setText(selectedFile.getAbsolutePath());
272 } catch (Exception exc) {
273 exc.printStackTrace();
274 }
275
276
277 browseWebBean.setUrl(resURL);
278 localUrlText.setText(resURL.toString());
279 }
280
281 /***
282 * @return
283 */
284 protected File showChooserAndReturnFile() {
285 JFileChooser chooser = new JFileChooser();
286 chooser.setFileFilter(new HtmlFilter());
287 int returnVal = chooser.showOpenDialog(browseButton);
288 if (returnVal != JFileChooser.APPROVE_OPTION) {
289 return null;
290 }
291
292 final File selectedFile = chooser.getSelectedFile();
293 return selectedFile;
294 }
295
296 class HtmlFilter extends javax.swing.filechooser.FileFilter {
297 public boolean accept(File file) {
298 String filename = file.getName();
299
300 if (file.isDirectory())
301 return true;
302 if (filename.endsWith(".html"))
303 return true;
304 if (filename.endsWith(".htm"))
305 return true;
306
307 return false;
308 }
309
310 public String getDescription() {
311 return "*.html,*.htm";
312 }
313 }
314
315 /***
316 * This subroutine attempts to copy text from the specified URL onto the screen.
317 * Any error must be handled by the caller of this subroutine.
318 * @param urlString contains the URL in text form
319 */
320 static String readTextFromURL( String urlString ) throws IOException {
321
322
323
324
325 URL url = new URL(urlString);
326 URLConnection connection = url.openConnection();
327 InputStream urlData = connection.getInputStream();
328 StringBuffer stringBuf = new StringBuffer();
329
330
331
332
333
334
335
336 String contentType = connection.getContentType();
337 if (contentType == null || contentType.startsWith("text") == false)
338 throw new IOException("URL does not seem to refer to a text file.");
339
340
341
342
343 BufferedReader in;
344 in = new BufferedReader( new InputStreamReader(urlData) );
345
346 while (true) {
347 String line = in.readLine();
348 if (line == null) {
349 break;
350 }
351 stringBuf.append(line);
352 }
353
354 return stringBuf.toString();
355 }
356
357 public static void main(String args[]) {
358 DisplayPage dp = new DisplayPage();
359 dp.setContent("<html><p>hello</p></html>");
360
361
362
363
364
365
366 JFrame frame = new JFrame("FrameDemo");
367
368
369 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
370
371
372
373
374
375 EditPageQuickEditorUI pageEditor = new EditPageQuickEditorUI(null);
376
377 pageEditor.setBean(dp);
378 frame.setSize(500, 500);
379
380
381 frame.getContentPane().add(pageEditor, BorderLayout.CENTER);
382
383
384 frame.pack();
385
386
387 frame.setVisible(true);
388 }
389
390 }