View Javadoc

1   /*
2    * Copyright (c) 2007 Regents of the University of California (Regents). Created
3    * by TELS, Graduate School of Education, University of California at Berkeley.
4    *
5    * This software is distributed under the GNU Lesser General Public License, v2.
6    *
7    * Permission is hereby granted, without written agreement and without license
8    * or royalty fees, to use, copy, modify, and distribute this software and its
9    * documentation for any purpose, provided that the above copyright notice and
10   * the following two paragraphs appear in all copies of this software.
11   *
12   * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
13   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
14   * PURPOSE. THE SOFTWAREAND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
15   * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
16   * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17   *
18   * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
19   * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
20   * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
21   * REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22   */
23  package org.telscenter.pas.steps.quickEditors.html;
24  
25  import java.awt.BorderLayout;
26  import java.awt.event.ActionListener;
27  import java.awt.event.FocusEvent;
28  import java.awt.event.FocusListener;
29  import java.net.MalformedURLException;
30  import java.net.URL;
31  
32  import javax.swing.Action;
33  import javax.swing.JButton;
34  import javax.swing.JComponent;
35  import javax.swing.JLabel;
36  import javax.swing.JPanel;
37  import javax.swing.JTextField;
38  
39  import net.miginfocom.swing.MigLayout;
40  
41  import org.apache.commons.lang.StringUtils;
42  import org.telscenter.pas.common.ui.panel.TextEditorPanel;
43  import org.telscenter.pas.steps.BrowseWebUI;
44  import org.telscenter.pas.steps.BrowseWeb.PopupPreviewAction;
45  
46  /***
47   * Creates the Authoring Mode for the OutsideLink Step
48   * 
49   * @author aperritano
50   *
51   */
52  public class OutsideLinkQuickEditorUI extends HtmlQuickEditorUI {
53  
54  	private static final int TEXT_BOX_LEN = 32;
55  
56  	private JButton browseButton;
57  	private JTextField externalUrlText;
58  	private JButton previewButton;
59  	
60  	
61  	/***
62  	 * Creates the UI for the step specific authoring that is displayed
63  	 * on the bottom right main section of the authoring tool
64  	 */
65  	protected void createUI() { 
66  		this.setOpaque(false);
67  		
68  		/* this is a layout that is not standard in Swing, the argument 
69  		   "insets # # # #"  determines how much spacing is on the top,
70  		   left, bottom, right from the border of the panel respectively */
71  		MigLayout layout = new MigLayout("insets 8 2 4 2"); 
72  		
73  		this.setLayout(layout);
74  		
75  		//the label for the text field
76  		this.add(createExternalLabelPanel());
77  		
78  		//the text field for the user to enter a URL
79  		this.add(createLocalTextPanel());
80  		
81  		//the preview button
82  		this.add(createPreviewButton(), "wrap");		
83  	}
84  
85  
86  
87  
88  	/***
89  	 * @return the panel that contains the preview button
90  	 */
91  	private JPanel createPreviewButton() {
92  		JPanel previewButtonPanel = new JPanel(new BorderLayout(0,0));
93  		
94  		//the action that is performed when the preview button is clicked
95  		PopupPreviewAction previewAction = 
96  			(PopupPreviewAction) browseWebBean.getPreviewAction(
97  					false, externalUrlText);
98  		
99  		
100 		previewAction.putValue(Action.NAME, "Preview Step");
101 		
102 		//sets the action to the button
103 		previewButton = new JButton(previewAction);
104 				
105 		previewButton.setOpaque(false);
106 		
107 		//adds the button to the right side of the panel
108 		previewButtonPanel.add(previewButton,BorderLayout.EAST);
109 		
110 		return previewButtonPanel;
111 	}
112 
113 
114 
115 
116 	/***
117 	 * @return the component that contains the text label
118 	 */
119 	private JComponent createExternalLabelPanel() {
120 		JLabel local = new JLabel("Outside Link:");
121 		return local;
122 	}
123 
124 	/*
125 	 * @return the component that contains the text field the user will 
126 	 * 			the URL into
127 	 */
128 	private JComponent createLocalTextPanel() {
129 		
130 		final JTextField outsideLinkUrlTextField = new JTextField(TEXT_BOX_LEN);
131 		
132 		if( browseWebBean.getUrl() != null ) {
133 			//if the url is specified in the otml
134 			String url = browseWebBean.getUrl().toString();
135 			outsideLinkUrlTextField.setText(url);
136 		} else {
137 			//if the url is not specified or if the step is newly created
138 			outsideLinkUrlTextField.setText("http://www.google.com/");
139 		}
140 		
141 		
142 		outsideLinkUrlTextField.setName("Url");
143 		
144 		outsideLinkUrlTextField.addFocusListener(new FocusListener() {
145 			public void focusGained(FocusEvent e) {
146 				// TODO Auto-generated method stub
147 			}
148 
149 			public void focusLost(FocusEvent e) {
150 				
151 				//TODO: check to see if url is well formed
152 				try {
153 					browseWebBean.setUrl(new URL(outsideLinkUrlTextField.getText()));
154 				} catch (MalformedURLException e1) {
155 					//TODO popup error
156 					//e1.printStackTrace();
157 				}// try
158 			}
159 		});
160 		
161 		outsideLinkUrlTextField.requestFocus();
162 		
163 		/* initially sets the text field to not editable until user clicks 
164 		   edit button */
165 		outsideLinkUrlTextField.setEditable(false);
166 		
167 		
168 		outsideLinkUrlTextField.setBackground(this.getBackground());
169 		
170 		TextEditorPanel panel = new TextEditorPanel(outsideLinkUrlTextField,null);
171 		
172 		/* sets the text field to this global variable so it can be accessed 
173 		   easily later */
174 		externalUrlText = outsideLinkUrlTextField;
175 		return panel;
176 	}
177 	
178 	/*
179 	 * This might not be used when using the mozswing browser
180 	 */
181 	protected void applyUrl() {
182 		if( externalUrlText.getText() != null && StringUtils.trimToNull(externalUrlText.getText()) != null ) {
183 			try {
184 				browseWebBean.setUrl(new URL(externalUrlText.getText()));
185 			} catch (MalformedURLException e1) {
186 				// TODO Auto-generated catch block
187 				//e1.printStackTrace();
188 			}
189 		}
190 	}
191 
192 }