private void hideCommandComponents() {
   commandLabel.getParent().remove(commandLabel);
   commandComboBox.getParent().remove(commandComboBox);
   argumentLabel.getParent().remove(argumentLabel);
   argumentScrollPane.getParent().remove(argumentScrollPane);
   argumentButtonPanel.getParent().remove(argumentButtonPanel);
   this.pack();
 }
 private void setIcon(Icon i) {
   if (icon.getParent() != null) {
     if (i != null && i.equals(icon.getIcon())) {
       return;
     }
     remove(icon);
   }
   if (i != null) {
     icon = new LinkLabel("", i, this, null);
     add(icon, 0);
   }
 }
示例#3
0
  @Override
  public void paintComponent(Graphics g) {
    final JLabel lbl = CHomeUI.SINGLETON_INSTANCE.getLblSelected();
    int yTop = (lbl.getY() + lbl.getParent().getY());

    // super.paintComponent(g);
    final Graphics2D g2d = (Graphics2D) g.create();
    final int w = getWidth();
    final int h = getHeight();

    FSkin.setGraphicsColor(g2d, d20);

    // Selected in this group, don't draw background under selected label.
    if (getY() < yTop && yTop < getY() + h) {
      g2d.fillRect(0, 0, w, lbl.getY());
      g2d.fillRect(0, lbl.getY() + lbl.getHeight(), w, h);

      FSkin.setGraphicsGradientPaint(g2d, w - 8, 0, l00, w, 0, d80);
      g2d.fillRect(w - 6, 0, w, lbl.getY());
      g2d.fillRect(w - 6, lbl.getY() + lbl.getHeight(), w, h);
    }
    // Selected not in this group; draw full background.
    else {
      g2d.fillRect(0, 0, w, h);

      FSkin.setGraphicsGradientPaint(g2d, w - 8, 0, l00, w, 0, d80);
      g2d.fillRect(w - 6, 0, w, h);
    }

    FSkin.setGraphicsColor(g2d, l10);
    g2d.drawLine(0, h - 1, w - 1, h - 1);

    FSkin.setGraphicsColor(g2d, d60);
    g2d.drawLine(0, 0, w - 1, 0);

    g2d.dispose();
  }
示例#4
0
  /**
   * Imports the data represented by the given Transferable into the given component. Returns 'true'
   * on success, 'false' otherwise.
   *
   * @param comp The component to import the data to.
   * @param t The Transferable that holds the data.
   * @return 'true' on success, 'false' otherwise.
   */
  public boolean importData(JComponent comp, Transferable t) {
    try {
      JLabel data;

      /*
        This variable is used to temporarily keep the old selected unit,
        while moving cargo from one carrier to another:
      */
      UnitLabel oldSelectedUnit = null;

      // Check flavor.
      if (t.isDataFlavorSupported(DefaultTransferHandler.flavor)) {
        data = (JLabel) t.getTransferData(DefaultTransferHandler.flavor);
      } else {
        logger.warning("Data flavor is not supported!");
        return false;
      }

      // Do not allow a transferable to be dropped upon itself:
      if (comp == data) {
        return false;
      }

      // Make sure we don't drop onto other Labels.
      if (comp instanceof UnitLabel) {
        UnitLabel unitLabel = (UnitLabel) comp;
        /**
         * If the unit/cargo is dropped on a carrier in port (EuropePanel.InPortPanel), then the
         * ship is selected and the unit is added to its cargo. If the unit is not a carrier, but
         * can be equipped, and the goods can be converted to equipment, equip the unit.
         *
         * <p>If not, assume that the user wished to drop the unit/cargo on the panel below.
         */
        if (unitLabel.getUnit().isCarrier()
            && unitLabel.getParent() instanceof InPortPanel
            && parentPanel instanceof PortPanel) {
          PortPanel portPanel = (PortPanel) parentPanel;
          if (data instanceof Draggable && ((Draggable) data).isOnCarrier()) {
            oldSelectedUnit = portPanel.getSelectedUnitLabel();
          }
          portPanel.setSelectedUnitLabel(unitLabel);
          comp = portPanel.getCargoPanel();
        } else if (unitLabel.canUnitBeEquipedWith(data)) {
          // don't do anything before partial amount has been checked
        } else {
          comp = getDropTarget(comp);
        }
      } else if (comp instanceof AbstractGoodsLabel) {
        comp = getDropTarget(comp);
      }

      // t is already in comp:
      if (data.getParent() == comp) {
        return false;
      }

      if (data instanceof UnitLabel) {

        // Check if the unit can be dragged to comp.

        Unit unit = ((UnitLabel) data).getUnit();
        if (comp instanceof DropTarget) {
          DropTarget target = (DropTarget) comp;
          if (!target.accepts(unit)) {
            return false;
          }

          if ((comp instanceof ColonyPanel.OutsideColonyPanel
                  || comp instanceof ColonyPanel.ColonyCargoPanel)
              && !gui.tryLeaveColony(unit)) {
            return false;
          }
          target.add(data, true);

          // Update unit selection

          // new unit selection has already been taken care
          // of if this unit was moved to ToAmericaPanel
          restoreSelection(oldSelectedUnit);
          comp.revalidate();

          return true;
        } else {
          return false;
        }
      } else if (data instanceof GoodsLabel) {
        // Check if the goods can be dragged to comp.
        GoodsLabel label = (GoodsLabel) data;
        Goods goods = label.getGoods();

        // Import the data.
        if (label.isPartialChosen()) {
          int defaultAmount = -1;
          if (goods.getLocation() instanceof GoodsLocation) {
            GoodsLocation loc = (GoodsLocation) goods.getLocation();
            if (goods.getAmount() > loc.getGoodsCapacity()) {
              // If over capacity, favour the amount that would
              // correct the problem.
              defaultAmount =
                  Math.min(goods.getAmount() - loc.getGoodsCapacity(), GoodsContainer.CARGO_SIZE);
            }
          }
          int amount = getAmount(goods.getType(), goods.getAmount(), defaultAmount, false);
          if (amount <= 0) return false;
          goods.setAmount(amount);
        } else if (goods.getAmount() > GoodsContainer.CARGO_SIZE) {
          goods.setAmount(GoodsContainer.CARGO_SIZE);
        }

        if (comp instanceof UnitLabel) {
          return equipUnitIfPossible((UnitLabel) comp, goods);
        } else if (comp instanceof JLabel) {
          logger.warning("Oops, I thought we didn't have to write this part.");
          return true;
        } else if (comp instanceof DropTarget) {
          DropTarget target = (DropTarget) comp;
          if (target.accepts(goods)) {
            target.add(data, true);
          } else {
            return false;
          }

          restoreSelection(oldSelectedUnit);

          comp.revalidate();

          return true;
        }
      } else if (data instanceof MarketLabel) {

        // Check if the unit can be dragged to comp.

        MarketLabel label = ((MarketLabel) data);

        // Import the data.

        if (label.isPartialChosen()) {
          int amount = getAmount(label.getType(), label.getAmount(), -1, true);
          if (amount <= 0) {
            return false;
          }
          label.setAmount(amount);
        }

        if (comp instanceof UnitLabel) {
          return equipUnitIfPossible((UnitLabel) comp, label.getGoods());
        } else if (comp instanceof JLabel) {
          logger.warning("Oops, I thought we didn't have to write this part.");
          return true;
        } else if (comp instanceof JPanel) {
          if (comp instanceof CargoPanel) {
            ((CargoPanel) comp).add(data, true);
          } else {
            logger.warning("The receiving component is of an invalid type.");
            return false;
          }

          comp.revalidate();
          return true;
        }
      }

      logger.warning("The dragged component is of an invalid type.");

    } catch (Exception e) { // TODO: Suggest a reconnect.
      logger.log(Level.WARNING, "Import data fail", e);
    }
    return false;
  }