package net.sourceforge.phpdt.sql.actions; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; 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 org.xml.sax.SAXException; import net.sourceforge.phpdt.sql.Messages; import net.sourceforge.phpdt.sql.view.BookmarkView; import net.sourceforge.phpdt.sql.view.bookmark.BookmarkContentProvider; /** * @author root * * To change this generated comment edit the template variable "typecomment": * Window>Preferences>Java>Templates. * To enable and disable the creation of type comments go to * Window>Preferences>Java>Code Generation. */ public class ImportBookmarksAction 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.OPEN); 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) { File importFile = new File(filename); XmlDocument doc = new XmlDocument(); FileInputStream source = null; try { source = new FileInputStream(importFile); } catch (FileNotFoundException e1) { e1.printStackTrace(); return; } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser; try { parser = factory.newDocumentBuilder(); doc = (XmlDocument) parser.parse(source); } catch (ParserConfigurationException e) { e.printStackTrace(); return; } catch (SAXException e) { e.printStackTrace(); return; } catch (IOException e) { e.printStackTrace(); return; } Element root = doc.getDocumentElement(); BookmarkContentProvider.getInstance().importXML(root); view.refresh(); } } /** * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection) */ public void selectionChanged(IAction action, ISelection selection) { } }