Resolves #678 AudioChannelPanel threading issue. (#679)

Co-authored-by: Denny <denny@denny-desktop>
This commit is contained in:
Denny Sheirer
2020-03-20 09:21:49 +00:00
committed by GitHub
parent fa16db129b
commit 23a3ee39cc

View File

@@ -52,6 +52,8 @@ import java.awt.EventQueue;
import java.awt.Font;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class AudioChannelPanel extends JPanel implements Listener<AudioEvent>, SettingChangeListener
{
@@ -84,6 +86,7 @@ public class AudioChannelPanel extends JPanel implements Listener<AudioEvent>, S
private Identifier mIdentifier;
private List<Alias> mAliases = Collections.EMPTY_LIST;
private AliasModel mAliasModel;
private Lock mLock = new ReentrantLock();
public AudioChannelPanel(IconManager iconManager, UserPreferences userPreferences, SettingsManager settingsManager,
AudioOutput audioOutput, AliasModel aliasModel)
@@ -198,16 +201,27 @@ public class AudioChannelPanel extends JPanel implements Listener<AudioEvent>, S
* Resets the from and to labels.
*/
private void resetLabels()
{
//Protect access to mIdentifier and mAliases
mLock.lock();
try
{
boolean updated = mIdentifier != null;
mIdentifier = null;
mAliases = Collections.EMPTY_LIST;
//Hold the lock through the label update
if(updated)
{
updateLabels();
}
}
finally
{
mLock.unlock();
}
}
private void updateIdentifiers(IdentifierCollection identifierCollection)
{
@@ -227,6 +241,11 @@ public class AudioChannelPanel extends JPanel implements Listener<AudioEvent>, S
boolean updated = false;
//Protect access to mIdentifier and mAliases
mLock.lock();
try
{
if(toIds.size() == 1)
{
Identifier currentIdentifier = mIdentifier;
@@ -255,22 +274,31 @@ public class AudioChannelPanel extends JPanel implements Listener<AudioEvent>, S
updated = true;
}
//Hold the lock through the label update
if(updated)
{
updateLabels();
}
}
finally
{
mLock.unlock();
}
}
/**
* Updates the alias label with text and icon from the alias. Note: this
* does not occur on the Swing event thread -- wrap any calls to this
* method with an event thread call.
* Updates the alias label with text and icon from the alias.
*/
private void updateLabels()
{
String identifier = null;
String iconName = null;
//Protect access to mIdentifier and mAliases
mLock.lock();
try
{
if(mAliases.size() == 1)
{
identifier = mAliases.get(0).getName();
@@ -292,19 +320,17 @@ public class AudioChannelPanel extends JPanel implements Listener<AudioEvent>, S
}
final ImageIcon icon = iconName != null ? mIconManager.getIcon(iconName, 18) : null;
final String identifierText = identifier;
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
EventQueue.invokeLater(() -> {
mIdentifierLabel.setText(identifierText);
mIconLabel.setIcon(icon);
}
});
}
finally
{
mLock.unlock();
}
}
/**
@@ -329,6 +355,7 @@ public class AudioChannelPanel extends JPanel implements Listener<AudioEvent>, S
switch(colorSetting.getColorSettingName())
{
case CHANNEL_STATE_LABEL_DECODER:
EventQueue.invokeLater(() -> {
if(mIdentifierLabel != null)
{
mIdentifierLabel.setForeground(mLabelColor);
@@ -337,6 +364,7 @@ public class AudioChannelPanel extends JPanel implements Listener<AudioEvent>, S
{
mIconLabel.setForeground(mLabelColor);
}
});
break;
default:
break;