3636import java .awt .*;
3737import java .net .*;
3838
39+ import processing .app .ContributionListing .AdvertisedContribution ;
3940import processing .app .ContributionListing .ContributionChangeListener ;
4041import processing .app .contribution .*;
4142
@@ -48,11 +49,11 @@ public class ContributionListPanel extends JPanel implements Scrollable, Contrib
4849 + "You can still intall this library manually by visiting\n "
4950 + "the library's website." ;
5051
51- ContributionManager contributionManager ;
52+ ContributionManager contribManager ;
5253
5354 JProgressBar setupProgressBar ;
5455
55- TreeMap <Contribution , ContributionPanel > contributionPanelsByInfo ;
56+ TreeMap <Contribution , ContributionPanel > panelByContribution ;
5657
5758 private static HyperlinkListener nullHyperlinkListener = new HyperlinkListener () {
5859
@@ -65,7 +66,7 @@ public void hyperlinkUpdate(HyperlinkEvent e) {
6566 public ContributionListPanel (ContributionManager libraryManager ) {
6667 super ();
6768
68- this .contributionManager = libraryManager ;
69+ this .contribManager = libraryManager ;
6970
7071 setLayout (new GridBagLayout ());
7172 setFocusable (true );
@@ -81,7 +82,8 @@ public ContributionListPanel(ContributionManager libraryManager) {
8182 setBackground (UIManager .getColor ("List.background" ));
8283 }
8384
84- contributionPanelsByInfo = new TreeMap <Contribution , ContributionPanel >();
85+ panelByContribution = new TreeMap <Contribution , ContributionPanel >(
86+ contribManager .getListing ().getComparator ());
8587
8688 GridBagConstraints c = new GridBagConstraints ();
8789 c .fill = GridBagConstraints .HORIZONTAL ;
@@ -96,33 +98,9 @@ public ContributionListPanel(ContributionManager libraryManager) {
9698
9799 }
98100
99- public void contributionAdded (Contribution contributionInfo ) {
100-
101- setupProgressBar .setVisible (false );
102-
103- if (contributionPanelsByInfo .containsKey (contributionInfo )) {
104- return ;
105- }
106-
107- ContributionPanel newPanel = new ContributionPanel ();
108-
109- synchronized (contributionPanelsByInfo ) {
110- contributionPanelsByInfo .put (contributionInfo , newPanel );
111- }
112-
113-
114- if (newPanel != null ) {
115- newPanel .setContribution (contributionInfo );
116-
117- add (newPanel );
118- updatePanelOrdering ();
119- updateColors ();
120- }
121- }
122-
123101 private void updatePanelOrdering () {
124102 int row = 0 ;
125- for (Entry <Contribution , ContributionPanel > entry : contributionPanelsByInfo .entrySet ()) {
103+ for (Entry <Contribution , ContributionPanel > entry : panelByContribution .entrySet ()) {
126104 GridBagConstraints c = new GridBagConstraints ();
127105 c .fill = GridBagConstraints .HORIZONTAL ;
128106 c .weightx = 1 ;
@@ -133,50 +111,87 @@ private void updatePanelOrdering() {
133111 }
134112 }
135113
136- public void contributionRemoved ( Contribution contributionInfo ) {
114+ public void contributionAdded ( final Contribution contribution ) {
137115
138- synchronized (contributionPanelsByInfo ) {
139- ContributionPanel panel = contributionPanelsByInfo .get (contributionInfo );
140- if (panel != null ) {
141- remove (panel );
116+ SwingUtilities .invokeLater (new Runnable () {
117+
118+ public void run () {
119+ setupProgressBar .setVisible (false );
120+
121+ if (panelByContribution .containsKey (contribution )) {
122+ return ;
123+ }
124+
125+ ContributionPanel newPanel = new ContributionPanel ();
126+
127+ synchronized (panelByContribution ) {
128+ panelByContribution .put (contribution , newPanel );
129+ }
130+
131+
132+ if (newPanel != null ) {
133+ newPanel .setContribution (contribution );
134+
135+ add (newPanel );
136+ updatePanelOrdering ();
137+ updateColors ();
138+ }
142139 }
143- }
144-
145- updateUI ();
140+ });
146141 }
147142
148- public void contributionChanged (Contribution oldInfo , Contribution newInfo ) {
149-
150- synchronized (contributionPanelsByInfo ) {
151- ContributionPanel panel = contributionPanelsByInfo .get (oldInfo );
152- contributionPanelsByInfo .remove (oldInfo );
153-
154- panel .setContribution (newInfo );
155- contributionPanelsByInfo .put (newInfo , panel );
156-
157- add (panel );
158- }
159-
160- updatePanelOrdering ();
143+ public void contributionRemoved (final Contribution contribution ) {
144+ SwingUtilities .invokeLater (new Runnable () {
145+ public void run () {
146+ synchronized (panelByContribution ) {
147+ ContributionPanel panel = panelByContribution .get (contribution );
148+ if (panel != null ) {
149+ remove (panel );
150+ }
151+ }
152+
153+ updateUI ();
154+ }
155+ });
156+ }
157+
158+ public void contributionChanged (final Contribution oldContrib ,
159+ final Contribution newContrib ) {
160+
161+ SwingUtilities .invokeLater (new Runnable () {
162+
163+ public void run () {
164+ synchronized (panelByContribution ) {
165+ ContributionPanel panel = panelByContribution .get (oldContrib );
166+ panelByContribution .remove (oldContrib );
167+
168+ panel .setContribution (newContrib );
169+ panelByContribution .put (newContrib , panel );
170+
171+ updatePanelOrdering ();
172+ }
173+ }
174+ });
161175 }
162176
163177 public void filterLibraries (List <Contribution > filteredContributions ) {
164178
165- synchronized (contributionPanelsByInfo ) {
179+ synchronized (panelByContribution ) {
166180
167- Set <Contribution > hiddenPanels = new TreeSet (contributionPanelsByInfo .keySet ());
181+ Set <Contribution > hiddenPanels = new TreeSet (contribManager .getListing ().getComparator ());
182+ hiddenPanels .addAll (panelByContribution .keySet ());
168183
169184 for (Contribution info : filteredContributions ) {
170185
171- ContributionPanel panel = contributionPanelsByInfo .get (info );
186+ ContributionPanel panel = panelByContribution .get (info );
172187 if (panel != null ) {
173188 panel .setVisible (true );
174189 hiddenPanels .remove (info );
175190 }
176191 }
177192
178193 for (Contribution info : hiddenPanels ) {
179- ContributionPanel panel = contributionPanelsByInfo .get (info );
194+ ContributionPanel panel = panelByContribution .get (info );
180195 if (panel != null ) {
181196 panel .setVisible (false );
182197 }
@@ -208,8 +223,8 @@ protected void setSelectedPanel(ContributionPanel panel) {
208223 protected void updateColors () {
209224
210225 int count = 0 ;
211- synchronized (contributionPanelsByInfo ) {
212- for (Entry <Contribution , ContributionPanel > entry : contributionPanelsByInfo .entrySet ()) {
226+ synchronized (panelByContribution ) {
227+ for (Entry <Contribution , ContributionPanel > entry : panelByContribution .entrySet ()) {
213228 ContributionPanel panel = entry .getValue ();
214229
215230 if (panel .isVisible () && panel .isSelected ()) {
@@ -401,7 +416,7 @@ public void actionPerformed(ActionEvent arg) {
401416 installOrRemoveButton .setEnabled (false );
402417
403418 installProgressBar .setVisible (true );
404- contributionManager .removeContribution ((InstalledContribution ) info ,
419+ contribManager .removeContribution ((InstalledContribution ) info ,
405420 new JProgressMonitor (installProgressBar ) {
406421
407422 public void finishedAction () {
@@ -488,7 +503,7 @@ private void addPaneComponents() {
488503
489504 iconArea = new JPanel () {
490505 protected void paintComponent (Graphics g ) {
491- Image icon = contributionManager .getContributionIcon (info .getType ());
506+ Image icon = contribManager .getContributionIcon (info .getType ());
492507 if (icon != null ) {
493508 g .drawImage (icon , 0 , 0 , this );
494509 }
@@ -553,7 +568,7 @@ protected void paintComponent(Graphics g) {
553568 public void actionPerformed (ActionEvent e ) {
554569 updateButton .setEnabled (false );
555570 // XXX: There is a bug here. The advertised library will be 'replaced' instead
556- installContribution ((AdvertisedContribution ) contributionManager . contributionListing .getAdvertisedContribution (info ));
571+ installContribution ((AdvertisedContribution ) contribManager . contribListing .getAdvertisedContribution (info ));
557572 }
558573 });
559574 descriptionPanel .add (updateButton , c );
@@ -650,7 +665,7 @@ public void setContribution(Contribution info) {
650665 descriptionText .setText (description .toString ());
651666 setAlignment (descriptionText , StyleConstants .ALIGN_JUSTIFIED );
652667
653- if (contributionManager .hasUpdates (info )) {
668+ if (contribManager . getListing () .hasUpdates (info )) {
654669 StringBuilder versionText = new StringBuilder ();
655670 versionText .append ("<html><body><i>" );
656671 versionText .append ("New version available!" );
@@ -685,7 +700,7 @@ private void installContribution(AdvertisedContribution info) {
685700
686701 installProgressBar .setVisible (true );
687702
688- contributionManager .downloadAndInstall (downloadUrl , info ,
703+ contribManager .downloadAndInstall (downloadUrl , info ,
689704 new JProgressMonitor (installProgressBar ) {
690705
691706 public void finishedAction () {
@@ -788,7 +803,8 @@ public void setSelected(boolean isSelected) {
788803 // hyperlink, it will be opened as the mouse is released.
789804 enableHyperlinks = alreadySelected ;
790805
791- updateButton .setVisible (isSelected () && contributionManager .hasUpdates (info ));
806+ updateButton .setVisible (isSelected ()
807+ && contribManager .getListing ().hasUpdates (info ));
792808 installOrRemoveButton .setVisible (isSelected ());
793809
794810 for (JTextPane textPane : htmlPanes ) {
0 commit comments