示例#1
0
  @SuppressWarnings("unchecked") // FIXME in Java7
  private void updateUnitList() {
    final Specification spec = getSpecification();
    DefaultListModel units = (DefaultListModel) unitList.getModel();
    units.clear();
    loop:
    for (UnitType unitType : buildableUnits) {
      // compare colony.getNoBuildReason()
      List<String> lockReason = new ArrayList<String>();
      if (unbuildableTypes.contains(unitType)) {
        continue;
      }

      if (unitType.getRequiredPopulation() > unitCount) {
        lockReason.add(
            Messages.message(
                StringTemplate.template("colonyPanel.populationTooSmall")
                    .addAmount("%number%", unitType.getRequiredPopulation())));
      }

      if (unitType.getLimits() != null) {
        for (Limit limit : unitType.getLimits()) {
          if (!limit.evaluate(colony)) {
            lockReason.add(Messages.message(limit.getDescriptionKey()));
          }
        }
      }

      if (!(colony.hasAbility(Ability.BUILD, unitType, getGame().getTurn())
          || FeatureContainer.hasAbility(featureContainer, Ability.BUILD, unitType, null))) {
        boolean builderFound = false;
        for (Ability ability : spec.getAbilities(Ability.BUILD)) {
          if (ability.appliesTo(unitType)
              && ability.getValue()
              && ability.getSource() != null
              && !unbuildableTypes.contains(ability.getSource())) {
            builderFound = true;
            lockReason.add(Messages.getName(ability.getSource()));
            break;
          }
        }
        if (!builderFound) {
          unbuildableTypes.add(unitType);
          continue;
        }
      }

      for (Entry<String, Boolean> entry : unitType.getRequiredAbilities().entrySet()) {
        if (colony.hasAbility(entry.getKey()) != entry.getValue()
            && FeatureContainer.hasAbility(featureContainer, entry.getKey(), null, null)
                != entry.getValue()) {
          List<FreeColGameObjectType> sources =
              spec.getTypesProviding(entry.getKey(), entry.getValue());
          if (sources.isEmpty()) {
            // no type provides the required ability
            unbuildableTypes.add(unitType);
            continue loop;
          } else {
            lockReason.add(Messages.message(sources.get(0).getNameKey()));
          }
        }
      }
      if (lockReason.isEmpty()) {
        lockReasons.put(unitType, null);
      } else {
        lockReasons.put(
            unitType,
            Messages.message(
                StringTemplate.template("colonyPanel.requires")
                    .addName("%string%", Utils.join("/", lockReason))));
      }
      if (lockReason.isEmpty() || showAll.isSelected()) {
        units.addElement(unitType);
      }
    }
  }