本文整理汇总了Java中net.sf.freecol.client.gui.label.UnitLabel.getUnit方法的典型用法代码示例。如果您正苦于以下问题:Java UnitLabel.getUnit方法的具体用法?Java UnitLabel.getUnit怎么用?Java UnitLabel.getUnit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.sf.freecol.client.gui.label.UnitLabel
的用法示例。
在下文中一共展示了UnitLabel.getUnit方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: importUnit
import net.sf.freecol.client.gui.label.UnitLabel; //导入方法依赖的package包/类
/**
* Import a unit specified by its label to a component.
*
* @param comp The component to import to.
* @param label The {@code UnitLabel} specifying the unit.
* @param oldSelectedUnit A label for the old {@code Unit} to restore
* selection to.
* @return True if the import succeeds.
*/
private boolean importUnit(JComponent comp, UnitLabel label,
UnitLabel oldSelectedUnit) {
if (!(comp instanceof DropTarget)) return importFail(comp, "unit");
final DropTarget target = (DropTarget)comp;
// Check if the unit can be dragged to comp.
final Unit unit = label.getUnit();
if (!target.accepts(unit)) {
return importFail(comp, "unacceptable unit (" + unit + ")");
}
// OK, add it.
target.add(label, 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;
}
示例2: addBoardItems
import net.sf.freecol.client.gui.label.UnitLabel; //导入方法依赖的package包/类
private boolean addBoardItems(final UnitLabel unitLabel, Location loc) {
final Unit tempUnit = unitLabel.getUnit();
if (tempUnit.isCarrier()) return false;
final InGameController igc = freeColClient.getInGameController();
boolean added = false;
for (Unit unit : transform(loc.getUnitList(), u ->
(u.isCarrier() && u.canCarryUnits() && u.canAdd(tempUnit)
&& tempUnit.getLocation() != u))) {
StringTemplate template
= StringTemplate.template("quickActionMenu.board")
.addStringTemplate("%unit%",
unit.getLabel(Unit.UnitLabelType.NATIONAL));
JMenuItem menuItem = Utility.localizedMenuItem(template);
menuItem.addActionListener((ActionEvent ae) -> {
igc.boardShip(tempUnit, unit);
});
this.add(menuItem);
added = true;
}
return added;
}
示例3: selectLabel
import net.sf.freecol.client.gui.label.UnitLabel; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void selectLabel() {
// Keep the previous selected unit if possible, otherwise default
// on the last carrier.
PortPanel portPanel = getPortPanel();
if (portPanel == null) return;
Unit selectedUnit = portPanel.getSelectedUnit();
UnitLabel lastCarrier = null;
for (Component component : getComponents()) {
if (component instanceof UnitLabel) {
UnitLabel label = (UnitLabel)component;
Unit unit = label.getUnit();
if (unit == selectedUnit) {
portPanel.setSelectedUnitLabel(label);
return;
}
if (unit.isCarrier() && unit.getTradeRoute() == null) {
lastCarrier = label;
}
}
}
if (lastCarrier != null) {
portPanel.setSelectedUnitLabel(lastCarrier);
}
// No revalidate+repaint as this is done in setSelectedUnitLabel
}
示例4: addTileItem
import net.sf.freecol.client.gui.label.UnitLabel; //导入方法依赖的package包/类
/**
* Add a menu item for the tile a unit is working.
*
* @param unitLabel The {@code UnitLabel} specifying the unit.
* @return True if an item was added.
*/
private boolean addTileItem(final UnitLabel unitLabel) {
final Unit unit = unitLabel.getUnit();
final Tile tile = (unit == null) ? null : unit.getWorkTile();
if (tile != null) {
addTileItem(tile);
return true;
}
return false;
}
示例5: add
import net.sf.freecol.client.gui.label.UnitLabel; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public Component add(Component comp, boolean editState) {
Container oldParent = comp.getParent();
if (editState) {
if (comp instanceof UnitLabel) {
UnitLabel unitLabel = (UnitLabel)comp;
Unit unit = unitLabel.getUnit();
if (!unit.isOnCarrier()) {
igc().putOutsideColony(unit);
}
if (unit.getColony() == null) {
closeColonyPanel();
return null;
} else if (!(unit.getLocation() instanceof Tile)
&& !unit.isOnCarrier()) {
return null;
}
oldParent.remove(comp);
initialize();
return comp;
} else {
logger.warning("Invalid component: " + comp);
return null;
}
} else {
((UnitLabel)comp).setSmall(false);
return add(comp);
}
}
示例6: updateDescriptionLabel
import net.sf.freecol.client.gui.label.UnitLabel; //导入方法依赖的package包/类
/**
* Updates the description label, which is a tooltip with
* the terrain type, road and plow indicator, if any.
*
* If a unit is on it update the tooltip of it instead.
*
* @param unitLabel The {@code UnitLabel} to update.
*/
private void updateDescriptionLabel(UnitLabel unitLabel) {
String tileMsg = Messages.message(this.colonyTile.getLabel());
if (unitLabel == null) {
setToolTipText(tileMsg);
} else {
final Unit unit = unitLabel.getUnit();
unitLabel.setDescriptionLabel(tileMsg + " ["
+ unit.getDescription(Unit.UnitLabelType.NATIONAL)
+ "]");
}
}
示例7: equipUnitIfPossible
import net.sf.freecol.client.gui.label.UnitLabel; //导入方法依赖的package包/类
private boolean equipUnitIfPossible(UnitLabel unitLabel,
AbstractGoods goods) {
final Unit unit = unitLabel.getUnit();
if (!unit.hasAbility(Ability.CAN_BE_EQUIPPED)
|| unit.getRole().hasAbility(Ability.ESTABLISH_MISSION)) {
// Do not equip missionaries. The test below will succeed
// when dragging incompatible goods (anything:-) because
// there is no actual missionary equipment.
return false;
}
for (Role role : transform(unit.getAvailableRoles(null),
r -> !r.isDefaultRole())) {
List<AbstractGoods> required = unit.getGoodsDifference(role, 1);
int count;
if (required.size() == 1
&& required.get(0).getType() == goods.getType()
&& (count = Math.min(role.getMaximumCount(),
goods.getAmount() / required.get(0).getAmount())) > 0
&& (role != unit.getRole() || count != unit.getRoleCount())) {
freeColClient.getInGameController()
.equipUnitForRole(unit, role, count);
unitLabel.updateIcon();
return true;
}
}
return false;
}
示例8: setSelectedUnit
import net.sf.freecol.client.gui.label.UnitLabel; //导入方法依赖的package包/类
/**
* Select a given unit.
*
* @param unit The {@code Unit} to select.
* @return True if the selection succeeds.
*/
public boolean setSelectedUnit(Unit unit) {
for (Component component : getComponents()) {
if (component instanceof UnitLabel) {
UnitLabel label = (UnitLabel)component;
if (label.getUnit() == unit) {
setSelectedUnitLabel(label);
return true;
}
}
}
return false;
}
示例9: setSelectedUnit
import net.sf.freecol.client.gui.label.UnitLabel; //导入方法依赖的package包/类
/**
* Select a given unit.
*
* @param unit The {@code Unit} to select.
* @return True if the selection succeeds.
*/
public boolean setSelectedUnit(Unit unit) {
for (Component component : getComponents()) {
if (component instanceof UnitLabel) {
UnitLabel label = (UnitLabel)component;
if (label.getUnit() == unit) {
getPortPanel().setSelectedUnitLabel(label);
return true;
}
}
}
return false;
}
示例10: createUnitMenu
import net.sf.freecol.client.gui.label.UnitLabel; //导入方法依赖的package包/类
/**
* Creates a popup menu for a Unit.
*
* @param unitLabel The {@code UnitLabel} to create items for.
*/
private void createUnitMenu(final UnitLabel unitLabel) {
final Unit unit = unitLabel.getUnit();
this.setLabel("Unit");
ImageIcon unitIcon = new ImageIcon(gui.getImageLibrary()
.getSmallUnitImage(unit));
JMenuItem name = new JMenuItem(unit.getDescription(Unit.UnitLabelType.NATIONAL)
+ " (" + Messages.message("colopedia") + ")", unitIcon);
name.setActionCommand(UnitAction.COLOPEDIA.toString());
name.addActionListener(unitLabel);
this.add(name);
this.addSeparator();
if (addCarrierItems(unitLabel)) this.addSeparator();
if (unit.isInEurope()) {
if (addCommandItems(unitLabel)) this.addSeparator();
if (addBoardItems(unitLabel, unit.getOwner().getEurope())) {
this.addSeparator();
}
} else if (unit.hasTile()) {
Colony colony = unit.getLocation().getTile().getColony();
if (colony != null) {
if (addTileItem(unitLabel)) this.addSeparator();
if (addWorkItems(unitLabel)) this.addSeparator();
if (unit.isInColony()
&& addEducationItems(unitLabel)) this.addSeparator();
if (unit.isInColony() && colony.canReducePopulation()) {
JMenuItem menuItem = Utility.localizedMenuItem("quickActionMenu.leaveTown");
menuItem.setActionCommand(UnitAction.LEAVE_TOWN.toString());
menuItem.addActionListener(unitLabel);
this.add(menuItem);
addBoardItems(unitLabel, colony.getTile());
this.addSeparator();
} else {
if (addCommandItems(unitLabel)) this.addSeparator();
if (addBoardItems(unitLabel, colony.getTile())) {
this.addSeparator();
}
}
} else {
if (addCommandItems(unitLabel)) this.addSeparator();
}
}
if (unit.hasAbility(Ability.CAN_BE_EQUIPPED)) {
if (addRoleItems(unitLabel)) this.addSeparator();
}
}
示例11: addRoleItems
import net.sf.freecol.client.gui.label.UnitLabel; //导入方法依赖的package包/类
/**
* Add menu items for role manipulation for a unit.
*
* Note "clear speciality" is here too to keep it well separated from
* other items.
*
* @param unitLabel The {@code UnitLabel} specifying the unit.
* @return True if menu items were added and a separator is now needed.
*/
private boolean addRoleItems(final UnitLabel unitLabel) {
final Specification spec = freeColClient.getGame().getSpecification();
final Unit unit = unitLabel.getUnit();
final Role role = unit.getRole();
final int roleCount = unit.getRoleCount();
boolean separatorNeeded = false;
UnitLocation uloc = (unit.isInEurope()) ? unit.getOwner().getEurope()
: unit.getSettlement();
if (uloc == null) return false;
for (Role r : transform(unit.getAvailableRoles(null),
r2 -> r2 != role)) {
JMenuItem newItem;
if (r.isDefaultRole()) { // Always valid
newItem = createRoleItem(unitLabel, role, roleCount, r, 0, 0);
} else {
newItem = null;
for (int count = r.getMaximumCount(); count > 0; count--) {
List<AbstractGoods> req = unit.getGoodsDifference(r, count);
try {
int price = uloc.priceGoods(req);
if (unit.getOwner().checkGold(price)) {
newItem = createRoleItem(unitLabel, role, roleCount,
r, count, price);
break;
}
} catch (FreeColException fce) {
continue;
}
}
}
if (newItem != null) {
this.add(newItem);
separatorNeeded = true;
}
}
UnitTypeChange uc = unit.getUnitChange(UnitChangeType.CLEAR_SKILL);
if (uc != null) {
if (separatorNeeded) this.addSeparator();
JMenuItem menuItem = Utility.localizedMenuItem("quickActionMenu.clearSpeciality",
new ImageIcon(gui.getImageLibrary().getTinyUnitImage(uc.to)));
menuItem.setActionCommand(UnitAction.CLEAR_SPECIALITY.toString());
menuItem.addActionListener(unitLabel);
this.add(menuItem);
if (unit.getLocation() instanceof Building
&& !((Building)unit.getLocation()).canAddType(uc.to)) {
menuItem.setEnabled(false);
}
separatorNeeded = true;
}
return separatorNeeded;
}
示例12: createUnitMenu
import net.sf.freecol.client.gui.label.UnitLabel; //导入方法依赖的package包/类
/**
* Creates a popup menu for a Unit.
*
* @param unitLabel The {@code UnitLabel} to create items for.
*/
private void createUnitMenu(final UnitLabel unitLabel) {
final Unit unit = unitLabel.getUnit();
this.setLabel("Unit");
ImageIcon unitIcon = new ImageIcon(gui.getImageLibrary()
.getSmallUnitImage(unit));
JMenuItem name = new JMenuItem(unit.getDescription(Unit.UnitLabelType.NATIONAL)
+ " (" + Messages.message("colopedia") + ")", unitIcon);
name.setActionCommand(UnitAction.COLOPEDIA.toString());
name.addActionListener(unitLabel);
this.add(name);
this.addSeparator();
if (addCarrierItems(unitLabel)) this.addSeparator();
if (unit.isInEurope()) {
if (addCommandItems(unitLabel)) this.addSeparator();
if (addBoardItems(unitLabel, unit.getOwner().getEurope())) {
this.addSeparator();
}
} else if (unit.hasTile()) {
Colony colony = unit.getLocation().getTile().getColony();
if (colony != null) {
if (addTileItem(unitLabel)) this.addSeparator();
if (addWorkItems(unitLabel)) this.addSeparator();
if (addEducationItems(unitLabel)) this.addSeparator();
if (unit.isInColony() && colony.canReducePopulation()) {
JMenuItem menuItem = Utility.localizedMenuItem("quickActionMenu.leaveTown");
menuItem.setActionCommand(UnitAction.LEAVE_TOWN.toString());
menuItem.addActionListener(unitLabel);
this.add(menuItem);
addBoardItems(unitLabel, colony.getTile());
this.addSeparator();
} else {
if (addCommandItems(unitLabel)) this.addSeparator();
if (addBoardItems(unitLabel, colony.getTile())) {
this.addSeparator();
}
}
} else {
if (addCommandItems(unitLabel)) this.addSeparator();
}
}
if (unit.hasAbility(Ability.CAN_BE_EQUIPPED)) {
if (addRoleItems(unitLabel)) this.addSeparator();
}
}