private void updateAllLists() { DefaultListModel current = (DefaultListModel) buildQueueList.getModel(); featureContainer = new FeatureContainer(); for (Object type : current.toArray()) { if (getMinimumIndex((BuildableType) type) >= 0) { FeatureContainer.addFeatures(featureContainer, (BuildableType) type); } else { current.removeElement(type); } } // ATTENTION: buildings must be updated first, since units // might depend on the build ability of an unbuildable // building updateBuildingList(); updateUnitList(); updateBuyBuildingButton(); // work-around to re-initialize construction panel PropertyChangeEvent event = new PropertyChangeEvent( colony, ConstructionPanel.EVENT, null, getBuildableTypes(buildQueueList)); constructionPanel.propertyChange(event); }
@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); } } }
@SuppressWarnings("unchecked") // FIXME in Java7 private void updateBuildingList() { DefaultListModel buildings = (DefaultListModel) buildingList.getModel(); DefaultListModel current = (DefaultListModel) buildQueueList.getModel(); buildings.clear(); loop: for (BuildingType buildingType : getSpecification().getBuildingTypeList()) { // compare colony.getNoBuildReason() List<String> lockReason = new ArrayList<String>(); Building colonyBuilding = colony.getBuilding(buildingType); if (current.contains(buildingType) || hasBuildingType(buildingType)) { // only one building of any kind continue; } else if (unbuildableTypes.contains(buildingType)) { continue; } else if (!buildingType.needsGoodsToBuild()) { // pre-built continue; } else if (unbuildableTypes.contains(buildingType.getUpgradesFrom())) { // impossible upgrade path unbuildableTypes.add(buildingType); continue; } if (buildingType.getRequiredPopulation() > unitCount) { lockReason.add( Messages.message( StringTemplate.template("colonyPanel.populationTooSmall") .addAmount("%number%", buildingType.getRequiredPopulation()))); } for (Entry<String, Boolean> entry : buildingType.getRequiredAbilities().entrySet()) { if (colony.hasAbility(entry.getKey()) != entry.getValue() && FeatureContainer.hasAbility(featureContainer, entry.getKey(), null, null) != entry.getValue()) { List<FreeColGameObjectType> sources = getSpecification().getTypesProviding(entry.getKey(), entry.getValue()); if (sources.isEmpty()) { // no type provides the required ability unbuildableTypes.add(buildingType); continue loop; } else { lockReason.add(Messages.message(sources.get(0).getNameKey())); } } } if (buildingType.getLimits() != null) { for (Limit limit : buildingType.getLimits()) { if (!limit.evaluate(colony)) { lockReason.add(Messages.message(limit.getDescriptionKey())); } } } if (buildingType.getUpgradesFrom() != null && !current.contains(buildingType.getUpgradesFrom())) { if (colonyBuilding == null || colonyBuilding.getType() != buildingType.getUpgradesFrom()) { lockReason.add(Messages.message(buildingType.getUpgradesFrom().getNameKey())); } } if (lockReason.isEmpty()) { lockReasons.put(buildingType, null); } else { lockReasons.put( buildingType, Messages.message( StringTemplate.template("colonyPanel.requires") .addName("%string%", Utils.join("/", lockReason)))); } if (lockReason.isEmpty() || showAll.isSelected()) { buildings.addElement(buildingType); } } }
@SuppressWarnings("unchecked") // FIXME in Java7 public BuildQueuePanel(FreeColClient freeColClient, GUI gui, Colony colony) { super( freeColClient, gui, new MigLayout("wrap 3", "[260:][390:, fill][260:]", "[][][300:400:][]")); this.colony = colony; this.unitCount = colony.getUnitCount(); featureContainer = new FeatureContainer(); for (UnitType unitType : getSpecification().getUnitTypeList()) { if (unitType.needsGoodsToBuild() && !unitType.hasAbility(Ability.BORN_IN_COLONY)) { buildableUnits.add(unitType); // can be built } } DefaultListModel current = new DefaultListModel(); for (BuildableType type : colony.getBuildQueue()) { current.addElement(type); FeatureContainer.addFeatures(featureContainer, type); } cellRenderer = getCellRenderer(); // remove previous listeners for (ItemListener listener : compact.getItemListeners()) { compact.removeItemListener(listener); } compact.setText(Messages.message("colonyPanel.compactView")); compact.addItemListener(this); // remove previous listeners for (ItemListener listener : showAll.getItemListeners()) { showAll.removeItemListener(listener); } showAll.setText(Messages.message("colonyPanel.showAll")); showAll.addItemListener(this); buildQueueList = new JList(current); buildQueueList.setTransferHandler(buildQueueHandler); buildQueueList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); buildQueueList.setDragEnabled(true); buildQueueList.setCellRenderer(cellRenderer); buildQueueList.addMouseListener(new BuildQueueMouseAdapter(false)); Action deleteAction = new AbstractAction() { @SuppressWarnings("deprecation") // FIXME in Java7 public void actionPerformed(ActionEvent e) { for (Object type : buildQueueList.getSelectedValues()) { removeBuildable(type); } updateAllLists(); } }; buildQueueList.getInputMap().put(KeyStroke.getKeyStroke("DELETE"), "delete"); buildQueueList.getActionMap().put("delete", deleteAction); Action addAction = new AbstractAction() { @SuppressWarnings("deprecation") // FIXME in Java7 public void actionPerformed(ActionEvent e) { DefaultListModel model = (DefaultListModel) buildQueueList.getModel(); for (Object type : ((JList) e.getSource()).getSelectedValues()) { model.addElement(type); } updateAllLists(); } }; BuildQueueMouseAdapter adapter = new BuildQueueMouseAdapter(true); DefaultListModel units = new DefaultListModel(); unitList = new JList(units); unitList.setTransferHandler(buildQueueHandler); unitList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); unitList.setDragEnabled(true); unitList.setCellRenderer(cellRenderer); unitList.addMouseListener(adapter); unitList.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "add"); unitList.getActionMap().put("add", addAction); DefaultListModel buildings = new DefaultListModel(); buildingList = new JList(buildings); buildingList.setTransferHandler(buildQueueHandler); buildingList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); buildingList.setDragEnabled(true); buildingList.setCellRenderer(cellRenderer); buildingList.addMouseListener(adapter); buildingList.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "add"); buildingList.getActionMap().put("add", addAction); JLabel headLine = new JLabel(Messages.message("colonyPanel.buildQueue")); headLine.setFont(bigHeaderFont); buyBuilding = new JButton(Messages.message("colonyPanel.buyBuilding")); buyBuilding.setActionCommand(BUY); buyBuilding.addActionListener(this); constructionPanel = new ConstructionPanel(gui, colony, false); constructionPanel.setOpaque(false); StringTemplate buildingNothing = StringTemplate.template("colonyPanel.currentlyBuilding").add("%buildable%", "nothing"); constructionPanel.setDefaultLabel(buildingNothing); updateAllLists(); add(headLine, "span 3, align center, wrap 40"); add(new JLabel(Messages.message("colonyPanel.units")), "align center"); add(new JLabel(Messages.message("colonyPanel.buildQueue")), "align center"); add(new JLabel(Messages.message("colonyPanel.buildings")), "align center"); add(new JScrollPane(unitList), "grow"); add(constructionPanel, "split 2, flowy"); add(new JScrollPane(buildQueueList), "grow"); add(new JScrollPane(buildingList), "grow, wrap 20"); add(buyBuilding, "span, split 4"); add(compact); add(showAll); add(okButton, "tag ok"); }