Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9e4e182f7d4a48b82db830a0a30d1958265c0f90 (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
/*******************************************************************************
 * 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 java.util.List;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.logging.OseeLevel;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.UserManager;
import org.eclipse.osee.framework.skynet.core.access.AccessControlManager;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
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.SkynetGuiPlugin;
import org.eclipse.osee.framework.ui.skynet.commandHandlers.Handlers;
import org.eclipse.osee.framework.ui.skynet.widgets.xBranch.BranchXViewerFactory;
import org.eclipse.osee.framework.ui.swt.Widgets;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;

/**
 * @author Jeff C. Phillips
 * @author Paul K. Waldfogel
 */
public class RenameBranchHandler extends CommandHandler {

   private int findColumnIndex(TreeViewer treeViewer, String columnName) {
      if (treeViewer != null && Widgets.isAccessible(treeViewer.getTree())) {
         TreeColumn[] columns = treeViewer.getTree().getColumns();
         for (int index = 0; index < columns.length; index++) {
            TreeColumn column = columns[index];
            if (columnName.equalsIgnoreCase(column.getText())) {
               return index;
            }
         }
      }
      return 0;
   }

   @Override
   public Object execute(ExecutionEvent event) throws ExecutionException {
      ISelectionProvider selectionProvider =
            AWorkbench.getActivePage().getActivePart().getSite().getSelectionProvider();

      if (selectionProvider instanceof TreeViewer) {
         final TreeViewer treeViewer = (TreeViewer) selectionProvider;
         Tree tree = treeViewer.getTree();
         IStructuredSelection selection = (IStructuredSelection) selectionProvider.getSelection();

         final Branch selectedBranch = Handlers.getBranchesFromStructuredSelection(selection).iterator().next();
         TreeItem[] myTreeItemsSelected = tree.getSelection();

         if (myTreeItemsSelected.length != 1) {
            return null;
         }
         TreeEditor myTreeEditor = new TreeEditor(tree);
         myTreeEditor.horizontalAlignment = SWT.LEFT;
         myTreeEditor.grabHorizontal = true;
         myTreeEditor.minimumWidth = 50;
         myTreeEditor.setColumn(findColumnIndex(treeViewer, BranchXViewerFactory.branchName.getName()));

         final TreeItem myTreeItem = myTreeItemsSelected[0];

         Control oldEditor = myTreeEditor.getEditor();
         if (oldEditor != null) {
            oldEditor.dispose();
         }
         final Text textBeingRenamed = new Text(tree, SWT.BORDER);
         textBeingRenamed.setText(selectedBranch.getName());

         textBeingRenamed.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
               updateText(textBeingRenamed.getText(), selectedBranch);
               treeViewer.refresh();
               textBeingRenamed.dispose();
            }

            @Override
            public void focusGained(FocusEvent e) {
            }
         });
         textBeingRenamed.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
               if (e.character == SWT.CR) {
                  updateText(textBeingRenamed.getText(), selectedBranch);
                  treeViewer.refresh();
                  textBeingRenamed.dispose();
               } else if (e.keyCode == SWT.ESC) {
                  textBeingRenamed.dispose();
               }
            }
         });
         textBeingRenamed.selectAll();
         textBeingRenamed.setFocus();
         myTreeEditor.setEditor(textBeingRenamed, myTreeItem);
      }
      return null;
   }

   private void updateText(String newLabel, Branch selectedBranch) {
      String oldName = selectedBranch.getName();
      selectedBranch.setName(newLabel);
      try {
         BranchManager.persist(selectedBranch);
      } catch (Exception ex) {
         selectedBranch.setName(oldName);
         OseeLog.log(SkynetGuiPlugin.class, OseeLevel.SEVERE_POPUP, ex);
      }
   }

   @Override
   public boolean isEnabledWithException(IStructuredSelection structuredSelection) throws OseeCoreException {
      List<Branch> branches = Handlers.getBranchesFromStructuredSelection(structuredSelection);
      return branches.size() == 1 && (AccessControlManager.isOseeAdmin() || branches.get(0).getBranchType().isWorkingBranch() || branches.get(
            0).getBaseTransaction().getAuthor() == UserManager.getUser().getArtId());
   }

}

Back to the top