1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

initial memory import/export prototyping

This commit is contained in:
Ted Williams 2007-06-12 17:28:20 +00:00
parent 905afac0b8
commit a0fdd5636d
7 changed files with 1099 additions and 1 deletions

View file

@ -1,6 +1,7 @@
source.. = src/ source.. = src/
output.. = bin/ output.. = bin/
bin.includes = META-INF/,\ bin.includes = META-INF/,\
plugin.properties,\
.,\ .,\
plugin.xml,\ plugin.xml,\
about.html about.html

View file

@ -0,0 +1,2 @@
ExportMemoryAction.label=Export
ImportMemoryAction.label=Import

View file

@ -27,4 +27,31 @@
<initializer class="org.eclipse.dd.debug.memory.renderings.traditional.TraditionalRenderingPreferenceInitializer"/> <initializer class="org.eclipse.dd.debug.memory.renderings.traditional.TraditionalRenderingPreferenceInitializer"/>
</extension> </extension>
<extension point="org.eclipse.ui.viewActions">
<viewContribution
targetID="org.eclipse.debug.ui.MemoryView"
id="org.eclipse.debug.ui.memoryView.toolbar">
<action
class="org.eclipse.dd.debug.memory.renderings.actions.ExportMemoryAction"
helpContextId="ExportMemoryAction_context"
icon="$nl$/icons/full/elcl16/pin.gif"
id="org.eclipse.dd.debug.memory.renderings.actions.ExportMemoryAction"
label="%ExportMemoryAction.label"
state="false"
style="push"
toolbarPath="additions"
tooltip="%ExportMemoryAction.label"/>
<action
class="org.eclipse.dd.debug.memory.renderings.actions.ImportMemoryAction"
helpContextId="ExportMemoryAction_context"
icon="$nl$/icons/full/elcl16/pin.gif"
id="org.eclipse.dd.debug.memory.renderings.actions.ImportMemoryAction"
label="%ImportMemoryAction.label"
state="false"
style="push"
toolbarPath="additions"
tooltip="%ImportMemoryAction.label"/>
</viewContribution>
</extension>
</plugin> </plugin>

View file

@ -0,0 +1,202 @@
/*******************************************************************************
* Copyright (c) 2007 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Ted R Williams (Wind River Systems, Inc.) - initial implementation
*******************************************************************************/
package org.eclipse.dd.debug.memory.renderings.actions;
import java.io.File;
import java.io.FileWriter;
import java.math.BigInteger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.core.model.IMemoryBlock;
import org.eclipse.debug.core.model.IMemoryBlockExtension;
import org.eclipse.debug.core.model.MemoryByte;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.internal.ui.views.memory.MemoryView;
import org.eclipse.debug.internal.ui.views.memory.MemoryViewIdRegistry;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.debug.ui.memory.IMemoryRendering;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
/**
* Action for exporting memory.
*/
public class ExportMemoryAction implements IViewActionDelegate {
private MemoryView fView;
public void init(IViewPart view) {
if (view instanceof MemoryView)
fView = (MemoryView) view;
}
public void run(IAction action) {
String secondaryId = MemoryViewIdRegistry
.getUniqueSecondaryId(IDebugUIConstants.ID_MEMORY_VIEW);
ISelection selection = fView.getSite().getSelectionProvider()
.getSelection();
if (selection instanceof IStructuredSelection) {
IStructuredSelection strucSel = (IStructuredSelection) selection;
// return if current selection is empty
if (strucSel.isEmpty())
return;
Object obj = strucSel.getFirstElement();
if (obj == null)
return;
IMemoryBlock memBlock = null;
if (obj instanceof IMemoryRendering) {
memBlock = ((IMemoryRendering) obj).getMemoryBlock();
} else if (obj instanceof IMemoryBlock) {
memBlock = (IMemoryBlock) obj;
}
Shell shell = DebugUIPlugin.getShell();
ExportMemoryDialog dialog = new ExportMemoryDialog(shell, memBlock);
dialog.open();
Object results[] = dialog.getResult();
if(results != null && results.length == 5)
{
String format = (String) results[0];
BigInteger start = (BigInteger) results[1];
BigInteger end = (BigInteger) results[2];
File file = (File) results[4];
if("S-Record".equals(format))
{
exportSRecord(file, start, end, (IMemoryBlockExtension) memBlock); // FIXME
}
}
}
}
private void exportSRecord(final File outputFile, final BigInteger startAddress, final BigInteger endAddress, final IMemoryBlockExtension memblock)
{
Job job = new Job("Memory Export to S-Record File"){ //$NON-NLS-1$
public IStatus run(IProgressMonitor monitor) {
try
{
try
{
// FIXME 4 byte default
BigInteger DATA_PER_RECORD = BigInteger.valueOf(16);
BigInteger transferAddress = startAddress;
FileWriter writer = new FileWriter(outputFile);
BigInteger jobs = endAddress.subtract(transferAddress).divide(DATA_PER_RECORD);
BigInteger factor = BigInteger.ONE;
if(jobs.compareTo(BigInteger.valueOf(0x7FFFFFFF)) > 0)
{
factor = jobs.divide(BigInteger.valueOf(0x7FFFFFFF));
jobs = jobs.divide(factor);
}
monitor.beginTask("Transferring Data", jobs.intValue());
BigInteger jobCount = BigInteger.ZERO;
while(transferAddress.compareTo(endAddress) < 0)
{
BigInteger length = DATA_PER_RECORD;
if(endAddress.subtract(transferAddress).compareTo(length) < 0)
length = endAddress.subtract(transferAddress);
writer.write("S3"); // FIXME 4 byte address
StringBuffer buf = new StringBuffer();
BigInteger sRecordLength = BigInteger.valueOf(4); // address size
sRecordLength = sRecordLength.add(length);
sRecordLength = sRecordLength.add(BigInteger.ONE); // checksum
String transferAddressString = transferAddress.toString(16);
String lengthString = sRecordLength.toString(16);
if(lengthString.length() == 1)
buf.append("0");
buf.append(lengthString);
for(int i = 0; i < 8 - transferAddressString.length(); i++)
buf.append("0");
buf.append(transferAddressString);
// data
MemoryByte bytes[] = memblock.getBytesFromAddress(transferAddress,
length.longValue() / memblock.getAddressableSize());
for(MemoryByte b : bytes)
{
String bString = BigInteger.valueOf(0xFF & b.getValue()).toString(16);
if(bString.length() == 1)
buf.append("0");
buf.append(bString);
}
BigInteger checksum = BigInteger.ZERO;
for(int i = 0; i < buf.length(); i+=2)
{
BigInteger value = new BigInteger(buf.substring(i, i+1), 16);
checksum = checksum.add(value);
}
buf.append(BigInteger.valueOf(0xFF - checksum.byteValue()).and(
BigInteger.valueOf(0xFF)).toString(16));
writer.write(buf.toString().toUpperCase());
writer.write("\n");
transferAddress = transferAddress.add(length);
jobCount = jobCount.add(BigInteger.ONE);
if(jobCount.compareTo(factor) == 0)
{
jobCount = BigInteger.ZERO;
monitor.worked(1);
}
}
writer.close();
monitor.done();
}
catch(Exception e) { e.printStackTrace();}
}
catch(Exception e) {e.printStackTrace();}
return Status.OK_STATUS;
}};
job.setUser(true);
job.schedule();
}
public void selectionChanged(IAction action, ISelection selection) {
}
}

View file

@ -0,0 +1,389 @@
package org.eclipse.dd.debug.memory.renderings.actions;
import java.io.File;
import java.math.BigInteger;
import org.eclipse.debug.core.model.IMemoryBlock;
import org.eclipse.debug.core.model.IMemoryBlockExtension;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.SelectionDialog;
public class ExportMemoryDialog extends SelectionDialog
{
private Combo formatCombo;
private IMemoryBlock fMemoryBlock;
private Text startText;
private Text endText;
private Text lengthText;
private Text fileText;
public ExportMemoryDialog(Shell parent, IMemoryBlock memoryBlock)
{
super(parent);
super.setTitle("Export Memory");
setShellStyle(getShellStyle() | SWT.RESIZE);
fMemoryBlock = memoryBlock;
}
/* (non-Javadoc)
* @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
*/
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);
validate();
}
/* (non-Javadoc)
* @see org.eclipse.ui.dialogs.SelectionDialog#getResult()
*/
public Object[] getResult() {
Object[] results = super.getResult();
if (results != null)
{
return results;
}
return new Object[0];
}
/* (non-Javadoc)
* @see org.eclipse.jface.dialogs.Dialog#cancelPressed()
*/
protected void cancelPressed() {
setResult(null);
super.cancelPressed();
}
/* (non-Javadoc)
* @see org.eclipse.jface.dialogs.Dialog#okPressed()
*/
protected void okPressed() {
setSelectionResult(new Object[]{ getFormat(), getStartAddress(), getEndAddress(), getLength(), getFile() });
super.okPressed();
}
public String getFormat()
{
return formatCombo.getItem(formatCombo.getSelectionIndex());
}
public BigInteger getEndAddress()
{
String text = endText.getText();
boolean hex = text.startsWith("0x");
BigInteger endAddress = new BigInteger(hex ? text.substring(2) : text,
hex ? 16 : 10);
return endAddress;
}
public BigInteger getStartAddress()
{
String text = startText.getText();
boolean hex = text.startsWith("0x");
BigInteger startAddress = new BigInteger(hex ? text.substring(2) : text,
hex ? 16 : 10);
return startAddress;
}
public BigInteger getLength()
{
String text = lengthText.getText();
boolean hex = text.startsWith("0x");
BigInteger lengthAddress = new BigInteger(hex ? text.substring(2) : text,
hex ? 16 : 10);
return lengthAddress;
}
public File getFile()
{
return new File(fileText.getText());
}
private void validate()
{
boolean isValid = true;
try
{
getEndAddress();
getStartAddress();
BigInteger length = getLength();
if(length.compareTo(BigInteger.ZERO) <= 0)
isValid = false;
if(!getFile().getParentFile().exists())
isValid = false;
}
catch(Exception e)
{
isValid = false;
}
getButton(IDialogConstants.OK_ID).setEnabled(isValid);
}
/* (non-Javadoc)
* @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
*/
protected Control createDialogArea(Composite parent) {
PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, DebugUIPlugin.getUniqueIdentifier() + ".AddMemoryRenderingDialog_context"); //$NON-NLS-1$ // FIXME
Composite composite = new Composite(parent, SWT.NONE);
FormLayout formLayout = new FormLayout();
formLayout.spacing = 5;
formLayout.marginWidth = formLayout.marginHeight = 9;
composite.setLayout(formLayout);
// format
Label textLabel = new Label(composite, SWT.NONE);
textLabel.setText("Format: ");
formatCombo = new Combo(composite, SWT.BORDER | SWT.READ_ONLY);
FormData data = new FormData();
data.top = new FormAttachment(formatCombo, 0, SWT.CENTER);
textLabel.setLayoutData(data);
data = new FormData();
data.left = new FormAttachment(textLabel);
formatCombo.setLayoutData(data);
formatCombo.setItems( new String[] { "S-Record" }); // TODO offer extension point
formatCombo.select(0);
// start address
Label startLabel = new Label(composite, SWT.NONE);
startLabel.setText("Start address: ");
data = new FormData();
data.top = new FormAttachment(formatCombo);
startLabel.setLayoutData(data);
startText = new Text(composite, SWT.NONE);
data = new FormData();
data.top = new FormAttachment(formatCombo);
data.left = new FormAttachment(startLabel);
data.width = 100;
startText.setLayoutData(data);
// end address
Label endLabel = new Label(composite, SWT.NONE);
endLabel.setText("End address: ");
data = new FormData();
data.top = new FormAttachment(startText, 0, SWT.CENTER);
data.left = new FormAttachment(startText);
endLabel.setLayoutData(data);
endText = new Text(composite, SWT.NONE);
data = new FormData();
data.top = new FormAttachment(startText, 0, SWT.CENTER);
data.left = new FormAttachment(endLabel);
data.width = 100;
endText.setLayoutData(data);
// length
Label lengthLabel = new Label(composite, SWT.NONE);
lengthLabel.setText("Length: ");
data = new FormData();
data.top = new FormAttachment(startText, 0, SWT.CENTER);
data.left = new FormAttachment(endText);
lengthLabel.setLayoutData(data);
lengthText = new Text(composite, SWT.NONE);
data = new FormData();
data.top = new FormAttachment(startText, 0, SWT.CENTER);
data.left = new FormAttachment(lengthLabel);
data.width = 100;
lengthText.setLayoutData(data);
// file
Label fileLabel = new Label(composite, SWT.NONE);
fileText = new Text(composite, SWT.NONE);
Button fileButton = new Button(composite, SWT.PUSH);
fileLabel.setText("File name: ");
data = new FormData();
data.top = new FormAttachment(fileButton, 0, SWT.CENTER);
fileLabel.setLayoutData(data);
data = new FormData();
data.top = new FormAttachment(fileButton, 0, SWT.CENTER);
data.left = new FormAttachment(fileLabel);
data.width = 300;
fileText.setLayoutData(data);
fileButton.setText("Browse...");
data = new FormData();
data.top = new FormAttachment(lengthText);
data.left = new FormAttachment(fileText);
fileButton.setLayoutData(data);
try
{
BigInteger startAddress = null;
if(fMemoryBlock instanceof IMemoryBlockExtension)
startAddress = ((IMemoryBlockExtension) fMemoryBlock)
.getBigBaseAddress(); // FIXME use selection/caret address?
else
startAddress = BigInteger.valueOf(fMemoryBlock.getStartAddress());
startText.setText("0x" + startAddress.toString(16));
endText.setText("0x" + startAddress.toString(16));
lengthText.setText("0");
}
catch(Exception e)
{
e.printStackTrace();
// TODO
}
fileButton.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
public void widgetSelected(SelectionEvent e) {
FileDialog dialog = new FileDialog(ExportMemoryDialog.this.getShell(), SWT.SAVE);
dialog.setText("Choose memory export file");
dialog.setFilterExtensions(new String[] { "*.*" } );
dialog.setFilterNames(new String[] { "All Files (*.*)" } );
dialog.setFileName(fileText.getText());
dialog.open();
if(dialog.getFileName() != null)
{
fileText.setText(dialog.getFilterPath() + File.separator + dialog.getFileName());
}
validate();
}
});
startText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
boolean valid = true;
try
{
getStartAddress();
}
catch(Exception ex)
{
valid = false;
}
startText.setForeground(valid ? Display.getDefault().getSystemColor(SWT.COLOR_BLACK) :
Display.getDefault().getSystemColor(SWT.COLOR_RED));
//
BigInteger endAddress = getEndAddress();
BigInteger startAddress = getStartAddress();
lengthText.setText(endAddress.subtract(startAddress).toString());
validate();
}
});
endText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
try
{
getEndAddress();
endText.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
BigInteger endAddress = getEndAddress();
BigInteger startAddress = getStartAddress();
String lengthString = endAddress.subtract(startAddress).toString();
if(!lengthText.getText().equals(lengthString))
lengthText.setText(lengthString);
}
catch(Exception ex)
{
endText.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_RED));
}
validate();
}
});
lengthText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
try
{
BigInteger length = getLength();
lengthText.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
BigInteger startAddress = getStartAddress();
String endString = "0x" + startAddress.add(length).toString(16);
if(!endText.getText().equals(endString))
endText.setText(endString);
}
catch(Exception ex)
{
lengthText.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_RED));
}
validate();
}
});
fileText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
validate();
}
});
return composite;
}
}

View file

@ -0,0 +1,194 @@
/*******************************************************************************
* Copyright (c) 2007 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Ted R Williams (Wind River Systems, Inc.) - initial implementation
*******************************************************************************/
package org.eclipse.dd.debug.memory.renderings.actions;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.core.model.IMemoryBlock;
import org.eclipse.debug.core.model.IMemoryBlockExtension;
import org.eclipse.debug.core.model.MemoryByte;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.internal.ui.views.memory.MemoryView;
import org.eclipse.debug.internal.ui.views.memory.MemoryViewIdRegistry;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.debug.ui.memory.IMemoryRendering;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
/**
* Action for downloading memory.
*/
public class ImportMemoryAction implements IViewActionDelegate {
private MemoryView fView;
public void init(IViewPart view) {
if (view instanceof MemoryView)
fView = (MemoryView) view;
}
public void run(IAction action) {
String secondaryId = MemoryViewIdRegistry
.getUniqueSecondaryId(IDebugUIConstants.ID_MEMORY_VIEW);
ISelection selection = fView.getSite().getSelectionProvider()
.getSelection();
if (selection instanceof IStructuredSelection) {
IStructuredSelection strucSel = (IStructuredSelection) selection;
// return if current selection is empty
if (strucSel.isEmpty())
return;
Object obj = strucSel.getFirstElement();
if (obj == null)
return;
IMemoryBlock memBlock = null;
if (obj instanceof IMemoryRendering) {
memBlock = ((IMemoryRendering) obj).getMemoryBlock();
} else if (obj instanceof IMemoryBlock) {
memBlock = (IMemoryBlock) obj;
}
Shell shell = DebugUIPlugin.getShell();
ImportMemoryDialog dialog = new ImportMemoryDialog(shell, memBlock);
dialog.open();
Object results[] = dialog.getResult();
if(results != null && results.length == 4)
{
String format = (String) results[0];
Boolean useCustomAddress = (Boolean) results[1];
BigInteger start = (BigInteger) results[2];
File file = (File) results[3];
if("S-Record".equals(format))
{
downloadSRecord(file, start, useCustomAddress.booleanValue(), (IMemoryBlockExtension) memBlock); // FIXME
}
}
}
}
private void downloadSRecord(final File inputFile, final BigInteger startAddress, final boolean useCustomAddress, final IMemoryBlockExtension memblock)
{
Job job = new Job("Memory Download from S-Record File"){ //$NON-NLS-1$
public IStatus run(IProgressMonitor monitor) {
try
{
try
{
// FIXME 4 byte default
final int CHECKSUM_LENGTH = 1;
BigInteger offset = null;
if(!useCustomAddress)
offset = BigInteger.ZERO;
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile)));
BigInteger jobs = BigInteger.valueOf(inputFile.length());
BigInteger factor = BigInteger.ONE;
if(jobs.compareTo(BigInteger.valueOf(0x7FFFFFFF)) > 0)
{
factor = jobs.divide(BigInteger.valueOf(0x7FFFFFFF));
jobs = jobs.divide(factor);
}
monitor.beginTask("Transferring Data", jobs.intValue());
BigInteger jobCount = BigInteger.ZERO;
String line = reader.readLine();
while(line != null)
{
String recordType = line.substring(0, 2);
int recordCount = Integer.parseInt(line.substring(2, 4), 16);
int bytesRead = 4 + recordCount;
int position = 4;
int addressSize = 0;
BigInteger recordAddress = null;
if("S3".equals(recordType))
addressSize = 4;
else if("S1".equals(recordType))
addressSize = 2;
else if("S2".equals(recordType))
addressSize = 3;
recordAddress = new BigInteger(line.substring(position, position + addressSize * 2), 16);
recordCount -= addressSize;
position += addressSize * 2;
if(offset == null)
offset = startAddress.subtract(recordAddress);
recordAddress = recordAddress.add(offset);
byte data[] = new byte[recordCount - CHECKSUM_LENGTH];
for(int i = 0; i < data.length; i++)
{
data[i] = new BigInteger(line.substring(position++, position++ + 1), 16).byteValue();
}
// FIXME error on incorrect checksum
memblock.setValue(recordAddress.subtract(memblock.getBigBaseAddress()), data);
jobCount = jobCount.add(BigInteger.valueOf(bytesRead));
while(jobCount.compareTo(factor) >= 0)
{
jobCount = jobCount.subtract(factor);
monitor.worked(1);
}
line = reader.readLine();
}
reader.close();
monitor.done();
}
catch(Exception e) { e.printStackTrace();}
}
catch(Exception e) {e.printStackTrace();}
return Status.OK_STATUS;
}};
job.setUser(true);
job.schedule();
}
public void selectionChanged(IAction action, ISelection selection) {
}
}

View file

@ -0,0 +1,283 @@
package org.eclipse.dd.debug.memory.renderings.actions;
import java.io.File;
import java.math.BigInteger;
import javax.swing.ButtonGroup;
import org.eclipse.debug.core.model.IMemoryBlock;
import org.eclipse.debug.core.model.IMemoryBlockExtension;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.SelectionDialog;
public class ImportMemoryDialog extends SelectionDialog
{
private Combo formatCombo;
private IMemoryBlock fMemoryBlock;
private Text startText;
private Text fileText;
private Button comboRestoreToThisAddress;
private Button comboRestoreToFileAddress;
public ImportMemoryDialog(Shell parent, IMemoryBlock memoryBlock)
{
super(parent);
super.setTitle("Download to Memory");
setShellStyle(getShellStyle() | SWT.RESIZE);
fMemoryBlock = memoryBlock;
}
/* (non-Javadoc)
* @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
*/
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);
validate();
}
/* (non-Javadoc)
* @see org.eclipse.ui.dialogs.SelectionDialog#getResult()
*/
public Object[] getResult() {
Object[] results = super.getResult();
if (results != null)
{
return results;
}
return new Object[0];
}
/* (non-Javadoc)
* @see org.eclipse.jface.dialogs.Dialog#cancelPressed()
*/
protected void cancelPressed() {
setResult(null);
super.cancelPressed();
}
/* (non-Javadoc)
* @see org.eclipse.jface.dialogs.Dialog#okPressed()
*/
protected void okPressed() {
setSelectionResult(new Object[]{ getFormat(), new Boolean(comboRestoreToThisAddress.getSelection()),
getStartAddress(), getFile() });
super.okPressed();
}
public String getFormat()
{
return formatCombo.getItem(formatCombo.getSelectionIndex());
}
public BigInteger getStartAddress()
{
String text = startText.getText();
boolean hex = text.startsWith("0x");
BigInteger startAddress = new BigInteger(hex ? text.substring(2) : text,
hex ? 16 : 10);
return startAddress;
}
public File getFile()
{
return new File(fileText.getText());
}
private void validate()
{
boolean isValid = true;
try
{
getStartAddress();
}
catch(Exception e)
{
isValid = false;
}
getButton(IDialogConstants.OK_ID).setEnabled(isValid);
}
/* (non-Javadoc)
* @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
*/
protected Control createDialogArea(Composite parent) {
PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, DebugUIPlugin.getUniqueIdentifier() + ".AddMemoryRenderingDialog_context"); //$NON-NLS-1$ // FIXME
Composite composite = new Composite(parent, SWT.NONE);
FormLayout formLayout = new FormLayout();
formLayout.spacing = 5;
formLayout.marginWidth = formLayout.marginHeight = 9;
composite.setLayout(formLayout);
// format
Label textLabel = new Label(composite, SWT.NONE);
textLabel.setText("Format: ");
formatCombo = new Combo(composite, SWT.BORDER | SWT.READ_ONLY);
FormData data = new FormData();
data.top = new FormAttachment(formatCombo, 0, SWT.CENTER);
textLabel.setLayoutData(data);
data = new FormData();
data.left = new FormAttachment(textLabel);
formatCombo.setLayoutData(data);
formatCombo.setItems( new String[] { "S-Record" }); // TODO offer extension point
formatCombo.select(0);
// restore to file address
comboRestoreToFileAddress = new Button(composite, SWT.RADIO);
comboRestoreToFileAddress.setText("Restore to address specified in the file");
data = new FormData();
data.top = new FormAttachment(formatCombo);
comboRestoreToFileAddress.setLayoutData(data);
// restore to this address
comboRestoreToThisAddress = new Button(composite, SWT.RADIO);
comboRestoreToThisAddress.setText("Restore to this address: ");
data = new FormData();
data.top = new FormAttachment(comboRestoreToFileAddress);
comboRestoreToThisAddress.setLayoutData(data);
startText = new Text(composite, SWT.NONE);
data = new FormData();
data.top = new FormAttachment(comboRestoreToFileAddress);
data.left = new FormAttachment(comboRestoreToThisAddress);
data.width = 100;
startText.setLayoutData(data);
// file
Label fileLabel = new Label(composite, SWT.NONE);
fileText = new Text(composite, SWT.NONE);
Button fileButton = new Button(composite, SWT.PUSH);
fileLabel.setText("File name: ");
data = new FormData();
data.top = new FormAttachment(fileButton, 0, SWT.CENTER);
fileLabel.setLayoutData(data);
data = new FormData();
data.top = new FormAttachment(fileButton, 0, SWT.CENTER);
data.left = new FormAttachment(fileLabel);
data.width = 300;
fileText.setLayoutData(data);
fileButton.setText("Browse...");
data = new FormData();
data.top = new FormAttachment(startText);
data.left = new FormAttachment(fileText);
fileButton.setLayoutData(data);
try
{
BigInteger startAddress = null;
if(fMemoryBlock instanceof IMemoryBlockExtension)
startAddress = ((IMemoryBlockExtension) fMemoryBlock)
.getBigBaseAddress(); // FIXME use selection/caret address?
else
startAddress = BigInteger.valueOf(fMemoryBlock.getStartAddress());
startText.setText("0x" + startAddress.toString(16));
}
catch(Exception e)
{
e.printStackTrace();
// TODO
}
fileButton.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
public void widgetSelected(SelectionEvent e) {
FileDialog dialog = new FileDialog(ImportMemoryDialog.this.getShell(), SWT.SAVE);
dialog.setText("Choose memory export file");
dialog.setFilterExtensions(new String[] { "*.*" } );
dialog.setFilterNames(new String[] { "All Files (*.*)" } );
dialog.setFileName(fileText.getText());
dialog.open();
if(dialog.getFileName() != null)
{
fileText.setText(dialog.getFilterPath() + File.separator + dialog.getFileName());
}
validate();
}
});
startText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
boolean valid = true;
try
{
getStartAddress();
}
catch(Exception ex)
{
valid = false;
}
startText.setForeground(valid ? Display.getDefault().getSystemColor(SWT.COLOR_BLACK) :
Display.getDefault().getSystemColor(SWT.COLOR_RED));
//
BigInteger startAddress = getStartAddress();
validate();
}
});
fileText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
validate();
}
});
return composite;
}
}