Skip to main content
summaryrefslogtreecommitdiffstats
blob: 64e5392e00cb4313147fa5180b27b76cfc479638 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.ui.internal.editors.text;

import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

import com.ibm.icu.text.Collator;

import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.ISelection;

import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;

import org.eclipse.ui.texteditor.AnnotationPreference;
import org.eclipse.ui.texteditor.MarkerAnnotationPreferences;

/**
 * The abstract class for next and previous pulldown action delegates.
 *
 * @since 3.0
 */
public abstract class NextPreviousPulldownActionDelegate extends Action implements IMenuCreator, IWorkbenchWindowPulldownDelegate2 {

	/** The cached menu. */
	private Menu fMenu;

	/** The preference store */
	private IPreferenceStore fStore;

	/** Action for handling menu item selection. */
	private static class NavigationEnablementAction extends Action implements Comparable {

		/** The preference store. */
		private IPreferenceStore fStore;

		/** The preference key for the value in the store. */
		private String fKey;

		/**
		 * The display string.
		 * @since 3.2
		 */
		private String fName;

		/**
		 * Creates a named navigation enablement action.
		 *
		 * @param name the name of this action
		 * @param store the preference store
		 * @param key the preference key
		 */
		public NavigationEnablementAction(String name, IPreferenceStore store, String key) {
			super(name, IAction.AS_CHECK_BOX);
			fStore= store;
			fKey= key;
			fName= name;
			setChecked(fStore.getBoolean(fKey));
		}

		@Override
		public void run() {
			fStore.setValue(fKey, isChecked());
		}

		@Override
		public int compareTo(Object o) {
			if (!(o instanceof NavigationEnablementAction))
				return -1;

			String otherName= ((NavigationEnablementAction)o).fName;

			return Collator.getInstance().compare(fName, otherName);
		}
	}

	/**
	 * Returns the preference key to be used in the
	 * <code>NavigationEnablementAction</code>.
	 *
	 * @param annotationPreference the annotation preference
	 * @return the preference key or <code>null</code> if the key is not defined in XML
	 */
	public abstract String getPreferenceKey(AnnotationPreference annotationPreference);

	@Override
	public Menu getMenu(Control parent) {
		if (fMenu != null)
			fMenu.dispose();

		fMenu= new Menu(parent);
		fillMenu(fMenu);

		return fMenu;
	}

	/**
	 * Creates a next previous action delegate.
	 */
	public NextPreviousPulldownActionDelegate() {
		fStore= EditorsPlugin.getDefault().getPreferenceStore();
	}

	@Override
	public Menu getMenu(Menu parent) {
		if (fMenu == null) {
			fMenu= new Menu(parent);
			fillMenu(fMenu);
		}

		return fMenu;
	}

	@Override
	public void dispose() {
		if (fMenu != null) {
			fMenu.dispose();
			fMenu= null;
		}
	}

	/**
	 * Fills the given menu using marker
	 * annotation preferences.
	 *
	 * @param menu the menu to fill
	 */
	private void fillMenu(Menu menu) {
		IAction[] actions= getActionsFromDescriptors();

		for (int i= 0; i < actions.length; i++) {
			ActionContributionItem item= new ActionContributionItem(actions[i]);
			item.fill(menu, -1);
		}
	}

	/**
	 * Creates actions using marker
	 * annotation preferences.
	 *
	 * @return the navigation enablement actions
	 */
	private IAction[] getActionsFromDescriptors() {
		MarkerAnnotationPreferences fMarkerAnnotationPreferences= EditorsPlugin.getDefault().getMarkerAnnotationPreferences();
		SortedSet containers= new TreeSet();

		Iterator iter= fMarkerAnnotationPreferences.getAnnotationPreferences().iterator();
		while (iter.hasNext()) {
			AnnotationPreference preference= (AnnotationPreference)iter.next();
			String key= preference.getShowInNextPrevDropdownToolbarActionKey();
			if (key != null && fStore.getBoolean(key)) {
				String preferenceKey= getPreferenceKey(preference);

				/*
				 * Fixes bug 41689
				 * This code can be simplified if we decide that
				 * we don't allow to use different settings for go to
				 * previous and go to next annotation.
				 */
				preferenceKey= preference.getIsGoToNextNavigationTargetKey();

				if (preferenceKey != null)
					containers.add(new NavigationEnablementAction(preference.getPreferenceLabel(), fStore, preferenceKey));
			}
		}

		return (IAction[]) containers.toArray(new Action[containers.size()]);
	}

	@Override
	public void init(IWorkbenchWindow window) {
	}

	@Override
	public void run(IAction action) {
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
	}
}

Back to the top