Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » Databinding with Enum + Combo
Databinding with Enum + Combo [message #5376] Fri, 15 May 2009 02:30 Go to next message
Charlie Kelly is currently offline Charlie KellyFriend
Messages: 276
Registered: July 2009
Senior Member
Are there any examples/snippets available for databinding with a enum
and a Combo?

Thanks

Charlie
Re: Databinding with Enum + Combo [message #6152 is a reply to message #5376] Fri, 15 May 2009 17:51 Go to previous messageGo to next message
Boris Bokowski is currently offline Boris BokowskiFriend
Messages: 272
Registered: July 2009
Senior Member
Hi Charlie,

Not that I am aware of, but if you could contribute one that would be great.
Currently, the snippets/examples plug-in does not use Java 1.5 features but
we would change that as soon as we add snippets dealing with enums,
generics, etc.

http://wiki.eclipse.org/JFace_Data_Binding/How_to_Contribute

Boris

"Charlie Kelly" <Eclipse@CharlieKelly.com> wrote in message
news:guik5e$c2n$1@build.eclipse.org...
> Are there any examples/snippets available for databinding with a enum and
> a Combo?
>
> Thanks
>
> Charlie
Re: Databinding with Enum + Combo [message #6195 is a reply to message #6152] Fri, 15 May 2009 19:42 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: fakemail.xyz.de

This is a multi-part message in MIME format.
--------------050409030108030606030603
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Boris Bokowski schrieb:
> Hi Charlie,
>
> Not that I am aware of, but if you could contribute one that would be great.
> Currently, the snippets/examples plug-in does not use Java 1.5 features but
> we would change that as soon as we add snippets dealing with enums,
> generics, etc.
>
> http://wiki.eclipse.org/JFace_Data_Binding/How_to_Contribute
>
> Boris
>
> "Charlie Kelly" <Eclipse@CharlieKelly.com> wrote in message
> news:guik5e$c2n$1@build.eclipse.org...
>> Are there any examples/snippets available for databinding with a enum and
>> a Combo?
>>
>> Thanks
>>
>> Charlie
>
>

I once implemented a Simple one .. may be its enough for you..







--------------050409030108030606030603
Content-Type: text/java;
name="ComboBoxViewer.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="ComboBoxViewer.java"

package uihelpers;

import helpers.GH;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.swt.widgets.Combo;

public class ComboBoxViewer<T> {


private final List<T> items;
private final Combo combo;


public ComboBoxViewer(Combo combo,T[] items) {
this(combo,Arrays.asList(items));
}

public ComboBoxViewer(Combo combo, List<T> items) {
this(combo,items, false);
}

/**
*
* @param combo - the combo for which this viewer is
* @param items - the items that can be choosen
* @param addEmptyItem - if an additional empty item should be added "no item selected"
*/
public ComboBoxViewer(Combo combo, List<T> items,boolean addEmptyItem) {
this.combo = combo;
this.items = new ArrayList<T>(items);
if (addEmptyItem) {
this.items.add(0, null);
}

for (T t: this.items) {
combo.add(get(t));
}
}

/**
* selects the given item in the ComboBox
*
* @param t - one item that is presenting the list
* throws IllegalArgumentException if not in the list.
*/
public void select(T t) {
int i = items.indexOf(t);
if (i == -1) {
throw new IllegalArgumentException();
} else {
combo.select(i);
}
}

/**
* selects the first item that will return the same string
* @param s
*/
public void selectByString(String s) {
for (T t : items) {
if ( GH.isNullOrEmpty(s) ? GH.isNullOrEmpty(get(t)) : s.equals(get(t))) {
select(t);
}
}
}

private String get(T t) {
if (t == null) {
return "";
}else {
return getShown(t);
}
}

/**
*
* @param t - an item that should be presented
* implementing classes should override this method..
*
* @return bhy default toString() is returned
*/
protected String getShown(T t) {
return t.toString();
}

/**
*
* @return the currently selected item
*/
public T getSelected() {
int i = combo.getSelectionIndex();
if (i != -1) {
return items.get(i);
}
return null;
}

public String getSelectedString() {
return get(getSelected());
}

}

--------------050409030108030606030603
Content-Type: text/java;
name="GH.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="GH.java"

package helpers;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Collection;
import java.util.Random;

import javax.swing.filechooser.FileSystemView;

/**
* Global Helpers that are useful for everyone..
*
* @author Quicksilver
*
*/
public final class GH {

private static final Random rand = new Random();

private static final FileSystemView chooser = FileSystemView.getFileSystemView();

/*
*
* @see java.util.Random#nextInt(int)
*/
public static int nextInt(int n) {
synchronized(rand) {
return rand.nextInt(n);
}
}
public static int nextInt() {
synchronized(rand) {
return rand.nextInt();
}
}




private GH() {}


/**
* provided for all the might use it..
* as creating costs memory that is never recovered..
* @return a FileSytemView instance..
*/
public static FileSystemView getFileSystemView() {
return chooser;
}

/**
* emulates behaviour of FileSystemView as good as possible...
* though its not the same..
* -> done as normal FileSystemView has a memory leak on that method..
*/
public static File[] getFiles(File parent,boolean useHidden) {
File[] files = parent.listFiles();
if (files == null) return new File[0];

if (useHidden) {
// int length = files.length;
int k = 0;
for (int i = 0; i+k < files.length; i++) {
while (i+k < files.length && files[i+k].isHidden()) {
k++;
}
if (i+k < files.length) {
files[i] = files[i+k];
}
}

if (k != 0) {
File[] onlyVisible = new File[files.length - k];
System.arraycopy(files, 0, onlyVisible, 0, onlyVisible.length );
return onlyVisible;
}
}
return files;
}

/**
* closes all provided streams ignoring exceptions
* @param closeable
*/
public static void close(Closeable... closeable) {
for (Closeable c: closeable) {
try {
if (c != null) {
c.close();
}
} catch (IOException e) {
} catch (RuntimeException e) {
e.printStackTrace();
}
}
}

/**
* sleeps ignoring interruption handling..
*
* @param millis - how long to sleep
*/
public static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch(InterruptedException ie) {}
}

/**
* replaces invalid characters in Filenames
*/
public static String replaceInvalidFilename(String filename) {

filename = filename.replace("\\", ".");
filename = filename.replace("/", ".");
filename = filename.replace("*", ".");
filename = filename.replace("?", ".");
filename = filename.replace("\"", "\'");
filename = filename.replace("<", ".");
filename = filename.replace(">", ".");
filename = filename.replace("|", ".");
filename = filename.replace(":", "-");

return filename;
}

/**
* replaces invalid filepath
*/
public static String replaceInvalidFilpath(String filename) {

filename = filename.replace(File.separator.equals("\\")?"/": "\\", ".");
filename = filename.replace("*", ".");
filename = filename.replace("?", ".");
filename = filename.replace("\"", "\'");
filename = filename.replace("<", ".");
filename = filename.replace(">", ".");
filename = filename.replace("|", ".");
filename = filename.replace(":", "-");

return filename;
}



/**
* replaces newline characters with \n
* and \ with \\
*
* so it does basic escaping
*
* @param s
* @return
*/
public static String replaces(String s) {
s = s.replace("\\", "\\\\");
s = s.replace("\n", "\\n");
return s;
}

/**
* reverses replacements of replace function
*/
public static String revReplace(String s) {
int i = 0;
while ((i = s.indexOf('\\',i)) != -1) {
if (s.length() > i+1) {
char c = s.charAt(i+1);
if (c == '\\') {
s = s.substring(0, i)+"\\"+s.substring(i+2) ;
} else if (c == 'n') {
s = s.substring(0, i)+"\n"+s.substring(i+2);
}
}
i++;
}

return s;
}

public static boolean isLocaladdress(InetAddress ia) {
return ia.isLoopbackAddress()|| ia.isSiteLocalAddress();
}


public static String getStacktrace(Thread t) {
String s = t.getName();
for (StackTraceElement ste:t.getStackTrace()) {
s += "\n"+ste.getFileName()+" Line:"+ste.getLineNumber()+" "+ste.getMethodName();
}
return s;
}

public static boolean isEmpty(String s) {
return s.length() == 0;
}

public static boolean isNullOrEmpty(String s) {
return s == null || s.length() == 0;
}

public static String toString(Object[] arr) {
if (arr == null) {
return "null";
}
if (arr.length == 0) {
return "{empty}";
}
String s = "";
for (Object o:arr) {
s+=","+o.toString();
}
return "{"+s.substring(1)+"}";
}


public static byte[] concatenate(byte[]... arrays) {
int totalsize= 0;
for (byte[] array: arrays) {
totalsize += array.length;
}
byte[] all = new byte[totalsize];
int currentpos = 0;
for (byte[] array: arrays) {
System.arraycopy(array, 0, all, currentpos, array.length);
currentpos += array.length;
}
return all;

}

public static int compareTo(int a , int b) {
return (a < b ? -1 : (a==b ? 0 : 1));
}

public static int compareTo(byte a , byte b) {
return (a < b ? -1 : (a==b ? 0 : 1));
}

public static int unsingedCompareTo(byte a,byte b) {
return compareTo(a & 0xff , b & 0xff);
}

/**
* concatenates each term in collection using .toString()
* and puts between each string "between"
*
* if the map is empty it will return the empty map string instead...
*
* prefix and postfix are applied around everything..
*/
public static String concat(Collection<?> terms,String between,String emptyMap) {
String ret = null;

for (Object o: terms) {
if (ret != null) {
ret += between+o.toString();
} else {
ret = o.toString();
}
}
if (ret == null) {
return emptyMap;
} else {
return ret;
}
}


}

--------------050409030108030606030603--
Re: Databinding with Enum + Combo [message #6209 is a reply to message #6195] Fri, 15 May 2009 19:44 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: fakemail.xyz.de

Christian schrieb:
> Boris Bokowski schrieb:
>> Hi Charlie,
>>
>> Not that I am aware of, but if you could contribute one that would be
>> great. Currently, the snippets/examples plug-in does not use Java 1.5
>> features but we would change that as soon as we add snippets dealing
>> with enums, generics, etc.
>>
>> http://wiki.eclipse.org/JFace_Data_Binding/How_to_Contribute
>>
>> Boris
>>
>> "Charlie Kelly" <Eclipse@CharlieKelly.com> wrote in message
>> news:guik5e$c2n$1@build.eclipse.org...
>>> Are there any examples/snippets available for databinding with a enum
>>> and a Combo?
>>>
>>> Thanks
>>>
>>> Charlie
>>
>>
>
> I once implemented a Simple one .. may be its enough for you..
>
>
>
>
>
>
Ah I think I misread you .. though you were looking for some kind of
viewer...
sry..
Re: Databinding with Enum + Combo [message #6224 is a reply to message #6152] Fri, 15 May 2009 20:58 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: eclipse-news.rizzoweb.com

This is a multi-part message in MIME format.
--------------010704090406010009050409
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Attached is a snippet that demonstrates binding an enum to a
ComboViewer. It's pretty simple once you see the keys.

Boris, I've got a patch for the snippets project with this. What
specifics (Project, Component, subject prefix, etc) should I use to
enter the Bugzilla?

Eric


Boris Bokowski wrote:
> Hi Charlie,
>
> Not that I am aware of, but if you could contribute one that would be great.
> Currently, the snippets/examples plug-in does not use Java 1.5 features but
> we would change that as soon as we add snippets dealing with enums,
> generics, etc.
>
> http://wiki.eclipse.org/JFace_Data_Binding/How_to_Contribute
>
> Boris
>
> "Charlie Kelly" <Eclipse@CharlieKelly.com> wrote in message
> news:guik5e$c2n$1@build.eclipse.org...
>> Are there any examples/snippets available for databinding with a enum and
>> a Combo?
>>
>> Thanks
>>
>> Charlie
>
>


--------------010704090406010009050409
Content-Type: text/plain;
name="Snippet034ComboViewerAndEnum.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="Snippet034ComboViewerAndEnum.java"

/*********************************************************** ********************
* Copyright (c) 2009 Eric Rizzo and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eric Rizzo - initial API and implementation
************************************************************ ******************/

package org.eclipse.jface.examples.databinding.snippets;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.value.IObservableVal ue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ViewersObservables;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Snippet034ComboViewerAndEnum {

public static void main(String[] args) {
Display display = new Display();
final Person model = new Person("Pat", Gender.Unknown);

Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
public void run() {
final Shell shell = new View(model).createShell();
// The SWT event loop
Display display = Display.getCurrent();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
});
// Print the results
System.out.println("person.getName() = " + model.getName());
System.out.println("person.getGender() = " + model.getGender());
}

static enum Gender {
Male, Female, Unknown;
}

// The data model class. This is normally a persistent class of some sort.
//
// In this example, we only push changes from the GUI to the model, so we
// don't worry about implementing JavaBeans bound properties. If we need
// our GUI to automatically reflect changes in the Person object, the
// Person object would need to implement the JavaBeans property change
// listener methods.
static class Person {
// A property...
String name;
Gender gender;

public Person(String name, Gender gender) {
this.name = name;
this.gender = gender;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Gender getGender() {
return gender;
}

public void setGender(Gender newGender) {
this.gender = newGender;
}
}

// The GUI view
static class View {
private Person viewModel;
private Text name;
private ComboViewer gender;

public View(Person viewModel) {
this.viewModel = viewModel;
}

public Shell createShell() {
// Build a UI
Display display = Display.getDefault();
Shell shell = new Shell(display);

RowLayout layout = new RowLayout(SWT.VERTICAL);
layout.fill = true;
layout.marginWidth = layout.marginHeight = 5;
shell.setLayout(layout);

name = new Text(shell, SWT.BORDER);
gender = new ComboViewer(shell, SWT.READ_ONLY);

// Here's the first key to binding a combo to an Enum:
// First give it an ArrayContentProvider,
// then set the input to the list of values from the Enum.
gender.setContentProvider(ArrayContentProvider.getInstance() );
gender.setInput(Gender.values());

// Bind the fields
DataBindingContext bindingContext = new DataBindingContext();

IObservableValue widgetObservable = SWTObservables.observeText(
name, SWT.Modify);
bindingContext.bindValue(widgetObservable, PojoObservables
.observeValue(viewModel, "name"));

// The second key to binding a combo to an Enum is to use a
// selection observable from the ComboViewer:
widgetObservable = ViewersObservables
.observeSingleSelection(gender);
bindingContext.bindValue(widgetObservable, PojoObservables
.observeValue(viewModel, "gender"));

// Open and return the Shell
shell.pack();
shell.open();
return shell;
}
}

}

--------------010704090406010009050409--
Re: Databinding with Enum + Combo [message #6254 is a reply to message #6224] Sat, 16 May 2009 18:26 Go to previous messageGo to next message
Charlie Kelly is currently offline Charlie KellyFriend
Messages: 276
Registered: July 2009
Senior Member
Eric Rizzo wrote:
> Attached is a snippet that demonstrates binding an enum to a
> ComboViewer. It's pretty simple once you see the keys.
>
> Boris, I've got a patch for the snippets project with this. What
> specifics (Project, Component, subject prefix, etc) should I use to
> enter the Bugzilla?
>
> Eric
>
Hi Eric,

Thanks for the snippet. The "keys" are particularly helpful.

Boris, given that this snippet is available, would you like me to create
an additional snippet?

Charlie


> Boris Bokowski wrote:
>> Hi Charlie,
>>
>> Not that I am aware of, but if you could contribute one that would be
>> great. Currently, the snippets/examples plug-in does not use Java 1.5
>> features but we would change that as soon as we add snippets dealing
>> with enums, generics, etc.
>>
>> http://wiki.eclipse.org/JFace_Data_Binding/How_to_Contribute
>>
>> Boris
>>
>> "Charlie Kelly" <Eclipse@CharlieKelly.com> wrote in message
>> news:guik5e$c2n$1@build.eclipse.org...
>>> Are there any examples/snippets available for databinding with a enum
>>> and a Combo?
>>>
>>> Thanks
>>>
>>> Charlie
>>
>>
>
Re: Databinding with Enum + Combo [message #7568 is a reply to message #6224] Mon, 18 May 2009 02:37 Go to previous messageGo to next message
Boris Bokowski is currently offline Boris BokowskiFriend
Messages: 272
Registered: July 2009
Senior Member
Hi Eric,

> Boris, I've got a patch for the snippets project with this. What
> specifics (Project, Component, subject prefix, etc) should I use to
> enter the Bugzilla?

Eclipse > Platform > UI, you can use a prefix [DataBinding] in the
description.

Thanks!
Boris
Re: Databinding with Enum + Combo [message #7587 is a reply to message #6254] Mon, 18 May 2009 02:39 Go to previous messageGo to next message
Boris Bokowski is currently offline Boris BokowskiFriend
Messages: 272
Registered: July 2009
Senior Member
Hi Charlie,

> Boris, given that this snippet is available, would you like me to create
> an additional snippet?

You could comment on the bugzilla Eric is going to enter (hoping that he
posts the bug id here), or improve on his snippet if you think it can be
improved.

Thanks!
Boris
Re: Databinding with Enum + Combo [message #7688 is a reply to message #7568] Mon, 18 May 2009 17:27 Go to previous message
Eclipse UserFriend
Originally posted by: eclipse-news.rizzoweb.com

Done: https://bugs.eclipse.org/bugs/show_bug.cgi?id=276755


Boris Bokowski wrote:
> Hi Eric,
>
>> Boris, I've got a patch for the snippets project with this. What
>> specifics (Project, Component, subject prefix, etc) should I use to
>> enter the Bugzilla?
>
> Eclipse > Platform > UI, you can use a prefix [DataBinding] in the
> description.
>
> Thanks!
> Boris
>
>
Previous Topic:Image control in TreeViewer
Next Topic:Implementing Auto complete
Goto Forum:
  


Current Time: Fri Mar 29 12:04:45 GMT 2024

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

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

Back to the top