All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentcs.operationsdesk.transport.LocationActionPanel Maven / Gradle / Ivy

There is a newer version: 6.2.0
Show newest version
/**
 * Copyright (c) The openTCS Authors.
 *
 * This program is free software and subject to the MIT license. (For details,
 * see the licensing information (LICENSE.txt) you should have received with
 * this copy of the software.)
 */
package org.opentcs.operationsdesk.transport;

import static java.util.Objects.requireNonNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;
import java.util.stream.Collectors;
import javax.swing.DefaultComboBoxModel;
import org.opentcs.data.order.DriveOrder;
import org.opentcs.guing.base.model.ModelComponent;
import org.opentcs.guing.base.model.elements.LocationModel;
import org.opentcs.guing.base.model.elements.LocationTypeModel;
import org.opentcs.guing.common.components.dialogs.DialogContent;
import org.opentcs.operationsdesk.util.I18nPlantOverviewOperating;
import org.opentcs.thirdparty.guing.common.jhotdraw.util.ResourceBundleUtil;

/**
 * A UI to select a point or location as a destination for a vehicle.
 */
public class LocationActionPanel
    extends
      DialogContent {

  /**
   * Available locations.
   */
  protected List fLocations;
  /**
   * Available actions.
   */
  protected List fActions;

  /**
   * Creates new instance.
   *
   * @param locations The location models.
   */
  @SuppressWarnings("this-escape")
  public LocationActionPanel(List locations) {
    this.fLocations = requireNonNull(locations, "locations");

    initComponents();

    setDialogTitle(
        ResourceBundleUtil.getBundle(I18nPlantOverviewOperating.VEHICLEPOPUP_PATH)
            .getString("locationActionPanel.title")
    );

    Collections.sort(fLocations, getComparator());

    List names = fLocations.stream()
        .map(location -> location.getName())
        .collect(Collectors.toList());

    locationsComboBox.setModel(new DefaultComboBoxModel<>(new Vector<>(names)));

    updateActions();
  }

  /**
   * Returns a Comparator to sort model components by their name.
   *
   * @return The Comparator
   */
  protected final Comparator getComparator() {
    return (ModelComponent item1, ModelComponent item2) -> {
      String s1 = item1.getName();
      String s2 = item2.getName();
      s1 = s1.toLowerCase();
      s2 = s2.toLowerCase();

      return s1.compareTo(s2);
    };
  }

  /**
   * Returns the selected location.
   *
   * @return the selected location or null.
   */
  public LocationModel getSelectedLocation() {
    int index = locationsComboBox.getSelectedIndex();

    if (index == -1) {
      return null;
    }

    return fLocations.get(index);
  }

  public String getSelectedAction() {
    int index = actionsComboBox.getSelectedIndex();

    if (index == -1) {
      return null;
    }

    return fActions.get(index);
  }

  private void updateActions() {
    LocationModel selectedLocation = getSelectedLocation();
    LocationTypeModel locationType = selectedLocation.getLocationType();
    List actions = locationType.getPropertyAllowedOperations().getItems();
    Collections.sort(actions);
    fActions = new ArrayList<>();
    fActions.add(DriveOrder.Destination.OP_NOP);
    fActions.addAll(actions);
    actionsComboBox.setModel(new DefaultComboBoxModel<>(new Vector<>(fActions)));
  }

  @Override
  public void update() {
  }

  @Override
  public void initFields() {
  }

  // FORMATTER:OFF
  // CHECKSTYLE:OFF
  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  // //GEN-BEGIN:initComponents
  private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    locationsLabel = new javax.swing.JLabel();
    locationsComboBox = new javax.swing.JComboBox<>();
    actionsLabel = new javax.swing.JLabel();
    actionsComboBox = new javax.swing.JComboBox<>();

    setPreferredSize(new java.awt.Dimension(150, 40));
    setLayout(new java.awt.GridBagLayout());

    locationsLabel.setFont(locationsLabel.getFont());
    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("i18n/org/opentcs/plantoverview/operating/dialogs/vehiclePopup"); // NOI18N
    locationsLabel.setText(bundle.getString("locationActionPanel.label_location.text")); // NOI18N
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
    add(locationsLabel, gridBagConstraints);

    locationsComboBox.setFont(locationsComboBox.getFont());
    locationsComboBox.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(java.awt.event.ItemEvent evt) {
        locationsComboBoxItemStateChanged(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.weightx = 0.5;
    add(locationsComboBox, gridBagConstraints);

    actionsLabel.setFont(actionsLabel.getFont());
    actionsLabel.setText(bundle.getString("locationActionPanel.label_action.text")); // NOI18N
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
    add(actionsLabel, gridBagConstraints);

    actionsComboBox.setFont(actionsComboBox.getFont());
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    add(actionsComboBox, gridBagConstraints);
  }// //GEN-END:initComponents
  // CHECKSTYLE:ON
  // FORMATTER:ON

  private void locationsComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_locationsComboBoxItemStateChanged
    updateActions();
  }//GEN-LAST:event_locationsComboBoxItemStateChanged

  // FORMATTER:OFF
  // CHECKSTYLE:OFF
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JComboBox actionsComboBox;
  private javax.swing.JLabel actionsLabel;
  private javax.swing.JComboBox locationsComboBox;
  private javax.swing.JLabel locationsLabel;
  // End of variables declaration//GEN-END:variables
  // CHECKSTYLE:ON
  // FORMATTER:ON
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy