Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d2b07159eecab3df790227d4fc19ed2848004f01 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*****************************************************************************
 * Copyright (c) 2010 CEA LIST.
 *
 *    
 * 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:
 *  Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.ui.perspectiveconfiguration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.internal.jobs.Worker;
import org.eclipse.jface.action.ContributionManager;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.preference.IPreferenceNode;
import org.eclipse.jface.preference.PreferenceNode;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PerspectiveAdapter;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.internal.Perspective;
import org.eclipse.ui.internal.WorkbenchPage;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.internal.WorkbenchWindow;
import org.eclipse.ui.internal.registry.ActionSetDescriptor;
import org.eclipse.ui.internal.registry.ActionSetRegistry;
import org.eclipse.ui.internal.registry.IActionSetDescriptor;
import org.eclipse.ui.internal.registry.PerspectiveRegistry;
import org.eclipse.ui.menus.IMenuService;
import org.eclipse.ui.menus.MenuUtil;

/**
 * this is a listener hat has in charge to configure a perspective when it is openend etc..
 * 
 */
public class PapyrusPerspectiveListener extends PerspectiveAdapter {

	private IWorkbenchWindow window;

	protected ConfigurationService configurationservice;

	protected ArrayList<PreferenceNode> hiddenRootPref = new ArrayList<PreferenceNode>();


	/**
	 * creation of the listener
	 * 
	 * @param window
	 */
	public PapyrusPerspectiveListener(IWorkbenchWindow window) {
		this.window = window;
		this.configurationservice = new ConfigurationService();

		//this is the lauch of eclipse, so we look for which perspective is opened
		if(window.getActivePage() != null) {
			IPerspectiveDescriptor openedPerspectiveDescriptor = window.getActivePage().getPerspective();
			if(configurationservice.getConfiguration(openedPerspectiveDescriptor.getId()) != null) {
				//it is the frist time?
				if(!isAlreadySave(openedPerspectiveDescriptor.getId())) {
					//Activator.log.debug("First time to open it");
					updateTooling(openedPerspectiveDescriptor);
					window.getActivePage().savePerspective();
				}

			}
			updatePreferencePage(openedPerspectiveDescriptor);
		}
	}

	/**
	 * return yes id it exist a file that custom a given perspective
	 * 
	 * @param perspectiveID
	 *        the id of the perspective
	 * @return yes it find a file,if not no
	 */
	public boolean isAlreadySave(String perspectiveID) {
		PerspectiveRegistry perspRegistry = (PerspectiveRegistry)WorkbenchPlugin.getDefault().getPerspectiveRegistry();
		try {
			IMemento memento = perspRegistry.getCustomPersp(perspectiveID);
			//Activator.log.debug(""+memento);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspectivedescriptor) {
		//Activator.log.debug("perspectiveActivated "+perspectivedescriptor.getId());
		super.perspectiveActivated(page, perspectivedescriptor);


		//test is we are interesting by the current perspective
		if(configurationservice.getConfiguration(perspectivedescriptor.getId()) != null) {
			if(!isAlreadySave(perspectivedescriptor.getId())) {
				updateTooling(perspectivedescriptor);
				page.savePerspective();
			}


		}
		updatePreferencePage(perspectivedescriptor);
		Perspective perspective = ((WorkbenchPage)page).findPerspective(perspectivedescriptor);
		
		//due to a bug on the refresh, we need to call explicitly this method.
		//to remove, when the origin is found.
		if( perspective!=null){
			perspective.updateActionBars();
		}
	}

	protected void updatePreferencePage(IPerspectiveDescriptor perspectivedescriptor) {
		//test is we are interesting by the current perspective
		if(configurationservice.getConfiguration(perspectivedescriptor.getId()) != null) {

			Configuration configuration = configurationservice.getConfiguration(perspectivedescriptor.getId());
			IPreferenceNode[] preferencepagelist = window.getWorkbench().getPreferenceManager().getRootSubNodes();

			// take in account the preferences
			for(int i = 0; i < preferencepagelist.length; i++) {
				if(preferencepagelist[i] instanceof PreferenceNode) {
					PreferenceNode node = (PreferenceNode)preferencepagelist[i];
					if(!configuration.getPreferenceID().contains(node.getId())) {
						hiddenRootPref.add(node);
					}
				}
			}
			Iterator<PreferenceNode> iteratorpreftoremove = hiddenRootPref.iterator();
			while(iteratorpreftoremove.hasNext()) {
				window.getWorkbench().getPreferenceManager().remove(iteratorpreftoremove.next());
			}
		} else {
			//this is not an interesting perspective
			Iterator<PreferenceNode> iteratorpreftoremove = hiddenRootPref.iterator();
			while(iteratorpreftoremove.hasNext()) {
				window.getWorkbench().getPreferenceManager().addToRoot(iteratorpreftoremove.next());
			}
			hiddenRootPref.clear();
		}

	}

	/**
	 * this method hide menu, tool bar and action hat are not interesting
	 * 
	 * @param perspectivedescriptor
	 */
	protected void updateTooling(IPerspectiveDescriptor perspectivedescriptor) {

		CustomizeActionBars customizeActionBars = loadData();

		if(configurationservice.getConfiguration(perspectivedescriptor.getId()) != null) {
			Configuration configuration = configurationservice.getConfiguration(perspectivedescriptor.getId());
			//Activator.log.debug("Update tooling");
			//Load all information about menus....
			WorkbenchPage worbenchPage = (WorkbenchPage)window.getActivePage();
			// Get the perspective
			Perspective perspective = worbenchPage.findPerspective(perspectivedescriptor);

			ICommandService cmdService = (ICommandService)worbenchPage.getActivePart().getSite().getService(ICommandService.class);


			ArrayList<String> commandIdToRemove = new ArrayList<String>();

			try {

				//put all commands that not reference to the given category
				for(int i = 0; i < cmdService.getDefinedCommands().length; i++) {
					if(!configuration.getCategoryIDList().contains(cmdService.getDefinedCommands()[i].getCategory().getId())) {
						commandIdToRemove.add(cmdService.getDefinedCommands()[i].getId());
					}
					//	else{System.err.println("keep "+cmdService.getDefinedCommands()[i].getId());}
				}

				//then verify for each command by taking account its id.
				for(int i = 0; i < cmdService.getDefinedCommands().length; i++) {
					//put the commands in the list to hide, theis command are not referenced in the given list of command id and are not in the given list of category 
					if(!configuration.getCommandIDList().contains(cmdService.getDefinedCommands()[i].getId())) {
						if(!configuration.getCategoryIDList().contains(cmdService.getDefinedCommands()[i].getCategory().getId())) {
							if(!commandIdToRemove.contains(cmdService.getDefinedCommands()[i].getId())) {
								commandIdToRemove.add(cmdService.getDefinedCommands()[i].getId());
							}
						}
					} else {//may be the id that we want to keep was in already in the list because of the filter on category
						if(commandIdToRemove.contains(cmdService.getDefinedCommands()[i].getId())) {
							commandIdToRemove.remove(cmdService.getDefinedCommands()[i].getId());
						}
					}
				}
			} catch (Exception e) {
				System.err.println(e);
			}


			perspective.getHiddenMenuItems().clear();
			perspective.getHiddenToolbarItems().clear();

			//look for all actionSet
			ArrayList<IActionSetDescriptor> actionSetToRemove = new ArrayList<IActionSetDescriptor>();
			Iterator<IActionSetDescriptor> iteratorActionSet = customizeActionBars.getActionSet().iterator();
			while(iteratorActionSet.hasNext()) {
				IActionSetDescriptor currentActionSetDescriptor = iteratorActionSet.next();
				//test if the configuationSet contains the id
				if(!configuration.getActionSetIDList().contains(currentActionSetDescriptor.getId())) {
					actionSetToRemove.add(currentActionSetDescriptor);
				}
			}



			// look for all menu
			ArrayList<String> menuToRemove = new ArrayList<String>();

			for(int i = 0; i < customizeActionBars.getMenuManager().getItems().length; i++) {
				if(!configuration.getMenuIDList().contains(customizeActionBars.getMenuManager().getItems()[i].getId())) {
					menuToRemove.add(customizeActionBars.getMenuManager().getItems()[i].getId());
				}
			}

			//look for all toolbar
			ArrayList<String> toolbarToRemove = new ArrayList<String>();
			for(int i = 0; i < customizeActionBars.getToolBarManager().getItems().length; i++) {

				if(!configuration.getToolBarID().contains(customizeActionBars.getToolBarManager().getItems()[i].getId())) {
					toolbarToRemove.add(customizeActionBars.getToolBarManager().getItems()[i].getId());
				}
			}

			perspective.getHiddenMenuItems().addAll(commandIdToRemove);
			perspective.getHiddenToolbarItems().addAll(commandIdToRemove);
			//hide elements
			perspective.turnOffActionSets((IActionSetDescriptor[])actionSetToRemove.toArray(new IActionSetDescriptor[actionSetToRemove.size()]));
			perspective.getHiddenMenuItems().addAll(menuToRemove);
			perspective.getHiddenToolbarItems().addAll(toolbarToRemove);
			perspective.updateActionBars();

		}

	}

	/**
	 * this method search all identifier of menu toolbar and actionSet that are loaded in eclipse
	 * 
	 * @return a structure that contains all references of the toolbar, menu, and actionSet..
	 */
	protected CustomizeActionBars loadData() {


		CustomizeActionBars ownedActionBar = new CustomizeActionBars(null);
		//actionSet
		// Just get the action sets at this point. Do not load the action set
		// until it is actually selected in the dialog.
		ActionSetRegistry reg = WorkbenchPlugin.getDefault().getActionSetRegistry();
		IActionSetDescriptor[] sets = reg.getActionSets();
		IActionSetDescriptor[] actionSetDescriptors = ((WorkbenchPage)window.getActivePage()).getActionSets();
		List initiallyAvailableActionSets = Arrays.asList(actionSetDescriptors);
		ownedActionBar.setActionSet(initiallyAvailableActionSets);



		// Fill fake action bars with static menu information.
		((WorkbenchWindow)window).fillActionBars(ownedActionBar, ActionBarAdvisor.FILL_PROXY | ActionBarAdvisor.FILL_MENU_BAR | ActionBarAdvisor.FILL_COOL_BAR);
		//	Activator.log.debug("\n+-> ActionSetDescriptor");
		for(int i = 0; i < sets.length; i++) {
			ActionSetDescriptor actionSetDesc = (ActionSetDescriptor)sets[i];
			//	Activator.log.debug("+--->" + actionSetDesc.getId());

		}

		final IMenuService menuService = (IMenuService)window.getService(IMenuService.class);
		menuService.populateContributionManager((ContributionManager)ownedActionBar.getMenuManager(), MenuUtil.MAIN_MENU);
		//Activator.log.debug("\n+-> Menu " + ownedActionBar.getMenuManager().getItems().length);
		for(int i = 0; i < ownedActionBar.getMenuManager().getItems().length; i++) {
			IContributionItem item = ownedActionBar.getMenuManager().getItems()[i];
			//	Activator.log.debug("+--->" + item.getId());
		}


		IToolBarManager toolBarManager = ownedActionBar.getToolBarManager();
		menuService.populateContributionManager((ContributionManager)toolBarManager, MenuUtil.MAIN_TOOLBAR);
		//Activator.log.debug("\n+-> toolbar " + ownedActionBar.getToolBarManager() + " " + ownedActionBar.getToolBarManager().getItems().length);
		for(int i = 0; i < ownedActionBar.getToolBarManager().getItems().length; i++) {
			IContributionItem item = toolBarManager.getItems()[i];
			//Activator.log.debug("+--->" + item.getId());
		}

		return ownedActionBar;
	}

	/**
	 * {@inheritDoc}
	 */

	public void perspectiveDeactivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
		//in the case of reset this method is called.
		//Activator.log.debug("perspectiveDeactivated "+perspective.getId());
		//updateTooling(perspective);
		//page.savePerspective();
	}
}

Back to the top