private EquipNode getInitialNode( MapToList<EquipmentFacade, EquipNode> equipMap, EquipmentSetFacade equipSet, EquipmentFacade equipmentFacade) { // First see if the user has selected a suitable node in the equipped tree List<EquipNode> possibleNodeList = equipMap.getListFor(equipmentFacade); int[] rows = equipmentSetTable.getSelectedRows(); List<EquipNode> paths = new ArrayList<EquipNode>(); for (int i = 0; i < rows.length; i++) { EquipNode path = (EquipNode) equipmentSetTable.getValueAt(rows[i], 0); if (possibleNodeList.contains(path)) { return path; } } // Check if the preferred location can be found in the list String preferredNodeName = equipSet.getPreferredLoc(equipmentFacade); for (EquipNode node : possibleNodeList) { if (preferredNodeName.equals(node.toString())) { return node; } } // Fall back to the first item in the list return equipMap.getElementInList(equipmentFacade, 0); }
/** * Check that EquipmentSetFacadeImpl when initialised with a dataset containing equipment hides * and shows the correct weapon slots. */ public void testSlotManagementOnInitWithEquipment() { PlayerCharacter pc = getCharacter(); EquipSet es = new EquipSet("0.1", "Unit Test Equip"); Equipment weapon = new Equipment(); weapon.setName("Morningstar"); addEquipToEquipSet(pc, es, weapon, 1.0f, LOC_PRIMARY); EquipmentSetFacadeImpl esfi = new EquipmentSetFacadeImpl(uiDelegate, pc, es, dataset); ListFacade<EquipNode> nodes = esfi.getNodes(); Map<String, EquipNode> nodeMap = new HashMap<String, EquipNode>(); for (EquipNode equipNode : nodes) { nodeMap.put(equipNode.toString(), equipNode); } EquipNode testNode = nodeMap.get("Morningstar"); assertNotNull("Morningstar should be present", testNode); assertEquals("Morningstar type", EquipNode.NodeType.EQUIPMENT, testNode.getNodeType()); assertEquals("Morningstar location", LOC_PRIMARY, esfi.getLocation(testNode)); // Test for removed slots String removedSlots[] = new String[] {"Primary Hand", "Double Weapon", "Both Hands"}; for (String slotName : removedSlots) { testNode = nodeMap.get(slotName); assertNull(slotName + " should not be present", testNode); } // Test for still present slots String retainedSlots[] = new String[] {"Secondary Hand", "Ring"}; for (String slotName : retainedSlots) { testNode = nodeMap.get(slotName); assertNotNull(slotName + " should be present", testNode); } }
/** Check that EquipmentSetFacadeImpl can be initialised with a dataset containing equipment. */ public void testInitWithEquipment() { PlayerCharacter pc = getCharacter(); EquipSet es = new EquipSet("0.1", "Unit Test Equip"); Equipment item = new Equipment(); item.setName("Satchel"); Equipment item2 = new Equipment(); item2.setName("Book"); Equipment item3 = new Equipment(); item3.setName("Quarterstaff"); EquipSet satchelEs = addEquipToEquipSet(pc, es, item, 1.0f); addEquipToEquipSet(pc, satchelEs, item2, 1.0f); addEquipToEquipSet(pc, es, item3, 1.0f, LOC_BOTH_HANDS); int adjustedBaseNodes = NUM_BASE_NODES - 4; EquipmentSetFacadeImpl esfi = new EquipmentSetFacadeImpl(uiDelegate, pc, es, dataset); ListFacade<EquipNode> nodeList = esfi.getNodes(); assertFalse("Expected a non empty path set", nodeList.isEmpty()); EquipNodeImpl node = (EquipNodeImpl) nodeList.getElementAt(0); assertEquals("Incorrect body struct name", Constants.EQUIP_LOCATION_EQUIPPED, node.toString()); assertEquals("Incorrect body struct type", NodeType.BODY_SLOT, node.getNodeType()); assertEquals("Incorrect sort key", "00", node.getSortKey()); assertEquals("Incorrect parent", null, node.getParent()); node = (EquipNodeImpl) nodeList.getElementAt(adjustedBaseNodes); assertEquals("Incorrect container name", item.getName(), node.toString()); assertEquals("Incorrect container type", NodeType.EQUIPMENT, node.getNodeType()); assertEquals("Incorrect sort key", "00|" + item.getName(), node.getSortKey()); assertEquals("Incorrect parent", nodeList.getElementAt(0), node.getParent()); node = (EquipNodeImpl) nodeList.getElementAt(adjustedBaseNodes + 2); assertEquals("Incorrect item name", item2.getName(), node.toString()); assertEquals("Incorrect item type", NodeType.EQUIPMENT, node.getNodeType()); assertEquals( "Incorrect sort key", "00|" + item.getName() + "|" + item2.getName(), node.getSortKey()); assertEquals("Incorrect parent", nodeList.getElementAt(adjustedBaseNodes), node.getParent()); node = (EquipNodeImpl) nodeList.getElementAt(adjustedBaseNodes + 1); assertEquals("Incorrect item name", item3.getName(), node.toString()); assertEquals("Incorrect item type", NodeType.EQUIPMENT, node.getNodeType()); assertEquals("Incorrect sort key", "01|" + item3.getName(), node.getSortKey()); assertEquals("Incorrect parent", LOC_HANDS, node.getParent().toString()); node = (EquipNodeImpl) nodeList.getElementAt(adjustedBaseNodes + 2); EquipNode parent = node.getParent(); assertEquals( "Root incorrect", Constants.EQUIP_LOCATION_EQUIPPED, parent.getParent().toString()); assertEquals("Leaf incorrect", item.getName(), parent.toString()); assertEquals("Incorrect nuber of paths found", adjustedBaseNodes + 3, nodeList.getSize()); }
/** Test the creation of phantom slots, looking at types and quantities particularly. */ public void testSlotCreation() { EquipSet es = new EquipSet("0.1", "Unit Test Equip"); EquipmentSetFacadeImpl esfi = new EquipmentSetFacadeImpl(uiDelegate, getCharacter(), es, dataset); ListFacade<EquipNode> nodes = esfi.getNodes(); Map<String, EquipNode> nodeMap = new HashMap<String, EquipNode>(); for (EquipNode equipNode : nodes) { nodeMap.put(equipNode.toString(), equipNode); } EquipNode testNode = nodeMap.get("Primary Hand"); assertNotNull("Primary Hand should be present", testNode); assertEquals("Primary Hand type", EquipNode.NodeType.PHANTOM_SLOT, testNode.getNodeType()); assertEquals("Primary Hand count", 1, esfi.getQuantity(testNode)); testNode = nodeMap.get("Secondary Hand"); assertNotNull("Secondary Hand should be present", testNode); assertEquals("Secondary Hand type", EquipNode.NodeType.PHANTOM_SLOT, testNode.getNodeType()); assertEquals("Secondary Hand count", 1, esfi.getQuantity(testNode)); testNode = nodeMap.get(Constants.EQUIP_LOCATION_SECONDARY + " 1"); assertNull(Constants.EQUIP_LOCATION_SECONDARY + " 1 should not be present", testNode); testNode = nodeMap.get(Constants.EQUIP_LOCATION_SECONDARY + " 2"); assertNull(Constants.EQUIP_LOCATION_SECONDARY + " 2 should not be present", testNode); testNode = nodeMap.get("Double Weapon"); assertNotNull("Double Weapon should be present", testNode); assertEquals("Double Weapon type", EquipNode.NodeType.PHANTOM_SLOT, testNode.getNodeType()); assertEquals("Double Weapon count", 1, esfi.getQuantity(testNode)); testNode = nodeMap.get("Both Hands"); assertNotNull("Both Hands should be present", testNode); assertEquals("Both Hands type", EquipNode.NodeType.PHANTOM_SLOT, testNode.getNodeType()); assertEquals("Both Hands count", 1, esfi.getQuantity(testNode)); testNode = nodeMap.get("Unarmed"); assertNotNull("Unarmed should be present", testNode); assertEquals("Unarmed type", EquipNode.NodeType.PHANTOM_SLOT, testNode.getNodeType()); assertEquals("Unarmed count", 1, esfi.getQuantity(testNode)); testNode = nodeMap.get("Ring"); assertNotNull("Ring should be present", testNode); assertEquals("Ring type", EquipNode.NodeType.PHANTOM_SLOT, testNode.getNodeType()); assertEquals("Ring count", 2, esfi.getQuantity(testNode)); }
@Override public void actionPerformed(ActionEvent e) { EquipmentSetFacade equipSet = character.getEquipmentSetRef().getReference(); int[] rows = equipmentSetTable.getSelectedRows(); List<EquipNode> paths = new ArrayList<EquipNode>(); for (int i = 0; i < rows.length; i++) { EquipNode path = (EquipNode) equipmentSetTable.getValueAt(rows[i], 0); if (path.getNodeType() == NodeType.EQUIPMENT) { paths.add(path); } } if (!paths.isEmpty()) { Object[][] data = new Object[paths.size()][3]; for (int i = 0; i < paths.size(); i++) { EquipNode path = paths.get(i); data[i][0] = path.getEquipment(); data[i][1] = 1; } Object[] columns = new Object[] { LanguageBundle.getString("in_equipItem"), // $NON-NLS-1$ LanguageBundle.getString("in_equipQuantityAbbrev"), // $NON-NLS-1$ }; DefaultTableModel tableModel = new DefaultTableModel(data, columns) { @Override public Class<?> getColumnClass(int columnIndex) { if (columnIndex == 1) { return Integer.class; } return Object.class; } @Override public boolean isCellEditable(int row, int column) { return column != 0; } }; JTable table = new JTable(tableModel); table.setFocusable(false); table.setCellSelectionEnabled(false); table.setDefaultRenderer(Integer.class, new TableCellUtilities.SpinnerRenderer()); table.setDefaultEditor(Integer.class, new SpinnerEditor(equipSet.getEquippedItems())); table.setRowHeight(22); table.getColumnModel().getColumn(0).setPreferredWidth(140); table.getColumnModel().getColumn(1).setPreferredWidth(50); table.setPreferredScrollableViewportSize(table.getPreferredSize()); JTableHeader header = table.getTableHeader(); header.setReorderingAllowed(false); JScrollPane pane = new JScrollPane(table); JPanel panel = new JPanel(new BorderLayout()); JLabel help = new JLabel(LanguageBundle.getString("in_equipSelectUnequipQty")); // $NON-NLS-1$ panel.add(help, BorderLayout.NORTH); panel.add(pane, BorderLayout.CENTER); int res = JOptionPane.showConfirmDialog( JOptionPane.getFrameForComponent(equipmentTable), panel, LanguageBundle.getString("in_equipUnequipSel"), // $NON-NLS-1$ JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if (res == JOptionPane.OK_OPTION) { for (int i = 0; i < paths.size(); i++) { equipSet.removeEquipment(paths.get(i), (Integer) tableModel.getValueAt(i, 1)); } } } }