Search in sources :

Example 1 with ImportDashboardDialog

use of org.netxms.ui.eclipse.dashboard.dialogs.ImportDashboardDialog in project netxms by netxms.

the class ImportDashboard method run.

/* (non-Javadoc)
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
@Override
public void run(IAction action) {
    final ImportDashboardDialog dlg = new ImportDashboardDialog(window.getShell());
    if (dlg.open() != Window.OK)
        return;
    final NXCSession session = (NXCSession) ConsoleSharedData.getSession();
    final Display display = Display.getCurrent();
    new ConsoleJob(Messages.get().ImportDashboard_JobTitle, part, Activator.PLUGIN_ID, null) {

        @Override
        protected void runInternal(IProgressMonitor monitor) throws Exception {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document dom = db.parse(dlg.getImportFile());
            Element root = dom.getDocumentElement();
            if (// $NON-NLS-1$
            !root.getNodeName().equals("dashboard"))
                throw new Exception(Messages.get().ImportDashboard_InvalidFile);
            root.normalize();
            List<DashboardElement> dashboardElements = new ArrayList<DashboardElement>();
            // $NON-NLS-1$
            NodeList elementsRoot = root.getElementsByTagName("elements");
            for (int i = 0; i < elementsRoot.getLength(); i++) {
                if (elementsRoot.item(i).getNodeType() != Node.ELEMENT_NODE)
                    continue;
                // $NON-NLS-1$
                NodeList elements = ((Element) elementsRoot.item(i)).getElementsByTagName("dashboardElement");
                for (int j = 0; j < elements.getLength(); j++) {
                    Element e = (Element) elements.item(j);
                    // $NON-NLS-1$ //$NON-NLS-2$
                    DashboardElement de = new DashboardElement(getNodeValueAsInt(e, "type", 0), getNodeValueAsXml(e, "element"));
                    // $NON-NLS-1$
                    de.setLayout(getNodeValueAsXml(e, "layout"));
                    dashboardElements.add(de);
                }
            }
            root.normalize();
            objectName = dlg.getObjectName();
            if (doIdMapping(display, session, dashboardElements, root)) {
                NXCObjectCreationData cd = new NXCObjectCreationData(AbstractObject.OBJECT_DASHBOARD, objectName, parentId);
                final long objectId = session.createObject(cd);
                NXCObjectModificationData md = new NXCObjectModificationData(objectId);
                // $NON-NLS-1$
                md.setColumnCount(getNodeValueAsInt(root, "columns", 1));
                // $NON-NLS-1$
                md.setObjectFlags(getNodeValueAsInt(root, "options", 0));
                md.setDashboardElements(dashboardElements);
                session.modifyObject(md);
            }
        }

        @Override
        protected String getErrorMessage() {
            return String.format(Messages.get().ImportDashboard_Error, dlg.getObjectName());
        }
    }.start();
}
Also used : NXCSession(org.netxms.client.NXCSession) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NXCObjectCreationData(org.netxms.client.NXCObjectCreationData) DashboardElement(org.netxms.client.dashboards.DashboardElement) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException) DashboardElement(org.netxms.client.dashboards.DashboardElement) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NXCObjectModificationData(org.netxms.client.NXCObjectModificationData) ImportDashboardDialog(org.netxms.ui.eclipse.dashboard.dialogs.ImportDashboardDialog) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) ConsoleJob(org.netxms.ui.eclipse.jobs.ConsoleJob) Display(org.eclipse.swt.widgets.Display)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 TransformerException (javax.xml.transform.TransformerException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Display (org.eclipse.swt.widgets.Display)1 NXCObjectCreationData (org.netxms.client.NXCObjectCreationData)1 NXCObjectModificationData (org.netxms.client.NXCObjectModificationData)1 NXCSession (org.netxms.client.NXCSession)1 DashboardElement (org.netxms.client.dashboards.DashboardElement)1 ImportDashboardDialog (org.netxms.ui.eclipse.dashboard.dialogs.ImportDashboardDialog)1 ConsoleJob (org.netxms.ui.eclipse.jobs.ConsoleJob)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1