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); }
@Override public void actionPerformed(ActionEvent e) { int[] selectedRows = equipmentTable.getSelectedRows(); MapToList<EquipmentFacade, EquipNode> equipMap = new HashMapToList<EquipmentFacade, EquipNode>(); EquipmentSetFacade equipSet = character.getEquipmentSetRef().getReference(); List<EquipmentFacade> equipment = new ArrayList<EquipmentFacade>(); for (int i = 0; i < selectedRows.length; i++) { EquipmentFacade equipmentFacade = selectedModel.getValue(selectedRows[i]); for (EquipNode path : equipSet.getNodes()) { if (equipSet.canEquip(path, equipmentFacade)) { equipMap.addToListFor(equipmentFacade, path); } } if (equipMap.containsListFor(equipmentFacade)) { equipment.add(equipmentFacade); } } if (!equipment.isEmpty()) { Object[][] data = new Object[equipment.size()][3]; for (int i = 0; i < equipment.size(); i++) { EquipmentFacade equipmentFacade = equipment.get(i); data[i][0] = equipmentFacade; data[i][1] = 1; data[i][2] = getInitialNode(equipMap, equipSet, equipmentFacade); } Object[] columns = new Object[] { LanguageBundle.getString("in_equipItem"), // $NON-NLS-1$ LanguageBundle.getString("in_equipQuantityAbbrev"), // $NON-NLS-1$ LanguageBundle.getString("in_equipContainer") // $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.setDefaultEditor(Object.class, new ComboEditor(equipMap)); table.setDefaultRenderer(Integer.class, new TableCellUtilities.SpinnerRenderer()); table.setDefaultEditor(Integer.class, new SpinnerEditor(unequippedList)); table.setRowHeight(22); table.getColumnModel().getColumn(0).setPreferredWidth(140); table.getColumnModel().getColumn(1).setPreferredWidth(50); table.getColumnModel().getColumn(2).setPreferredWidth(120); 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_equipSelectQtyLoc")); // $NON-NLS-1$ panel.add(help, BorderLayout.NORTH); panel.add(pane, BorderLayout.CENTER); int res = JOptionPane.showConfirmDialog( JOptionPane.getFrameForComponent(equipmentTable), panel, LanguageBundle.getString("in_equipEquipSel"), // $NON-NLS-1$ JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if (res == JOptionPane.OK_OPTION) { for (int i = 0; i < equipment.size(); i++) { EquipNode path = (EquipNode) tableModel.getValueAt(i, 2); equipSet.addEquipment(path, equipment.get(i), (Integer) tableModel.getValueAt(i, 1)); } } } }
@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)); } } } }