Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

JTree and right click popup menu ?

Posted on 2002-07-18
10
Medium Priority
?
756 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 1200 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month11 days, 2 hours left to enroll

571 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