X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2FmodelBrowser%2Fhandlers%2FToggleExternalFlag.java;h=d03199d9c72f549f161d729fc82503fa749adfdd;hp=5ef611bada2975c26966dcab7f5822568269717f;hb=refs%2Fchanges%2F38%2F238%2F2;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/ToggleExternalFlag.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/ToggleExternalFlag.java index 5ef611bad..d03199d9c 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/ToggleExternalFlag.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/ToggleExternalFlag.java @@ -1,115 +1,115 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * 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: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.modeling.ui.modelBrowser.handlers; - -import java.util.Map; - -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.Command; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.core.commands.State; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.ISelectionProvider; -import org.eclipse.ui.IWorkbenchSite; -import org.eclipse.ui.PlatformUI; -import org.eclipse.ui.commands.ICommandService; -import org.eclipse.ui.commands.IElementUpdater; -import org.eclipse.ui.handlers.HandlerUtil; -import org.eclipse.ui.menus.UIElement; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.Session; -import org.simantics.db.WriteGraph; -import org.simantics.db.common.request.WriteRequest; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.request.Read; -import org.simantics.diagram.stubs.DiagramResource; -import org.simantics.modeling.ui.Activator; -import org.simantics.ui.SimanticsUI; -import org.simantics.utils.ui.AdaptionUtils; -import org.simantics.utils.ui.ErrorLogger; - -public class ToggleExternalFlag extends AbstractHandler implements IElementUpdater { - - private static Resource getFlagResource(ISelection sel) { - Resource r = AdaptionUtils.adaptToSingle(sel, Resource.class); - return r; - } - - @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - ISelection sel = HandlerUtil.getCurrentSelection(event); - final Resource resource = getFlagResource(sel); - if (resource == null) - return null; - - SimanticsUI.getSession().asyncRequest(new WriteRequest() { - @Override - public void perform(WriteGraph g) throws DatabaseException { - DiagramResource dr = DiagramResource.getInstance(g); - Boolean ext = g.hasStatement(resource, dr.ExternalFlag); - if (ext) - g.deny(resource, dr.ExternalFlag); - else - g.claim(resource, dr.ExternalFlag, resource); - } - }); - - return null; - } - - @Override - public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) { - ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class); - Command command = commandService.getCommand("org.simantics.modeling.ui.toggleExternalFlag"); - - // Get a State object for the toggleSubscriptionGroup command - State state = command.getState("org.simantics.modeling.ui.toggleExternalFlag.state"); - if (state == null) { - state = new State(); - state.setValue(Boolean.TRUE); - command.addState("org.simantics.modeling.ui.toggleExternalFlag.state", state); - } - - // Get the current value for the State object - Session s = SimanticsUI.peekSession(); - if (s != null) { - Boolean value = Boolean.TRUE; - IWorkbenchSite site = (IWorkbenchSite) parameters.get("org.eclipse.ui.part.IWorkbenchPartSite"); - ISelectionProvider sp = site.getSelectionProvider(); - if (sp != null) { - final Resource resource = getFlagResource(sp.getSelection()); - if (resource != null) { - try { - value = s.syncRequest(new Read() { - @Override - public Boolean perform(ReadGraph graph) throws DatabaseException { - DiagramResource dr = DiagramResource.getInstance(graph); - return Boolean.valueOf(graph.hasStatement(resource, dr.ExternalFlag)); - } - }); - } catch (DatabaseException e) { - ErrorLogger.defaultLogError(e); - } - } - } - state.setValue(value); - } - - Boolean checked = (Boolean) state.getValue(); - element.setChecked(checked); - element.setIcon(checked ? Activator.TICK_ICON : null); - } - -} - +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * 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: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.modeling.ui.modelBrowser.handlers; + +import java.util.Map; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.Command; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.commands.State; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.ISelectionProvider; +import org.eclipse.ui.IWorkbenchSite; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.commands.ICommandService; +import org.eclipse.ui.commands.IElementUpdater; +import org.eclipse.ui.handlers.HandlerUtil; +import org.eclipse.ui.menus.UIElement; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; +import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.modeling.ui.Activator; +import org.simantics.ui.SimanticsUI; +import org.simantics.utils.ui.AdaptionUtils; +import org.simantics.utils.ui.ErrorLogger; + +public class ToggleExternalFlag extends AbstractHandler implements IElementUpdater { + + private static Resource getFlagResource(ISelection sel) { + Resource r = AdaptionUtils.adaptToSingle(sel, Resource.class); + return r; + } + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + ISelection sel = HandlerUtil.getCurrentSelection(event); + final Resource resource = getFlagResource(sel); + if (resource == null) + return null; + + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph g) throws DatabaseException { + DiagramResource dr = DiagramResource.getInstance(g); + Boolean ext = g.hasStatement(resource, dr.ExternalFlag); + if (ext) + g.deny(resource, dr.ExternalFlag); + else + g.claim(resource, dr.ExternalFlag, resource); + } + }); + + return null; + } + + @Override + public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) { + ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class); + Command command = commandService.getCommand("org.simantics.modeling.ui.toggleExternalFlag"); + + // Get a State object for the toggleSubscriptionGroup command + State state = command.getState("org.simantics.modeling.ui.toggleExternalFlag.state"); + if (state == null) { + state = new State(); + state.setValue(Boolean.TRUE); + command.addState("org.simantics.modeling.ui.toggleExternalFlag.state", state); + } + + // Get the current value for the State object + Session s = SimanticsUI.peekSession(); + if (s != null) { + Boolean value = Boolean.TRUE; + IWorkbenchSite site = (IWorkbenchSite) parameters.get("org.eclipse.ui.part.IWorkbenchPartSite"); + ISelectionProvider sp = site.getSelectionProvider(); + if (sp != null) { + final Resource resource = getFlagResource(sp.getSelection()); + if (resource != null) { + try { + value = s.syncRequest(new Read() { + @Override + public Boolean perform(ReadGraph graph) throws DatabaseException { + DiagramResource dr = DiagramResource.getInstance(graph); + return Boolean.valueOf(graph.hasStatement(resource, dr.ExternalFlag)); + } + }); + } catch (DatabaseException e) { + ErrorLogger.defaultLogError(e); + } + } + } + state.setValue(value); + } + + Boolean checked = (Boolean) state.getValue(); + element.setChecked(checked); + element.setIcon(checked ? Activator.TICK_ICON : null); + } + +} +