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