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.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;
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 QuantumPlugin.getImage("bookmarks.gif");
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) {
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:");
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);
201 Button button = new Button(fileArea, SWT.NONE);
202 button.setText("Browse");
203 button.addSelectionListener(new SelectionAdapter() {
204 public void widgetSelected(SelectionEvent event) {
210 private void setSourceIsSelected(boolean selected) {
211 this.sourceIsSelected = selected;
212 setPageComplete(selected & this.destinationIsSelected);
215 private void setDestinationIsSelected(boolean selected) {
216 this.destinationIsSelected = selected;
217 setPageComplete(selected & this.sourceIsSelected);
220 protected void promptForFile() {
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);
230 protected void deselectAll() {
231 this.treeViewer.setCheckedElements(new Object[0]);
232 setSourceIsSelected(false);
235 protected void selectAll() {
236 Bookmark[] bookmarks = BookmarkCollection.getInstance().getBookmarks();
237 this.treeViewer.setCheckedElements(bookmarks);
238 setSourceIsSelected(bookmarks.length > 0);
241 public boolean finish() {
243 String fileName = this.fileNameText.getText();
244 File file = new File(fileName);
246 // prompt for overwrite
247 } else if (!file.getParentFile().exists()) {
252 Object[] bookmarks = this.treeViewer.getCheckedElements();
253 Document document = XMLHelper.createEmptyDocument();
254 ModelToXMLConverter.getInstance().createRoot(document);
256 for (int i = 0, length = (bookmarks == null) ? 0 : bookmarks.length;
259 ModelToXMLConverter.getInstance().convert(
260 document.getDocumentElement(), (Bookmark) bookmarks[i]);
263 FileWriter writer = new FileWriter(file);
265 XMLHelper.write(writer, document);
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);