View Javadoc

1   package org.treetank.filelistener.ui.dialogs;
2   
3   import java.io.File;
4   import java.io.FileNotFoundException;
5   import java.io.IOException;
6   
7   import org.eclipse.swt.SWT;
8   import org.eclipse.swt.events.SelectionAdapter;
9   import org.eclipse.swt.events.SelectionEvent;
10  import org.eclipse.swt.widgets.Button;
11  import org.eclipse.swt.widgets.Combo;
12  import org.eclipse.swt.widgets.Dialog;
13  import org.eclipse.swt.widgets.DirectoryDialog;
14  import org.eclipse.swt.widgets.Display;
15  import org.eclipse.swt.widgets.Label;
16  import org.eclipse.swt.widgets.Shell;
17  import org.eclipse.swt.widgets.Text;
18  import org.eclipse.wb.swt.SWTResourceManager;
19  import org.treetank.api.IFilelistenerReadTrx;
20  import org.treetank.exception.TTIOException;
21  import org.treetank.filelistener.file.Filelistener;
22  
23  import com.google.common.io.Files;
24  
25  public class RestoreDialog extends Dialog {
26  
27      protected Object mResult;
28      protected Shell mShell;
29      private Combo mCombo;
30      private Label mLblChooseConfigurationFrom;
31      private Label mLblFolder;
32      private Text mTextFolder;
33      private Button mBtnFolder;
34      private Button mBtnSubmit;
35      private Button mBtnCancel;
36  
37      private Filelistener mListener;
38  
39      private String mListenFolder;
40  
41      /**
42       * Create the dialog.
43       * 
44       * @param parent
45       * @param style
46       * @param mListener
47       */
48      public RestoreDialog(Shell parent, int style, Filelistener pListener) {
49          super(parent, style);
50          mListener = pListener;
51          setText("Restore into new folder");
52      }
53  
54      /**
55       * Open the dialog.
56       * 
57       * @return the result
58       */
59      public Object open() {
60          createContents();
61          mShell.open();
62          mShell.layout();
63          Display display = getParent().getDisplay();
64          while (!mShell.isDisposed()) {
65              if (!display.readAndDispatch()) {
66                  display.sleep();
67              }
68          }
69          return mResult;
70      }
71  
72      /**
73       * Create contents of the dialog.
74       */
75      private void createContents() {
76          mShell = new Shell(getParent(), getStyle());
77          mShell.setSize(448, 273);
78          mShell.setText(getText());
79  
80          mCombo = new Combo(mShell, SWT.NONE);
81          mCombo.setBounds(10, 133, 430, 29);
82  
83          try {
84              for (String s : Filelistener.getFilelisteners().keySet()) {
85                  mCombo.add(s);
86              }
87          } catch (FileNotFoundException e1) {
88              // TODO Auto-generated catch block
89              e1.printStackTrace();
90          } catch (ClassNotFoundException e1) {
91              // TODO Auto-generated catch block
92              e1.printStackTrace();
93          } catch (IOException e1) {
94              // TODO Auto-generated catch block
95              e1.printStackTrace();
96          }
97  
98          mLblChooseConfigurationFrom = new Label(mShell, SWT.NONE);
99          mLblChooseConfigurationFrom.setBounds(10, 104, 430, 17);
100         mLblChooseConfigurationFrom.setText("Choose configuration from below");
101 
102         mLblFolder = new Label(mShell, SWT.NONE);
103         mLblFolder.setText("Choose an empty folder:");
104         mLblFolder.setBounds(10, 20, 430, 17);
105 
106         mTextFolder = new Text(mShell, SWT.BORDER);
107         mTextFolder.setBounds(10, 54, 372, 27);
108 
109         mBtnFolder = new Button(mShell, SWT.NONE);
110         mBtnFolder.addSelectionListener(new SelectionAdapter() {
111             @Override
112             public void widgetSelected(final SelectionEvent e) {
113                 do_mBtnFolder_widgetSelected(e);
114             }
115         });
116         mBtnFolder.setImage(SWTResourceManager.getImage(RestoreDialog.class,
117             "/com/sun/java/swing/plaf/gtk/icons/Directory.gif"));
118         mBtnFolder.setBounds(388, 54, 52, 29);
119 
120         mBtnSubmit = new Button(mShell, SWT.NONE);
121         mBtnSubmit.addSelectionListener(new SelectionAdapter() {
122             @Override
123             public void widgetSelected(final SelectionEvent e) {
124                 do_mBtnSubmit_widgetSelected(e);
125             }
126         });
127         mBtnSubmit.setBounds(248, 185, 91, 29);
128         mBtnSubmit.setText("Submit");
129 
130         mBtnCancel = new Button(mShell, SWT.NONE);
131         mBtnCancel.addSelectionListener(new SelectionAdapter() {
132             @Override
133             public void widgetSelected(final SelectionEvent e) {
134                 do_btnCancel_widgetSelected(e);
135             }
136         });
137         mBtnCancel.setBounds(345, 185, 91, 29);
138         mBtnCancel.setText("Cancel");
139 
140     }
141 
142     protected void do_btnCancel_widgetSelected(final SelectionEvent e) {
143         getParent().dispose();
144     }
145 
146     protected void do_mBtnFolder_widgetSelected(final SelectionEvent e) {
147         DirectoryDialog dialog = new DirectoryDialog(mShell);
148         dialog.setText("Choose a folder");
149         String homefolder = System.getProperty("user.home");
150         dialog.setFilterPath(homefolder);
151 
152         dialog.open();
153 
154         mTextFolder.setText(dialog.getFilterPath());
155         this.mListenFolder = mTextFolder.getText();
156     }
157 
158     protected void do_mBtnSubmit_widgetSelected(final SelectionEvent e) {
159         File f = new File(this.mListenFolder);
160 
161         IFilelistenerReadTrx trx = mListener.getTrx(mCombo.getText());
162 
163         if (!f.exists()) {
164             // TODO: Errordialog no folder..
165         }
166 
167         if (f.isDirectory() && f.listFiles().length == 0) {
168             if (trx != null) {
169                 String[] filePaths = trx.getFilePaths();
170 
171                 for (String s : filePaths) {
172                     System.out.println("Restoring file: " + s);
173                     try {
174                         File file = trx.getFullFile(s);
175 
176                         Files.copy(file, new File(new StringBuilder().append(this.mListenFolder).append(s)
177                             .toString()));
178 
179                     } catch (TTIOException | IOException e1) {
180                         // TODO: ErrorDialog for this file..
181                         e1.printStackTrace();
182                     }
183                 }
184                 
185                 mShell.dispose();
186                 
187             }
188         } else {
189             // TODO: Not directory OR not empty
190         }
191 
192     }
193 }