Contains utility methods dealing with the application as a whole.
コード例 #1
0
        ExportSelectionToNewNodeXLWorkbook()
        {
            AssertValid();

            // Get the path to the application's template.

            String sTemplatePath;

            if (!ApplicationUtil.TryGetTemplatePath(out sTemplatePath))
            {
                throw new ExportWorkbookException(
                          ApplicationUtil.GetMissingTemplateMessage());
            }

            Workbook oNewNodeXLWorkbook = null;

            CopyTableToNewNodeXLWorkbook(WorksheetNames.Edges,
                                         TableNames.Edges, sTemplatePath, ref oNewNodeXLWorkbook);

            CopyTableToNewNodeXLWorkbook(WorksheetNames.Vertices,
                                         TableNames.Vertices, sTemplatePath, ref oNewNodeXLWorkbook);

            if (oNewNodeXLWorkbook == null)
            {
                throw new ExportWorkbookException(
                          "There are no selected edges or vertices to export to a new"
                          + " workbook."
                          );
            }

            return(oNewNodeXLWorkbook);
        }
コード例 #2
0
        //*************************************************************************
        //  Constructor: GraphMetricCalculationManager()
        //
        /// <summary>
        /// Initializes a new instance of the <see
        /// cref="GraphMetricCalculationManager" /> class.
        /// </summary>
        //*************************************************************************

        public GraphMetricCalculationManager()
        {
            m_oBackgroundWorker = null;

            // Set the path to the executable that calculates graph metrics using
            // the SNAP library.

            GraphMetricCalculatorBase.SetSnapGraphMetricCalculatorPath(
                Path.Combine(ApplicationUtil.GetApplicationFolder(),
                             GraphMetricCalculatorBase
                             .DefaultSnapGraphMetricCalculatorFileName));

            AssertValid();
        }
コード例 #3
0
        AddBuiltInGraphDataProviders
        (
            List <Object> oGraphDataProviders
        )
        {
            Debug.Assert(oGraphDataProviders != null);

            String sGraphDataProvidersFilePath = Path.Combine(
                ApplicationUtil.GetApplicationFolder(), GraphDataProvidersFileName);

            if (File.Exists(sGraphDataProvidersFilePath))
            {
                AddGraphDataProvidersFromFile(
                    sGraphDataProvidersFilePath, oGraphDataProviders);
            }
        }
コード例 #4
0
        GetMissingTemplateMessage()
        {
            String sTemplatePath;

            ApplicationUtil.TryGetTemplatePath(out sTemplatePath);

            return(String.Format(

                       "The {0} Excel template couldn't be found."
                       + "\r\n\r\n"
                       + "The {0} setup program should have copied the template to"
                       + " {1}.  If you moved the template somewhere else, you won't"
                       + " be able to use this feature."
                       ,
                       ApplicationUtil.ApplicationName,
                       sTemplatePath
                       ));
        }
コード例 #5
0
        ConvertNodeXLWorkbook
        (
            String otherWorkbookFile,
            String convertedWorkbookFile
        )
        {
            Debug.Assert(!String.IsNullOrEmpty(otherWorkbookFile));
            Debug.Assert(File.Exists(otherWorkbookFile));
            Debug.Assert(!String.IsNullOrEmpty(convertedWorkbookFile));

            // The application's template is needed to get the customization
            // information.

            String sTemplatePath;

            if (!ApplicationUtil.TryGetTemplatePath(out sTemplatePath))
            {
                throw new NodeXLWorkbookConversionException(
                          ApplicationUtil.GetMissingTemplateMessage());
            }

            try
            {
                File.Copy(otherWorkbookFile, convertedWorkbookFile, true);
            }
            catch (UnauthorizedAccessException)
            {
                throw new NodeXLWorkbookConversionException(
                          "The converted copy already exists and is read-only.  It can't"
                          + " be overwritten."
                          );
            }
            catch (IOException oIOException)
            {
                if (oIOException.Message.Contains(
                        "it is being used by another process"))
                {
                    throw new NodeXLWorkbookConversionException(
                              "The converted copy already exists and is open in Excel."
                              + "  It can't be overwritten."
                              );
                }

                throw (oIOException);
            }

            // Remove the other customization.

            try
            {
                if (ServerDocument.GetCustomizationVersion(
                        convertedWorkbookFile) > 0)
                {
                    ServerDocument.RemoveCustomization(convertedWorkbookFile);
                }
            }
            catch (Microsoft.VisualStudio.Tools.Applications.Runtime.
                   UnknownCustomizationFileException)
            {
                throw new NodeXLWorkbookConversionException(
                          "The file doesn't appear to be an Excel workbook."
                          );
            }

            // Create a ServerDocument from the application's template.  The
            // solution ID and deployment manifest name will be obtained from this.

            using (ServerDocument oTemplateServerDocument =
                       new ServerDocument(sTemplatePath, FileAccess.Read))
            {
                // For some reason, ServerDocument.AddCustomization() also requires
                // a path to the NodeXL assembly file, even though it doesn't get
                // embedded in the document.

                String sAssemblyFile = new Uri(
                    Assembly.GetExecutingAssembly().CodeBase).LocalPath;

                String [] asNonPublicCachedDataMembers;

                ServerDocument.AddCustomization(convertedWorkbookFile,
                                                sAssemblyFile, oTemplateServerDocument.SolutionId,
                                                oTemplateServerDocument.DeploymentManifestUrl, false,
                                                out asNonPublicCachedDataMembers);
            }
        }
コード例 #6
0
        SaveGraphToNodeXLWorkbook
        (
            XmlDocument oGraphMLDocument,
            String sGraphMLFilePath,
            String sNodeXLWorkbookPath,
            String sNodeXLWorkbookSettingsFilePath,
            Boolean bSetAutomateTasksOnOpen,
            Application oExcelApplication
        )
        {
            Debug.Assert(oGraphMLDocument != null);

            Debug.Assert(sNodeXLWorkbookPath == null ||
                         sNodeXLWorkbookPath.Length > 0);

            Debug.Assert(oExcelApplication != null);

            String sWorkbookSettings = null;

            if (sNodeXLWorkbookSettingsFilePath != null)
            {
                try
                {
                    sWorkbookSettings = GetWorkbookSettings(
                        sNodeXLWorkbookSettingsFilePath);
                }
                catch (Exception oException)
                {
                    OnException(oException,
                                ErrorCode.CouldNotReadWorkbookSettingsFile,

                                String.Format(

                                    "The NodeXL options file \"{0}\" couldn't be read."
                                    ,
                                    sNodeXLWorkbookSettingsFilePath
                                    )
                                );
                }
            }

            // Create a new workbook from the NodeXL template.

            Workbook oNodeXLWorkbook = null;

            try
            {
                oNodeXLWorkbook = ApplicationUtil.CreateNodeXLWorkbook(
                    oExcelApplication);
            }
            catch (IOException oIOException)
            {
                throw new ConvertGraphMLToNodeXLWorkbookException(
                          ErrorCode.CouldNotFindNodeXLTemplate, oIOException.Message);
            }
            catch (Exception oException)
            {
                OnException(oException,
                            ErrorCode.CouldNotCreateNodeXLWorkbook,
                            "A NodeXL workbook couldn't be created."
                            );
            }

            // Create a NodeXL graph from the XML document.

            IGraph oGraph = (new GraphMLGraphAdapter()).LoadGraphFromString(
                oGraphMLDocument.OuterXml);

            try
            {
                // Turn off text wrap if necessary to speed up the import.

                GraphImportTextWrapManager.ManageTextWrapBeforeImport(
                    oGraph, oNodeXLWorkbook, false);

                // Import the graph into the workbook.
                //
                // Note that the GraphMLGraphAdapter stored String arrays on the
                // IGraph object that specify the names of the attributes that it
                // added to the graph's edges and vertices.  These get used by the
                // ImportGraph method to determine which columns need to be added
                // to the edge and vertex worksheets.

                GraphImporter.ImportGraph(oGraph,

                                          ( String[] )oGraph.GetRequiredValue(
                                              ReservedMetadataKeys.AllEdgeMetadataKeys,
                                              typeof(String[])),

                                          ( String[] )oGraph.GetRequiredValue(
                                              ReservedMetadataKeys.AllVertexMetadataKeys,
                                              typeof(String[])),

                                          false, oNodeXLWorkbook);

                // Store the graph's directedness in the workbook.

                PerWorkbookSettings oPerWorkbookSettings =
                    new ExcelTemplate.PerWorkbookSettings(oNodeXLWorkbook);

                oPerWorkbookSettings.GraphDirectedness = oGraph.Directedness;

                if (sWorkbookSettings != null)
                {
                    oPerWorkbookSettings.WorkbookSettings = sWorkbookSettings;
                }

                Object oGraphDescriptionAsObject;

                if (!String.IsNullOrEmpty(sGraphMLFilePath))
                {
                    // The GraphML came from a file.

                    GraphImporter.UpdateGraphHistoryAfterImport(oNodeXLWorkbook,

                                                                GraphImporter.GetImportedGraphMLFileDescription(
                                                                    sGraphMLFilePath, oGraph),

                                                                GraphImporter.GetImportedGraphMLFileTitle(oGraph),

                                                                null);
                }
                else if (oGraph.TryGetValue(ReservedMetadataKeys.GraphDescription,
                                            typeof(String), out oGraphDescriptionAsObject))
                {
                    // The GraphML came from the NetworkServer program.
                    //
                    // Note that we can't have GraphImporter check the user's
                    // ImportUserSettings object here to determine if the import
                    // description should be saved.  Accessing user setting objects
                    // requires access to Globals.ThisWorkbook, which is null when
                    // GraphImporter is called from another process.

                    GraphImporter
                    .UpdateGraphHistoryAfterImportWithoutPermissionCheck(
                        oNodeXLWorkbook, (String)oGraphDescriptionAsObject,
                        null, null, oPerWorkbookSettings);
                }

                if (bSetAutomateTasksOnOpen)
                {
                    // Store an "automate tasks on open" flag in the workbook,
                    // indicating that task automation should be run on it the next
                    // time it's opened.  (It is up to the user of this class to
                    // open the workbook to trigger automation.)

                    oPerWorkbookSettings.AutomateTasksOnOpen = true;
                }
            }
            catch (Exception oException)
            {
                OnException(oException,
                            ErrorCode.CouldNotImportGraphMLIntoNodeXLWorkbook,
                            "The GraphML couldn't be imported into the NodeXL workbook."
                            );
            }

            if (sNodeXLWorkbookPath == null)
            {
                return;
            }

            try
            {
                ExcelUtil.SaveWorkbookAs(oNodeXLWorkbook, sNodeXLWorkbookPath);
            }
            catch (Exception oException)
            {
                OnException(oException, ErrorCode.SaveNodeXLWorkbookFileError,
                            "The NodeXL workbook couldn't be saved."
                            );
            }

            try
            {
                oNodeXLWorkbook.Close(false, Missing.Value, Missing.Value);
            }
            catch (Exception oException)
            {
                OnException(oException, ErrorCode.SaveNodeXLWorkbookFileError,
                            "The NodeXL workbook couldn't be closed."
                            );
            }
        }