Wednesday, August 1, 2007

Slush Bucket for Eclipse RCP

I have been developing an Eclipse RCP application for one of my clients. I sure wish I had started to blog it from the very beginning as I have learned so much that isn't documented properly online or in the books I have. I guess it's better late then never so here we go.


The latest stumbling block I ran into was that Eclipse SWT (their replacement to Java Swing) did not have slush buckets or also referred to as a pick list. So I decided to code my own. Now that I have I thought I would post my working code for others to use.

Below is the code as I have it and I will also throw in some very important notes about the code below the code. Such as my use of an Application Singleton class. My way of coding it may not be the most efficient, I really don't know, but it works for me and while searching for this type of code I noticed a lot of other people needed help on this too. So I helps some other Eclipse people out there!

Label loglabel = toolkit.createLabel(sectionClient, "Logistic Regression Independent Variables:");
Composite logGroup;
logGroup = new Composite(sectionClient, SWT.FLAT);
logGroup.setLayout(new GridLayout(3, false));
logGroup.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL | GridData.GRAB_HORIZONTAL));
List list = new List (logGroup, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);

GridData g = new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1);
g.heightHint = 150;
g.minimumWidth = 30;
list.setLayoutData(g);
list.setToolTipText("The source list of fields you can choose. \nHighlight the fields you wish to have as independent variables \nin the logistic regression and move to the right list box\nusing the center buttons.");
list.setItems(data[0]); // 0 is the headers
ApplicationSingleton.getInstance().setSelList(list);
Group middleGroup;
middleGroup = new Group(logGroup, SWT.FLAT);
middleGroup.setLayout(new GridLayout(1, false));

Button bright;
bright = new Button(middleGroup, SWT.PUSH);
bright.setText(">");
bright.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
bright.addSelectionListener(new SelectionListener() {

public void widgetSelected(SelectionEvent event) {
List list = ApplicationSingleton.getInstance().getSelList();
int [] selections;
selections = list.getSelectionIndices();
List list2 = ApplicationSingleton.getInstance().getTargetList();
for(int i=0; i < selections.length; i++)
{
list2.add(list.getItem(selections[i]));
}
list.remove(selections);
}
public void widgetDefaultSelected(SelectionEvent event) {
}

});
Button bleft;
bleft = new Button(middleGroup, SWT.PUSH);
bleft.setText("<");
bleft.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
bleft.addSelectionListener(new SelectionListener() {

public void widgetSelected(SelectionEvent event) {
List list2 = ApplicationSingleton.getInstance().getTargetList();
List list = ApplicationSingleton.getInstance().getSelList();
int [] selections;
selections = list2.getSelectionIndices();
for(int i=0; i < selections.length; i++)
{
list.add(list2.getItem(selections[i]));
}
list2.remove(selections);
}
public void widgetDefaultSelected(SelectionEvent event) {
}

});
Button bfullright;
bfullright = new Button(middleGroup, SWT.PUSH);
bfullright.setText(">>");
bfullright.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
bfullright.addSelectionListener(new SelectionListener() {

public void widgetSelected(SelectionEvent event) {
List list2 = ApplicationSingleton.getInstance().getTargetList();
List list = ApplicationSingleton.getInstance().getSelList();
String [] strings, strings2;
strings = list.getItems();
strings2 = list2.getItems();

// Combine both sets and move it all to the right
String[] temp = new String[strings.length + strings2.length];
System.arraycopy(strings, 0, temp, 0, strings.length);
System.arraycopy(strings2, 0, temp, strings.length, strings2.length);
list2.setItems(temp);

list.removeAll();
}
public void widgetDefaultSelected(SelectionEvent event) {
}

});
Button bfullleft;
bfullleft = new Button(middleGroup, SWT.PUSH);
bfullleft.setText("<<");
bfullleft.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
bfullleft.addSelectionListener(new SelectionListener() {

public void widgetSelected(SelectionEvent event) {
List list2 = ApplicationSingleton.getInstance().getTargetList();
List list = ApplicationSingleton.getInstance().getSelList();
String [] strings, strings2;
strings = list.getItems();
strings2 = list2.getItems();

// Combine both sets and move it all to the right
String[] temp = new String[strings.length + strings2.length];
System.arraycopy(strings, 0, temp, 0, strings.length);
System.arraycopy(strings2, 0, temp, strings.length, strings2.length);
list.setItems(temp);

list2.removeAll();
}
public void widgetDefaultSelected(SelectionEvent event) {
}

});

List list2 = new List (logGroup, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
g = new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1);
g.heightHint = 150;
g.minimumWidth = 30;
list2.setLayoutData(g);
ApplicationSingleton.getInstance().setTargetList(list2);
Button bRedrawChart;
bRedrawChart = new Button(logGroup, SWT.PUSH);
bRedrawChart.setText("Redraw Chart");
bRedrawChart.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 3, 2));
bRedrawChart.setToolTipText("After selecting the Indepenent Variables\nfor your Logistic Regression Click Redraw.");
bRedrawChart.addSelectionListener(new SelectionListener() {

public void widgetSelected(SelectionEvent event) {
Button b = ApplicationSingleton.getInstance().getY1LogButton();
b.setSelection(true);
ApplicationSingleton.getInstance().getCurrentChart().doY1Logistic(true);
List list2 = ApplicationSingleton.getInstance().getTargetList();
String [] strings;
strings = list2.getItems();
ApplicationSingleton.getInstance().getCurrentChart().setLogX(strings);
ChartView view = (ChartView)getSite().getWorkbenchWindow().getActivePage().findView(ChartView.ID);
view.loadNewChart();
}
public void widgetDefaultSelected(SelectionEvent event) {
}

});


So in my code I am doing a slush bucket to determine which columns of data should be used for logistic regression. With that said, the biggest challenge was how do I expose variables, such as the list boxes, to the selection listener events? Well you write a class called "ApplicationSingleton" that sets and gets variables that then can be used anywhere in your application by doing a getInstance of the one ApplicationSingleton class we have.

So I hope this helps and if you have any questions please let me know!

5 comments:

Andi said...

I would appriciate it if you could give some answer to my question: I am developing a database-based application with 2 views.

So where is the right place to save the database connection?? Both views share the same connection.

Thanks

Andi

Unknown said...

Read my post on exactly how to do that here:
http://gopickmybrain.blogspot.com/2007/08/eclipse-rcp-views-how-to-communicate.html

Andi said...

sorry, but the link is not available :-(

Unknown said...

Looks like leaving comments got the url truncated. Just go to my 2007 posts and look for the blog titled 'Eclipse RCP Views - How to communicate between them' (or google search the site).

Andi said...

>3. By using setCurrentChart() we can store our variables in view1 and in view2 we can use getCurrentChart.

sorry, could you please provide an example???