*** empty log message ***
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / ExportBookmarkWizardPage.java
1 package com.quantum.wizards;
2
3 import java.io.File;
4 import java.io.FileWriter;
5 import java.io.IOException;
6
7 import javax.xml.parsers.ParserConfigurationException;
8
9 import com.quantum.ImageStore;
10 import com.quantum.Messages;
11 import com.quantum.model.Bookmark;
12 import com.quantum.model.BookmarkCollection;
13 import com.quantum.model.xml.ModelToXMLConverter;
14 import com.quantum.ui.dialog.ExceptionDisplayDialog;
15 import com.quantum.util.xml.XMLHelper;
16
17 import org.eclipse.jface.viewers.CheckStateChangedEvent;
18 import org.eclipse.jface.viewers.CheckboxTreeViewer;
19 import org.eclipse.jface.viewers.ICheckStateListener;
20 import org.eclipse.jface.viewers.ILabelProvider;
21 import org.eclipse.jface.viewers.ILabelProviderListener;
22 import org.eclipse.jface.viewers.ITreeContentProvider;
23 import org.eclipse.jface.viewers.Viewer;
24 import org.eclipse.jface.wizard.WizardPage;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.ModifyEvent;
27 import org.eclipse.swt.events.ModifyListener;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.FileDialog;
36 import org.eclipse.swt.widgets.Label;
37 import org.eclipse.swt.widgets.Text;
38 import org.w3c.dom.Document;
39
40 /**
41  * @author BC
42  */
43 public class ExportBookmarkWizardPage extends WizardPage {
44     
45     public class ContentProvider implements ITreeContentProvider {
46
47         public Object[] getChildren(Object parentElement) {
48             if (parentElement instanceof BookmarkCollection) {
49                 return ((BookmarkCollection) parentElement).getBookmarks();
50             } else {
51                 return new Object[0];
52             }
53         }
54         public Object getParent(Object element) {
55             if (element instanceof Bookmark) {
56                 return BookmarkCollection.getInstance();
57             } else {
58                 return null;
59             }
60         }
61         public boolean hasChildren(Object element) {
62             if (element instanceof BookmarkCollection) {
63                 return ((BookmarkCollection) element).getBookmarks().length > 0;
64             } else {
65                 return false;
66             }
67         }
68         public Object[] getElements(Object inputElement) {
69             return getChildren(inputElement);
70         }
71         public void dispose() {
72         }
73         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
74         }
75     }
76     
77     public class LabelProvider implements ILabelProvider {
78
79         public Image getImage(Object element) {
80             if (element instanceof Bookmark) {
81                 return ImageStore.getImage(ImageStore.BOOKMARK);
82             } else {
83                 return null;
84             }
85         }
86
87         public String getText(Object element) {
88             
89             if (element instanceof Bookmark) {
90                 return ((Bookmark) element).getName();
91             } else {
92                 return element.toString();
93             }
94         }
95
96         public void addListener(ILabelProviderListener listener) {
97         }
98
99         public void dispose() {
100         }
101
102         public boolean isLabelProperty(Object element, String property) {
103             return false;
104         }
105
106         public void removeListener(ILabelProviderListener listener) {
107         }
108     }
109     
110     private CheckboxTreeViewer treeViewer;
111     private Text fileNameText;
112     
113     private boolean sourceIsSelected = false;
114     private boolean destinationIsSelected = false;
115
116     /**
117      * @param pageName
118      */
119     protected ExportBookmarkWizardPage() {
120         super("page1");
121         setTitle(Messages.getString(getClass(), "title"));
122     }
123
124     public void createControl(Composite pageContainer) {
125         this.sourceIsSelected = false;
126         this.destinationIsSelected = false;
127         
128         Composite composite = new Composite(pageContainer, SWT.NULL);
129         composite.setLayout(new GridLayout());
130         composite.setLayoutData(
131             new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
132
133         this.treeViewer = new CheckboxTreeViewer(composite, 
134             SWT.CHECK | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
135         this.treeViewer.setContentProvider(new ContentProvider());
136         this.treeViewer.setLabelProvider(new LabelProvider());
137         this.treeViewer.setInput(BookmarkCollection.getInstance());
138         selectAll();
139         
140         this.treeViewer.addCheckStateListener(new ICheckStateListener() {
141             public void checkStateChanged(CheckStateChangedEvent event) {
142                 setSourceIsSelected(
143                     ExportBookmarkWizardPage.this.treeViewer.getCheckedElements().length > 0);
144             }
145         });
146
147         GridData data = new GridData();
148         data.horizontalAlignment = GridData.HORIZONTAL_ALIGN_FILL;
149         data.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
150         data.grabExcessHorizontalSpace = true;
151         data.heightHint = 200;
152         data.widthHint = 400;
153         this.treeViewer.getControl().setLayoutData(data);
154
155         Composite buttons = new Composite(composite, SWT.NULL);
156         buttons.setLayout(new GridLayout(2, false));
157         buttons.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
158         
159         Button selectAll = new Button(buttons, SWT.NONE);
160         selectAll.setText(Messages.getString(getClass(), "selectAll"));
161         selectAll.addSelectionListener(new SelectionAdapter() {
162             public void widgetSelected(SelectionEvent event) {
163                 selectAll();
164             }
165         });
166
167         Button deselectAll = new Button(buttons, SWT.NONE);
168         deselectAll.setText(Messages.getString(getClass(), "deselectAll"));
169         deselectAll.addSelectionListener(new SelectionAdapter() {
170             public void widgetSelected(SelectionEvent event) {
171                 deselectAll();
172             }
173         });
174         
175         createDestinationArea(composite);
176
177         setControl(composite);
178     }
179
180     private void createDestinationArea(Composite composite) {
181         Composite fileArea = new Composite(composite, SWT.NULL);
182         fileArea.setLayout(new GridLayout(3, false));
183         fileArea.setLayoutData(
184             new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
185         Label label = new Label(fileArea, SWT.NONE);
186         label.setText("File name:");
187         
188         this.fileNameText = new Text(fileArea, SWT.BORDER);
189         GridData data = new GridData();
190         data.horizontalAlignment = GridData.HORIZONTAL_ALIGN_FILL;
191         data.widthHint = 300;
192         this.fileNameText.setLayoutData(data);
193         this.fileNameText.addModifyListener(new ModifyListener() {
194             public void modifyText(ModifyEvent event) {
195                 String text = ((Text) event.getSource()).getText();
196                 setDestinationIsSelected(text != null && text.trim().length() > 0);
197             }
198         });
199         
200         Button button = new Button(fileArea, SWT.NONE);
201         button.setText("Browse");
202         button.addSelectionListener(new SelectionAdapter() {
203             public void widgetSelected(SelectionEvent event) {
204                 promptForFile();
205             }
206         });
207     }
208     
209     private void setSourceIsSelected(boolean selected) {
210         this.sourceIsSelected = selected;
211         setPageComplete(selected & this.destinationIsSelected);
212     }
213     
214     private void setDestinationIsSelected(boolean selected) {
215         this.destinationIsSelected = selected;
216         setPageComplete(selected & this.sourceIsSelected);
217     }
218     
219     protected void promptForFile() {
220     
221         FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
222         dialog.setFilterExtensions(new String[] { "xml" });
223         dialog.setFilterNames(new String[] { "XML Files (*.xml)"});
224         String filename = dialog.open();
225         if (filename != null) {
226             this.fileNameText.setText(filename);
227         }
228     }
229     protected void deselectAll() {
230         this.treeViewer.setCheckedElements(new Object[0]);
231         setSourceIsSelected(false);
232     }
233
234     protected void selectAll() {
235         Bookmark[] bookmarks = BookmarkCollection.getInstance().getBookmarks();
236         this.treeViewer.setCheckedElements(bookmarks);
237         setSourceIsSelected(bookmarks.length > 0);
238     }
239     
240     public boolean finish() {
241         
242         String fileName = this.fileNameText.getText();
243         File file = new File(fileName);
244         if (file.exists()) {
245             // prompt for overwrite
246         } else if (!file.getParentFile().exists()) {
247             // do what?
248         }
249         
250         try {
251             Object[] bookmarks = this.treeViewer.getCheckedElements();
252             Document document = XMLHelper.createEmptyDocument();
253             ModelToXMLConverter.getInstance().createRoot(document);
254                 
255             for (int i = 0, length = (bookmarks == null) ? 0 : bookmarks.length;
256                 i < length;
257                 i++) {
258                 ModelToXMLConverter.getInstance().convert(
259                     document.getDocumentElement(), (Bookmark) bookmarks[i]);
260             }
261
262             FileWriter writer = new FileWriter(file);
263             try {
264                 XMLHelper.write(writer, document);
265             } finally {
266                 writer.close();
267             }
268         } catch (IOException e) {
269             ExceptionDisplayDialog.openError(getShell(), 
270                 Messages.getString(getClass(), "error.IOException.title"), 
271                 Messages.getString(getClass(), "error.IOException.message", 
272                     new Object[] { fileName }), e);
273         } catch (ParserConfigurationException e) {
274             ExceptionDisplayDialog.openError(getShell(), 
275                 Messages.getString(getClass(), "error.IOException.title"), 
276                 Messages.getString(getClass(), "error.IOException.message", 
277                     new Object[] { fileName }), e);
278         }
279         
280         return true;
281     }
282 }