Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Friday, October 26, 2007

Communication between Views using eventLoopIdle

I have been working on an application that is now up to 3 swing components embedded in my Eclipse RCP / SWT application. This has become problematic having these 3rd party components to communicate properly with each other. While it is easy to communicate with these other components embedded in other views I was having a problem with RCP not properly repainting the screen. So I have come up with a work around that works for me and I thought I would share it with any other Eclipse Rich Client Platform developer out there that may be struggling.

If you open up ApplicationWorkbenchAdvisor.java - there is a function in there called public void eventLoopIdle(Display display). In this function I put my calls. I used my Application Singleton class to facilitate communication. By adding a semaphore type variable I can see if other views need to go and update their screen. In my specific case a user has a view of a PDF and when they click on an action object this needs to send the file information to a chart window that then opens the file clicked on in the PDF view, parse it and display the chart with the give options defined by the PDF link.

If you have any questions on this just email or leave me a comment. Good luck!

Friday, August 17, 2007

Tabbed Views in Eclipse RCP

Tabbed Views in Eclipse RCP sounds really complicated but Eclipse has made this really easy. Just follow these simple instructions and you will be off and running with multiple tabbed views.

  1. Open your perspective.java file (If you don't have this file yet, you are in trouble!)
  2. in public void createInitialLayout(IPageLayout layout) you should have some code that looks similar to this:
    layout.addStandaloneView(ItagTreeView.ID, true, IPageLayout.LEFT, 1.0f, editorArea);
    layout.addStandaloneView(ChartView.ID, true, IPageLayout.RIGHT, 0.5f, ItagTreeView.ID);
    layout.addStandaloneView(FormView.ID, true, IPageLayout.BOTTOM, 0.5f, ItagTreeView.ID);

    // create a folder for multiple analysis views so that they will open up
    // in tabs next to the table view.
    IFolderLayout folder = layout.createFolder("Tables", IPageLayout.BOTTOM, 0.5f, ChartView.ID);
    folder.addPlaceholder(AnalysisView.ID + ":*");
    folder.addView(TableView.ID);

    The above code has 4 views and the table view is it. Our AnalysisView is a 2nd tab under the TableView.

    You can see instead of calling addStandaloneView we call createFolder. The "folder" is how we combine several views to make a tabbed view presentation.
I hope that this will help anyone struggling to add tabbed views for an Eclipse RCP application. If you have any questions or comments please feel free to contact me.

Thursday, August 16, 2007

Java Application from Existing Source in Eclipse

For many newer people you may not find good documentation on how to quickly and easily create a new Eclipse project from existing java files. This is really easy if the source has an ANT build file but if you don't it becomes a little less obvious, at least until you have done it once or twice. Thus I thought I would blog about this simple yet not-so-well documented functionality.

Steps to create a project in eclipse from existing java files.
  1. Create an Empty Workspace in Eclipse (lets call it Test)
  2. Now Using your file browser go to the folder where the new .classpath and .projects are. This will be your "Test" folder under the workspace folder.
  3. From here drill down to your src folder. In this directory drag and drop the top level of the source code you have, for example mine is "com" folder and under this are all my packages and java files.
  4. Go up to the same level as the src folder and go down into your lib folder.
  5. In here copy all required JAR files that your java files have.
  6. Now go back into Eclipse and on the left under your Package Explorer right click the src folder and pick refresh. Do the same for your lib folder.
  7. Now right click on your project (in our example Test) . Select Properties. Pick "Java Build Path" on the left. Then click "Add External JARS.." on the right. Add all the .jar files you did in step 5.
  8. Thats it! You now need to do run and define what it is you're running but that is self-explanatory.
Good luck and leave me a comment if you have any questions or problems.

Monday, August 6, 2007

Eclipse RCP Views - How to communicate between them

When I was starting out my Eclispe program life, which was not very long ago, I had quite the dilemna of how in the hell would I have ViewOne communicate with ViewTwo, three and four. After talking to a good friend of mine he introduced me to an old idea that still works great. Use a class called ApplicationSingleton. This class is really very handy and I thought I'd talk about it here with a simple example.



public class ApplicationSingleton {
static private ApplicationSingleton instance;
private ItagTreeView.TreeParent chart;

public static ApplicationSingleton getInstance() {
if (instance == null) {
instance = new ApplicationSingleton();
}
return instance;
}
private ApplicationSingleton() {
super();
}
public void SetCurrentChart(ItagTreeView.TreeParent tp) {
chart = tp;
}
public ItagTreeView.TreeParent getCurrentChart() {
return chart;
}
}



Now to talk about some parts of this class.

  1. We include a private variable of our class called "instance"
  2. From our application code we make calls to getInstance(), such as this:
    TreeParent tp = ApplicationSingleton.getInstance().GetCurrentChart(); The first time we call getInstance it will make an instance of this class.
  3. By using setCurrentChart() we can store our variables in view1 and in view2 we can use getCurrentChart.
Using this method allowed me to have two distinct view communicate. One view was my ChartView and the other was my FormView. I also tied in a TreeView so when a user double clicks a chart node it grabs the data for that node and displays a chart and resets the form view for the given chart.

Easy, right? :) If you have any questions or comments I'd love to hear them!

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!