1 /******************************************************************************* 2 * Copyright (c) 2018 Angelo ZERR. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * Angelo Zerr <angelo.zerr@gmail.com> - [minimap] Initialize minimap view - Bug 535450 13 *******************************************************************************/ 14 package org.eclipse.ui.internal.views.minimap; 15 16 import java.util.HashMap; 17 import java.util.Map; 18 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Control; 22 import org.eclipse.swt.widgets.Label; 23 24 import org.eclipse.jface.dialogs.IPageChangedListener; 25 26 import org.eclipse.ui.part.MultiPageEditorPart; 27 import org.eclipse.ui.part.Page; 28 import org.eclipse.ui.part.PageBook; 29 30 import org.eclipse.ui.texteditor.ITextEditor; 31 32 /** 33 * Minimap with multi page editor which shows a minimap for the current page 34 * which is an {@link ITextEditor}. 35 * 36 */ 37 public class MultiPageMinimapPage extends Page { 38 39 private final MultiPageEditorPart fMultiPageEditor; 40 private final Map<Object, Control> fTextWidgetMap; 41 private final IPageChangedListener fPageChangedListener; 42 private PageBook fPageBook; 43 private Label fErrorLabel; 44 MultiPageMinimapPage(MultiPageEditorPart multiPageEditor)45 public MultiPageMinimapPage(MultiPageEditorPart multiPageEditor) { 46 this.fMultiPageEditor = multiPageEditor; 47 this.fTextWidgetMap = new HashMap<>(); 48 this.fPageChangedListener = e -> { 49 Object selectedPage = multiPageEditor.getSelectedPage(); 50 // Find from cache the minimap for the selected page 51 Control textWidget = fTextWidgetMap.get(selectedPage); 52 if (textWidget != null) { 53 fPageBook.showPage(textWidget); 54 return; 55 } 56 57 MinimapPage minimapPage = null; 58 if (selectedPage instanceof ITextEditor) { 59 // Create and show a minimap page for the given text editor page 60 ITextEditor textEditor = (ITextEditor) selectedPage; 61 minimapPage = MinimapPage.createMinimapPage(textEditor); 62 } 63 if (minimapPage != null) { 64 minimapPage.createControl(fPageBook); 65 textWidget = minimapPage.getControl(); 66 fTextWidgetMap.put(selectedPage, textWidget); 67 fPageBook.showPage(textWidget); 68 } else { 69 fTextWidgetMap.put(selectedPage, fErrorLabel); 70 fPageBook.showPage(fErrorLabel); 71 } 72 }; 73 multiPageEditor.addPageChangedListener(fPageChangedListener); 74 } 75 76 @Override createControl(Composite parent)77 public void createControl(Composite parent) { 78 fPageBook = new PageBook(parent, SWT.NORMAL); 79 fErrorLabel = new Label(fPageBook, SWT.NORMAL); 80 fErrorLabel.setText(MinimapMessages.MinimapViewNoMinimap); 81 fPageChangedListener.pageChanged(null); 82 } 83 84 @Override getControl()85 public Control getControl() { 86 return fPageBook; 87 } 88 89 @Override setFocus()90 public void setFocus() { 91 fPageBook.setFocus(); 92 } 93 94 @Override dispose()95 public void dispose() { 96 super.dispose(); 97 fMultiPageEditor.removePageChangedListener(fPageChangedListener); 98 } 99 } 100