Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouples AssetPanel from Resource window so that each one can browse independently #1266

Merged
merged 6 commits into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
Expand All @@ -55,7 +56,9 @@
import net.rptools.maptool.client.AppPreferences;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.MapToolUtil;
import net.rptools.maptool.client.ui.assetpanel.AssetDirectory;
import net.rptools.maptool.client.ui.assetpanel.AssetPanel;
import net.rptools.maptool.client.ui.assetpanel.AssetPanelModel;
import net.rptools.maptool.model.Asset;
import net.rptools.maptool.model.AssetManager;
import net.rptools.maptool.model.Grid;
Expand Down Expand Up @@ -159,11 +162,14 @@ public void actionPerformed(ActionEvent e) {
});
// Color picker
paintChooser = new PaintChooser();
AssetPanelModel model = new AssetPanelModel();
Set<File> assetRootList = AppPreferences.getAssetRoots();
for (File file : assetRootList) {
model.addRootGroup(new AssetDirectory(file, AppConstants.IMAGE_FILE_FILTER));
}

TextureChooserPanel textureChooserPanel =
new TextureChooserPanel(
paintChooser,
MapTool.getFrame().getAssetPanel().getModel(),
"mapPropertiesTextureChooser");
new TextureChooserPanel(paintChooser, model, "mapPropertiesTextureChooser");
paintChooser.addPaintChooser(textureChooserPanel);
paintChooser.setPreferredSize(new Dimension(450, 400));
mapSelectorDialog = new MapSelectorDialog();
Expand Down Expand Up @@ -623,11 +629,14 @@ public Asset chooseAsset() {
}

private JComponent createImageExplorerPanel() {
AssetPanelModel model = new AssetPanelModel();
Set<File> assetRootList = AppPreferences.getAssetRoots();
for (File file : assetRootList) {
model.addRootGroup(new AssetDirectory(file, AppConstants.IMAGE_FILE_FILTER));
}
final AssetPanel assetPanel =
new AssetPanel(
"mapPropertiesImageExplorer",
MapTool.getFrame().getAssetPanel().getModel(),
JSplitPane.HORIZONTAL_SPLIT);
new AssetPanel("mapPropertiesImageExplorer", model, JSplitPane.HORIZONTAL_SPLIT);

assetPanel.addImageSelectionListener(
new SelectionListener() {
public void selectionPerformed(List<Object> selectedList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ public class AssetPanel extends JComponent {
private Timer updateFilterTimer;
private JProgressBar imagePanelProgressBar;

public boolean isLimitReached() {
return limitReached;
}

public void setLimitReached(boolean limitReached) {
this.limitReached = limitReached;
}

private boolean limitReached = false;

public AssetPanel(String controlName) {
this(controlName, new AssetPanelModel());
}
Expand Down Expand Up @@ -390,7 +400,7 @@ public void addAssetRoot(Directory dir) {

public void setDirectory(Directory dir) {
imagePanel.setModel(
new ImageFileImagePanelModel(dir) {
new ImageFileImagePanelModel(dir, this) {
@Override
public Transferable getTransferable(int index) {
// TransferableAsset t = (TransferableAsset) super.getTransferable(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -79,19 +77,22 @@ public class ImageFileImagePanelModel implements ImagePanelModel {
}
}

private final AssetPanel assetPanel;

private Directory dir;
private static String filter;
private boolean global;
private static List<File> fileList = new ArrayList<File>();
private List<File> fileList = new ArrayList<File>();
private List<Directory> subDirList;

private static int pagesProcessed = 0;
private static PdfExtractor extractorSwingWorker;
private static boolean pdfExtractIsRunning = false;
private static ScheduledExecutorService extractThreadPool;

public ImageFileImagePanelModel(Directory dir) {
public ImageFileImagePanelModel(Directory dir, AssetPanel assetPanel) {
this.dir = dir;
this.assetPanel = assetPanel;
}

public void rescan(Directory dir) {
Expand Down Expand Up @@ -395,15 +396,15 @@ private PdfExtractor(boolean forceRescan) {

pageCount = extractor.getPageCount();
extractThreadPool = Executors.newScheduledThreadPool(numThreads);
MapTool.getFrame().getAssetPanel().setImagePanelProgressMax(pageCount);
assetPanel.setImagePanelProgressMax(pageCount);
}

@Override
protected Void doInBackground() throws Exception {
try {
// 0 page count means it's already been processed (or PDF if empty)
if (pageCount > 0 || forceRescan) {
MapTool.getFrame().getAssetPanel().showImagePanelProgress(true);
assetPanel.showImagePanelProgress(true);

for (int pageNumber = 1; pageNumber < pageCount + 1; pageNumber++) {
ExtractImagesTask task = new ExtractImagesTask(pageNumber, pageCount, dir, forceRescan);
Expand Down Expand Up @@ -470,13 +471,13 @@ private void updatePdfProgress(int progress, File tempFile) {
if (progress == 0) {
pagesProcessed = 0;
fileListCleanup(new PdfAsDirectory(tempFile, AppConstants.IMAGE_FILE_FILTER));
MapTool.getFrame().getAssetPanel().showImagePanelProgress(false);
assetPanel.showImagePanelProgress(false);
} else {
fileListCleanup();
}

MapTool.getFrame().getAssetPanel().setImagePanelProgress(pagesProcessed++);
MapTool.getFrame().getAssetPanel().updateImagePanel();
assetPanel.setImagePanelProgress(pagesProcessed++);
assetPanel.updateImagePanel();
}

private void fileListCleanup(Directory dir) {
Expand All @@ -491,11 +492,10 @@ private void fileListCleanup(Directory dir) {
}

private void fileListCleanup() {
Set<File> tempSet = new HashSet<File>();
tempSet.addAll(fileList);
Set<File> tempSet = new HashSet<File>(fileList);
fileList.clear();
fileList.addAll(tempSet);
Collections.sort(fileList, filenameComparator);
fileList.sort(filenameComparator);
}

private void refreshHeroLab() {
Expand All @@ -513,16 +513,11 @@ private void refreshHeroLab() {
fileList.addAll(heroLabFile.extractAllCharacters(portfolioChanged));

if (filter != null && filter.length() > 0) {
for (ListIterator<File> iter = fileList.listIterator(); iter.hasNext(); ) {
File file = iter.next();
if (!file.getName().toUpperCase().contains(filter)) {
iter.remove();
}
}
fileList.removeIf(file -> !file.getName().toUpperCase().contains(filter));
}

Collections.sort(fileList, filenameComparator);
MapTool.getFrame().getAssetPanel().updateGlobalSearchLabel(fileList.size());
fileList.sort(filenameComparator);
assetPanel.updateGlobalSearchLabel(fileList.size());
}

/**
Expand All @@ -537,7 +532,7 @@ private void refresh() {
fileList = new ArrayList<File>();
subDirList = new ArrayList<Directory>();

if (global == true && filter != null && filter.length() > 0) {
if (global && filter != null && filter.length() > 0) {
// FIXME populate fileList from all filenames in the library
// Use the AssetManager class, something akin to searchForImageReferences()
// but I don't want to do a search; I want to use the existing cached results.
Expand All @@ -553,20 +548,16 @@ private void refresh() {
fileList.addAll(dir.getFiles());

// Filter current directory of files
for (ListIterator<File> iter = fileList.listIterator(); iter.hasNext(); ) {
File file = iter.next();
if (!file.getName().toUpperCase().contains(filter)) {
iter.remove();
}
}
fileList.removeIf(file -> !file.getName().toUpperCase().contains(filter));

// Now search remaining subdirectories and filter as it goes.
// Stop at any time if it reaches SEARCH_LIMIT
subDirList.addAll(dir.getSubDirs());
ListFilesSwingWorker.reset();
assetPanel.setLimitReached(false);

for (Directory folder : subDirList) {
ListFilesSwingWorker workerThread = new ListFilesSwingWorker(folder.getPath());
ListFilesSwingWorker workerThread =
new ListFilesSwingWorker(folder.getPath(), assetPanel);
workerThread.execute();
}

Expand All @@ -581,18 +572,13 @@ private void refresh() {
}

if (filter != null && filter.length() > 0) {
for (ListIterator<File> iter = fileList.listIterator(); iter.hasNext(); ) {
File file = iter.next();
if (!file.getName().toUpperCase().contains(filter)) {
iter.remove();
}
}
fileList.removeIf(file -> !file.getName().toUpperCase().contains(filter));
}
}

Collections.sort(fileList, filenameComparator);
fileList.sort(filenameComparator);
try {
MapTool.getFrame().getAssetPanel().updateGlobalSearchLabel(fileList.size());
assetPanel.updateGlobalSearchLabel(fileList.size());
} catch (NullPointerException e) {
// This currently throws a NPE if the frame was not finished initializing when runs. For now,
// lets log a message and continue.
Expand All @@ -602,29 +588,26 @@ private void refresh() {
}
}

private static class ListFilesSwingWorker extends SwingWorker<Void, Integer> {
private class ListFilesSwingWorker extends SwingWorker<Void, Integer> {
private final File folderPath;
private static boolean limitReached = false;

private ListFilesSwingWorker(File path) {
folderPath = path;
}
private AssetPanel assetPanel;

private static void reset() {
limitReached = false;
private ListFilesSwingWorker(File path, AssetPanel assetPanel) {
this.folderPath = path;
this.assetPanel = assetPanel;
}

@Override
protected Void doInBackground() throws Exception {
MapTool.getFrame().getAssetPanel().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
assetPanel.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
listFilesInSubDirectories();
publish(fileList.size());
return null;
}

@Override
protected void process(List<Integer> integers) {
MapTool.getFrame().getAssetPanel().updateGlobalSearchLabel(fileList.size());
assetPanel.updateGlobalSearchLabel(fileList.size());
}

@Override
Expand All @@ -640,9 +623,7 @@ protected void done() {
// Jamz: Causes cursor to flicker due to multiple threads running. Needs a supervisior thread
// to
// watch over all threads. Pain to code, leave for later? Remove cursor changes?
MapTool.getFrame()
.getAssetPanel()
.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
assetPanel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}

/*
Expand All @@ -669,21 +650,21 @@ public boolean accept(File dir, String name) {
});

for (final File fileEntry : files) {
if (fileEntry.getName().toUpperCase().contains(filter) && !limitReached)
if (fileEntry.getName().toUpperCase().contains(filter) && !assetPanel.isLimitReached())
fileList.add(fileEntry);
if (limitReached()) break;
}

for (final File fileEntry : folders) {
if (limitReached()) break;
ListFilesSwingWorker workerThread = new ListFilesSwingWorker(fileEntry);
ListFilesSwingWorker workerThread = new ListFilesSwingWorker(fileEntry, assetPanel);
workerThread.execute();
}
}

private boolean limitReached() {
if (fileList.size() > AppConstants.ASSET_SEARCH_LIMIT) limitReached = true;
return limitReached;
if (fileList.size() > AppConstants.ASSET_SEARCH_LIMIT) assetPanel.setLimitReached(true);
return assetPanel.isLimitReached();
}
}

Expand Down