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

    Default [SOLVED] using SelectionChangedEvent and getSelection()

    I'm trying to display the directory of a selection if it is of type Target. The issue I'm having is that each Server has 3 Targets all with the same name but different directories. getSelection() just returns a String representation of what I'm clicking on and I need an Object representation to be able to use getServerParent() so I can get that specific directory and not just a default.
    Here is my listener
    Java Code:
    treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
    			   public void selectionChanged(SelectionChangedEvent event) {
    				   String testString = event.getSelection().toString().replace("[", "").replace("]","");
    				   final IActionBars bars = getViewSite().getActionBars();
    				   bars.getStatusLineManager().setMessage(testString);
    			
    				   
    				   //Displays UNC on status bar  
    				   Server[] servers = ServerGroup.getChildren();
    				   String[] serverNames = new String[servers.length];
    				   for(int i = 0; i < servers.length; i++){
    					   serverNames[i] = servers[i].getName(); 
    				   }
    				   for(int x = 0; x <serverNames.length; x++){
    					  if(testString.equalsIgnoreCase(serverNames[x])){
    						  bars.getStatusLineManager().setMessage(servers[x].getUNC());
    					  }
    				   }		
    				   //Sets Servers/Targets/Directories into an Array[][]
    				   Object[][] someObjects = new Object[servers.length][4];
    				   for(int x = 0; x < servers.length; x++){
    					   Target[] targets = servers[x].getChildren();
    					   someObjects[x][x] = servers[x];
    					   for (int y = 0; y < targets.length; y++){
    						   someObjects[x][y] = targets[y];
    					   }
    				   }
    				   //Displays Directories on status bar
    				   for(int x = 0; x < servers.length; x++){
    					   Target[] targets = servers[x].getChildren();
    					   for (int y = 0; y < targets.length; y++){
    						   if(event.getSelection().equals(someObjects[x][y])){
    							   bars.getStatusLineManager().setMessage(someObjects[x][y].toString());
    							   System.out.println("True");
    						   }
    						   System.out.println(event.getSelection());
    						   //System.out.println(someObjects[x][y]);
    						   System.out.println("("+x+","+y+") False");
    					   }
    				   }
    				   
    				   //TODO create popup menu
     
    				   //Outputs to status line
    				   if(event.getSelection().isEmpty()){
    					   bars.getStatusLineManager().setMessage("Empty");
    				   }
    			   }
    			});
    By default my tree looks like this
    Java Code:
    +Servers
    	+Westchase
    		Dev
    		Stage
    		Prod
    	+SHP
    		Dev
    		Stage
    		Prod
    	+Orange
    		Dev
    		Stage
    		Prod
    How would I go about getting the Object I selected and not the String? I can post my Server/Target class codes if y'all are confused by this code if you want also.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Events usually have a pointer to the object that generated the event: getSource()

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

    Default

    getSource() returns org.eclipse.jface.viewers.TreeViewer@2006a0. That makes sense since I'm clicking nodes in a view generated by the TreeViewer. It's pointing to the actual node not the object the node represents I think.

    I think I might need to travel the tree somehow that way it will know what server Target belongs to so that I can maybe go through the arraylist of that server to get the target directory.

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

    Default

    I came across this little goody.
    Java Code:
    ISelection sel = event.getSelection();
    Object selObj = ((IStructuredSelection) sel).getFirstElement();
    This allows me to just have a few if statements checking if selObj is an instanceof "whatever object" I want.

    Thanks for all the help Norm. Your an eternal fountain of insight and I look forward reading more of your posts.

Posting Permissions

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