DefaultCommandMenuInserter implements CommandMenuInserter { static void sortCommandsByAnchor(List<Command> commands) { ArrayList<Command> firstCommands = new ArrayList<>(commands.size()); ArrayList<Command> lastCommands = new ArrayList<>(commands.size()); ArrayList<Command> betweenCommands = new ArrayList<>(commands); for (Command command : commands) { if (command.getPlaceFirst()) { firstCommands.add(command); betweenCommands.remove(command); } else if (command.getPlaceLast() || (command.getPlaceBefore() == null && command.getPlaceAfter() == null)) { lastCommands.add(command); betweenCommands.remove(command); } } Assert.state(firstCommands.size() + lastCommands.size() + betweenCommands.size() == commands.size()); Collections.sort(firstCommands, NAME_COMPARATOR); Collections.sort(betweenCommands, NAME_COMPARATOR); Collections.sort(lastCommands, NAME_COMPARATOR); HashMap<String, Command> commandMap = new HashMap<>(betweenCommands.size() * 2 + 1); for (Command command : commands) { commandMap.put(command.getCommandID(), command); } LinkedList<Command> sortedCommands = new LinkedList<>(); sortedCommands.addAll(firstCommands); sortedCommands.addAll(betweenCommands); sortedCommands.addAll(lastCommands); boolean change = true; int iteration; for (iteration = 0; change && iteration < commands.size(); iteration++) { change = false; for (Command command : commands) { String placeBefore = command.getPlaceBefore(); if (placeBefore != null) { int index = sortedCommands.indexOf(command); Assert.state(index >= 0); Command subsequentCommand = commandMap.get(placeBefore); if (subsequentCommand != null) { int subsequentIndex = sortedCommands.indexOf(subsequentCommand); Assert.state(subsequentIndex >= 0); if (subsequentIndex < index) { sortedCommands.remove(command); sortedCommands.add(subsequentIndex, command); change = true; } } } String placeAfter = command.getPlaceAfter(); if (placeAfter != null) { int index = sortedCommands.indexOf(command); Assert.state(index >= 0); Command precedingCommand = commandMap.get(placeAfter); if (precedingCommand != null) { int precedingIndex = sortedCommands.indexOf(precedingCommand); Assert.state(precedingIndex >= 0); if (precedingIndex > index) { sortedCommands.add(precedingIndex + 1, command); sortedCommands.remove(index); change = true; } } } } } if (iteration > 1 && iteration == commands.size()) { BeamLogManager.getSystemLogger().warning(String.format("Sorting updated command list took %d iterations!", iteration)); } commands.clear(); commands.addAll(sortedCommands); } DefaultCommandMenuInserter(CommandManager commandManager); @Override void insertCommandIntoMenu(Command newCommand, JMenu menu);  }
@Test public void testSortByAnchor_FirstOnly_3() { List<Command> commands = toList(createCommand("a", FIRST), createCommand("b", FIRST), createCommand("c", FIRST)); reverseList(commands); sortCommandsByAnchor(commands); int index = 0; assertEquals("a", commands.get(index++).getCommandID()); assertEquals("b", commands.get(index++).getCommandID()); assertEquals("c", commands.get(index++).getCommandID()); }
@Test public void testSortByAnchor_LastOnly_3() { List<Command> commands = toList(createCommand("a", LAST), createCommand("b", LAST), createCommand("c", LAST)); reverseList(commands); sortCommandsByAnchor(commands); int index = 0; assertEquals("a", commands.get(index++).getCommandID()); assertEquals("b", commands.get(index++).getCommandID()); assertEquals("c", commands.get(index++).getCommandID()); }
@Test public void testSortByAnchor_FirstAndLast_6() { List<Command> commands = toList(createCommand("a", FIRST), createCommand("b", FIRST), createCommand("c", FIRST), createCommand("d", LAST), createCommand("e", LAST), createCommand("f", LAST)); reverseList(commands); sortCommandsByAnchor(commands); int index = 0; assertEquals("a", commands.get(index++).getCommandID()); assertEquals("b", commands.get(index++).getCommandID()); assertEquals("c", commands.get(index++).getCommandID()); assertEquals("d", commands.get(index++).getCommandID()); assertEquals("e", commands.get(index++).getCommandID()); assertEquals("f", commands.get(index++).getCommandID()); }
@Test public void testSortByAnchor_FirstAndLast_2() { List<Command> commands = toList(createCommand("a", FIRST), createCommand("b", LAST)); reverseList(commands); sortCommandsByAnchor(commands); int index = 0; assertEquals("a", commands.get(index++).getCommandID()); assertEquals("b", commands.get(index++).getCommandID()); }
@Test public void testSortByAnchor_BeforeAndLast_2() { List<Command> commands = toList(createCommand("a", BEFORE, "b"), createCommand("b", LAST, null)); reverseList(commands); sortCommandsByAnchor(commands); int index = 0; assertEquals("a", commands.get(index++).getCommandID()); assertEquals("b", commands.get(index++).getCommandID()); }
@Test public void testSortByAnchor_BeforeAndLast_3() { List<Command> commands = toList(createCommand("a", BEFORE, "c"), createCommand("b", BEFORE, "c"), createCommand("c", LAST)); reverseList(commands); sortCommandsByAnchor(commands); int index = 0; assertEquals("a", commands.get(index++).getCommandID()); assertEquals("b", commands.get(index++).getCommandID()); assertEquals("c", commands.get(index++).getCommandID()); }
@Test public void testSortByAnchor_FirstAndAfter_3() { List<Command> commands = toList(createCommand("a", FIRST), createCommand("b", AFTER, "a"), createCommand("c", AFTER, "a")); reverseList(commands); sortCommandsByAnchor(commands); int index = 0; assertEquals("a", commands.get(index++).getCommandID()); assertEquals("b", commands.get(index++).getCommandID()); assertEquals("c", commands.get(index++).getCommandID()); }
@Test public void testSortByAnchor_NoneAndAfter_2() { List<Command> commands = toList(createCommand("a"), createCommand("b", AFTER, "a")); reverseList(commands); sortCommandsByAnchor(commands); int index = 0; assertEquals("a", commands.get(index++).getCommandID()); assertEquals("b", commands.get(index++).getCommandID()); }
@Test public void testSortByAnchor_NoneAndAfter_3() { List<Command> commands = toList(createCommand("a"), createCommand("b", AFTER, "a"), createCommand("c", AFTER, "b")); reverseList(commands); sortCommandsByAnchor(commands); int index = 0; assertEquals("a", commands.get(index++).getCommandID()); assertEquals("b", commands.get(index++).getCommandID()); assertEquals("c", commands.get(index++).getCommandID()); }
@Test public void testSortByAnchor_NoneAndAfter_5() { List<Command> commands = toList(createCommand("a"), createCommand("b", AFTER, "a"), createCommand("c", AFTER, "b"), createCommand("d", AFTER, "c"), createCommand("e", AFTER, "d")); reverseList(commands); sortCommandsByAnchor(commands); int index = 0; assertEquals("a", commands.get(index++).getCommandID()); assertEquals("b", commands.get(index++).getCommandID()); assertEquals("c", commands.get(index++).getCommandID()); assertEquals("d", commands.get(index++).getCommandID()); assertEquals("e", commands.get(index++).getCommandID()); }
@Test public void testSortByAnchor_AllKinds_4() { List<Command> commands = toList(createCommand("a", FIRST), createCommand("b", AFTER, "a"), createCommand("c", BEFORE, "d"), createCommand("d", LAST, null)); reverseList(commands); sortCommandsByAnchor(commands); int index = 0; assertEquals("a", commands.get(index++).getCommandID()); assertEquals("b", commands.get(index++).getCommandID()); assertEquals("c", commands.get(index++).getCommandID()); assertEquals("d", commands.get(index++).getCommandID()); }
@Test public void testSortByAnchor_AllKinds_12() { List<Command> commands = toList(createCommand("a", FIRST), createCommand("b", FIRST), createCommand("c", FIRST), createCommand("d", AFTER, "c"), createCommand("e", AFTER, "c"), createCommand("f", AFTER, "c"), createCommand("g", BEFORE, "j"), createCommand("h", BEFORE, "j"), createCommand("i", BEFORE, "j"), createCommand("j", LAST), createCommand("k", LAST), createCommand("l", LAST)); reverseList(commands); sortCommandsByAnchor(commands); int index = 0; assertEquals("a", commands.get(index++).getCommandID()); assertEquals("b", commands.get(index++).getCommandID()); assertEquals("c", commands.get(index++).getCommandID()); assertEquals("d", commands.get(index++).getCommandID()); assertEquals("e", commands.get(index++).getCommandID()); assertEquals("f", commands.get(index++).getCommandID()); assertEquals("g", commands.get(index++).getCommandID()); assertEquals("h", commands.get(index++).getCommandID()); assertEquals("i", commands.get(index++).getCommandID()); assertEquals("j", commands.get(index++).getCommandID()); assertEquals("k", commands.get(index++).getCommandID()); assertEquals("l", commands.get(index++).getCommandID()); }