Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0d3e90bdb84ffdf28e49670958b1afd42a45c832 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*******************************************************************************
 * Copyright (c) 2018 Angelo ZERR.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Angelo Zerr <angelo.zerr@gmail.com> - [minimap] Initialize minimap view - Bug 535450
 *******************************************************************************/
package org.eclipse.ui.internal.views.minimap;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

import org.eclipse.jface.dialogs.IPageChangedListener;

import org.eclipse.ui.part.MultiPageEditorPart;
import org.eclipse.ui.part.Page;
import org.eclipse.ui.part.PageBook;

import org.eclipse.ui.texteditor.ITextEditor;

/**
 * Minimap with multi page editor which shows a minimap for the current page
 * which is an {@link ITextEditor}.
 *
 */
public class MultiPageMinimapPage extends Page {

	private final MultiPageEditorPart fMultiPageEditor;
	private final Map<Object, Control> fTextWidgetMap;
	private final IPageChangedListener fPageChangedListener;
	private PageBook fPageBook;
	private Label fErrorLabel;

	public MultiPageMinimapPage(MultiPageEditorPart multiPageEditor) {
		this.fMultiPageEditor = multiPageEditor;
		this.fTextWidgetMap = new HashMap<>();
		this.fPageChangedListener = e -> {
			Object selectedPage = multiPageEditor.getSelectedPage();
			// Find from cache the minimap for the selected page
			Control textWidget = fTextWidgetMap.get(selectedPage);
			if (textWidget != null) {
				fPageBook.showPage(textWidget);
				return;
			}

			MinimapPage minimapPage = null;
			if (selectedPage instanceof ITextEditor) {
				// Create and show a minimap page for the given text editor page
				ITextEditor textEditor = (ITextEditor) selectedPage;
				minimapPage = MinimapPage.createMinimapPage(textEditor);
			}
			if (minimapPage != null) {
				minimapPage.createControl(fPageBook);
				textWidget = minimapPage.getControl();
				fTextWidgetMap.put(selectedPage, textWidget);
				fPageBook.showPage(textWidget);
			} else {
				fTextWidgetMap.put(selectedPage, fErrorLabel);
				fPageBook.showPage(fErrorLabel);
			}
		};
		multiPageEditor.addPageChangedListener(fPageChangedListener);
	}

	@Override
	public void createControl(Composite parent) {
		fPageBook = new PageBook(parent, SWT.NORMAL);
		fErrorLabel = new Label(fPageBook, SWT.NORMAL);
		fErrorLabel.setText(MinimapMessages.MinimapViewNoMinimap);
		fPageChangedListener.pageChanged(null);
	}

	@Override
	public Control getControl() {
		return fPageBook;
	}

	@Override
	public void setFocus() {
		fPageBook.setFocus();
	}

	@Override
	public void dispose() {
		super.dispose();
		fMultiPageEditor.removePageChangedListener(fPageChangedListener);
	}
}

Back to the top