3feed8fda8700554bb32baf9f4f95660054e4784
[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.Messages;
10 import com.quantum.QuantumPlugin;
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 QuantumPlugin.getImage("bookmarks.gif");
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         GridData data;
182         Composite fileArea = new Composite(composite, SWT.NULL);
183         fileArea.setLayout(new GridLayout(3, false));
184         fileArea.setLayoutData(
185             new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
186         Label label = new Label(fileArea, SWT.NONE);
187         label.setText("File name:");
188         
189         this.fileNameText = new Text(fileArea, SWT.BORDER);
190         data = new GridData();
191         data.horizontalAlignment = GridData.HORIZONTAL_ALIGN_FILL;
192         data.widthHint = 300;
193         this.fileNameText.setLayoutData(data);
194         this.fileNameText.addModifyListener(new ModifyListener() {
195             public void modifyText(ModifyEvent event) {
196                 String text = ((Text) event.getSource()).getText();
197                 setDestinationIsSelected(text != null && text.trim().length() > 0);
198             }
199         });
200         
201         Button button = new Button(fileArea, SWT.NONE);
202         button.setText("Browse");
203         button.addSelectionListener(new SelectionAdapter() {
204             public void widgetSelected(SelectionEvent event) {
205                 promptForFile();
206             }
207         });
208     }
209     
210     private void setSourceIsSelected(boolean selected) {
211         this.sourceIsSelected = selected;
212         setPageComplete(selected & this.destinationIsSelected);
213     }
214     
215     private void setDestinationIsSelected(boolean selected) {
216         this.destinationIsSelected = selected;
217         setPageComplete(selected & this.sourceIsSelected);
218     }
219     
220     protected void promptForFile() {
221     
222         FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
223         dialog.setFilterExtensions(new String[] { "xml" });
224         dialog.setFilterNames(new String[] { "XML Files (*.xml)"});
225         String filename = dialog.open();
226         if (filename != null) {
227             this.fileNameText.setText(filename);
228         }
229     }
230     protected void deselectAll() {
231         this.treeViewer.setCheckedElements(new Object[0]);
232         setSourceIsSelected(false);
233     }
234
235     protected void selectAll() {
236         Bookmark[] bookmarks = BookmarkCollection.getInstance().getBookmarks();
237         this.treeViewer.setCheckedElements(bookmarks);
238         setSourceIsSelected(bookmarks.length > 0);
239     }
240     
241     public boolean finish() {
242         
243         String fileName = this.fileNameText.getText();
244         File file = new File(fileName);
245         if (file.exists()) {
246             // prompt for overwrite
247         } else if (!file.getParentFile().exists()) {
248             // do what?
249         }
250         
251         try {
252             Object[] bookmarks = this.treeViewer.getCheckedElements();
253             Document document = XMLHelper.createEmptyDocument();
254             ModelToXMLConverter.getInstance().createRoot(document);
255                 
256             for (int i = 0, length = (bookmarks == null) ? 0 : bookmarks.length;
257                 i < length;
258                 i++) {
259                 ModelToXMLConverter.getInstance().convert(
260                     document.getDocumentElement(), (Bookmark) bookmarks[i]);
261             }
262
263             FileWriter writer = new FileWriter(file);
264             try {
265                 XMLHelper.write(writer, document);
266             } finally {
267                 writer.close();
268             }
269         } catch (IOException e) {
270             ExceptionDisplayDialog.openError(getShell(), 
271                 Messages.getString(getClass(), "error.IOException.title"), 
272                 Messages.getString(getClass(), "error.IOException.message", 
273                     new Object[] { fileName }), e);
274         } catch (ParserConfigurationException e) {
275             ExceptionDisplayDialog.openError(getShell(), 
276                 Messages.getString(getClass(), "error.IOException.title"), 
277                 Messages.getString(getClass(), "error.IOException.message", 
278                     new Object[] { fileName }), e);
279         }
280         
281         return true;
282     }
283 }