Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0456c77f924f1a2e5f7416fc77cf2d47602af5bb (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
/*******************************************************************************
 * 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.commandHandlers.branch;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.osee.framework.access.AccessControlManager;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.enums.CoreBranches;
import org.eclipse.osee.framework.core.enums.PermissionEnum;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.TransactionRecord;
import org.eclipse.osee.framework.plugin.core.util.IExceptionableRunnable;
import org.eclipse.osee.framework.plugin.core.util.Jobs;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionManager;
import org.eclipse.osee.framework.ui.plugin.util.AWorkbench;
import org.eclipse.osee.framework.ui.plugin.util.CommandHandler;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.osee.framework.ui.skynet.widgets.dialog.EntryCheckDialog;
import org.eclipse.osee.framework.ui.swt.Displays;

/**
 * @author Jeff C. Phillips
 * @author Ryan D. Brooks
 * @author Robert A. Fisher
 */
public class BranchCreationHandler extends CommandHandler {

   @Override
   public Object executeWithException(ExecutionEvent event) throws OseeCoreException {
      IStructuredSelection selection =
         (IStructuredSelection) AWorkbench.getActivePage().getActivePart().getSite().getSelectionProvider().getSelection();
      Object backingData = selection.getFirstElement();

      final TransactionRecord parentTransactionId;
      if (backingData instanceof Branch) {
         Branch branch = (Branch) backingData;
         parentTransactionId = TransactionManager.getHeadTransaction(branch);
      } else if (backingData instanceof TransactionRecord) {
         parentTransactionId = (TransactionRecord) backingData;
      } else {
         throw new OseeStateException("Backing data for the jobbed node in the branchview was not of the expected type");
      }
      final EntryCheckDialog dialog =
         new EntryCheckDialog(Displays.getActiveShell(), "Branch", null, "Enter the name of the new branch:",
            "Include a copy of the chosen transaction on the new branch", MessageDialog.INFORMATION, new String[] {
               "OK",
               "Cancel"}, 0);
      int result = dialog.open();

      if (result == 0 && dialog.getEntry() != null) {

         IExceptionableRunnable runnable = new IExceptionableRunnable() {
            @Override
            public IStatus run(IProgressMonitor monitor) throws Exception {
               Branch branch = parentTransactionId.getBranch();
               if (branch.equals(CoreBranches.SYSTEM_ROOT)) {
                  BranchManager.createTopLevelBranch(dialog.getEntry());
               } else {
                  if (dialog.isChecked()) {
                     BranchManager.createWorkingBranchFromTx(parentTransactionId, dialog.getEntry());
                  } else {
                     BranchManager.createWorkingBranch(parentTransactionId, dialog.getEntry(), null, null);
                  }

               }
               return Status.OK_STATUS;
            }
         };

         Jobs.runInJob("Create Branch", runnable, Activator.class, Activator.PLUGIN_ID);
      }

      return null;
   }

   @Override
   public boolean isEnabledWithException(IStructuredSelection structuredSelection) throws OseeCoreException {
      boolean enabled;
      if (structuredSelection.size() != 1) {
         return false;
      }

      Object object = structuredSelection.getFirstElement();
      IOseeBranch branch = null;

      if (object instanceof IOseeBranch) {
         branch = (IOseeBranch) object;
      } else if (object instanceof TransactionRecord) {
         branch = ((TransactionRecord) object).getBranch();
      }

      if (branch == null || BranchManager.getBranch(branch).getArchiveState().isArchived()) {
         return false;
      }

      enabled = AccessControlManager.hasPermission(branch, PermissionEnum.READ);
      return enabled;
   }
}

Back to the top