Guest User

Untitled

a guest
Jul 29th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. package backup;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.WindowAdapter;
  7. import java.awt.event.WindowEvent;
  8. import java.beans.PropertyChangeEvent;
  9. import java.beans.PropertyChangeListener;
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14.  
  15. import javax.swing.BoxLayout;
  16. import javax.swing.JButton;
  17. import javax.swing.JFileChooser;
  18. import javax.swing.JFrame;
  19. import javax.swing.JLabel;
  20. import javax.swing.JOptionPane;
  21. import javax.swing.JPanel;
  22. import javax.swing.JTextField;
  23. import javax.swing.WindowConstants;
  24.  
  25. import org.apache.commons.io.FileUtils;
  26.  
  27. @SuppressWarnings("serial")
  28. public class BackupApplication extends JPanel implements ActionListener, PropertyChangeListener
  29. {
  30. JFileChooser chooser;
  31. JButton src, dest, start;
  32. JPanel srcHost, destHost, startHost, headHost;
  33. JTextField srcText, destText, destArea, nameText, delayText;
  34. JLabel statusText, nameLabel, delayLabel;
  35. Thread t;
  36. File data;
  37.  
  38. public BackupApplication()
  39. {
  40. Dimension d = new Dimension(120, 20);
  41.  
  42. src = new JButton("Source");
  43. dest = new JButton("Destination");
  44. start = new JButton("Start");
  45.  
  46. src.setPreferredSize(d);
  47. dest.setPreferredSize(d);
  48. start.setPreferredSize(d);
  49.  
  50. src.addActionListener(this);
  51. dest.addActionListener(this);
  52. start.addActionListener(this);
  53.  
  54. srcText = new JTextField(25);
  55. destText = new JTextField(25);
  56. nameText = new JTextField(10);
  57. delayText = new JTextField(2);
  58.  
  59. statusText = new JLabel("Copying currently not active.");
  60. nameLabel = new JLabel("Backup Name:");
  61. delayLabel = new JLabel("Rate (in mins):");
  62.  
  63. d = new Dimension(320, 20);
  64.  
  65. srcText.setMaximumSize(d);
  66. destText.setMaximumSize(d);
  67.  
  68. statusText.setMaximumSize(d);
  69.  
  70. statusText.addPropertyChangeListener(this);
  71.  
  72. headHost = new JPanel();
  73. headHost.setLayout(new BoxLayout(headHost, BoxLayout.X_AXIS));
  74. srcHost = new JPanel();
  75. srcHost.setLayout(new BoxLayout(srcHost, BoxLayout.X_AXIS));
  76. destHost = new JPanel();
  77. destHost.setLayout(new BoxLayout(destHost, BoxLayout.X_AXIS));
  78. startHost = new JPanel();
  79. startHost.setLayout(new BoxLayout(startHost, BoxLayout.X_AXIS));
  80.  
  81. d = new Dimension(200, 30);
  82.  
  83. srcHost.setPreferredSize(d);
  84. destHost.setPreferredSize(d);
  85. startHost.setPreferredSize(d);
  86.  
  87. headHost.add(nameLabel);
  88. headHost.add(nameText);
  89. headHost.add(delayLabel);
  90. headHost.add(delayText);
  91. srcHost.add(srcText);
  92. srcHost.add(src);
  93. destHost.add(destText);
  94. destHost.add(dest);
  95. startHost.add(statusText);
  96. startHost.add(start);
  97.  
  98. add(headHost);
  99. add(srcHost);
  100. add(destHost);
  101. add(startHost);
  102. }
  103.  
  104. public BackupApplication(JLabel label)
  105. {
  106. statusText = label;
  107. }
  108.  
  109. public static void main(String[] args) throws IOException
  110. {
  111. JFrame frame = new JFrame("Backup Application");
  112. BackupApplication app = new BackupApplication();
  113. app.setLayout(new BoxLayout(app, BoxLayout.Y_AXIS));
  114. frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  115. frame.addWindowListener(new WindowAdapter()
  116. {
  117. public void windowClosing(WindowEvent e)
  118. {
  119. int q = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?");
  120. if (q == JOptionPane.OK_OPTION)
  121. System.exit(0);
  122. }
  123. });
  124. frame.getContentPane().add(app, "Center");
  125. frame.setSize(app.getPreferredSize());
  126. frame.setVisible(true);
  127. app.settings(true);
  128. }
  129.  
  130. public void settings(boolean isRead) throws IOException
  131. {
  132. data = new File("./backup.txt");
  133. data.createNewFile();
  134. JTextField[] fields = {nameText, delayText, srcText, destText};
  135. List<String> lines;
  136.  
  137. if(isRead)
  138. {
  139. lines = FileUtils.readLines(data);
  140. for(int i = 0; i < fields.length; i++)
  141. fields[i].setText(lines.get(i));
  142. }
  143. else
  144. {
  145. lines = new ArrayList<String>();
  146. for(int i = 0; i < fields.length; i++)
  147. lines.add(fields[i].getText());
  148. FileUtils.writeLines(data, lines);
  149. }
  150. }
  151.  
  152. @SuppressWarnings("deprecation")
  153. @Override
  154. public void actionPerformed(ActionEvent e)
  155. {
  156. if (e.getSource() == start)
  157. {
  158. if (t != null)
  159. {
  160. t.stop();
  161. t = null;
  162. start.setText("Start");
  163. return;
  164. }
  165. if (srcText.getText() != "" && destText.getText() != "" && srcText.getText() != destText.getText())
  166. {
  167. try
  168. {
  169. settings(false);
  170. }
  171. catch (IOException e1)
  172. {
  173. e1.printStackTrace();
  174. }
  175.  
  176. t = new Thread(new CopyWorker(nameText.getText(), Integer.parseInt(delayText.getText()) * 60000,
  177. srcText.getText(), destText.getText(), statusText));
  178. t.start();
  179. statusText.setText("Copying initialized...");
  180. start.setText("Stop");
  181. }
  182. else
  183. {
  184. JOptionPane.showMessageDialog(null, "Your paths are empty or invalid!");
  185. }
  186. return;
  187. }
  188. if (t != null)
  189. {
  190. t.stop();
  191. t = null;
  192. start.setText("Start");
  193. }
  194.  
  195. chooser = new JFileChooser();
  196. chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  197. chooser.setAcceptAllFileFilterUsed(false);
  198.  
  199. destArea = e.getSource() == src ? srcText : destText;
  200.  
  201. chooser.setCurrentDirectory(new File(destArea.getText() == "" ? "." : destArea.getText()));
  202.  
  203. chooser.setDialogTitle(String.format("Choose %s Location", destArea == srcText ? "Source" : "Destination"));
  204.  
  205. if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
  206. destArea.setText(chooser.getSelectedFile().toString());
  207.  
  208. }
  209.  
  210. public Dimension getPreferredSize()
  211. {
  212. return new Dimension(400, 150);
  213. }
  214.  
  215. @SuppressWarnings("deprecation")
  216. @Override
  217. public void propertyChange(PropertyChangeEvent evt)
  218. {
  219. if (evt.getSource() == statusText)
  220. {
  221. if (statusText.getText().equals("Error"))
  222. {
  223. start.setText("Start");
  224. if (t != null)
  225. {
  226. t.stop();
  227. t = null;
  228. }
  229. }
  230. }
  231. }
  232.  
  233. }
Add Comment
Please, Sign In to add comment