/**
   * Create a panel to define trade route cargos.
   *
   * @param freeColClient The <code>FreeColClient</code> for the game.
   * @param newRoute The <code>TradeRoute</code> to operate on.
   */
  public TradeRouteInputPanel(FreeColClient freeColClient, TradeRoute newRoute) {
    super(freeColClient, new MigLayout("wrap 4, fill", "[]20[fill]rel"));

    final Game game = freeColClient.getGame();
    final Player player = getMyPlayer();
    final TradeRoute tradeRoute = newRoute.copy(game, TradeRoute.class);

    this.newRoute = newRoute;
    this.cargoHandler = new CargoHandler();
    this.dragListener = new DragListener(freeColClient, this);
    this.dropListener = new DropListener();

    this.stopListModel = new DefaultListModel<>();
    for (TradeRouteStop stop : tradeRoute.getStops()) {
      this.stopListModel.addElement(stop);
    }

    this.stopList = new JList<>(this.stopListModel);
    this.stopList.setCellRenderer(new StopRenderer());
    this.stopList.setFixedCellHeight(48);
    this.stopList.setDragEnabled(true);
    this.stopList.setTransferHandler(new StopListHandler());
    this.stopList.addKeyListener(
        new KeyListener() {

          @Override
          public void keyTyped(KeyEvent e) {
            if (e.getKeyChar() == KeyEvent.VK_DELETE) {
              deleteCurrentlySelectedStops();
            }
          }

          @Override
          public void keyPressed(KeyEvent e) {} // Ignore

          @Override
          public void keyReleased(KeyEvent e) {} // Ignore
        });
    this.stopList.addListSelectionListener(this);
    JScrollPane tradeRouteView = new JScrollPane(stopList);

    JLabel nameLabel = Utility.localizedLabel("tradeRouteInputPanel.nameLabel");
    this.tradeRouteName = new JTextField(tradeRoute.getName());

    JLabel destinationLabel = Utility.localizedLabel("tradeRouteInputPanel.destinationLabel");
    this.destinationSelector = new JComboBox<>();
    this.destinationSelector.setRenderer(new DestinationCellRenderer());
    StringTemplate template = StringTemplate.template("tradeRouteInputPanel.allColonies");
    this.destinationSelector.addItem(Messages.message(template));
    if (player.getEurope() != null) {
      this.destinationSelector.addItem(player.getEurope().getId());
    }
    for (Colony colony : freeColClient.getMySortedColonies()) {
      this.destinationSelector.addItem(colony.getId());
    }

    this.messagesBox = new JCheckBox(Messages.message("tradeRouteInputPanel.silence"));
    this.messagesBox.setSelected(tradeRoute.isSilent());
    this.messagesBox.addActionListener(
        (ActionEvent ae) -> {
          tradeRoute.setSilent(messagesBox.isSelected());
        });

    this.addStopButton = Utility.localizedButton("tradeRouteInputPanel.addStop");
    this.addStopButton.addActionListener(
        (ActionEvent ae) -> {
          addSelectedStops();
        });

    this.removeStopButton = Utility.localizedButton("tradeRouteInputPanel.removeStop");
    this.removeStopButton.addActionListener(
        (ActionEvent ae) -> {
          deleteCurrentlySelectedStops();
        });

    this.goodsPanel = new GoodsPanel();
    this.goodsPanel.setTransferHandler(this.cargoHandler);
    this.goodsPanel.setEnabled(false);
    this.cargoPanel = new CargoPanel();
    this.cargoPanel.setTransferHandler(this.cargoHandler);

    JButton cancelButton = Utility.localizedButton("cancel");
    cancelButton.setActionCommand(CANCEL);
    cancelButton.addActionListener(this);
    setCancelComponent(cancelButton);

    add(Utility.localizedHeader("tradeRouteInputPanel.editRoute", false), "span, align center");
    add(tradeRouteView, "span 1 5, grow");
    add(nameLabel);
    add(this.tradeRouteName, "span");
    add(destinationLabel);
    add(this.destinationSelector, "span");
    add(this.messagesBox);
    add(this.addStopButton);
    add(this.removeStopButton, "span");
    add(this.goodsPanel, "span");
    add(this.cargoPanel, "span, height 80:, growy");
    add(okButton, "newline 20, span, split 2, tag ok");
    add(cancelButton, "tag cancel");

    // update cargo panel if stop is selected
    if (this.stopListModel.getSize() > 0) {
      this.stopList.setSelectedIndex(0);
      TradeRouteStop selectedStop = this.stopListModel.firstElement();
      this.cargoPanel.initialize(selectedStop);
    }

    // update buttons according to selection
    updateButtons();

    getGUI().restoreSavedSize(this, getPreferredSize());
  }
Esempio n. 2
0
  /** The constructor that will add the items to this panel. */
  public ReportLabourPanel(FreeColClient freeColClient) {
    super(freeColClient, "reportLabourAction");

    this.data = new HashMap<>();
    this.unitCount = new TypeCountMap<>();
    for (Unit unit : getMyPlayer().getUnits()) {
      UnitType type = unit.getType();
      this.unitCount.incrementCount(type, 1);
      Map<Location, Integer> unitMap = this.data.get(type);
      if (unitMap == null) {
        unitMap = new HashMap<>();
        this.data.put(type, unitMap);
      }

      Location location = unit.getLocation();
      if (location == null) {
        logger.warning("Unit has null location: " + unit);
      } else if (location.getSettlement() != null) {
        location = location.getSettlement();
      } else if (unit.isInEurope()) {
        location = getMyPlayer().getEurope();
      } else if (location.getTile() != null) {
        location = location.getTile();
      }
      Integer count = unitMap.get(location);
      if (count == null) {
        unitMap.put(location, 1);
      } else {
        unitMap.put(location, count + 1);
      }
    }

    this.colonies = freeColClient.getMySortedColonies();

    DefaultListModel<LabourUnitPanel> model = new DefaultListModel<>();
    for (UnitType unitType : getSpecification().getUnitTypeList()) {
      if (unitType.isPerson() && unitType.isAvailableTo(getMyPlayer())) {
        int count = this.unitCount.getCount(unitType);
        model.addElement(new LabourUnitPanel(unitType, count));
      }
    }
    Action selectAction =
        new AbstractAction() {
          @Override
          public void actionPerformed(ActionEvent e) {
            showDetails();
          }
        };
    Action quitAction =
        new AbstractAction() {
          @Override
          public void actionPerformed(ActionEvent e) {
            getGUI().removeFromCanvas(ReportLabourPanel.this);
          }
        };

    // Add all the components
    this.panelList = new JList<>(model);
    this.panelList.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "select");
    this.panelList.getActionMap().put("select", selectAction);
    this.panelList.getInputMap().put(KeyStroke.getKeyStroke("ESCAPE"), "quit");
    this.panelList.getActionMap().put("quit", quitAction);
    this.panelList.addMouseListener(
        new MouseAdapter() {
          @Override
          public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
              showDetails();
            }
          }
        });
    this.panelList.setOpaque(false);
    this.panelList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    this.panelList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    this.panelList.setVisibleRowCount(-1);
    this.panelList.setCellRenderer(new LabourUnitPanelRenderer());

    this.scrollPane.setViewportView(this.panelList);
  }