Skip to content

Ghidra is a software reverse engineering (SRE) framework

use DynamicTableColumn methods for width and renderer.

Jason P. Leasure934e4948efc3
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusComponentProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusComponentProvider.java
index 6321f7b9eb..f2e313dbe3 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusComponentProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusComponentProvider.java
@@ -23,7 +23,6 @@ import java.util.stream.Collectors;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
-import javax.swing.table.TableColumn;
import docking.action.builder.ActionBuilder;
import docking.util.AnimationUtils;
@@ -33,6 +32,7 @@ import docking.widgets.table.*;
import generic.jar.ResourceFile;
import generic.util.Path;
import ghidra.app.services.ConsoleService;
+import ghidra.docking.settings.Settings;
import ghidra.framework.plugintool.ComponentProviderAdapter;
import ghidra.framework.plugintool.PluginTool;
import ghidra.framework.preferences.Preferences;
@@ -41,6 +41,8 @@ import ghidra.util.Msg;
import ghidra.util.exception.CancelledException;
import ghidra.util.filechooser.GhidraFileChooserModel;
import ghidra.util.filechooser.GhidraFileFilter;
+import ghidra.util.table.column.AbstractGColumnRenderer;
+import ghidra.util.table.column.AbstractWrapperTypeColumnRenderer;
import ghidra.util.task.*;
import resources.ResourceManager;
@@ -130,70 +132,14 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {
}
private void configureTableColumns() {
- TableColumn column;
-
- int skinnyWidth = 60;
- GTableColumnModel columnModel = (GTableColumnModel) bundleStatusTable.getColumnModel();
-
- List<TableColumn> columns = columnModel.getAllColumns();
-
- //
- column = columns.get(bundleStatusTableModel.enabledColumn.getModelIndex());
- column.setPreferredWidth(skinnyWidth);
- column.setMinWidth(skinnyWidth);
- column.setMaxWidth(skinnyWidth);
- column.setWidth(skinnyWidth);
-
- //
- column = columns.get(bundleStatusTableModel.activeColumn.getModelIndex());
- column.setPreferredWidth(skinnyWidth);
- column.setMinWidth(skinnyWidth);
- column.setMaxWidth(skinnyWidth);
- column.setWidth(skinnyWidth);
- column.setCellRenderer(new GBooleanCellRenderer() {
- @Override
- public Component getTableCellRendererComponent(GTableCellRenderingData data) {
- BundleStatus status = (BundleStatus) data.getRowObject();
- Component component = super.getTableCellRendererComponent(data);
- if (status.isBusy()) {
- cb.setVisible(false);
- cb.setEnabled(false);
- setHorizontalAlignment(SwingConstants.CENTER);
- setText("...");
- }
- else {
- cb.setVisible(true);
- cb.setEnabled(true);
- setText("");
- }
- return component;
- }
- });
- columnModel.setVisible(column, false);
-
- //
- column = columns.get(bundleStatusTableModel.typeColumn.getModelIndex());
- FontMetrics fontmetrics = panel.getFontMetrics(panel.getFont());
- int width = 10 +
- SwingUtilities.computeStringWidth(fontmetrics, GhidraBundle.Type.SOURCE_DIR.toString());
- column.setMaxWidth(width);
- column.setWidth(width);
-
- //
- column = columns.get(bundleStatusTableModel.pathColumn.getModelIndex());
- column.setCellRenderer(new GTableCellRenderer() {
- @Override
- public Component getTableCellRendererComponent(GTableCellRenderingData data) {
- ResourceFile file = (ResourceFile) data.getValue();
- JLabel label = (JLabel) super.getTableCellRendererComponent(data);
- label.setText(Path.toPathString(file));
- GhidraBundle bundle = bundleHost.getExistingGhidraBundle(file);
- if (bundle == null || bundle instanceof GhidraPlaceholderBundle || !file.exists()) {
- label.setForeground(Color.RED);
- }
- return label;
- }
- });
+ int skinnyWidth=60;
+ BusyBooleanRenderer renderer = new BusyBooleanRenderer();
+ bundleStatusTableModel.activeColumn.setColumnRenderer(renderer);
+ bundleStatusTableModel.activeColumn.setColumnPreferredWidth(skinnyWidth);
+ bundleStatusTableModel.enabledColumn.setColumnRenderer(renderer);
+ bundleStatusTableModel.enabledColumn.setColumnPreferredWidth(skinnyWidth);
+ bundleStatusTableModel.typeColumn.setColumnPreferredWidth(90);
+ bundleStatusTableModel.pathColumn.setColumnRenderer(new MissingFileRenderer());
}
private void addBundlesAction(String actionName, String description, Icon icon,
@@ -551,4 +497,45 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {
}
}
+ private class BusyBooleanRenderer extends GBooleanCellRenderer
+ implements AbstractWrapperTypeColumnRenderer<Boolean> {
+ @Override
+ public Component getTableCellRendererComponent(GTableCellRenderingData data) {
+ BundleStatus status = (BundleStatus) data.getRowObject();
+ Component component = super.getTableCellRendererComponent(data);
+ if (status.isBusy()) {
+ cb.setVisible(false);
+ cb.setEnabled(false);
+ setHorizontalAlignment(SwingConstants.CENTER);
+ setText("...");
+ }
+ else {
+ cb.setVisible(true);
+ cb.setEnabled(true);
+ setText("");
+ }
+ return component;
+ }
+
+ }
+
+ private class MissingFileRenderer extends AbstractGColumnRenderer<ResourceFile> {
+ @Override
+ public Component getTableCellRendererComponent(GTableCellRenderingData data) {
+ ResourceFile file = (ResourceFile) data.getValue();
+ JLabel label = (JLabel) super.getTableCellRendererComponent(data);
+ label.setText(Path.toPathString(file));
+ GhidraBundle bundle = bundleHost.getExistingGhidraBundle(file);
+ if (bundle == null || bundle instanceof GhidraPlaceholderBundle || !file.exists()) {
+ label.setForeground(Color.RED);
+ }
+ return label;
+ }
+
+ @Override
+ public String getFilterString(ResourceFile file, Settings settings) {
+ return Path.toPathString(file);
+ }
+
+ }
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusTableModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusTableModel.java
index b2c6a3e96d..064d3a44c4 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusTableModel.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusTableModel.java
@@ -29,6 +29,7 @@ import ghidra.docking.settings.Settings;
import ghidra.framework.plugintool.ServiceProvider;
import ghidra.util.Msg;
import ghidra.util.SystemUtilities;
+import ghidra.util.table.column.GColumnRenderer;
/**
* Model for {@link BundleStatus} objects.
@@ -361,6 +362,7 @@ public class BundleStatusTableModel
@Override
protected TableColumnDescriptor<BundleStatus> createTableColumnDescriptor() {
+
TableColumnDescriptor<BundleStatus> columnDescriptor = new TableColumnDescriptor<>();
enabledColumn = new Column<>("Enabled") {
@Override
@@ -395,26 +397,34 @@ public class BundleStatusTableModel
void setValue(BundleStatus status, Boolean newValue) {
fireBundleActivationChangeRequested(status, newValue);
}
-
};
columnDescriptor.addHiddenColumn(activeColumn);
typeColumn = new Column<>("Type") {
+ @Override
public String getValue(BundleStatus status) {
return status.getType().toString();
}
+ @Override
+ public int getColumnPreferredWidth() {
+ return 90;
+ }
+
};
columnDescriptor.addVisibleColumn(typeColumn);
pathColumn = new Column<>("Path") {
+ @Override
public ResourceFile getValue(BundleStatus status) {
return status.getFile();
}
+
};
columnDescriptor.addVisibleColumn(pathColumn);
summaryColumn = new Column<>("Summary") {
+ @Override
public String getValue(BundleStatus status) {
return status.getSummary();
}
@@ -424,40 +434,61 @@ public class BundleStatusTableModel
return columnDescriptor;
}
- abstract class Column<ROW_TYPE>
- extends AbstractDynamicTableColumn<BundleStatus, ROW_TYPE, List<BundleStatus>> {
- final String name;
+ abstract class Column<COLUMN_TYPE>
+ extends AbstractDynamicTableColumn<BundleStatus, COLUMN_TYPE, List<BundleStatus>> {
+ final String columnName;
+ GColumnRenderer<COLUMN_TYPE> renderer;
+ int width = -1;
Column(String name) {
super();
- this.name = name;
+ this.columnName = name;
}
boolean editable(BundleStatus status) {
return false;
}
- abstract ROW_TYPE getValue(BundleStatus status);
+ abstract COLUMN_TYPE getValue(BundleStatus status);
@Override
- public ROW_TYPE getValue(BundleStatus rowObject, Settings settings, List<BundleStatus> data,
- ServiceProvider serviceProvider0) throws IllegalArgumentException {
+ public COLUMN_TYPE getValue(BundleStatus rowObject, Settings settings,
+ List<BundleStatus> data, ServiceProvider serviceProvider0)
+ throws IllegalArgumentException {
return getValue(rowObject);
}
- void setValue(BundleStatus status, ROW_TYPE aValue) {
- throw new RuntimeException(name + " is not editable!");
+ void setValue(BundleStatus status, COLUMN_TYPE aValue) {
+ throw new RuntimeException(columnName + " is not editable!");
}
@Override
public String getColumnName() {
- return name;
+ return columnName;
}
int getModelIndex() {
return getColumnIndex(this);
}
+ public void setColumnRenderer(GColumnRenderer<COLUMN_TYPE> renderer) {
+ this.renderer = renderer;
+ }
+
+ @Override
+ public GColumnRenderer<COLUMN_TYPE> getColumnRenderer() {
+ return renderer;
+ }
+
+ @Override
+ public int getColumnPreferredWidth() {
+ return width;
+ }
+
+ public void setColumnPreferredWidth(int width) {
+ this.width = width;
+ }
+
}
}