import org.simantics.diagram.synchronization.ErrorHandler;
import org.simantics.g2d.canvas.ICanvasContext;
import org.simantics.g2d.diagram.IDiagram;
-import org.simantics.g2d.diagram.handler.SubstituteElementClass;
import org.simantics.g2d.element.ElementClass;
import org.simantics.g2d.element.IElement;
}
@Override
- public void execute(AsyncReadGraph graph, ElementClass mutableClazz) {
-
- List<SubstituteElementClass> substitutes = diagram.getDiagramClass().getItemsByClass(SubstituteElementClass.class);
- for (SubstituteElementClass subs : substitutes) {
- mutableClazz = subs.substitute(diagram, mutableClazz);
- }
- final ElementClass ec = mutableClazz;
-
+ public void execute(AsyncReadGraph graph, final ElementClass ec) {
+
graph.asyncRequest(new SpawnRequest(canvas, ec, data), new TransientCacheAsyncListener<IElement>() {
@Override
*******************************************************************************/
package org.simantics.diagram.adapter;
-import java.util.List;
-
import org.simantics.db.AsyncReadGraph;
import org.simantics.db.Resource;
import org.simantics.db.common.primitiverequest.Adapter;
import org.simantics.diagram.synchronization.ErrorHandler;
import org.simantics.g2d.canvas.ICanvasContext;
import org.simantics.g2d.diagram.IDiagram;
-import org.simantics.g2d.diagram.handler.SubstituteElementClass;
import org.simantics.g2d.element.ElementClass;
import org.simantics.g2d.element.IElement;
import org.simantics.scl.runtime.tuple.Tuple3;
}
@Override
- public void execute(AsyncReadGraph graph, ElementClass mutableClazz) {
-
- List<SubstituteElementClass> substitutes = diagram.getDiagramClass().getItemsByClass(SubstituteElementClass.class);
- for (SubstituteElementClass subs : substitutes) {
- mutableClazz = subs.substitute(diagram, mutableClazz);
- }
- final ElementClass ec = mutableClazz;
+ public void execute(AsyncReadGraph graph, final ElementClass ec) {
graph.asyncRequest(new SpawnRequest(canvas, ec, data), new TransientCacheAsyncListener<IElement>() {
import org.simantics.g2d.element.IElement;
import org.simantics.g2d.element.IElementClassProvider;
import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd;
-import org.simantics.g2d.element.handler.ElementHandler;
import org.simantics.g2d.element.handler.ElementLayerListener;
-import org.simantics.g2d.element.handler.ElementLayers;
import org.simantics.g2d.element.handler.TerminalTopology;
import org.simantics.g2d.element.impl.Element;
import org.simantics.g2d.layers.ILayer;
public ElementClass substitute(IDiagram d, ElementClass ec) {
if (d != diagram)
throw new IllegalArgumentException("specified diagram does not have this SubstituteElementClass handler");
-
- // If the element class is our own, there's no point in creating
- // a copy of it.
- /*if (ec.contains(elementLayerListener))
- return ec;*/
-
- List<ElementHandler> all = ec.getAll();
- List<ElementHandler> result = new ArrayList<ElementHandler>(all);
- for (ElementHandler eh : all) {
- if (eh instanceof ElementLayers)
- result.add(elementLayerListener);
- }
- return ElementClass.compile(result, false).setId(ec.getId());
+ // Nothing to substitute here
+ return ec;
}
}
}
}
+ @Override
+ public void flush() {
+ modificationQueue.flush();
+ }
+
void changeTag(IElement e, Resource tag, boolean set) {
Object object = e.getHint(ElementHints.KEY_OBJECT);
Resource tagged = null;
if (tagged == null)
return;
- modificationQueue.async(new TagChange(tagged, tag, set), null);
+ modificationQueue.offer(new TagChange(tagged, tag, set), null);
}
};
d.setHint(DiagramHints.KEY_LAYERS, layers);
d.setHint(DiagramHints.KEY_LAYERS_EDITOR, layers);
+ d.setHint(DiagramHints.KEY_ELEMENT_LAYER_LISTENER, elementLayerListener);
d.addCompositionVetoListener(diagramListener);
d.addCompositionListener(diagramListener);
import org.simantics.db.exception.CancelTransactionException;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.exception.ServiceException;
+import org.simantics.db.layer0.util.RemoverUtil;
import org.simantics.db.procedure.AsyncProcedure;
import org.simantics.db.procedure.Listener;
import org.simantics.diagram.stubs.DiagramResource;
import org.simantics.g2d.diagram.IDiagram;
import org.simantics.g2d.element.ElementHints;
import org.simantics.g2d.element.IElement;
-import org.simantics.g2d.element.handler.ElementLayers;
import org.simantics.g2d.layers.IEditableLayer;
import org.simantics.g2d.layers.IEditableLayer.ILayerListener;
import org.simantics.g2d.layers.IEditableLayer.LayerChangeEvent;
if (removedName.equals(name)) {
g.denyStatement(diagram, dia.HasLayer, l);
deleteLayer(g, l);
-
- // NOTE: leave the layer tags intact, remove them gradually
- // by checking the validity of all layer tags during element
- // writeback.
layers.remove(name);
return;
}
}
void deleteLayer(WriteGraph g, Resource layer) throws DatabaseException {
- g.deny(layer);
+ RemoverUtil.remove(g, layer);
}
}
}
}
- void putElementOnVisibleLayers(IDiagram diagram, IElement element) {
- // Make the new element visible and focusable on all currently
- // active layers.
- ILayers diagramLayers = diagram.getHint(DiagramHints.KEY_LAYERS);
- if (diagramLayers != null) {
- element.setHint(ElementHints.KEY_VISIBLE_LAYERS, new HashSet<ILayer>());
- element.setHint(ElementHints.KEY_FOCUS_LAYERS, new HashSet<ILayer>());
- Set<ILayer> visibleLayers = diagramLayers.getVisibleLayers();
-
- if (DEBUG_LAYERS)
- System.out.println("Marking element visible and focusable only on visible layers: " + visibleLayers);
-
- for (ElementLayers elementLayers : element.getElementClass().getItemsByClass(ElementLayers.class)) {
- for (ILayer layer : visibleLayers) {
- elementLayers.setVisibility(element, layer, true);
- elementLayers.setFocusability(element, layer, true);
- }
- }
- }
- }
-
public void putElementOnVisibleLayers(IDiagram diagram, WriteGraph g, Resource element) throws DatabaseException {
// Make the new element visible and focusable on all currently
// active layers.
import org.simantics.g2d.diagram.handler.impl.PickContextImpl;
import org.simantics.g2d.diagram.participant.ElementPainter;
import org.simantics.g2d.element.IElement;
+import org.simantics.g2d.element.handler.ElementLayerListener;
import org.simantics.g2d.layers.ILayers;
import org.simantics.g2d.layers.ILayersEditor;
import org.simantics.g2d.routing.IRouter2;
public static final Key KEY_LAYERS_EDITOR = new KeyOf(ILayersEditor.class, "LAYERS_EDITOR");
+ public static final Key KEY_ELEMENT_LAYER_LISTENER = new KeyOf(ElementLayerListener.class, "ELEMENT_LAYER_LISTENER");
+
public static final Key KEY_FIXED_LAYERS = new KeyOf(String[].class, "FIXED_LAYERS");
public static final Key KEY_MUTATOR = new KeyOf(DiagramMutator.class, "MUTATIONS");
/**
* @author Tuukka Lehtonen
*/
-public interface ElementLayerListener extends ElementHandler {
+public interface ElementLayerListener {
void visibilityChanged(IElement e, ILayer layer, boolean visible);
void focusabilityChanged(IElement e, ILayer layer, boolean focusable);
+ void flush();
}
*******************************************************************************/
package org.simantics.g2d.element.handler;
-import java.util.Collection;
-
import org.simantics.g2d.element.IElement;
import org.simantics.g2d.layers.ILayer;
import org.simantics.g2d.layers.ILayers;
boolean isVisible(IElement e, ILayer layer);
boolean isFocusable(IElement e, ILayers layers);
boolean isFocusable(IElement e, ILayer layer);
- boolean setVisibility(IElement e, ILayer layer, boolean value);
- boolean setFocusability(IElement e, ILayer layer, boolean value);
}
import org.simantics.g2d.element.ElementHints;
import org.simantics.g2d.element.IElement;
-import org.simantics.g2d.element.handler.ElementLayerListener;
import org.simantics.g2d.element.handler.ElementLayers;
import org.simantics.g2d.layers.ILayer;
import org.simantics.g2d.layers.ILayers;
}
- @Override
- public boolean setVisibility(IElement e, ILayer layer, boolean value) {
-
- Set<ILayer> elementLayers = (Set<ILayer>) e.getHint(ElementHints.KEY_VISIBLE_LAYERS);
- if (elementLayers == null)
- return false;
- boolean result;
- if (value)
- result = elementLayers.add(layer);
- else
- result = elementLayers.remove(layer);
-
- if (result) {
- for (ElementLayerListener ell : e.getElementClass().getItemsByClass(ElementLayerListener.class)) {
- ell.visibilityChanged(e, layer, value);
- }
- }
-
- return result;
- }
-
- @Override
- public boolean setFocusability(IElement e, ILayer layer, boolean value) {
-
- Set<ILayer> elementLayers = (Set<ILayer>) e.getHint(ElementHints.KEY_FOCUS_LAYERS);
- if (elementLayers == null)
- return false;
- boolean result;
- if (value)
- result = elementLayers.add(layer);
- else
- result = elementLayers.remove(layer);
-
- if (result) {
- for (ElementLayerListener ell : e.getElementClass().getItemsByClass(ElementLayerListener.class)) {
- ell.focusabilityChanged(e, layer, value);
- }
- }
-
- return result;
- }
-
}
import org.simantics.g2d.diagram.IDiagram;
import org.simantics.g2d.diagram.participant.Selection;
import org.simantics.g2d.element.ElementClass;
+import org.simantics.g2d.element.ElementHints;
import org.simantics.g2d.element.IElement;
+import org.simantics.g2d.element.handler.ElementLayerListener;
import org.simantics.g2d.element.handler.ElementLayers;
import org.simantics.g2d.layers.IEditableLayer;
import org.simantics.g2d.layers.ILayer;
return null;
}
- boolean setAttribute(IElement e, ILayer layer, Attribute attribute, boolean value) {
- ElementClass ec = e.getElementClass();
- for (ElementLayers el : ec.getItemsByClass(ElementLayers.class)) {
- switch (attribute) {
- case Visible:
- return el.setVisibility(e, layer, value);
- case Focusable:
- return el.setFocusability(e, layer, value);
- }
- }
- return false;
- }
-
Tristate getJointAttribute(Collection<IElement> elements, ILayer layer, Attribute attribute) {
Tristate state = null;
for (IElement e : elements) {
return state;
}
- int setAttribute(Collection<IElement> elements, ILayer layer, Attribute attribute, boolean value) {
- int result = 0;
- for (IElement e : elements) {
- if (setAttribute(e, layer, attribute, value))
- ++result;
+ void setAttribute(Collection<IElement> elements, ILayer layer, Attribute attribute, boolean value) {
+ // Short-circuit the hint updates so that the result will be immediately available for viewer.refresh().
+ // There is no listener for the element layer hints so the viewer won't update the tristates automatically!
+
+ IDiagram diagram = context.getDefaultHintContext().getHint(DiagramHints.KEY_DIAGRAM);
+ ElementLayerListener elementLayerListener = diagram.getHint(DiagramHints.KEY_ELEMENT_LAYER_LISTENER);
+ switch (attribute) {
+ case Visible:
+ for (IElement e : elements) {
+ Set<ILayer> elementLayers = (Set<ILayer>) e.getHint(ElementHints.KEY_VISIBLE_LAYERS);
+ if (elementLayers != null) {
+ if (value)
+ elementLayers.add(layer);
+ else
+ elementLayers.remove(layer);
+ }
+ elementLayerListener.visibilityChanged(e, layer, value);
+ }
+ break;
+ case Focusable:
+ for (IElement e : elements) {
+ Set<ILayer> elementLayers = (Set<ILayer>) e.getHint(ElementHints.KEY_FOCUS_LAYERS);
+ if (elementLayers != null) {
+ if (value)
+ elementLayers.add(layer);
+ else
+ elementLayers.remove(layer);
+ }
+ elementLayerListener.focusabilityChanged(e, layer, value);
+ }
+ break;
}
- return result;
+ elementLayerListener.flush();
}
}
@Override
public void hintRemoved(IHintObservable sender, Key key, Object oldValue) {
- // TODO Auto-generated method stub
- System.out.println("REM");
}
});
}
};
+ private static String findFreshName(ILayers layers, String proposal) {
+ Set<ILayer> all = layers.getLayers();
+ String name = proposal;
+ int i = 1;
+ while (true) {
+ boolean match = false;
+ for (ILayer layer : all) {
+ if (name.equals(layer.getName())) {
+ match = true;
+ break;
+ }
+ }
+ if (!match)
+ return name;
+ ++i;
+ name = proposal + " " + i; //$NON-NLS-1$
+ }
+ }
+
private void initialize2(ILayersEditor layers, IDiagram diagram) {
layers.addLayersListener(layersListener);
addButton.setToolTipText(Messages.DiagramLayersPage_NewTT);
addButton.addSelectionListener(new SelectionListener() {
- String findFreshName(ILayers layers, String proposal) {
- Set<ILayer> all = layers.getLayers();
- String name = proposal;
- int i = 1;
- while (true) {
- boolean match = false;
- for (ILayer layer : all) {
- if (name.equals(layer.getName())) {
- match = true;
- break;
- }
- }
- if (!match)
- return name;
- ++i;
- name = proposal + " " + i; //$NON-NLS-1$
- }
- }
-
@Override
public void widgetSelected(SelectionEvent e) {
String name = findFreshName(layers, Messages.DiagramLayersPage_NewRole);
// FIXME: Eclipse currently eats F2 presses. This should be
// implemented as a command handler or find some way to
// force these listeners to have priority...
- System.out.println("startediting"); //$NON-NLS-1$
-
TreeItem[] items = viewer.getTree().getSelection();
if(items.length != 1)
return;
rect = item.getBounds(1);
if (rect.contains(pt)) {
Tristate state = getJointAttribute(elements, layer, Attribute.Visible);
- if (setAttribute(elements, layer, Attribute.Visible, state.toggle().toBoolean()) > 0) {
- refresh();
- }
+ setAttribute(elements, layer, Attribute.Visible, state.toggle().toBoolean());
+ refresh();
return;
}
Rectangle rect2 = item.getBounds(2);
if (rect2.contains(pt)) {
Tristate state = getJointAttribute(elements, layer, Attribute.Focusable);
- if (setAttribute(elements, layer, Attribute.Focusable, state.toggle().toBoolean()) > 0) {
- refresh();
- }
+ setAttribute(elements, layer, Attribute.Focusable, state.toggle().toBoolean());
+ refresh();
return;
}
}
case SWT.FocusOut:
if(layer instanceof IEditableLayer) {
IEditableLayer l = (IEditableLayer)layer;
- l.setName(text.getText());
- System.out.println("renamed layer to " + text.getText()); //$NON-NLS-1$
- //viewer.refresh();
+ String name = findFreshName(layers, text.getText());
+ l.setName(name);
+ viewer.refresh();
}
// // Item may be disposed if the tree gets reset after a previous editing.
case SWT.TRAVERSE_RETURN:
if(layer instanceof IEditableLayer) {
IEditableLayer l = (IEditableLayer)layer;
- l.setName(text.getText());
+ String name = findFreshName(layers, text.getText());
+ l.setName(name);
+ viewer.refresh();
//System.out.println("renamed layer to " + text.getText());
//viewer.refresh();
}