gt:         if (selectedJob != null) {
context: /*
   * ProActive Parallel Suite(TM):
   * The Open Source library for parallel and distributed
   * Workflows & Scheduling, Orchestration, Cloud Automation
   * and Big Data Analysis on Enterprise Grids & Clouds.
   *
   * Copyright (c) 2007 - 2017 ActiveEon
   * Contact: contact@activeeon.com
   *
   * This library is free software: you can redistribute it and/or
   * modify it under the terms of the GNU Affero General Public License
   * as published by the Free Software Foundation: version 3 of
   * the License.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU Affero General Public License for more details.
   *
   * You should have received a copy of the GNU Affero General Public License
   * along with this program. If not, see <http://www.gnu.org/licenses/>.
   *
   * If needed, contact us to obtain a release under GPL Version 2 or 3
   * or a different license than the AGPL.
   */
  package org.ow2.proactive_grid_cloud_portal.scheduler.client.view;
  
  import static org.ow2.proactive_grid_cloud_portal.scheduler.client.view.ResultView.DESTINATION_FIELD_NAME;
  import static org.ow2.proactive_grid_cloud_portal.scheduler.client.view.ResultView.JOB_ID_FIELD_NAME;
  import static org.ow2.proactive_grid_cloud_portal.scheduler.client.view.ResultView.SESSION_ID_FIELD_NAME;
  import static org.ow2.proactive_grid_cloud_portal.scheduler.client.view.ResultView.TASK_ID_FIELD_NAME;
  
  import java.util.List;
  
  import org.ow2.proactive_grid_cloud_portal.common.client.model.LoginModel;
  import org.ow2.proactive_grid_cloud_portal.scheduler.client.Job;
  import org.ow2.proactive_grid_cloud_portal.scheduler.client.JobStatus;
  import org.ow2.proactive_grid_cloud_portal.scheduler.client.SchedulerController;
  import org.ow2.proactive_grid_cloud_portal.scheduler.client.SchedulerListeners.JobSelectedListener;
  import org.ow2.proactive_grid_cloud_portal.scheduler.client.SchedulerListeners.TaskResultListener;
  import org.ow2.proactive_grid_cloud_portal.scheduler.client.SchedulerModelImpl;
  import org.ow2.proactive_grid_cloud_portal.scheduler.client.model.ExecutionsModel;
  import org.ow2.proactive_grid_cloud_portal.scheduler.client.model.JobsModel;
  import org.ow2.proactive_grid_cloud_portal.scheduler.client.view.grid.KeyValueGrid;
  
  import com.google.gwt.core.client.GWT;
  import com.smartgwt.client.types.Alignment;
  import com.smartgwt.client.types.FormMethod;
  import com.smartgwt.client.types.VerticalAlignment;
  import com.smartgwt.client.widgets.IButton;
  import com.smartgwt.client.widgets.Label;
  import com.smartgwt.client.widgets.form.DynamicForm;
  import com.smartgwt.client.widgets.form.fields.HiddenItem;
  import com.smartgwt.client.widgets.layout.HLayout;
  import com.smartgwt.client.widgets.layout.Layout;
  import com.smartgwt.client.widgets.layout.VLayout;
  
  
  public class JobResultView implements JobSelectedListener, TaskResultListener {
  
      private static final String NO_JOB_SELECED = "No job selected.";
  
      public static final String NOT_AUTHORIZED = "You are not authorized to see this job's result";
  
      private static final String RESULT_LIST_NO_ITEMS = "<b>Result List: no items to show.</b>";
  
      private static final String RESULT_LIST = "<b>Result List:</b>";
  
      private static final String RESULT_LIST_NOT_FINISHED = "<b>Result List (Partial, for all finished Tasks):</b>";
  
      private static final String RESULT_MAP = "Result Map";
  
      private static final String RESULT_MAP_NOT_FINISHED = "Result Map (Partial, from all finished Tasks):";
  
      /**
       * label when no job is selected or job is not finished
       */
      private Label placeHolderLabel;
  
      private Label preciousResultLabel;
  
      private KeyValueGrid resultMap;
  
      private SchedulerController controller;
  
      private VLayout preciousButtons;
  
      private DynamicForm downloadForm;
  
      private Job selectedJob;
  
      public JobResultView(SchedulerController controller) {
          this.controller = controller;
          ExecutionsModel executionModel = ((SchedulerModelImpl) controller.getModel()).getExecutionsModel();
          JobsModel jobsModel = executionModel.getJobsModel();
          jobsModel.addJobSelectedListener(this);
          jobsModel.addTaskResultListener(this);
      }
  
      /**
       * @return the Widget to display, ready to be added in a container
       */
      public Layout build() {
          Layout root = new VLayout();
          root.setWidth100();
          root.setHeight100();
          root.setPadding(20);
          root.setMembersMargin(5);
  
          placeHolderLabel = new Label(NO_JOB_SELECED);
          placeHolderLabel.setWidth100();
          placeHolderLabel.setAlign(Alignment.CENTER);
          root.addMember(placeHolderLabel);
  
          preciousResultLabel = new Label(RESULT_LIST);
          preciousResultLabel.setAutoHeight();
          preciousResultLabel.setWidth100();
          preciousResultLabel.hide();
          root.addMember(preciousResultLabel);
  
          HiddenItem sess = new HiddenItem(SESSION_ID_FIELD_NAME);
          sess.setValue(LoginModel.getInstance().getSessionId());
  
          HiddenItem job = new HiddenItem(JOB_ID_FIELD_NAME);
          HiddenItem destination = new HiddenItem(DESTINATION_FIELD_NAME);
          HiddenItem task = new HiddenItem(TASK_ID_FIELD_NAME);
  
          downloadForm = new DynamicForm();
          downloadForm.setWidth100();
          downloadForm.setAutoHeight();
          downloadForm.setMethod(FormMethod.POST);
          downloadForm.setFields(sess, job, destination, task);
          downloadForm.setAction(GWT.getModuleBaseURL() + "downloader");
  
          root.addMember(downloadForm);
  
          preciousButtons = new VLayout();
          preciousButtons.setMembersMargin(10);
          preciousButtons.setWidth100();
          preciousButtons.setAutoHeight();
          root.addMember(preciousButtons);
  
          resultMap = new KeyValueGrid(RESULT_MAP);
          resultMap.setWidth100();
          resultMap.setAutoHeight();
          resultMap.hide();
          root.addMember(resultMap);
  
          showNoJobSelected();
          return root;
      }
  
      @Override
      public void jobSelected(Job job) {
          selectedJob = job;
          showJobSelected(job);
      }
  
      private boolean isFinished(Job job) {
          return job.getStatus().equals(JobStatus.FINISHED) || job.getStatus().equals(JobStatus.FAILED) ||
                 job.getStatus().equals(JobStatus.KILLED) || job.getStatus().equals(JobStatus.CANCELED);
      }
  
      @Override
      public void jobUnselected() {
          showNoJobSelected();
      }
  
      @Override
      public void selectedJobUpdated(Job job) {
          selectedJob = job;
          showJobSelected(job);
      }
  
      private void showNoJobSelected() {
          selectedJob = null;
          placeHolderLabel.setContents(NO_JOB_SELECED);
          placeHolderLabel.show();
          resultMap.hide();
          preciousButtons.hide();
          preciousResultLabel.hide();
  
      }
  
      @Override
      public void preciousTaskNamesLoaded(List<String> preciousTaskNames) {
          HLayout[] iButtons = preciousTaskNames.stream().map(preciousTask -> {
              HLayout row = new HLayout();
              row.setWidth100();
              row.setMargin(1);
              row.setPadding(1);
              row.setAutoHeight();
              row.setMembersMargin(10);
  
              Label label = new Label(preciousTask);
              label.setWidth(100);
              label.setHeight(22);
              label.setValign(VerticalAlignment.CENTER);
              row.addMember(label);
  
              IButton openInBrowser = new IButton("Open in browser");
              openInBrowser.setWidth(200);
              openInBrowser.setHeight(22);
  
              openInBrowser.addClickHandler(event -> doDownload(preciousTask, downloadForm, "browser", "_blank"));
              row.addMember(openInBrowser);
  
              IButton saveAsFile = new IButton("Save as file");
              saveAsFile.setHeight(22);
              saveAsFile.setWidth(200);
              saveAsFile.addClickHandler(event -> doDownload(preciousTask, downloadForm, "file", "_top"));
              row.addMember(saveAsFile);
  
              return row;
          }).toArray(HLayout[]::new);
          preciousButtons.setMembers(iButtons);
  
          placeHolderLabel.hide();
          resultMap.buildEntries(selectedJob.getResultMap());
          resultMap.show();
          if (isFinished(selectedJob)) {
              if (preciousTaskNames.isEmpty()) {
                  preciousResultLabel.setContents(RESULT_LIST_NO_ITEMS);
              } else {
                  preciousResultLabel.setContents(RESULT_LIST);
              }
              resultMap.setLabel(RESULT_MAP);
          } else {
              preciousResultLabel.setContents(RESULT_LIST_NOT_FINISHED);
              resultMap.setLabel(RESULT_MAP_NOT_FINISHED);
          }
          preciousResultLabel.show();
          preciousButtons.show();
      }
  
      private void showJobSelected(Job job) {
          selectedJob = job;
          placeHolderLabel.setContents("Loading...");
          placeHolderLabel.show();
          resultMap.hide();
          preciousButtons.hide();
          preciousResultLabel.hide();
  
          controller.getExecutionController().getJobsController().fetchMetadataOfPreciousResults();
      }
  
      @Override
      public void preciousTaskNamesNotAuthorized() {
          placeHolderLabel.setContents(NOT_AUTHORIZED);
          placeHolderLabel.show();
          resultMap.hide();
          resultMap.hide();
          preciousButtons.hide();
          preciousResultLabel.hide();
      }
  
      private void doDownload(String readableName, DynamicForm form, String contentType, String target) {
  