Skip to main content
summaryrefslogtreecommitdiffstats
blob: 115d58e0f5a4e7d378ae28abcac047ef44c01e43 (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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * 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:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.ui.branch.graph.parts;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.ConnectionLayer;
import org.eclipse.draw2d.Cursors;
import org.eclipse.draw2d.FanRouter;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.MouseMotionListener;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.ShortestPathConnectionRouter;
import org.eclipse.draw2d.Viewport;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.jdk.core.type.HashCollection;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.ui.branch.graph.Activator;
import org.eclipse.osee.framework.ui.branch.graph.figure.BranchFigure;
import org.eclipse.osee.framework.ui.branch.graph.figure.FigureFactory;
import org.eclipse.osee.framework.ui.branch.graph.figure.TxFigure;
import org.eclipse.osee.framework.ui.branch.graph.model.BranchModel;
import org.eclipse.osee.framework.ui.branch.graph.model.GraphCache;
import org.eclipse.osee.framework.ui.branch.graph.model.TxModel;
import org.eclipse.osee.framework.ui.branch.graph.utility.GraphOptions;
import org.eclipse.osee.framework.ui.branch.graph.utility.GraphOptions.ConnectionFilter;
import org.eclipse.osee.framework.ui.branch.graph.utility.GraphOptions.TxFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;

public class GraphEditPart extends AbstractGraphicalEditPart {

   private final GraphicalViewer viewer;
   private final Map<Branch, BranchFigure> branchFigureMap;
   private final Map<Long, TxModel> txNumberToTxModelMap;
   private final Map<Long, TxFigure> txNumberToTxFigureMap;
   private final HashCollection<ConnectionType, Connection> connectionMap;

   private final HashCollection<Integer, BranchModel> branchesByLevel;

   private final IPreferenceStore preferenceStore;

   private enum ConnectionType {
      BRANCH_INTERNAL,
      PARENT_CHILD,
      MERGE;
   }

   public GraphEditPart(GraphicalViewer viewer) {
      super();
      this.viewer = viewer;
      this.branchFigureMap = new HashMap<Branch, BranchFigure>();
      this.txNumberToTxModelMap = new HashMap<Long, TxModel>();
      this.txNumberToTxFigureMap = new HashMap<Long, TxFigure>();
      this.branchesByLevel = new HashCollection<Integer, BranchModel>();
      this.connectionMap = new HashCollection<ConnectionType, Connection>();
      this.preferenceStore = Activator.getInstance().getPreferenceStore();
   }

   /*
    * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
    */
   @Override
   protected void createEditPolicies() {
      //      installEditPolicy(EditPolicy.LAYOUT_ROLE, new GraphXYLayoutEditPolicy());
   }

   /*
    * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
    */
   @Override
   protected IFigure createFigure() {
      IFigure figure = new Figure();
      figure.setBackgroundColor(ColorConstants.white);
      figure.setOpaque(true);

      XYLayout layout = new XYLayout();
      figure.setLayoutManager(layout);

      ConnectionLayer connLayer = (ConnectionLayer) getLayer(LayerConstants.CONNECTION_LAYER);
      FanRouter router = new FanRouter();
      router.setSeparation(20);
      router.setNextRouter(new ShortestPathConnectionRouter(figure));
      connLayer.setConnectionRouter(router);
      connLayer.setAntialias(SWT.ON);
      return figure;
   }

   /*
    * @see org.eclipse.gef.editparts.AbstractEditPart#getModelChildren()
    */
   @Override
   protected List<BranchModel> getModelChildren() {
      GraphCache graphCache = (GraphCache) getModel();

      List<BranchModel> nodes = new ArrayList<BranchModel>();
      nodes.add(graphCache.getRootModel());
      nodes.addAll(graphCache.getRootModel().getAllChildrenBelow());
      Collections.sort(nodes, new Comparator<BranchModel>() {
         @Override
         public int compare(BranchModel o1, BranchModel o2) {
            int level1 = o1.getDepth();
            int level2 = o2.getDepth();
            int result = new Integer(level1).compareTo(new Integer(level2));
            //            if (result == 0) {
            //               BranchModel parent1 = o1.getParentBranch();
            //               BranchModel parent2 = o2.getParentBranch();
            //               if (parent1 == null && parent2 == null) {
            //                  result = 0;
            //               } else if (parent1 != null && parent2 == null) {
            //                  result = 1;
            //               } else if (parent2 != null && parent1 == null) {
            //                  result = -1;
            //               } else {
            //                  result = parent1.getBranch().compareTo(parent2.getBranch());
            //               }
            //            }
            //            if (result == 0) {
            //               TxModel m1 = o1.getFirstTx();
            //               TxModel m2 = o2.getFirstTx();
            //               try {
            //                  result = m1.compareTo(m2);
            //               } catch (Exception ex) {
            //                  OseeLog.log(RevisionGraphActivator.class, Level.SEVERE, String.format(
            //                        "One of the branch models did not have a starting tx. 1:[%s] 2:[%s]", o1, o2));
            //               }
            //            }
            return result;
         }
      });

      List<BranchModel> toDraw = new ArrayList<BranchModel>();
      for (int index = 0; index < nodes.size(); index++) {
         BranchModel model = nodes.get(index);
         model.setIndex(index);
         toDraw.add(model);
         branchFigureMap.put(model.getBranch(), FigureFactory.createBranchLabelFigure(model));
         branchesByLevel.put(model.getDepth(), model);
      }

      for (TxModel model : graphCache.getTxModels()) {
         BranchModel parent = model.getParentBranchModel();
         if (parent != null && parent.areTxsVisible() && parent.isVisible()) {
            Long txNumber = model.getRevision();
            txNumberToTxModelMap.put(txNumber, model);
            txNumberToTxFigureMap.put(txNumber, FigureFactory.createTxFigure(model));
         } else {
            OseeLog.log(Activator.class, Level.SEVERE,
               String.format("Orphan TxModel: [%s]", model.toString()));
         }
      }

      createTxConnections(toDraw);
      createMergedConnections(toDraw);

      setConnectionVisibility();
      return toDraw;
   }

   public int getMaxTxForGraphLevel(int graphLevel) {
      int max = Integer.MIN_VALUE;
      Collection<BranchModel> models = branchesByLevel.getValues(graphLevel);
      if (models != null) {
         for (BranchModel model : models) {
            if (model.areTxsVisible()) {
               max = Math.max(model.getTxs().size(), max);
            } else {
               max = Math.max(3, max);
            }
         }
      }
      return max;
   }

   public int getNumberOfBranchesAtGraphLevel(int graphLevel) {
      Collection<?> collection = branchesByLevel.getValues(graphLevel);
      return collection != null ? collection.size() : 0;
   }

   public int getMaxNumberOfBranchesAtAnyLevel() {
      int max = Integer.MIN_VALUE;
      for (Integer level : branchesByLevel.keySet()) {
         max = Math.max(max, getNumberOfBranchesAtGraphLevel(level));
      }
      return max;
   }

   public BranchFigure getFigure(Branch branch) {
      return branchFigureMap.get(branch);
   }

   public TxFigure getTxFigure(TxModel txModel) {
      return txNumberToTxFigureMap.get(txModel.getRevision());
   }

   private void createMergedConnections(Collection<BranchModel> toReturn) {
      for (BranchModel branchNode : toReturn) {
         for (TxModel txModel : branchNode.getTxs()) {
            if (txModel.getMergedTx() != null) {
               TxFigure txFigure = txNumberToTxFigureMap.get(txModel.getRevision());
               for (TxModel merged : txModel.getMergedTx()) {
                  TxFigure mergedView = txNumberToTxFigureMap.get(merged.getRevision());
                  if (mergedView != null) {
                     String message = getConnectionLabel(merged, txModel);
                     connect(ConnectionType.MERGE, getFigure(), mergedView, txFigure, message, true, ColorConstants.red);
                  }
               }
            }
         }
      }
   }

   private String getConnectionLabel(TxModel source, TxModel target) {
      return String.format("%s:%s - %s:%s", source.getParentBranchModel().getBranch().getShortName(),
         source.getRevision(), target.getParentBranchModel().getBranch().getShortName(), target.getRevision());
   }

   private void createTxConnections(Collection<BranchModel> toReturn) {
      for (BranchModel branchModel : toReturn) {
         if (branchModel.areTxsVisible()) {
            for (TxModel txModel : branchModel.getTxs()) {
               TxFigure txFigure = getTxFigure(txModel);
               boolean connectToBranchLabel = branchModel.getFirstTx() == txModel;
               String msg = null;
               if (txModel.getParentTx() != null) {
                  TxModel parentTxModel = txModel.getParentTx();
                  TxFigure parent = getTxFigure(parentTxModel);
                  if (parent != null) {
                     msg = getConnectionLabel(parentTxModel, txModel);
                     connect(ConnectionType.BRANCH_INTERNAL, getFigure(), parent, txFigure, msg, false,
                        ColorConstants.black);
                  }
               } else if (txModel.getSourceTx() != null) {
                  TxModel sourceTx = txModel.getSourceTx();
                  TxFigure source = getTxFigure(sourceTx);
                  if (source != null) {
                     msg = getConnectionLabel(sourceTx, txModel);
                     connect(ConnectionType.PARENT_CHILD, getFigure(), source, txFigure, msg, true, ColorConstants.blue);
                  }
               } else {
                  connectToBranchLabel = true;
               }

               if (connectToBranchLabel) {
                  BranchFigure branchFigure = branchFigureMap.get(branchModel.getBranch());
                  msg = getConnectionLabel(branchModel.getFirstTx(), txModel);
                  connect(ConnectionType.BRANCH_INTERNAL, getFigure(), branchFigure, txFigure, msg, false,
                     ColorConstants.black);
               }
            }
         } else {
            TxModel txModel = branchModel.getFirstTx();
            if (txModel == null) {
               OseeLog.log(Activator.class, Level.SEVERE,
                  String.format("Branch did not have a starting tx [%s]", branchModel));
            } else {
               if (txModel.getSourceTx() != null) {
                  TxModel sourceTx = txModel.getSourceTx();
                  String msg = getConnectionLabel(sourceTx, txModel);

                  BranchFigure branchFigure = branchFigureMap.get(branchModel.getBranch());
                  if (sourceTx.getParentBranchModel().areTxsVisible()) {
                     TxFigure source = getTxFigure(sourceTx);
                     if (source != null) {
                        connect(ConnectionType.PARENT_CHILD, getFigure(), source, branchFigure, msg, true,
                           ColorConstants.lightBlue);
                     }
                  } else {
                     BranchFigure source = branchFigureMap.get(sourceTx.getParentBranchModel().getBranch());
                     connect(ConnectionType.PARENT_CHILD, getFigure(), source, branchFigure, msg, true,
                        ColorConstants.lightBlue);
                  }
               }
            }
         }
      }
   }

   private Set<ConnectionType> getFilteredConnectionTypes() {
      int connectLevel = preferenceStore.getInt(GraphOptions.FILTER_CONNECTIONS_PREFERENCE);
      ConnectionFilter[] filters = ConnectionFilter.values();
      ConnectionFilter filter = ConnectionFilter.NO_FILTER;
      if (connectLevel < filters.length && connectLevel >= 0) {
         filter = filters[connectLevel];
      }
      Set<ConnectionType> filtered = new HashSet<ConnectionType>();
      switch (filter) {
         case FILTER_CHILD_BRANCH_CONNECTIONS:
            filtered.add(ConnectionType.PARENT_CHILD);
            break;
         case FILTER_MERGE_CONNECTIONS:
            filtered.add(ConnectionType.MERGE);
            break;
         case FILTER_ALL_CONNECTIONS:
            for (ConnectionType connectionType : ConnectionType.values()) {
               if (connectionType != ConnectionType.BRANCH_INTERNAL) {
                  filtered.add(connectionType);
               }
            }
            break;
         case NO_FILTER:
         default:
            break;
      }
      return filtered;
   }

   public void setConnectionVisibility() {
      Set<ConnectionType> filteredTypes = getFilteredConnectionTypes();
      for (ConnectionType connectType : connectionMap.keySet()) {
         Collection<Connection> connections = connectionMap.getValues(connectType);
         if (connections != null) {
            for (Connection connection : connections) {
               boolean isVisible = false;
               if (!filteredTypes.contains(connectType)) {
                  isVisible = true;
               } else {
                  IFigure source = connection.getSourceAnchor().getOwner();
                  IFigure target = connection.getTargetAnchor().getOwner();
                  if (source instanceof TxFigure && ((TxFigure) source).isSelected()) {
                     isVisible = true;
                  } else if (target instanceof TxFigure && ((TxFigure) target).isSelected()) {
                     isVisible = true;
                  } else {
                     isVisible = false;
                  }
               }
               connection.setVisible(isVisible);
            }
         }
      }
   }

   public void setTxVisibility() {
      int filterSetting = preferenceStore.getInt(GraphOptions.TRANSACTION_FILTER);
      boolean isVisible = TxFilter.NO_FILTER.ordinal() == filterSetting;
      GraphCache graph = (GraphCache) getModel();
      for (BranchModel model : graph.getBranchModels()) {
         model.setTxsVisible(isVisible);
      }
      viewer.setContents(graph);
   }

   private void connect(ConnectionType connectType, IFigure contents, IFigure source, IFigure target, String toolTip, boolean hasEndPoint, Color color) {
      PolylineConnection connection =
         FigureFactory.createConnection(getFigure(), source, target, toolTip, hasEndPoint, color);
      ConnectionMouseListener listener = new ConnectionMouseListener(connection);
      connection.addMouseMotionListener(listener);
      connection.addMouseListener(listener);
      connection.setCursor(Cursors.HAND);
      connectionMap.put(connectType, connection);
   }

   private void scrollTo(Rectangle fbounds) {
      scrollTo(fbounds.x + fbounds.width / 2, fbounds.y + fbounds.height / 2);
   }

   private void scrollTo(int ax, int ay) {
      Viewport viewport = ((FigureCanvas) viewer.getControl()).getViewport();
      Rectangle vbounds = viewport.getBounds();
      Point p = new Point(ax, ay);
      int x = p.x - vbounds.width / 2;
      int y = p.y - vbounds.height / 2;
      viewport.setHorizontalLocation(x);
      viewport.setVerticalLocation(y);
   }

   private void scrollTo(IFigure target) {
      scrollTo(target.getBounds());
   }

   private final class ConnectionMouseListener implements MouseMotionListener, MouseListener {

      private final PolylineConnection connection;

      public ConnectionMouseListener(PolylineConnection connection) {
         this.connection = connection;
      }

      @Override
      public void mouseDragged(MouseEvent event) {
      }

      @Override
      public void mouseEntered(MouseEvent event) {
      }

      @Override
      public void mouseExited(MouseEvent event) {
         connection.setLineWidth(1);
      }

      @Override
      public void mouseHover(MouseEvent event) {
         connection.setLineWidth(3);
      }

      @Override
      public void mouseMoved(MouseEvent event) {
      }

      @Override
      public void mouseDoubleClicked(MouseEvent event) {
      }

      @Override
      public void mousePressed(MouseEvent event) {
         TxFigure txFigure = (TxFigure) connection.getTargetAnchor().getOwner();
         scrollTo(txFigure);
         Map<?, ?> map = viewer.getEditPartRegistry();

         TxModel txModel = txNumberToTxModelMap.get(txFigure.getTxNumber());
         if (txModel != null) {
            EditPart editPart = (EditPart) map.get(txModel);
            if (editPart != null) {
               viewer.select(editPart);
            }
         }
      }

      @Override
      public void mouseReleased(MouseEvent event) {
      }
   }
}

Back to the top