示例#1
0
        /// <summary>
        /// Open a document and add it to the recent files menu.
        /// </summary>
        /// <param name="path">The path of the document to open.</param>
        private void OpenDocument(string path)
        {
            if (System.IO.Path.GetExtension(path).ToLowerInvariant() == ".cddx" || System.IO.Path.GetExtension(path).ToLowerInvariant() == ".zip")
            {
                using (IO.CDDX.CDDXReader reader = new IO.CDDX.CDDXReader())
                {
                    bool succeeded = reader.Load(File.OpenRead(path));

                    List<IOComponentType> unavailableComponents = null;
                    CircuitDocument loadedDocument = null;
                    if (succeeded)
                    {
                        loadedDocument = reader.Document.ToCircuitDocument(reader, out unavailableComponents);
                        loadedDocument.Metadata.Format = reader.LoadResult.Format; // Set format
                    }
                    if (unavailableComponents == null)
                        unavailableComponents = new List<IOComponentType>();

                    // Show load result dialog
                    if (reader.LoadResult.Type != DocumentLoadResultType.Success || reader.LoadResult.Errors.Count > 0 || unavailableComponents.Count > 0)
                    {
                        winDocumentLoadResult loadResultWindow = new winDocumentLoadResult();
                        loadResultWindow.Owner = this;
                        loadResultWindow.SetMessage(reader.LoadResult.Type);
                        loadResultWindow.SetErrors(reader.LoadResult.Errors);
                        loadResultWindow.SetUnavailableComponents(unavailableComponents);
                        loadResultWindow.ShowDialog();
                    }

                    if (succeeded)
                    {
                        circuitDisplay.Document = loadedDocument;
                        circuitDisplay.DrawConnections();
                        m_docPath = path;
                        m_documentTitle = System.IO.Path.GetFileNameWithoutExtension(path);
                        this.Title = m_documentTitle + " - Circuit Diagram";
                        m_undoManager = new UndoManager();
                        circuitDisplay.UndoManager = m_undoManager;
                        m_undoManager.ActionDelegate = new CircuitDiagram.UndoManager.UndoActionDelegate(UndoActionProcessor);
                        m_undoManager.ActionOccurred += new EventHandler(m_undoManager_ActionOccurred);
                        AddRecentFile(path);
                    }
                }
            }
            else
            {
                using (IO.Xml.XmlReader reader = new IO.Xml.XmlReader())
                {
                    bool succeeded = reader.Load(File.OpenRead(path));

                    List<IOComponentType> unavailableComponents = null;
                    CircuitDocument loadedDocument = null;
                    if (succeeded)
                    {
                        loadedDocument = reader.Document.ToCircuitDocument(reader, out unavailableComponents);
                        loadedDocument.Metadata.Format = reader.LoadResult.Format; // Set format
                    }
                    if (unavailableComponents == null)
                        unavailableComponents = new List<IOComponentType>();

                    // Show load result dialog
                    if (reader.LoadResult.Type != DocumentLoadResultType.Success || reader.LoadResult.Errors.Count > 0 || unavailableComponents.Count > 0)
                    {
                        winDocumentLoadResult loadResultWindow = new winDocumentLoadResult();
                        loadResultWindow.Owner = this;
                        loadResultWindow.SetMessage(reader.LoadResult.Type);
                        loadResultWindow.SetErrors(reader.LoadResult.Errors);
                        loadResultWindow.SetUnavailableComponents(unavailableComponents);
                        loadResultWindow.ShowDialog();
                    }

                    if (succeeded)
                    {
                        circuitDisplay.Document = loadedDocument;
                        circuitDisplay.DrawConnections();
                        m_docPath = path;
                        m_documentTitle = System.IO.Path.GetFileNameWithoutExtension(path);
                        this.Title = m_documentTitle + " - Circuit Diagram";
                        m_undoManager = new UndoManager();
                        circuitDisplay.UndoManager = m_undoManager;
                        m_undoManager.ActionDelegate = new CircuitDiagram.UndoManager.UndoActionDelegate(UndoActionProcessor);
                        m_undoManager.ActionOccurred += new EventHandler(m_undoManager_ActionOccurred);
                        AddRecentFile(path);
                    }
                }
            }
        }
示例#2
0
        void importMenuItem_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Import";
            IDocumentReader reader = (sender as MenuItem).Tag as IDocumentReader;
            ofd.Filter = String.Format("{0} (*{1})|*{1}", reader.FileTypeName, reader.FileTypeExtension);
            if (ofd.ShowDialog() == true)
            {
                bool succeeded = reader.Load(File.OpenRead(ofd.FileName));

                List<IOComponentType> unavailableComponents = null;
                CircuitDocument loadedDocument = null;
                if (succeeded)
                {
                    loadedDocument = reader.Document.ToCircuitDocument(reader, out unavailableComponents);
                    loadedDocument.Metadata.Format = reader.LoadResult.Format; // Set format
                }
                if (unavailableComponents == null)
                    unavailableComponents = new List<IOComponentType>();

                // Show load result dialog
                if (reader.LoadResult.Type != DocumentLoadResultType.Success || reader.LoadResult.Errors.Count > 0 || unavailableComponents.Count > 0)
                {
                    winDocumentLoadResult loadResultWindow = new winDocumentLoadResult();
                    loadResultWindow.Owner = this;
                    loadResultWindow.SetMessage(reader.LoadResult.Type);
                    loadResultWindow.SetErrors(reader.LoadResult.Errors);
                    loadResultWindow.SetUnavailableComponents(unavailableComponents);
                    loadResultWindow.ShowDialog();
                }

                if (succeeded)
                {
                    circuitDisplay.Document = loadedDocument;
                    circuitDisplay.DrawConnections();
                    m_docPath = null; // Cannot be saved to imported format
                    m_documentTitle = System.IO.Path.GetFileName(ofd.FileName);
                    this.Title = m_documentTitle + " - Circuit Diagram";
                    m_undoManager = new UndoManager();
                    circuitDisplay.UndoManager = m_undoManager;
                    m_undoManager.ActionDelegate = new CircuitDiagram.UndoManager.UndoActionDelegate(UndoActionProcessor);
                    m_undoManager.ActionOccurred += new EventHandler(m_undoManager_ActionOccurred);
                }
            }
        }