Skip to main content
summaryrefslogtreecommitdiffstats
blob: 38245a9193ceb253f0b952c88bd6f55426d82529 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*******************************************************************************
 * 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.ote.ui.define.panels;

import org.eclipse.osee.framework.core.client.ClientSessionManager;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.skynet.core.event.AccessControlEventType;
import org.eclipse.osee.framework.skynet.core.event.IAccessControlEventListener;
import org.eclipse.osee.framework.skynet.core.event.OseeEventManager;
import org.eclipse.osee.framework.skynet.core.event.Sender;
import org.eclipse.osee.framework.skynet.core.event2.AccessControlEvent;
import org.eclipse.osee.framework.ui.plugin.util.Displays;
import org.eclipse.osee.framework.ui.skynet.panels.BranchSelectSimpleComposite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

/**
 * @author Roberto E. Escobar
 */
public class GrayableBranchSelectionComposite extends Composite implements IAccessControlEventListener {

   private static final String GROUP_TEXT = "Upload results into OSEE";
   private static final String CHECK_BUTTON_TEXT = "Enable upload";
   private static final String CHECK_BUTTON_TOOL_TIP =
         "Select to store outfiles as Test Run Artifacts.\n" + "NOTE: User must be authenticated to create Artifacts.";
   private static final String AUTHENTICATION_WARNING_MESSAGE =
         "Authentication failure - Outfile upload not allowed. Double-Click on the lock icon at the bottom of the screen to authenticate.";
   private static final String FEATURE_DISABLED = "Feature disabled.";

   private BranchSelectSimpleComposite branchSelectComposite;
   private Button branchSelectEnabled;
   private Composite statusComposite;
   private Composite selectableComposite;
   private Label statusLabel;
   private StackLayout stackedLayout;
   private Group group;
   private boolean featureEnabled;

   public GrayableBranchSelectionComposite(Composite parent, int style) {
      super(parent, style);
      createControl(this);
   }

   @Override
   public void handleAccessControlArtifactsEvent(Sender sender, AccessControlEvent accessControlEvent) {
      if (accessControlEvent.getEventType() == AccessControlEventType.UserAuthenticated) {
         handleUserAuthenticated();
      }
   }

   private void createControl(Composite parent) {
      GridLayout gL = new GridLayout();
      gL.marginWidth = 0;
      gL.marginHeight = 0;
      parent.setLayout(gL);
      parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

      group = new Group(parent, SWT.NONE);
      group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
      stackedLayout = new StackLayout();
      group.setLayout(stackedLayout);
      group.setText(GROUP_TEXT);

      createStatusBar(group);
      createSelectableArea(group);
      handleBranchSelectEnabled(false);
      OseeEventManager.addListener(this);
   }

   private void createSelectableArea(Composite parent) {
      selectableComposite = new Composite(parent, SWT.NONE);
      selectableComposite.setLayout(new GridLayout());
      selectableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

      branchSelectEnabled = new Button(selectableComposite, SWT.CHECK);
      branchSelectEnabled.setText(CHECK_BUTTON_TEXT);
      branchSelectEnabled.setToolTipText(CHECK_BUTTON_TOOL_TIP);

      branchSelectEnabled.addSelectionListener(new SelectionAdapter() {
         @Override
         public void widgetSelected(SelectionEvent e) {
            handleBranchSelectEnabled(branchSelectEnabled.getSelection());
         }
      });
      this.branchSelectComposite =
            BranchSelectSimpleComposite.createWorkingBranchSelectComposite(selectableComposite, SWT.NONE);
   }

   private void createStatusBar(Composite parent) {
      statusComposite = new Composite(parent, SWT.BORDER);
      statusComposite.setLayout(new GridLayout(2, false));
      statusComposite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false));
      statusComposite.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
      statusComposite.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_FOREGROUND));

      Label imageLabel = new Label(statusComposite, SWT.NONE);
      imageLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
      imageLabel.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_WARN_TSK));
      imageLabel.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
      imageLabel.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_FOREGROUND));

      statusLabel = new Label(statusComposite, SWT.NONE);
      statusLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
      statusLabel.setText("");
      statusLabel.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
      statusLabel.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_RED));
   }

   private void handleBranchSelectEnabled(boolean isSelected) {
      setSelectable(true);
      if (ClientSessionManager.isSessionValid() != true) {
         statusLabel.setText(AUTHENTICATION_WARNING_MESSAGE);
         setSelectable(false);
         isSelected = false;
      }
      branchSelectEnabled.setSelection(isSelected);
      branchSelectComposite.setEnabled(isSelected);
      for (Control control : branchSelectComposite.getChildren()) {
         control.setEnabled(isSelected);
      }
   }

   public Branch getSelectedBranch() {
      return branchSelectComposite.getSelectedBranch();
   }

   public boolean isBranchSelectEnabled() {
      return branchSelectEnabled.getSelection();
   }

   public void setFeatureEnabled(final boolean isEnabled) {
      Display.getDefault().asyncExec(new Runnable() {
         public void run() {
            featureEnabled = isEnabled;
            handleBranchSelectEnabled(false);
            statusLabel.setText(featureEnabled != true ? FEATURE_DISABLED : "");
            setEnabled(featureEnabled);
            setSelectable(featureEnabled);
         }
      });
   }

   private boolean isFeatureEnabled() {
      return featureEnabled;
   }

   @Override
   public void setEnabled(boolean enabled) {
      super.setEnabled(enabled);
      for (Control child : group.getChildren()) {
         child.setEnabled(enabled);
      }
      branchSelectEnabled.setEnabled(enabled);
   }

   public String[] getBranchIds() {
      return branchSelectComposite.getBranchIds();
   }

   public void restoreWidgetValues(boolean saveAsArtifact, String[] branchIds, String lastSelected) {
      branchSelectEnabled.setSelection(saveAsArtifact && ClientSessionManager.isSessionValid());
      branchSelectComposite.restoreWidgetValues(branchIds, lastSelected);
   }

   private void setSelectable(final boolean isSelectable) {
      Display.getDefault().asyncExec(new Runnable() {
         public void run() {
            if (isSelectable != false) {
               stackedLayout.topControl = selectableComposite;
            } else {
               stackedLayout.topControl = statusComposite;
            }
            group.layout();
         }
      });
   }

   @Override
   public void dispose() {
      OseeEventManager.removeListener(this);
      super.dispose();
   }

   private void handleUserAuthenticated() {
      Displays.ensureInDisplayThread(new Runnable() {
         @Override
         public void run() {
            if (branchSelectEnabled != null && branchSelectEnabled.isDisposed() != true && isFeatureEnabled() != false) {
               handleBranchSelectEnabled(branchSelectEnabled.getSelection());
            }
         }
      });
   }

   public boolean runOnEventInDisplayThread() {
      return true;
   }
}

Back to the top