]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/DuplicatePinnedViewHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / DuplicatePinnedViewHandler.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.ui.actions;\r
13 \r
14 import java.util.HashMap;\r
15 import java.util.Map;\r
16 import java.util.UUID;\r
17 \r
18 import org.eclipse.core.commands.AbstractHandler;\r
19 import org.eclipse.core.commands.ExecutionEvent;\r
20 import org.eclipse.core.commands.ExecutionException;\r
21 import org.eclipse.jface.viewers.ISelection;\r
22 import org.eclipse.ui.IWorkbenchPart;\r
23 import org.eclipse.ui.commands.ICommandService;\r
24 import org.eclipse.ui.handlers.HandlerUtil;\r
25 import org.eclipse.ui.services.IServiceScopes;\r
26 import org.simantics.browsing.ui.platform.PropertyPageView;\r
27 import org.simantics.utils.ui.ExceptionUtils;\r
28 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
29 \r
30 /**\r
31  * @author Tuukka Lehtonen\r
32  */\r
33 public class DuplicatePinnedViewHandler extends AbstractHandler {\r
34 \r
35     private static final String PIN_SELECTION_COMMAND = "org.simantics.modeling.ui.pinSelection";\r
36 \r
37     @Override\r
38     public Object execute(ExecutionEvent event) throws ExecutionException {\r
39         IWorkbenchPart originalPart = HandlerUtil.getActivePartChecked(event);\r
40         if (!(originalPart instanceof PropertyPageView))\r
41             return null;\r
42         PropertyPageView originalPropertyView = (PropertyPageView) originalPart;\r
43 \r
44         try {\r
45             // Activate a new unique instance of the same property page view.\r
46             // Its initialization procedures and IContributedContentsView\r
47             // implementation should take care that the view will be initialized\r
48             // with a property page just like the original.\r
49             ISelection originalSelection = originalPropertyView.getLastSelection();\r
50 \r
51             final String id = originalPart.getSite().getId() + "Pinned:" + UUID.randomUUID().toString();\r
52             PropertyPageView newPart = (PropertyPageView) WorkbenchUtils.activateView(id);\r
53 \r
54             newPart.partActivated(originalPart);\r
55             newPart.selectionChanged(originalPart, originalSelection);\r
56 \r
57             // Make sure that the view is pinned and that its pinned button is in the right state.\r
58             newPart.pinWorkbenchSelection(true);\r
59 \r
60             ICommandService commandService = (ICommandService) newPart.getViewSite().getService(ICommandService.class);\r
61             Map<Object, Object> filter = new HashMap<Object, Object>();\r
62             filter.put(IServiceScopes.PARTSITE_SCOPE, newPart.getSite());\r
63             commandService.refreshElements(PIN_SELECTION_COMMAND, filter);\r
64 \r
65         } catch (Exception e) {\r
66             ExceptionUtils.logAndShowError(e);\r
67         }\r
68 \r
69         return null;\r
70     }\r
71 \r
72 }\r