Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 33a89173f25cdcb709fc1b169796016a0ce3e6c8 (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/**
 * Copyright (c) 2015 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.actioneditor.sourceviewer;

import com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.google.inject.Inject;
import java.util.List;
import org.eclipse.core.runtime.Assert;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.etrice.core.room.Attribute;
import org.eclipse.etrice.core.room.DataType;
import org.eclipse.etrice.core.room.InterfaceItem;
import org.eclipse.etrice.core.room.Message;
import org.eclipse.etrice.core.room.Operation;
import org.eclipse.etrice.core.room.Port;
import org.eclipse.etrice.core.room.ProtocolClass;
import org.eclipse.etrice.core.room.RefableType;
import org.eclipse.etrice.core.room.SPP;
import org.eclipse.etrice.core.room.VarDecl;
import org.eclipse.etrice.core.room.util.RoomHelpers;
import org.eclipse.etrice.ui.behavior.actioneditor.Activator;
import org.eclipse.etrice.ui.behavior.actioneditor.sourceviewer.ActionCodeColorManager;
import org.eclipse.etrice.ui.behavior.detailcode.RuntimeDetailExpressionProvider;
import org.eclipse.etrice.ui.behavior.fsm.detailcode.IDetailExpressionProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.xtend.lib.annotations.Delegate;
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor;
import org.eclipse.xtext.xbase.lib.Conversions;
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.Pair;

@FinalFieldsConstructor
@SuppressWarnings("all")
public class DetailExpressionUIProvider implements IDetailExpressionProvider {
  private final static String IMAGE_RT_METHOD = "icons/rt_method.png";
  
  @Delegate
  private final IDetailExpressionProvider delegate;
  
  @Inject
  protected ILabelProvider labelProvider;
  
  @Inject
  protected RoomHelpers roomHelpers;
  
  /**
   * Return postfix string and its selection (relative start, length)
   */
  public Pair<String, Point> getPostfixReplacement(final IDetailExpressionProvider.ExpressionFeature feature) {
    Pair<String, Point> _xblockexpression = null;
    {
      this.assertNotNull(feature);
      String[] _switchResult = null;
      IDetailExpressionProvider.ExpressionPostfix _postfix = feature.getPostfix();
      if (_postfix != null) {
        switch (_postfix) {
          case PARENTHESES:
            _switchResult = new String[] { "(", ")" };
            break;
          case BRACKETS:
            _switchResult = new String[] { "[", "]" };
            break;
          default:
            return Pair.<String, Point>of("", null);
        }
      } else {
        return Pair.<String, Point>of("", null);
      }
      String[] brackets = _switchResult;
      String _switchResult_1 = null;
      Object _data = feature.getData();
      final Object data = _data;
      boolean _matched = false;
      if (data instanceof Operation) {
        _matched=true;
        EList<VarDecl> _arguments = ((Operation)data).getArguments();
        final Function1<VarDecl, String> _function = (VarDecl it) -> {
          return it.getName();
        };
        List<String> _map = ListExtensions.<VarDecl, String>map(_arguments, _function);
        _switchResult_1 = IterableExtensions.join(_map, ", ");
      }
      if (!_matched) {
        if (data instanceof Message) {
          VarDecl _data_1 = ((Message)data).getData();
          boolean _notEquals = (!Objects.equal(_data_1, null));
          if (_notEquals) {
            _matched=true;
            VarDecl _data_2 = ((Message)data).getData();
            _switchResult_1 = _data_2.getName();
          }
        }
      }
      if (!_matched) {
        if (data instanceof Attribute) {
          _matched=true;
        }
        if (!_matched) {
          if (data instanceof InterfaceItem) {
            _matched=true;
          }
        }
        if (_matched) {
          _switchResult_1 = "0";
        }
      }
      if (!_matched) {
        _switchResult_1 = "";
      }
      String replacement = _switchResult_1;
      Point _xifexpression = null;
      boolean _isEmpty = replacement.isEmpty();
      boolean _not = (!_isEmpty);
      if (_not) {
        int _length = replacement.length();
        _xifexpression = new Point(1, _length);
      }
      final Point selection = _xifexpression;
      final String[] _converted_brackets = (String[])brackets;
      String _head = IterableExtensions.<String>head(((Iterable<String>)Conversions.doWrapArray(_converted_brackets)));
      String _plus = (_head + replacement);
      final String[] _converted_brackets_1 = (String[])brackets;
      String _last = IterableExtensions.<String>last(((Iterable<String>)Conversions.doWrapArray(_converted_brackets_1)));
      String _plus_1 = (_plus + _last);
      _xblockexpression = Pair.<String, Point>of(_plus_1, selection);
    }
    return _xblockexpression;
  }
  
  /**
   * Return completion string and its selection (relative start, length)
   */
  public Pair<String, Point> getCompletion(final IDetailExpressionProvider.ExpressionFeature feature) {
    this.assertNotNull(feature);
    Pair<String, Point> postfix = this.getPostfixReplacement(feature);
    Point point = postfix.getValue();
    boolean _notEquals = (!Objects.equal(point, null));
    if (_notEquals) {
      int _x = point.x;
      String _id = feature.getId();
      int _length = _id.length();
      point.x = (_x + _length);
    }
    String _id_1 = feature.getId();
    String _key = postfix.getKey();
    String _plus = (_id_1 + _key);
    return Pair.<String, Point>of(_plus, point);
  }
  
  public RGB getColor(final IDetailExpressionProvider.ExpressionFeature feature) {
    this.assertNotNull(feature);
    RGB _switchResult = null;
    Object _data = feature.getData();
    final Object data = _data;
    boolean _matched = false;
    if (data instanceof InterfaceItem) {
      _matched=true;
      _switchResult = ActionCodeColorManager.INTERFACE_ITEM;
    }
    if (!_matched) {
      if (data instanceof Attribute) {
        _matched=true;
        _switchResult = ActionCodeColorManager.ATTRIBUTE;
      }
    }
    if (!_matched) {
      if (data instanceof Operation) {
        _matched=true;
        _switchResult = ActionCodeColorManager.OPERATION;
      }
    }
    if (!_matched) {
      if (data instanceof EObject) {
        _matched=true;
        _switchResult = ActionCodeColorManager.SPECIAL_FEATURE;
      }
    }
    if (!_matched) {
      if (data instanceof RuntimeDetailExpressionProvider.RuntimeMethodExpressionData) {
        _matched=true;
        _switchResult = ActionCodeColorManager.OPERATION;
      }
    }
    return _switchResult;
  }
  
  /**
   * Text format:
   * {@code completionInfo : typedInfo - classInfo}
   */
  public String getDisplayString(final IDetailExpressionProvider.ExpressionFeature feature) {
    this.assertNotNull(feature);
    final Object data = feature.getData();
    String _id = feature.getId();
    Pair<String, Point> _postfixReplacement = this.getPostfixReplacement(feature);
    String _key = _postfixReplacement.getKey();
    String completionInfo = (_id + _key);
    String typedInfo = "";
    String _xifexpression = null;
    if ((data instanceof EObject)) {
      EClass _eClass = ((EObject)data).eClass();
      _xifexpression = _eClass.getName();
    } else {
      _xifexpression = "";
    }
    String classInfo = _xifexpression;
    boolean _matched = false;
    if (data instanceof Attribute) {
      _matched=true;
      RefableType _type = ((Attribute)data).getType();
      DataType _type_1 = _type.getType();
      String _name = _type_1.getName();
      typedInfo = _name;
    }
    if (!_matched) {
      if (data instanceof InterfaceItem) {
        _matched=true;
        ProtocolClass _protocol = this.roomHelpers.getProtocol(((InterfaceItem)data));
        String _name = _protocol.getName();
        typedInfo = _name;
      }
    }
    if (!_matched) {
      if (data instanceof RuntimeDetailExpressionProvider.RuntimeMethodExpressionData) {
        String _id_1 = feature.getId();
        boolean _equals = Objects.equal(_id_1, RuntimeDetailExpressionProvider.RT_METHOD_GET_REPLICATION);
        if (_equals) {
          _matched=true;
          typedInfo = "int";
        }
      }
    }
    if (!_matched) {
      if (data instanceof VarDecl) {
        _matched=true;
        RefableType _refType = ((VarDecl)data).getRefType();
        DataType _type = _refType.getType();
        String _name = _type.getName();
        typedInfo = _name;
        classInfo = "";
      }
    }
    if (!_matched) {
      {
        final String label = this.labelProvider.getText(data);
        String _commonPrefix = Strings.commonPrefix(label, completionInfo);
        boolean _isEmpty = _commonPrefix.isEmpty();
        boolean _not = (!_isEmpty);
        if (_not) {
          completionInfo = label;
        }
      }
    }
    IDetailExpressionProvider.ExpressionPostfix _postfix = feature.getPostfix();
    boolean _equals = Objects.equal(_postfix, IDetailExpressionProvider.ExpressionPostfix.NONE);
    if (_equals) {
      boolean _matched_1 = false;
      if (data instanceof SPP) {
        boolean _isEventDriven = ((SPP)data).isEventDriven();
        if (_isEventDriven) {
          _matched_1=true;
        }
      }
      if (!_matched_1) {
        if (data instanceof Port) {
          if ((((Port)data).isReplicated() && ((Port)data).isEventDriven())) {
            _matched_1=true;
          }
        }
      }
      if (_matched_1) {
        completionInfo = (completionInfo + " (broadcast)");
      }
    }
    boolean _isEmpty = typedInfo.isEmpty();
    boolean _not = (!_isEmpty);
    if (_not) {
      typedInfo = (" : " + typedInfo);
    }
    boolean _isEmpty_1 = classInfo.isEmpty();
    boolean _not_1 = (!_isEmpty_1);
    if (_not_1) {
      classInfo = (" - " + classInfo);
    }
    return ((completionInfo + typedInfo) + classInfo);
  }
  
  public Image getImage(final IDetailExpressionProvider.ExpressionFeature feature) {
    Image _xblockexpression = null;
    {
      this.assertNotNull(feature);
      Image _switchResult = null;
      Object _data = feature.getData();
      boolean _matched = false;
      if (_data instanceof EObject) {
        _matched=true;
        Object _data_1 = feature.getData();
        _switchResult = this.labelProvider.getImage(_data_1);
      }
      if (!_matched) {
        if (_data instanceof RuntimeDetailExpressionProvider.RuntimeMethodExpressionData) {
          _matched=true;
          _switchResult = Activator.getImage(DetailExpressionUIProvider.IMAGE_RT_METHOD);
        }
      }
      _xblockexpression = _switchResult;
    }
    return _xblockexpression;
  }
  
  /**
   * Filter by prefix
   */
  public Iterable<IDetailExpressionProvider.ExpressionFeature> getContextFeaturesWithPrefix(final IDetailExpressionProvider.ExpressionFeature ctx, final String prefix) {
    List<IDetailExpressionProvider.ExpressionFeature> _contextFeatures = this.delegate.getContextFeatures(ctx);
    final Function1<IDetailExpressionProvider.ExpressionFeature, Boolean> _function = (IDetailExpressionProvider.ExpressionFeature it) -> {
      String _id = it.getId();
      return Boolean.valueOf(_id.startsWith(prefix));
    };
    return IterableExtensions.<IDetailExpressionProvider.ExpressionFeature>filter(_contextFeatures, _function);
  }
  
  /**
   * Filter by prefix
   */
  public Iterable<IDetailExpressionProvider.ExpressionFeature> getInitialFeaturesWithPrefix(final String prefix) {
    List<IDetailExpressionProvider.ExpressionFeature> _initialFeatures = this.delegate.getInitialFeatures();
    final Function1<IDetailExpressionProvider.ExpressionFeature, Boolean> _function = (IDetailExpressionProvider.ExpressionFeature it) -> {
      String _id = it.getId();
      return Boolean.valueOf(_id.startsWith(prefix));
    };
    return IterableExtensions.<IDetailExpressionProvider.ExpressionFeature>filter(_initialFeatures, _function);
  }
  
  protected void assertNotNull(final IDetailExpressionProvider.ExpressionFeature feature) {
    String _id = feature.getId();
    boolean _isNullOrEmpty = Strings.isNullOrEmpty(_id);
    boolean _not = (!_isNullOrEmpty);
    Assert.isTrue(_not);
    IDetailExpressionProvider.ExpressionPostfix _postfix = feature.getPostfix();
    Assert.isNotNull(_postfix);
    Object _data = feature.getData();
    Assert.isNotNull(_data);
  }
  
  public DetailExpressionUIProvider(final IDetailExpressionProvider delegate) {
    super();
    this.delegate = delegate;
  }
  
  public List<IDetailExpressionProvider.ExpressionFeature> getInitialFeatures() {
    return this.delegate.getInitialFeatures();
  }
  
  public List<IDetailExpressionProvider.ExpressionFeature> getContextFeatures(final IDetailExpressionProvider.ExpressionFeature ctx) {
    return this.delegate.getContextFeatures(ctx);
  }
}

Back to the top