Results 1 to 2 of 2
  1. #1
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default [SOLVED] Node traversal using TreeViewer and SelectionEvent

    I have 2 drop down menus that I want to auto fill(Server and Target) with information from the current node selected. Let's say I have
    Java Code:
    +Server
         +Westchase
               -Dev
               -Stage
               -Prod
         +Orange
               -Dev
               -Stage
               -Prod    //I double click this node
    If I double click on Prod it opens up my EditTargetView. Right now my code has you pick a server and target using drop down menus
    Java Code:
    		//Server Dropdown
    		new Label(banner, SWT.NONE).setText("Servers:");
    	    final Combo serverCombo = new Combo(banner, SWT.READ_ONLY);
    	    serverCombo.setBounds(50, 50, 400, 65);
    	    final Server[] servers = ServerGroup.getChildren();
    	    final String[] serverNames = new String[servers.length];
    	    for(int i = 0; i < servers.length; i++){
    	    	serverNames[i] = servers[i].getName(); 
    	    }
    	    serverCombo.setItems(serverNames);
    	    
    		//Target Dropdown
    	    new Label(banner, SWT.NONE).setText("Targets:");
    	    final Combo targetCombo = new Combo(banner, SWT.READ_ONLY);
    	    targetCombo.setBounds(50, 120, 300, 65);
    	    
    	    //creates the list for Target dropdown
    	    serverCombo.addSelectionListener(new SelectionAdapter(){
    	    	public void widgetSelected(SelectionEvent event){
    	    		if (serverCombo.getSelectionIndex()== -1){
    	    			oldDirectory.setText("");
    	    		}
    	    		else{
    	    			String testString = serverCombo.getItem(serverCombo.getSelectionIndex());
    	    			Server[] checkServer = ServerGroup.getChildren();
    	    				for(int i = 0; i<checkServer.length; i++){
    	    					if(testString.equalsIgnoreCase(checkServer[i].getName())){
    	    						theServer = checkServer[i];
    	    						break;
    	    					}
    	    				}	
    					final Target[] targets = theServer.getChildren();   
    					
    	    			//adds the selected servers children to the target combo
    	    			final String[] targetNames = new String[3];
    	    			for(int x = 0; x < targets.length; x++){
    						targetNames[x] = targets[x].getName();
    					}
    	    			targetCombo.setItems(targetNames);
    					targetCombo.addSelectionListener(new SelectionAdapter(){
    						public void widgetSelected(SelectionEvent event){
    							if(targetCombo.getSelectionIndex() == -1){
    								oldDirectory.setText("");
    							}
    							else{
    								Object firstElement = targetCombo.getItem(targetCombo.getSelectionIndex());
    								String testString = targetCombo.getItem(targetCombo.getSelectionIndex());
    								
    								for(int x = 0; x<targetNames.length; x++){
    									if(testString.equalsIgnoreCase(targetNames[x])){
    										oldDirectory.setText(targets[x].getDirectory());
    									}
    								}
    							}
    						}
    					});
    	    		}
    	    	}
    	    });
    but I need it to auto-fill in the menu using the node selected and its parent(Orange). I can manage to have it set the Target using
    Java Code:
    ISelection sel = event.getSelection();
    				   Object selObj = ((IStructuredSelection) sel).getFirstElement();
    in my double click listener but I've never used a hashmap or binary tree before so I have no idea how to navigate through the TreeViewer to get the parent. Where should I start?

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    I cheated but it works.
    Java Code:
    Object someObjectClass = ((IStructuredSelection) event.getSelection()).getFirstElement().getClass();
    				   
    				   if(someObjectClass.toString().equalsIgnoreCase("class groups.Target")){
    					   if(((Target) selObj).getServerParent() != null){
    						   server = ((Target) selObj).getServerParent();   
    						   System.out.println(server);
    					   }
    					   else if(((Target) selObj).getAssetParent() != null){
    						   asset = ((Target) selObj).getAssetParent();
    						   System.out.println(asset);
    					   }   
    				   }

Similar Threads

  1. Array traversal issues
    By sondratheloser in forum New To Java
    Replies: 3
    Last Post: 08-13-2012, 12:49 AM
  2. SWT treeviewer
    By sushistix in forum SWT / JFace
    Replies: 4
    Last Post: 10-28-2008, 09:25 PM
  3. How do I refresh a TreeViewer object?
    By xcallmejudasx in forum SWT / JFace
    Replies: 0
    Last Post: 10-27-2008, 03:51 PM
  4. TreeViewer Demonstration
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:54 PM
  5. Preorder Traversal problem...
    By tuckker in forum New To Java
    Replies: 3
    Last Post: 12-04-2007, 06:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •