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
     
    22 
    33import java.util.List; 
    44 
     5import org.sophie2.base.commons.structures.ImmTreeList; 
    56import org.sophie2.base.commons.util.ImmList; 
    67import org.sophie2.base.commons.util.ImmMap; 
    78import org.sophie2.base.commons.util.NaiveImmList; 
     9import org.sophie2.base.commons.util.ImmMap.ImmEntry; 
    810import org.sophie2.base.dnd.DndTransferable; 
    911import org.sophie2.base.model.book.BookH; 
    1012import org.sophie2.base.model.book.ElementGroupH; 
     
    4244 *  
    4345 * @author diana, mira 
    4446 */ 
    45 public class TemplateUtil { 
    46  
     47public class TemplateUtil {      
    4748        /** 
    4849         * Creates template element and adds it to the library. 
    4950         *  
    5051         * @param templateTitle 
    5152         *                              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) 
    5457         * @param templatedElement 
    5558         *                              The element that we want to use as a template,  
    5659         *                              that is the one that will be copied as template. 
     
    6164         * @param createDefaultTemplate  
    6265         *              True if the template will  be used as a default one, false otherwise. 
    6366         */ 
    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,  
    6671                        final boolean createDefaultTemplate) { 
    6772 
    6873                assert templatedElement != null; 
    6974 
    7075                BookView bookView = templatedElement.getBookView(); 
    7176                BookH book = bookView.model().get(); 
     77                 
    7278                ResourceAccess bookAccess = bookView.getAccess(); 
    7379 
    7480                final ResourceRefR4 ref; 
     
    100106                final String kind = templatedElement.model().get().getKind(); 
    101107                final ResourceRefList newTemplates =  
    102108                        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) { 
    105113                        @Override 
    106114                        public void performAuto() { 
    107115                                getChanger().copyResource(relativeRef, templateRef); 
    108116                                getChanger().setRaw(templatesKey, newTemplates); 
    109117 
    110118                                ResourceChanger subCh = getChanger().getSub(templateRef); 
     119                                 
    111120                                subCh.setRaw(ResourceR4.KEY_TITLE, templateTitle); 
     121                                subCh.setRaw(ResourceR4.KEY_KIND, kind); 
    112122 
    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                                }                                        
    117133                                if(createDefaultTemplate) { 
    118134                                        getChanger().setRaw(defaultTemplateKey, templateRef); 
    119135                                } 
    120136                        } 
    121137 
    122138                }.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); 
    123185        } 
    124186 
    125187        /** 
     
    362424                        final ResourceRefR4 templateRef = templateH.getRef(); 
    363425                        final String frameKind = templateH.getKind(); 
    364426                        final NaiveImmList<TemplatedKey<?>> immKeys = templateH.getApplicableTemplatedKeys(); 
    365  
     427                         
    366428                        final ResourceRefR4 frameRef = ResourceRefR4.generateRandomSub(FrameH.NAME_PREFIX); 
    367429                        final String frameTitle = templateH.getTitle(); 
    368430 
  • modules/org.sophie2.main.func.text/src/main/java/org/sophie2/main/func/text/utils/TextChainUtils.java

     
    525525 
    526526                                frameTemplRef =  ResourceRefR4.generateRandomSub(FrameH.NAME_PREFIX); 
    527527                                String templateTitle = getTemplateName(frameH.getTitle(), bookH, true); 
    528                                 TemplateUtil.createTemplate(templateTitle, NaiveImmList.<TemplatedKey<?>>getEmpty(), 
     528                                TemplateUtil.createTemplate(templateTitle, null, NaiveImmList.<ResourceRefR4>getEmpty(), 
    529529                                                frameView, frameTemplRef, false, false); 
    530530                                if (frameH instanceof HeadTextFrameH) { 
    531531                                        HeadTextFrameH thisHeadH = (HeadTextFrameH) frameH; 
     
    586586                                || ResourceRefR4.NONE_REF.equals(res)) { 
    587587                        res =  ResourceRefR4.generateRandomSub(PageH.NAME_PREFIX); 
    588588                        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); 
    591591                } else { 
    592592                        res = ResourceRefR4.getRelativeRef(book.getRef(), pageH.getRef().append(res)); 
    593593                } 
  • 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
    
    
     
    33import java.util.HashMap; 
    44import java.util.List; 
    55import java.util.Map; 
     6import java.util.Map.Entry; 
    67 
    78import javax.swing.JComponent; 
    89 
    910import org.sophie2.base.commons.skin.IconId; 
    1011import org.sophie2.base.commons.skin.IconsSet; 
    1112import org.sophie2.base.commons.structures.ImmTreeList; 
     13import org.sophie2.base.commons.structures.ImmTreeMap; 
    1214import org.sophie2.base.commons.util.ImageUtil; 
    1315import org.sophie2.base.commons.util.ImmList; 
    14 import org.sophie2.base.commons.util.NaiveImmList; 
     16import org.sophie2.base.commons.util.ImmMap; 
    1517import org.sophie2.base.dialogs.DialogManager; 
    1618import org.sophie2.base.halos.ClickHaloButton; 
    1719import org.sophie2.base.halos.HaloButton; 
    1820import org.sophie2.base.model.book.BookH; 
     21import org.sophie2.base.model.book.ElementH; 
    1922import org.sophie2.base.model.book.FrameH; 
    2023import org.sophie2.base.model.book.PageH; 
    2124import org.sophie2.base.model.book.resource.r4.BookR4; 
     
    2427import org.sophie2.base.model.resources.r4.ResourceRefR4; 
    2528import org.sophie2.base.model.resources.r4.access.ResourceAccess; 
    2629import org.sophie2.base.model.resources.r4.keys.Key; 
    27 import org.sophie2.base.model.resources.r4.keys.TemplatedKey; 
    2830import org.sophie2.base.model.resources.r4.resources.ResourceR4; 
    2931import org.sophie2.base.skins.SkinElementId; 
    3032import org.sophie2.base.visual.skins.ElementSkinPart; 
     
    119121 
    120122                                final ResourceRefR4 ref; 
    121123                                final String kind; 
    122                                 final Class<? extends ResourceR4> resourceClass; 
    123124                                final String defaultTitle; 
    124125                                final Key<ResourceRefList> templatesKey; 
    125126                                final String resourcePrefix; 
     127                                //Class<? extends ResourceR4> resourceClass; 
    126128                                if (frameView != null) { 
    127129                                        templatedElem = frameView; 
    128130                                        // preparing frame template 
    129131                                        ref = frameView.getAccess().getRef(); 
    130132                                        kind = frameView.model().get().getResourceKind(); 
    131                                         resourceClass = ResourceR4.getClassByKind(kind); 
     133                                        //resourceClass = ResourceR4.getClassByKind(kind); 
    132134                                        defaultTitle = frameView.model().get().getTitle(); 
    133135                                        templatesKey = BookR4.KEY_FRAME_TEMPLATES; 
    134136                                        resourcePrefix = FrameH.NAME_PREFIX; 
     
    137139                                        // preparing page template 
    138140                                        ref = pwa.getRootPageView().getAccess().getRef(); 
    139141                                        kind = PageR4.KIND; 
    140                                         resourceClass = PageR4.class; 
     142                                        //resourceClass = PageR4.class; 
    141143                                        defaultTitle = pwa.getRootPageView().model().get().getTitle(); 
    142144                                        templatesKey = BookR4.KEY_PAGE_TEMPLATES; 
    143145                                        resourcePrefix = PageH.NAME_PREFIX; 
    144146                                } 
     147                                 
     148                                ElementH templateElemH = templatedElem.model().get(); 
     149                                 
    145150                                String title = source.defaultTitles.get(ref); 
    146151                                if (title == null) { 
    147152                                        title = defaultTitle + " template"; 
    148153                                } 
    149154 
    150                                 TemplateInfo info = new TemplateInfo(title, ResourceR4.getKnownKeys(resourceClass).values(), false); 
     155                                TemplateInfo info = new TemplateInfo(title, templateElemH, false); 
    151156                                TemplateDialog.Input input = new TemplateDialog.Input(pwa.swingComponent().get(), info); 
     157                                 
    152158 
    153159                                TemplateInfo res = DialogManager.get().showDialog(input); 
    154160 
     
    160166                                        final ResourceRefR4 templateRef = (foundResourceRef == null ? ResourceRefR4 
    161167                                                        .generateRandomSub(resourcePrefix) : foundResourceRef); 
    162168                                        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                                         
    165170 
    166171                                        boolean proceed = true; 
    167172                                        if (foundResourceRef != null) { 
     
    176181                                                } 
    177182                                        } 
    178183 
    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); 
    185200                                                } 
    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, 
    189206                                                                templatedElem, templateRef, true, res.getIsDefault()); 
    190207                                        } 
    191208                                } 
     
    258275                DialogManager.get().showDialog(new MessageDialogInput(parent, message)); 
    259276        } 
    260277} 
     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 jani 
    22  */ 
    23 public class TemplateDialogDemo { 
    24         /** 
    25          * Entry point for demo. 
    26          *  
    27          * @param args 
    28          *            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", ResourceR4 
    41                                                                 .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

     
    1010import java.awt.event.FocusListener; 
    1111import java.awt.event.MouseAdapter; 
    1212import java.awt.event.MouseEvent; 
     13import java.util.ArrayList; 
     14import java.util.Collection; 
    1315import java.util.Enumeration; 
    1416import java.util.HashMap; 
     17import java.util.List; 
    1518import java.util.Map; 
    16 import java.util.Set; 
    1719import java.util.Map.Entry; 
    1820 
    1921import javax.swing.JButton; 
     
    3133import javax.swing.tree.TreePath; 
    3234 
    3335import org.sophie2.base.commons.gui.TriStateCheckBox; 
     36import org.sophie2.base.commons.gui.TriStateCheckBox.State; 
    3437import org.sophie2.base.commons.util.ImmList; 
    3538import org.sophie2.base.commons.util.NaiveImmList; 
    3639import org.sophie2.base.dialogs.Dialog; 
    3740import org.sophie2.base.dialogs.DialogInput; 
     41import org.sophie2.base.model.book.ElementH; 
     42import org.sophie2.base.model.resources.r4.ResourceRefR4; 
    3843import org.sophie2.base.model.resources.r4.keys.CompositeKey; 
    3944import org.sophie2.base.model.resources.r4.keys.Key; 
    4045import org.sophie2.base.model.resources.r4.keys.TemplatedKey; 
     46import org.sophie2.base.model.resources.r4.resources.ResourceR4; 
    4147import org.sophie2.core.mvc.EventFilterBuilder; 
    4248import org.sophie2.core.mvc.OperationDef; 
    4349import org.sophie2.core.mvc.events.EventR3; 
     50import org.sophie2.main.app.commons.dialogs.TemplateDialog.CheckBoxTree.CheckNode; 
    4451 
    4552/** 
    4653 * The dialog which is used to add a page or frame as a template. It generates a 
     
    457464        protected static class SwingDialog { 
    458465                 
    459466                /** 
     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                /** 
    460521                 * The instance of the book template helper. 
    461522                 */ 
     523                 
    462524                private static SwingDialog instance; 
    463525 
    464526                private static final int PANEL_HEIGHT = 300; 
     
    481543                private JButton selectAllButton = null; 
    482544                private JButton selectNoneButton = null; 
    483545                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; 
    487550 
    488551                /** 
    489552                 * Holds the initial info for the dialog. 
     
    539602                                JPanel bottom = new JPanel(); 
    540603                                bottom.add(getOkButton()); 
    541604                                bottom.add(getCancelButton()); 
    542  
     605         
    543606                                this.treeScrollPane = new JScrollPane(); 
    544607 
    545608                                this.mainPanel.add(top, BorderLayout.NORTH); 
     
    615678                                                String title = getTitleField().getText(); 
    616679                                                boolean isDefault = SwingDialog.this.isDefaultTemplateNode.isSelected(); 
    617680 
    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                                                        } 
    621695                                                } 
     696                                                 
     697                                                TemplateInfo res = new TemplateInfo(title, SwingDialog.this.keyStates, toDelete, isDefault);                                     
    622698 
    623                                                 TemplateInfo res = new TemplateInfo(title, SwingDialog.this.keyStates, isDefault);                                               
    624  
    625699                                                setResultInfo(res); 
    626700                                                SwingDialog.this.dialog.setVisible(false); 
    627701                                        } 
     
    693767                        return this.selectNoneButton; 
    694768                } 
    695769 
     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                } 
    696812                /** 
    697813                 * Performs setup of the dialog - sets up the title and the panel with 
    698814                 * the checkboxes. 
     
    700816                private void setup() { 
    701817                        getTitleField().setText(this.initialInfo.getTitle()); 
    702818 
    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());                      
    715825                        this.checkBoxTree.expandPath(new TreePath (this.checkBoxTree.getRootNode())); 
    716826                } 
    717827 
     
    720830                 * adds the key if he's at the appropriate level 
    721831                 */ 
    722832                @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) { 
    724835                        if (key instanceof TemplatedKey) { 
    725836                                if (!HIDDEN_KEYS.contains((TemplatedKey) key) && key.getParts().size() == level) { 
    726837                                        ImmList<String> parts = key.getParts(); 
    727838                                        String text = parts.get(parts.size() - 1); 
    728  
    729                                         CheckBoxTree.CheckNode node = this.checkBoxTree.addNode(text, parent); 
     839                                         
     840                                        CheckNode node = this.checkBoxTree.addNode(text, parent); 
    730841                                        node.setSelected(state); 
    731                                         keyControlMap.put(key, node); 
     842                                        keyControlMap.put(new NodeCacheKey(key, resource), node); 
    732843                                } 
    733844                        } 
    734  
     845                         
    735846                        else if (key instanceof CompositeKey && key.getParts().size() == level) { 
    736847                                ImmList<String> parts = key.getParts(); 
    737848                                String text = parts.get(parts.size() - 1); 
    738  
    739                                 CheckBoxTree.CheckNode node = this.checkBoxTree.addNode(text, parent); 
    740  
     849                                 
     850                                CheckNode node = this.checkBoxTree.addNode(text, parent); 
     851                                 
    741852                                String prefix = key.getId().concat(Key.SEPARATOR); 
    742853                                for (final Key<?> subKey : keys) { 
    743854                                        String keyId = subKey.getId(); 
    744855                                        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);                                                    
    747858                                        } 
    748859                                } 
    749  
    750860                        } 
    751861                } 
    752862 
  • modules/org.sophie2.main.app.commons/src/main/java/org/sophie2/main/app/commons/dialogs/TemplateInfo.java

     
    11package org.sophie2.main.app.commons.dialogs; 
    22 
    3 import java.util.Collection; 
    43import java.util.HashMap; 
     4import java.util.List; 
    55import java.util.Map; 
    66 
     7import org.sophie2.base.model.book.ElementH; 
     8import org.sophie2.base.model.resources.r4.ResourceRefR4; 
    79import org.sophie2.base.model.resources.r4.keys.Key; 
    810import org.sophie2.core.prolib.annot.Immutable; 
    911 
     
    2426 
    2527        private String title; 
    2628        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; 
    2845 
    2946        /** 
    3047         * @return 
     
    4966         * @return  
    5067         *                      A {@link Map} containing the states. 
    5168         */ 
    52         public Map<Key<?>, Boolean> getKeyStates() { 
     69        public Map <ResourceRefR4, Map <Key<?>, Boolean>> getKeyStates() { 
    5370                return this.keyStates; 
    5471        } 
    55  
     72         
    5673        /** 
     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        /** 
    5790         * A default constructor. 
    5891         */ 
    5992        private TemplateInfo() { 
    6093                this.title = ""; 
    61                 this.keyStates = new HashMap<Key<?>, Boolean>(); 
     94                this.keyStates = new HashMap<ResourceRefR4, Map<Key<?>, Boolean>>(); 
    6295                this.isDefault = false; 
    6396        } 
    6497 
     
    69102         *            The title of the template. 
    70103         * @param keyStates 
    71104         *            The initial states of the keys. 
     105         * @param toDelete  
     106         *                        The initial resources which should be deleted 
    72107         * @param isDefault  
    73108         *            The boolean value for the usage of the template. 
    74109         */ 
    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) { 
    76111                this.title = title; 
    77112                this.keyStates = keyStates; 
     113                this.resourcesToDelete = toDelete; 
    78114                this.isDefault = isDefault; 
    79115        } 
    80116 
     117 
    81118        /** 
    82          * Constructor. 
     119         * Constructor 
    83120         *  
    84121         * @param title 
    85          *            The title of the template. 
    86          * @param keys 
    87          *            The initial keys - true is used as a default state. 
    88          * @param isDefault  
    89          *            The boolean value for the usage 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. 
    90127         */ 
    91         public TemplateInfo(String title, Collection<Key<?>> keys, boolean isDefault) { 
     128         
     129        public TemplateInfo (String title, ElementH rootElement, boolean isDefault) { 
    92130                this.title = title; 
    93131                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;          
    98133        } 
    99 } 
    100  No newline at end of file 
     134         
     135} 
     136 
     137