Edit - @Uri correctly pointed out that this was an abuse of annotations; trying to actually create the menu data itself in annotations is just plain stupid.
They are good for binding, however I think that I will use them to bind text data with methods (part of @Menu ("File"), since it is more explicit and flexible than reflecting the method name. In addition, I learned a bit from this I will send the code here in a few days as an answer.
- original post -
I have not used these newfangled annotations, but they look surprisingly interesting. It's hard for me to understand the syntax though (or, in a more appropriate way, the best way to use it).
When writing code in response to this question, it occurred to me that my methods were pretty outdated.
I used to parse strings to determine my method structure and then used reflection to pass it to classes, but I think annotations can significantly improve the menu structure.
I would like to replace my test class in a file with something like the following:
@TopMenu("File,Edit") @Menu(name="File","Save,Load,Print,Preview,Quit") @Menu(name="Print","Preview,Print") @Menu(name="Edit","Copy,Paste") public class TestMenu { @MenuItem ("Save") public void save() { System.out.println("saved"); } @MenuItem ("Load") public void load() { System.out.println("loaded"); } ...
and pass the entire class to a method that produces and returns the JMenuBar associated with the class instance without further input.
The first problem is that I cannot figure out how to pass the "default" strings, they all want to have (attribute = "value") instead of just ("value"), can this be done? I can live without him, but it's a bit verbose. It would be even better if I could get rid of the partners and / or quotes, but I do not hold my breath (I think that for this I would have to define an individual interface for each menu item, which is unacceptable).
Secondly, he doesnβt like several @Menu tags for one class. I could get around this by parsing one line, but I was wondering if there was another way.
Most importantly, is there a library that does this already? (If no one comes up, I will post the code in this thread when I earn it, if someone likes it.)