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
109 109 final ResourceRefList newTemplates = 110 110 templates.contains(templateRef) ? templates : templates.add(templateRef); 111 111 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) { 115 133 @Override 116 134 public void performAuto() { 117 135 getChanger().copyResource(relativeRef, templateRef); … … 136 154 if(createDefaultTemplate) { 137 155 getChanger().setRaw(defaultTemplateKey, templateRef); 138 156 } 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 the161 * corresponding parent's KEY_SUB_ELEMENTS key).162 */163 new AutoAction (description, isSign) {164 @Override165 public void performAuto () {166 ResourceChanger subCh = getChanger().getSub(templateRef);167 157 168 158 for (ResourceRefR4 iterRef : finalRootsToDelete) { 169 159 ResourceRefR4 parent = iterRef.getParentRef(), parentToIterRef = ResourceRefR4.getRelativeRef(parent, iterRef); … … 184 174 subCh.removeResource(iterRef); 185 175 } 186 176 } 177 187 178 }.register(bookAccess); 179 180 181 182 183 // new AutoAction (description, isSign) { 184 // @Override 185 // public void performAuto () { 186 // 187 // } 188 // } 189 // }.register(bookAccess); 188 190 } 189 191 190 192 /** … … 369 371 } 370 372 } 371 373 372 p rivatestatic void templatePageElements(ElementH templateH, ElementH parent,374 public static void templatePageElements(ElementH templateH, ElementH parent, 373 375 List<ResourceRefR4> newElements, Map<ResourceRefR4, ResourceRefR4> existing) { 374 376 377 // TODO: make sure KEY_TEMPLATE is set for groups, too 378 375 379 if (!newElements.contains(templateH.getRef()) && !existing.containsKey(templateH.getRef())) { 376 380 return; 377 381 } … … 435 439 final String frameKind = templateH.getKind(); 436 440 final NaiveImmList<TemplatedKey<?>> immKeys = templateH.getApplicableTemplatedKeys(); 437 441 438 final ResourceRefR4 frameRef = ResourceRefR4.generateRandomSub(FrameH.NAME_PREFIX);442 final ResourceRefR4 frameRef = templateH.getRef().getThisChildRef(); 439 443 final String frameTitle = templateH.getTitle(); 440 444 441 445 final ResourceRefR4 parentRef = parent.getRef(); -
modules/org.sophie2.main.func.templates/src/main/java/org/sophie2/main/func/templates/MainFuncTemplatesModule.java
19 19 import org.sophie2.main.app.menus.file.SaveBookAsTemplateItem; 20 20 import org.sophie2.main.func.templates.book.BookTemplatesLogic; 21 21 import org.sophie2.main.func.templates.book.BookTemplatesPalette; 22 import org.sophie2.main.func.templates.composite.CompositeTemplateLogic; 22 23 import org.sophie2.main.func.templates.frame.FrameTemplateLogic; 23 24 import org.sophie2.main.func.templates.frame.FrameTemplatesPalette; 24 25 import org.sophie2.main.func.templates.page.ApplyPageTemplateDialog; … … 62 63 SimpleOperation.fillExtensions(res, FrameTemplateLogic.class); 63 64 SimpleOperation.fillExtensions(res, PageTemplateLogic.class); 64 65 SimpleOperation.fillExtensions(res, BookTemplatesLogic.class); 65 66 SimpleOperation.fillExtensions(res, CompositeTemplateLogic.class); 67 66 68 res.add(new SimpleSophieExtension<Dialog>(Dialog.class, 67 69 new ApplyPageTemplateDialog())); 68 70 AutoVisualProvider.fillExtensions(res, ApplyPageTemplateSwingDialog.class); -
modules/org.sophie2.base.model.book/src/main/java/org/sophie2/base/model/book/ElementH.java
5 5 6 6 import org.sophie2.base.commons.structures.ImmTreeList; 7 7 import org.sophie2.base.commons.util.ImmList; 8 import org.sophie2.base.commons.util.NaiveImmList; 8 9 import org.sophie2.base.commons.util.position.ImmArea; 9 10 import org.sophie2.base.commons.util.position.ImmInsets; 10 11 import org.sophie2.base.commons.util.position.ImmMatrix; … … 33 34 import org.sophie2.base.model.resources.r4.resources.ResourceH; 34 35 import org.sophie2.base.model.resources.r4.resources.ResourceR4; 35 36 import org.sophie2.core.logging.SophieLog; 37 import org.sophie2.core.mvc.EventParams; 38 import org.sophie2.core.mvc.LogicR3; 36 39 import org.sophie2.core.prolib.impl.AutoProperty; 37 40 import org.sophie2.core.prolib.interfaces.Prop; 38 41 … … 567 570 } 568 571 return null; 569 572 } 573 574 @SuppressWarnings("unused") 575 private Prop<ImmList<ActivationChannel>> templateSubElements() { 576 class templateSubElements extends AutoProperty<ImmList<ActivationChannel>> { 570 577 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 571 648 } -
modules/org.sophie2.main.func.templates/src/main/java/org/sophie2/main/func/templates/composite/CompositeTemplateLogic.java
1 package org.sophie2.main.func.templates.composite; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 7 import org.sophie2.base.model.book.ElementH; 8 import org.sophie2.base.model.book.ElementH.TemplateEventIds; 9 import org.sophie2.base.model.resources.r4.ResourceRefR4; 10 import org.sophie2.base.model.resources.r4.access.ResourceAccess; 11 import org.sophie2.base.model.resources.r4.changes.AutoAction; 12 import org.sophie2.base.model.resources.r4.resources.ResourceH; 13 import org.sophie2.core.mvc.EventFilterBuilder; 14 import org.sophie2.core.mvc.OperationDef; 15 import org.sophie2.core.mvc.events.EventR3; 16 import 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 */ 23 public 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 }