]> gerrit.simantics Code Review - simantics/platform.git/blob - diagramEditor/handlers/DisconnectFlagHandler.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / diagramEditor / handlers / DisconnectFlagHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2015 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor.handlers;
13
14 import java.util.ArrayList;
15 import java.util.Set;
16
17 import org.eclipse.core.commands.AbstractHandler;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.runtime.jobs.Job;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.ui.handlers.HandlerUtil;
26 import org.simantics.DatabaseJob;
27 import org.simantics.db.Resource;
28 import org.simantics.db.WriteGraph;
29 import org.simantics.db.common.request.WriteRequest;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.modeling.actions.DisconnectFlag;
32 import org.simantics.modeling.ui.Activator;
33 import org.simantics.ui.SimanticsUI;
34 import org.simantics.utils.ui.ISelectionUtils;
35
36 /**
37  * @author Tuukka Lehtonen
38  */
39 public class DisconnectFlagHandler extends AbstractHandler {
40
41     @Override
42     public Object execute(ExecutionEvent event) throws ExecutionException {
43         ISelection sel = HandlerUtil.getCurrentSelection(event);
44         final Set<Resource> resources = ISelectionUtils.filterSetSelection(sel, Resource.class);
45         if (resources.isEmpty())
46             return null;
47
48         Job job = new DatabaseJob("Disconnect Flags") {
49             @Override
50             protected IStatus run(IProgressMonitor monitor) {
51                 try {
52                     SimanticsUI.getSession().syncRequest(new WriteRequest() {
53                         @Override
54                         public void perform(WriteGraph graph) throws DatabaseException {
55                             graph.markUndoPoint();
56                             DisconnectFlag.disconnectFlags(graph, new ArrayList<>(resources));
57                         }
58                     });
59                     return Status.OK_STATUS;
60                 } catch (DatabaseException e) {
61                     return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to disconnect flags.", e); 
62                 }
63             }
64         };
65         job.schedule();
66         return null;
67     }
68
69 }