Skip to main content
summaryrefslogtreecommitdiffstats
blob: a5b926163240f57221e2e6a0f9ef19ead870d217 (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
/*******************************************************************************
 * 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 java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.data.IOseeBranch;
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.jdk.core.util.AHTML;
import org.eclipse.osee.framework.logging.OseeLevel;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.plugin.core.util.Jobs;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.ui.plugin.PluginUiImage;
import org.eclipse.osee.framework.ui.skynet.change.BranchTransactionUiData;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.osee.framework.ui.skynet.widgets.GenericXWidget;
import org.eclipse.osee.framework.ui.swt.ALayout;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.osee.framework.ui.swt.FontManager;
import org.eclipse.osee.framework.ui.swt.ImageManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * @author Karol M. Wilk
 * @author Jeff C. Phillips
 */
public class XBranchWidget extends GenericXWidget {
   private BranchXViewer branchXViewer;
   public final static String normalColor = "#EEEEEE";
   private static final String LOADING = "Loading ...";
   protected Label extraInfoLabel;
   private XBranchContentProvider branchContentProvider;
   private XBranchLabelProvider xBranchLabelProvider;
   private FavoriteSorter sorter;
   private static final String VIEW_ID = "BrachView";
   private final boolean filterRealTime;
   private final boolean searchRealTime;
   private ToolBar toolBar;
   private final BranchId selectedBranch;
   private final List<BranchSelectedListener> branchSelectedListeners;
   private BranchXViewerFactory branchXViewerFactory;
   private final IBranchWidgetMenuListener menuListener;

   public XBranchWidget(IBranchWidgetMenuListener menuListener) {
      this(false, false, null, menuListener);
   }

   public XBranchWidget(boolean filterRealTime, boolean searchRealTime, BranchId selectedBranch, IBranchWidgetMenuListener menuListener) {
      super(VIEW_ID);
      this.filterRealTime = filterRealTime;
      this.searchRealTime = searchRealTime;
      this.selectedBranch = selectedBranch;
      this.menuListener = menuListener;
      branchSelectedListeners = new CopyOnWriteArrayList<>();
      branchXViewerFactory = new BranchXViewerFactory();
   }

   public XBranchWidget(BranchXViewerFactory branchXViewerFactory, IBranchWidgetMenuListener menuListener) {
      this(false, false, null, menuListener);
      this.branchXViewerFactory = branchXViewerFactory;
   }

   public void setBranchOptions(boolean state, BranchOptionsEnum... options) {
      for (BranchOptionsEnum option : options) {
         if (branchContentProvider != null) {
            switch (option) {
               case FAVORITE_KEY:
                  sorter.setFavoritesFirst(state);
                  break;
               case FLAT_KEY:
                  branchContentProvider.setPresentation(state);
                  break;
               case SHOW_MERGE_BRANCHES:
                  branchContentProvider.setShowMergeBranches(state);
                  break;
               case SHOW_TRANSACTIONS:
                  branchContentProvider.setShowTransactions(state);
                  break;
               case SHOW_ARCHIVED_BRANCHES:
                  branchContentProvider.setShowArchivedBranches(state);
                  break;
               case SHOW_WORKING_BRANCHES_ONLY:
                  branchContentProvider.setShowOnlyWorkingBranches(state);
                  break;
            }
         }
      }
      refresh();
   }

   public void reveal(Branch branch) {
      branchXViewer.reveal(branch);
      branchXViewer.setSelection(new StructuredSelection(branch), true);
      refresh();
   }

   public void setSelectedBranch(Branch branch) {
      branchXViewer.reveal(branch);
      branchXViewer.setSelection(new StructuredSelection(branch), true);
   }

   @Override
   public void adaptControls(FormToolkit toolkit) {
      super.adaptControls(toolkit);
      toolkit.adapt(extraInfoLabel, true, true);
   }

   @Override
   protected void createControls(Composite parent, int horizontalSpan) {
      // Create Text Widgets
      if (isDisplayLabel() && !getLabel().equals("")) {
         labelWidget = new Label(parent, SWT.NONE);
         labelWidget.setText(getLabel() + ":");
         if (getToolTip() != null) {
            labelWidget.setToolTipText(getToolTip());
         }
      }

      Composite mainComp = new Composite(parent, SWT.BORDER);
      mainComp.setLayout(ALayout.getZeroMarginLayout());
      mainComp.setLayoutData(new GridData(GridData.FILL_BOTH));
      if (toolkit != null) {
         toolkit.paintBordersFor(mainComp);
      }

      createTaskActionBar(mainComp);

      branchXViewer = new BranchXViewer(mainComp, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION, branchXViewerFactory,
         this, filterRealTime, searchRealTime);
      branchXViewer.setMenuListener(menuListener);
      branchXViewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));

      branchContentProvider = new XBranchContentProvider(branchXViewer);
      branchXViewer.setContentProvider(branchContentProvider);
      xBranchLabelProvider = new XBranchLabelProvider(branchXViewer);
      branchXViewer.setLabelProvider(xBranchLabelProvider);
      sorter = new FavoriteSorter(branchXViewer);
      branchXViewer.setSorter(sorter);

      if (toolkit != null) {
         toolkit.adapt(branchXViewer.getStatusLabel(), false, false);
      }
      new ActionContributionItem(branchXViewer.getCustomizeAction()).fill(toolBar, -1);

      Tree tree = branchXViewer.getTree();
      GridData gridData = new GridData(GridData.FILL_BOTH);
      gridData.heightHint = 100;
      tree.setLayout(ALayout.getZeroMarginLayout());
      tree.setLayoutData(gridData);
      tree.setHeaderVisible(true);
      tree.setLinesVisible(true);
   }

   public void createTaskActionBar(Composite parent) {
      Composite composite = new Composite(parent, SWT.NONE);
      GridLayout layout = ALayout.getZeroMarginLayout(2, false);
      layout.marginLeft = 5;
      composite.setLayout(layout);
      composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

      extraInfoLabel = new Label(composite, SWT.NONE);
      extraInfoLabel.setAlignment(SWT.LEFT);
      extraInfoLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
      extraInfoLabel.setText("\n");
      extraInfoLabel.setFont(FontManager.getCourierNew12Bold());

      if (branchXViewerFactory.isBranchManager()) {
         toolBar = new ToolBar(composite, SWT.FLAT);
         ToolItem item = null;

         item = new ToolItem(toolBar, SWT.PUSH);
         item.setImage(ImageManager.getImage(PluginUiImage.REFRESH));
         item.setToolTipText("Refresh");
         item.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
               try {
                  BranchManager.refreshBranches();
               } catch (OseeCoreException ex) {
                  OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
               }
               loadData();
            }
         });
      }
   }

   public ArrayList<IOseeBranch> getSelectedBranches() {
      ArrayList<IOseeBranch> items = new ArrayList<>();
      if (branchXViewer == null || branchXViewer.getSelection().isEmpty()) {
         return items;
      }
      Iterator<?> i = ((IStructuredSelection) branchXViewer.getSelection()).iterator();
      while (i.hasNext()) {
         Object obj = i.next();

         if (obj instanceof Branch) {
            items.add((Branch) obj);
         }
      }
      return items;
   }

   @SuppressWarnings("rawtypes")
   public ArrayList<TransactionRecord> getSelectedTransactionRecords() {
      ArrayList<TransactionRecord> items = new ArrayList<>();
      if (branchXViewer == null || branchXViewer.getSelection().isEmpty()) {
         return items;
      }
      Iterator i = ((IStructuredSelection) branchXViewer.getSelection()).iterator();
      while (i.hasNext()) {
         Object obj = i.next();

         if (obj instanceof TransactionRecord) {
            items.add((TransactionRecord) obj);
         }
      }
      return items;
   }

   @Override
   public Control getControl() {
      return branchXViewer.getTree();
   }

   @Override
   public void dispose() {
      super.dispose();
      if (branchXViewer != null) {
         branchXViewer.dispose();
      }
   }

   @Override
   public void refresh() {
      loadData();
      branchXViewer.refresh();
      validate();
   }

   @Override
   public IStatus isValid() {
      return Status.OK_STATUS;
   }

   @Override
   public String toHTML(String labelFont) {
      return AHTML.simplePage("Unhandled");
   }

   public BranchXViewer getXViewer() {
      return branchXViewer;
   }

   @Override
   public Object getData() {
      return branchXViewer.getInput();
   }

   public void loadData() {
      loadData(((XBranchContentProvider) branchXViewer.getContentProvider()).getBranchManagerChildren());
   }

   public void setExtraInfoLabel(String message) {
      if (extraInfoLabel != null && !extraInfoLabel.isDisposed()) {
         extraInfoLabel.setText(message);
      }
   }

   public void loadData(final Object input) {
      final Object[] expandedBranches = getXViewer().getExpandedElements();

      setExtraInfoLabel(LOADING);

      Job job = new Job("Banch Manager") {

         @Override
         protected IStatus run(IProgressMonitor monitor) {

            Displays.ensureInDisplayThread(new Runnable() {

               @Override
               public void run() {
                  if (extraInfoLabel != null && !extraInfoLabel.isDisposed()) {
                     extraInfoLabel.setText("");
                  }
                  if (branchXViewer != null && branchXViewer.getTree() != null && !branchXViewer.getTree().isDisposed()) {
                     if (input instanceof BranchTransactionUiData) {
                        Object[] transactions = ((BranchTransactionUiData) input).getTransactions();
                        branchXViewer.setInput(transactions);
                     } else {
                        branchXViewer.setInput(input);
                     }
                     getXViewer().setExpandedElements(expandedBranches);
                     if (selectedBranch != null) {
                        getXViewer().reveal(selectedBranch);
                        getXViewer().setSelection(new StructuredSelection(selectedBranch), true);
                        for (BranchSelectedListener listener : branchSelectedListeners) {
                           listener.onBranchSelected(selectedBranch);
                        }
                     }
                  }
               }
            });
            return Status.OK_STATUS;
         }
      };
      Jobs.startJob(job);
   }

   @Override
   public boolean isEmpty() {
      return branchXViewer.getTree().getItemCount() == 0;
   }

   public void addBranchSelectedListener(BranchSelectedListener listener) {
      branchSelectedListeners.add(listener);
   }

   public void removeBranchSelectedListener(BranchSelectedListener listener) {
      branchSelectedListeners.remove(listener);
   }

   public interface BranchSelectedListener {

      public void onBranchSelected(BranchId branch);
   }
   public interface IBranchWidgetMenuListener {
      public void updateMenuActionsForTable(MenuManager mm);

   }

}

Back to the top