From dfb964b89adf01a89c0d1de64c16e62c0e002d80 Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Fri, 22 Aug 2014 08:04:52 +0000 Subject: [PATCH] refs #5184 - Modified EnumerationTab add button to activate the newly created index for renaming - Rename for EnumerationIndex can be executed via context menu "Rename" or F2 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@30115 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn.ui/plugin.xml | 26 +++ .../EnumerationIndexRenameNodeHandler.java | 73 +++++++++ .../sysdyn/ui/properties/EnumerationTab.java | 151 ++++++++++++++++-- .../widgets/arrays/EnumerationIndexNode.java | 12 +- 4 files changed, 242 insertions(+), 20 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/EnumerationIndexRenameNodeHandler.java diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index 7b06dd5b..4334595d 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -811,6 +811,21 @@ tooltip="Exports the trend to SVG file"> + + + + + + + + + + + + + + + + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/EnumerationIndexRenameNodeHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/EnumerationIndexRenameNodeHandler.java new file mode 100644 index 00000000..11b5cc3b --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/EnumerationIndexRenameNodeHandler.java @@ -0,0 +1,73 @@ +/******************************************************************************* + * Copyright (c) 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.sysdyn.ui.handlers; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IWorkbenchPart; +import org.eclipse.ui.IWorkbenchSite; +import org.eclipse.ui.handlers.HandlerUtil; +import org.eclipse.ui.part.IPage; +import org.eclipse.ui.part.IPageSite; +import org.simantics.browsing.ui.GraphExplorer; +import org.simantics.browsing.ui.NodeContext; +import org.simantics.browsing.ui.common.ColumnKeys; +import org.simantics.browsing.ui.platform.PropertyPageView; +import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite; +import org.simantics.sysdyn.ui.properties.SysdynPropertyPage; +import org.simantics.utils.ui.ISelectionUtils; + +public class EnumerationIndexRenameNodeHandler extends AbstractHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + ISelection sel2 = HandlerUtil.getActiveMenuSelection(event); + NodeContext ctx = ISelectionUtils.filterSingleSelection(sel2, NodeContext.class); + if (ctx == null) + return null; + + IWorkbenchPart part = HandlerUtil.getActivePart(event); + if (part == null) + return null; + + PropertyPageView ppv = (PropertyPageView) part; + IPage p = ppv.getCurrentPage(); + Control control = p.getControl(); + + Control geComposite = findGEComposite(control); + GraphExplorerComposite ge = (GraphExplorerComposite) geComposite; + ge.getExplorer().startEditing(ctx, ColumnKeys.SINGLE); + + return null; + } + + private Control findGEComposite(Control control) { + Control[] comp = ((Composite) control).getChildren(); + Control ge = null; + for (Control cont : comp) { + if (cont instanceof GraphExplorerComposite) + return cont; + else if (cont instanceof Composite) { + ge = findGEComposite(cont); + return ge; + } + else + continue; + } + return ge; + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EnumerationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EnumerationTab.java index 7514f47c..4252b6cd 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EnumerationTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EnumerationTab.java @@ -15,6 +15,10 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.viewers.ISelectionProvider; @@ -29,7 +33,9 @@ import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeItem; import org.eclipse.ui.IWorkbenchSite; +import org.simantics.Simantics; import org.simantics.browsing.ui.NodeContext; +import org.simantics.browsing.ui.common.ColumnKeys; import org.simantics.browsing.ui.swt.SingleSelectionInputSource; import org.simantics.browsing.ui.swt.widgets.Button; import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite; @@ -47,6 +53,7 @@ import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.utils.ListUtils; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.Variables; import org.simantics.db.management.ISessionContext; @@ -62,8 +69,8 @@ import org.simantics.sysdyn.ui.properties.widgets.arrays.EnumerationIndexNode; import org.simantics.sysdyn.ui.properties.widgets.arrays.ReplaceableIndexesWidget; import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameInputValidator; import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNamePropertyModifier; -import org.simantics.ui.SimanticsUI; import org.simantics.utils.datastructures.ArrayMap; +import org.simantics.utils.threads.SWTThread; import org.simantics.utils.ui.AdaptionUtils; public class EnumerationTab extends LabelPropertyTabContributor implements Widget { @@ -78,6 +85,10 @@ public class EnumerationTab extends LabelPropertyTabContributor implements Widge Variable variable; Table table; + + private Button add; + private Button remove; + @Override public void createControls(Composite body, IWorkbenchSite site, final ISessionContext context, WidgetSupport support) { @@ -97,6 +108,8 @@ public class EnumerationTab extends LabelPropertyTabContributor implements Widge indexExplorer = new GraphExplorerComposite(ArrayMap.keys( "displaySelectors", "displayFilter").values(false, false), site, container, support, SWT.FULL_SELECTION | SWT.BORDER | SWT.MULTI | SWT.CHECK); + indexExplorer.setContextMenuId("#EnumerationTabPopup"); + indexExplorer .setBrowseContexts(SysdynResource.URIs.EnumerationIndexes); indexExplorer.setInputSource(new SingleSelectionInputSource( @@ -112,7 +125,7 @@ public class EnumerationTab extends LabelPropertyTabContributor implements Widge final EnumerationIndexNode node = (EnumerationIndexNode) context.getAdapter(EnumerationIndexNode.class); node.setShowInChartsSelected(checked); - SimanticsUI.getSession().asyncRequest(new ReadRequest() { + Simantics.getSession().asyncRequest(new ReadRequest() { @Override public void run(ReadGraph graph) throws DatabaseException { updateModelResults(graph); @@ -122,7 +135,7 @@ public class EnumerationTab extends LabelPropertyTabContributor implements Widge } }); - + indexExplorer.finish(); GridDataFactory.fillDefaults().grab(true, true).span(4, 1).applyTo( @@ -133,13 +146,13 @@ public class EnumerationTab extends LabelPropertyTabContributor implements Widge ((Tree) c).setLinesVisible(true); - final Button add = new Button(container, support, SWT.None); + add = new Button(container, support, SWT.None); add.setText("Add"); - add.addSelectionListener(new addEnumerationIndexListener(support)); + add.addSelectionListener(new AddEnumerationIndexListener(support)); - final Button remove = new Button(container, support, SWT.None); + remove = new Button(container, support, SWT.None); remove.setText("Remove"); - remove.addSelectionListener(new removeEnumerationIndexListener(support)); + remove.addSelectionListener(new RemoveEnumerationIndexListener(support)); ReplaceableIndexesWidget externalIndexes = new ReplaceableIndexesWidget(container, support, SWT.NULL); GridDataFactory.fillDefaults().applyTo(externalIndexes.getWidget()); @@ -150,11 +163,11 @@ public class EnumerationTab extends LabelPropertyTabContributor implements Widge } - private class addEnumerationIndexListener implements SelectionListener, Widget { + private class AddEnumerationIndexListener implements SelectionListener, Widget { Resource enumerationIndexes; - public addEnumerationIndexListener(WidgetSupport support) { + public AddEnumerationIndexListener(WidgetSupport support) { support.register(this); } @@ -177,10 +190,15 @@ public class EnumerationTab extends LabelPropertyTabContributor implements Widge @Override public void widgetSelected(SelectionEvent e) { try { - SimanticsUI.getSession().syncRequest(new WriteRequest() { + + currentItemCount = getCurrentItemCount(); + lastItemCount = currentItemCount; + + Simantics.getSession().syncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { + graph.markUndoPoint(); SysdynResource sr = SysdynResource.getInstance(graph); Layer0 l0 = Layer0.getInstance(graph); @@ -195,8 +213,17 @@ public class EnumerationTab extends LabelPropertyTabContributor implements Widge ArrayList index = new ArrayList(); index.add(ei); ListUtils.insertBack(graph, enumerationIndexes, index); + + List list = ListUtils.toList(graph, enumerationIndexes); + if (!list.isEmpty()) + updateRemoveButton(true); + + Layer0Utils.addCommentMetadata(graph, "Added new Enumeration Index " + NameUtils.getSafeName(graph, ei) + " to " + enumerationIndexes); } }); + + enableItemForRename(); + } catch (DatabaseException e1) { e1.printStackTrace(); } @@ -205,16 +232,16 @@ public class EnumerationTab extends LabelPropertyTabContributor implements Widge @Override public void widgetDefaultSelected(SelectionEvent e) { - + System.out.println("asd"); } } - private class removeEnumerationIndexListener implements SelectionListener, Widget { + private class RemoveEnumerationIndexListener implements SelectionListener, Widget { Resource enumerationIndexes; - public removeEnumerationIndexListener(WidgetSupport support) { + public RemoveEnumerationIndexListener(WidgetSupport support) { support.register(this); } @@ -227,6 +254,9 @@ public class EnumerationTab extends LabelPropertyTabContributor implements Widge @Override public void run(ReadGraph graph) throws DatabaseException { enumerationIndexes = graph.getSingleObject(enumeration, SysdynResource.getInstance(graph).Enumeration_enumerationIndexList); + List list = ListUtils.toList(graph, enumerationIndexes); + if (list.isEmpty()) + updateRemoveButton(false); } }); } catch (DatabaseException e) { @@ -240,16 +270,21 @@ public class EnumerationTab extends LabelPropertyTabContributor implements Widge final IStructuredSelection selection = (IStructuredSelection)selectionProvider.getSelection(); try { - SimanticsUI.getSession().syncRequest(new WriteRequest() { + Simantics.getSession().syncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { + graph.markUndoPoint(); for(Object o : selection.toList()) { Resource r = AdaptionUtils.adaptToSingle(o, Resource.class); if(r == null) continue; ListUtils.removeElement(graph, enumerationIndexes, r); } + List list = ListUtils.toList(graph, enumerationIndexes); + if (list.isEmpty()) + updateRemoveButton(false); + Layer0Utils.addCommentMetadata(graph, "Removed Enumeration Index " ); } }); } catch (DatabaseException e1) { @@ -331,10 +366,11 @@ public class EnumerationTab extends LabelPropertyTabContributor implements Widge public void widgetSelected(SelectionEvent e) { final boolean selected = showAll.getWidget().getSelection(); try { - SimanticsUI.getSession().syncRequest(new WriteRequest() { + Simantics.getSession().syncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { + graph.markUndoPoint(); SysdynResource sr = SysdynResource.getInstance(graph); List indexes = ListUtils.toList(graph, enumerationIndexes); for(Resource index : indexes) { @@ -345,7 +381,7 @@ public class EnumerationTab extends LabelPropertyTabContributor implements Widge graph.claimLiteral(index, sr.EnumerationIndex_showEnumerationIndexInCharts, false, Bindings.BOOLEAN); } } - + Layer0Utils.addCommentMetadata(graph, "Modified Show all in charts"); updateModelResults(graph); } }); @@ -385,4 +421,87 @@ public class EnumerationTab extends LabelPropertyTabContributor implements Widge public void setInput(ISessionContext context, Object input) { variable = AdaptionUtils.adaptToSingle(input, Variable.class); } + + private void updateRemoveButton(final boolean enable) { + SWTThread.getThreadAccess().asyncExec(new Runnable() { + + @Override + public void run() { + remove.getWidget().setEnabled(enable); + if (!enable) + add.getWidget().setFocus(); + } + }); + } + + private int currentItemCount = 0; + private int lastItemCount = 0; + + private void enableItemForRename() { + + + + Job j = new Job("") { + + @Override + protected IStatus run(IProgressMonitor monitor) { + + while (lastItemCount == currentItemCount) { + SWTThread.getThreadAccess().syncExec(new Runnable() { + + @Override + public void run() { + lastItemCount = getCurrentItemCount(); + } + }); + try { + Thread.sleep(50); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + SWTThread.getThreadAccess().syncExec(new Runnable() { + + @Override + public void run() { + Control c = indexExplorer.getExplorer().getControl(); + if (c instanceof Tree) { + Tree tree = (Tree) c; + TreeItem[] items = tree.getItems(); + TreeItem lastItem = items[items.length - 1]; + tree.setSelection(lastItem); + } + } + }); + + SWTThread.getThreadAccess().syncExec(new Runnable() { + + @Override + public void run() { + Control c = indexExplorer.getExplorer().getControl(); + if (c instanceof Tree) { + Tree tree = (Tree) c; + TreeItem[] items = tree.getItems(); + TreeItem lastItem = items[items.length - 1]; + NodeContext context = (NodeContext)lastItem.getData(); + tree.setSelection(lastItem); + indexExplorer.getExplorer().startEditing(context, ColumnKeys.SINGLE); + tree.setSelection(lastItem); + } + } + }); + + return Status.OK_STATUS; + } + }; + j.schedule(); + } + + public int getCurrentItemCount() { + Control c = indexExplorer.getExplorer().getControl(); + Tree tree = (Tree) c; + return tree.getItemCount(); + } } + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/EnumerationIndexNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/EnumerationIndexNode.java index ff7b7021..b2ca447b 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/EnumerationIndexNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/EnumerationIndexNode.java @@ -1,5 +1,6 @@ package org.simantics.sysdyn.ui.properties.widgets.arrays; +import org.simantics.Simantics; import org.simantics.browsing.ui.common.node.AbstractNode; import org.simantics.browsing.ui.common.node.IDoubleClickableNode; import org.simantics.browsing.ui.common.node.IModifiableNode; @@ -8,12 +9,13 @@ import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.db.request.Read; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.utils.VariableNameValidator; -import org.simantics.ui.SimanticsUI; public class EnumerationIndexNode extends AbstractNode implements IModifiableNode, IDoubleClickableNode { @@ -38,7 +40,7 @@ public class EnumerationIndexNode extends AbstractNode implements IMo }; try { - return SimanticsUI.getSession().syncRequest(request); + return Simantics.getSession().syncRequest(request); } catch (DatabaseException e) { e.printStackTrace(); return ""; @@ -54,7 +56,7 @@ public class EnumerationIndexNode extends AbstractNode implements IMo @Override public void modify(final String label) { - SimanticsUI.getSession().asyncRequest(new WriteRequest() { + Simantics.getSession().asyncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { graph.claimLiteral(data, Layer0.getInstance(graph).HasName, label); @@ -78,11 +80,13 @@ public class EnumerationIndexNode extends AbstractNode implements IMo public void setShowInChartsSelected(final boolean selected) { try { - SimanticsUI.getSession().syncRequest(new WriteRequest() { + Simantics.getSession().syncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { + graph.markUndoPoint(); SysdynResource sr = SysdynResource.getInstance(graph); graph.claimLiteral(data, sr.EnumerationIndex_showEnumerationIndexInCharts, selected); + Layer0Utils.addCommentMetadata(graph, "Modified show in chart of " + NameUtils.getSafeName(graph, data) + " to " + selected); } }); } catch (DatabaseException e) { -- 2.47.1