Esempio n. 1
0
        public void RemoveModule(IGenericMenuCallback callback)
        {
            ProjectModule  toRemove      = window.CurrentModule;
            Transaction    xn            = new Transaction();
            IProjectAccess proj          = xn.RequestWriteAccess(window.Project);
            int            failureReason = 0;

            using (xn.Start()) {
                IEnumerator <ProjectModule> mods = proj.GetModules().GetEnumerator();
                int size;
                if (!mods.MoveNext())
                {
                    size = 0;
                }
                else
                {
                    if (!mods.MoveNext())
                    {
                        size = 1;
                    }
                    else
                    {
                        size = 2;
                    }
                }
                if (size == 1)
                {
                    failureReason = 1;
                }
                else
                {
                    bool success = proj.RemoveModule(toRemove);
                    if (!success)
                    {
                        failureReason = 2;
                    }
                }
            }
            if (failureReason == 1)
            {
                callback.Notify("Cannot Remove", "Cannot remove only module in project.");
            }
            else if (failureReason == 2)
            {
                callback.Notify("Cannot Remove", "Module not found within project.");
            }
        }
Esempio n. 2
0
        public ToolboxModel(WindowModel window)
        {
            this.window = window;

            Transaction    xn   = new Transaction();
            IProjectAccess proj = xn.RequestReadAccess(window.Project);

            using (xn.Start()) {
                foreach (ProjectModule mod in proj.GetModules())
                {
                    string        modName = proj.GetModuleName(mod);
                    ToolboxModule toAdd   = new ToolboxModule(mod, modName);
                    if (mod == window.CurrentModule)
                    {
                        toAdd.SetName(toAdd.Name + "*");
                        currentModule = toAdd;
                    }
                    modules.Add(toAdd);
                }
            }
            window.Project.ProjectModifiedEvent += RefreshModules;

            List <ToolboxItem> builtins = new List <ToolboxItem>();

            Component[] masters =
            {
                new AndGate(),
                new OrGate(),
                new NotGate(),
                new ToggleSwitch(),
                new Led(),
                new Pin(),
                new PinOut()
            };
            foreach (Component master in masters)
            {
                builtins.Add(new ToolboxComponent(master));
            }

            List <ToolboxDrawer> drawers = new List <ToolboxDrawer>();

            drawers.Add(new ToolboxDrawer("Project", modules.AsReadOnly()));
            drawers.Add(new ToolboxDrawer("Built-Ins", builtins.AsReadOnly()));
            this.drawers = drawers.AsReadOnly();
        }
Esempio n. 3
0
        private void RefreshModules(object src, ProjectModifiedArgs args)
        {
            ProjectModule changedModule = args.ChangedModule;
            ToolboxItem   changedItem   = null;
            ProjectModule currentModule = window.CurrentModule;

            Transaction    xn   = new Transaction();
            IProjectAccess proj = xn.RequestReadAccess(window.Project);

            using (xn.Start()) {
                Dictionary <ProjectModule, ToolboxModule> oldItems = new Dictionary <ProjectModule, ToolboxModule>();
                List <ToolboxModule> newModules = new List <ToolboxModule>();
                foreach (ToolboxModule item in modules)
                {
                    oldItems[item.Module] = item;
                    if (item.Module == changedModule)
                    {
                        changedItem = item;
                    }
                }
                foreach (ProjectModule mod in proj.GetModules())
                {
                    string curName = proj.GetModuleName(mod);
                    if (mod == currentModule)
                    {
                        curName = curName + "*";
                    }
                    ToolboxModule nextItem;
                    if (oldItems.TryGetValue(mod, out nextItem))
                    {
                        if (nextItem.Name != curName)
                        {
                            nextItem.SetName(curName);
                        }
                    }
                    else
                    {
                        nextItem = new ToolboxModule(mod, curName);
                    }
                    newModules.Add(nextItem);
                    if (nextItem.Module == changedModule)
                    {
                        changedItem = nextItem;
                    }
                }
                this.modules.Clear();
                foreach (ToolboxModule item in newModules)
                {
                    this.modules.Add(item);
                }
            }

            if (changedItem != null)
            {
                ToolboxChangedArgs.ChangeTypes changeType;
                bool found = true;
                switch (args.ChangeType)
                {
                case ProjectModifiedArgs.ChangeTypes.ModuleAdded:
                    changeType = ToolboxChangedArgs.ChangeTypes.ItemAdded;
                    break;

                case ProjectModifiedArgs.ChangeTypes.ModuleRemoved:
                    changeType = ToolboxChangedArgs.ChangeTypes.ItemRemoved;
                    break;

                case ProjectModifiedArgs.ChangeTypes.ModuleRenamed:
                    changeType = ToolboxChangedArgs.ChangeTypes.ItemRenamed;
                    break;

                default:
                    changeType = ToolboxChangedArgs.ChangeTypes.ItemAdded; // unused value to trick compiler
                    found      = false;
                    break;
                }
                if (found)
                {
                    OnToolboxChanged(changeType, changedItem);
                }
            }
        }