Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: reeb/lazysync/LazySyncWin.java


1   // LazySyncClient.java
2   package reeb.lazysync;
3   
4   import java.awt.*;
5   import java.awt.event.*;
6   import java.net.*;
7   import java.io.*;
8   import java.util.*;
9   import javax.swing.*;
10  
11  /** The SavaJe Java LazySync client */
12  public class LazySyncWin extends JFrame implements ActionListener
13  {
14    /** The text field */
15    private JTextArea logText;
16    
17    /** The ip text field */
18    private JTextField ipText;
19    
20    /** The syncdir field */
21    private JTextField dirText;
22    
23    /** The LazySync thread */
24    private LazySyncThread thread;
25    
26    /** The printwriter for the text area */
27    private PrintWriter console;
28    
29    /** The sync menu item */
30    private JMenuItem syncItem;
31    
32    /** The sync button */
33    private JButton syncButt;
34    
35    /** The timer checks for task completion */
36    private javax.swing.Timer timer;
37    
38    /** Constructor */
39    public LazySyncWin()
40    {
41      JMenuBar menuBar;
42      JMenu menu;
43      JMenuItem menuItem;
44      JScrollPane scroller;
45      JPanel topPanel, bottomPanel;
46      
47      // Setup window
48      setTitle("LazySync 0.1");
49      setSize(240,240);
50      setLocation(0,0);
51      getContentPane().setLayout(new BorderLayout());
52      
53      // Setup menus
54      menuBar=new JMenuBar();
55      setJMenuBar(menuBar);
56      
57      // Setup file menu
58      menu=new JMenu("File");
59      menuBar.add(menu);
60      
61      // Add Synchronize item
62      syncItem=new JMenuItem("Synchronize");
63      syncItem.setActionCommand("sync");
64      syncItem.addActionListener(this);
65      menu.add(syncItem);
66      
67      // Add separator
68      menu.addSeparator();
69      
70      // Add Exit item
71      menuItem=new JMenuItem("Exit");
72      menuItem.setActionCommand("exit");
73      menuItem.addActionListener(this);
74      menu.add(menuItem);
75      
76      // Setup help menu
77      menu=new JMenu("Help");
78      menuBar.add(menu);
79      
80      // Add About item
81      menuItem=new JMenuItem("About");
82      menuItem.setActionCommand("about");
83      menuItem.addActionListener(this);
84      menu.add(menuItem);
85      
86      // Create the top panel
87      topPanel=new JPanel();
88      topPanel.setLayout(new GridBagLayout());
89      getContentPane().add(topPanel,BorderLayout.NORTH);
90      
91      // Create the button
92      syncButt=new JButton(getImageFromJAR("reeb/lazysync/afraid.gif"));
93      syncButt.setActionCommand("sync");
94      syncButt.addActionListener(this);
95      syncButt.setMinimumSize(new Dimension(35,syncButt.getMinimumSize().height));
96      syncButt.setPreferredSize(new Dimension(35,syncButt.getPreferredSize().height));
97      syncButt.setMaximumSize(new Dimension(35,syncButt.getMaximumSize().height));
98      syncButt.setToolTipText("Synchronize!");
99      
100     // Add the button
101     topPanel.add(syncButt,new GridBagConstraints(0,0,1,2,0,0,GridBagConstraints.NORTHWEST,GridBagConstraints.NONE,new Insets(5,5,0,0),0,0));
102     
103     // Add the label and text field
104     topPanel.add(new JLabel("Server Ip:"),new GridBagConstraints(1,0,1,1,0,0,GridBagConstraints.NORTHWEST,GridBagConstraints.NONE,new Insets(5,5,0,0),0,0));
105     ipText=new JTextField();
106     ipText.setFont(new Font(ipText.getFont().getName(),ipText.getFont().getStyle(),10));
107     topPanel.add(ipText,new GridBagConstraints(2,0,1,1,0,0,GridBagConstraints.NORTHWEST,GridBagConstraints.HORIZONTAL,new Insets(5,5,0,5),0,0));
108     
109     // Add the label and text field
110     topPanel.add(new JLabel("Root Dir:"),new GridBagConstraints(1,1,1,1,0,0,GridBagConstraints.NORTHWEST,GridBagConstraints.NONE,new Insets(5,5,5,0),0,0));
111     dirText=new JTextField();
112     dirText.setFont(new Font(dirText.getFont().getName(),dirText.getFont().getStyle(),10));
113     topPanel.add(dirText,new GridBagConstraints(2,1,1,1,1,1,GridBagConstraints.NORTHWEST,GridBagConstraints.HORIZONTAL,new Insets(5,5,5,5),0,0));
114 
115     // Create the bottom panel
116     bottomPanel=new JPanel();
117     bottomPanel.setLayout(new BorderLayout());
118     getContentPane().add(bottomPanel,BorderLayout.CENTER);
119     
120     // Create the scroller
121     scroller=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
122     bottomPanel.add(scroller,BorderLayout.CENTER);
123     
124     // Create the text pane
125     logText=new JTextArea();
126     logText.setFont(new Font(logText.getFont().getName(),logText.getFont().getStyle(),10));
127     logText.setEditable(false);
128     scroller.setViewportView(logText);
129     
130     // Get the settings
131     readProperties();
132     
133     // Create the writer
134     console=new PrintWriter(new TextAreaWriter(logText));
135     
136     // Create the timer
137     timer=new javax.swing.Timer(1000,this);
138   }
139   
140   /** Writes out the properties */
141   private void writeProperties()
142   {
143     File propsFile;
144     Properties clientSettings;
145     FileOutputStream fos;
146     
147     // Catch IO exceptions
148     try
149     {
150       // Get the properties file
151       propsFile=new File(System.getProperty("user.home"),"lazysync.properties");
152     
153       // Create the file output stream
154       fos=new FileOutputStream(propsFile);
155     
156       // Create the properties object
157       clientSettings=new Properties();
158     
159       // Set the items
160       clientSettings.setProperty("serverip",ipText.getText());
161       clientSettings.setProperty("syncdir",dirText.getText());
162     
163       // Write it out
164       clientSettings.store(fos,"LazySync Settings File");
165     
166       // Close the stream
167       fos.close();
168     }
169     catch (IOException e)
170     {
171       // Report
172       e.printStackTrace();
173     }
174   }
175   
176   /** Reads in the properties */
177   private void readProperties()
178   {
179     File propsFile;
180     Properties clientSettings;
181     FileInputStream fis;
182     
183     // Get the properties file
184     propsFile=new File(System.getProperty("user.home"),"lazysync.properties");
185     
186     // Check to see if the properties file exists
187     if (propsFile.exists())
188     {
189       // Catch exceptions
190       try
191       {
192         // Get the file input stream
193         fis=new FileInputStream(propsFile);
194       
195         // Create the properties object
196         clientSettings=new Properties();
197       
198         // Load them in
199         clientSettings.load(fis);
200       
201         // Close the file
202         fis.close();
203       
204         // Set the text fields
205         ipText.setText(clientSettings.getProperty("serverip"));
206         dirText.setText(clientSettings.getProperty("syncdir"));
207       }
208       catch (IOException e)
209       {
210         // Simply dump to console
211         e.printStackTrace();
212       }
213     }
214   }
215   
216   /** Returns an ImageIcon from the main jar */
217   private ImageIcon getImageFromJAR(String imageName)
218   {
219     ClassLoader classLoader;
220     Image image;
221     ImageIcon imageIcon;
222     URL imageURL;
223     
224     // Get the class loader
225     classLoader=imageName.getClass().getClassLoader();
226     
227     // If system
228     if (classLoader==null)
229       // Set system
230       classLoader=ClassLoader.getSystemClassLoader();
231     
232     // Get the URL
233     imageURL=classLoader.getResource(imageName);
234     
235     // Get the image
236     image=Toolkit.getDefaultToolkit().getImage(imageURL);
237     
238     // Create the image icon
239     imageIcon=new ImageIcon(image);
240     
241     // Return it
242     return imageIcon;
243   }
244   
245   /** The action handler */
246   public void actionPerformed(ActionEvent event)
247   {
248     // Check timer
249     if (event.getSource()==timer)
250       // Check thread
251       checkSyncThread();
252     else if (event.getActionCommand().equals("about"))
253       // Show about
254       JOptionPane.showMessageDialog(this,"LazySync v0.1\n\nBy James Bray\n\nwww.reeb.freeserve.co.uk","About",JOptionPane.INFORMATION_MESSAGE);
255     else if (event.getActionCommand().equals("exit"))
256       // Exit
257       exit();
258     else if (event.getActionCommand().equals("sync"))
259       // Sync up
260       sync();
261   }
262   
263   /** Checks the sync thread for completion */
264   private void checkSyncThread()
265   {
266     // Check it
267     if ((thread!=null) && (!thread.isAlive()))
268     {
269       // Enable GUI
270       ipText.setEnabled(true);
271       dirText.setEnabled(true);
272       syncItem.setText("Synchronize");
273       syncButt.setToolTipText("Synchronize!");
274       
275       // Stop timer
276       timer.stop();
277     }
278   }
279   
280   /** Exits out */
281   public void exit()
282   {
283     // Check thread
284     if ((thread!=null) && (thread.isAlive()))
285     {
286       // Log
287       console.println("Aborting...");
288       console.flush();
289       
290       // Request stop
291       thread.stopThread();
292       
293       // Catch exception
294       try
295       {
296         // Wait for it
297         thread.join();
298       }
299       catch (InterruptedException e)
300       {
301       }
302     }
303     
304     // Save settings
305     writeProperties();
306       
307     // And exit
308     System.exit(0);
309   }
310   
311   /** Performs the sync operation */
312   public void sync()
313   {
314     TextAreaWriter logWriter;
315       
316     // Check thread
317     if ((thread!=null) && (thread.isAlive()))
318     {
319       // Log
320       console.println("Aborting...");
321       console.flush();
322       
323       // Request stop
324       thread.stopThread();
325       
326       // Catch exception
327       try
328       {
329         // Wait for it
330         thread.join();
331       }
332       catch (InterruptedException e)
333       {
334       }
335 
336       // Disable GUI
337       ipText.setEnabled(true);
338       dirText.setEnabled(true);
339       syncItem.setText("Synchronize");
340       syncButt.setToolTipText("Synchronize!");
341     }
342     else
343     {
344       // Clear the text field
345       console=new PrintWriter(new TextAreaWriter(logText));
346 
347       // Create the thread
348       thread=new LazySyncThread(console,ipText.getText(),dirText.getText());
349       
350       // Enable GUI
351       ipText.setEnabled(false);
352       dirText.setEnabled(false);
353       syncItem.setText("Stop Sync");
354       syncButt.setToolTipText("Stop Sync");
355           
356       // Start the thread
357       thread.start();
358       
359       // Start the timer
360       timer.start();
361     }
362   }
363 
364   /** Main entry point */
365   public static void main(String args[])
366   {
367     // Display the window
368     new LazySyncWin().setVisible(true);
369   }
370   
371 }