more cleaning and moving/renaming things

This commit is contained in:
Ben Fry
2023-01-14 17:51:05 -05:00
parent 3b210c1c31
commit ce39bb543d
5 changed files with 87 additions and 69 deletions
@@ -48,14 +48,14 @@ public class ContributionListing {
static final String LOCAL_FILENAME = "contribs.txt";
/** Location of the listing file on disk, will be read and written. */
File listingFile;
boolean listDownloaded;
private File listingFile;
private boolean listDownloaded;
// boolean listDownloadFailed;
ReentrantLock downloadingLock;
private ReentrantLock downloadingLock;
final Set<AvailableContribution> availableContribs;
Map<String, Contribution> librariesByImportHeader;
Set<Contribution> allContribs;
private Map<String, Contribution> importToLibrary;
private Set<Contribution> allContribs;
Set<ListPanel> listPanels;
@@ -63,7 +63,7 @@ public class ContributionListing {
private ContributionListing() {
listPanels = new HashSet<>();
availableContribs = new HashSet<>();
librariesByImportHeader = new HashMap<>();
importToLibrary = new HashMap<>();
allContribs = ConcurrentHashMap.newKeySet();
downloadingLock = new ReentrantLock();
@@ -88,6 +88,11 @@ public class ContributionListing {
}
static protected Set<Contribution> getAllContribs() {
return getInstance().allContribs;
}
/**
* Update the list of contribs with entries for what is installed.
* If it matches an entry from contribs.txt, replace that entry.
@@ -131,13 +136,18 @@ public class ContributionListing {
}
// This could just be a remove followed by an add, but contributionChanged()
// is a little weird, so that should be cleaned up first [fry 230114]
protected void replaceContribution(Contribution oldContrib, Contribution newContrib) {
if (oldContrib != null && newContrib != null) {
if (oldContrib.getImports() != null) {
for (String importName : oldContrib.getImports()) {
if (getLibrariesByImportHeader().containsKey(importName)) {
getLibrariesByImportHeader().put(importName, newContrib);
}
importToLibrary.remove(importName);
}
}
if (newContrib.getImports() != null) {
for (String importName : newContrib.getImports()) {
importToLibrary.put(importName, newContrib);
}
}
allContribs.remove(oldContrib);
@@ -153,7 +163,7 @@ public class ContributionListing {
private void addContribution(Contribution contribution) {
if (contribution.getImports() != null) {
for (String importName : contribution.getImports()) {
getLibrariesByImportHeader().put(importName, contribution);
getLibraryImportMap().put(importName, contribution);
}
}
allContribs.add(contribution);
@@ -167,7 +177,7 @@ public class ContributionListing {
protected void removeContribution(Contribution contribution) {
if (contribution.getImports() != null) {
for (String importName : contribution.getImports()) {
getLibrariesByImportHeader().remove(importName);
getLibraryImportMap().remove(importName);
}
}
allContribs.remove(contribution);
@@ -363,8 +373,10 @@ public class ContributionListing {
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
/** Used by JavaEditor to auto-import */
public Map<String, Contribution> getLibrariesByImportHeader() {
return librariesByImportHeader;
/**
* Used by JavaEditor to auto-import. Not known to be used by other Modes.
*/
public Map<String, Contribution> getLibraryImportMap() {
return importToLibrary;
}
}
@@ -277,7 +277,7 @@ public class ContributionTab extends JPanel {
private Set<String> listCategories() {
Set<String> categories = new HashSet<>();
for (Contribution c : ContributionListing.getInstance().allContribs) {
for (Contribution c : ContributionListing.getAllContribs()) {
if (filter.matches(c)) {
for (String category : c.getCategories()) {
categories.add(category);
@@ -515,7 +515,7 @@ public class ListPanel extends JPanel implements Scrollable {
@Override
public int getRowCount() {
return ContributionListing.getInstance().allContribs.size() + (sectionsEnabled ? 4 : 0);
return ContributionListing.getAllContribs().size() + (sectionsEnabled ? 4 : 0);
}
@Override
@@ -539,7 +539,7 @@ public class ListPanel extends JPanel implements Scrollable {
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
final Set<Contribution> allContribs =
ContributionListing.getInstance().allContribs;
ContributionListing.getAllContribs();
if (rowIndex >= allContribs.size()) {
return sections[rowIndex - allContribs.size()];
}
@@ -603,7 +603,7 @@ public class ListPanel extends JPanel implements Scrollable {
}
private boolean includeSection(SectionHeaderContribution section) {
return ContributionListing.getInstance().allContribs.stream()
return ContributionListing.getAllContribs().stream()
.filter(contribution -> contribution.getType() == section.getType())
.anyMatch(this::includeContribution);
}
@@ -315,67 +315,20 @@ public abstract class LocalContribution extends Contribution {
private void remove(Base base, ContribProgress pm, StatusPanel status, boolean updating) {
pm.startTask("Removing");
boolean doBackup = Preferences.getBoolean("contribution.backup.on_remove");
if (getType() == ContributionType.MODE) {
//Set<Sketch> sketches = new HashSet<>();
List<Editor> editors = new ArrayList<>(); // might be nice to be in order
ModeContribution m = (ModeContribution) this;
for (Editor editor : base.getEditors()) {
if (editor.getMode().equals(m.getMode())) {
Sketch sketch = editor.getSketch();
if (sketch.isModified()) {
pm.cancel();
editor.toFront();
Messages.showMessage("Save Sketch",
"Please first save “" + sketch.getName() + "”.");
return;
} else {
// Keep track of open Editor windows using this Mode
//sketchMainList.add(sketch.getMainPath());
//sketches.add(sketch);
editors.add(editor);
}
}
}
// Close any open Editor windows that were using this Mode,
// and if updating, build up a list of paths for the sketches
// so that we can dispose of the Editor objects.
//StringList sketchPathList = new StringList();
for (Editor editor : editors) {
//sketchPathList.append(editor.getSketch().getMainPath());
StatusDetail.storeSketchPath(editor.getSketch().getMainPath());
base.handleClose(editor, true);
}
editors.clear();
m.clearClassLoader(base);
//StatusPanelDetail.storeSketches(sketchPathList);
/*
if (!removeMode(base, updating)) {
pm.cancel();
Messages.showMessage("Mode Manager",
"Please save your Sketch and change the Mode of all Editor\n" +
"windows that have " + name + " as the active Mode.");
return;
*/
if (!updating) {
// Notify the Base in case this is the current Mode
base.modeRemoved(m.getMode());
// If that was the last Editor window, and we deleted its Mode,
// open a fresh window using the default Mode.
if (base.getEditors().size() == 0) {
base.handleNew();
}
}
}
if (getType() == ContributionType.TOOL) {
} else if (getType() == ContributionType.TOOL) {
// menu will be rebuilt below with the refreshContribs() call
base.clearToolMenus();
((ToolContribution) this).clearClassLoader();
}
boolean success;
boolean doBackup = Preferences.getBoolean("contribution.backup.on_remove");
if (doBackup) {
success = backup(true, status);
} else {
@@ -451,6 +404,59 @@ public abstract class LocalContribution extends Contribution {
}
private boolean removeMode(Base base, boolean updating) {
List<Editor> editors = new ArrayList<>(); // might be nice to be in order
ModeContribution m = (ModeContribution) this;
for (Editor editor : base.getEditors()) {
if (editor.getMode().equals(m.getMode())) {
Sketch sketch = editor.getSketch();
if (sketch.isModified()) {
editor.toFront();
Messages.showMessage("Save Sketch",
"Please first save “" + sketch.getName() + "”.");
return false;
} else {
// Keep track of open Editor windows using this Mode
//sketchMainList.add(sketch.getMainPath());
//sketches.add(sketch);
editors.add(editor);
}
}
}
// Close any open Editor windows that were using this Mode,
// and if updating, build up a list of paths for the sketches
// so that we can dispose of the Editor objects.
//StringList sketchPathList = new StringList();
for (Editor editor : editors) {
//sketchPathList.append(editor.getSketch().getMainPath());
StatusDetail.storeSketchPath(editor.getSketch().getMainPath());
base.handleClose(editor, true);
}
editors.clear();
m.clearClassLoader(base);
//StatusPanelDetail.storeSketches(sketchPathList);
/*
pm.cancel();
Messages.showMessage("Mode Manager",
"Please save your Sketch and change the Mode of all Editor\n" +
"windows that have " + name + " as the active Mode.");
return;
*/
if (!updating) {
// Notify the Base in case this is the current Mode
base.modeRemoved(m.getMode());
// If that was the last Editor window, and we deleted its Mode,
// open a fresh window using the default Mode.
if (base.getEditors().size() == 0) {
base.handleNew();
}
}
return true;
}
public File getFolder() {
return folder;
}
@@ -1238,7 +1238,7 @@ public class JavaEditor extends Editor {
*/
private List<AvailableContribution> getNotInstalledAvailableLibs(List<String> importHeadersList) {
Map<String, Contribution> importMap =
ContributionListing.getInstance().getLibrariesByImportHeader();
ContributionListing.getInstance().getLibraryImportMap();
List<AvailableContribution> libList = new ArrayList<>();
for (String importHeaders : importHeadersList) {
int dot = importHeaders.lastIndexOf('.');