Skip to main content
summaryrefslogtreecommitdiffstats
blob: 04bf3535fe034edac9639197e0e58d5ee2a10164 (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
/*******************************************************************************
 * 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.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.osee.framework.core.model.Branch;

/**
 * @author Roberto E. Escobar
 */
public class BranchModel extends Node implements Serializable {

   private static final long serialVersionUID = 1017422142119868019L;
   private BranchModel parent;
   private final LinkedList<TxModel> txs;
   private final List<BranchModel> children;

   private transient Branch branch;
   private transient boolean isLoaded;
   private transient int depth;
   private transient boolean txsVisible;

   public BranchModel(Branch branch) {
      super();
      this.branch = branch;
      this.children = new ArrayList<BranchModel>();
      this.txs = new LinkedList<TxModel>();
      this.parent = null;
      this.isLoaded = false;
      this.depth = -1;
      this.txsVisible = true;
   }

   protected void reset() {
      this.children.clear();
      this.parent = null;
   }

   protected void resetTxs() {
      this.txs.clear();
   }

   protected boolean isLoaded() {
      return isLoaded;
   }

   protected void setIsLoaded(boolean loaded) {
      this.isLoaded = loaded;
   }

   public Branch getBranch() {
      return branch;
   }

   public boolean isDefaultBranch() {
      //      Branch defaultBranch = BranchManager.getDefaultBranch();
      //      if (defaultBranch != null && branch != null) {
      //         return defaultBranch.equals(branch);
      //      }
      return false;
   }

   public BranchModel getParentBranch() {
      return parent;
   }

   protected void setParentBranch(BranchModel parent) {
      this.parent = parent;
   }

   public List<BranchModel> getChildren() {
      return children;
   }

   public List<BranchModel> getAllChildrenBelow() {
      List<BranchModel> children = new ArrayList<BranchModel>(getChildren());
      for (BranchModel child : getChildren()) {
         children.addAll(child.getAllChildrenBelow());
      }
      return children;
   }

   protected void addChildBranchModel(BranchModel branchModel) {
      if (!children.contains(branchModel)) {
         branchModel.setParentBranch(this);
         int toSearch = branchModel.getAllChildrenBelow().size();
         int insertAt = -1;
         for (BranchModel child : children) {
            if (child.getAllChildrenBelow().size() > toSearch) {
               break;
            }
            insertAt++;
         }
         int size = children.size();
         if (size != 0 && insertAt >= 0 && insertAt < size) {
            children.add(insertAt, branchModel);
         } else {
            children.add(branchModel);
         }
      }
   }

   protected void addTx(TxModel txModel) {
      if (!txs.contains(txModel)) {
         txModel.setBranchModel(this);

         Long toSearch = txModel.getRevision();
         int insertAt = -1;
         for (int index = 0; index < txs.size(); index++) {
            TxModel toCheck = txs.get(index);
            if (toCheck.getRevision() > toSearch) {
               insertAt = index;
               break;
            }
         }
         if (insertAt > -1) {
            TxModel greater = txs.get(insertAt);
            TxModel parent = greater.getParentTx();
            if (parent != null) {
               txModel.setParentTx(parent);
            } else {
               int toGet = insertAt - 1;
               if (toGet >= 0) {
                  txModel.setParentTx(txs.get(toGet));
               }
            }
            greater.setParentTx(txModel);
            txs.add(insertAt, txModel);
         } else {
            TxModel last = getLastTx();
            if (last != null) {
               txModel.setParentTx(last);
            }
            txs.add(txModel);
         }
      }
   }

   public List<TxModel> getTxs() {
      return txs;
   }

   public TxModel getFirstTx() {
      return txs.isEmpty() ? null : txs.get(0);
   }

   public TxModel getLastTx() {
      return txs.isEmpty() ? null : txs.get(txs.size() - 1);
   }

   @Override
   public boolean equals(Object obj) {
      if (obj instanceof BranchModel) {
         BranchModel other = (BranchModel) obj;
         return other.getBranch().equals(getBranch());
      }
      return false;
   }

   @Override
   public int hashCode() {
      return branch.hashCode();
   }

   @Override
   public String toString() {
      return String.format("Branch:[%s] Type:[%s] Children:[%s] TxNodes:[%s]", branch.getName(),
         branch.getBranchType().name(), children.size(), txs.size());
   }

   public boolean areTxsVisible() {
      return txsVisible;
   }

   public void setTxsVisible(boolean txsVisible) {
      if (areTxsVisible() != txsVisible) {
         this.txsVisible = txsVisible;
         for (TxModel tx : txs) {
            tx.setVisible(txsVisible);
         }
      }
   }

   public void setDepth(int depth) {
      this.depth = depth;
   }

   public int getDepth() {
      return depth;
   }
}

Back to the top