Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-18-2009, 11:32 AM
Member
 
Join Date: Mar 2009
Posts: 4
Rep Power: 0
negroscuro is on a distinguished road
Default CheckedTreeDialog adding Handler/Listener
Hello,
I have to stablish the behavior of the view regarding the checked items in order to have coherency between the different tree nodes checked.
I mean if you check a leaf node I want to also check automatically all higher branch nodes or If you uncheck the las checked leaf node then unchek all the higher nodes without child nodes checked...

So I need to catch an event when user checks or unchecks any tree element in the view.

Could anyone tell me how should I add a listener to do this? or how other way I should try to achieve it?

Thank you in advance.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 03-20-2009, 02:23 PM
Member
 
Join Date: Mar 2009
Posts: 4
Rep Power: 0
negroscuro is on a distinguished road
Default
nobody can help?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-24-2009, 11:46 AM
Member
 
Join Date: Mar 2009
Posts: 42
Rep Power: 0
trax is on a distinguished road
Default
/**
* This class demonstrates the CheckboxTreeViewer
*/
public class CheckFileTree extends FileTree {
/**
* Configures the shell
*
* @param shell
* the shell
*/
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setText("Check File Tree");
}

/**
* Creates the main window's contents
*
* @param parent
* the main window
* @return Control
*/
protected Control createContents(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(1, false));

// Add a checkbox to toggle whether the labels preserve case
Button preserveCase = new Button(composite, SWT.CHECK);
preserveCase.setText("&Preserve case");

// Create the tree viewer to display the file tree
final CheckboxTreeViewer tv = new CheckboxTreeViewer(composite);
tv.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
tv.setContentProvider(new FileTreeContentProvider());
tv.setLabelProvider(new FileTreeLabelProvider());
tv.setInput("root"); // pass a non-null that will be ignored

// When user checks the checkbox, toggle the preserve case attribute
// of the label provider
preserveCase.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
boolean preserveCase = ((Button) event.widget).getSelection();
FileTreeLabelProvider ftlp = (FileTreeLabelProvider) tv
.getLabelProvider();
ftlp.setPreserveCase(preserveCase);
}
});

// When user checks a checkbox in the tree, check all its children
tv.addCheckStateListener(new ICheckStateListener() {
public void checkStateChanged(CheckStateChangedEvent event) {
// If the item is checked . . .
if (event.getChecked()) {
// . . . check all its children

tv.setSubtreeChecked(event.getElement(), true);
}
}
});

return composite;
}

/**
* The application entry point
*
* @param args
* the command line arguments
*/
public static void main(String[] args) {
new CheckFileTree().run();
}
}

/**
* This class demonstrates TreeViewer. It shows the drives, directories, and
* files on the system.
*/

class FileTree extends ApplicationWindow {
/**
* FileTree constructor
*/
public FileTree() {
super(null);
}

/**
* Runs the application
*/
public void run() {
// Don't return from open() until window closes
setBlockOnOpen(true);

// Open the main window
open();

// Dispose the display
Display.getCurrent().dispose();
}

/**
* Configures the shell
*
* @param shell
* the shell
*/
protected void configureShell(Shell shell) {
super.configureShell(shell);

// Set the title bar text and the size
shell.setText("File Tree");
shell.setSize(400, 400);
}

/**
* Creates the main window's contents
*
* @param parent
* the main window
* @return Control
*/
protected Control createContents(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(1, false));

// Add a checkbox to toggle whether the labels preserve case
Button preserveCase = new Button(composite, SWT.CHECK);
preserveCase.setText("&Preserve case");

// Create the tree viewer to display the file tree
final TreeViewer tv = new TreeViewer(composite);
tv.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
tv.setContentProvider(new FileTreeContentProvider());
tv.setLabelProvider(new FileTreeLabelProvider());
tv.setInput("root"); // pass a non-null that will be ignored

// When user checks the checkbox, toggle the preserve case attribute
// of the label provider
preserveCase.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
boolean preserveCase = ((Button) event.widget).getSelection();
FileTreeLabelProvider ftlp = (FileTreeLabelProvider) tv
.getLabelProvider();
ftlp.setPreserveCase(preserveCase);
}
});
return composite;
}

}

/**
* This class provides the content for the tree in FileTree
*/

class FileTreeContentProvider implements ITreeContentProvider {
/**
* Gets the children of the specified object
*
* @param arg0
* the parent object
* @return Object[]
*/
public Object[] getChildren(Object arg0) {
// Return the files and subdirectories in this directory
return ((File) arg0).listFiles();
}

/**
* Gets the parent of the specified object
*
* @param arg0
* the object
* @return Object
*/
public Object getParent(Object arg0) {
// Return this file's parent file
return ((File) arg0).getParentFile();
}

/**
* Returns whether the passed object has children
*
* @param arg0
* the parent object
* @return boolean
*/
public boolean hasChildren(Object arg0) {
// Get the children
Object[] obj = getChildren(arg0);

// Return whether the parent has children
return obj == null ? false : obj.length > 0;
}

/**
* Gets the root element(s) of the tree
*
* @param arg0
* the input data
* @return Object[]
*/
public Object[] getElements(Object arg0) {
// These are the root elements of the tree
// We don't care what arg0 is, because we just want all
// the root nodes in the file system
return File.listRoots();
}

/**
* Disposes any created resources
*/
public void dispose() {
// Nothing to dispose
}

/**
* Called when the input changes
*
* @param arg0
* the viewer
* @param arg1
* the old input
* @param arg2
* the new input
*/
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
// Nothing to change
}
}

/**
* This class provides the labels for the file tree
*/

class FileTreeLabelProvider implements ILabelProvider {
// The listeners
private List listeners;

// Images for tree nodes
private Image file;

private Image dir;

// Label provider state: preserve case of file names/directories
boolean preserveCase;

/**
* Constructs a FileTreeLabelProvider
*/
public FileTreeLabelProvider() {
// Create the list to hold the listeners
listeners = new ArrayList();

// Create the images
try {
file = new Image(null, new FileInputStream("images/file.gif"));
dir = new Image(null, new FileInputStream("images/directory.gif"));
} catch (FileNotFoundException e) {
// Swallow it; we'll do without images
}
}

/**
* Sets the preserve case attribute
*
* @param preserveCase
* the preserve case attribute
*/
public void setPreserveCase(boolean preserveCase) {
this.preserveCase = preserveCase;

// Since this attribute affects how the labels are computed,
// notify all the listeners of the change.
LabelProviderChangedEvent event = new LabelProviderChangedEvent(this);
for (int i = 0, n = listeners.size(); i < n; i++) {
ILabelProviderListener ilpl = (ILabelProviderListener) listeners
.get(i);
ilpl.labelProviderChanged(event);
}
}

/**
* Gets the image to display for a node in the tree
*
* @param arg0
* the node
* @return Image
*/
public Image getImage(Object arg0) {
// If the node represents a directory, return the directory image.
// Otherwise, return the file image.
return ((File) arg0).isDirectory() ? dir : file;
}

/**
* Gets the text to display for a node in the tree
*
* @param arg0
* the node
* @return String
*/
public String getText(Object arg0) {
// Get the name of the file
String text = ((File) arg0).getName();

// If name is blank, get the path
if (text.length() == 0) {
text = ((File) arg0).getPath();
}

// Check the case settings before returning the text
return preserveCase ? text : text.toUpperCase();
}

/**
* Adds a listener to this label provider
*
* @param arg0
* the listener
*/
public void addListener(ILabelProviderListener arg0) {
listeners.add(arg0);
}

/**
* Called when this LabelProvider is being disposed
*/
public void dispose() {
// Dispose the images
if (dir != null)
dir.dispose();
if (file != null)
file.dispose();
}

/**
* Returns whether changes to the specified property on the specified
* element would affect the label for the element
*
* @param arg0
* the element
* @param arg1
* the property
* @return boolean
*/
public boolean isLabelProperty(Object arg0, String arg1) {
return false;
}

/**
* Removes the listener
*
* @param arg0
* the listener to remove
*/
public void removeListener(ILabelProviderListener arg0) {
listeners.remove(arg0);
}
}



I think this code will solve most of ur problems.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-25-2009, 03:18 PM
Member
 
Join Date: Mar 2009
Posts: 4
Rep Power: 0
negroscuro is on a distinguished road
Default
Perfect I will read all this stuff, thank you very much!!!
Best Regards
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-26-2009, 05:19 PM
Member
 
Join Date: Mar 2009
Posts: 4
Rep Power: 0
negroscuro is on a distinguished road
Default
Mmmmh I have been reading your code but I have noticed that you are using:
private CheckboxTreeViewer checkedtreeDialog = null;

not the same class about I asked:
private CheckedTreeSelectionDialog checkedtreeDialog = null;


So I don't have the method:
tv.addCheckStateListener(new ICheckStateListener() );
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 03-30-2009, 07:30 AM
Member
 
Join Date: Mar 2009
Posts: 42
Rep Power: 0
trax is on a distinguished road
Default
CheckedTreeSelectionDialog does not contain api for selecting the parent objects , u can use CheckboxTreeViewer .

Regards,
trax
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Redirect Page in SOAP Message Handler bluesheeva Advanced Java 0 12-28-2008 01:43 PM
event handler not working properly H3rtaherta Java 2D 3 11-24-2008 03:39 AM
JList and JButton event handler not working H3rtaherta AWT / Swing 3 11-22-2008 01:00 AM
Help with Handler baltimore AWT / Swing 1 08-04-2007 10:42 PM
Adding listener to non-Java object? cruxblack Advanced Java 5 07-30-2007 03:19 AM


All times are GMT +2. The time now is 11:06 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org