Skip to main content
summaryrefslogtreecommitdiffstats
blob: 147caadf85e600e9ae02f3225cfbe518ea42f0b6 (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
/*******************************************************************************
 * Copyright (c) 2013 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.ats.util.widgets;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import org.eclipse.osee.ats.internal.Activator;
import org.eclipse.osee.ats.internal.AtsClientService;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.MergeBranch;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.ui.skynet.FrameworkImage;
import org.eclipse.osee.framework.ui.skynet.util.MergeInProgressHandler;
import org.eclipse.osee.framework.ui.skynet.widgets.dialog.FilteredCheckboxBranchDialog;
import org.eclipse.osee.framework.ui.swt.ImageManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

/**
 * @author Angel Avila
 */
public class XWorkingBranchDeleteMerge extends XWorkingBranchButtonAbstract {

   public final static String WIDGET_NAME = "XWorkingBranchDeleteMerge";

   @Override
   protected void initButton(final Button button) {
      button.setToolTipText("Delete Merge Branch(es)");
      button.setImage(ImageManager.getImage(FrameworkImage.DELETE));
      button.addListener(SWT.Selection, new Listener() {
         @Override
         public void handleEvent(Event e) {
            try {
               Branch workingBranch = BranchManager.getBranch(getTeamArt().getWorkingBranch());
               if (isWorkingBranchCommitWithMergeInProgress()) {
                  List<Branch> selectedBranches = new ArrayList<>();
                  Collection<IOseeBranch> branchesAlreadyCommitted =
                     AtsClientService.get().getBranchService().getBranchesCommittedTo(getTeamArt());
                  List<MergeBranch> mergeBranches = BranchManager.getMergeBranches(workingBranch);

                  Set<Branch> destinationMinusAlreadyCommitted = new HashSet<>();
                  // Remove all the Merge branches having to do with a Destination branch that's already been committed, can't delete these merge branches
                  for (MergeBranch branch : mergeBranches) {
                     if (!branchesAlreadyCommitted.contains(branch.getDestinationBranch())) {
                        destinationMinusAlreadyCommitted.add(branch.getDestinationBranch());
                     }
                  }

                  if (destinationMinusAlreadyCommitted.size() > 1) {
                     FilteredCheckboxBranchDialog dialog =
                        new FilteredCheckboxBranchDialog("Select Destination Branch(es)",
                           "Select the Destination branch(es) for which you want to Delete the Merge Branch",
                           destinationMinusAlreadyCommitted);
                     if (dialog.open() == 0) {
                        for (IOseeBranch branchToken : dialog.getChecked()) {
                           selectedBranches.add(BranchManager.getBranch(branchToken));
                        }
                     }
                  } else if (destinationMinusAlreadyCommitted.size() == 1) {
                     MergeBranch mergeBranch = BranchManager.getFirstMergeBranch(workingBranch);
                     selectedBranches.add(mergeBranch.getDestinationBranch());
                  }

                  if (!selectedBranches.isEmpty()) {
                     MergeInProgressHandler.deleteMultipleMergeBranches(workingBranch, selectedBranches, false);
                  }
               }
            } catch (OseeCoreException ex) {
               OseeLog.log(Activator.class, Level.SEVERE, ex);
            }
         }
      });
   }

   @Override
   protected void refreshEnablement(Button button) {
      button.setEnabled(destinationBranchNotCommitted());
   }

   private boolean destinationBranchNotCommitted() {
      boolean toReturn = false;
      try {
         if (isWorkingBranchCommitWithMergeInProgress()) {
            List<MergeBranch> mergeBranches = BranchManager.getMergeBranches(getWorkingBranch());
            Collection<IOseeBranch> committedBranches =
               AtsClientService.get().getBranchService().getBranchesCommittedTo(getTeamArt());
            List<MergeBranch> remainingMergeBranches = new ArrayList<>();

            for (MergeBranch mergeBranch : mergeBranches) {
               if (!committedBranches.contains(mergeBranch.getDestinationBranch())) {
                  remainingMergeBranches.add(mergeBranch);
               }
            }
            if (!remainingMergeBranches.isEmpty()) {
               toReturn = true;
            }
         }
      } catch (OseeCoreException ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      }

      return toReturn;

   }
}

Back to the top