Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9dc2c149e72682a1c778ef122797176493567f9e (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*******************************************************************************
 * Copyright (c) 2009, 2013 David Green and others.
 * 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:
 *     David Green - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.wikitext.context.ui;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.mylyn.context.core.AbstractContextListener;
import org.eclipse.mylyn.context.core.AbstractContextStructureBridge;
import org.eclipse.mylyn.context.core.ContextChangeEvent;
import org.eclipse.mylyn.context.core.ContextCore;
import org.eclipse.mylyn.context.core.IInteractionElement;
import org.eclipse.mylyn.internal.wikitext.ui.editor.IFoldingStructure;
import org.eclipse.mylyn.wikitext.parser.outline.OutlineItem;
import org.eclipse.mylyn.wikitext.parser.outline.OutlineItem.Visitor;
import org.eclipse.ui.IEditorPart;

/**
 * based on implementation of ActiveFoldingListener in org.eclipse.mylyn.java.ui
 * 
 * @author David Green
 * @author Shawn Minto bug 274706 updated to use new 3.2 APIs.
 */
class ActiveFoldingListener extends AbstractContextListener {

	private final IEditorPart part;

	private final IFoldingStructure foldingStructure;

	private org.eclipse.jface.util.IPropertyChangeListener preferenceListener = new IPropertyChangeListener() {
		public void propertyChange(PropertyChangeEvent event) {
			if (WikiTextContextUiPlugin.PREF_ACTIVE_FOLDING_ENABLED.equals(event.getProperty())) {
				Object newValue = event.getNewValue();
				foldingEnabled = Boolean.TRUE.toString().equals(newValue.toString());
				updateFolding(false);
			}
		}
	};

	private boolean foldingEnabled;

	private final AbstractContextStructureBridge bridge;

	private final IPreferenceStore preferences;

	public ActiveFoldingListener(IEditorPart part, IFoldingStructure foldingStructure) {
		this.part = part;
		this.foldingStructure = foldingStructure;
		bridge = ContextCore.getStructureBridge(WikiTextContextStructureBridge.CONTENT_TYPE);
		ContextCore.getContextManager().addListener(this);
		preferences = WikiTextContextUiPlugin.getDefault().getPreferenceStore();
		preferences.addPropertyChangeListener(preferenceListener);
		foldingEnabled = preferences.getBoolean(WikiTextContextUiPlugin.PREF_ACTIVE_FOLDING_ENABLED);
		updateFolding(false);
	}

	private void updateFolding(boolean elementsDeleted) {
		if (!foldingStructure.isFoldingEnabled()) {
			return;
		}
		if (!foldingEnabled || !ContextCore.getContextManager().isContextActive()) {
			foldingStructure.expandAll();
		} else {
			OutlineItem outline = (OutlineItem) part.getAdapter(OutlineItem.class);
			if (outline != null) {
				final List<OutlineItem> toExpand = new ArrayList<OutlineItem>();
				outline.accept(new Visitor() {
					public boolean visit(OutlineItem item) {
						String identifier = bridge.getHandleIdentifier(item);
						IInteractionElement element = ContextCore.getContextManager().getElement(identifier);
						if (element != null && element.getInterest().isInteresting()) {
							toExpand.add(item);
						}
						return true;
					}
				});
				if (toExpand.isEmpty()) {
					foldingStructure.collapseAll(elementsDeleted);
				} else {
					foldingStructure.expandElementsExclusive(toExpand, elementsDeleted);
				}
			}
		}
	}

	@Override
	public void contextChanged(ContextChangeEvent event) {
		switch (event.getEventKind()) {
		case ACTIVATED:
			if (foldingStructure.isFoldingEnabled()) {
				updateFolding(false);
			}
			break;
		case DEACTIVATED:
			if (foldingStructure.isFoldingEnabled()) {
				foldingStructure.expandAll();
			}
			break;
		case CLEARED:
			if (event.isActiveContext()) {
				if (foldingStructure.isFoldingEnabled()) {
					if (!foldingEnabled || !ContextCore.getContextManager().isContextActive()) {
						foldingStructure.expandAll();
					} else {
						foldingStructure.collapseAll(true);
					}
				}
			}
			break;

		case INTEREST_CHANGED:
			if (foldingStructure.isFoldingEnabled()) {
				updateFolding(false);
			}
			break;
		case ELEMENTS_DELETED:
			if (foldingStructure.isFoldingEnabled()) {
				updateFolding(true);
			}
			break;
		default:
			break;
		}
	}

	public void dispose() {
		if (preferenceListener != null) {
			preferences.removePropertyChangeListener(preferenceListener);
			preferenceListener = null;
		}
		ContextCore.getContextManager().removeListener(this);
	}

}

Back to the top