Skip to main content
summaryrefslogtreecommitdiffstats
blob: af9fef32b598700309a64c3397acd3c3744111e2 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*******************************************************************************
 * Copyright (c) 2011 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.ats.view.web.components;

import org.eclipse.osee.ats.api.components.AtsSearchHeaderComponent;
import org.eclipse.osee.ats.api.data.AtsSearchParameters;
import org.eclipse.osee.ats.api.search.AtsSearchPresenter;
import org.eclipse.osee.ats.view.web.AtsNavigator;
import org.eclipse.osee.ats.view.web.AtsUiApplication;
import org.eclipse.osee.ats.view.web.search.AtsSearchResultsView;
import org.eclipse.osee.display.api.data.ViewId;
import org.eclipse.osee.display.api.search.SearchProgressListener;
import org.eclipse.osee.display.api.search.SearchProgressProvider;
import org.eclipse.osee.display.view.web.CssConstants;
import org.eclipse.osee.display.view.web.components.ComponentUtility;
import org.eclipse.osee.display.view.web.components.OseeExceptionDialogComponent;
import org.eclipse.osee.display.view.web.components.OseeLeftMarginContainer;
import org.eclipse.osee.display.view.web.components.OseeLogoLink;
import org.eclipse.osee.display.view.web.components.OseeSearchHeaderComponent;
import org.eclipse.osee.vaadin.widgets.HasViewTitle;
import com.vaadin.Application;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.event.Action;
import com.vaadin.event.Action.Handler;
import com.vaadin.event.ShortcutAction;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.ProgressIndicator;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window.Notification;

/**
 * @author Shawn F. Cook
 */
@SuppressWarnings("serial")
public class AtsSearchHeaderImpl extends OseeSearchHeaderComponent implements AtsSearchHeaderComponent, Handler, HasViewTitle, SearchProgressListener {

   private final String SEARCHBUTTON_SEARCH = "Search";
   private final String SEARCHBUTTON_CANCEL = "Cancel";
   private boolean isLayoutComplete = false;
   private final ComboBox programCombo = new ComboBox("Program:");
   private final ComboBox buildCombo = new ComboBox("Build:");
   private final CheckBox nameOnlyCheckBox = new CheckBox("Name Only", true);
   private final TextField searchTextField = new TextField();
   private final Button searchButton = new Button(SEARCHBUTTON_SEARCH);
   private boolean lockProgramCombo = false;
   private final Panel searchTextPanel = new Panel();
   private final ProgressIndicator progressIndicator = new ProgressIndicator();
   private final Label workingLabel = new Label("Working");
   private boolean isSearchInProgress = false;

   @Override
   public void attach() {
      if (!isLayoutComplete) {
         AtsSearchPresenter searchPresenter = getPresenter();
         if (searchPresenter != null && searchPresenter instanceof SearchProgressProvider) {
            ((SearchProgressProvider) searchPresenter).addListener(this);
         } else {
            ComponentUtility.logWarn("AtsSearchHeaderImpl.attach() Invalid searchPresenter", this);
         }
         createLayout();
      }
      isLayoutComplete = true;
   }

   private void validateSearchAndEnableSearchButton() {
      ViewId program = (ViewId) programCombo.getValue();
      ViewId build = (ViewId) buildCombo.getValue();
      if (program != null && build != null) {
         searchButton.setEnabled(true);
      } else {
         searchButton.setEnabled(false);
      }
   }

   private void selectProgram() {
      if (ComponentUtility.isAccessible(programCombo)) {
         ViewId program = (ViewId) programCombo.getValue();
         getPresenter().selectProgram(program, this);
      }
   }

   public AtsSearchHeaderImpl() {
      if (ComponentUtility.isAccessible(programCombo)) {
         programCombo.setNullSelectionAllowed(false);
         programCombo.addListener(new Property.ValueChangeListener() {
            @Override
            public void valueChange(ValueChangeEvent event) {
               if (!lockProgramCombo) {
                  selectProgram();
               }
               validateSearchAndEnableSearchButton();
            }
         });
         programCombo.setImmediate(true);

         buildCombo.addListener(new Property.ValueChangeListener() {
            @Override
            public void valueChange(ValueChangeEvent event) {
               validateSearchAndEnableSearchButton();
            }
         });
         buildCombo.setImmediate(true);
      }
      if (ComponentUtility.isAccessible(buildCombo)) {
         buildCombo.setNullSelectionAllowed(false);
      }

      searchTextField.setImmediate(true);
      validateSearchAndEnableSearchButton();
   }

   protected void selectSearch() {
      if (searchButton.isEnabled()) {
         if (searchButton.getCaption().equals(SEARCHBUTTON_SEARCH)) {
            if (ComponentUtility.isAccessible(programCombo, buildCombo, nameOnlyCheckBox, searchTextField)) {
               ViewId program = (ViewId) programCombo.getValue();
               ViewId build = (ViewId) buildCombo.getValue();
               boolean nameOnly = nameOnlyCheckBox.toString().equalsIgnoreCase("true");
               String searchPhrase = (String) searchTextField.getValue();
               AtsSearchParameters params = new AtsSearchParameters(searchPhrase, nameOnly, build, program);
               getPresenter().selectSearch(getRequestedDataId(), params, getNavigator());
            } else {
               ComponentUtility.logWarn("AtsSearchHeaderComponent.selectSearch - WARNING: null value detected.", this);
            }
         } else if (searchButton.getCaption().equals(SEARCHBUTTON_CANCEL)) {
            getPresenter().selectCancel();
         }
      }
   }

   private void startPolling() {
      progressIndicator.setVisible(true);
   }

   private void stopPolling() {
      progressIndicator.setVisible(false);
   }

   public void createLayout() {
      setWidth(100, UNITS_PERCENTAGE);
      setStyleName(CssConstants.OSEE_SEARCH_HEADER_COMPONENT_SMALL);

      HorizontalLayout hLayout_ProgBuildName = new HorizontalLayout();
      HorizontalLayout hLayout_SearchTextBtn = new HorizontalLayout();
      hLayout_ProgBuildName.setSizeUndefined();
      hLayout_SearchTextBtn.setSizeUndefined();

      Label hSpacer_ProgBuild = new Label("");
      hSpacer_ProgBuild.setHeight(null);
      hSpacer_ProgBuild.setWidth(30, UNITS_PIXELS);
      Label hSpacer_BuildName = new Label("");
      hSpacer_BuildName.setHeight(null);
      hSpacer_BuildName.setWidth(30, UNITS_PIXELS);

      searchTextField.setStyleName(CssConstants.OSEE_SEARCH_TEXTFIELD);

      Label hSpacer_SearchTextBtn = new Label("");
      hSpacer_SearchTextBtn.setHeight(null);
      hSpacer_SearchTextBtn.setWidth(30, UNITS_PIXELS);
      searchButton.addListener(new Button.ClickListener() {
         @Override
         public void buttonClick(Button.ClickEvent event) {
            String msg = "";
            for (int i = 0; i < 1000; i++) {
               msg += this.toString();
               msg += "\n";
            }
            selectSearch();
         }
      });

      OseeLogoLink oseeLogoImg =
         new OseeLogoLink(getNavigator(), CssConstants.OSEE_TITLE_MEDIUM_TEXT, AtsSearchResultsView.class);
      Label hSpacer_LogoRight = new Label("");
      oseeLogoImg.setSizeUndefined();

      hSpacer_LogoRight.setWidth(15, UNITS_PIXELS);

      VerticalLayout vLayout_SearchCrit = new VerticalLayout();
      vLayout_SearchCrit.setSizeUndefined();

      OseeLeftMarginContainer leftMarginContainer = new OseeLeftMarginContainer();

      HorizontalLayout hLayout_SearchText = new HorizontalLayout();
      searchTextPanel.setScrollable(false);
      searchTextPanel.addActionHandler(this);
      searchTextPanel.setContent(hLayout_SearchText);
      progressIndicator.setPollingInterval(500);
      progressIndicator.setStyleName("invisible");
      workingLabel.setVisible(false);
      stopPolling();

      hLayout_SearchText.addComponent(searchTextField);

      hLayout_ProgBuildName.addComponent(programCombo);
      hLayout_ProgBuildName.addComponent(hSpacer_ProgBuild);
      hLayout_ProgBuildName.addComponent(buildCombo);
      hLayout_ProgBuildName.addComponent(hSpacer_BuildName);
      hLayout_ProgBuildName.addComponent(nameOnlyCheckBox);
      hLayout_ProgBuildName.addComponent(workingLabel);

      hLayout_SearchTextBtn.addComponent(searchTextPanel);
      hLayout_SearchTextBtn.addComponent(hSpacer_SearchTextBtn);
      hLayout_SearchTextBtn.addComponent(searchButton);
      hLayout_SearchTextBtn.addComponent(progressIndicator);

      vLayout_SearchCrit.addComponent(hLayout_ProgBuildName);
      vLayout_SearchCrit.addComponent(hLayout_SearchTextBtn);

      leftMarginContainer.addComponent(oseeLogoImg);
      leftMarginContainer.addComponent(hSpacer_LogoRight);
      leftMarginContainer.addComponent(vLayout_SearchCrit);

      setCompositionRoot(leftMarginContainer);

      hLayout_ProgBuildName.setComponentAlignment(programCombo, Alignment.MIDDLE_LEFT);
      hLayout_ProgBuildName.setComponentAlignment(buildCombo, Alignment.MIDDLE_CENTER);
      hLayout_ProgBuildName.setComponentAlignment(nameOnlyCheckBox, Alignment.BOTTOM_RIGHT);

      hLayout_SearchTextBtn.setComponentAlignment(searchButton, Alignment.MIDDLE_RIGHT);
   }

   @Override
   public void addProgram(ViewId program) {
      if (ComponentUtility.isAccessible(programCombo)) {
         lockProgramCombo = true;
         programCombo.addItem(program);
         lockProgramCombo = false;
      }
   }

   @Override
   public void clearBuilds() {
      if (ComponentUtility.isAccessible(buildCombo) && !lockProgramCombo) {
         buildCombo.removeAllItems();
      }
   }

   @Override
   public void addBuild(ViewId build) {
      if (ComponentUtility.isAccessible(buildCombo)) {
         buildCombo.addItem(build);
      }
   }

   @Override
   public void setSearchCriteria(AtsSearchParameters params) {
      if (params != null) {
         if (ComponentUtility.isAccessible(programCombo)) {
            //            lockProgramCombo = true;
            programCombo.setValue(params.getProgram());
            //            lockProgramCombo = false;
         }
         if (ComponentUtility.isAccessible(buildCombo)) {
            buildCombo.setValue(params.getBuild());
         }
         if (ComponentUtility.isAccessible(nameOnlyCheckBox)) {
            nameOnlyCheckBox.setValue(params.isNameOnly());
         }
         if (ComponentUtility.isAccessible(searchTextField)) {
            searchTextField.setValue(params.getSearchString());
         }
      } else {
         ComponentUtility.logWarn("AtsSearchHeaderComponent.setSearchCriteria - WARNING: null value detected.", this);
      }
   }

   @Override
   public void clearAll() {
      if (ComponentUtility.isAccessible(programCombo)) {
         //         programCombo.removeAllItems();
         programCombo.setValue(null);
      }
      if (ComponentUtility.isAccessible(buildCombo)) {
         //         buildCombo.removeAllItems();
         buildCombo.setValue(null);
      }
      if (ComponentUtility.isAccessible(nameOnlyCheckBox)) {
         nameOnlyCheckBox.setValue(true);
      }
      if (ComponentUtility.isAccessible(searchTextField)) {
         searchTextField.setValue("");
      }
   }

   @Override
   public void setErrorMessage(String shortMsg, String longMsg, MsgType msgType) {
      OseeExceptionDialogComponent dlg =
         new OseeExceptionDialogComponent(msgType, shortMsg, longMsg, getApplication().getMainWindow());
   }

   //TODO: None of this works because Vaadin only supports key actions for Windows and Panel Objects. (this is
   // a Component)
   private final Action action_enter = new ShortcutAction("Enter key", ShortcutAction.KeyCode.ENTER, null);
   private final Action[] actions = new Action[] {action_enter};

   @Override
   public Action[] getActions(Object target, Object sender) {
      return actions;
   }

   @Override
   public void handleAction(Action action, Object sender, Object target) {
      if (sender == searchTextPanel && action == action_enter) {
         selectSearch();
      }
   }

   @SuppressWarnings("unchecked")
   @Override
   public AtsUiApplication<AtsSearchHeaderComponent, AtsSearchParameters> getApplication() {
      return (AtsUiApplication<AtsSearchHeaderComponent, AtsSearchParameters>) super.getApplication();
   }

   private AtsNavigator getNavigator() {
      return getApplication().getNavigator();
   }

   private AtsSearchPresenter<AtsSearchHeaderComponent, AtsSearchParameters> getPresenter() {
      return getApplication().getPresenter();
   }

   private String getRequestedDataId() {
      return getApplication().getUrl();
   }

   @Override
   public String getViewTitle() {
      return (String) this.searchTextField.getValue();
   }

   @Override
   public void searchInProgress() {
      programCombo.setEnabled(false);
      buildCombo.setEnabled(false);
      nameOnlyCheckBox.setEnabled(false);
      searchTextField.setEnabled(false);
      searchButton.setCaption(SEARCHBUTTON_CANCEL);
      isSearchInProgress = true;
      startPolling();

      Thread thread = new Thread(new Runnable() {
         private int i = 0;

         @Override
         public void run() {
            workingLabel.setVisible(true);
            while (isSearchInProgress) {
               if (i == 0) {
                  workingLabel.setStyleName(CssConstants.OSEE_WORKING_LABEL_1);
                  workingLabel.setValue("Working.");
                  i++;
               } else if (i == 1) {
                  workingLabel.setStyleName(CssConstants.OSEE_WORKING_LABEL_2);
                  workingLabel.setValue("Working..");
                  i++;
               } else {
                  workingLabel.setStyleName(CssConstants.OSEE_WORKING_LABEL_3);
                  workingLabel.setValue("Working...");
                  i = 0;
               }
               try {
                  Thread.sleep(500);
               } catch (InterruptedException ex) {
                  ComponentUtility.logWarn(
                     "AtsSearchHeaderComponent.searchInProgress.Runnable.run - WARNING: InterruptedException.",
                     AtsSearchHeaderImpl.this);
                  break;
               }
            }
            workingLabel.setVisible(false);
            stopPolling();
         }
      }, "thread_working_label");
      thread.start();
   }

   @Override
   public void searchCancelled() {
      programCombo.setEnabled(true);
      buildCombo.setEnabled(true);
      nameOnlyCheckBox.setEnabled(true);
      searchTextField.setEnabled(true);
      validateSearchAndEnableSearchButton();
      searchButton.setCaption(SEARCHBUTTON_SEARCH);
      Application application = getApplication();
      if (application != null) {
         application.getMainWindow().showNotification("Search Cancelled", Notification.TYPE_TRAY_NOTIFICATION);
      }
      isSearchInProgress = false;
   }

   @Override
   public void searchCompleted() {
      programCombo.setEnabled(true);
      buildCombo.setEnabled(true);
      nameOnlyCheckBox.setEnabled(true);
      searchTextField.setEnabled(true);
      validateSearchAndEnableSearchButton();
      searchButton.setCaption(SEARCHBUTTON_SEARCH);
      isSearchInProgress = false;
   }
}

Back to the top