[go: up one dir, main page]

Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Nebula » [CompositeTable] Sample code to demonstrate databinding in a CompositeTable
[CompositeTable] Sample code to demonstrate databinding in a CompositeTable [message #31269] Thu, 08 March 2007 19:30 Go to next message
Eclipse UserFriend
Originally posted by: gsimonds.loomissayles.com

Hi

Does anybody have any sample code they could pass along to demonstrate
databinding to a CompositeTable? I saw Martin's post in January
requesting sample code and am afraid that I am still a bit in the dark.
I am looking at using CompositeTable but first need to see how
difficult the databinding will be.

Thanks.

Geoff
Re: [CompositeTable] Sample code to demonstrate databinding in a CompositeTable [message #31868 is a reply to message #31269] Fri, 16 March 2007 16:13 Go to previous message
Dave Orme is currently offline Dave OrmeFriend
Messages: 424
Registered: July 2009
Senior Member
Geoff wrote:
> Hi
>
> Does anybody have any sample code they could pass along to demonstrate
> databinding to a CompositeTable? I saw Martin's post in January
> requesting sample code and am afraid that I am still a bit in the dark.
> I am looking at using CompositeTable but first need to see how
> difficult the databinding will be.

Here's the basic idea:

public class CompositeTableSnippet7 {
// First some data...
private static class Name {
private String first;
private String last;

public Name(String first, String last) {
this.first = first;
this.last = last;
}

public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
}

static Name[] swtCommitters = new Name[] {
new Name("Grant", "Gayed"),
new Name("Veronika", "Irvine"),
new Name("Steve", "Northover"),
new Name("Mike", "Wilson"),
new Name("Christophe", "Cornu"),
new Name("Lynne", "Kues"),
new Name("Silenio", "Quarti"),
new Name("Tod", "Creasey"),
new Name("Felipe", "Heidrich"),
new Name("Billy", "Biggs"),
new Name("B", "Shingar")
};

static class Opposite implements Comparator {
private Comparator c;
public Opposite(Comparator c) {
this.c = c;
}
public int compare(Object o1, Object o2) {
return -1 * c.compare(o1, o2);
}
}

static final Comparator firstNameAscending = new Comparator() {
public int compare(Object o1, Object o2) {
Name name1 = (Name) o1;
Name name2 = (Name) o2;
return name1.first.compareToIgnoreCase(name2.first);
};
};
static final Comparator firstNameDescending = new
Opposite(firstNameAscending);

static final Comparator lastNameAscending = new Comparator() {
public int compare(Object o1, Object o2) {
Name name1 = (Name) o1;
Name name2 = (Name) o2;
return name1.last.compareToIgnoreCase(name2.last);
};
};
static final Comparator lastNameDescending = new
Opposite(lastNameAscending);


private static class Header extends AbstractNativeHeader {
public Header(Composite parent, int style) {
super(parent, style);
setWeights(new int[] { 160, 100 });
setColumnText(new String[] {"First name",
"Last name"});
}

protected boolean sortOnColumn(int column,
int sortDirection) {
Comparator comparator = null;

if (column == 0) {
if (sortDirection == SWT.DOWN) {
comparator =
firstNameDescending;
} else {
comparator = firstNameAscending;
}
} else {
if (sortDirection == SWT.DOWN) {
comparator = lastNameDescending;
} else {
comparator = lastNameAscending;
}
}

Arrays.sort(swtCommitters, comparator);
table.refreshAllRows();

return true;
}
}

/*
* So that the row's columns size with the header, we use a
* ResizableGridRowLayout here. Weights and fittingHorizontally
* is gotten from the header, so we don't need to set anything else.
*/
private static class Row extends AbstractSelectableRow {
public Row(Composite parent, int style) {
super(parent, style);
setLayout(new ResizableGridRowLayout());
setColumnCount(2);
}
}

private static CompositeTable table;


// Where it all starts...

public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setText("CompositeTable Snippet 5 -- List first/last names
with sortable, movable, resizable columns and a native header");
shell.setLayout(new FillLayout());

table = new CompositeTable(shell, SWT.NULL);
new Header(table, SWT.NULL); // Just drop the Header and Row
new Row(table, SWT.NULL);
table.setRunTime(true);
table.setNumRowsInCollection(swtCommitters.length);

// Note the JFace-like virtual table API
table.addRowContentProvider(new IRowContentProvider() {
public void refresh(CompositeTable sender,
final int currentObjectOffset,
Control rowControl) {
// First, dispose old bindings...
final Row row = (Row) rowControl;
DataBindingContext oldContext =
(DataBindingContext)
row.getData("DataBindingContext");
if (oldContext != null) {
oldContext.dispose();
}

// Now, create new bindings.
Realm.runWithDefault(
SWTObservables.getRealm(
row.getDisplay()), new Runnable() {
public void run() {
DataBindingContext newContext =
new DataBindingContext();
row.setData("DataBindingContext",
newContext);
Control[] children =
row.getChildren();
newContext.bindValue(
SWTObservables.observeText(
(Label)children[0]),
BeansObservables.observeValue(
swtCommitters[currentObjectOffset],
"first"),
null, null);
newContext.bindValue(
SWTObservables.observeText(
(Label)children[1]),
BeansObservables.observeValue(
swtCommitters[currentObjectOffset],
"last"),
null, null);
}});
}
});

shell.setSize(500, 150);
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}

}




--
Senior Consultant, Trainer
Coconut Palm Software, Inc.
http://www.coconut-palm-software.com
Re: [CompositeTable] Sample code to demonstrate databinding in a CompositeTable [message #580238 is a reply to message #31269] Fri, 16 March 2007 16:13 Go to previous message
Dave Orme is currently offline Dave OrmeFriend
Messages: 424
Registered: July 2009
Senior Member
Geoff wrote:
> Hi
>
> Does anybody have any sample code they could pass along to demonstrate
> databinding to a CompositeTable? I saw Martin's post in January
> requesting sample code and am afraid that I am still a bit in the dark.
> I am looking at using CompositeTable but first need to see how
> difficult the databinding will be.

Here's the basic idea:

public class CompositeTableSnippet7 {
// First some data...
private static class Name {
private String first;
private String last;

public Name(String first, String last) {
this.first = first;
this.last = last;
}

public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
}

static Name[] swtCommitters = new Name[] {
new Name("Grant", "Gayed"),
new Name("Veronika", "Irvine"),
new Name("Steve", "Northover"),
new Name("Mike", "Wilson"),
new Name("Christophe", "Cornu"),
new Name("Lynne", "Kues"),
new Name("Silenio", "Quarti"),
new Name("Tod", "Creasey"),
new Name("Felipe", "Heidrich"),
new Name("Billy", "Biggs"),
new Name("B", "Shingar")
};

static class Opposite implements Comparator {
private Comparator c;
public Opposite(Comparator c) {
this.c = c;
}
public int compare(Object o1, Object o2) {
return -1 * c.compare(o1, o2);
}
}

static final Comparator firstNameAscending = new Comparator() {
public int compare(Object o1, Object o2) {
Name name1 = (Name) o1;
Name name2 = (Name) o2;
return name1.first.compareToIgnoreCase(name2.first);
};
};
static final Comparator firstNameDescending = new
Opposite(firstNameAscending);

static final Comparator lastNameAscending = new Comparator() {
public int compare(Object o1, Object o2) {
Name name1 = (Name) o1;
Name name2 = (Name) o2;
return name1.last.compareToIgnoreCase(name2.last);
};
};
static final Comparator lastNameDescending = new
Opposite(lastNameAscending);


private static class Header extends AbstractNativeHeader {
public Header(Composite parent, int style) {
super(parent, style);
setWeights(new int[] { 160, 100 });
setColumnText(new String[] {"First name",
"Last name"});
}

protected boolean sortOnColumn(int column,
int sortDirection) {
Comparator comparator = null;

if (column == 0) {
if (sortDirection == SWT.DOWN) {
comparator =
firstNameDescending;
} else {
comparator = firstNameAscending;
}
} else {
if (sortDirection == SWT.DOWN) {
comparator = lastNameDescending;
} else {
comparator = lastNameAscending;
}
}

Arrays.sort(swtCommitters, comparator);
table.refreshAllRows();

return true;
}
}

/*
* So that the row's columns size with the header, we use a
* ResizableGridRowLayout here. Weights and fittingHorizontally
* is gotten from the header, so we don't need to set anything else.
*/
private static class Row extends AbstractSelectableRow {
public Row(Composite parent, int style) {
super(parent, style);
setLayout(new ResizableGridRowLayout());
setColumnCount(2);
}
}

private static CompositeTable table;


// Where it all starts...

public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setText("CompositeTable Snippet 5 -- List first/last names
with sortable, movable, resizable columns and a native header");
shell.setLayout(new FillLayout());

table = new CompositeTable(shell, SWT.NULL);
new Header(table, SWT.NULL); // Just drop the Header and Row
new Row(table, SWT.NULL);
table.setRunTime(true);
table.setNumRowsInCollection(swtCommitters.length);

// Note the JFace-like virtual table API
table.addRowContentProvider(new IRowContentProvider() {
public void refresh(CompositeTable sender,
final int currentObjectOffset,
Control rowControl) {
// First, dispose old bindings...
final Row row = (Row) rowControl;
DataBindingContext oldContext =
(DataBindingContext)
row.getData("DataBindingContext");
if (oldContext != null) {
oldContext.dispose();
}

// Now, create new bindings.
Realm.runWithDefault(
SWTObservables.getRealm(
row.getDisplay()), new Runnable() {
public void run() {
DataBindingContext newContext =
new DataBindingContext();
row.setData("DataBindingContext",
newContext);
Control[] children =
row.getChildren();
newContext.bindValue(
SWTObservables.observeText(
(Label)children[0]),
BeansObservables.observeValue(
swtCommitters[currentObjectOffset],
"first"),
null, null);
newContext.bindValue(
SWTObservables.observeText(
(Label)children[1]),
BeansObservables.observeValue(
swtCommitters[currentObjectOffset],
"last"),
null, null);
}});
}
});

shell.setSize(500, 150);
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}

}




--
Senior Consultant, Trainer
Coconut Palm Software, Inc.
http://www.coconut-palm-software.com
Previous Topic:[Grid]Possibility to setTree() on rowheader column?
Next Topic:grid column rendering
Goto Forum:
  


Current Time: Fri Dec 13 17:33:44 GMT 2024

Powered by FUDForum. Page generated in 0.03400 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top