Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 12a40c75cf6b4982258dc9b4ade8f7da2ce2056e (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
/*
 * Copyright (c) 2015 Eike Stepper (Berlin, Germany) and others.
 * 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.internal.ui.handlers;

import org.eclipse.emf.cdo.common.branch.CDOBranch;
import org.eclipse.emf.cdo.common.branch.CDOBranchCreationContext;
import org.eclipse.emf.cdo.common.branch.CDOBranchPoint;
import org.eclipse.emf.cdo.internal.ui.dialogs.CreateBranchDialog;

import org.eclipse.net4j.util.ui.handlers.AbstractBaseHandler;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Eike Stepper
 */
public class CreateBranchHandler extends AbstractBaseHandler<CDOBranchCreationContext>
{
  private CDOBranchPoint base;

  private String name;

  public CreateBranchHandler()
  {
    super(CDOBranchCreationContext.class, false);
  }

  @Override
  protected void preRun(ExecutionEvent event) throws Exception
  {
    if (elements.size() == 1)
    {
      CDOBranchCreationContext context = elements.get(0);
      base = context.getBase();
      name = getValidChildName(base.getBranch());

      Shell shell = HandlerUtil.getActiveShell(event);
      CreateBranchDialog dialog = new CreateBranchDialog(shell, base, name);
      if (dialog.open() == CreateBranchDialog.OK)
      {
        base = dialog.getBranchPoint();
        name = dialog.getName();
        return;
      }
    }

    base = null;
    name = null;
    cancel();
  }

  @Override
  protected void doExecute(IProgressMonitor monitor) throws Exception
  {
    try
    {
      CDOBranch branch = base.getBranch();
      branch.createBranch(name, base.getTimeStamp());
    }
    finally
    {
      base = null;
      name = null;
    }
  }

  public static String getValidChildName(CDOBranch branch)
  {
    Set<String> names = new HashSet<String>();
    for (CDOBranch child : branch.getBranches())
    {
      names.add(child.getName());
    }

    for (int i = 1; i < Integer.MAX_VALUE; i++)
    {
      String name = "branch" + i;
      if (!names.contains(name))
      {
        return name;
      }
    }

    throw new IllegalStateException("Too many sub branches: " + branch);
  }

  /**
   * @author Eike Stepper
   */
  public static class TagHandler extends CreateBranchHandler
  {
  }
}

Back to the top