Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8f3c7d886093971582074411e19eb28b58babf07 (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
/*******************************************************************************
 * 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.skynet.widgets.xBranch;

import static org.eclipse.osee.framework.core.enums.CoreBranches.COMMON;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.nebula.widgets.xviewer.XViewerCells;
import org.eclipse.nebula.widgets.xviewer.XViewerColumn;
import org.eclipse.nebula.widgets.xviewer.XViewerLabelProvider;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.enums.CoreBranches;
import org.eclipse.osee.framework.core.enums.SystemUser;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.TransactionRecord;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.UserManager;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.ui.skynet.ArtifactImageManager;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.swt.graphics.Image;

/**
 * @author Jeff C. Phillips
 */
public class XBranchLabelProvider extends XViewerLabelProvider {
   private final static DateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
   private final BranchXViewer branchXViewer;

   public XBranchLabelProvider(BranchXViewer branchXViewer) {
      super(branchXViewer);
      this.branchXViewer = branchXViewer;
   }

   @Override
   public String getColumnText(Object element, XViewerColumn cCol, int columnIndex) {
      String columnText = "";
      try {
         if (element instanceof Branch) {
            columnText = getBranchText((Branch) element, cCol, columnIndex);
         } else if (element instanceof TransactionRecord) {
            columnText = getTransactionText((TransactionRecord) element, cCol, columnIndex);
         } else if (element instanceof Collection<?>) {
            columnText = getAggrTransactionList((Collection<?>) element, columnIndex);
         }
      } catch (Exception ex) {
         return XViewerCells.getCellExceptionString(ex);
      }
      return columnText;
   }

   private String getAggrTransactionList(Collection<?> collection, int columnIndex) {
      Object headCursor = collection;
      Object tailCursor = collection;
      String columnText = "";

      while (headCursor instanceof List<?> && !((List<?>) headCursor).isEmpty()) {
         headCursor = ((List<?>) headCursor).get(0);
      }
      while (tailCursor instanceof List<?> && !((List<?>) tailCursor).isEmpty()) {
         List<?> list = (List<?>) tailCursor;
         tailCursor = list.get(list.size() - 1);
      }

      if (headCursor instanceof TransactionRecord && tailCursor instanceof TransactionRecord) {
         TransactionRecord headTransaction = (TransactionRecord) headCursor;
         TransactionRecord tailTransaction = (TransactionRecord) tailCursor;

         if (columnIndex == 0) {
            columnText = String.valueOf(headTransaction.getId() + "..." + tailTransaction.getId());
         } else if (columnIndex == 1) {
            columnText = DATE_FORMAT.format(headTransaction.getTimeStamp());
         }
      } else {
         columnText =
            "Unexpected aggregation of " + headCursor.getClass().getSimpleName() + " and " + tailCursor.getClass().getSimpleName();
      }
      return columnText;
   }

   private String getBranchText(Branch branch, XViewerColumn cCol, int columnIndex) {
      if (cCol.equals(BranchXViewerFactory.branchName)) {
         return branch.getName();
      } else if (cCol.equals(BranchXViewerFactory.archivedState)) {
         return branch.getArchiveState().toString();
      } else if (cCol.equals(BranchXViewerFactory.timeStamp)) {
         try {
            String date = "";
            if (branch.getBaseTransaction() != null) {
               date = DATE_FORMAT.format(branch.getBaseTransaction().getTimeStamp());
            }
            return date;
         } catch (OseeCoreException ex) {
            return XViewerCells.getCellExceptionString(ex);
         }
      } else if (cCol.equals(BranchXViewerFactory.author)) {
         String userName = "";
         try {
            if (branch.getBaseTransaction() != null) {
               userName = UserManager.getSafeUserNameById(branch.getBaseTransaction().getAuthor());
            }
            return userName;
         } catch (OseeCoreException ex) {
            return XViewerCells.getCellExceptionString(ex);
         }
      } else if (cCol.equals(BranchXViewerFactory.comment)) {
         try {
            String branchComment = "";
            if (branch.getBaseTransaction() != null) {
               branchComment = branch.getBaseTransaction().getComment();
            }
            return branchComment;
         } catch (OseeCoreException ex) {
            return XViewerCells.getCellExceptionString(ex);
         }
      } else if (cCol.equals(BranchXViewerFactory.associatedArtifact)) {
         try {
            return BranchManager.getAssociatedArtifact(branch).getName();
         } catch (OseeCoreException ex) {
            return XViewerCells.getCellExceptionString(ex);
         }
      } else if (cCol.equals(BranchXViewerFactory.branchState)) {
         return branch.getBranchState().name();
      } else if (cCol.equals(BranchXViewerFactory.branchType)) {
         return branch.getBranchType().name();
      } else if (cCol.equals(BranchXViewerFactory.branchUuid)) {
         return String.valueOf(branch.getUuid());
      } else if (cCol.equals(BranchXViewerFactory.branchUuid)) {
         return String.valueOf(branch.getUuid());
      } else if (cCol.equals(BranchXViewerFactory.parentBranch)) {
         try {
            return branch.equals(CoreBranches.SYSTEM_ROOT) ? "none" : branch.getParentBranch().getName();
         } catch (OseeCoreException ex) {
            return XViewerCells.getCellExceptionString(ex);
         }
      } else if (cCol.equals(BranchXViewerFactory.inheritAccessControl)) {
         return String.valueOf(branch.isInheritAccessControl());
      }
      return "";
   }

   protected String getTransactionText(TransactionRecord transaction, XViewerColumn cCol, int columnIndex) {
      String columnText = "";

      if (cCol.equals(BranchXViewerFactory.branchName) || cCol.equals(BranchXViewerFactory.transaction)) {
         columnText = String.valueOf(transaction.getId());
      }
      if (cCol.equals(BranchXViewerFactory.timeStamp)) {
         columnText = DATE_FORMAT.format(transaction.getTimeStamp());
      } else if (cCol.equals(BranchXViewerFactory.author)) {
         try {
            columnText = UserManager.getSafeUserNameById(transaction.getAuthor());
         } catch (Exception ex) {
            return XViewerCells.getCellExceptionString(ex);
         }
      } else if (cCol.equals(BranchXViewerFactory.comment)) {
         columnText = transaction.getComment();
      } else if (cCol.equals(BranchXViewerFactory.associatedArtifact)) {
         try {
            if (transaction.getCommit() == 0) {
               return "";
            }
            Artifact art = ArtifactQuery.getArtifactFromId(transaction.getCommit(), COMMON);
            if (art != null) {
               columnText = art.getName();
            }
         } catch (OseeCoreException ex) {
            return XViewerCells.getCellExceptionString(ex);
         }
      }
      return columnText;
   }

   @Override
   public boolean isLabelProperty(Object element, String property) {
      return false;
   }

   @Override
   public void addListener(ILabelProviderListener listener) {
      // do nothing
   }

   @Override
   public void removeListener(ILabelProviderListener listener) {
      // do nothing
   }

   public BranchXViewer getTreeViewer() {
      return branchXViewer;
   }

   @Override
   public Image getColumnImage(Object element, XViewerColumn xCol, int columnIndex) {
      if (xCol.equals(BranchXViewerFactory.associatedArtifact)) {
         if (element instanceof BranchId) {
            try {
               Artifact associatedArtifact = BranchManager.getAssociatedArtifact((BranchId) element);
               if (!associatedArtifact.equals(SystemUser.OseeSystem)) {
                  return ArtifactImageManager.getImage(associatedArtifact);
               }
            } catch (OseeCoreException ex) {
               OseeLog.log(Activator.class, Level.SEVERE, ex);
            }
         } else if (element instanceof TransactionRecord) {
            try {
               if (((TransactionRecord) element).getCommit() == 0) {
                  return null;
               }
               Artifact artifact = ArtifactQuery.getArtifactFromId(((TransactionRecord) element).getCommit(), COMMON);
               if (artifact != null) {
                  return ArtifactImageManager.getImage(artifact);
               }
            } catch (OseeCoreException ex) {
               OseeLog.log(Activator.class, Level.SEVERE, ex);
            }
         }
      }
      Image returnImage = BranchViewImageHandler.getImage(element, columnIndex);
      return returnImage;
   }

   @Override
   public void dispose() {
      // do nothing
   }
}

Back to the top