Skip to content

Commit c259869

Browse files
committed
Undo button to remove deletion flag for tools
1 parent b0cdbbc commit c259869

4 files changed

Lines changed: 94 additions & 57 deletions

File tree

app/src/processing/app/Base.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public class Base {
7777
Preferences preferencesFrame;
7878

7979
// A single instance of the library manager window
80-
ContributionManager libraryManagerFrame;
80+
ContributionManager contributionManagerFrame;
8181

8282
// set to true after the first time the menu is built.
8383
// so that the errors while building don't show up again.
@@ -232,12 +232,12 @@ public Base(String[] args) {
232232
for (ToolContribution contrib : ToolContribution
233233
.list(getSketchbookToolsFolder(), false)) {
234234

235-
if (new File(contrib.getFolder(), ContributionManager.DELETION_FLAG).exists()) {
235+
if (ContributionManager.isFlaggedForDeletion(contrib)) {
236236
removeDir(contrib.getFolder());
237237
}
238238
}
239239

240-
libraryManagerFrame = new ContributionManager();
240+
contributionManagerFrame = new ContributionManager();
241241

242242
// Make sure ThinkDifferent has library examples too
243243
defaultMode.rebuildLibraryList();
@@ -1416,20 +1416,20 @@ public void handlePrefs() {
14161416
* Show the library installer window.
14171417
*/
14181418
public void handleOpenContributionManager() {
1419-
libraryManagerFrame.showFrame(activeEditor);
1419+
contributionManagerFrame.showFrame(activeEditor);
14201420
}
14211421

14221422
public void handleShowUpdates() {
1423-
libraryManagerFrame.showFrame(activeEditor);
1424-
libraryManagerFrame.setFilterText("has:updates");
1423+
contributionManagerFrame.showFrame(activeEditor);
1424+
contributionManagerFrame.setFilterText("has:updates");
14251425
}
14261426

14271427
/**
14281428
* Installed the libraries in the given file after a confirmation from the
14291429
* user. Returns the number of libraries installed.
14301430
*/
14311431
public boolean handleConfirmAndInstallLibrary(File libFile) {
1432-
return libraryManagerFrame.confirmAndInstallLibrary(activeEditor, libFile) != null;
1432+
return contributionManagerFrame.confirmAndInstallLibrary(activeEditor, libFile) != null;
14331433
}
14341434

14351435
// ...................................................................

app/src/processing/app/ContributionListPanel.java

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@
4242

4343
public class ContributionListPanel extends JPanel implements Scrollable, ContributionChangeListener {
4444

45-
static public final String DELETION_MESSAGE = "<html><body><i>This tool has "
45+
static public final String DELETION_MESSAGE = "<i>This tool has "
4646
+ "been flagged for deletion. Restart all instances of the editor to "
47-
+ "finalize the removal process.</i></body></html>";
47+
+ "finalize the removal process.</i>";
4848

4949
static public final String INSTALL_FAILURE_TITLE = "Install Failed";
5050

@@ -354,7 +354,7 @@ private class ContributionPanel extends JPanel {
354354

355355
/** Should only be set through setContribution(), otherwise UI components
356356
* will not be updated. */
357-
Contribution info;
357+
Contribution contrib;
358358

359359
boolean alreadySelected;
360360

@@ -386,6 +386,8 @@ private class ContributionPanel extends JPanel {
386386

387387
private ActionListener installActionListener;
388388

389+
private ActionListener undoActionListener;
390+
389391
private ContributionPanel() {
390392

391393
htmlPanes = new HashSet<JTextPane>();
@@ -406,35 +408,37 @@ public void hyperlinkUpdate(HyperlinkEvent e) {
406408
};
407409

408410
installActionListener = new ActionListener() {
409-
public void actionPerformed(ActionEvent arg) {
410-
if (info instanceof AdvertisedContribution) {
411-
installContribution((AdvertisedContribution) info);
411+
public void actionPerformed(ActionEvent e) {
412+
if (contrib instanceof AdvertisedContribution) {
413+
installContribution((AdvertisedContribution) contrib);
414+
}
415+
}
416+
};
417+
418+
undoActionListener = new ActionListener() {
419+
public void actionPerformed(ActionEvent e) {
420+
if (contrib instanceof InstalledContribution) {
421+
InstalledContribution installed = (InstalledContribution) contrib;
422+
ContributionManager.removeFlagForDeletion(installed);
423+
contribManager.getListing().replaceContribution(contrib, contrib);
412424
}
413425
}
414426
};
415427

416428
removeActionListener = new ActionListener() {
417429
public void actionPerformed(ActionEvent arg) {
418-
if (info.isInstalled() && info instanceof InstalledContribution) {
430+
if (contrib.isInstalled() && contrib instanceof InstalledContribution) {
419431
updateButton.setEnabled(false);
420432
installOrRemoveButton.setEnabled(false);
421433

422434
installProgressBar.setVisible(true);
423-
contribManager.removeContribution((InstalledContribution) info,
435+
contribManager.removeContribution((InstalledContribution) contrib,
424436
new JProgressMonitor(installProgressBar) {
425437

426438
public void finishedAction() {
427439
// Finished uninstalling the library
428440
resetInstallProgressBarState();
429-
430-
switch (info.getType()) {
431-
case LIBRARY:
432-
case LIBRARY_COMPILATION:
433-
installOrRemoveButton.setEnabled(true);
434-
break;
435-
case TOOL:
436-
descriptionText.setText(DELETION_MESSAGE);
437-
}
441+
installOrRemoveButton.setEnabled(true);
438442
}
439443
});
440444
}
@@ -515,7 +519,7 @@ private void addPaneComponents() {
515519

516520
iconArea = new JPanel() {
517521
protected void paintComponent(Graphics g) {
518-
Image icon = contribManager.getContributionIcon(info.getType());
522+
Image icon = contribManager.getContributionIcon(contrib.getType());
519523
if (icon != null) {
520524
g.drawImage(icon, 0, 0, this);
521525
}
@@ -580,7 +584,7 @@ protected void paintComponent(Graphics g) {
580584
public void actionPerformed(ActionEvent e) {
581585
updateButton.setEnabled(false);
582586
// XXX: There is a bug here. The advertised library will be 'replaced' instead
583-
installContribution((AdvertisedContribution) contribManager.contribListing.getAdvertisedContribution(info));
587+
installContribution((AdvertisedContribution) contribManager.contribListing.getAdvertisedContribution(contrib));
584588
}
585589
});
586590
descriptionPanel.add(updateButton, c);
@@ -648,36 +652,46 @@ private void setExpandListener(Component component,
648652
}
649653
}
650654

651-
public void setContribution(Contribution info) {
655+
public void setContribution(Contribution contrib) {
652656

653-
this.info = info;
657+
this.contrib = contrib;
654658

655659
setFocusable(true);
656660

657661
StringBuilder nameText = new StringBuilder();
658662
nameText.append("<html><body><b>");
659-
if (info.getUrl() == null) {
660-
nameText.append(info.getName());
663+
if (contrib.getUrl() == null) {
664+
nameText.append(contrib.getName());
661665
} else {
662-
nameText.append("<a href=\"" + info.getUrl() + "\">" + info.getName() + "</a>");
666+
nameText.append("<a href=\"" + contrib.getUrl() + "\">" + contrib.getName() + "</a>");
663667
}
664668
nameText.append("</b>");
665-
nameText.append(createAuthorString(info.getAuthorList()));
669+
nameText.append(createAuthorString(contrib.getAuthorList()));
666670
nameText.append("</body></html>");
667671
headerText.setText(nameText.toString());
668672

669-
categoryLabel.setText("[" + info.getCategory() + "]");
673+
categoryLabel.setText("[" + contrib.getCategory() + "]");
670674

671675
StringBuilder description = new StringBuilder();
672676
description.append("<html><body>");
673-
if (info.getSentence() != null)
674-
description.append(info.getSentence());
677+
678+
if (ContributionManager.isFlaggedForDeletion(contrib)) {
679+
description.append(DELETION_MESSAGE);
680+
installOrRemoveButton.setText("Undo");
681+
installOrRemoveButton.addActionListener(new ActionListener() {
682+
public void actionPerformed(ActionEvent e) {
683+
684+
}
685+
});
686+
} else if (contrib.getSentence() != null) {
687+
description.append(contrib.getSentence());
688+
}
675689

676690
description.append("</body></html>");
677691
descriptionText.setText(description.toString());
678692
setAlignment(descriptionText, StyleConstants.ALIGN_JUSTIFIED);
679693

680-
if (contribManager.getListing().hasUpdates(info)) {
694+
if (contribManager.getListing().hasUpdates(contrib)) {
681695
StringBuilder versionText = new StringBuilder();
682696
versionText.append("<html><body><i>");
683697
versionText.append("New version available!");
@@ -690,12 +704,21 @@ public void setContribution(Contribution info) {
690704
updateButton.setVisible(false);
691705
}
692706

693-
if (info.isInstalled()) {
694-
installOrRemoveButton.removeActionListener(installActionListener);
695-
installOrRemoveButton.addActionListener(removeActionListener);
696-
installOrRemoveButton.setText("Remove");
707+
if (contrib.isInstalled()) {
708+
if (ContributionManager.isFlaggedForDeletion(contrib)) {
709+
installOrRemoveButton.removeActionListener(installActionListener);
710+
installOrRemoveButton.removeActionListener(removeActionListener);
711+
installOrRemoveButton.addActionListener(undoActionListener);
712+
installOrRemoveButton.setText("Undo");
713+
} else {
714+
installOrRemoveButton.removeActionListener(installActionListener);
715+
installOrRemoveButton.removeActionListener(undoActionListener);
716+
installOrRemoveButton.addActionListener(removeActionListener);
717+
installOrRemoveButton.setText("Remove");
718+
}
697719
} else {
698720
installOrRemoveButton.removeActionListener(removeActionListener);
721+
installOrRemoveButton.removeActionListener(undoActionListener);
699722
installOrRemoveButton.addActionListener(installActionListener);
700723
installOrRemoveButton.setText("Install");
701724
}
@@ -816,7 +839,7 @@ public void setSelected(boolean isSelected) {
816839
enableHyperlinks = alreadySelected;
817840

818841
updateButton.setVisible(isSelected()
819-
&& contribManager.getListing().hasUpdates(info));
842+
&& contribManager.getListing().hasUpdates(contrib));
820843
installOrRemoveButton.setVisible(isSelected());
821844

822845
for (JTextPane textPane : htmlPanes) {

app/src/processing/app/ContributionManager.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,8 @@ public class ContributionManager {
4444

4545
static public final String DELETION_FLAG = "flagged_for_deletion";
4646

47-
static private final String DRAG_AND_DROP_SECONDARY =
48-
".plb files usually contain contributed libraries for <br>" +
49-
"Processing. Click “Yes” to install this library to your<br>" +
50-
"sketchbook. If you wish to add this file to your<br>" +
51-
"sketch instead, click “No” and use <i>Sketch &gt;<br>Add File...</i>";
47+
static private final String DOUBLE_CLICK_SECONDARY =
48+
"Click “Yes” to install this library to your sketchbook...";
5249

5350
static private final String DISCOVERY_ERROR_TITLE = "Trouble discovering libraries";
5451

@@ -358,14 +355,8 @@ public void run() {
358355
case TOOL:
359356
case MODE:
360357
if (backupContribution(contribution, false)) {
361-
try {
362-
File d = new File(contribution.getFolder(), DELETION_FLAG);
363-
364-
// Only returns false if the file already exists, in which case
365-
// the contribution would have been deleted at start-up anyway
366-
d.createNewFile();
367-
} catch (IOException e) {
368-
e.printStackTrace();
358+
if (flagForDeletion(contribution)) {
359+
contribListing.replaceContribution(contribution, contribution);
369360
}
370361
}
371362
}
@@ -469,7 +460,7 @@ public Library confirmAndInstallLibrary(Editor editor, File libFile) {
469460

470461
int result = Base.showYesNoQuestion(this.editor, "Install",
471462
"Install libraries from " + libFile.getName() + "?",
472-
DRAG_AND_DROP_SECONDARY);
463+
DOUBLE_CLICK_SECONDARY);
473464

474465
if (result == JOptionPane.YES_OPTION) {
475466
return installLibrary(libFile, true);
@@ -975,6 +966,29 @@ public ContributionListing getListing() {
975966
return contribListing;
976967
}
977968

969+
static public boolean flagForDeletion(InstalledContribution contrib) {
970+
// Only returns false if the file already exists, so we can
971+
// ignore the return value.
972+
try {
973+
new File(contrib.getFolder(), DELETION_FLAG).createNewFile();
974+
return true;
975+
} catch (IOException e) {
976+
return false;
977+
}
978+
}
979+
980+
static public boolean removeFlagForDeletion(InstalledContribution contrib) {
981+
return new File(contrib.getFolder(), DELETION_FLAG).delete();
982+
}
983+
984+
static public boolean isFlaggedForDeletion(Contribution contrib) {
985+
if (contrib instanceof InstalledContribution) {
986+
InstalledContribution installed = (InstalledContribution) contrib;
987+
return new File(installed.getFolder(), DELETION_FLAG).exists();
988+
}
989+
return false;
990+
}
991+
978992
}
979993

980994
abstract class JProgressMonitor extends AbstractProgressMonitor {

app/src/processing/app/UpdateCheck.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ public void updateCheck() throws Exception {
123123
// Wait for xml file to be downloaded and updates to come in. (this
124124
// should really be handled better).
125125
Thread.sleep(5 * 1000);
126-
if (!base.libraryManagerFrame.hasAlreadyBeenOpened() &&
127-
base.libraryManagerFrame.contribListing.hasUpdates()) {
126+
if (!base.contributionManagerFrame.hasAlreadyBeenOpened() &&
127+
base.contributionManagerFrame.contribListing.hasUpdates()) {
128128
promptToOpenContributionManager();
129129
}
130130
}

0 commit comments

Comments
 (0)