From ce39bb543dd325d96f26250f0962b2d105ee6ec2 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Sat, 14 Jan 2023 17:51:05 -0500 Subject: [PATCH] more cleaning and moving/renaming things --- .../app/contrib/ContributionListing.java | 40 ++++--- .../app/contrib/ContributionTab.java | 2 +- app/src/processing/app/contrib/ListPanel.java | 6 +- .../app/contrib/LocalContribution.java | 106 +++++++++--------- java/src/processing/mode/java/JavaEditor.java | 2 +- 5 files changed, 87 insertions(+), 69 deletions(-) diff --git a/app/src/processing/app/contrib/ContributionListing.java b/app/src/processing/app/contrib/ContributionListing.java index d00bbb833..d9cc1d1e5 100644 --- a/app/src/processing/app/contrib/ContributionListing.java +++ b/app/src/processing/app/contrib/ContributionListing.java @@ -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 availableContribs; - Map librariesByImportHeader; - Set allContribs; + private Map importToLibrary; + private Set allContribs; Set 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 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 getLibrariesByImportHeader() { - return librariesByImportHeader; + /** + * Used by JavaEditor to auto-import. Not known to be used by other Modes. + */ + public Map getLibraryImportMap() { + return importToLibrary; } } diff --git a/app/src/processing/app/contrib/ContributionTab.java b/app/src/processing/app/contrib/ContributionTab.java index dbbf4e8cb..5f87188a2 100644 --- a/app/src/processing/app/contrib/ContributionTab.java +++ b/app/src/processing/app/contrib/ContributionTab.java @@ -277,7 +277,7 @@ public class ContributionTab extends JPanel { private Set listCategories() { Set 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); diff --git a/app/src/processing/app/contrib/ListPanel.java b/app/src/processing/app/contrib/ListPanel.java index 1d365c62c..bbf54989e 100644 --- a/app/src/processing/app/contrib/ListPanel.java +++ b/app/src/processing/app/contrib/ListPanel.java @@ -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 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); } diff --git a/app/src/processing/app/contrib/LocalContribution.java b/app/src/processing/app/contrib/LocalContribution.java index 38bcfd1af..5a078e33d 100644 --- a/app/src/processing/app/contrib/LocalContribution.java +++ b/app/src/processing/app/contrib/LocalContribution.java @@ -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 sketches = new HashSet<>(); - List 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 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; } diff --git a/java/src/processing/mode/java/JavaEditor.java b/java/src/processing/mode/java/JavaEditor.java index fe215e723..48f63e774 100644 --- a/java/src/processing/mode/java/JavaEditor.java +++ b/java/src/processing/mode/java/JavaEditor.java @@ -1238,7 +1238,7 @@ public class JavaEditor extends Editor { */ private List getNotInstalledAvailableLibs(List importHeadersList) { Map importMap = - ContributionListing.getInstance().getLibrariesByImportHeader(); + ContributionListing.getInstance().getLibraryImportMap(); List libList = new ArrayList<>(); for (String importHeaders : importHeadersList) { int dot = importHeaders.lastIndexOf('.');