1 package com.quantum.wizards;
4 import java.io.FileWriter;
5 import java.io.IOException;
7 import javax.xml.parsers.ParserConfigurationException;
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;
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;
43 public class ExportBookmarkWizardPage extends WizardPage {
45 public class ContentProvider implements ITreeContentProvider {
47 public Object[] getChildren(Object parentElement) {
48 if (parentElement instanceof BookmarkCollection) {
49 return ((BookmarkCollection) parentElement).getBookmarks();
54 public Object getParent(Object element) {
55 if (element instanceof Bookmark) {
56 return BookmarkCollection.getInstance();
61 public boolean hasChildren(Object element) {
62 if (element instanceof BookmarkCollection) {
63 return ((BookmarkCollection) element).getBookmarks().length > 0;
68 public Object[] getElements(Object inputElement) {
69 return getChildren(inputElement);
71 public void dispose() {
73 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
77 public class LabelProvider implements ILabelProvider {
79 public Image getImage(Object element) {
80 if (element instanceof Bookmark) {
81 return ImageStore.getImage(ImageStore.BOOKMARK);
87 public String getText(Object element) {
89 if (element instanceof Bookmark) {
90 return ((Bookmark) element).getName();
92 return element.toString();
96 public void addListener(ILabelProviderListener listener) {
99 public void dispose() {
102 public boolean isLabelProperty(Object element, String property) {
106 public void removeListener(ILabelProviderListener listener) {
110 private CheckboxTreeViewer treeViewer;
111 private Text fileNameText;
113 private boolean sourceIsSelected = false;
114 private boolean destinationIsSelected = false;
119 protected ExportBookmarkWizardPage() {
121 setTitle(Messages.getString(getClass(), "title"));
124 public void createControl(Composite pageContainer) {
125 this.sourceIsSelected = false;
126 this.destinationIsSelected = false;
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));
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());
140 this.treeViewer.addCheckStateListener(new ICheckStateListener() {
141 public void checkStateChanged(CheckStateChangedEvent event) {
143 ExportBookmarkWizardPage.this.treeViewer.getCheckedElements().length > 0);
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);
155 Composite buttons = new Composite(composite, SWT.NULL);
156 buttons.setLayout(new GridLayout(2, false));
157 buttons.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
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) {
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) {
175 createDestinationArea(composite);
177 setControl(composite);
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:");
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);
200 Button button = new Button(fileArea, SWT.NONE);
201 button.setText("Browse");
202 button.addSelectionListener(new SelectionAdapter() {
203 public void widgetSelected(SelectionEvent event) {
209 private void setSourceIsSelected(boolean selected) {
210 this.sourceIsSelected = selected;
211 setPageComplete(selected & this.destinationIsSelected);
214 private void setDestinationIsSelected(boolean selected) {
215 this.destinationIsSelected = selected;
216 setPageComplete(selected & this.sourceIsSelected);
219 protected void promptForFile() {
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);
229 protected void deselectAll() {
230 this.treeViewer.setCheckedElements(new Object[0]);
231 setSourceIsSelected(false);
234 protected void selectAll() {
235 Bookmark[] bookmarks = BookmarkCollection.getInstance().getBookmarks();
236 this.treeViewer.setCheckedElements(bookmarks);
237 setSourceIsSelected(bookmarks.length > 0);
240 public boolean finish() {
242 String fileName = this.fileNameText.getText();
243 File file = new File(fileName);
245 // prompt for overwrite
246 } else if (!file.getParentFile().exists()) {
251 Object[] bookmarks = this.treeViewer.getCheckedElements();
252 Document document = XMLHelper.createEmptyDocument();
253 ModelToXMLConverter.getInstance().createRoot(document);
255 for (int i = 0, length = (bookmarks == null) ? 0 : bookmarks.length;
258 ModelToXMLConverter.getInstance().convert(
259 document.getDocumentElement(), (Bookmark) bookmarks[i]);
262 FileWriter writer = new FileWriter(file);
264 XMLHelper.write(writer, document);
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);