Example #1
0
        DocumentEvents saveEvent;                                                                  //DocumentEvents object used for saving events

        /// <summary>
        /// Initialization of UI and editing related events and members
        /// </summary>
        /// <param name="textView"></param>
        /// <param name="mcb"></param>
        /// <param name="cs"></param>
        public CoProCommandFilter(IWpfTextView textView, CoProNetwork mcb, GraphicObjects cs)
        {
            m_textView                = textView;
            m_adornmentLayer          = m_textView.GetAdornmentLayer("MultiEditLayer");
            m_added                   = false;
            m_textView.LayoutChanged += m_textView_LayoutChanged;
            cb   = mcb;
            gobj = cs;
            //crts.DTE2.Events.TextEditorEvents.LineChanged += new _dispTextEditorEvents_LineChangedEventHandler();
            events    = gobj.DTE2.Events;
            saveEvent = events.DocumentEvents;
            saveEvent.DocumentSaved += new _dispDocumentEvents_DocumentSavedEventHandler(my_DocWasSaved);
            cb.NewCaret             += new NewCaretEventHandler(my_NewCaret);
            cb.ChangeCaret          += new ChangeCaretEventHandler(my_ChangedCaret);
            cb.EditorDisc           += new EditorDisconnectedEventHandler(my_EditorDisc);
            cb.NewText       += new NewTextEventHandler(my_AddedText);
            cb.RemovedText   += new RemovedTextEventHandler(my_RemovedText);
            cb.SaveEvent     += new SaveEventHandler(my_Save);
            cb.AddAllEditors += new AddCurrentEditorsEventHandler(my_AddEditors);
            cb.ItemRemoved   += new ItemRemovedEventHandler(my_RemovedItem);
            cb.ItemAdded     += new NewItemAddedEventHandler(my_ItemAdded);
            textView.Caret.PositionChanged += new EventHandler <CaretPositionChangedEventArgs>(my_PositionChanged);
            textView.TextBuffer.Changed    += TextBuffer_Changed;
            InitBrushes();
            uiDisp = Dispatcher.CurrentDispatcher;
            ITextDocument textDoc;
            var           rc = m_textView.TextBuffer.Properties.TryGetProperty <ITextDocument>(
                typeof(ITextDocument), out textDoc);

            filename = gobj.DTE2.Solution.FullName;
            filename = filename.Substring(filename.LastIndexOf('\\') + 1);
            filename = filename.Split('.')[0];
            filename = textDoc.FilePath.Substring(textDoc.FilePath.IndexOf(filename));
            if (cb.IsAdmin)
            {
                cb.AdminEvent += my_AdminCallback;
            }
            if (isFirstTime)
            {
                isFirstTime = false;
            }
            else
            {
                if (!cb.IsAdmin)
                {
                    cb.UpdateSpecificFile(filename);
                }
            }
            filename = filename.Substring(filename.LastIndexOf('\\') + 1);
            cb.IntializePosition(filename, m_textView.Caret.Position.BufferPosition.Position, cb.Name);

            currBufferSize = m_textView.TextSnapshot.Length;
        }
        /// <summary>
        /// This function is called whenever a new item to edit is opened
        /// </summary>
        /// <param name="textViewAdapter">adapter of the text view (given by the VS itself [MEF] )</param>
        public void VsTextViewCreated(IVsTextView textViewAdapter)
        {
            if (!runFlag)
            {
                cb = CoProgrammerPackage.cb;// gets current network object
                if (cb == null)
                {
                    cb = new CoProNetwork();//if null create new
                    CoProgrammerPackage.cb = cb;
                }
                ToolWindowPane window = CoProgrammerPackage.currRunning.FindToolWindow(typeof(CoProToolWindow), 0, true);
                if ((null == window) || (null == window.Frame))
                {
                    throw new NotSupportedException(Resources.CanNotCreateWindow);
                }
                gobj = new GraphicObjects(GetCurrentViewHost(textViewAdapter), cb, (CoProExplorer)window.Content);
                gobj.CoProExplorer.SetConnection(cb); //updates co pro explorer window
                if (isFirst)                          // on opening the solution
                {
                    se                 = ((Events2)gobj.DTE2.Events).SolutionEvents;
                    de                 = ((Events2)gobj.DTE2.Events).DebuggerEvents;
                    se.Opened         += SubscribeGlobalEvents;                                     //solution opened event
                    de.OnEnterRunMode += new _dispDebuggerEvents_OnEnterRunModeEventHandler(OnRun); //when running the code event
                    bool setAdminConfiguraions = false;
                    var  slnName   = gobj.DTE2.Solution.FullName;
                    var  adminFile = slnName.Substring(0, slnName.Substring(0, slnName.LastIndexOf('\\')).LastIndexOf('\\')) + "\\admin.txt";
                    //IF THERE IS ADMIN INFO
                    if (File.Exists(adminFile))//internal file that was created
                    {
                        StreamReader sr  = new StreamReader(adminFile);
                        string       dir = sr.ReadToEnd();
                        if (dir.Split('\n')[0] == slnName.Substring(0, slnName.LastIndexOf('\\')))
                        {
                            CoProgrammerPackage.service = new CoProServer();// runs the server
                            string filesDir = slnName.Substring(0, slnName.LastIndexOf('\\')) + "\\CoProFiles";
                            if (!Directory.Exists(filesDir))
                            {
                                Directory.CreateDirectory(slnName.Substring(0, slnName.LastIndexOf('\\')) + "\\CoProFiles");
                            }
                            string        path = slnName.Substring(0, slnName.LastIndexOf('\\'));
                            XElement      xe   = CoProgrammerPackage.CreateFileSystemXmlTree(path, 1, path.Substring(path.LastIndexOf('\\') + 1));
                            XmlTextWriter xwr  = new XmlTextWriter(path + "\\CoProFiles\\timestamps.xml", System.Text.Encoding.UTF8);// timestamps file creation
                            xwr.Formatting = Formatting.Indented;
                            xe.WriteTo(xwr);
                            xwr.Close();
                            FileStream fs     = File.Create(slnName.Substring(0, slnName.LastIndexOf('\\')) + "\\CoProFiles\\client.txt");
                            string     ipPort = "localhost:" + (CoProgrammerPackage.service.PortOfService() + 10).ToString() + ":" + dir.Split('\n')[1];
                            fs.Write(Encoding.ASCII.GetBytes(ipPort), 0, ipPort.Length);// creating client file if there is no one exsisting
                            fs.Close();
                            setAdminConfiguraions = true;
                        }
                    }
                    //IF THERE IS A CLIENT INFO
                    if (File.Exists(slnName.Substring(0, slnName.LastIndexOf('\\')) + "\\CoProFiles\\client.txt"))// internal client info file
                    {
                        StreamReader sr        = new StreamReader(slnName.Substring(0, slnName.LastIndexOf('\\')) + "\\CoProFiles\\client.txt");
                        string       iportName = sr.ReadToEnd();
                        cb.SetIpPort(iportName.Split(':')[0], iportName.Split(':')[1]);
                        cb.Name = iportName.Split(':')[2]; // configurates the class accoridng to info in file
                        if (cb.Connect())                  // attempt to connect
                        {
                            cb.ProjPath = slnName.Substring(0, slnName.LastIndexOf('\\'));
                            if (setAdminConfiguraions)
                            {
                                if (cb.SetAdmin(true))
                                {
                                    cb.IsAdmin = true;// if is admin, he gets the access to some funcitons
                                    cb.SetProjectDir(slnName.Substring(0, slnName.LastIndexOf('\\')));
                                }
                            }
                            else
                            {
                                cb.IsAdmin = false;
                                cb.UpdateProject();// updates project for regular clients
                            }
                            if (cb.IsAdmin)
                            {
                                cb.ExpectedSequence++;       // +1 to current count toget next messages
                            }
                            gobj.CoProExplorer.UpdateInfo(); // updates window info
                        }
                    }
                    else
                    {
                        cb   = null;
                        gobj = null;
                    }
                    isFirst = false;
                }

                IWpfTextView textView = editorFactory.GetWpfTextView(textViewAdapter);//gets the text view
                if (textView == null)
                {
                    return;
                }
                AddCommandFilter(textViewAdapter, new CoProCommandFilter(textView, cb, gobj));//adds an instance of our command filter to the text view
            }
            runFlag = false;
        }