Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6d4755f440711cd6bd5a32a6b3f10e19b85e7a6f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * 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:
 * 		Juergen Haug (initial contribution)
 */
package org.eclipse.etrice.ui.behavior.fsm.support;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.etrice.core.fsm.fSM.InitialTransition;
import org.eclipse.etrice.core.fsm.fSM.ModelComponent;
import org.eclipse.etrice.core.fsm.fSM.RefinedState;
import org.eclipse.etrice.core.fsm.fSM.RefinedTransition;
import org.eclipse.etrice.core.fsm.fSM.State;
import org.eclipse.etrice.core.fsm.fSM.StateGraph;
import org.eclipse.etrice.core.fsm.fSM.StateGraphNode;
import org.eclipse.etrice.core.fsm.fSM.TrPoint;
import org.eclipse.etrice.core.fsm.fSM.Transition;
import org.eclipse.etrice.ui.behavior.fsm.provider.BaseDiagramProvider;
import org.eclipse.etrice.ui.behavior.fsm.support.IPositionProvider;
import org.eclipse.etrice.ui.behavior.fsm.support.StateSupport;
import org.eclipse.etrice.ui.behavior.fsm.support.TrPointSupport;
import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm;
import org.eclipse.graphiti.mm.algorithms.Text;
import org.eclipse.graphiti.mm.algorithms.styles.Point;
import org.eclipse.graphiti.mm.pictograms.Anchor;
import org.eclipse.graphiti.mm.pictograms.Connection;
import org.eclipse.graphiti.mm.pictograms.ConnectionDecorator;
import org.eclipse.graphiti.mm.pictograms.FreeFormConnection;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.services.Graphiti;
import org.eclipse.graphiti.services.ILinkService;
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ListExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;

/**
 * IPositionProvider based on {@linkplain BaseDiagramProvider}
 * 	Logic copied from DefaultPositionProvider
 */
@FinalFieldsConstructor
@SuppressWarnings("all")
public class BaseDiagramPositionProvider implements IPositionProvider {
  private final BaseDiagramProvider baseDiagram;
  
  public int getMargin(final StateGraphNode node) {
    int _switchResult = (int) 0;
    boolean _matched = false;
    if (node instanceof State) {
      _matched=true;
      _switchResult = StateSupport.MARGIN;
    }
    if (!_matched) {
      if (node instanceof TrPoint) {
        _matched=true;
        _switchResult = TrPointSupport.MARGIN;
      }
    }
    if (!_matched) {
      _switchResult = 0;
    }
    return _switchResult;
  }
  
  public int getMargin(final StateGraph graph) {
    return 0;
  }
  
  private IPositionProvider.PosAndSize toPosAndSize(final PictogramElement pe, final int margin) {
    IPositionProvider.PosAndSize _xblockexpression = null;
    {
      EObject _eContainer = pe.eContainer();
      GraphicsAlgorithm _graphicsAlgorithm = ((PictogramElement) _eContainer).getGraphicsAlgorithm();
      EList<GraphicsAlgorithm> _graphicsAlgorithmChildren = _graphicsAlgorithm.getGraphicsAlgorithmChildren();
      final GraphicsAlgorithm borderGa = IterableExtensions.<GraphicsAlgorithm>head(_graphicsAlgorithmChildren);
      final GraphicsAlgorithm ga = pe.getGraphicsAlgorithm();
      int _x = ga.getX();
      int _width = borderGa.getWidth();
      double _divide = (_x / ((double) _width));
      double _multiply = (_divide * this.sx);
      final double x = (_multiply + margin);
      int _y = ga.getY();
      int _height = borderGa.getHeight();
      double _divide_1 = (_y / ((double) _height));
      double _multiply_1 = (_divide_1 * this.sy);
      final double y = (_multiply_1 + margin);
      int _width_1 = ga.getWidth();
      int _minus = (_width_1 - (2 * margin));
      int _width_2 = borderGa.getWidth();
      double _divide_2 = (_minus / ((double) _width_2));
      final double width = (_divide_2 * this.sx);
      int _height_1 = ga.getHeight();
      int _minus_1 = (_height_1 - (2 * margin));
      int _height_2 = borderGa.getHeight();
      double _divide_3 = (_minus_1 / ((double) _height_2));
      final double height = (_divide_3 * this.sy);
      _xblockexpression = new IPositionProvider.PosAndSize(((int) x), ((int) y), ((int) width), ((int) height));
    }
    return _xblockexpression;
  }
  
  @Override
  public IPositionProvider.PosAndSize getGraphPosAndSize(final StateGraph sg) {
    IPositionProvider.PosAndSize _xblockexpression = null;
    {
      Collection<PictogramElement> _pictograms = this.baseDiagram.getPictograms(sg);
      final Function1<PictogramElement, Boolean> _function = (PictogramElement it) -> {
        EObject _parentBo = this.getParentBo(it);
        return Boolean.valueOf((_parentBo instanceof ModelComponent));
      };
      final PictogramElement pe = IterableExtensions.<PictogramElement>findFirst(_pictograms, _function);
      IPositionProvider.PosAndSize _xifexpression = null;
      boolean _notEquals = (!Objects.equal(pe, null));
      if (_notEquals) {
        IPositionProvider.PosAndSize _xblockexpression_1 = null;
        {
          GraphicsAlgorithm _graphicsAlgorithm = pe.getGraphicsAlgorithm();
          EList<GraphicsAlgorithm> _graphicsAlgorithmChildren = _graphicsAlgorithm.getGraphicsAlgorithmChildren();
          final GraphicsAlgorithm visibleGa = IterableExtensions.<GraphicsAlgorithm>head(_graphicsAlgorithmChildren);
          GraphicsAlgorithm _graphicsAlgorithm_1 = pe.getGraphicsAlgorithm();
          int _x = _graphicsAlgorithm_1.getX();
          GraphicsAlgorithm _graphicsAlgorithm_2 = pe.getGraphicsAlgorithm();
          int _y = _graphicsAlgorithm_2.getY();
          int _width = visibleGa.getWidth();
          int _height = visibleGa.getHeight();
          _xblockexpression_1 = new IPositionProvider.PosAndSize(_x, _y, _width, _height);
        }
        _xifexpression = _xblockexpression_1;
      }
      _xblockexpression = _xifexpression;
    }
    return _xblockexpression;
  }
  
  @Override
  public List<IPositionProvider.Pos> getPoints(final Transition trans) {
    ArrayList<IPositionProvider.Pos> _xblockexpression = null;
    {
      PictogramElement _xifexpression = null;
      if ((trans instanceof RefinedTransition)) {
        Transition _target = ((RefinedTransition)trans).getTarget();
        Collection<PictogramElement> _pictograms = this.baseDiagram.getPictograms(_target);
        _xifexpression = IterableExtensions.<PictogramElement>head(_pictograms);
      } else {
        Collection<PictogramElement> _pictograms_1 = this.baseDiagram.getPictograms(trans);
        _xifexpression = IterableExtensions.<PictogramElement>head(_pictograms_1);
      }
      final PictogramElement pe = _xifexpression;
      ArrayList<IPositionProvider.Pos> _newArrayList = CollectionLiterals.<IPositionProvider.Pos>newArrayList();
      final Procedure1<ArrayList<IPositionProvider.Pos>> _function = (ArrayList<IPositionProvider.Pos> pointList) -> {
        if ((pe instanceof Connection)) {
          EObject _eContainer = trans.eContainer();
          final IPositionProvider.PosAndSize graphPosAndSize = this.getGraphPosAndSize(((StateGraph) _eContainer));
          EList<ConnectionDecorator> _connectionDecorators = ((Connection)pe).getConnectionDecorators();
          final Function1<ConnectionDecorator, GraphicsAlgorithm> _function_1 = (ConnectionDecorator it) -> {
            return it.getGraphicsAlgorithm();
          };
          List<GraphicsAlgorithm> _map = ListExtensions.<ConnectionDecorator, GraphicsAlgorithm>map(_connectionDecorators, _function_1);
          Iterable<Text> _filter = Iterables.<Text>filter(_map, Text.class);
          final Text text = IterableExtensions.<Text>head(_filter);
          boolean _notEquals = (!Objects.equal(text, null));
          if (_notEquals) {
            int _x = text.getX();
            int _width = graphPosAndSize.getWidth();
            double _divide = (_x / ((double) _width));
            double _multiply = (_divide * this.sx);
            int _y = text.getY();
            int _height = graphPosAndSize.getHeight();
            double _divide_1 = (_y / ((double) _height));
            double _multiply_1 = (_divide_1 * this.sy);
            IPositionProvider.Pos _pos = new IPositionProvider.Pos(
              ((int) _multiply), 
              ((int) _multiply_1));
            pointList.add(_pos);
            if ((pe instanceof FreeFormConnection)) {
              EList<Point> _bendpoints = ((FreeFormConnection)pe).getBendpoints();
              final Function1<Point, IPositionProvider.Pos> _function_2 = (Point it) -> {
                int _x_1 = text.getX();
                int _width_1 = graphPosAndSize.getWidth();
                double _divide_2 = (_x_1 / ((double) _width_1));
                double _multiply_2 = (_divide_2 * this.sx);
                double _plus = (_multiply_2 + this.x);
                int _y_1 = text.getY();
                int _height_1 = graphPosAndSize.getHeight();
                double _divide_3 = (_y_1 / ((double) _height_1));
                double _multiply_3 = (_divide_3 * this.sy);
                double _plus_1 = (_multiply_3 + this.y);
                return new IPositionProvider.Pos(
                  ((int) _plus), 
                  ((int) _plus_1));
              };
              List<IPositionProvider.Pos> _map_1 = ListExtensions.<Point, IPositionProvider.Pos>map(_bendpoints, _function_2);
              Iterables.<IPositionProvider.Pos>addAll(pointList, _map_1);
            }
          }
        }
      };
      _xblockexpression = ObjectExtensions.<ArrayList<IPositionProvider.Pos>>operator_doubleArrow(_newArrayList, _function);
    }
    return _xblockexpression;
  }
  
  @Override
  public IPositionProvider.PosAndSize getPosition(final StateGraphNode node) {
    final Function1<EObject, IPositionProvider.PosAndSize> _function = (EObject it) -> {
      Collection<PictogramElement> _pictograms = this.baseDiagram.getPictograms(it);
      final Function1<PictogramElement, Boolean> _function_1 = (PictogramElement it_1) -> {
        EObject _parentBo = this.getParentBo(it_1);
        return Boolean.valueOf((_parentBo instanceof StateGraph));
      };
      PictogramElement _findFirst = IterableExtensions.<PictogramElement>findFirst(_pictograms, _function_1);
      IPositionProvider.PosAndSize _posAndSize = null;
      if (_findFirst!=null) {
        int _margin = this.getMargin(node);
        _posAndSize=this.toPosAndSize(_findFirst, _margin);
      }
      return _posAndSize;
    };
    final Function1<EObject, IPositionProvider.PosAndSize> getBasePos = _function;
    IPositionProvider.PosAndSize _xifexpression = null;
    if ((node instanceof RefinedState)) {
      IPositionProvider.PosAndSize _elvis = null;
      IPositionProvider.PosAndSize _apply = getBasePos.apply(node);
      if (_apply != null) {
        _elvis = _apply;
      } else {
        State _target = ((RefinedState)node).getTarget();
        IPositionProvider.PosAndSize _apply_1 = getBasePos.apply(_target);
        _elvis = _apply_1;
      }
      _xifexpression = _elvis;
    } else {
      _xifexpression = getBasePos.apply(node);
    }
    return _xifexpression;
  }
  
  @Override
  public StateGraph getInitialPoint(final StateGraph stateGraph) {
    EList<Transition> _transitions = stateGraph.getTransitions();
    Iterable<InitialTransition> _filter = Iterables.<InitialTransition>filter(_transitions, InitialTransition.class);
    InitialTransition _head = IterableExtensions.<InitialTransition>head(_filter);
    Collection<PictogramElement> _pictograms = this.baseDiagram.getPictograms(_head);
    final PictogramElement initTransPe = IterableExtensions.<PictogramElement>head(_pictograms);
    if ((initTransPe instanceof Connection)) {
      ILinkService _linkService = Graphiti.getLinkService();
      Anchor _start = ((Connection)initTransPe).getStart();
      final EObject sourceAnchorBo = _linkService.getBusinessObjectForLinkedPictogramElement(_start);
      if ((sourceAnchorBo instanceof StateGraph)) {
        return ((StateGraph)sourceAnchorBo);
      }
    }
    return null;
  }
  
  @Override
  public IPositionProvider.PosAndSize getPosition(final StateGraph graph) {
    Collection<PictogramElement> _pictograms = this.baseDiagram.getPictograms(graph);
    final Function1<PictogramElement, Boolean> _function = (PictogramElement it) -> {
      EObject _parentBo = this.getParentBo(it);
      return Boolean.valueOf((_parentBo instanceof StateGraph));
    };
    PictogramElement _findFirst = IterableExtensions.<PictogramElement>findFirst(_pictograms, _function);
    IPositionProvider.PosAndSize _posAndSize = null;
    if (_findFirst!=null) {
      int _margin = this.getMargin(graph);
      _posAndSize=this.toPosAndSize(_findFirst, _margin);
    }
    return _posAndSize;
  }
  
  @Override
  public <T extends StateGraphNode> List<IPositionProvider.PosAndSize> getPositions(final List<T> items) {
    final Function1<T, IPositionProvider.PosAndSize> _function = (T it) -> {
      Collection<PictogramElement> _pictograms = this.baseDiagram.getPictograms(it);
      PictogramElement _head = IterableExtensions.<PictogramElement>head(_pictograms);
      IPositionProvider.PosAndSize _posAndSize = null;
      if (_head!=null) {
        int _margin = this.getMargin(it);
        _posAndSize=this.toPosAndSize(_head, _margin);
      }
      return _posAndSize;
    };
    return ListExtensions.<T, IPositionProvider.PosAndSize>map(items, _function);
  }
  
  @Override
  public double[] getSubPosition(final StateGraphNode subNode) {
    Collection<PictogramElement> _pictograms = this.baseDiagram.getPictograms(subNode);
    final Function1<PictogramElement, Boolean> _function = (PictogramElement it) -> {
      EObject _parentBo = this.getParentBo(it);
      return Boolean.valueOf((_parentBo instanceof State));
    };
    final PictogramElement pe = IterableExtensions.<PictogramElement>findFirst(_pictograms, _function);
    boolean _notEquals = (!Objects.equal(pe, null));
    if (_notEquals) {
      EObject _eContainer = pe.eContainer();
      final PictogramElement parentPe = ((PictogramElement) _eContainer);
      GraphicsAlgorithm _graphicsAlgorithm = pe.getGraphicsAlgorithm();
      int _x = _graphicsAlgorithm.getX();
      GraphicsAlgorithm _graphicsAlgorithm_1 = parentPe.getGraphicsAlgorithm();
      int _width = _graphicsAlgorithm_1.getWidth();
      int _minus = (_width - (2 * StateSupport.MARGIN));
      final double relX = (((double) _x) / _minus);
      GraphicsAlgorithm _graphicsAlgorithm_2 = pe.getGraphicsAlgorithm();
      int _y = _graphicsAlgorithm_2.getY();
      GraphicsAlgorithm _graphicsAlgorithm_3 = parentPe.getGraphicsAlgorithm();
      int _height = _graphicsAlgorithm_3.getHeight();
      int _minus_1 = (_height - (2 * StateSupport.MARGIN));
      final double relY = (((double) _y) / _minus_1);
      return new double[] { relX, relY };
    }
    return null;
  }
  
  private int x;
  
  private int y;
  
  @Override
  public void setPosition(final int x, final int y) {
    this.x = x;
    this.y = y;
  }
  
  private double sx;
  
  private double sy;
  
  @Override
  public void setScale(final double sx, final double sy) {
    this.sx = sx;
    this.sy = sy;
  }
  
  private EObject getParentBo(final PictogramElement pe) {
    ILinkService _linkService = Graphiti.getLinkService();
    EObject _eContainer = pe.eContainer();
    return _linkService.getBusinessObjectForLinkedPictogramElement(((PictogramElement) _eContainer));
  }
  
  public BaseDiagramPositionProvider(final BaseDiagramProvider baseDiagram) {
    super();
    this.baseDiagram = baseDiagram;
  }
}

Back to the top