Ticket #2312: dnd-new-templated-pages.patch

File dnd-new-templated-pages.patch, 12.0 KB (added by deni, 15 years ago)
  • src/main/java/org/sophie2/main/app/commons/book/panels/BookPanelLogic.java

    ### Eclipse Workspace Patch 1.0
    #P org.sophie2.main.app.commons
     
    55import org.sophie2.base.bound.ComboInput; 
    66import org.sophie2.base.layout.impl.DefaultDocView; 
    77import org.sophie2.base.model.book.BookH; 
     8import org.sophie2.base.model.resources.r4.ResourceRefR4; 
    89import org.sophie2.base.model.resources.r4.changes.AutoAction; 
    910import org.sophie2.base.model.resources.r4.resources.ResourceR4; 
    1011import org.sophie2.core.logging.SophieLog; 
     
    340341                                return true; // Handled, no changes made. 
    341342                        } 
    342343 
    343                         bookView.addNewPage(pageIndex); 
     344                        bookView.addNewPage(pageIndex, ResourceRefR4.NONE_REF); 
    344345                } else { 
    345346 
    346347                        assert pageIndex >= 0 && pageIndex <= model.getPages().size() : 
  • src/main/java/org/sophie2/main/app/commons/book/BookView.java

     
    478478 
    479479        /** 
    480480         * Adds a new page to this book to the given index and sets it to be 
    481          * current. 
     481         * current.  
     482         * If the <code>templateRef</code> is not {@link ResourceRefR4#NONE_REF}, 
     483         * applies this page template to the new page. Otherwise, it way apply  
     484         * the default page template or it may not apply a template at all. 
    482485         *  
    483486         * @param index 
    484487         *            The index where in the pages list the new page should be 
    485488         *            created. 
     489         * @param templateRef 
    486490         */ 
    487         public void addNewPage(final int index) { 
     491        public void addNewPage(final int index, ResourceRefR4 templateRef) { 
    488492 
    489493                assert !getViewOptions().isPreviewMode() : "You should not add pages in preview mode"; 
    490494 
     
    492496                final ResourceRefList pages = model().get().getPages(); 
    493497                final String pageName = ResourceUtil.getNextTitle(this.access().get().getRef(), 
    494498                                PageR4.DEFAULT_TITLE); 
     499                ResourceRefR4 realTemplateRef = getTemplateRef(templateRef); 
     500                boolean significant = ResourceRefR4.NONE_REF.equals(realTemplateRef); 
    495501 
    496                 new AutoAction("Add a new page", true) { 
     502                new AutoAction("Add a new page", significant) { 
    497503 
    498504                        @Override 
    499505                        public void performAuto() { 
     
    502508 
    503509                }.register(getAccess()); 
    504510 
    505                 ResourceRefR4 templatePageRef = model().get().getDefaultPageTemplate();                           
     511                if(!ResourceRefR4.NONE_REF.equals(realTemplateRef)) { 
    506512 
    507                 if(! templatePageRef.equals(ResourceRefR4.NONE_REF)) { 
     513                        if (!realTemplateRef.isAbsolute()) { 
     514                                realTemplateRef = this.getAccess().getRef().getAbsolute(realTemplateRef); 
     515                        } 
     516                        assert realTemplateRef.isAbsolute(); 
     517                        final ResourceRefR4 pageToTemplateRef = ResourceRefR4.getRelativeRef(pageRef, 
     518                                        realTemplateRef); 
    508519 
    509                         ResourceRefR4 absoluteDefRef = this.getAccess().getRef().getAbsolute(templatePageRef); 
    510                         assert absoluteDefRef.isAbsolute(); 
    511                         final ResourceRefR4 templateRef =ResourceRefR4.getRelativeRef(pageRef, 
    512                                         absoluteDefRef); 
     520                        ResourceAccess templateAccess = this.getAccess().open(pageToTemplateRef, null); 
    513521 
    514                         ResourceAccess templateAccess = this.getAccess().open(templateRef, null); 
    515  
    516522                        ResourceAccess pageAccess = this.getPageView(pageRef).getAccess(); 
    517523 
    518524                        PageH templateH = ResourceH.getHelper(templateAccess, PageH.class); 
    519525                        final NaiveImmList<TemplatedKey<?>> immKeys = templateH.getApplicableTemplatedKeys(); 
    520526 
    521                         new AutoAction("Apply page template.", true) { 
     527                        new AutoAction("Add a new templated page.", true) { 
    522528                                @Override 
    523529                                public void performAuto() { 
    524                                         TemplateUtil.applyPageTemplate(getChanger(), templateRef, immKeys); 
     530                                        TemplateUtil.applyPageTemplate(getChanger(), pageToTemplateRef, immKeys); 
    525531                                } 
    526532                        }.register(pageAccess); 
    527533 
    528                         SophieLog.debugf("Added a new page with index %d to the %s book.", index, model().get() 
    529                                         .getTitle()); 
     534                        SophieLog.debugf("Added a new page with index %d to the %s book.", index,  
     535                                        model().get().getTitle()); 
     536                } 
     537                 
     538                goToPage(model().get().getPages().get(index)); 
    530539 
    531                         goToPage(model().get().getPages().get(index)); 
    532  
    533                         SophieLog.debugf("Went to page %d.", index + 1); 
     540                SophieLog.debugf("Went to page %d.", index + 1); 
     541        } 
     542         
     543        private ResourceRefR4 getTemplateRef(ResourceRefR4 templateRef) { 
     544                if (templateRef != null && !ResourceRefR4.NONE_REF.equals(templateRef)) { 
     545                        return templateRef; 
     546                }  
     547                 
     548                ResourceRefR4 defaultTemplateRef = model().get().getDefaultPageTemplate();       
     549                if (defaultTemplateRef != null && !ResourceRefR4.NONE_REF.equals(defaultTemplateRef)) { 
     550                        return defaultTemplateRef; 
    534551                } 
     552                 
     553                return ResourceRefR4.NONE_REF;           
    535554        } 
     555         
    536556        /** 
    537557         * The events that may be fired by this. 
    538558         *  
  • src/main/java/org/sophie2/main/app/commons/util/TemplateUtil.java

     
    169169                                if (key.equals(ResourceFrame.KEY_MAIN_RESOURCE) && skipMainResource) { 
    170170                                        continue; 
    171171                                } 
    172                                  
    173172                                changer.setRaw(key, null); 
    174173                                changer.setRaw(key.getLockKey(), false);  
    175174                        } 
  • src/main/java/org/sophie2/main/app/layout/left/pages/PagePreviewPaletteLogic.java

    #P org.sophie2.main.app.layout
     
    11package org.sophie2.main.app.layout.left.pages; 
    22 
     3import java.awt.Rectangle; 
     4 
    35import org.sophie2.base.commons.util.position.ImmPoint; 
     6import org.sophie2.base.commons.util.position.ImmRect; 
    47import org.sophie2.base.dialogs.DialogManager; 
    58import org.sophie2.base.dnd.DndData; 
    69import org.sophie2.base.dnd.SophieDragDropHandler; 
     
    3235 * Event handlers for the {@link PagePreviewPalette}. 
    3336 */ 
    3437public enum PagePreviewPaletteLogic implements OperationDef { 
    35          
     38                 
    3639        /** 
    3740         * Handles a user request to add a new page before current page. 
    3841         */ 
     
    169172         
    170173        /** 
    171174         * Handles dropping a page template over the {@link PagePreviewPalette}. 
    172          * Applies the template to the page over which it was dropped. 
     175         * Applies the template to the page over which it was dropped or creates a new page 
     176         * if it was dropped over the blank space between page items. 
    173177         */ 
    174178        ON_DND_APPLY_TEMPLATE { 
     179                 
     180                /** 
     181                 * The padding (in pixels) page items are assumed to have. 
     182                 * If a page template is dropped over the top or bottom padding, a new page 
     183                 * will be created from it. 
     184                 * Otherwise, it is assumed that it was dropped over the page thumbnail  
     185                 * and the template is applied to the page. 
     186                 */ 
     187                public static final int PAGE_ITEM_PADDING = 10; 
    175188 
    176189                public void defineFilter(EventFilterBuilder filter) { 
    177190                        filter.setSourceClass(PagePreviewPalette.class); 
     
    180193 
    181194                public boolean handle(EventR3 event) { 
    182195                         
     196                        PagePreviewPalette palette = event.getSource(PagePreviewPalette.class); 
     197                        BookView bookView = AppViewUtil.getCurrentBookView(palette); 
     198                        if (bookView.getViewOptions().isPreviewMode()) { 
     199                                return false; 
     200                        } 
     201                        BookH book = bookView.model().get(); 
     202                         
    183203                        DndImport dndImport = event.getEventParam( 
    184204                                        SophieDragDropHandler.TransferEventIds.TRANSACTION_PARAM_INDEX, 
    185205                                        DndImport.class); 
     
    188208                                        SophieDragDropHandler.TransferEventIds.POINT_PARAM_INDEX, 
    189209                                        ImmPoint.class); 
    190210                         
    191                         // find the page over which the template was dropped 
    192                         PagePreviewPalette palette = event.getSource(PagePreviewPalette.class); 
     211                        // find the page item over which the template was dropped 
    193212                        int index = palette.mainComponent().get().locationToIndex(point.toPoint()); 
    194                         if (!palette.mainComponent().get().getCellBounds(index, index).contains(point.toPoint())) { 
    195                                 return false; 
    196                         }                                                
    197                         PageItem pageItem = palette.items().get().get(index); 
     213                        PageItem pageItem = palette.items().get().get(index);                    
    198214                        PageH page = pageItem.pageView().get().model().get(); 
    199215                         
    200                         // apply the template 
    201                         BookH book = AppViewUtil.getCurrentBook(palette); 
    202                         boolean handled = TemplateUtil.applyPageTemplate(dndImport.getTransferable(), page, book); 
     216                        // determine whether the template was dropped over the page itself or over blank space 
     217                        Rectangle bounds = palette.mainComponent().get().getCellBounds(index, index); 
     218                        ImmRect topPaddingRect = new ImmRect(bounds.x, bounds.y, bounds.width, PAGE_ITEM_PADDING); 
     219                        ImmRect innerRect = new ImmRect(bounds.x, bounds.y + PAGE_ITEM_PADDING,  
     220                                                bounds.width, bounds.height - 2 * PAGE_ITEM_PADDING); 
     221                        ImmRect bottomPaddingRect = new ImmRect(bounds.x, bounds.y + bounds.height - PAGE_ITEM_PADDING, 
     222                                        bounds.width, PAGE_ITEM_PADDING); 
    203223                         
    204                         // go to the page that was changed 
    205                         if (handled == true) { 
    206                                 BookView bookView = AppViewUtil.getCurrentBookView(palette); 
    207                                 ResourceRefR4 bookToPageRef = ResourceRefR4.getRelativeRef(book.getRef(), page.getRef()); 
    208                                 bookView.goToPage(bookToPageRef); 
     224                        if (innerRect.contains(point)) {                                 
     225                                boolean handled = TemplateUtil.applyPageTemplate(dndImport.getTransferable(), page, book); 
     226                                 
     227                                if (handled == true) { 
     228                                        ResourceRefR4 bookToPageRef = ResourceRefR4.getRelativeRef(book.getRef(), page.getRef()); 
     229                                        bookView.goToPage(bookToPageRef); 
     230                                } 
     231                                 
     232                                return handled;                          
    209233                        } 
    210234                         
    211                         return handled; 
     235                        TemplateRefData refData = dndImport.getTransferable().getDndData(TemplateRefData.class); 
     236                        if (refData == null || !TemplateUtil.isPageTemplate(book, refData.getTemplateRef())) { 
     237                                return false; 
     238                        } 
     239                         
     240                        if (topPaddingRect.contains(point)) { 
     241                                insertPage(bookView, page.getRef(), false, refData.getTemplateRef()); 
     242                                return true; 
     243                        } 
     244                                                 
     245                        if (bottomPaddingRect.contains(point)) { 
     246                                insertPage(bookView, page.getRef(), true, refData.getTemplateRef()); 
     247                                return true; 
     248                        } 
     249                         
     250                        if (!bounds.contains(point.toPoint())) { 
     251                                insertPage(bookView, page.getRef(), true, refData.getTemplateRef()); 
     252                                return true; 
     253                        } 
     254                         
     255                        return false; 
    212256                } 
    213257        }; 
    214258 
     
    230274 
    231275                BookView bookView = AppViewUtil.getCurrentBookView(palette); 
    232276 
    233                 if ("author".equals(System.getProperty("edition")) && !bookView.getViewOptions().isPreviewMode()) { 
    234                         BookH book = bookView.model().get(); 
     277                if (!bookView.getViewOptions().isPreviewMode()) { 
    235278 
    236279                        RootPageView pageView = bookView.getCurrentPageView(); 
    237  
    238280                        ResourceRefR4 currPage = pageView.model().get().getRef(); 
    239                         int currentIndex = book.getIndexOf(currPage); 
    240                         assert currentIndex >= 0; 
     281                        insertPage(bookView, currPage, after, ResourceRefR4.NONE_REF); 
     282                } 
    241283 
    242                         if (after) { 
    243                                 currentIndex++; 
    244                         } 
    245                         final int index = currentIndex; 
     284                return true; 
     285        } 
     286         
     287        /** 
     288         * Inserts a new page in the book model of the given BookView. The page is inserted 
     289         * right before or after the given page, depending on the <code>after</code> argument. 
     290         * If the <code>templateRef</code> is points to a valid page template, it is applied 
     291         * to the new page. 
     292         *  
     293         * @param bookView 
     294         *              BookView of the book in which to insert the new page. 
     295         * @param pageRef 
     296         *              Reference to the page before or after which the new one should be inserted. 
     297         * @param after 
     298         *              Whether the new page should be inserted after the given one. 
     299         * @param templateRef 
     300         *              A reference to the template that should be applied to the new page. 
     301         */ 
     302        protected static void insertPage(BookView bookView, ResourceRefR4 pageRef, 
     303                        boolean after, ResourceRefR4 templateRef) { 
     304                BookH book = bookView.model().get(); 
     305                 
     306                int currentIndex = book.getIndexOf(pageRef); 
     307                assert currentIndex >= 0; 
    246308 
    247                         bookView.addNewPage(index); 
     309                if (after) { 
     310                        currentIndex++; 
    248311                } 
     312                final int index = currentIndex; 
    249313 
    250                 return true; 
     314                bookView.addNewPage(index, templateRef); 
    251315        } 
    252316}