Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

JTree and right click popup menu ?

Posted on 2002-07-18
10
742 Views
Last Modified: 2010-08-05
Well I have a JTree which is made using DefaultMutableTreeNode with userObjects.

Now also I have as a standard certain actions which I can perform on a node when I right click on the node. Thus for that I have a popup menu.

The popup menu has following actions which I can perform on the particular node.

1. Archive
2. Unarchive
3. Delete
4. Undelete
5. Rename
6. Add
7. Move (using self makde DND by implementing MouseMotionListener)

Now to enable or to disable these options the rules are all lying in DefaultMutableTreeNode instance.

How should I have a set pattern to make this enable/disabling feature execute.

The situation is like if the node has its beans deleteFlag() set to true the all options have to be disabled except for undelete.

If the archiveFlag() is true then only unarchive and delete options have to enabled rest all disabled.

I mean I want to have a object oriented solution to this problem instead of getting lost in -- if and else if statements.

Kinldy demostrate me with a scalable solution so that I can really work well for the same in not only robust but ease of use way.

Thanx in advance.

dumdum420









0
Comment
Question by:dumdum420
  • 6
  • 4
10 Comments
 
LVL 1

Expert Comment

by:wgilster
ID: 7162957
First, since all of the rules are contained in your extended version of the DefaultMutableTreeNode I would have a method in this class called something like:
void applyBeanState(JMenuItem/Component item)

BeanState is probably not a good name, but your smart you can think of a better name.  Anyway you would then call this method iterating through each MenuItem or Component of the JPopupMenu.  The applyBeanState() method would contain all of the messy if/else code you spoke of which would change the properties of the component you passed in it.  

Of course you could go a step further and create some type of delegate pattern for each different type of action, archive, unarchive.  That way you could get around having to write the if/else mess.  But not knowing your project, that sounds a bit like overkill.
0
 

Author Comment

by:dumdum420
ID: 7163042
I understand you dont no my project but think :

all these actions will return one of the two states
true/false.

Now I will ellaborate one of the action UNARCHIVE to you ...

these are in a flow

  if node is archived (DISABLE THIS ACTION);
  else is nodes parent term is archived (DISABLE THIS ACTION);
  else (ENABLE THIS ACTION);

Similarly other actions have there workflows set to ...
but for if the node has its deleteFlag() set to true we should not be even in the business to check for the UNARCHIVE workflow ...

Thus if i check each workflow individually against each action then lot of things are repeated and if i dont i have bad code of else if etc etc. I mean I want a a set example in a pattern how to do this stuff in OOP way.

I mean if it is delegate kindly send me a pseudo code of how to do this stuff else think and I really need a very practical and robust solution.

Thanx in advance.

0
 
LVL 1

Accepted Solution

by:
wgilster earned 300 total points
ID: 7163196
It sounds like my simple solution isn't scalable in the direction you were looking for, although I wouldn't overlook the future need to do more than just boolean property setting.

Here is a possible solution.  You develop an interface called something like
interface PropertySettingDelegate {
  void applyProperties(DefaultMutableTreeNode node, Component mycomp);
}

Then you create subclasses of this class called:
class ArchiveDelegate implements PropertySettingDelegate
class DeleteDelegate implements PropertySettingDelegate
...
for each of your menu actions.  REMEMBER to put your property setting logic in each of these delegates.

Next when you are creating each of these menu items, you need to attach the menu item with it's corresponding delegate.  I would start off using a hashtable:

Hashtable menuItemComponentToDelegateLinks = new Hashtable();
menuItemComponentToActionLinks.put(ArchiveMenuItem, new ArchiveActionDelegate());
menuItemComponentToActionLinks.put(DeleteMenuItem, new DeleteActionDelegate());

Then when the user clicks on the node you will iterate through the menuItems in your popup and lookup it's corresponding delegate like this:
for (int t = 0; t < numberOfMenuItemsInPopup; t++) {
  Component currentMenuItem = getComponentInPopupMenuByIndex(popupMenuToShow, t);
  PropertySettingDelegate someDelegate = menuItemComponentToDelegateLinks.get(currentMenuItem);
  someDelegate.applyProperties(ClickedNode, currentMenuItem);
}

This gives you some extreme scalibility, because you get to reuse the delegates for different popup menus that you might want to create in the future and you have a separation of duties for each different instance of the delegate. The problem is that it's OO which means it takes some thinking to get used to it.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:dumdum420
ID: 7163319
Well this sounds very intersting I am really going to try this approach.

This approach essentially means that I have to apply business rule for each and every item in my popup menu.

Just on a wild card I am asking you is there a way that if the node has its deleteFlag() set to true  i dont even have to check for other deletgates and just enable the undelteFlag delegate.

This saves lot of iteration and that is what is being in the digital world is all about. I think what I am asking is definitely a wrong approach but still ...???

Get back to you as soon as I can!!!

Thanks again.

dumdum420
0
 
LVL 1

Expert Comment

by:wgilster
ID: 7171742
So how did it all turn out?

To answer your last question, yes.  This will start to get ugly if you start putting optimizations such as what you stated.  But if you think about it, you wouldn't even be saving an iteration as you thought, because you have to iterate through all of those menus to turn them off anyway.  All you would be saving is maybe an if statement which is very cheap.

Wes G.
0
 

Author Comment

by:dumdum420
ID: 7177700
It did work for me. Thanx a lot the appraoch was real clean and made the code real flexible. But one thing since my move operation is GUI based like drag and drop how would this fit into this approach.

Anyways I am giving points. I will very sincerely wait for your reply for the move operations. If you have anyother query that I can clarify please do post.

Thax in advance.

dumdum420
0
 

Author Comment

by:dumdum420
ID: 7177702
Thanx I have posted another query do let me know about the move operation.

Thax again.
0
 

Author Comment

by:dumdum420
ID: 7178200
well i got stuck when i tried to put lineSeperators in my JMenuItem since I am getting things from indexing of the JPopupMenu and seperator also is added to the index.

Help I am really stuck.

dumdum420
0
 
LVL 1

Expert Comment

by:wgilster
ID: 7178237
You need to add a line in this method:

for (int t = 0; t < numberOfMenuItemsInPopup; t++) {
 Component currentMenuItem = getComponentInPopupMenuByIndex(popupMenuToShow, t);
 PropertySettingDelegate someDelegate = menuItemComponentToDelegateLinks.get(currentMenuItem);
 >>> Add Line the line here
 someDelegate.applyProperties(ClickedNode, currentMenuItem);
}


>>>Add this line
 if (!(someDelegate instanceof JLineSeperator))


0
 

Author Comment

by:dumdum420
ID: 7186156
thanx I could add line seperator.

dumdum420
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error in @AspectJ Based AOP with Spring 2 18
hibernate example using maven 12 58
String array comparison 4 38
ejb entity bean example 2 15
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question