Eclipse ViewPart not showing a new panel
In my Eclipse plug-in I have a ViewPart that implements a ProgressListener. I start my adding a frame that will display my data plot and then load data from a file, which calls my progressComplete() when done. This works fine.
public void createPartControl(Composite parent)
{
parentComposite = 开发者_运维问答parent;
Composite composite = new Composite(parentComposite, SWT.EMBEDDED);
m_frame = SWT_AWT.new_Frame(composite);
String fileName = "/Users/fred/Documents/file.ipf";
startFileLoad(fileName, -1);
createActions();
}
However, I now want to remove the hardcoded file and have the user select a file from a button. I am using an Action for this button (where, for now, the button calls the same hardcoded filename) as:
m_actionOpenDataFile = new Action()
{
public void run()
{
String fileName = "/Users/fred/Documents/file.ipf";
startFileLoad(fileName, -1);
}
};
m_actionOpenDataFile.setText("Open");
m_actionOpenDataFile.setToolTipText("Open file");
m_actionOpenDataFile.setImageDescriptor(ImageDescriptor.createFromURL(ic.getURL("file.png")));
When selecting this button the action runs and the file is loaded, however the panel that displays the data is not shown on the Eclipse ViewPart. Can anyone suggest why this is so?
public void progressComplete(ProgressInfo info)
{
DataFile dataFile = (DataFile)info.getSource();
if(dataFile != null)
{
try
{
TData data = new TData(dataFile, this, "data_progress");
data.parsedFile();
DataRender dataRender = new DataRender(this, data);
DataPanel dataPanel = new DataPanel(data, dataRender);
dataRender.setPanel(dataPanel.getBufferPanel());
data.setAssociatedPanel(dataPanel);
data.addParserProgressListener(dataPanel);
m_frame.add(dataPanel);
When using the action button, I have attempted to pass the m_frame and ViewPart as part of the ProgressInfo, but the debug tells me that the variable cannot be accessed by the thread.
Thank you sgibly. From your suggestion I wrote:
m_actionOpenDataFile = new Action()
{
public void run()
{
new UIJob("load data")
{
public IStatus runInUIThread(IProgressMonitor monitor)
{
String fileName = "/Users/fred/Documents/file.ipf";
startFileLoad(fileName, -1);
return Status.OK_STATUS;
}
}.schedule();
}
};
This still does not give any output on the display. The Action run() creates a thread and this I guess creates the UI thread, or does it inherit the non UI properties?
From what I understand, you get an invalid thread access exception. If this is the case, you are probably trying to access the UI from a non-UI thread.
To fix that, you can wrap the call for the m_frame.add...
with a UIJob
, schedule it and join()
it, if you want the execution to be synchronized.
(another option is to use Display -> syncExec()
, but I would stick to the UIJob
)
精彩评论