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.awt.Font;
import java.util.Collections; import java.util.Collections;
import java.util.List; 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 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 Identifier mIdentifier;
private List<Alias> mAliases = Collections.EMPTY_LIST; private List<Alias> mAliases = Collections.EMPTY_LIST;
private AliasModel mAliasModel; private AliasModel mAliasModel;
private Lock mLock = new ReentrantLock();
public AudioChannelPanel(IconManager iconManager, UserPreferences userPreferences, SettingsManager settingsManager, public AudioChannelPanel(IconManager iconManager, UserPreferences userPreferences, SettingsManager settingsManager,
AudioOutput audioOutput, AliasModel aliasModel) AudioOutput audioOutput, AliasModel aliasModel)
@@ -199,13 +202,24 @@ public class AudioChannelPanel extends JPanel implements Listener<AudioEvent>, S
*/ */
private void resetLabels() private void resetLabels()
{ {
boolean updated = mIdentifier != null; //Protect access to mIdentifier and mAliases
mIdentifier = null; mLock.lock();
mAliases = Collections.EMPTY_LIST;
if(updated) try
{ {
updateLabels(); boolean updated = mIdentifier != null;
mIdentifier = null;
mAliases = Collections.EMPTY_LIST;
//Hold the lock through the label update
if(updated)
{
updateLabels();
}
}
finally
{
mLock.unlock();
} }
} }
@@ -227,11 +241,28 @@ public class AudioChannelPanel extends JPanel implements Listener<AudioEvent>, S
boolean updated = false; boolean updated = false;
if(toIds.size() == 1) //Protect access to mIdentifier and mAliases
{ mLock.lock();
Identifier currentIdentifier = mIdentifier;
if(currentIdentifier == null || currentIdentifier != toIds.get(0)) try
{
if(toIds.size() == 1)
{
Identifier currentIdentifier = mIdentifier;
if(currentIdentifier == null || currentIdentifier != toIds.get(0))
{
mIdentifier = toIds.get(0);
AliasList aliasList = mAliasModel.getAliasList(identifierCollection);
if(aliasList != null)
{
mAliases = aliasList.getAliases(mIdentifier);
}
updated = true;
}
}
else
{ {
mIdentifier = toIds.get(0); mIdentifier = toIds.get(0);
AliasList aliasList = mAliasModel.getAliasList(identifierCollection); AliasList aliasList = mAliasModel.getAliasList(identifierCollection);
@@ -242,69 +273,64 @@ public class AudioChannelPanel extends JPanel implements Listener<AudioEvent>, S
} }
updated = true; updated = true;
} }
}
else
{
mIdentifier = toIds.get(0);
AliasList aliasList = mAliasModel.getAliasList(identifierCollection);
if(aliasList != null) //Hold the lock through the label update
if(updated)
{ {
mAliases = aliasList.getAliases(mIdentifier); updateLabels();
} }
updated = true;
} }
finally
if(updated)
{ {
updateLabels(); mLock.unlock();
} }
} }
/** /**
* Updates the alias label with text and icon from the alias. Note: this * Updates the alias label with text and icon from the alias.
* does not occur on the Swing event thread -- wrap any calls to this
* method with an event thread call.
*/ */
private void updateLabels() private void updateLabels()
{ {
String identifier = null; String identifier = null;
String iconName = null; String iconName = null;
if(mAliases.size() == 1) //Protect access to mIdentifier and mAliases
{ mLock.lock();
identifier = mAliases.get(0).getName();
iconName = mAliases.get(0).getIconName();
}
else if(mAliases.size() > 1)
{
identifier = Joiner.on(", ").skipNulls().join(mAliases);
}
if(identifier == null && mIdentifier != null) try
{ {
identifier = mTalkgroupFormatPreference.format(mIdentifier); if(mAliases.size() == 1)
}
if(identifier == null)
{
identifier = "-----";
}
final ImageIcon icon = iconName != null ? mIconManager.getIcon(iconName, 18) : null;
final String identifierText = identifier;
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{ {
identifier = mAliases.get(0).getName();
iconName = mAliases.get(0).getIconName();
}
else if(mAliases.size() > 1)
{
identifier = Joiner.on(", ").skipNulls().join(mAliases);
}
if(identifier == null && mIdentifier != null)
{
identifier = mTalkgroupFormatPreference.format(mIdentifier);
}
if(identifier == null)
{
identifier = "-----";
}
final ImageIcon icon = iconName != null ? mIconManager.getIcon(iconName, 18) : null;
final String identifierText = identifier;
EventQueue.invokeLater(() -> {
mIdentifierLabel.setText(identifierText); mIdentifierLabel.setText(identifierText);
mIconLabel.setIcon(icon); mIconLabel.setIcon(icon);
} });
}); }
finally
{
mLock.unlock();
}
} }
/** /**
@@ -329,14 +355,16 @@ public class AudioChannelPanel extends JPanel implements Listener<AudioEvent>, S
switch(colorSetting.getColorSettingName()) switch(colorSetting.getColorSettingName())
{ {
case CHANNEL_STATE_LABEL_DECODER: case CHANNEL_STATE_LABEL_DECODER:
if(mIdentifierLabel != null) EventQueue.invokeLater(() -> {
{ if(mIdentifierLabel != null)
mIdentifierLabel.setForeground(mLabelColor); {
} mIdentifierLabel.setForeground(mLabelColor);
if(mIconLabel != null) }
{ if(mIconLabel != null)
mIconLabel.setForeground(mLabelColor); {
} mIconLabel.setForeground(mLabelColor);
}
});
break; break;
default: default:
break; break;