Solved

JTree and right click popup menu ?

Posted on 2002-07-18
10
734 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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now