Skip to content

Commit

Permalink
cache tags and size to speed up reload
Browse files Browse the repository at this point in the history
  • Loading branch information
kuronekochomusuke committed Nov 10, 2024
1 parent caf47fb commit 20d5296
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private JPanel createFilter() {
JPanel filterPanel = new JPanel();
filterPanel.setLayout(new BoxLayout(filterPanel, BoxLayout.PAGE_AXIS));

List<String> tags = Arrays.stream(BoardsTagger.Tags.values()).map(BoardsTagger.Tags::getName).distinct().toList();
List<String> tags = Arrays.stream(BoardsTagger.Tags.values()).map(BoardsTagger.Tags::getName).distinct().sorted().toList();
DefaultListModel<String> tagsModel = new DefaultListModel<>();
tagsModel.addAll(tags);
listBoardTags = new JList<>(tagsModel);
Expand Down Expand Up @@ -138,7 +138,7 @@ private JPanel createList() {
JPanel listPanel = new JPanel();
listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.PAGE_AXIS));
boardModel = new BoardTableModel();
boardModel.setData(bc.getBoardPaths().values().stream().toList());
boardModel.setData(bc);
boardTable = new JTable();
boardTable.setName("Board");
ListSelectionModel boardSelModel = boardTable.getSelectionModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import megamek.common.*;
import megamek.common.util.ImageUtil;
import megamek.common.util.fileUtils.MegaMekFile;
import megamek.utilities.BoardClassifier;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
Expand Down Expand Up @@ -72,17 +73,15 @@ public int getPreferredWidth(int col) {
};
}

public void setData(List<String> paths) {
data = paths;
public void setData(BoardClassifier bc) {
data = bc.getBoardPaths().values().stream().toList();;
tags = new ArrayList<>();
size = new ArrayList<>();

for (String path : paths) {
Board board = new Board(16, 17);
board.load(new MegaMekFile(Configuration.boardsDir(), path).getFile());

tags.add(board.getTags().toString());
size.add(board.getWidth() + "x" + board.getHeight());
for (String path : data) {
String key = Configuration.boardsDir() + path;
tags.add(bc.getBoardTags().get(key));
size.add(bc.getBoardWidth().get(key) + "x" + bc.getBoardHeigth().get(key));
}

fireTableDataChanged();
Expand Down
18 changes: 18 additions & 0 deletions megamek/src/megamek/utilities/BoardClassifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class BoardClassifier {

// function that maps full board paths to partial board paths
private Map<String, String> boardPaths = new HashMap<>();
private Map<String, String> boardTags = new HashMap<>();
private Map<String, Integer> boardWidth = new HashMap<>();
private Map<String, Integer> boardHeight = new HashMap<>();

public Map<Tags, List<String>> getBoardsByTag() {
return boardsByTag;
Expand Down Expand Up @@ -79,6 +82,18 @@ public Map<String, String> getBoardPaths() {
return boardPaths;
}

public Map<String, String> getBoardTags() {
return boardTags;
}

public Map<String, Integer> getBoardWidth() {
return boardWidth;
}

public Map<String, Integer> getBoardHeigth() {
return boardHeight;
}

public void setBoardPaths(Map<String, String> boardPaths) {
this.boardPaths = boardPaths;
}
Expand Down Expand Up @@ -146,6 +161,9 @@ private void scanForBoardsInDir(final File boardDir, final String basePath) {
}

getBoardPaths().put(filePath.getPath(), partialBoardPath);
getBoardTags().put(filePath.getPath(), Board.getTags(filePath).toString());
getBoardWidth().put(filePath.getPath(), dimension.width());

Check warning

Code scanning / CodeQL

Dereferenced variable may be null Warning

Variable
dimension
may be null at this access as suggested by
this
null guard.
getBoardHeigth().put(filePath.getPath(), dimension.height());
}
}
}
Expand Down

0 comments on commit 20d5296

Please sign in to comment.