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

org.opentcs.operationsdesk.application.action.actions.FindVehicleAction 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.application.action.actions;

import static java.util.Objects.requireNonNull;
import static javax.swing.Action.LARGE_ICON_KEY;
import static javax.swing.Action.MNEMONIC_KEY;
import static javax.swing.Action.SMALL_ICON;
import static org.opentcs.operationsdesk.util.I18nPlantOverviewOperating.MENU_PATH;

import jakarta.inject.Inject;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import org.opentcs.customizations.plantoverview.ApplicationFrame;
import org.opentcs.guing.base.model.elements.VehicleModel;
import org.opentcs.guing.common.components.dialogs.ClosableDialog;
import org.opentcs.guing.common.components.drawing.OpenTCSDrawingEditor;
import org.opentcs.guing.common.persistence.ModelManager;
import org.opentcs.guing.common.util.ImageDirectory;
import org.opentcs.operationsdesk.components.dialogs.FindVehiclePanel;
import org.opentcs.operationsdesk.components.dialogs.FindVehiclePanelFactory;
import org.opentcs.operationsdesk.util.I18nPlantOverviewOperating;
import org.opentcs.thirdparty.guing.common.jhotdraw.util.ResourceBundleUtil;

/**
 * An action to find a vehicle on the drawing.
 */
public class FindVehicleAction
    extends
      AbstractAction {

  /**
   * This action's ID.
   */
  public static final String ID = "actions.findVehicle";

  private static final ResourceBundleUtil BUNDLE = ResourceBundleUtil.getBundle(MENU_PATH);
  /**
   * Provides the current system model.
   */
  private final ModelManager modelManager;
  /**
   * The drawing editor.
   */
  private final OpenTCSDrawingEditor drawingEditor;
  /**
   * The parent component for dialogs shown by this action.
   */
  private final Component dialogParent;
  /**
   * The panel factory.
   */
  private final FindVehiclePanelFactory panelFactory;

  /**
   * Creates a new instance.
   *
   * @param modelManager Provides the current system model.
   * @param drawingEditor The drawing editor.
   * @param dialogParent The parent component for dialogs shown by this action.
   * @param panelFactory The panel factory.
   */
  @Inject
  @SuppressWarnings("this-escape")
  public FindVehicleAction(
      ModelManager modelManager,
      OpenTCSDrawingEditor drawingEditor,
      @ApplicationFrame
      Component dialogParent,
      FindVehiclePanelFactory panelFactory
  ) {
    this.modelManager = requireNonNull(modelManager, "modelManager");
    this.drawingEditor = requireNonNull(drawingEditor, "drawingEditor");
    this.dialogParent = requireNonNull(dialogParent, "dialogParent");
    this.panelFactory = requireNonNull(panelFactory, "panelFactory");

    putValue(NAME, BUNDLE.getString("findVehicleAction.name"));
    putValue(MNEMONIC_KEY, Integer.valueOf('F'));

    ImageIcon icon = ImageDirectory.getImageIcon("/toolbar/find-vehicle.22.png");
    putValue(SMALL_ICON, icon);
    putValue(LARGE_ICON_KEY, icon);
  }

  @Override
  public void actionPerformed(ActionEvent evt) {
    findVehicle();
  }

  private void findVehicle() {
    List vehicles = new ArrayList<>(modelManager.getModel().getVehicleModels());
    if (vehicles.isEmpty()) {
      return;
    }

    Collections.sort(vehicles, Comparator.comparing(VehicleModel::getName));
    FindVehiclePanel content = panelFactory.createFindVehiclesPanel(
        vehicles,
        drawingEditor.getActiveView()
    );
    String title = ResourceBundleUtil.getBundle(I18nPlantOverviewOperating.FINDVEHICLE_PATH)
        .getString("findVehicleAction.dialog_findVehicle.title");
    ClosableDialog dialog = new ClosableDialog(dialogParent, true, content, title);
    dialog.setLocationRelativeTo(dialogParent);
    dialog.setVisible(true);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy