View Javadoc

1   /***
2    * 
3    */
4   package org.telscenter.pas.steps.quickEditors.html;
5   
6   import info.clearthought.layout.TableLayout;
7   import info.clearthought.layout.TableLayoutConstants;
8   
9   import java.awt.BorderLayout;
10  import java.awt.Dimension;
11  import java.awt.Font;
12  import java.awt.event.ActionEvent;
13  import java.awt.event.ActionListener;
14  import java.awt.event.FocusEvent;
15  import java.awt.event.FocusListener;
16  import java.io.File;
17  import java.io.FileInputStream;
18  import java.io.FileWriter;
19  import java.io.IOException;
20  import java.net.MalformedURLException;
21  import java.net.URL;
22  import java.util.jar.JarOutputStream;
23  
24  import javax.swing.BorderFactory;
25  import javax.swing.ButtonGroup;
26  import javax.swing.ImageIcon;
27  import javax.swing.JButton;
28  import javax.swing.JDialog;
29  import javax.swing.JFileChooser;
30  import javax.swing.JFrame;
31  import javax.swing.JLabel;
32  import javax.swing.JPanel;
33  import javax.swing.JRadioButton;
34  import javax.swing.JScrollPane;
35  import javax.swing.JTextArea;
36  import javax.swing.JTextField;
37  import javax.swing.ScrollPaneConstants;
38  import javax.swing.SwingConstants;
39  import javax.swing.event.DocumentEvent;
40  import javax.swing.event.DocumentListener;
41  
42  import net.sf.sail.core.beans.Pod;
43  import net.sf.sail.core.util.BinaryUtils;
44  import net.sf.sail.core.util.PodUtils;
45  
46  import org.apache.commons.lang.StringUtils;
47  import org.telscenter.pas.common.ui.CommonUI;
48  import org.telscenter.pas.common.ui.text.CurrentLineHighlighter;
49  import org.telscenter.pas.steps.AbstractUrlStep;
50  import org.telscenter.pas.steps.BrowseWeb;
51  import org.telscenter.pas.steps.Evidence;
52  import org.telscenter.pas.steps.propertyEditors.rim.RimMediator;
53  import org.telscenter.pas.ui.util.PasColors;
54  
55  /***
56   * @author aperritano
57   * 
58   */
59  public class CopyOfHtmlQuickEditorUI extends JPanel {
60  
61  	private URL resURL;
62  	
63  	private static final long serialVersionUID = 1L;
64  
65  	private static final int TEXT_BOX_LEN = 35;
66  
67  	private Object bean;
68  
69  	private JRadioButton webLinkRadio;
70  
71  	private JTextField webUrlText;
72  
73  	private JButton browseButton;
74  
75  	private JTextField localUrlText;
76  
77  	private JRadioButton localFileRadio;
78  
79  	private JTextArea htmlTextArea = new JTextArea();
80  
81  	private boolean htmlModified = false;
82  
83  	boolean localSelected;
84  
85  	boolean webSelected;
86  
87  	private JLabel htmlLabel;
88  
89  	protected String currentFileName;
90  
91  	private JScrollPane htmlAreaScrollPane;
92  
93  	private JButton setPageButton;
94  
95  	public CopyOfHtmlQuickEditorUI() {
96  	}
97  
98  	protected void createUI() {
99  		setLayout(new BorderLayout(4, 4));
100 		setBorder(CommonUI.create4PEmptyBorder());
101 
102 		setBackground(PasColors.basicInfoBackgroundColor);
103 		final JPanel formPanel = new JPanel();
104 
105 		final double pref = TableLayoutConstants.PREFERRED;
106 		final double fill = TableLayoutConstants.FILL;
107 		final double vSpace = 5;
108 		final double vSpace2 = 10;
109 		final double hSpace = 6;
110 		final double size[][] = {
111 				{ pref, hSpace, pref, hSpace, pref, // rt
112 				},
113 
114 				{ pref, vSpace, pref, vSpace, pref, vSpace, pref, vSpace, pref,
115 						vSpace, fill }
116 
117 		};
118 		formPanel.setLayout(new TableLayout(size));
119 		formPanel.setOpaque(false);
120 		// formPanel.setBorder(CommonUI.create4PEmptyBorder());
121 		createUrlPanel(formPanel);
122 
123 		// its an evidence step
124 		if (bean instanceof Evidence) {
125 			createEvidenceTitlePanel(formPanel);
126 			createEvidenceAttributionPanel(formPanel);
127 		//createHTMLTextArea(formPanel, 10);
128 		} else {
129 			//createHTMLTextArea(formPanel, 6);
130 		}// if
131 
132 		this.enableWebWidgets(webSelected);
133 		this.enableLocalWidgets(localSelected);
134 
135 		add(formPanel, BorderLayout.CENTER);
136 	}
137 
138 	protected void enableLocalWidgets(boolean b) {
139 		localUrlText.setEnabled(b);
140 		browseButton.setEnabled(b);
141 		htmlTextArea.setEnabled(b);
142 		//htmlLabel.setEnabled(b);
143 	}
144 
145 	protected void enableWebWidgets(boolean b) {
146 		webUrlText.setEnabled(b);
147 		setPageButton.setEnabled(b);
148 	}
149 
150 	protected void createUrlPanel(final JPanel urlPanel) {
151 		// FIXME this isn't handling the URL yet
152 
153 		JLabel introLabel = new JLabel(
154 				"Select a local HTML file or paste a web HTML link.");
155 		introLabel.setFont(introLabel.getFont().deriveFont(Font.BOLD));
156 		introLabel.setHorizontalAlignment(SwingConstants.LEFT);
157 		urlPanel.add(introLabel, "0,0, 4,0");
158 		
159 		URL url = ((AbstractUrlStep) bean).getUrl();
160 		
161 		String theUrl = null;
162 		if( url != null)
163 			 theUrl = url.toString();
164 		else
165 			theUrl = "http://www.google.com";
166 		boolean isLocal = StringUtils.contains(theUrl, "podar");
167 		String localText = null;
168 		String webText = null;
169 
170 		if (isLocal) {
171 			localText = theUrl;
172 			webText = "";
173 			localSelected = true;
174 			webSelected = false;
175 		} else {
176 			localText = "";
177 			webText = theUrl;
178 			localSelected = false;
179 			webSelected = true;
180 		}// if
181 
182 		ButtonGroup group = new ButtonGroup();
183 
184 		// web file
185 		webLinkRadio = new JRadioButton("Web Address:");
186 		webLinkRadio.setSelected(webSelected);
187 		webLinkRadio.addActionListener(new ActionListener() {
188 
189 			public void actionPerformed(ActionEvent e) {
190 				enableLocalWidgets(false);
191 				enableWebWidgets(true);
192 			}
193 		});
194 		webLinkRadio.setOpaque(false);
195 		group.add(webLinkRadio);
196 		urlPanel.add(webLinkRadio, "0,2");
197 
198 		webUrlText = new JTextField(webText, TEXT_BOX_LEN);
199 		webUrlText.requestFocus();
200 
201 	
202 		webUrlText.setEditable(true);
203 		urlPanel.add(webUrlText, "2,2");
204 
205 		setPageButton = new JButton("Set Web URL");
206 	//	setPageButton.setIcon(new ImageIcon(getClass().getResource("icons/setHTML16.png")));
207 		setPageButton.addActionListener(new ActionListener() {
208 
209 			public void actionPerformed(ActionEvent e) {
210 				String text = webUrlText.getText();
211 
212 				if (text != null) {
213 					try {
214 						((AbstractUrlStep) bean).setUrl(new URL(text));
215 					} catch (MalformedURLException e1) {
216 						// TODO Auto-generated catch block
217 						e1.printStackTrace();
218 					}
219 				}// if
220 			}
221 		});
222 		setPageButton.setToolTipText("Set the page for this set as the entered web address.");
223 		CommonUI.makeEnhancedButton(setPageButton);
224 		urlPanel.add(setPageButton, "4,2");
225 		
226 		
227 		// local file
228 		localFileRadio = new JRadioButton("Local File:");
229 		localFileRadio.setSelected(localSelected);
230 
231 		localFileRadio.addActionListener(new ActionListener() {
232 
233 			public void actionPerformed(ActionEvent e) {
234 				enableLocalWidgets(true);
235 				enableWebWidgets(false);
236 
237 			}
238 		});
239 		localFileRadio.setOpaque(false);
240 		group.add(localFileRadio);
241 		urlPanel.add(localFileRadio, "0,4");
242 
243 		localUrlText = new JTextField(localText, TEXT_BOX_LEN);
244 		localUrlText.requestFocus();
245 		localUrlText.setEditable(false);
246 		urlPanel.add(localUrlText, "2,4");
247 
248 		browseButton = new JButton("Import File");
249 		//browseButton.setIcon(new ImageIcon(getClass().getResource("icons/importHTML16.png")));
250 		browseButton.addActionListener(new ActionListener() {
251 
252 			public void actionPerformed(ActionEvent e) {
253 					
254 				final File selectedFile = showChooserAndReturnFile();
255 
256 				if( selectedFile == null )
257 					return;
258 
259 				/*
260 				try {
261 					FileReader reader = new FileReader(selectedFile);
262 					htmlTextArea.read(reader,
263 							selectedFile.getName());
264 					reader.close();
265 					htmlTextArea.revalidate();
266 					System.out.println("text: " + htmlTextArea.getText());
267 
268 				} catch (IOException e1) {
269 					e1.printStackTrace();
270 				}// try
271 					*/
272 				
273 				Pod browseWebStep = PodUtils.resolvePod(bean);
274 				try {
275 					// need to chech if the archive already exists
276 					// and get the existing one.
277 					JarOutputStream podResources = BinaryUtils
278 							.newPodArchiveFor(browseWebStep);
279 
280 					resURL = BinaryUtils.addToArchive(browseWebStep,
281 							podResources, selectedFile.getName(),
282 							new FileInputStream(selectedFile));
283 					podResources.close();
284 					// ((AbstractUrlStep) bean).setUrl(resURL);
285 
286 				} catch (Exception exc) {
287 					exc.printStackTrace();
288 				}// try
289 		
290 				
291 				((BrowseWeb) bean).setUrl(resURL);
292 				
293 //				System.out.println("text: " + htmlTextArea.getText());
294 //				htmlTextArea.revalidate();				
295 			}
296 		});
297 		browseButton.setToolTipText("Import a HTML from a local file.");
298 		CommonUI.makeEnhancedButton(browseButton);
299 		urlPanel.add(browseButton, "4,4");
300 
301 		urlPanel.setMaximumSize(urlPanel.getPreferredSize());
302 	}
303 	
304 	
305 
306 	protected void createEvidenceTitlePanel(final JPanel ePanel) {
307 		final JLabel eTitleLabel = new JLabel("Evidence Title:");
308 		eTitleLabel.setFont(eTitleLabel.getFont().deriveFont(Font.BOLD));
309 		eTitleLabel.setHorizontalAlignment(SwingConstants.LEFT);
310 		eTitleLabel.setOpaque(false);
311 		ePanel.add(eTitleLabel, "0,6");
312 		final JTextField eTitleText = new JTextField(((Evidence) bean)
313 				.getEvidenceTitle(), TEXT_BOX_LEN);
314 		eTitleText.requestFocus();
315 		eTitleText.addFocusListener(new FocusListener() {
316 
317 			public void focusGained(FocusEvent e) {
318 				// TODO Auto-generated method stub
319 
320 			}
321 
322 			public void focusLost(FocusEvent e) {
323 				((Evidence) bean).setEvidenceTitle(eTitleText.getText());
324 				System.out.println("et.focusLost()");
325 
326 			}
327 		});
328 		ePanel.add(eTitleText, "2,6");
329 		ePanel.setMaximumSize(ePanel.getPreferredSize());
330 	}
331 
332 	protected void createEvidenceAttributionPanel(final JPanel aPanel) {
333 
334 		final JLabel attLabel = new JLabel("Attribution:");
335 		attLabel.setFont(attLabel.getFont().deriveFont(Font.BOLD));
336 		attLabel.setHorizontalAlignment(SwingConstants.LEFT);
337 		attLabel.setOpaque(false);
338 		aPanel.add(attLabel, "0,8");
339 		final JTextField attText = new JTextField(((Evidence) bean)
340 				.getAttribution());
341 		attText.addFocusListener(new FocusListener() {
342 
343 			public void focusGained(FocusEvent e) {
344 				// TODO Auto-generated method stub
345 
346 			}
347 
348 			public void focusLost(FocusEvent e) {
349 				((Evidence) bean).setAttribution(attText.getText());
350 				System.out.println("att.focusLost()");
351 
352 			}
353 		});
354 		attText.requestFocus();
355 		aPanel.add(attText, "2,8");
356 		aPanel.setMaximumSize(aPanel.getPreferredSize());
357 	}
358 
359 	protected void createHTMLTextArea(final JPanel htmlPanel, int row) {
360 
361 		htmlLabel = new JLabel("HTML Text:");
362 		// htmlLabel.setFont(htmlLabel.getFont().deriveFont(
363 		// Font.BOLD));
364 		htmlLabel.setHorizontalAlignment(SwingConstants.LEFT);
365 		htmlLabel.setVerticalTextPosition(SwingConstants.NORTH);
366 		htmlLabel.setOpaque(false);
367 		htmlPanel.add(htmlLabel, "0," + row + ",l,t"); //$NON-NLS-1$
368 
369 		htmlTextArea.getDocument().addDocumentListener(new DocumentListener() {
370 
371 			public void changedUpdate(DocumentEvent e) {
372 				setHtmlModified(true);
373 			}
374 
375 			public void insertUpdate(DocumentEvent e) {
376 				setHtmlModified(true);
377 			}
378 
379 			public void removeUpdate(DocumentEvent e) {
380 				setHtmlModified(true);
381 			}
382 		});
383 		htmlTextArea.addFocusListener(new FocusListener() {
384 
385 			public void focusGained(FocusEvent e) {
386 			}
387 
388 			public void focusLost(FocusEvent e) {
389 				System.out.println(".focusLost()");
390 				if (isHtmlModified()) {
391 					saveHTMLtoPod();
392 					setHtmlModified(false);
393 				}// if
394 			}
395 		});
396 		CommonUI.makeMultilineEditable(htmlTextArea);
397 		htmlTextArea.setEditable(true);
398 
399 		CurrentLineHighlighter.install(htmlTextArea);
400 		htmlTextArea.setOpaque(true);
401 		htmlTextArea.setBackground(PasColors.basicInfoTextAreaColor);
402 
403 		htmlAreaScrollPane = new JScrollPane(htmlTextArea);
404 		htmlAreaScrollPane
405 				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
406 		htmlAreaScrollPane
407 				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
408 		htmlAreaScrollPane.setPreferredSize(new Dimension(150, 500));
409 		htmlAreaScrollPane.setBorder(CommonUI.createEtchedBorder());
410 		htmlPanel.add(htmlAreaScrollPane, "2," + row); //$NON-NLS-1$
411 		htmlPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
412 	}
413 
414 	/***
415 	 * 
416 	 */
417 	protected void saveHTMLtoPod() {
418 		Pod browseWebStep = PodUtils.resolvePod(bean);
419 
420 		if (currentFileName == null)
421 			currentFileName = Math.random() + ".htm";
422 
423 		File newFile = new File(currentFileName);
424 		try {
425 			htmlTextArea.write(new FileWriter(newFile));
426 		} catch (IOException e) {
427 			// TODO Auto-generated catch block
428 			e.printStackTrace();
429 		}
430 		// String authoredDataName = "mwStepTest.mml";
431 		// File modelResource = new File("src/test/resources/" +
432 		// authoredDataName);
433 
434 		// need to get the pod
435 		// figure out the name of the file to add to the
436 
437 		try {
438 			// need to chech if the archive already exists
439 			// and get the existing one.
440 
441 			Pod somePod = PodUtils.resolvePod(bean);
442 
443 			JarOutputStream podResources = BinaryUtils
444 					.newPodArchiveFor(browseWebStep);
445 
446 			URL resURL = BinaryUtils.addToArchive(somePod, podResources,
447 					newFile.getName(), new FileInputStream(newFile));
448 			podResources.close();
449 			((AbstractUrlStep) bean).setUrl(resURL);
450 		} catch (Exception exc) {
451 			exc.printStackTrace();
452 		}// try
453 
454 	}
455 
456 	public static void main(String[] args) {
457 		JFrame f = new JFrame();
458 
459 		JButton goButton = new JButton("go");
460 		goButton.addActionListener(new ActionListener() {
461 
462 			public void actionPerformed(ActionEvent e) {
463 				CopyOfHtmlQuickEditorUI hq = new CopyOfHtmlQuickEditorUI();
464 
465 				JDialog d = new JDialog();
466 				d.getContentPane().add(hq);
467 				d.setVisible(true);
468 
469 			}
470 		});
471 
472 		f.getContentPane().add(goButton);
473 		f.setSize(100, 100);
474 		f.setVisible(true);
475 	}
476 
477 	/***
478 	 * @return the bean
479 	 */
480 	public Object getBean() {
481 		return bean;
482 	}
483 
484 	/***
485 	 * @param bean
486 	 *            the bean to set
487 	 */
488 	public void setBean(Object bean) {
489 		this.bean = bean;
490 		this.createUI();
491 	}
492 
493 	class HtmlFilter extends javax.swing.filechooser.FileFilter {
494 		public boolean accept(File file) {
495 			String filename = file.getName();
496 
497 			if (file.isDirectory())
498 				return true;
499 			if (filename.endsWith(".html"))
500 				return true;
501 			if (filename.endsWith(".htm"))
502 				return true;
503 
504 			return false;
505 		}
506 
507 		public String getDescription() {
508 			return "*.html,*.htm";
509 		}
510 	}
511 
512 	/***
513 	 * @return the htmlModified
514 	 */
515 	public boolean isHtmlModified() {
516 		return htmlModified;
517 	}
518 
519 	/***
520 	 * @param htmlModified
521 	 *            the htmlModified to set
522 	 */
523 	public void setHtmlModified(boolean htmlModified) {
524 		this.htmlModified = htmlModified;
525 	}
526 
527 	/***
528 	 * @return
529 	 */
530 	protected File showChooserAndReturnFile() {
531 		JFileChooser chooser = new JFileChooser();
532 		chooser.setFileFilter(new HtmlFilter());
533 		int returnVal = chooser.showOpenDialog(browseButton);
534 		if (returnVal != JFileChooser.APPROVE_OPTION) {
535 			return null;
536 		}
537 
538 		final File selectedFile = chooser.getSelectedFile();
539 		return selectedFile;
540 	}
541 
542 }