Ticket #2403: subelements_addition.patch
File subelements_addition.patch, 30.0 KB (added by boyanl, 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
2 2 3 3 import java.util.List; 4 4 5 import org.sophie2.base.commons.structures.ImmTreeList; 5 6 import org.sophie2.base.commons.util.ImmList; 6 7 import org.sophie2.base.commons.util.ImmMap; 7 8 import org.sophie2.base.commons.util.NaiveImmList; 9 import org.sophie2.base.commons.util.ImmMap.ImmEntry; 8 10 import org.sophie2.base.dnd.DndTransferable; 9 11 import org.sophie2.base.model.book.BookH; 10 12 import org.sophie2.base.model.book.ElementGroupH; … … 42 44 * 43 45 * @author diana, mira 44 46 */ 45 public class TemplateUtil { 46 47 public class TemplateUtil { 47 48 /** 48 49 * Creates template element and adds it to the library. 49 50 * 50 51 * @param templateTitle 51 52 * The title of the new template. 52 * @param keys 53 * The keys that should be in mode "use-teplate" 53 * @param resourcesKeys 54 * A map describing which keys should be *excluded* from the template for which elements 55 * @param toDelete 56 * A list of references to elements which should be deleted (they have all their keys and sub-elements unchecked) 54 57 * @param templatedElement 55 58 * The element that we want to use as a template, 56 59 * that is the one that will be copied as template. … … 61 64 * @param createDefaultTemplate 62 65 * True if the template will be used as a default one, false otherwise. 63 66 */ 64 public static void createTemplate(final String templateTitle, final ImmList<TemplatedKey<?>> keys, 65 ElementView templatedElement, final ResourceRefR4 templateRef, boolean isSign, 67 public static void createTemplate(final String templateTitle, 68 final ImmMap<ResourceRefR4, ImmList<Key<?>>> resourcesKeys, 69 final ImmList<ResourceRefR4> toDelete, 70 final ElementView templatedElement, final ResourceRefR4 templateRef, boolean isSign, 66 71 final boolean createDefaultTemplate) { 67 72 68 73 assert templatedElement != null; 69 74 70 75 BookView bookView = templatedElement.getBookView(); 71 76 BookH book = bookView.model().get(); 77 72 78 ResourceAccess bookAccess = bookView.getAccess(); 73 79 74 80 final ResourceRefR4 ref; … … 100 106 final String kind = templatedElement.model().get().getKind(); 101 107 final ResourceRefList newTemplates = 102 108 templates.contains(templateRef) ? templates : templates.add(templateRef); 103 104 new AutoAction(description, isSign) { 109 110 //auto-action which changes the keys of the root element/its sub-elements 111 String changeKeysDescription = "Change the template's elements' keys"; 112 new AutoAction(changeKeysDescription, false) { 105 113 @Override 106 114 public void performAuto() { 107 115 getChanger().copyResource(relativeRef, templateRef); 108 116 getChanger().setRaw(templatesKey, newTemplates); 109 117 110 118 ResourceChanger subCh = getChanger().getSub(templateRef); 119 111 120 subCh.setRaw(ResourceR4.KEY_TITLE, templateTitle); 121 subCh.setRaw(ResourceR4.KEY_KIND, kind); 112 122 113 for (TemplatedKey<?> key : keys) { 114 subCh.setRaw(((TemplatedKey<?>) key).getApplyKey(), false); 115 } 116 subCh.setRaw(ResourceR4.KEY_KIND, kind); 123 if (resourcesKeys != null) { 124 for (ImmEntry<ResourceRefR4, ImmList<Key<?>>> entry : resourcesKeys) { 125 ResourceChanger subChanger = (entry.getKey().equals(ref) ? subCh : subCh.getSub (entry.getKey()) ); 126 127 for (Key<?> key : entry.getValue()) { 128 subChanger.setRaw(((TemplatedKey<?>) key).getApplyKey(), 129 false); 130 } 131 } 132 } 117 133 if(createDefaultTemplate) { 118 134 getChanger().setRaw(defaultTemplateKey, templateRef); 119 135 } 120 136 } 121 137 122 138 }.register(bookAccess); 139 140 ImmList<ResourceRefR4> rootsToDelete = ImmTreeList.<ResourceRefR4>create(); 141 142 /* 143 * Adds the references to be removed. We're looking for references who should be deleted, 144 * but their parent shouldn't be (so when we remove them, we'll remove all their sub-elements as well, 145 * which is what we need). Note that iterRef.getParentRef() will never equal the ref of the element we're templating, 146 * because we pass the root as an absolute reference and the sub-elements as relative ones. 147 */ 148 for (ResourceRefR4 iterRef : toDelete) { 149 if (!toDelete.contains(iterRef.getParentRef()) && !iterRef.equals(ref) ) { 150 rootsToDelete = rootsToDelete.add(iterRef); 151 } 152 } 153 154 final ImmList<ResourceRefR4> finalRootsToDelete = rootsToDelete; 155 156 /* 157 * Auto-action which deletes the unneeded sub-elements (also removes their activation channels from the 158 * corresponding parent's KEY_SUB_ELEMENTS key). 159 */ 160 new AutoAction (description, isSign) { 161 @Override 162 public void performAuto () { 163 ResourceChanger subCh = getChanger().getSub(templateRef); 164 165 for (ResourceRefR4 iterRef : finalRootsToDelete) { 166 ResourceRefR4 parent = iterRef.getParentRef(), parentToIterRef = ResourceRefR4.getRelativeRef(parent, iterRef); 167 ResourceChanger parentChanger = subCh.getSub(parent); 168 169 ImmList<ActivationChannel> channels = parentChanger.getRaw(CompositeElement.KEY_SUB_ELEMENTS); 170 171 for (ActivationChannel channel : channels) { 172 ResourceRefR4 elRes = channel.getElementResource(); 173 174 if (elRes.equals(parentToIterRef) ) { 175 channels = channels.remove(channel); 176 break; 177 } 178 } 179 parentChanger.setRaw(CompositeElement.KEY_SUB_ELEMENTS, channels); 180 181 subCh.removeResource(iterRef); 182 } 183 } 184 }.register(bookAccess); 123 185 } 124 186 125 187 /** … … 362 424 final ResourceRefR4 templateRef = templateH.getRef(); 363 425 final String frameKind = templateH.getKind(); 364 426 final NaiveImmList<TemplatedKey<?>> immKeys = templateH.getApplicableTemplatedKeys(); 365 427 366 428 final ResourceRefR4 frameRef = ResourceRefR4.generateRandomSub(FrameH.NAME_PREFIX); 367 429 final String frameTitle = templateH.getTitle(); 368 430 -
modules/org.sophie2.main.func.text/src/main/java/org/sophie2/main/func/text/utils/TextChainUtils.java
525 525 526 526 frameTemplRef = ResourceRefR4.generateRandomSub(FrameH.NAME_PREFIX); 527 527 String templateTitle = getTemplateName(frameH.getTitle(), bookH, true); 528 TemplateUtil.createTemplate(templateTitle, NaiveImmList.<TemplatedKey<?>>getEmpty(),528 TemplateUtil.createTemplate(templateTitle, null, NaiveImmList.<ResourceRefR4>getEmpty(), 529 529 frameView, frameTemplRef, false, false); 530 530 if (frameH instanceof HeadTextFrameH) { 531 531 HeadTextFrameH thisHeadH = (HeadTextFrameH) frameH; … … 586 586 || ResourceRefR4.NONE_REF.equals(res)) { 587 587 res = ResourceRefR4.generateRandomSub(PageH.NAME_PREFIX); 588 588 String templateTitle = getTemplateName(pageH.getTitle(), book, false); 589 TemplateUtil.createTemplate(templateTitle, 590 NaiveImmList.<TemplatedKey<?>>getEmpty(),page, res, false, false);589 TemplateUtil.createTemplate(templateTitle, null, NaiveImmList.<ResourceRefR4>getEmpty(), 590 page, res, false, false); 591 591 } else { 592 592 res = ResourceRefR4.getRelativeRef(book.getRef(), pageH.getRef().append(res)); 593 593 } -
modules/org.sophie2.main.app.halos/src/main/java/org/sophie2/main/app/halos/shared/AddTemplateHaloButton.java
Property changes on: modules\org.sophie2.main.app.commons\src\test\java\org\sophie2\main\app\commons\dialogs ___________________________________________________________________ Added: svn:ignore + TemplateDialogDemo.java
3 3 import java.util.HashMap; 4 4 import java.util.List; 5 5 import java.util.Map; 6 import java.util.Map.Entry; 6 7 7 8 import javax.swing.JComponent; 8 9 9 10 import org.sophie2.base.commons.skin.IconId; 10 11 import org.sophie2.base.commons.skin.IconsSet; 11 12 import org.sophie2.base.commons.structures.ImmTreeList; 13 import org.sophie2.base.commons.structures.ImmTreeMap; 12 14 import org.sophie2.base.commons.util.ImageUtil; 13 15 import org.sophie2.base.commons.util.ImmList; 14 import org.sophie2.base.commons.util. NaiveImmList;16 import org.sophie2.base.commons.util.ImmMap; 15 17 import org.sophie2.base.dialogs.DialogManager; 16 18 import org.sophie2.base.halos.ClickHaloButton; 17 19 import org.sophie2.base.halos.HaloButton; 18 20 import org.sophie2.base.model.book.BookH; 21 import org.sophie2.base.model.book.ElementH; 19 22 import org.sophie2.base.model.book.FrameH; 20 23 import org.sophie2.base.model.book.PageH; 21 24 import org.sophie2.base.model.book.resource.r4.BookR4; … … 24 27 import org.sophie2.base.model.resources.r4.ResourceRefR4; 25 28 import org.sophie2.base.model.resources.r4.access.ResourceAccess; 26 29 import org.sophie2.base.model.resources.r4.keys.Key; 27 import org.sophie2.base.model.resources.r4.keys.TemplatedKey;28 30 import org.sophie2.base.model.resources.r4.resources.ResourceR4; 29 31 import org.sophie2.base.skins.SkinElementId; 30 32 import org.sophie2.base.visual.skins.ElementSkinPart; … … 119 121 120 122 final ResourceRefR4 ref; 121 123 final String kind; 122 final Class<? extends ResourceR4> resourceClass;123 124 final String defaultTitle; 124 125 final Key<ResourceRefList> templatesKey; 125 126 final String resourcePrefix; 127 //Class<? extends ResourceR4> resourceClass; 126 128 if (frameView != null) { 127 129 templatedElem = frameView; 128 130 // preparing frame template 129 131 ref = frameView.getAccess().getRef(); 130 132 kind = frameView.model().get().getResourceKind(); 131 resourceClass = ResourceR4.getClassByKind(kind);133 //resourceClass = ResourceR4.getClassByKind(kind); 132 134 defaultTitle = frameView.model().get().getTitle(); 133 135 templatesKey = BookR4.KEY_FRAME_TEMPLATES; 134 136 resourcePrefix = FrameH.NAME_PREFIX; … … 137 139 // preparing page template 138 140 ref = pwa.getRootPageView().getAccess().getRef(); 139 141 kind = PageR4.KIND; 140 resourceClass = PageR4.class;142 //resourceClass = PageR4.class; 141 143 defaultTitle = pwa.getRootPageView().model().get().getTitle(); 142 144 templatesKey = BookR4.KEY_PAGE_TEMPLATES; 143 145 resourcePrefix = PageH.NAME_PREFIX; 144 146 } 147 148 ElementH templateElemH = templatedElem.model().get(); 149 145 150 String title = source.defaultTitles.get(ref); 146 151 if (title == null) { 147 152 title = defaultTitle + " template"; 148 153 } 149 154 150 TemplateInfo info = new TemplateInfo(title, ResourceR4.getKnownKeys(resourceClass).values(), false);155 TemplateInfo info = new TemplateInfo(title, templateElemH, false); 151 156 TemplateDialog.Input input = new TemplateDialog.Input(pwa.swingComponent().get(), info); 157 152 158 153 159 TemplateInfo res = DialogManager.get().showDialog(input); 154 160 … … 160 166 final ResourceRefR4 templateRef = (foundResourceRef == null ? ResourceRefR4 161 167 .generateRandomSub(resourcePrefix) : foundResourceRef); 162 168 final String templateTitle = res.getTitle(); 163 final ImmList<Key<?>> keys = ImmTreeList.<Key<?>>create(res.getKeyStates().keySet()); 164 final ImmList<Boolean> values = ImmTreeList.<Boolean>create(res.getKeyStates().values()); 169 165 170 166 171 boolean proceed = true; 167 172 if (foundResourceRef != null) { … … 176 181 } 177 182 } 178 183 179 ImmList<TemplatedKey<?>> notToTemplate = NaiveImmList.<TemplatedKey<?>>getEmpty(); 180 for (int i = 0; i < keys.size(); i++) { 181 Key<?> key = keys.get(i); 182 Boolean value = values.get(i); 183 if (!value && key instanceof TemplatedKey<?>) { 184 notToTemplate = notToTemplate.add((TemplatedKey<?>)key); 184 if (proceed) { 185 //convert the output from the dialog to immutables 186 187 Map<ResourceRefR4, Map<Key<?>, Boolean>> keyStates = res.getKeyStates(); 188 189 //this map contains a list of keys for each resourceref which *shouldn't* be included in the template 190 Map<ResourceRefR4, ImmList<Key<?>>> temp = new HashMap<ResourceRefR4, ImmList<Key<?>>> (); 191 192 for (Entry<ResourceRefR4, Map<Key<?>, Boolean>> entry : keyStates.entrySet()) { 193 ImmList<Key<?>> listRes = ImmTreeList.<Key<?>>create(); 194 for (Entry<Key<?>, Boolean> keyEntry : entry.getValue().entrySet()) { 195 if (keyEntry.getValue() == false) { 196 listRes = listRes.add(keyEntry.getKey()); 197 } 198 } 199 temp.put(entry.getKey(), listRes); 185 200 } 186 } 187 if (proceed) { 188 TemplateUtil.createTemplate(templateTitle, notToTemplate, 201 202 ImmMap<ResourceRefR4, ImmList<Key<?>>> immKeyStates = ImmTreeMap.<ResourceRefR4, ImmList<Key<?>>>create (temp); 203 ImmList<ResourceRefR4> deleteList = ImmTreeList.<ResourceRefR4>create(res.getDeleteList()); 204 205 TemplateUtil.createTemplate(templateTitle, immKeyStates, deleteList, 189 206 templatedElem, templateRef, true, res.getIsDefault()); 190 207 } 191 208 } … … 258 275 DialogManager.get().showDialog(new MessageDialogInput(parent, message)); 259 276 } 260 277 } 278 279 -
modules/org.sophie2.main.app.commons/src/test/java/org/sophie2/main/app/commons/dialogs/TemplateDialogDemo.java
1 package org.sophie2.main.app.commons.dialogs;2 3 import javax.swing.SwingUtilities;4 5 import org.sophie2.base.bound.BoundModule;6 import org.sophie2.base.commons.BaseCommonsModule;7 import org.sophie2.base.model.book.BaseModelBookModule;8 import org.sophie2.base.model.book.frame.FrameR4;9 import org.sophie2.base.model.resources.r4.BaseModelResourcesR4Module;10 import org.sophie2.base.model.resources.r4.resources.ResourceR4;11 import org.sophie2.base.skins.BaseSkinsModule;12 import org.sophie2.base.visual.BaseVisualModule;13 import org.sophie2.core.modularity.FakeModuleRegistry;14 import org.sophie2.core.mvc.CoreMvcModule;15 import org.sophie2.main.app.commons.MainAppModule;16 import org.sophie2.main.app.commons.dialogs.TemplateDialog.SwingDialog;17 18 /**19 * A demo for {@link TemplateDialog}.20 *21 * @author jani22 */23 public class TemplateDialogDemo {24 /**25 * Entry point for demo.26 *27 * @param args28 * Not used.29 */30 @SuppressWarnings("unchecked")31 public static void main(String[] args) {32 System.setProperty("sophie2.development", "true");33 FakeModuleRegistry.start(CoreMvcModule.class, BaseSkinsModule.class,34 BaseCommonsModule.class,35 BaseVisualModule.class, BoundModule.class, MainAppModule.class,36 BaseModelResourcesR4Module.class, BaseModelBookModule.class);37 SwingUtilities.invokeLater(new Runnable() {38 public void run() {39 SwingDialog.get().show(40 (new TemplateInfo("Template test", ResourceR441 .getKnownKeys(FrameR4.class).values(), false)), null);42 System.exit(0);43 }44 });45 }46 } -
modules/org.sophie2.main.app.commons/src/main/java/org/sophie2/main/app/commons/dialogs/TemplateDialog.java
10 10 import java.awt.event.FocusListener; 11 11 import java.awt.event.MouseAdapter; 12 12 import java.awt.event.MouseEvent; 13 import java.util.ArrayList; 14 import java.util.Collection; 13 15 import java.util.Enumeration; 14 16 import java.util.HashMap; 17 import java.util.List; 15 18 import java.util.Map; 16 import java.util.Set;17 19 import java.util.Map.Entry; 18 20 19 21 import javax.swing.JButton; … … 31 33 import javax.swing.tree.TreePath; 32 34 33 35 import org.sophie2.base.commons.gui.TriStateCheckBox; 36 import org.sophie2.base.commons.gui.TriStateCheckBox.State; 34 37 import org.sophie2.base.commons.util.ImmList; 35 38 import org.sophie2.base.commons.util.NaiveImmList; 36 39 import org.sophie2.base.dialogs.Dialog; 37 40 import org.sophie2.base.dialogs.DialogInput; 41 import org.sophie2.base.model.book.ElementH; 42 import org.sophie2.base.model.resources.r4.ResourceRefR4; 38 43 import org.sophie2.base.model.resources.r4.keys.CompositeKey; 39 44 import org.sophie2.base.model.resources.r4.keys.Key; 40 45 import org.sophie2.base.model.resources.r4.keys.TemplatedKey; 46 import org.sophie2.base.model.resources.r4.resources.ResourceR4; 41 47 import org.sophie2.core.mvc.EventFilterBuilder; 42 48 import org.sophie2.core.mvc.OperationDef; 43 49 import org.sophie2.core.mvc.events.EventR3; 50 import org.sophie2.main.app.commons.dialogs.TemplateDialog.CheckBoxTree.CheckNode; 44 51 45 52 /** 46 53 * The dialog which is used to add a page or frame as a template. It generates a … … 457 464 protected static class SwingDialog { 458 465 459 466 /** 467 * An inner class that serves as a key in the map that matches a given key from a given resource to a node in the tree 468 */ 469 private static class NodeCacheKey { 470 private Key<?> key; 471 private ResourceRefR4 resourceRef; 472 473 NodeCacheKey (Key<?> key, ResourceRefR4 resourceRef) { 474 this.key = key; 475 this.resourceRef = resourceRef; 476 } 477 478 @Override 479 public int hashCode() { 480 final int prime = 31; 481 int result = 1; 482 result = prime * result + ((this.key == null) ? 0 : this.key.hashCode()); 483 result = prime * result 484 + ((this.resourceRef == null) ? 0 : this.resourceRef.hashCode()); 485 return result; 486 } 487 488 @Override 489 public boolean equals(Object obj) { 490 if (this == obj) { 491 return true; 492 } 493 if (obj == null) { 494 return false; 495 } 496 if (this.getClass() != obj.getClass()) { 497 return false; 498 } 499 500 NodeCacheKey other = (NodeCacheKey) obj; 501 if (this.key == null) { 502 if (other.key != null) 503 return false; 504 } else if (!this.key.equals(other.key)) 505 return false; 506 if (this.resourceRef == null) { 507 if (other.resourceRef != null) { 508 return false; 509 } 510 } else if (!this.resourceRef.equals(other.resourceRef)) { 511 return false; 512 } 513 return true; 514 } 515 516 517 } 518 519 520 /** 460 521 * The instance of the book template helper. 461 522 */ 523 462 524 private static SwingDialog instance; 463 525 464 526 private static final int PANEL_HEIGHT = 300; … … 481 543 private JButton selectAllButton = null; 482 544 private JButton selectNoneButton = null; 483 545 private CheckBoxTree checkBoxTree = null; 484 private Map<Key<?>, CheckBoxTree.CheckNode> keyMap = null; 485 private HashMap<Key<?>, Boolean> keyStates = null; 486 private CheckBoxTree.CheckNode isDefaultTemplateNode = null; 546 private Map <NodeCacheKey, CheckNode> keyToNodeMap = null; 547 private Map <ResourceRefR4, Map <Key<?>, Boolean>> keyStates = null; 548 private Map <ResourceRefR4, CheckNode> resourcesCheckNodes; 549 private CheckNode isDefaultTemplateNode = null; 487 550 488 551 /** 489 552 * Holds the initial info for the dialog. … … 539 602 JPanel bottom = new JPanel(); 540 603 bottom.add(getOkButton()); 541 604 bottom.add(getCancelButton()); 542 605 543 606 this.treeScrollPane = new JScrollPane(); 544 607 545 608 this.mainPanel.add(top, BorderLayout.NORTH); … … 615 678 String title = getTitleField().getText(); 616 679 boolean isDefault = SwingDialog.this.isDefaultTemplateNode.isSelected(); 617 680 618 SwingDialog.this.keyStates = new HashMap<Key<?>, Boolean>(); 619 for (Entry<Key<?>, CheckBoxTree.CheckNode> entry : SwingDialog.this.keyMap.entrySet()) { 620 SwingDialog.this.keyStates.put(entry.getKey(), entry.getValue().isSelected()); 681 for (Entry<ResourceRefR4, Map<Key<?>, Boolean>> entry : SwingDialog.this.keyStates.entrySet()) { 682 for (Entry<Key<?>, Boolean> keyEntry : entry.getValue().entrySet()) { 683 CheckNode node = SwingDialog.this.keyToNodeMap.get (new NodeCacheKey (keyEntry.getKey(), entry.getKey()) ); 684 if (node != null) { 685 keyEntry.setValue(node.isSelected()); 686 } 687 } 688 } 689 690 List<ResourceRefR4> toDelete = new ArrayList<ResourceRefR4> (); 691 for (Entry<ResourceRefR4, CheckNode> entry : SwingDialog.this.resourcesCheckNodes.entrySet ()) { 692 if (entry.getValue().getState() == State.UNCHECKED) { 693 toDelete.add(entry.getKey ()); 694 } 621 695 } 696 697 TemplateInfo res = new TemplateInfo(title, SwingDialog.this.keyStates, toDelete, isDefault); 622 698 623 TemplateInfo res = new TemplateInfo(title, SwingDialog.this.keyStates, isDefault);624 625 699 setResultInfo(res); 626 700 SwingDialog.this.dialog.setVisible(false); 627 701 } … … 693 767 return this.selectNoneButton; 694 768 } 695 769 770 private void constructTreeFromElement (ElementH element, CheckNode parentNode, ResourceRefR4 rootRef) { 771 ResourceRefR4 resourceRef = element.getRef(); 772 773 /* 774 * Hold an absolute reference to the root element and relative references to the sub-elements 775 */ 776 if (!resourceRef.equals(rootRef)) { 777 resourceRef = ResourceRefR4.getRelativeRef (rootRef, resourceRef); 778 } 779 String title = element.getTitle(); 780 CheckNode thisNode; 781 782 if (parentNode == null) { //if no parent node is given, create a new tree and set up its scroll pane 783 this.checkBoxTree = new CheckBoxTree (this.initialInfo.getTitle()); 784 this.isDefaultTemplateNode = this.checkBoxTree.addNode (DEFAULT_TEMPLATE_CHECK_BOX_LABEL); 785 this.treeScrollPane.setViewportView(this.checkBoxTree); 786 thisNode = this.checkBoxTree.getRootNode(); 787 } 788 else { 789 thisNode = this.checkBoxTree.addNode (title, parentNode); 790 } 791 792 this.resourcesCheckNodes.put(resourceRef, thisNode); 793 Class <? extends ResourceR4> resourceClass = ResourceR4.getClassByKind( element.getKind() ); 794 Collection<Key<?>> elementKeys = ResourceR4.getKnownKeys(resourceClass).values(); 795 796 Map<Key<?>, Boolean> elementKeyMap = new HashMap <Key<?>, Boolean> (); 797 this.keyStates.put(resourceRef, elementKeyMap); 798 799 boolean initialKeyState = true; 800 for (Key<?> key : elementKeys) { 801 elementKeyMap.put(key, true); 802 } 803 804 for (Key<?> key: elementKeys) { 805 addKey (thisNode, resourceRef, key, initialKeyState, this.keyToNodeMap, elementKeys, 1); 806 } 807 808 List<ElementH> childElems = element.getSubElements (); 809 for (ElementH child: childElems) 810 constructTreeFromElement(child, thisNode, rootRef); 811 } 696 812 /** 697 813 * Performs setup of the dialog - sets up the title and the panel with 698 814 * the checkboxes. … … 700 816 private void setup() { 701 817 getTitleField().setText(this.initialInfo.getTitle()); 702 818 703 this.keyMap = new HashMap<Key<?>, CheckBoxTree.CheckNode>(); 704 this.keyStates = new HashMap<Key<?>, Boolean>(); 705 706 this.checkBoxTree = new CheckBoxTree (this.initialInfo.getTitle ()); 707 this.isDefaultTemplateNode = this.checkBoxTree.addNode (DEFAULT_TEMPLATE_CHECK_BOX_LABEL); 708 709 this.treeScrollPane.setViewportView(this.checkBoxTree); 710 Set<Key<?>> keys = this.initialInfo.getKeyStates().keySet(); 711 for (final Key<?> key : keys) { 712 addKey(this.checkBoxTree.getRootNode(), key, this.initialInfo.getKeyStates().get(key), this.keyMap, keys, 1); 713 } 714 819 this.keyToNodeMap = new HashMap<NodeCacheKey, CheckNode>(); 820 this.keyStates = new HashMap<ResourceRefR4, Map<Key<?>, Boolean>> (); 821 this.resourcesCheckNodes = new HashMap<ResourceRefR4, CheckNode> (); 822 823 ElementH rootElement = this.initialInfo.getRootElement (); 824 constructTreeFromElement (rootElement, null, rootElement.getRef()); 715 825 this.checkBoxTree.expandPath(new TreePath (this.checkBoxTree.getRootNode())); 716 826 } 717 827 … … 720 830 * adds the key if he's at the appropriate level 721 831 */ 722 832 @SuppressWarnings("synthetic-access") 723 private void addKey (CheckBoxTree.CheckNode parent, final Key<?> key, final Boolean state, Map<Key<?>, CheckBoxTree.CheckNode> keyControlMap, Set<Key<?>> keys, int level) { 833 834 private void addKey (CheckNode parent, final ResourceRefR4 resource, final Key<?> key, final Boolean state, Map<NodeCacheKey, CheckNode> keyControlMap, Collection<Key<?>> keys, int level) { 724 835 if (key instanceof TemplatedKey) { 725 836 if (!HIDDEN_KEYS.contains((TemplatedKey) key) && key.getParts().size() == level) { 726 837 ImmList<String> parts = key.getParts(); 727 838 String text = parts.get(parts.size() - 1); 728 729 Check BoxTree.CheckNode node = this.checkBoxTree.addNode(text, parent);839 840 CheckNode node = this.checkBoxTree.addNode(text, parent); 730 841 node.setSelected(state); 731 keyControlMap.put( key, node);842 keyControlMap.put(new NodeCacheKey(key, resource), node); 732 843 } 733 844 } 734 845 735 846 else if (key instanceof CompositeKey && key.getParts().size() == level) { 736 847 ImmList<String> parts = key.getParts(); 737 848 String text = parts.get(parts.size() - 1); 738 739 Check BoxTree.CheckNode node = this.checkBoxTree.addNode(text, parent);740 849 850 CheckNode node = this.checkBoxTree.addNode(text, parent); 851 741 852 String prefix = key.getId().concat(Key.SEPARATOR); 742 853 for (final Key<?> subKey : keys) { 743 854 String keyId = subKey.getId(); 744 855 if (keyId.startsWith(prefix)) { 745 Boolean subState = this. initialInfo.getKeyStates().get(subKey);746 addKey(node, subKey, subState, this.keyMap, keys, level + 1);856 Boolean subState = this.keyStates.get(resource).get(subKey); 857 addKey(node, resource, subKey, subState, this.keyToNodeMap, keys, level + 1); 747 858 } 748 859 } 749 750 860 } 751 861 } 752 862 -
modules/org.sophie2.main.app.commons/src/main/java/org/sophie2/main/app/commons/dialogs/TemplateInfo.java
1 1 package org.sophie2.main.app.commons.dialogs; 2 2 3 import java.util.Collection;4 3 import java.util.HashMap; 4 import java.util.List; 5 5 import java.util.Map; 6 6 7 import org.sophie2.base.model.book.ElementH; 8 import org.sophie2.base.model.resources.r4.ResourceRefR4; 7 9 import org.sophie2.base.model.resources.r4.keys.Key; 8 10 import org.sophie2.core.prolib.annot.Immutable; 9 11 … … 24 26 25 27 private String title; 26 28 private boolean isDefault; 27 private Map<Key<?>, Boolean> keyStates; 29 30 /** 31 * Represents the key states of the element we're creating the template from and its subelements. 32 */ 33 private Map <ResourceRefR4, Map <Key<?>, Boolean>> keyStates; 34 35 /** 36 * Contains which elements should be deleted (not included in the template as subelements - the ones with all their keys and 37 * subelements unchecked). 38 */ 39 private List<ResourceRefR4> resourcesToDelete; 40 41 /** 42 * Root element (the one we're creating the template from) 43 */ 44 private ElementH rootElement; 28 45 29 46 /** 30 47 * @return … … 49 66 * @return 50 67 * A {@link Map} containing the states. 51 68 */ 52 public Map <Key<?>, Boolean> getKeyStates() {69 public Map <ResourceRefR4, Map <Key<?>, Boolean>> getKeyStates() { 53 70 return this.keyStates; 54 71 } 55 72 56 73 /** 74 * @return 75 * The root element from which the info is constructed. 76 */ 77 public ElementH getRootElement () { 78 return this.rootElement; 79 } 80 81 /** 82 * @return 83 * The list of references to resources which should be deleted from the template 84 */ 85 public List<ResourceRefR4> getDeleteList () { 86 return this.resourcesToDelete; 87 } 88 89 /** 57 90 * A default constructor. 58 91 */ 59 92 private TemplateInfo() { 60 93 this.title = ""; 61 this.keyStates = new HashMap< Key<?>, Boolean>();94 this.keyStates = new HashMap<ResourceRefR4, Map<Key<?>, Boolean>>(); 62 95 this.isDefault = false; 63 96 } 64 97 … … 69 102 * The title of the template. 70 103 * @param keyStates 71 104 * The initial states of the keys. 105 * @param toDelete 106 * The initial resources which should be deleted 72 107 * @param isDefault 73 108 * The boolean value for the usage of the template. 74 109 */ 75 public TemplateInfo(String title, Map <Key<?>, Boolean> keyStates, boolean isDefault) {110 public TemplateInfo(String title, Map <ResourceRefR4, Map <Key<?>, Boolean>> keyStates, List <ResourceRefR4> toDelete, boolean isDefault) { 76 111 this.title = title; 77 112 this.keyStates = keyStates; 113 this.resourcesToDelete = toDelete; 78 114 this.isDefault = isDefault; 79 115 } 80 116 117 81 118 /** 82 * Constructor .119 * Constructor 83 120 * 84 121 * @param title 85 * 86 * @param keys87 * The initial keys - true is used as a default state.88 * @param isDefault 89 * The boolean value for theusage of the template.122 * The title of the template. 123 * @param rootElement 124 * The element to construct the info from. 125 * @param isDefault 126 * The boolean value for usage of the template. 90 127 */ 91 public TemplateInfo(String title, Collection<Key<?>> keys, boolean isDefault) { 128 129 public TemplateInfo (String title, ElementH rootElement, boolean isDefault) { 92 130 this.title = title; 93 131 this.isDefault = isDefault; 94 this.keyStates = new HashMap<Key<?>, Boolean>(); 95 for (Key<?> key : keys) { 96 this.keyStates.put(key, true); 97 } 132 this.rootElement = rootElement; 98 133 } 99 } 100 No newline at end of file 134 135 } 136 137