PageRenderTime 65ms CodeModel.GetById 17ms app.highlight 42ms RepoModel.GetById 2ms app.codeStats 0ms

/src/forteresce/portprofile/gui/panels/ProfilesImportPanel.java

http://portprofile.googlecode.com/
Java | 268 lines | 198 code | 50 blank | 20 comment | 21 complexity | c67980949438ea3af2a3bfafe171c390 MD5 | raw file
  1/**
  2 * Date Created: Apr 20, 2009
  3 */
  4
  5package forteresce.portprofile.gui.panels;
  6
  7import java.awt.Dimension;
  8import java.awt.GridBagConstraints;
  9import java.awt.GridBagLayout;
 10import java.awt.Insets;
 11import java.awt.event.ActionEvent;
 12import java.awt.event.ActionListener;
 13import java.io.File;
 14import java.util.List;
 15import java.util.concurrent.ExecutionException;
 16
 17import javax.swing.JButton;
 18import javax.swing.JFrame;
 19import javax.swing.JLabel;
 20import javax.swing.JPanel;
 21import javax.swing.JTextField;
 22import javax.swing.SwingConstants;
 23import javax.swing.SwingWorker;
 24import javax.swing.filechooser.FileFilter;
 25
 26import org.apache.commons.io.FileUtils;
 27import org.apache.log4j.Logger;
 28
 29import forteresce.portprofile.config.constants.ProfileConstants;
 30import forteresce.portprofile.config.enums.SystemPropertiesEnum;
 31import forteresce.portprofile.gui.PortProfileFrame;
 32import forteresce.portprofile.gui.dialogs.PortProfileDialogs;
 33import forteresce.portprofile.gui.events.ShowProfilesEvent;
 34import forteresce.portprofile.gui.events.ShowWaitEvent;
 35import forteresce.portprofile.profiles.bean.ProfileBean;
 36import forteresce.portprofile.profiles.bean.ProfileBeanImpl;
 37import forteresce.portprofile.profiles.util.ProfileUtil;
 38import forteresce.portprofile.profiles.util.ZipUtil;
 39
 40/**
 41 * A simple panel to be used as a place holder for the profile selection 
 42 * @author forteresce
 43 */
 44public class ProfilesImportPanel extends JPanel implements ActionListener {
 45	private static final long serialVersionUID = 4622949131851593532L;
 46	private Logger log = Logger.getLogger(ProfilesImportPanel.class);
 47
 48	private JFrame parentFrame;
 49	private JTextField fileField;
 50	private JTextField importProfileName;
 51	
 52	private static final String BROWSE_BUTTON_TEXT = "...";
 53	private static final String IMPORT_BUTTON_TEXT = "Import";
 54
 55	public ProfilesImportPanel(JFrame parentFrame) {
 56		super();
 57
 58		//store the reference to parent frame
 59		this.parentFrame = parentFrame;
 60		
 61		//the regular ones
 62		setMinimumSize(ProfileConstants.MAIN_PANEL_DIM);
 63		setLayout(new GridBagLayout());
 64		
 65		//add the components
 66		addComponents();
 67	}
 68	
 69	private void addComponents() {
 70		Dimension labelDimension = new Dimension(125, 25);
 71		Dimension fieldDimension = new Dimension(160, 25);
 72
 73		JLabel selectLabel = new JLabel("Select a profile:");
 74		selectLabel.setHorizontalAlignment(SwingConstants.RIGHT);
 75		selectLabel.setPreferredSize(labelDimension);
 76
 77		fileField = new JTextField();
 78		fileField.setPreferredSize(fieldDimension);
 79		
 80		JButton browseButton = new JButton(BROWSE_BUTTON_TEXT);
 81		browseButton.addActionListener(this);
 82		browseButton.setPreferredSize(new Dimension(25, 20));
 83		
 84		JLabel importAsLabel = new JLabel("Import As:");
 85		importAsLabel.setHorizontalAlignment(SwingConstants.RIGHT);
 86		importAsLabel.setPreferredSize(labelDimension);
 87		
 88		importProfileName = new JTextField();
 89		importProfileName.setPreferredSize(fieldDimension);
 90		importProfileName.setText("pp_" + System.currentTimeMillis());
 91		
 92		JButton importButton = new JButton("Import");
 93		importButton.addActionListener(this);
 94		
 95		//add the components to the pane
 96		GridBagConstraints constraints = new GridBagConstraints();
 97		constraints.insets = new Insets(5, 10, 5, 10);
 98		
 99		constraints.gridx = 0;
100		constraints.gridy = 0;
101		add(selectLabel, constraints);
102
103		constraints.gridx = 1;
104		constraints.gridy = 0;
105		add(fileField, constraints);
106
107		constraints.gridx = 2;
108		constraints.gridy = 0;
109		add(browseButton, constraints);
110
111		constraints.gridx = 0;
112		constraints.gridy = 1;
113		add(importAsLabel, constraints);
114		
115		constraints.gridx = 1;
116		constraints.gridy = 1;
117		add(importProfileName, constraints);
118		
119		constraints.gridx = 0;
120		constraints.gridy = 2;
121		constraints.gridwidth = 3;
122		add(importButton, constraints);
123	}
124	
125	/**
126	 * All of the main logic is performed on the user action
127	 */
128	public void actionPerformed(ActionEvent event) {
129		JButton button = (JButton) event.getSource();
130		String text = button.getText();
131
132		if (BROWSE_BUTTON_TEXT.equals(text)) {
133			fileField.setText(PortProfileDialogs.showFileChooser(parentFrame, new FileFilter() {
134				@Override
135				public boolean accept(File f) {
136					return f.isDirectory() || f.getName().endsWith(ProfileConstants.PROFILE_FILE_EXTN);
137				}
138				
139				@Override
140				public String getDescription() {
141					return ProfileConstants.PROFILE_FILE_DESC;
142				}
143			}));
144		} else {
145			if (IMPORT_BUTTON_TEXT.equals(text) && validateInput()) {
146				
147				if (parentFrame instanceof PortProfileFrame) {
148					((PortProfileFrame) parentFrame).dispatchEvent(new ShowWaitEvent(
149							parentFrame, "Please wait while profile is uncompressed."));
150				}
151				
152				ProfileImporter importer = new ProfileImporter();		
153				importer.execute();		
154			}
155		}
156	}
157
158	private boolean validateInput() {
159		String fileName = fileField.getText();
160		if (fileName == null || "".equals(fileName)) {
161		   PortProfileDialogs.showError(parentFrame, "Please select a file to import from.");
162		   return false;
163		}
164
165		String profileName = importProfileName.getText();
166		if (!profileName.matches("\\w+")) {
167			PortProfileDialogs.showError(parentFrame, "Please enter a valid import as profile name.");
168			return false;
169		}
170		
171		try {
172			if (null != ProfileUtil.getProfileByName(profileName)) {
173				PortProfileDialogs.showError(parentFrame, "Conflicting profile name. Please enter another name.");
174				return false;
175			}
176		} catch (Exception e) {
177			log.error("Error checking for conflicting profiles.", e);
178		}
179		
180		return true;
181	}
182
183	
184	/**
185	 * An inner swingworker class to do the importing of the profile
186	 * @author forteresce
187	 */
188	class ProfileImporter extends SwingWorker<Boolean, Void> {
189		private Logger log = Logger.getLogger(ProfileImporter.class);
190
191		private static final int UNZIPPED_PROFILE = 1;
192		private File newDir = null;
193
194		@Override
195		protected Boolean doInBackground() throws Exception {
196			String profileZip = fileField.getText();
197			
198			// 1. Unzip the profile to tmp folder
199			boolean result = ZipUtil.unzip(profileZip, SystemPropertiesEnum.JAVA_IO_TMPDIR.get());
200			if (!result) {
201				return Boolean.FALSE;
202			} else {
203				setProgress(UNZIPPED_PROFILE);
204				publish(new Void[1]);
205			}
206
207			// 2. Add entry to the existing profiles
208			String parentFolder = ZipUtil.getParentFolder(profileZip); 
209			if (null == parentFolder) {
210				return Boolean.FALSE;
211			}
212
213			File unzippedProfileDir = new File(SystemPropertiesEnum.JAVA_IO_TMPDIR.get() + SystemPropertiesEnum.FILE_SEPARATOR.get() + parentFolder);
214			newDir = new File (ProfileUtil.generateProfilePath(importProfileName.getText()));
215			
216			FileUtils.copyDirectory(unzippedProfileDir, newDir);
217			log.debug("Created profile in : " + newDir.getAbsolutePath());
218			
219			ProfileBean newProfile = new ProfileBeanImpl();
220			newProfile.setName(importProfileName.getText());
221			newProfile.setDefault("1");
222			newProfile.setIsRelative("0");
223			newProfile.setPath(newDir.getAbsolutePath());
224			
225			if (!ProfileUtil.addNewProfile(newProfile)) {
226				return Boolean.FALSE;
227			}
228			
229			return Boolean.TRUE;
230		}
231		
232		@Override
233		protected void process(List<Void> chunks) {
234			if (parentFrame instanceof PortProfileFrame) {
235				switch(getProgress()) {
236					case UNZIPPED_PROFILE: ((PortProfileFrame) parentFrame).dispatchEvent(new ShowWaitEvent(parentFrame, "Done uncompressing.\nAdding new profile."));
237												break;
238					default: ((PortProfileFrame) parentFrame).dispatchEvent(new ShowWaitEvent(parentFrame, "Status Unknown."));
239				}
240			}
241		}
242		
243		@Override
244		protected void done() {
245			super.done();
246			try {
247				if (parentFrame instanceof PortProfileFrame) {
248					((PortProfileFrame) parentFrame) .dispatchEvent(new ShowProfilesEvent(parentFrame));
249				}
250
251				if(!get().booleanValue()) {
252					switch(getProgress()) {
253						case UNZIPPED_PROFILE: PortProfileDialogs.showError(parentFrame, "Error adding profile.");
254													break;
255						default: PortProfileDialogs.showError(parentFrame, "Error unzipping selected profile file.");
256					}
257					
258				} else {
259					PortProfileDialogs.showInfo(parentFrame, "Profile successfully imported.\nProfile Folder: " + newDir.getAbsolutePath());
260				}
261			} catch (InterruptedException e) {
262				log.error(e);
263			} catch (ExecutionException e) {
264				log.error(e);
265			}
266		}
267	}
268}