Skip to content

Commit

Permalink
Dialog tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniovazquezblanco committed Feb 7, 2025
1 parent 3148616 commit ef505c2
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 22 deletions.
38 changes: 33 additions & 5 deletions src/main/java/svd/SVDPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
package svd;

import java.io.File;
import java.io.IOException;

import javax.swing.JComponent;
import javax.swing.SwingConstants;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import docking.action.builder.ActionBuilder;
import docking.tool.ToolConstants;
Expand All @@ -34,6 +38,10 @@
import ghidra.util.Msg;
import ghidra.util.task.TaskBuilder;
import ghidra.util.task.TaskLauncher;
import io.svdparser.SvdDevice;
import io.svdparser.SvdParserException;
import svd.ui.SvdFileDialog;
import svd.ui.SvdInfoDialog;

//@formatter:off
@PluginInfo(
Expand All @@ -54,17 +62,33 @@ public SVDPlugin(PluginTool tool) {
private void createActions() {
new ActionBuilder("Load SVD File", this.getName()).withContext(ProgramActionContext.class)
.validContextWhen(pac -> pac.getProgram() != null).menuPath(ToolConstants.MENU_FILE, "Load SVD File...")
.menuGroup("Import SVD", "5").onAction(pac -> loadSvd(pac)).buildAndInstall(tool);
.menuGroup("Import SVD", "5").onAction(pac -> {
try {
loadSvd(pac);
} catch (SvdParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}).buildAndInstall(tool);
}

private void loadSvd(ProgramActionContext pac) {
private void loadSvd(ProgramActionContext pac) throws SvdParserException, SAXException, IOException, ParserConfigurationException {
Program program = pac.getProgram();
AutoAnalysisManager currentAutoAnalysisManager = AutoAnalysisManager.getAnalysisManager(program);
if (currentAutoAnalysisManager.isAnalyzing()) {
Msg.showWarn(getClass(), null, "Load SVD", "Unable to load SVD file while analysis is running.");
return;
}

tool.setStatusInfo("Loading SVD.");

JComponent parentComponent = pac.getComponentProvider().getComponent();
Expand All @@ -73,11 +97,15 @@ private void loadSvd(ProgramActionContext pac) {
tool.setStatusInfo("SVD loading was cancelled.");
return;
}


SvdDevice device = SvdDevice.fromFile(file);
SvdInfoDialog svdInfoDialog = new SvdInfoDialog(device);
tool.showDialog(svdInfoDialog);

SvdLoadTask loadTask = new SvdLoadTask(program, file);
TaskBuilder.withTask(loadTask).setStatusTextAlignment(SwingConstants.LEADING).setLaunchDelay(0);
new TaskLauncher(loadTask);

tool.setStatusInfo("SVD loader finished.");
}
}
27 changes: 11 additions & 16 deletions src/main/java/svd/SvdLoadTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ private void createMemoryBlock(BlockInfo blockInfo) {
int transactionId = mProgram.startTransaction("SVD memory block creation");
boolean ok = false;
try {
MemoryBlock memBlock = mMemory.createUninitializedBlock(blockInfo.name, addr, blockInfo.block.getSize().longValue(),
false);
MemoryBlock memBlock = mMemory.createUninitializedBlock(blockInfo.name, addr,
blockInfo.block.getSize().longValue(), false);
memBlock.setRead(blockInfo.isReadable);
memBlock.setWrite(blockInfo.isWritable);
memBlock.setExecute(blockInfo.isExecutable);
Expand All @@ -191,12 +191,11 @@ private void createMemoryBlock(BlockInfo blockInfo) {
}

private void updateMatchingMemoryBlock(MemoryBlock collidingMemoryBlock, BlockInfo blockInfo) {
if (!collidingMemoryBlock.getName().equals(blockInfo.name)
&& OptionDialog.showYesNoDialog(null, "Load SVD",
"An existing memory block with name \"" + collidingMemoryBlock.getName()
+ "\" is in the same region as the \"" + blockInfo.name
+ "\" peripheral. Do you want to rename it to \"" + blockInfo.name
+ "\"?") == OptionDialog.OPTION_ONE) {
if (!collidingMemoryBlock.getName().equals(blockInfo.name) && OptionDialog.showYesNoDialog(null, "Load SVD",
"An existing memory block with name \"" + collidingMemoryBlock.getName()
+ "\" is in the same region as the \"" + blockInfo.name
+ "\" peripheral. Do you want to rename it to \"" + blockInfo.name
+ "\"?") == OptionDialog.OPTION_ONE) {
int transactionId = mProgram.startTransaction("SVD memory block rename");
boolean ok = false;
try {
Expand All @@ -208,8 +207,7 @@ private void updateMatchingMemoryBlock(MemoryBlock collidingMemoryBlock, BlockIn
}
mProgram.endTransaction(transactionId, ok);
}
if (collidingMemoryBlock.isRead() != blockInfo.isReadable && OptionDialog.showYesNoDialog(null,
"Load SVD",
if (collidingMemoryBlock.isRead() != blockInfo.isReadable && OptionDialog.showYesNoDialog(null, "Load SVD",
"Memory block \"" + collidingMemoryBlock.getName() + "\" is marked as"
+ ((!collidingMemoryBlock.isRead()) ? " non" : "")
+ " readable. The SVD file suggests it should be"
Expand All @@ -228,8 +226,7 @@ private void updateMatchingMemoryBlock(MemoryBlock collidingMemoryBlock, BlockIn
mProgram.endTransaction(transactionId, ok);
}

if (collidingMemoryBlock.isWrite() != blockInfo.isWritable && OptionDialog.showYesNoDialog(null,
"Load SVD",
if (collidingMemoryBlock.isWrite() != blockInfo.isWritable && OptionDialog.showYesNoDialog(null, "Load SVD",
"Memory block \"" + collidingMemoryBlock.getName() + "\" is marked as"
+ ((!collidingMemoryBlock.isWrite()) ? " non" : "")
+ " writable. The SVD file suggests it should be"
Expand All @@ -248,8 +245,7 @@ private void updateMatchingMemoryBlock(MemoryBlock collidingMemoryBlock, BlockIn
mProgram.endTransaction(transactionId, ok);
}

if (collidingMemoryBlock.isExecute() != blockInfo.isExecutable && OptionDialog.showYesNoDialog(null,
"Load SVD",
if (collidingMemoryBlock.isExecute() != blockInfo.isExecutable && OptionDialog.showYesNoDialog(null, "Load SVD",
"Memory block \"" + collidingMemoryBlock.getName() + "\" is marked as"
+ ((!collidingMemoryBlock.isExecute()) ? " non" : "")
+ " executable. The SVD file suggests it should be"
Expand All @@ -269,8 +265,7 @@ private void updateMatchingMemoryBlock(MemoryBlock collidingMemoryBlock, BlockIn
mProgram.endTransaction(transactionId, ok);
}

if (collidingMemoryBlock.isVolatile() != blockInfo.isVolatile && OptionDialog.showYesNoDialog(null,
"Load SVD",
if (collidingMemoryBlock.isVolatile() != blockInfo.isVolatile && OptionDialog.showYesNoDialog(null, "Load SVD",
"Memory block \"" + collidingMemoryBlock.getName() + "\" is marked as"
+ ((!collidingMemoryBlock.isVolatile()) ? " non" : "")
+ " volatile. The SVD file suggests it should be"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package svd;
package svd.ui;

import java.io.File;

Expand Down
126 changes: 126 additions & 0 deletions src/main/java/svd/ui/SvdInfoDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package svd.ui;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import docking.ReusableDialogComponentProvider;
import docking.widgets.table.AbstractGTableModel;
import docking.widgets.table.GTable;
import io.svdparser.SvdDevice;
import io.svdparser.SvdPeripheral;

public class SvdInfoDialog extends ReusableDialogComponentProvider {

public SvdInfoDialog(SvdDevice dev) {
super("SVD Information");
addWorkPanel(createMainPanel(dev));
}

private JComponent createMainPanel(SvdDevice dev) {
JPanel panel = new JPanel(new BorderLayout());

SvdPeripheralTableModel svdPeriphModel = new SvdPeripheralTableModel(dev);
GTable svdPeriphTable = new GTable(svdPeriphModel);
JScrollPane svdPeriphTableScroll = new JScrollPane(svdPeriphTable);
panel.add(svdPeriphTableScroll);

addOKButton();

return panel;
}

public class SvdPeripheralTableModel extends AbstractGTableModel<SvdPeripheral> {

private List<ReferenceCol> columns = new ArrayList<>();
private List<SvdPeripheral> rowDataList;

SvdPeripheralTableModel(SvdDevice dev) {
rowDataList = dev.getPeripherals();
columns.add(new NameColumn());
columns.add(new BaseAddressColumn());
}

@Override
public String getName() {
return "Peripherals";
}

@Override
public List<SvdPeripheral> getModelData() {
return rowDataList;
}

@Override
public int getColumnCount() {
return columns.size();
}

@Override
public String getColumnName(int column) {
return columns.get(column).getName();
}

@Override
public Class<?> getColumnClass(int columnIndex) {
return columns.get(columnIndex).getColumnClass();
}

@Override
public Object getColumnValueForRow(SvdPeripheral t, int columnIndex) {
return columns.get(columnIndex).getValueForRow(rowDataList, t);
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}

private abstract class ReferenceCol {
private String name;
private Class<?> classType;

ReferenceCol(String name, Class<?> classType) {
this.name = name;
this.classType = classType;
}

public String getName() {
return name;
}

public Class<?> getColumnClass() {
return classType;
}

protected abstract Object getValueForRow(List<SvdPeripheral> data, SvdPeripheral t);
}

private class NameColumn extends ReferenceCol {
NameColumn() {
super("Name", String.class);
}

@Override
protected Object getValueForRow(List<SvdPeripheral> data, SvdPeripheral t) {
return t.getName();
}
}

private class BaseAddressColumn extends ReferenceCol {
BaseAddressColumn() {
super("Base addr", String.class);
}

@Override
protected Object getValueForRow(List<SvdPeripheral> data, SvdPeripheral t) {
return String.format("0x%04x", t.getBaseAddr());
}
}

}
}

0 comments on commit ef505c2

Please sign in to comment.