Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » ICellModifier using
ICellModifier using [message #435789] Tue, 04 May 2004 08:48 Go to next message
Eclipse UserFriend
Originally posted by: john.rmts.donpac.ru

Hi,

My task is to write simple editor for host list from LDAP Directory. I
can't understand how to use ICellModifier for TableViewer.

I wrote this (sorry for large code, but it's minimal: LdapHost class and
it's factories are excluded):

public class NetworkConsoleWindow extends ApplicationWindow {
private DirContext ctx;
public NetworkConsoleWindow() throws IOException,
FileNotFoundException, NamingException {
super(null);
Properties connProperties = new Properties();
connProperties.load(new FileInputStream("connection.properties"));
connProperties.getProperty("ldap.url");
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.OBJECT_FACTORIES,
"networkconsole.core.LdapHostObjectFactory");
env.put(Context.STATE_FACTORIES,
"networkconsole.core.LdapHostStateFactory");
env.put(Context.PROVIDER_URL, connProperties.getProperty("ldap.url")+"/"+
connProperties.getProperty("ldap.hosts.context"));
env.put(Context.SECURITY_PRINCIPAL,
connProperties.getProperty("ldap.user"));
env.put(Context.SECURITY_CREDENTIALS,
connProperties.getProperty("ldap.password"));
ctx = new InitialDirContext(env);
}
private void dispose() throws NamingException {
ctx.close();
}
protected Control createContents(Composite parent) {
getShell().setText("Network Console");
Table table = new Table (parent,
SWT.BORDER|SWT.SINGLE|SWT.FULL_SELECTION);
table.setHeaderVisible (true);
String [] columnNames = new String[] {
"Host Name",
"Allow NAT"
};
for (int i=0; i<columnNames.length; i++) {
TableColumn column = new TableColumn (table, SWT.LEFT);
column.setText(columnNames[i]);
}
TableViewer viewer = new TableViewer (table);
viewer.setContentProvider(new HostsContentProvider());
viewer.setLabelProvider(new HostsLabelProvider());
viewer.setUseHashlookup(true);
viewer.setColumnProperties(columnNames);
CellEditor[] editors = new CellEditor[columnNames.length];
editors[1] = new CheckboxCellEditor(table);
viewer.setCellEditors(editors);
viewer.setCellModifier(new HostsCellModifier(ctx));
viewer.setInput(ctx);
return table;
}
public static void main(String[] args) throws IOException,
FileNotFoundException, NamingException {
NetworkConsoleWindow w = new NetworkConsoleWindow();
w.setBlockOnOpen(true);
w.open();
Display.getCurrent().dispose();
}
private class HostsContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement) {
try {
if (inputElement instanceof InitialDirContext) {
NamingEnumeration enum =
((InitialDirContext)inputElement).listBindings("");
ArrayList v = new ArrayList();
while (enum.hasMore()) {
Object item = ((Binding)enum.nextElement()).getObject();
if (item instanceof LdapHost)
v.add(item);
}
return v.toArray();
}
else
return null;
}
catch (NamingException exception){
return null;
}
}
public void dispose() {}
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {}
}
private class HostsLabelProvider extends LabelProvider implements
ITableLabelProvider {
public Image getColumnImage(Object arg0, int arg1) {
return null;
}
public String getColumnText(Object element, int columnIndex) {
if (element instanceof LdapHost) {
LdapHost host = (LdapHost)element;
switch (columnIndex) {
case 0: return host.name;
case 1: return (host.allowNat)?("Yes"):("No");
}
}
return "";
}
}
public class HostsCellModifier implements ICellModifier {
private DirContext ctx;
public HostsCellModifier(DirContext ctx) {
super();
this.ctx = ctx;
}
public boolean canModify(Object element, String property) {
if (property == "Allow NAT")
return true;
else
return false;
}
public Object getValue(Object element, String property) {
if (element instanceof LdapHost && property == "Allow NAT") {
LdapHost host = (LdapHost)element;
return new Boolean(host.allowNat);
}
else
return null;
}
public void modify(Object element, String property, Object value) {
if (element instanceof LdapHost && property == "Allow NAT") {
LdapHost host = (LdapHost)element;
try {
LdapHost host_new = (LdapHost)ctx.lookup("cn="+host.name);
host.allowNat = ((Boolean)value).booleanValue();
ctx.rebind("cn="+host.name, host);
}
catch (NamingException exception) {
}
}
}
}
}

This code display host list in table, but I can't edit "Allow NAT"
column. What is wrong?
Re: ICellModifier using [message #440232 is a reply to message #435789] Mon, 26 July 2004 23:02 Go to previous message
Paul E. Keyser is currently offline Paul E. KeyserFriend
Messages: 878
Registered: July 2009
Senior Member
I'm not sure, but I think you should switch all cases of this code:
property == "Allow NAT"
to read like this:
property.equals(ALLOW_NAT);

and define one static String somewhere convenient:
static final String ALLOW_NAT = "Allow NAT";

and use ALLOW_NAT also in your array of column-names.

HTH,
Paul

Eugene Prokopiev wrote:

> Hi,
>
> My task is to write simple editor for host list from LDAP Directory. I
> can't understand how to use ICellModifier for TableViewer.
>
> I wrote this (sorry for large code, but it's minimal: LdapHost class and
> it's factories are excluded):
>
> public class NetworkConsoleWindow extends ApplicationWindow {
> private DirContext ctx;
> public NetworkConsoleWindow() throws IOException,
> FileNotFoundException, NamingException {
> super(null);
> Properties connProperties = new Properties();
> connProperties.load(new
> FileInputStream("connection.properties"));
> connProperties.getProperty("ldap.url");
> Hashtable env = new Hashtable();
> env.put(Context.INITIAL_CONTEXT_FACTORY,
> "com.sun.jndi.ldap.LdapCtxFactory");
> env.put(Context.OBJECT_FACTORIES,
> "networkconsole.core.LdapHostObjectFactory");
> env.put(Context.STATE_FACTORIES,
> "networkconsole.core.LdapHostStateFactory");
> env.put(Context.PROVIDER_URL,
> connProperties.getProperty("ldap.url")+"/"+
> connProperties.getProperty("ldap.hosts.context"));
> env.put(Context.SECURITY_PRINCIPAL,
> connProperties.getProperty("ldap.user"));
> env.put(Context.SECURITY_CREDENTIALS,
> connProperties.getProperty("ldap.password"));
> ctx = new InitialDirContext(env);
> }
> private void dispose() throws NamingException {
> ctx.close();
> }
> protected Control createContents(Composite parent) {
> getShell().setText("Network Console");
> Table table = new Table (parent,
> SWT.BORDER|SWT.SINGLE|SWT.FULL_SELECTION);
> table.setHeaderVisible (true);
> String [] columnNames = new String[] {
> "Host Name",
> "Allow NAT"
> };
> for (int i=0; i<columnNames.length; i++) {
> TableColumn column = new TableColumn (table, SWT.LEFT);
> column.setText(columnNames[i]);
> }
> TableViewer viewer = new TableViewer (table);
> viewer.setContentProvider(new HostsContentProvider());
> viewer.setLabelProvider(new HostsLabelProvider());
> viewer.setUseHashlookup(true);
> viewer.setColumnProperties(columnNames);
> CellEditor[] editors = new CellEditor[columnNames.length];
> editors[1] = new CheckboxCellEditor(table);
> viewer.setCellEditors(editors);
> viewer.setCellModifier(new HostsCellModifier(ctx));
> viewer.setInput(ctx);
> return table;
> }
> public static void main(String[] args) throws IOException,
> FileNotFoundException, NamingException {
> NetworkConsoleWindow w = new NetworkConsoleWindow();
> w.setBlockOnOpen(true);
> w.open();
> Display.getCurrent().dispose();
> }
> private class HostsContentProvider implements
> IStructuredContentProvider {
> public Object[] getElements(Object inputElement) {
> try {
> if (inputElement instanceof InitialDirContext) {
> NamingEnumeration enum =
> ((InitialDirContext)inputElement).listBindings("");
> ArrayList v = new ArrayList();
> while (enum.hasMore()) {
> Object item =
> ((Binding)enum.nextElement()).getObject();
> if (item instanceof LdapHost)
> v.add(item);
> }
> return v.toArray();
> }
> else
> return null;
> }
> catch (NamingException exception){
> return null;
> }
> }
> public void dispose() {}
> public void inputChanged(Viewer arg0, Object arg1, Object arg2) {}
> }
> private class HostsLabelProvider extends LabelProvider implements
> ITableLabelProvider {
> public Image getColumnImage(Object arg0, int arg1) {
> return null;
> }
> public String getColumnText(Object element, int columnIndex)
> {
> if (element instanceof LdapHost) {
> LdapHost host = (LdapHost)element;
> switch (columnIndex) {
> case 0: return host.name;
> case 1: return (host.allowNat)?("Yes"):("No");
> }
> }
> return "";
> }
> }
> public class HostsCellModifier implements ICellModifier {
> private DirContext ctx;
> public HostsCellModifier(DirContext ctx) {
> super();
> this.ctx = ctx;
> }
> public boolean canModify(Object element, String property) {
> if (property == "Allow NAT")
> return true;
> else
> return false;
> }
> public Object getValue(Object element, String property) {
> if (element instanceof LdapHost && property == "Allow NAT") {
> LdapHost host = (LdapHost)element;
> return new Boolean(host.allowNat);
> }
> else
> return null;
> }
> public void modify(Object element, String property, Object
> value) {
> if (element instanceof LdapHost && property == "Allow NAT") {
> LdapHost host = (LdapHost)element;
> try {
> LdapHost host_new =
> (LdapHost)ctx.lookup("cn="+host.name);
> host.allowNat = ((Boolean)value).booleanValue();
> ctx.rebind("cn="+host.name, host);
> }
> catch (NamingException exception) {
> }
> }
> }
> }
> }
>
> This code display host list in table, but I can't edit "Allow NAT"
> column. What is wrong?
>
Previous Topic:How to prevent a TableTree from collapsing
Next Topic:Setup Linux Environment For Build
Goto Forum:
  


Current Time: Fri Nov 28 19:56:33 GMT 2014

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

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