package net.sourceforge.phpdt.sql.actions; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.apache.crimson.tree.XmlDocument; import org.eclipse.jface.action.IAction; import org.eclipse.jface.viewers.ISelection; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.ui.IViewActionDelegate; import org.eclipse.ui.IViewPart; import org.w3c.dom.Element; import net.sourceforge.phpdt.sql.Messages; import net.sourceforge.phpdt.sql.view.BookmarkView; import net.sourceforge.phpdt.sql.view.bookmark.BookmarkContentProvider; /** * @author root * Asks the user for a file name and saves there the bookmarks in xml format */ public class ExportBookmarksAction implements IViewActionDelegate { BookmarkView view; FileDialog dialog; /** * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart) */ public void init(IViewPart view) { this.view = (BookmarkView) view; dialog = new FileDialog(view.getSite().getShell(), SWT.SAVE); dialog.setFilterExtensions(new String[]{"*.xml","*.*"}); //$NON-NLS-1$ //$NON-NLS-2$ dialog.setFilterNames(new String[]{Messages.getString("filedialog.xmlFiles"),Messages.getString("filedialog.xmlFiles")}); //$NON-NLS-1$ //$NON-NLS-2$ } /** * @see org.eclipse.ui.IActionDelegate#run(IAction) */ public void run(IAction action) { String filename = dialog.open(); if (filename != null) { /*Check for the presence of a "." - either indicates an * extension has been provided or that a filename with a '.' * has been specified - if the latter, it is assumed the user * knows what they're doing - could be dangerous! :-) */ if (filename.indexOf(".") ==0) filename += ".xml"; File target = new File(filename); XmlDocument doc = new XmlDocument(); FileOutputStream out = null; try { out = new FileOutputStream(target); } catch (FileNotFoundException e1) { e1.printStackTrace(); return; } Element root = (Element) doc.appendChild(doc.createElement(Messages.getString("ExportXMLAction.SavedData"))); //$NON-NLS-1$ BookmarkContentProvider.getInstance().exportXML(root); try { doc.write(out); out.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection) */ public void selectionChanged(IAction action, ISelection selection) { } }