Monday, November 10, 2008

CheckboxTreeViewer in SWT



import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

public class CheckboxTreeViewerTest extends Composite
{
   
public CheckboxTreeViewerTest(Composite parent)
    {
       
super(parent, SWT.NULL);
        createTreeViewer
();
   
}
   
   
private void createTreeViewer()
    {
       
GridLayout gridLayout = new GridLayout(1, true);
        setLayout
(gridLayout);
       
       
final CheckboxTreeViewer i_treeViewer = new CheckboxTreeViewer(this);
        i_treeViewer.getTree
().setLayoutData(new GridData(GridData.FILL_BOTH));
        i_treeViewer.setContentProvider
(new TreeContentProvider());
        i_treeViewer.setInput
(getRootNode());
        i_treeViewer.addCheckStateListener
(new ICheckStateListener()
        {
           
public void checkStateChanged(CheckStateChangedEvent event)
            {
               
// If the item is changed, check all its children
               
i_treeViewer.setGrayed(event.getElement(), false);
                i_treeViewer.setSubtreeChecked
(event.getElement(), event
                        .getChecked
());
               
((TreeNode) event.getElement())
                       
.setIsEnabled(event.getChecked());
               
if (((TreeNode) event.getElement()).getChildren().size() > 0)
                {
                   
for (TreeNode node : ((TreeNode) event.getElement())
                           
.getChildren())
                    {
                       
node.setIsEnabled(event.getChecked());
                   
}
                }
               
               
// If the item is changed, check its parent
               
TreeNode parentNode = ((TreeNode) event.getElement())
                       
.getParent();
                List<Boolean> isGrayedList =
new ArrayList<Boolean>();
               
for (TreeNode node : parentNode.getChildren())
                {
                   
isGrayedList.add(node.isEnabled());
               
}
               
               
if (isGrayedList.contains(true) &&
                        !isGrayedList.contains
(false))
                {
                   
i_treeViewer.setGrayed(parentNode, false);
                    i_treeViewer.setChecked
(parentNode, true);
               
}
               
else if (isGrayedList.contains(false) &&
                        !isGrayedList.contains
(true))
                {
                   
i_treeViewer.setGrayed(parentNode, false);
                    i_treeViewer.setChecked
(parentNode, false);
               
}
               
else
               
{
                   
i_treeViewer.setGrayed(parentNode, true);
                    i_treeViewer.setChecked
(parentNode, true);
               
}
            }
        })
;
   
}
   
   
private TreeNode getRootNode()
    {
       
TreeNode root = new TreeNode("root");
        TreeNode child =
new TreeNode("child");
        List<TreeNode> subChildList =
new ArrayList<TreeNode>();
       
for (int i = 0; i < 10; i++)
        {
           
subChildList.add(new TreeNode("subChild" + i));
       
}
       
child.addChildren(subChildList);
        root.addChild
(child);
       
return root;
   
}
}

class TreeNode implements Comparable<TreeNode>
{
   
private String i_name;
   
   
private List<TreeNode> i_children = new ArrayList<TreeNode>();
   
   
private TreeNode i_parent;
   
   
private boolean i_isEnabled;
   
   
public TreeNode(String n)
    {
       
i_name = n;
   
}
   
   
protected TreeNode getParent()
    {
       
return i_parent;
   
}
   
   
public TreeNode addChild(TreeNode child)
    {
       
i_children.add(child);
        child.i_parent =
this;
       
return this;
   
}
   
   
public TreeNode addChildren(List<TreeNode> children)
    {
       
if (children != null)
        {
           
for (TreeNode child : children)
            {
               
i_children.add(child);
                Collections.sort
(i_children);
                child.i_parent =
this;
           
}
        }
       
return this;
   
}
   
   
public List<TreeNode> getChildren()
    {
       
return i_children;
   
}
   
   
public boolean isEnabled()
    {
       
return i_isEnabled;
   
}
   
   
public void setIsEnabled(boolean enabled)
    {
       
i_isEnabled = enabled;
   
}
   
   
public String toString()
    {
       
return i_name;
   
}
   
   
public int compareTo(TreeNode o)
    {
       
return i_name.compareTo(o.toString());
   
}
   
   
/*
     * (non-Javadoc)
     *
     * @see java.lang.Object#hashCode()
     */
   
@Override
   
public int hashCode()
    {
       
final int prime = 31;
       
int result = 1;
        result = prime * result +
((i_name == null) ? 0 : i_name.hashCode());
       
return result;
   
}
   
   
/*
     * (non-Javadoc)
     *
     * @see java.lang.Object#equals(java.lang.Object)
     */
   
@Override
   
public boolean equals(Object obj)
    {
       
if (this == obj)
           
return true;
       
if (obj == null)
           
return false;
       
if (getClass() != obj.getClass())
           
return false;
        TreeNode other =
(TreeNode) obj;
       
if (i_name == null)
        {
           
if (other.i_name != null)
               
return false;
       
}
       
else if (!i_name.equals(other.i_name))
           
return false;
       
return true;
   
}
}

class TreeContentProvider implements ITreeContentProvider
{
   
/*
     * (non-Javadoc)
     *
     * @see
     * org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.
     * Object)
     */
   
public Object[] getChildren(Object parentElement)
    {
       
return ((TreeNode) parentElement).getChildren().toArray();
   
}
   
   
/*
     * (non-Javadoc)
     *
     * @see
     * org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object
     * )
     */
   
public Object getParent(Object element)
    {
       
return ((TreeNode) element).getParent();
   
}
   
   
/*
     * (non-Javadoc)
     *
     * @see
     * org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.
     * Object)
     */
   
public boolean hasChildren(Object element)
    {
       
return ((TreeNode) element).getChildren().size() > 0;
   
}
   
   
/*
     * (non-Javadoc)
     *
     * @see
     * org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java
     * .lang.Object)
     */
   
public Object[] getElements(Object inputElement)
    {
       
return ((TreeNode) inputElement).getChildren().toArray();
   
}
   
   
/*
     * (non-Javadoc)
     *
     * @see org.eclipse.jface.viewers.IContentProvider#dispose()
     */
   
public void dispose()
    {
       
    }
   
   
/*
     * (non-Javadoc)
     *
     * @see
     * org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface
     * .viewers.Viewer, java.lang.Object, java.lang.Object)
     */
   
public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
    {
       
    }
}

The exception in JBoss Portal Server

If you are using JBoss Portal Server 2.6.2/2.6.3, when you startup the server on the first time, you can see some errors like that:

11:48:49,482 ERROR JDBCExceptionReporter Table not found in statement…

Nothing's wrong, the table don't exist in the first startup, an error is thrown but the table is created just after. Those messages will disappear on the next startup.

Another issue, when I start JBoss Portal Server using run.bat, and WSRP is now complaining with the following stack trace:

14:05:46,564 ERROR [Scheduler$Listener] Invoke of the Schedulable MBean failed
javax.management.MBeanException
......................................
... 22 more
Caused by: java.lang.UnsupportedOperationException: setProperty must be overridd
en by all subclasses of SOAPMessage
......................................
... 29 more

To resolve the issue, we need to reconfigure WSRP to listen on the correct interface. Updating WSRP ports and URL bindings when Portal is not running on the default port or not bound to localhost.

You can pass the full host name using the -b option of the run command when you launch AS. If it is still not working, you can then force the port number by modifying jboss-portal.sar/portal-wsrp.sar/portal-wsrp.jse/WEB-INF/wsdl/wsrp_services.wsdl (if you're not using the source distribution and compiling Portal from scratch, you might need to expand jboss-portal.sar/portal-wsrp.sar/portal-wsrp.jse just like you would expand a JAR file) to change all the soap-address elements to include http://hostname:hostport/path-to-service

I unzip the jboss-portal-2.6.2.GA\server\default\deploy\jboss-portal.sar\portal-wsrp.sar\portal-wsrp.jse file, then I find the portal-wsrp\WEB-INF\wsdl\wsrp_services.wsdl, and then enable the 'wsdl:service name="WSRPService"' part. After the modification, the issue will be resolved.