Ticket #2413: add-remove-eleents-from-template-de.patch

File add-remove-eleents-from-template-de.patch, 13.5 KB (added by deni, 15 years ago)
  • modules/org.sophie2.main.app.commons/src/main/java/org/sophie2/main/app/commons/util/TemplateUtil.java

    ### Eclipse Workspace Patch 1.0
    #P sophie
     
    109109                final ResourceRefList newTemplates =  
    110110                        templates.contains(templateRef) ? templates : templates.add(templateRef); 
    111111                 
    112                 //auto-action which changes the keys of the root element/its sub-elements 
    113                 String changeKeysDescription = "Change the template's elements' keys"; 
    114                 new AutoAction(changeKeysDescription, false) { 
     112                 
     113                ImmList<ResourceRefR4> rootsToDelete = ImmTreeList.<ResourceRefR4>create(); 
     114                /* 
     115                 * Adds the references to be removed. We're looking for references who should be deleted, 
     116                 * but their parent shouldn't be (so when we remove them, we'll remove all their sub-elements as well,  
     117                 * which is what we need). Note that iterRef.getParentRef() will never equal the ref of the element we're templating, 
     118                 * because we pass the root as an absolute reference and the sub-elements as relative ones.  
     119                 */ 
     120                for (ResourceRefR4 iterRef : toDelete) { 
     121                        if (!toDelete.contains(iterRef.getParentRef()) && !iterRef.equals(ref) ) { 
     122                                rootsToDelete = rootsToDelete.add(iterRef); 
     123                        } 
     124                }                
     125                final ImmList<ResourceRefR4> finalRootsToDelete = rootsToDelete; 
     126                 
     127                /* 
     128                 * Copies the template as a child of the book, changes its keys and deletes 
     129                 * the unneeded sub-elements (also removes their activation channels from the  
     130                 * corresponding parent's KEY_SUB_ELEMENTS key). 
     131                 */ 
     132                new AutoAction(description, true) { 
    115133                        @Override 
    116134                        public void performAuto() { 
    117135                                getChanger().copyResource(relativeRef, templateRef); 
     
    136154                                if(createDefaultTemplate) { 
    137155                                        getChanger().setRaw(defaultTemplateKey, templateRef); 
    138156                                } 
    139                         } 
    140  
    141                 }.register(bookAccess); 
    142                  
    143                 ImmList<ResourceRefR4> rootsToDelete = ImmTreeList.<ResourceRefR4>create(); 
    144                  
    145                 /* 
    146                  * Adds the references to be removed. We're looking for references who should be deleted, 
    147                  * but their parent shouldn't be (so when we remove them, we'll remove all their sub-elements as well,  
    148                  * which is what we need). Note that iterRef.getParentRef() will never equal the ref of the element we're templating, 
    149                  * because we pass the root as an absolute reference and the sub-elements as relative ones.  
    150                  */ 
    151                 for (ResourceRefR4 iterRef : toDelete) { 
    152                         if (!toDelete.contains(iterRef.getParentRef()) && !iterRef.equals(ref) ) { 
    153                                 rootsToDelete = rootsToDelete.add(iterRef); 
    154                         } 
    155                 } 
    156                  
    157                 final ImmList<ResourceRefR4> finalRootsToDelete = rootsToDelete; 
    158                  
    159                 /* 
    160                  * Auto-action which deletes the unneeded sub-elements (also removes their activation channels from the  
    161                  * corresponding parent's KEY_SUB_ELEMENTS key). 
    162                  */ 
    163                 new AutoAction (description, isSign) { 
    164                         @Override 
    165                         public void performAuto () { 
    166                                 ResourceChanger subCh = getChanger().getSub(templateRef); 
    167157                                                                 
    168158                                for (ResourceRefR4 iterRef : finalRootsToDelete) { 
    169159                                        ResourceRefR4 parent = iterRef.getParentRef(), parentToIterRef = ResourceRefR4.getRelativeRef(parent, iterRef); 
     
    184174                                        subCh.removeResource(iterRef);   
    185175                                } 
    186176                        } 
     177 
    187178                }.register(bookAccess); 
     179                 
     180 
     181                 
     182 
     183//              new AutoAction (description, isSign) { 
     184//                      @Override 
     185//                      public void performAuto () { 
     186// 
     187//                              } 
     188//                      } 
     189//              }.register(bookAccess); 
    188190        } 
    189191 
    190192        /** 
     
    369371                } 
    370372        } 
    371373         
    372         private static void templatePageElements(ElementH templateH, ElementH parent,  
     374        public static void templatePageElements(ElementH templateH, ElementH parent,  
    373375                        List<ResourceRefR4> newElements, Map<ResourceRefR4, ResourceRefR4> existing) { 
    374376 
     377                // TODO: make sure KEY_TEMPLATE is set for groups, too 
     378                 
    375379                if (!newElements.contains(templateH.getRef()) && !existing.containsKey(templateH.getRef())) { 
    376380                        return; 
    377381                } 
     
    435439                final String frameKind = templateH.getKind(); 
    436440                final NaiveImmList<TemplatedKey<?>> immKeys = templateH.getApplicableTemplatedKeys(); 
    437441 
    438                 final ResourceRefR4 frameRef = ResourceRefR4.generateRandomSub(FrameH.NAME_PREFIX); 
     442                final ResourceRefR4 frameRef = templateH.getRef().getThisChildRef(); 
    439443                final String frameTitle = templateH.getTitle(); 
    440444 
    441445                final ResourceRefR4 parentRef = parent.getRef(); 
  • modules/org.sophie2.main.func.templates/src/main/java/org/sophie2/main/func/templates/MainFuncTemplatesModule.java

     
    1919import org.sophie2.main.app.menus.file.SaveBookAsTemplateItem; 
    2020import org.sophie2.main.func.templates.book.BookTemplatesLogic; 
    2121import org.sophie2.main.func.templates.book.BookTemplatesPalette; 
     22import org.sophie2.main.func.templates.composite.CompositeTemplateLogic; 
    2223import org.sophie2.main.func.templates.frame.FrameTemplateLogic; 
    2324import org.sophie2.main.func.templates.frame.FrameTemplatesPalette; 
    2425import org.sophie2.main.func.templates.page.ApplyPageTemplateDialog; 
     
    6263                SimpleOperation.fillExtensions(res, FrameTemplateLogic.class);           
    6364                SimpleOperation.fillExtensions(res, PageTemplateLogic.class);            
    6465                SimpleOperation.fillExtensions(res, BookTemplatesLogic.class); 
    65                  
     66                SimpleOperation.fillExtensions(res, CompositeTemplateLogic.class); 
     67 
    6668                res.add(new SimpleSophieExtension<Dialog>(Dialog.class, 
    6769                                new ApplyPageTemplateDialog())); 
    6870                AutoVisualProvider.fillExtensions(res, ApplyPageTemplateSwingDialog.class); 
  • modules/org.sophie2.base.model.book/src/main/java/org/sophie2/base/model/book/ElementH.java

     
    55 
    66import org.sophie2.base.commons.structures.ImmTreeList; 
    77import org.sophie2.base.commons.util.ImmList; 
     8import org.sophie2.base.commons.util.NaiveImmList; 
    89import org.sophie2.base.commons.util.position.ImmArea; 
    910import org.sophie2.base.commons.util.position.ImmInsets; 
    1011import org.sophie2.base.commons.util.position.ImmMatrix; 
     
    3334import org.sophie2.base.model.resources.r4.resources.ResourceH; 
    3435import org.sophie2.base.model.resources.r4.resources.ResourceR4; 
    3536import org.sophie2.core.logging.SophieLog; 
     37import org.sophie2.core.mvc.EventParams; 
     38import org.sophie2.core.mvc.LogicR3; 
    3639import org.sophie2.core.prolib.impl.AutoProperty; 
    3740import org.sophie2.core.prolib.interfaces.Prop; 
    3841 
     
    567570                } 
    568571                return null; 
    569572        } 
     573         
     574        @SuppressWarnings("unused") 
     575        private Prop<ImmList<ActivationChannel>> templateSubElements() { 
     576                class templateSubElements extends AutoProperty<ImmList<ActivationChannel>> { 
    570577 
     578                        private ImmList<ActivationChannel> prevTemplateElements =  
     579                                NaiveImmList.<ActivationChannel>getEmpty(); 
     580                        private ResourceRefR4 prevTemplate = ResourceRefR4.NONE_REF; 
     581 
     582                        @Override 
     583                        protected ImmList<ActivationChannel> compute() { 
     584 
     585                                ResourceRefR4 templateRef = getTemplate(); 
     586                                ResourceAccess templateAccess = getAccess().open(templateRef, null); 
     587                                ImmList<ActivationChannel> subelements =  
     588                                        CompositeElement.KEY_SUB_ELEMENTS.get(templateAccess); 
     589 
     590                                if (!this.prevTemplateElements.equals(subelements)) { 
     591                                         
     592                                        if (templateRef.equals(this.prevTemplate)) { 
     593                                                for (ActivationChannel channel : this.prevTemplateElements) { 
     594                                                        if (!subelements.contains(channel)) { 
     595                                                                LogicR3.fire(ElementH.this, null, null, null,  
     596                                                                                TemplateEventIds.TEMPLATE_ELEMENT_DELETED, 
     597                                                                                ResourceRefR4.PARENT_REF.append(templateRef.append(channel.getElementResource()))); 
     598                                                        } 
     599                                                } 
     600 
     601                                                for (ActivationChannel channel : subelements) { 
     602                                                        if (!this.prevTemplateElements.contains(channel)) { 
     603                                                                LogicR3.fire(ElementH.this, null, null, null,  
     604                                                                                TemplateEventIds.TEMPLATE_ELEMENT_ADDED, 
     605                                                                                templateRef.append(channel.getElementResource())); 
     606                                                        } 
     607                                                } 
     608                                        } else {                                                 
     609                                                this.prevTemplate = templateRef; 
     610                                        } 
     611 
     612                                        this.prevTemplateElements = subelements; 
     613                                } 
     614 
     615                                return subelements; 
     616                        } 
     617 
     618                } 
     619                return getBean().makeProp(templateSubElements.class); 
     620        } 
     621         
     622        /** 
     623         * Events related to changes in this element's template. 
     624         *  
     625         * @author deni 
     626         */ 
     627        public enum TemplateEventIds { 
     628                 
     629                /** 
     630                 * A new sub-element has been added to the template. 
     631                 */ 
     632                @EventParams({ ResourceRefR4.class }) 
     633                TEMPLATE_ELEMENT_ADDED, 
     634                 
     635                /** 
     636                 * A sub-element has been deleted from the template. 
     637                 */ 
     638                @EventParams({ ResourceRefR4.class }) 
     639                TEMPLATE_ELEMENT_DELETED; 
     640                 
     641                /** 
     642                 * The index of the ResourceRef parameter pointing to the sub-element 
     643                 * that was added/removed. 
     644                 */ 
     645                public static final int RESOURCE_REF_PARAM_INDEX = 0; 
     646        } 
     647 
    571648} 
  • modules/org.sophie2.main.func.templates/src/main/java/org/sophie2/main/func/templates/composite/CompositeTemplateLogic.java

     
     1package org.sophie2.main.func.templates.composite; 
     2 
     3import java.util.ArrayList; 
     4import java.util.HashMap; 
     5import java.util.List; 
     6 
     7import org.sophie2.base.model.book.ElementH; 
     8import org.sophie2.base.model.book.ElementH.TemplateEventIds; 
     9import org.sophie2.base.model.resources.r4.ResourceRefR4; 
     10import org.sophie2.base.model.resources.r4.access.ResourceAccess; 
     11import org.sophie2.base.model.resources.r4.changes.AutoAction; 
     12import org.sophie2.base.model.resources.r4.resources.ResourceH; 
     13import org.sophie2.core.mvc.EventFilterBuilder; 
     14import org.sophie2.core.mvc.OperationDef; 
     15import org.sophie2.core.mvc.events.EventR3; 
     16import org.sophie2.main.app.commons.util.TemplateUtil; 
     17 
     18/** 
     19 * Logic for handling events related to changes in the template of an element. 
     20 *  
     21 * @author deni 
     22 */ 
     23public enum CompositeTemplateLogic implements OperationDef { 
     24         
     25        /** 
     26         * Handles adding a new sub-element (sub-template) to the template of the source element. 
     27         * Creates a new sub-element by the sub-template. 
     28         */ 
     29        ON_TEMPLATE_SUBELEMENT_ADDED { 
     30 
     31                public void defineFilter(EventFilterBuilder filter) { 
     32                        filter.setSourceClass(ElementH.class); 
     33                        filter.setEventId(TemplateEventIds.TEMPLATE_ELEMENT_ADDED); 
     34                } 
     35 
     36                public boolean handle(EventR3 event) { 
     37                        ElementH source = event.getSource(ElementH.class); 
     38                        ResourceRefR4 templateRef = event.getEventParam(TemplateEventIds.RESOURCE_REF_PARAM_INDEX, 
     39                                        ResourceRefR4.class); 
     40                        ResourceAccess templateAccess = source.getAccess().open(templateRef, null); 
     41                        ElementH templateH = ResourceH.getHelper(templateAccess, ElementH.class); 
     42                         
     43                        ResourceRefR4 subelementToTemplate = ResourceRefR4.PARENT_REF.append(templateRef); 
     44                        boolean templateUsed = false; 
     45                        for (ElementH subelement : source.getSubElements()) { 
     46                                if (subelement.getTemplate().equals(subelementToTemplate)) { 
     47                                        templateUsed = true; 
     48                                        break; 
     49                                } 
     50                        } 
     51                         
     52                        if (!templateUsed) { 
     53                                TemplateUtil.templatePageElements(templateH, source, getAllSubelements(templateH),  
     54                                                new HashMap<ResourceRefR4, ResourceRefR4>()); 
     55                                AutoAction.registerEndChange(source.getAccess(), "Add elements by template."); 
     56                        } 
     57                         
     58                        return true; 
     59                } 
     60                 
     61                private List<ResourceRefR4> getAllSubelements(ElementH element) { 
     62                        List<ResourceRefR4> res = new ArrayList<ResourceRefR4>(); 
     63                        res.add(element.getRef()); 
     64                        for (ElementH subelement : element.getSubElements()) { 
     65                                res.addAll(getAllSubelements(subelement)); 
     66                        }                
     67                        return res; 
     68                } 
     69                 
     70        }, 
     71         
     72        /** 
     73         * Handles deleting a sub-element (sub-template) from the template of the source element. 
     74         * Deletes the element that was using the sub-template. 
     75         */ 
     76        ON_TEMPLATE_SUBELEMENT_DELETED { 
     77 
     78                public void defineFilter(EventFilterBuilder filter) { 
     79                        filter.setSourceClass(ElementH.class); 
     80                        filter.setEventId(TemplateEventIds.TEMPLATE_ELEMENT_DELETED);                    
     81                } 
     82 
     83                public boolean handle(EventR3 event) { 
     84                        ElementH source = event.getSource(ElementH.class); 
     85                        ResourceRefR4 templateRef = event.getEventParam(TemplateEventIds.RESOURCE_REF_PARAM_INDEX, 
     86                                        ResourceRefR4.class); 
     87                         
     88                        for (ElementH subelement : source.getSubElements()) { 
     89                                ResourceRefR4 subelementToTemplateRef = ResourceRefR4.getRelativeRef( 
     90                                                subelement.getRef(), templateRef); 
     91                                if (subelementToTemplateRef.equals(subelement.getTemplate())) { 
     92                                        source.removeSubElement(subelement.getRef().getThisChildRef(),  
     93                                                        "removing subelement", false); 
     94                                } 
     95                        } 
     96                        AutoAction.registerEndChange(source.getAccess(), "Delete elements from template."); 
     97                         
     98                        return true; 
     99                } 
     100                 
     101        } 
     102 
     103}