View Javadoc

1   /**************************************************************************
2   Copyright 2005 Webstersmalley
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  *************************************************************************/
16  
17  
18  package com.webstersmalley.picweb.offline.config;
19  
20  import java.awt.FileDialog;
21  import java.awt.event.ActionEvent;
22  import java.io.File;
23  import java.io.FileNotFoundException;
24  
25  import javax.swing.AbstractAction;
26  import javax.swing.Action;
27  import javax.swing.JButton;
28  import javax.swing.JFrame;
29  import javax.swing.JLabel;
30  import javax.swing.JTextField;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.swixml.SwingEngine;
35  import org.w3c.dom.Document;
36  
37  import com.webstersmalley.picweb.offline.WebsiteGenerator;
38  import com.webstersmalley.picweb.utils.IOUtils;
39  
40  /***
41   * GUI Frame holding the configuration options and details.
42   *
43   * This UI will be generated by an XML file via SwiXML.
44   *
45   * @see ConfigurationModel
46   * @author Matthew Smalley
47   */
48  public class ConfigurationFrame
49  {
50  	/*** Logger for the class. */
51  	private static Log log = LogFactory.getLog(ConfigurationFrame.class);
52  	
53  	/*** The GUI Frame */
54  	private JFrame frame;
55  	/*** Label to display status */
56  	public JLabel statusLabel;
57  	/*** Root folder text field */
58  	public JTextField rootFolder;
59  	/*** Output folder text field */
60  	public JTextField outputFolder;
61  	/*** Title text field */
62  	public JTextField title;
63  	/*** Row size text field */
64  	public JTextField rowSize;
65  	/*** Maximum rows text field */
66  	public JTextField maxRows;
67  	/*** Thumb size text field */
68  	public JTextField thumbSize;
69  	/*** Image size text field */
70  	public JTextField imageSize;
71  	/*** Template filename text field */
72  	public JTextField templateFilename;
73  	/*** Google Ad ID text field */
74  	public JTextField googleAdId;
75  	/*** Artifacts folder */
76  	public JTextField artifactsFolder;
77  	/*** Go button text field */
78  	public JButton goButton;
79  
80  	/***
81  	 * Controls what happens when you select open through the menu
82  	 * or via the hotkey (CTRL-O).
83  	 * Brings up a file dialog to open a configuration file
84  	 */
85  	public Action openAction = new AbstractAction()
86  	{
87  		public void actionPerformed(ActionEvent e)
88  		{
89  			log.debug("Open");
90  			FileDialog fd = new FileDialog(frame, "Select a configuration file to load", FileDialog.LOAD);
91  			fd.setDirectory(".");
92  			fd.setVisible(true);
93  			String filename = fd.getFile();
94  			log.debug(filename);
95  			if (fd != null)
96  			{
97  				try
98  				{
99  					ConfigurationModel model = ConfigurationModel.fromXML(filename);
100 					populateFromModel(model);
101 				}
102 				catch (Exception ex)
103 				{
104 					log.error("Error detected loading configuration", ex);
105 				}
106 			}
107 		}
108 	};
109 
110 	/***
111 	 * Controls what happens when you select save through the menu
112 	 * or via the hotkey (CTRL-S).
113 	 * Brings up a file dialog to save the configuration
114 	 */
115 	public Action saveAction = new AbstractAction()
116 	{
117 		public void actionPerformed(ActionEvent e)
118 		{
119 			log.debug("Save");
120 			FileDialog fd = new FileDialog(frame, "Select a configuration file to save", FileDialog.SAVE);
121 			fd.setDirectory(".");
122 			fd.setVisible(true);
123 			String filename = fd.getFile();
124 			log.debug(filename);
125 			try
126 			{
127 				Document config = getConfigurationModel().toXML();
128 				IOUtils.writeXmlFile(filename, config);
129 			} 
130 			catch (Exception ex)
131 			{
132 				log.error("Error saving configuration", ex);
133 			}
134 		}
135 	};
136 
137 	/***
138 	 * Controls what happens when you select exit through the menu
139 	 * or via the hotkey (ALT-F4).
140 	 * Essentially, exits the application
141 	 */
142 	public Action exitAction = new AbstractAction()
143 	{
144 		public void actionPerformed(ActionEvent e)
145 		{
146 			log.debug("Exit");
147 			System.exit(0);
148 		}
149 	};
150 
151 	/***
152 	 * Action to be performed when the submit button is clicked.
153 	 *
154 	 * This instantiates a {@link ConfigurationModel} based on the
155 	 * currently selected configuration. This is then passed to the
156 	 * {@link WebsiteGenerator}, which is then finally exceuted.
157 	 * @see #getConfigurationModel
158 	 */
159 	public Action submitAction = new AbstractAction()
160 	{
161 		public void actionPerformed(ActionEvent e)
162 		{
163 			log.debug("Submit");
164 			goButton.setEnabled(false);
165 			statusLabel.setText("Generating...");
166 
167 			ConfigurationModel model = getConfigurationModel();
168 			try
169 			{
170 				WebsiteGenerator generator = new WebsiteGenerator(model);
171 				generator.go();
172 			}
173 			catch (Exception ex)
174 			{
175 				log.error("Error generating website: ", ex);
176 			}
177 			statusLabel.setText("Completed");
178 			goButton.setEnabled(true);
179 		}
180 	};
181 
182 	/***
183 	 * Constructs a configuration frame.
184 	 * Loads in an xml file based on the classname, and instantiates
185 	 * the GUI using SwiXML.
186 	 */
187 	public ConfigurationFrame()
188 	{
189 		String filename = this.getClass().getName() + ".xml";
190 		File uiFile = new File("src/xml" + File.separator + filename);
191 
192 		try
193 		{
194 			log.debug("Launching " + filename + " UI");
195 			frame = (JFrame) new SwingEngine(this).render(uiFile);
196 			frame.setVisible(true);
197 		} catch (Exception e)
198 		{
199 			log.error("Error launching UI", e);
200 		}
201 	}
202 
203 	/***
204 	 * Returns a new {@link ConfigurationModel} based on
205 	 * the state of the frame.
206 	 * @return the generated model
207 	 */
208 	private ConfigurationModel getConfigurationModel()
209 	{
210 		ConfigurationModel model = new ConfigurationModel();
211 		model.setRootFolder(rootFolder.getText());
212 		model.setOutputFolder(outputFolder.getText());
213 		model.setTitle(title.getText());
214 		model.setRowSize(new Integer(rowSize.getText()).intValue());
215 		model.setMaxRows(new Integer(maxRows.getText()).intValue());
216 		model.setThumbSize(new Integer(thumbSize.getText()).intValue());
217 		model.setImageSize(new Integer(imageSize.getText()).intValue());
218 		model.setTemplateFilename(templateFilename.getText());
219 		model.setGoogleAdId(googleAdId.getText());
220 		model.setArtifactsFolder(artifactsFolder.getText());
221 		return model;
222 	}
223 	
224 	/***
225 	 * Populates the text fields from values from the passed in model
226 	 * @param model
227 	 */
228 	private void populateFromModel(ConfigurationModel model)
229 	{
230 		rootFolder.setText(model.getRootFolder());
231 		outputFolder.setText(model.getOutputFolder());
232 		title.setText(model.getTitle());
233 		rowSize.setText(Integer.toString(model.getRowSize()));
234 		maxRows.setText(Integer.toString(model.getMaxRows()));
235 		thumbSize.setText(Integer.toString(model.getThumbSize()));
236 		imageSize.setText(Integer.toString(model.getImageSize()));
237 		templateFilename.setText(model.getTemplateFilename());
238 		googleAdId.setText(model.getGoogleAdId());
239 		artifactsFolder.setText(model.getArtifactsFolder());
240 	}
241 
242 	/***
243 	 * Main method for the entire Picweb<i>offline</i> application.
244 	 *
245 	 * @param args ignored.
246 	 */
247 	public static void main(String[] args)
248 	{
249 		new ConfigurationFrame();
250 	}
251 }