Skip to main content
summaryrefslogtreecommitdiffstats
blob: f2c571a8fb8122561f75123f2bd02c0252da12d0 (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
/*******************************************************************************
 * 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.Collections;
import java.util.HashSet;
import java.util.Set;
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.logging.OseeLevel;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.osee.framework.ui.swt.ALayout;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

/**
 * @author Roberto E. Escobar
 */
public class BranchSelectComposite extends Composite {
   protected static final int SIZING_TEXT_FIELD_WIDTH = 250;

   private Button branchSelectButton;
   private Text branchSelectTextWidget;
   private IOseeBranch selectedBranch;
   private final Set<Listener> listeners;
   private final boolean allowOnlyWorkingBranches;

   public BranchSelectComposite(Composite parent, int style, boolean allowOnlyWorkingBranches) {
      super(parent, style);
      this.allowOnlyWorkingBranches = allowOnlyWorkingBranches;
      this.listeners = Collections.synchronizedSet(new HashSet<Listener>());
      createControl(this);
   }

   public static BranchSelectComposite createWorkingBranchSelectComposite(Composite parent, int style) {
      return new BranchSelectComposite(parent, style, true);
   }

   public static BranchSelectComposite createBranchSelectComposite(Composite parent, int style) {
      return new BranchSelectComposite(parent, style, false);
   }

   private void createControl(Composite parent) {
      parent.setLayout(ALayout.getZeroMarginLayout(2, false));
      parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

      branchSelectTextWidget = new Text(parent, SWT.BORDER | SWT.READ_ONLY);
      GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
      data.widthHint = SIZING_TEXT_FIELD_WIDTH;
      branchSelectTextWidget.setLayoutData(data);
      branchSelectTextWidget.setBackground(Displays.getSystemColor(SWT.COLOR_WHITE));
      branchSelectTextWidget.setText(" -- Select A Branch -- ");
      branchSelectTextWidget.setDoubleClickEnabled(false);
      branchSelectTextWidget.addListener(SWT.MouseDoubleClick, new Listener() {
         @Override
         public void handleEvent(Event event) {
            handleSelectedBranch(event);
            notifyListener(event);
         }
      });

      branchSelectButton = new Button(parent, SWT.PUSH);
      branchSelectButton.setText("Select Branch...");
      branchSelectButton.addListener(SWT.Selection, new Listener() {

         @Override
         public void handleEvent(Event event) {
            handleSelectedBranch(event);
            notifyListener(event);
         }
      });
      branchSelectButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
      branchSelectButton.addListener(SWT.MouseUp, new Listener() {
         @Override
         public void handleEvent(Event event) {
            if (event.button == 3) {
               try {
                  setSelected(BranchManager.getCommonBranch());
                  notifyListener(event);
               } catch (OseeCoreException ex) {
                  OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
               }
            }
         }
      });
   }

   public IOseeBranch getSelectedBranch() {
      return selectedBranch;
   }

   private boolean areOnlyWorkingBranchesAllowed() {
      return allowOnlyWorkingBranches;
   }

   private void handleSelectedBranch(Event event) {
      if (event.widget == branchSelectButton || event.widget == branchSelectTextWidget && branchSelectTextWidget.getDoubleClickEnabled()) {
         if (areOnlyWorkingBranchesAllowed()) {
            Branch newBranch = BranchSelectionDialog.getWorkingBranchFromUser();
            if (newBranch != null) {
               setSelected(newBranch);
            }
         } else {
            Branch newBranch = BranchSelectionDialog.getBranchFromUser();
            if (newBranch != null) {
               setSelected(newBranch);
            }
         }
      }
   }

   public void setSelected(IOseeBranch branch) {
      selectedBranch = branch;
      if (branch == null) {
         branchSelectTextWidget.setText(" -- Select A Branch -- ");
      } else {
         branchSelectTextWidget.setText(selectedBranch.getName());
      }
   }

   private void notifyListener(Event event) {
      synchronized (listeners) {
         for (Listener listener : listeners) {
            listener.handleEvent(event);
         }
      }
   }

   public void addListener(Listener listener) {
      synchronized (listeners) {
         listeners.add(listener);
      }
   }

   public void removeListener(Listener listener) {
      synchronized (listeners) {
         listeners.remove(listener);
      }
   }

   /**
    * @param defaultSelectedBranch the defaultSelectedBranch to set
    */
   public void setDefaultSelectedBranch(IOseeBranch defaultSelectedBranch) {
      setSelected(defaultSelectedBranch);
   }

   /**
    * @return the branchSelectLabel
    */
   public Text getBranchSelectText() {
      return branchSelectTextWidget;
   }

   /**
    * @return the branchSelectLabel
    */
   public Button getBranchSelectButton() {
      return branchSelectButton;
   }
}

Back to the top