Results 1 to 3 of 3
  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 using instanceof to get Object type and parent type?

    Is it possible to somehow && instanceof to check if the selected object is of type Target and is a child of type Asset. The issue is I have 2 editors for the same Object based on what type their parent is. Here is my listener
    Java Code:
    treeViewer.addOpenListener(new IOpenListener() {
    			public void open(final OpenEvent event) {
    				Object firstElement = ((IStructuredSelection)event.getSelection()).getFirstElement();
    				if (firstElement instanceof Target){
    					try {
    						PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView("NOVLayout.edittargetview");
    					} catch (PartInitException e) {
    						e.printStackTrace();
    					}
    					System.out.println("Open target editor");
    				}
    )};
    I want it to be something along the lines of firstElement instanceof Target && firstElement.getParent() instanceof Asset
    I'm not sure if I can do this though. Does the selected object have a way to know what type of node its parent is?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    First try compiling it. Does it compile? Copy and post error message here. You will have to cast the Object to the type you want.
    The compound if (a && b) should work. The shortcuted operand will skip the second test if the first one fails.
    Otherwise break the statement up into multiple parts.

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

    Default

    I guess I don't need to worry about traveling the tree to get its parent. Just had to create 2 methods within my Target class that would return the server parent or the asset parent. Some casting and it works.
    Java Code:
    if (firstElement instanceof Target && (( (Target) firstElement).getAssetParent() instanceof Asset))

    Eh that's what I get for over complicating things.

Similar Threads

  1. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 10:43 AM
  2. List views, a type of object
    By Leprechaun in forum New To Java
    Replies: 2
    Last Post: 02-06-2008, 03:07 AM
  3. Need help with creating array of type object
    By riz618 in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 06:14 AM
  4. Getting object type
    By gapper in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 08:49 AM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 PM

Posting Permissions

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