public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { BuildableType item = (BuildableType) value; JPanel panel = isSelected ? selectedPanel : itemPanel; panel.removeAll(); ((ImageIcon) imageLabel.getIcon()) .setImage(ResourceManager.getImage(item.getId() + ".image", buildingDimension)); nameLabel.setText(Messages.message(item.getNameKey())); panel.setToolTipText(lockReasons.get(item)); panel.add(imageLabel, "span 1 2"); if (lockReasons.get(item) == null) { panel.add(nameLabel, "wrap"); } else { panel.add(nameLabel, "split 2"); panel.add(lockLabel, "wrap"); } List<AbstractGoods> required = item.getRequiredGoods(); int size = required.size(); for (int i = 0; i < size; i++) { AbstractGoods goods = required.get(i); ImageIcon icon = new ImageIcon(ResourceManager.getImage(goods.getType().getId() + ".image", 0.66)); JLabel goodsLabel = new JLabel(Integer.toString(goods.getAmount()), icon, SwingConstants.CENTER); if (i == 0 && size > 1) { panel.add(goodsLabel, "split " + size); } else { panel.add(goodsLabel); } } return panel; }
@Override @SuppressWarnings({"deprecation", "unchecked"}) // FIXME in Java7 public void mousePressed(MouseEvent e) { if (!enabled && e.getClickCount() == 1 && !e.isConsumed()) { enabled = true; } if (enabled) { JList source = (JList) e.getSource(); if ((e.getButton() == MouseEvent.BUTTON3 || e.isPopupTrigger())) { int index = source.locationToIndex(e.getPoint()); BuildableType type = (BuildableType) source.getModel().getElementAt(index); getGUI().showColopediaPanel(type.getId()); } else if ((e.getClickCount() > 1 && !e.isConsumed())) { DefaultListModel model = (DefaultListModel) buildQueueList.getModel(); if (source.getSelectedIndex() == -1) { source.setSelectedIndex(source.locationToIndex(e.getPoint())); } for (Object type : source.getSelectedValues()) { if (add) { model.addElement(type); } else { model.removeElement(type); } } updateAllLists(); } } }
private void initialize(BuildableType buildable) { removeAll(); if (buildable == null) { String clickToBuild = Messages.message(getDefaultLabel()); int breakingPoint = Messages.getBreakingPoint(clickToBuild); if (breakingPoint > 0) { add(new JLabel(clickToBuild.substring(0, breakingPoint)), "span, align center"); add(new JLabel(clickToBuild.substring(breakingPoint + 1)), "span, align center"); } else { add(new JLabel(clickToBuild), "span, align center"); } } else { int turnsToComplete = colony.getTurnsToComplete(buildable); String turnsStr = Messages.getTurnsText(turnsToComplete); add( new JLabel(new ImageIcon(ResourceManager.getImage(buildable.getId() + ".image", 0.75))), "spany"); add( new JLabel( Messages.message( StringTemplate.template("colonyPanel.currentlyBuilding") .addName("%buildable%", buildable)))); add( new JLabel( Messages.message( StringTemplate.template("turnsToComplete.long").addName("%number%", turnsStr)))); for (AbstractGoods requiredGoods : buildable.getGoodsRequired()) { int amountNeeded = requiredGoods.getAmount(); int amountAvailable = colony.getGoodsCount(requiredGoods.getType()); int amountProduced = colony.getAdjustedNetProductionOf(requiredGoods.getType()); add( new FreeColProgressBar( gui, requiredGoods.getType(), 0, amountNeeded, amountAvailable, amountProduced), "height 20:"); } } revalidate(); repaint(); }
private int getMaximumIndex(BuildableType buildableType) { ListModel buildQueue = buildQueueList.getModel(); final int buildQueueLastPos = buildQueue.getSize(); boolean canBuild = false; if (colony.canBuild(buildableType)) { canBuild = true; } if (buildableType instanceof UnitType) { // does not depend on anything, nothing depends on it // can be built at any time if (canBuild) { return buildQueueLastPos; } // check for building in queue that allows builting this unit for (int index = 0; index < buildQueue.getSize(); index++) { BuildableType toBuild = (BuildableType) buildQueue.getElementAt(index); if (toBuild == buildableType) { continue; } if (toBuild.hasAbility(Ability.BUILD, buildableType)) { return buildQueueLastPos; } } return UNABLE_TO_BUILD; } if (buildableType instanceof BuildingType) { BuildingType upgradesFrom = ((BuildingType) buildableType).getUpgradesFrom(); BuildingType upgradesTo = ((BuildingType) buildableType).getUpgradesTo(); // does not depend on nothing, but still cannot be built if (!canBuild && upgradesFrom == null) { return UNABLE_TO_BUILD; } // if can be built and does not have any upgrade, // then it can be built at any time if (canBuild && upgradesTo == null) { return buildQueueLastPos; } // if can be built, does not depend on anything, mark upgradesfrom as found boolean foundUpgradesFrom = canBuild ? true : false; for (int index = 0; index < buildQueue.getSize(); index++) { BuildableType toBuild = (BuildableType) buildQueue.getElementAt(index); if (toBuild == buildableType) { continue; } if (!canBuild && !foundUpgradesFrom && upgradesFrom.equals(toBuild)) { foundUpgradesFrom = true; // nothing else to upgrade this building to if (upgradesTo == null) { return buildQueueLastPos; } } // found a building it upgrades to, cannot go to or beyond this position if (foundUpgradesFrom && upgradesTo != null && upgradesTo.equals(toBuild)) { return index; } // Don't go past a unit this building can build. if (buildableType.hasAbility(Ability.BUILD, toBuild)) { return index; } } return buildQueueLastPos; } return UNABLE_TO_BUILD; }