### Eclipse Workspace Patch 1.0
#P org.sophie2.main.app.commons
|
|
|
5 | 5 | import org.sophie2.base.bound.ComboInput; |
6 | 6 | import org.sophie2.base.layout.impl.DefaultDocView; |
7 | 7 | import org.sophie2.base.model.book.BookH; |
| 8 | import org.sophie2.base.model.resources.r4.ResourceRefR4; |
8 | 9 | import org.sophie2.base.model.resources.r4.changes.AutoAction; |
9 | 10 | import org.sophie2.base.model.resources.r4.resources.ResourceR4; |
10 | 11 | import org.sophie2.core.logging.SophieLog; |
… |
… |
|
340 | 341 | return true; // Handled, no changes made. |
341 | 342 | } |
342 | 343 | |
343 | | bookView.addNewPage(pageIndex); |
| 344 | bookView.addNewPage(pageIndex, ResourceRefR4.NONE_REF); |
344 | 345 | } else { |
345 | 346 | |
346 | 347 | assert pageIndex >= 0 && pageIndex <= model.getPages().size() : |
#P org.sophie2.main.app.layout
|
|
|
1 | 1 | package org.sophie2.main.app.layout.left.pages; |
2 | 2 | |
| 3 | import java.awt.Rectangle; |
| 4 | |
3 | 5 | import org.sophie2.base.commons.util.position.ImmPoint; |
| 6 | import org.sophie2.base.commons.util.position.ImmRect; |
4 | 7 | import org.sophie2.base.dialogs.DialogManager; |
5 | 8 | import org.sophie2.base.dnd.DndData; |
6 | 9 | import org.sophie2.base.dnd.SophieDragDropHandler; |
… |
… |
|
32 | 35 | * Event handlers for the {@link PagePreviewPalette}. |
33 | 36 | */ |
34 | 37 | public enum PagePreviewPaletteLogic implements OperationDef { |
35 | | |
| 38 | |
36 | 39 | /** |
37 | 40 | * Handles a user request to add a new page before current page. |
38 | 41 | */ |
… |
… |
|
169 | 172 | |
170 | 173 | /** |
171 | 174 | * 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. |
173 | 177 | */ |
174 | 178 | 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; |
175 | 188 | |
176 | 189 | public void defineFilter(EventFilterBuilder filter) { |
177 | 190 | filter.setSourceClass(PagePreviewPalette.class); |
… |
… |
|
180 | 193 | |
181 | 194 | public boolean handle(EventR3 event) { |
182 | 195 | |
| 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 | |
183 | 203 | DndImport dndImport = event.getEventParam( |
184 | 204 | SophieDragDropHandler.TransferEventIds.TRANSACTION_PARAM_INDEX, |
185 | 205 | DndImport.class); |
… |
… |
|
188 | 208 | SophieDragDropHandler.TransferEventIds.POINT_PARAM_INDEX, |
189 | 209 | ImmPoint.class); |
190 | 210 | |
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 |
193 | 212 | 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); |
198 | 214 | PageH page = pageItem.pageView().get().model().get(); |
199 | 215 | |
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); |
203 | 223 | |
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; |
209 | 233 | } |
210 | 234 | |
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; |
212 | 256 | } |
213 | 257 | }; |
214 | 258 | |
… |
… |
|
230 | 274 | |
231 | 275 | BookView bookView = AppViewUtil.getCurrentBookView(palette); |
232 | 276 | |
233 | | if ("author".equals(System.getProperty("edition")) && !bookView.getViewOptions().isPreviewMode()) { |
234 | | BookH book = bookView.model().get(); |
| 277 | if (!bookView.getViewOptions().isPreviewMode()) { |
235 | 278 | |
236 | 279 | RootPageView pageView = bookView.getCurrentPageView(); |
237 | | |
238 | 280 | 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 | } |
241 | 283 | |
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; |
246 | 308 | |
247 | | bookView.addNewPage(index); |
| 309 | if (after) { |
| 310 | currentIndex++; |
248 | 311 | } |
| 312 | final int index = currentIndex; |
249 | 313 | |
250 | | return true; |
| 314 | bookView.addNewPage(index, templateRef); |
251 | 315 | } |
252 | 316 | } |