i am trying to build an eclipse plugin. There are 2 following components of the plugin. 1) File Browser. 2) A JFreeChart
The goal: after browsing and selecting a file with the file browser, JFreeChart will make a chart with the selected file. No matter how many times i select different file, the chart will project those file into my required chart.
I have one viewpart. Both File-Browser and JFreeChart uses the parent composite. I have already implemented the static part that means with my plugin i can browse files and the chart can represent any file (not the file i browse yet and that's is where i am stuck). Here i want to make a chart that will update as i browse to different files. Please have a look at my code.
FileChooser.java
mButton = new Button(this, SWT.NONE);
mButton.setText("Browse");
//mText.setText("F:\\Java Works\\Ground\\bin\\test.txt");
mText.setText("");
chartDraw = mText.getText();
mButton.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
FileDialog dlg = new FileDialog(mButton.getShell(), SWT.OPEN );
dlg.setText("Open");
path = dlg.open();
if (path == null) return;
mText.setText(path);
chartDraw = mText.getText();
}
});
ChartView.java
public void createPartControl(Composite parent){
Composite top = new Composite(parent, SWT.NONE);// embedded Composite
// setup the layout of top to be GridLayout.
GridLayout layout1 = new GridLayout();
layout1.marginHeight = 0;
layout1.marginWidth = 0;
top.setLayout(layout1);
// top banner
Composite banner = new Composite(top, SWT.NONE);// banner is added to
// "top"
banner.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL,
GridData.VERTICAL_ALIGN_BEGINNING, true, false));
layout1 = new GridLayout();
layout1.marginHeight = 5;
layout1.marginWidth = 10;
layout1.numColumns = 1;
banner.setLayout(layout1);
// setup bold font
Font boldFont = JFaceResources.getFontRegistry().getBold(
JFaceResources.DEFAULT_FONT);
GridData gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
gridData.minimumWidth = 400;
gridData.minimumHeight = 50;
gridData.grabExcessHorizontalSpace = true;
Label l = new Label(banner, SWT.WRAP);
l.setText("Source File:");
l.setFont(boldFont);
final FileChooser fileChooser = new FileChooser(banner);
gridData.heightHint = 25;
fileChooser.setLayoutData(gridData);
ami = fileChooser.getchartDraw();
// Viewing Chart Starts Here
FetchDataChart chart1 = new FetchDataChart();
//XYSeriesCollection dataset = chart1.createDataset();
XYSeriesCollection dataset = chart1.createDataset(fileChooser.getchartDraw()); // I am extracting the file path and drawing the chart.
JFreeChart chart;
try {
chart = chart1.createChart(dataset);
ChartComposite frame = new ChartComposite(parent, SWT.NONE, chart, true);
frame.pack();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getAmi() {
return ami;
}
Now my question is how can i connect with FileBrowser and JFreeChart so that i can know the file path immediately after it changes and the view part shows the chart as file changes in FileBrowser? Some guideline and reference would be helpful. For your kind information, i am totally new in Eclipse PDE/RCP zone.
Thanks for the help. Please let me know if you want to know something more to solve this issue.
I have used command handler way to resolve this issue. I used command parameter in FileChooser.java to pass the file-path to the handler and then handler create chart based on that. But of course you have to dispose the frame that you created before to obtain a new refreshed/updated view. See below the handler class for better understanding. Have a look at the changed portion of codes below.
FileChooser.java
mButton.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
FileDialog dlg = new FileDialog(mButton.getShell(), SWT.OPEN );
dlg.setText("Open");
path = dlg.open();
if (path == null) return;
mText.setText(path);
chartDraw = mText.getText();
ArrayList<Parameterization> parameters = new ArrayList<Parameterization>();
IParameter iparam;
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
ICommandService cmdService = (ICommandService)window.getService(ICommandService.class);
Command cmd = cmdService.getCommand("press.command");
try {
//get the parameter
iparam = cmd.getParameter("press.command.parameter");
Parameterization params = new Parameterization(iparam, chartDraw);
parameters.add(params);
//build the parameterized command
ParameterizedCommand pc = new ParameterizedCommand(cmd, parameters.toArray(new Parameterization[parameters.size()]));
//execute the command
IHandlerService handlerService = (IHandlerService)window.getService(IHandlerService.class);
handlerService.executeCommand(pc, null);
} catch (ExecutionException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NotDefinedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NotEnabledException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NotHandledException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
And the handler class is like below:
public class Handler extends AbstractHandler{
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
String filePath = event
.getParameter("press.command.parameter");
IWorkbenchPage workbenchPage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
IViewPart view = workbenchPage.findView("freechart.views.ChartView");
if (view instanceof ChartView) {
ChartView myView = (ChartView)view;
myView.frame.dispose();
myView.createChart(filePath);
myView.parent.pack();
myView.parent.layout(true);
}
return null;
}
}