WidgetInspector: run on client thread

This commit is contained in:
Max Weber
2018-10-01 23:45:27 -06:00
parent b559a6b1af
commit e18e063ef5
2 changed files with 57 additions and 44 deletions

View File

@@ -24,24 +24,45 @@
*/
package net.runelite.client.plugins.devtools;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import net.runelite.api.widgets.Widget;
import net.runelite.client.callback.ClientThread;
public class WidgetInfoTableModel extends AbstractTableModel
{
@Inject
private ClientThread clientThread;
private static final int COL_FIELD = 0;
private static final int COL_VALUE = 1;
private static final List<WidgetField> fields = populateWidgetFields();
private Widget widget = null;
private Map<WidgetField, Object> values = null;
public void setWidget(Widget w)
{
this.widget = w;
fireTableStructureChanged();
clientThread.invoke(() ->
{
Map<WidgetField, Object> newValues = w == null ? null : fields.stream().collect(ImmutableMap.toImmutableMap(
Function.identity(),
i -> i.getValue(w)
));
SwingUtilities.invokeLater(() ->
{
widget = w;
values = newValues;
fireTableStructureChanged();
});
});
}
@Override
@@ -67,11 +88,11 @@ public class WidgetInfoTableModel extends AbstractTableModel
@Override
public int getRowCount()
{
if (widget == null)
if (values == null)
{
return 0;
}
return fields.size();
return values.size();
}
@Override
@@ -83,7 +104,7 @@ public class WidgetInfoTableModel extends AbstractTableModel
case COL_FIELD:
return field.getName();
case COL_VALUE:
return field.getValue(widget);
return values.get(field);
default:
return null;
}
@@ -104,7 +125,11 @@ public class WidgetInfoTableModel extends AbstractTableModel
public void setValueAt(Object value, int rowIndex, int columnIndex)
{
WidgetField<?> field = fields.get(rowIndex);
field.setValue(widget, value);
clientThread.invoke(() ->
{
field.setValue(widget, value);
setWidget(widget);
});
}
private static List<WidgetField> populateWidgetFields()

View File

@@ -36,7 +36,6 @@ import java.awt.event.WindowEvent;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
@@ -45,7 +44,7 @@ import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.SwingWorker;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import lombok.extern.slf4j.Slf4j;
@@ -54,12 +53,14 @@ import net.runelite.api.events.ConfigChanged;
import net.runelite.api.widgets.Widget;
import net.runelite.api.widgets.WidgetInfo;
import net.runelite.api.widgets.WidgetItem;
import net.runelite.client.callback.ClientThread;
import net.runelite.client.ui.ClientUI;
@Slf4j
class WidgetInspector extends JFrame
{
private final Client client;
private final ClientThread clientThread;
private final DevToolsPlugin plugin;
private final DevToolsConfig config;
@@ -70,10 +71,11 @@ class WidgetInspector extends JFrame
private static final Map<Integer, WidgetInfo> widgetIdMap = new HashMap<>();
@Inject
WidgetInspector(DevToolsPlugin plugin, Client client, WidgetInfoTableModel infoTableModel, DevToolsConfig config, EventBus eventBus)
WidgetInspector(DevToolsPlugin plugin, Client client, ClientThread clientThread, WidgetInfoTableModel infoTableModel, DevToolsConfig config, EventBus eventBus)
{
this.plugin = plugin;
this.client = client;
this.clientThread = clientThread;
this.infoTableModel = infoTableModel;
this.config = config;
@@ -157,45 +159,31 @@ class WidgetInspector extends JFrame
private void refreshWidgets()
{
new SwingWorker<DefaultMutableTreeNode, Void>()
clientThread.invokeLater(() ->
{
@Override
protected DefaultMutableTreeNode doInBackground() throws Exception
{
Widget[] rootWidgets = client.getWidgetRoots();
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
Widget[] rootWidgets = client.getWidgetRoots();
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
plugin.currentWidget = null;
plugin.itemIndex = -1;
for (Widget widget : rootWidgets)
{
DefaultMutableTreeNode childNode = addWidget("R", widget);
if (childNode != null)
{
root.add(childNode);
}
}
SwingUtilities.invokeLater(() ->
{
plugin.currentWidget = null;
plugin.itemIndex = -1;
for (Widget widget : rootWidgets)
{
DefaultMutableTreeNode childNode = addWidget("R", widget);
if (childNode != null)
{
root.add(childNode);
}
}
return root;
}
@Override
protected void done()
{
try
{
plugin.currentWidget = null;
plugin.itemIndex = -1;
refreshInfo();
widgetTree.setModel(new DefaultTreeModel(get()));
}
catch (InterruptedException | ExecutionException ex)
{
throw new RuntimeException(ex);
}
}
}.execute();
refreshInfo();
widgetTree.setModel(new DefaultTreeModel(root));
});
});
}
private DefaultMutableTreeNode addWidget(String type, Widget widget)