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

import java.util.Collection;
import java.util.List;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.nebula.widgets.xviewer.XViewer;
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.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.ui.skynet.widgets.xBranch.BranchOptionsEnum;
import org.eclipse.osee.framework.ui.skynet.widgets.xBranch.XBranchWidget;
import org.eclipse.osee.framework.ui.skynet.widgets.xBranch.XBranchWidget.BranchSelectedListener;
import org.eclipse.osee.framework.ui.skynet.widgets.xBranch.XBranchWidget.IBranchWidgetMenuListener;
import org.eclipse.osee.framework.ui.skynet.widgets.xBranch.actions.SetAsFavoriteAction;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

/**
 * @author Donald G. Dunne
 */
public class BranchSelectionDialog extends MessageDialog implements IBranchWidgetMenuListener {

   private IOseeBranch selected;
   private BranchId defaultSelected;
   private static BranchId lastSelectedBranch;
   private XBranchWidget branchWidget;
   private boolean allowOnlyWorkingBranches;
   private final Collection<? extends BranchId> branches;

   public BranchSelectionDialog(String title, boolean allowOnlyWorkingBranches) {
      this(title, null);
      this.allowOnlyWorkingBranches = allowOnlyWorkingBranches;
   }

   public BranchSelectionDialog(String title, Collection<? extends BranchId> branches) {
      super(Displays.getActiveShell(), title, null, null, MessageDialog.NONE, new String[] {"Ok", "Cancel"}, 0);
      allowOnlyWorkingBranches = false;
      selected = null;
      this.branches = branches;
      setShellStyle(getShellStyle() | SWT.RESIZE);
   }

   public void setDefaultSelection(BranchId branch) {
      defaultSelected = branch;
   }

   public IOseeBranch getSelection() {
      return selected;
   }

   @Override
   protected Control createDialogArea(Composite container) {
      branchWidget = new XBranchWidget(true, true, defaultSelected, this);
      branchWidget.setDisplayLabel(false);
      branchWidget.createWidgets(container, 1);
      branchWidget.setBranchOptions(true, BranchOptionsEnum.FAVORITE_KEY, BranchOptionsEnum.FLAT_KEY);
      branchWidget.setBranchOptions(allowOnlyWorkingBranches, BranchOptionsEnum.SHOW_WORKING_BRANCHES_ONLY);
      branchWidget.addBranchSelectedListener(new BranchSelectedListener() {

         @Override
         public void onBranchSelected(BranchId branch) {
            getButton(IDialogConstants.OK_ID).setEnabled(true);
            storeSelectedBranch();
         }

      });
      final XViewer viewer = branchWidget.getXViewer();
      viewer.getFilterDataUI().addFilterTextListener(new KeyListener() {

         @Override
         public void keyReleased(KeyEvent e) {
            Collection<TreeItem> visibleItems = viewer.getVisibleItems();
            if (visibleItems.size() == 1) {
               viewer.setSelection(new StructuredSelection(new Object[] {visibleItems.iterator().next().getData()}));
               getButton(IDialogConstants.OK_ID).setEnabled(true);
               storeSelectedBranch();
            }
         }

         @Override
         public void keyPressed(KeyEvent e) {
            // do nothing
         }
      });
      if (branches != null) {
         branchWidget.loadData(branches);
      } else {
         branchWidget.loadData();
      }

      GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
      gd.heightHint = 500;
      gd.widthHint = 800;
      Tree viewersTree = viewer.getTree();
      viewersTree.setLayoutData(gd);
      viewersTree.addListener(SWT.MouseDoubleClick, new Listener() {
         @Override
         public void handleEvent(Event event) {
            handleDoubleClick();
         }
      });
      viewersTree.addSelectionListener(new SelectionAdapter() {
         @Override
         public void widgetSelected(SelectionEvent e) {
            getButton(IDialogConstants.OK_ID).setEnabled(true);
            storeSelectedBranch();
         }

      });

      return branchWidget.getControl();
   }

   @Override
   public void updateMenuActionsForTable(MenuManager mm) {
      mm.insertBefore(XViewer.MENU_GROUP_PRE, new SetAsFavoriteAction(branchWidget));
   }

   @Override
   protected Button createButton(Composite parent, int id, String label, boolean defaultButton) {
      Button selectionButton = super.createButton(parent, id, label, defaultButton);
      // default the ok button to disabled until the user selects a branch
      if (id == IDialogConstants.OK_ID) {
         selectionButton.setEnabled(false);
      }
      return selectionButton;
   }

   private void storeSelectedBranch() {
      List<IOseeBranch> branches = branchWidget.getSelectedBranches();

      if (!branches.isEmpty()) {
         selected = branches.iterator().next();
         BranchManager.setLastBranch(selected);
         lastSelectedBranch = selected;
      }
   }

   private void handleDoubleClick() {
      storeSelectedBranch();
      okPressed();
   }

   private static IOseeBranch createDialog(boolean allowOnlyWorkingBranches) {
      IOseeBranch toReturn = null;
      BranchSelectionDialog branchSelection = new BranchSelectionDialog("Select Branch", allowOnlyWorkingBranches);
      if (lastSelectedBranch != null) {
         try {
            Branch fullBranch = BranchManager.getBranch(lastSelectedBranch);
            if (fullBranch != null && fullBranch.getArchiveState().isUnArchived()) {
               branchSelection.setDefaultSelection(lastSelectedBranch);
            }
         } catch (OseeCoreException ex) {
            //do nothing
         }
      }
      int result = branchSelection.open();
      if (result == Window.OK) {
         toReturn = branchSelection.getSelection();
      }
      return toReturn;
   }

   public static IOseeBranch getBranchFromUser() {
      return createDialog(false);
   }

   public static IOseeBranch getWorkingBranchFromUser() {
      return createDialog(true);
   }
}

Back to the top