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