dashboard export/import working, but without id remapping yet
authorVictor Kirhenshtein <victor@netxms.org>
Fri, 27 Apr 2012 21:33:24 +0000 (21:33 +0000)
committerVictor Kirhenshtein <victor@netxms.org>
Fri, 27 Apr 2012 21:33:24 +0000 (21:33 +0000)
android/src/console/.project
src/java/netxms-eclipse/Dashboard/src/org/netxms/ui/eclipse/dashboard/actions/ExportDashboard.java
src/java/netxms-eclipse/Dashboard/src/org/netxms/ui/eclipse/dashboard/actions/ImportDashboard.java
src/java/netxms-eclipse/Dashboard/src/org/netxms/ui/eclipse/dashboard/dialogs/ImportDashboardDialog.java
src/java/netxms-eclipse/FileManager/src/org/netxms/ui/eclipse/filemanager/widgets/LocalFileSelector.java

index dc95d28..795f928 100644 (file)
@@ -6,33 +6,33 @@
        </projects>
        <buildSpec>
                <buildCommand>
-                       <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
+                       <name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
+                       <triggers>full,incremental,</triggers>
                        <arguments>
+                               <dictionary>
+                                       <key>LaunchConfigHandle</key>
+                                       <value>&lt;project&gt;/.externalToolBuilders/Generate build_number.xml from SVN revision.launch</value>
+                               </dictionary>
                        </arguments>
                </buildCommand>
                <buildCommand>
-                       <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
+                       <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
                        <arguments>
                        </arguments>
                </buildCommand>
                <buildCommand>
-                       <name>org.eclipse.jdt.core.javabuilder</name>
+                       <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
                        <arguments>
                        </arguments>
                </buildCommand>
                <buildCommand>
-                       <name>com.android.ide.eclipse.adt.ApkBuilder</name>
+                       <name>org.eclipse.jdt.core.javabuilder</name>
                        <arguments>
                        </arguments>
                </buildCommand>
                <buildCommand>
-                       <name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
-                       <triggers>full,incremental,</triggers>
+                       <name>com.android.ide.eclipse.adt.ApkBuilder</name>
                        <arguments>
-                               <dictionary>
-                                       <key>LaunchConfigHandle</key>
-                                       <value>&lt;project&gt;/.externalToolBuilders/Generate build_number.xml from SVN revision.launch</value>
-                               </dictionary>
                        </arguments>
                </buildCommand>
        </buildSpec>
index 839a992..daac12d 100644 (file)
@@ -84,7 +84,9 @@ public class ExportDashboard implements IObjectActionDelegate
                xml.append(dashboard.getObjectName());\r
                xml.append("</name>\n\t<columns>");\r
                xml.append(dashboard.getNumColumns());\r
-               xml.append("</columns>\n\t<elements>\n");\r
+               xml.append("</columns>\n\t<options>");\r
+               xml.append(dashboard.getOptions());\r
+               xml.append("</options>\n\t<elements>\n");\r
                for(DashboardElement e : dashboard.getElements())\r
                {\r
                        xml.append("\t\t<dashboardElement>\n\t\t\t<type>");\r
index caa4833..ffb8f7f 100644 (file)
  */\r
 package org.netxms.ui.eclipse.dashboard.actions;\r
 \r
+import java.io.StringWriter;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
 import javax.xml.parsers.DocumentBuilder;\r
 import javax.xml.parsers.DocumentBuilderFactory;\r
+import javax.xml.transform.OutputKeys;\r
+import javax.xml.transform.Transformer;\r
+import javax.xml.transform.TransformerException;\r
+import javax.xml.transform.TransformerFactory;\r
+import javax.xml.transform.TransformerFactoryConfigurationError;\r
+import javax.xml.transform.dom.DOMSource;\r
+import javax.xml.transform.stream.StreamResult;\r
 import org.eclipse.core.runtime.IProgressMonitor;\r
 import org.eclipse.jface.action.IAction;\r
 import org.eclipse.jface.viewers.ISelection;\r
@@ -29,17 +39,20 @@ import org.eclipse.ui.IObjectActionDelegate;
 import org.eclipse.ui.IWorkbenchPart;\r
 import org.eclipse.ui.IWorkbenchWindow;\r
 import org.netxms.client.NXCObjectCreationData;\r
+import org.netxms.client.NXCObjectModificationData;\r
 import org.netxms.client.NXCSession;\r
+import org.netxms.client.dashboards.DashboardElement;\r
 import org.netxms.client.objects.Dashboard;\r
 import org.netxms.client.objects.DashboardRoot;\r
 import org.netxms.client.objects.GenericObject;\r
 import org.netxms.ui.eclipse.dashboard.Activator;\r
 import org.netxms.ui.eclipse.dashboard.dialogs.ImportDashboardDialog;\r
 import org.netxms.ui.eclipse.jobs.ConsoleJob;\r
-import org.netxms.ui.eclipse.objectbrowser.dialogs.CreateObjectDialog;\r
 import org.netxms.ui.eclipse.shared.ConsoleSharedData;\r
 import org.w3c.dom.Document;\r
 import org.w3c.dom.Element;\r
+import org.w3c.dom.Node;\r
+import org.w3c.dom.NodeList;\r
 \r
 /**\r
  * Create dashboard object\r
@@ -61,6 +74,32 @@ public class ImportDashboard implements IObjectActionDelegate
                window = targetPart.getSite().getWorkbenchWindow();\r
        }\r
 \r
+       /* (non-Javadoc)\r
+        * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)\r
+        */\r
+       @Override\r
+       public void selectionChanged(IAction action, ISelection selection)\r
+       {\r
+               if ((selection instanceof IStructuredSelection) && (((IStructuredSelection)selection).size() == 1))\r
+               {\r
+                       final Object object = ((IStructuredSelection)selection).getFirstElement();\r
+                       if ((object instanceof Dashboard) || (object instanceof DashboardRoot))\r
+                       {\r
+                               parentId = ((GenericObject)object).getObjectId();\r
+                       }\r
+                       else\r
+                       {\r
+                               parentId = -1;\r
+                       }\r
+               }\r
+               else\r
+               {\r
+                       parentId = -1;\r
+               }\r
+\r
+               action.setEnabled(parentId != -1);\r
+       }\r
+\r
        /* (non-Javadoc)\r
         * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)\r
         */\r
@@ -83,13 +122,35 @@ public class ImportDashboard implements IObjectActionDelegate
                                if (!root.getNodeName().equals("dashboard"))\r
                                        throw new Exception("Invalid import file");\r
                                \r
+                               root.normalize();\r
+                               \r
+                               List<DashboardElement> dashboardElements = new ArrayList<DashboardElement>(); \r
                                \r
+                               NodeList elementsRoot = root.getElementsByTagName("elements");\r
+                               for(int i = 0; i < elementsRoot.getLength(); i++)\r
+                               {\r
+                                       if (elementsRoot.item(i).getNodeType() != Node.ELEMENT_NODE)\r
+                                               continue;\r
+                                       \r
+                                       NodeList elements = ((Element)elementsRoot.item(i)).getElementsByTagName("dashboardElement");\r
+                                       for(int j = 0; j < elements.getLength(); j++)\r
+                                       {\r
+                                               Element e = (Element)elements.item(j);\r
+                                               DashboardElement de = new DashboardElement(getNodeValueAsInt(e, "type", 0), getNodeValueAsXml(e, "element"));\r
+                                               de.setLayout(getNodeValueAsXml(e, "layout"));\r
+                                               dashboardElements.add(de);\r
+                                       }\r
+                               }\r
                                \r
-                               /*\r
                                NXCSession session = (NXCSession)ConsoleSharedData.getSession();\r
                                NXCObjectCreationData cd = new NXCObjectCreationData(GenericObject.OBJECT_DASHBOARD, dlg.getObjectName(), parentId);\r
-                               session.createObject(cd);\r
-                               */\r
+                               final long objectId = session.createObject(cd);\r
+                               \r
+                               NXCObjectModificationData md = new NXCObjectModificationData(objectId);\r
+                               md.setColumnCount(getNodeValueAsInt(root, "columns", 1));\r
+                               md.setDashboardOptions(getNodeValueAsInt(root, "options", 0));\r
+                               md.setDashboardElements(dashboardElements);\r
+                               session.modifyObject(md);\r
                        }\r
 \r
                        @Override\r
@@ -99,30 +160,63 @@ public class ImportDashboard implements IObjectActionDelegate
                        }\r
                }.start();\r
        }\r
-\r
-       /* (non-Javadoc)\r
-        * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)\r
+       \r
+       /**\r
+        * Get value of given node as integer.\r
+        * \r
+        * @param parent\r
+        * @param tag\r
+        * @param defaultValue\r
+        * @return\r
         */\r
-       @Override\r
-       public void selectionChanged(IAction action, ISelection selection)\r
+       private static int getNodeValueAsInt(Element parent, String tag, int defaultValue)\r
        {\r
-               if ((selection instanceof IStructuredSelection) && (((IStructuredSelection)selection).size() == 1))\r
+               NodeList l = parent.getElementsByTagName(tag);\r
+               if ((l.getLength() == 0) || (l.item(0).getNodeType() != Node.ELEMENT_NODE))\r
+                       return defaultValue;\r
+               \r
+               try\r
                {\r
-                       final Object object = ((IStructuredSelection)selection).getFirstElement();\r
-                       if ((object instanceof Dashboard) || (object instanceof DashboardRoot))\r
-                       {\r
-                               parentId = ((GenericObject)object).getObjectId();\r
-                       }\r
-                       else\r
-                       {\r
-                               parentId = -1;\r
-                       }\r
+                       return Integer.parseInt(((Element)l.item(0)).getTextContent());\r
                }\r
-               else\r
+               catch(NumberFormatException e)\r
                {\r
-                       parentId = -1;\r
+                       return defaultValue;\r
                }\r
-\r
-               action.setEnabled(parentId != -1);\r
+       }\r
+       \r
+       /**\r
+        * Get node value as XML document.\r
+        * \r
+        * @param parent\r
+        * @param tag\r
+        * @return\r
+        * @throws TransformerFactoryConfigurationError\r
+        * @throws TransformerException\r
+        */\r
+       private static String getNodeValueAsXml(Element parent, String tag) throws TransformerFactoryConfigurationError, TransformerException\r
+       {\r
+               NodeList l = parent.getElementsByTagName(tag);\r
+               if ((l.getLength() == 0) || (l.item(0).getNodeType() != Node.ELEMENT_NODE))\r
+                       return "<" + tag + "></" + tag + ">";\r
+               return nodeToString(l.item(0));\r
+       }\r
+       \r
+       /**\r
+        * Convert DOM tree node to string (XML document).\r
+        * \r
+        * @param node\r
+        * @return\r
+        * @throws TransformerFactoryConfigurationError\r
+        * @throws TransformerException\r
+        */\r
+       private static String nodeToString(Node node) throws TransformerFactoryConfigurationError, TransformerException\r
+       {\r
+               StringWriter sw = new StringWriter();\r
+               Transformer t = TransformerFactory.newInstance().newTransformer();\r
+               t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");\r
+               t.setOutputProperty(OutputKeys.INDENT, "yes");\r
+               t.transform(new DOMSource(node), new StreamResult(sw));\r
+               return sw.toString();\r
        }\r
 }\r
index d10eda1..1529183 100644 (file)
@@ -1,6 +1,6 @@
 /**\r
  * NetXMS - open source network management system\r
- * Copyright (C) 2003-2010 Victor Kirhenshtein\r
+ * Copyright (C) 2003-2012 Victor Kirhenshtein\r
  *\r
  * This program is free software; you can redistribute it and/or modify\r
  * it under the terms of the GNU General Public License as published by\r
@@ -22,13 +22,14 @@ import java.io.File;
 import org.eclipse.jface.dialogs.Dialog;\r
 import org.eclipse.jface.dialogs.MessageDialog;\r
 import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.layout.GridData;\r
 import org.eclipse.swt.layout.GridLayout;\r
 import org.eclipse.swt.widgets.Composite;\r
 import org.eclipse.swt.widgets.Control;\r
 import org.eclipse.swt.widgets.Shell;\r
-import org.eclipse.swt.widgets.Text;\r
 import org.netxms.ui.eclipse.filemanager.widgets.LocalFileSelector;\r
 import org.netxms.ui.eclipse.tools.WidgetHelper;\r
+import org.netxms.ui.eclipse.widgets.LabeledText;\r
 \r
 /**\r
  * "Import Dashboard" dialog\r
@@ -37,7 +38,7 @@ public class ImportDashboardDialog extends Dialog
 {\r
        private String objectName;\r
        private File importFile;\r
-       private Text textName;\r
+       private LabeledText textName;\r
        private LocalFileSelector importFileSelector;\r
 \r
        /**\r
@@ -73,14 +74,24 @@ public class ImportDashboardDialog extends Dialog
       layout.marginHeight = WidgetHelper.DIALOG_HEIGHT_MARGIN;\r
       dialogArea.setLayout(layout);\r
                \r
-      textName = WidgetHelper.createLabeledText(dialogArea, SWT.SINGLE | SWT.BORDER, SWT.DEFAULT,\r
-               "Object name", "", WidgetHelper.DEFAULT_LAYOUT_DATA);\r
-      textName.getShell().setMinimumSize(300, 0);\r
-      textName.setTextLimit(63);\r
-      textName.setFocus();\r
+      textName = new LabeledText(dialogArea, SWT.NONE);\r
+      textName.setLabel("Object name");\r
+      textName.getTextControl().setTextLimit(63);\r
+      GridData gd = new GridData();\r
+      gd.horizontalAlignment = SWT.FILL;\r
+      gd.grabExcessHorizontalSpace = true;\r
+      gd.widthHint = 350;\r
+      textName.setLayoutData(gd);\r
       \r
       importFileSelector = new LocalFileSelector(dialogArea, SWT.NONE, false);\r
       importFileSelector.setLabel("Import file");\r
+      importFileSelector.setFilterExtensions(new String[] { "*.xml", "*.*" });\r
+      importFileSelector.setFilterNames(new String[] { "XML files", "All files" });\r
+      gd = new GridData();\r
+      gd.horizontalAlignment = SWT.FILL;\r
+      gd.grabExcessHorizontalSpace = true;\r
+      gd.widthHint = 350;\r
+      importFileSelector.setLayoutData(gd);\r
       \r
                return dialogArea;\r
        }\r
index 4a17349..1d24c2d 100644 (file)
@@ -30,6 +30,8 @@ import org.netxms.ui.eclipse.widgets.AbstractSelector;
 public class LocalFileSelector extends AbstractSelector\r
 {\r
        private File file = null;\r
+       private String[] filterExtensions = { "*.*" };\r
+       private String[] filterNames = { "All files" };\r
        \r
        /**\r
         * @param parent\r
@@ -51,8 +53,8 @@ public class LocalFileSelector extends AbstractSelector
        {\r
                FileDialog fd = new FileDialog(getShell(), SWT.OPEN);\r
                fd.setText("Select File");\r
-               fd.setFilterExtensions(new String[] { "*.*" });\r
-               fd.setFilterNames(new String[] { "All files" });\r
+               fd.setFilterExtensions(filterExtensions);\r
+               fd.setFilterNames(filterNames);\r
                String selected = fd.open();\r
                if (selected != null)\r
                        setFile(new File(selected));\r
@@ -93,4 +95,36 @@ public class LocalFileSelector extends AbstractSelector
                        setText("<none>");\r
                }\r
        }\r
+\r
+       /**\r
+        * @return the filterExtensions\r
+        */\r
+       public String[] getFilterExtensions()\r
+       {\r
+               return filterExtensions;\r
+       }\r
+\r
+       /**\r
+        * @param filterExtensions the filterExtensions to set\r
+        */\r
+       public void setFilterExtensions(String[] filterExtensions)\r
+       {\r
+               this.filterExtensions = filterExtensions;\r
+       }\r
+\r
+       /**\r
+        * @return the filterNames\r
+        */\r
+       public String[] getFilterNames()\r
+       {\r
+               return filterNames;\r
+       }\r
+\r
+       /**\r
+        * @param filterNames the filterNames to set\r
+        */\r
+       public void setFilterNames(String[] filterNames)\r
+       {\r
+               this.filterNames = filterNames;\r
+       }\r
 }\r