Since "static abstract" isn't possible, any thoughts on how to do this...

E

Elrod

Ok, here's the scenario:
I have a GUI application which uses a JTree for navigation. The nodes
on the tree have a parameter that stores a Class object, and when the
user clicks on the node, a new instance of a form represented by that
Class is instantiated. Up to this point, I have no problems.
The catch is that different users will see slightly different sets of
tree nodes, depending upon which permissions they have.
The way I was *hoping* to handle this was to declare an abstract class
like this:

public abstract UserForm extends javax.swing.JPanel
{
public abstract static long requiredRightsToView();
public abstract static long requiredRightsToEdit();
}

....and have all forms extend this class, and as such be forced to
declare their individual required access rights. I would then retrieve
those values as I build the tree to determine whether or not a
particular node should be shown. The methods (or potentially just
static constants) must be static so that I don't need to instantiate
each class to get its required rights.

I am quite open to any other methods, even if they bare no resemblance
to the train of thought I was on. Essentially, the core of what I need
is to be able to retrieve the requiredRights values from a form given
only the form's Class object and without instantiating the class.

thanks
 
J

Jacob

Elrod said:
Ok, here's the scenario:
I have a GUI application which uses a JTree for navigation. The nodes
on the tree have a parameter that stores a Class object, and when the
user clicks on the node, a new instance of a form represented by that
Class is instantiated. Up to this point, I have no problems.
The catch is that different users will see slightly different sets of
tree nodes, depending upon which permissions they have.
The way I was *hoping* to handle this was to declare an abstract class
like this:

public abstract UserForm extends javax.swing.JPanel
{
public abstract static long requiredRightsToView();
public abstract static long requiredRightsToEdit();
}

...and have all forms extend this class, and as such be forced to
declare their individual required access rights. I would then retrieve
those values as I build the tree to determine whether or not a
particular node should be shown. The methods (or potentially just
static constants) must be static so that I don't need to instantiate
each class to get its required rights.

I am quite open to any other methods, even if they bare no resemblance
to the train of thought I was on. Essentially, the core of what I need
is to be able to retrieve the requiredRights values from a form given
only the form's Class object and without instantiating the class.


If you have your access flags as a "static final long" within
your class, whould it somehow be possible to reach their value
by use of class inspection (reflection)?

A simpler, but less elegant solution:

public class AccessManager
{
public static long requiredRightsToView (Class clazz)
{
if (clazz == UserForm1.class)
return someValue1;
else if (clazz == UserForm2.class)
return someValue2;
:

else
return someDefault;
}

// same for requiredRightsToEdit
}
 
V

VisionSet

Elrod said:
Ok, here's the scenario:
I have a GUI application which uses a JTree for navigation. The nodes
on the tree have a parameter that stores a Class object, and when the
user clicks on the node, a new instance of a form represented by that
Class is instantiated. Up to this point, I have no problems.
The catch is that different users will see slightly different sets of
tree nodes, depending upon which permissions they have.
The way I was *hoping* to handle this was to declare an abstract class
like this:

public abstract UserForm extends javax.swing.JPanel
{
public abstract static long requiredRightsToView();
public abstract static long requiredRightsToEdit();
}

...and have all forms extend this class, and as such be forced to
declare their individual required access rights. I would then retrieve
those values as I build the tree to determine whether or not a
particular node should be shown. The methods (or potentially just
static constants) must be static so that I don't need to instantiate
each class to get its required rights.

I am quite open to any other methods, even if they bare no resemblance
to the train of thought I was on. Essentially, the core of what I need
is to be able to retrieve the requiredRights values from a form given
only the form's Class object and without instantiating the class.

mmmm, some sort of abstract factory approach...

Have a HashMap of the available classes for your tree, you must have some
sort of collection already right?
Have the classes as key and an AccessRights object as value.

You can have it so a class loader loads the classes, and the classes
registers themselves with the abstract factory and update the HashMap.

Then use your Abstract Factory to both determine access rights and get your
instances.
 
C

Chris Smith

Elrod said:
I have a GUI application which uses a JTree for navigation. The nodes
on the tree have a parameter that stores a Class object, and when the
user clicks on the node, a new instance of a form represented by that
Class is instantiated. Up to this point, I have no problems.
The catch is that different users will see slightly different sets of
tree nodes, depending upon which permissions they have.
The way I was *hoping* to handle this was to declare an abstract class
like this:

public abstract UserForm extends javax.swing.JPanel
{
public abstract static long requiredRightsToView();
public abstract static long requiredRightsToEdit();
}

Wanting to declare a static method signature that's repeated in other
classes is a warning sign for bad design. Perhaps instead of tracking
java.lang.Class instances, you could track instances of factory objects,
as such:

public class UserFormFactory
{
public long requiredRightsToView()
{
return ...;
}

public long requiredRightsToEdit()
{
return ...;
}

public JComponent getFormComponent()
{
return new UserForm();
}
}

These instances, once retrieved, would give you the same abilities to
create new forms as the Class instance, but also add the methods to get
access rights.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
B

Boudewijn Dijkstra

Elrod said:
Ok, here's the scenario:
I have a GUI application which uses a JTree for navigation. The nodes
on the tree have a parameter that stores a Class object, and when the
user clicks on the node, a new instance of a form represented by that
Class is instantiated. Up to this point, I have no problems.
The catch is that different users will see slightly different sets of
tree nodes, depending upon which permissions they have.
The way I was *hoping* to handle this was to declare an abstract class
like this:

public abstract UserForm extends javax.swing.JPanel
{
public abstract static long requiredRightsToView();
public abstract static long requiredRightsToEdit();
}

...and have all forms extend this class, and as such be forced to
declare their individual required access rights. I would then retrieve
those values as I build the tree to determine whether or not a
particular node should be shown. The methods (or potentially just
static constants) must be static so that I don't need to instantiate
each class to get its required rights.

If you use a static method of a class for the first time, that class gets
loaded by the classloader and a Class object gets created. Either way,
objects get created. Why would you prefer letting the classloader create
the objects?
 
E

Elrod

If you use a static method of a class for the first time, that class gets
loaded by the classloader and a Class object gets created. Either way,
objects get created. Why would you prefer letting the classloader create
the objects?

I'm not entirely sure I understand what you're asking here, but from the way
I interpret it, what I DO want is for the class to be loaded and the Class
object created so that I can access the accessRights values. I DO NOT want
the actual form that the Class represents to be generated until the menu
item is clicked by the user.
 
E

Elrod

mmmm, some sort of abstract factory approach...
Have a HashMap of the available classes for your tree, you must have some
sort of collection already right?
Have the classes as key and an AccessRights object as value.

You can have it so a class loader loads the classes, and the classes
registers themselves with the abstract factory and update the HashMap.

Then use your Abstract Factory to both determine access rights and get your
instances.

This approach looks to be about as close to what I'm looking for as I'm
going to get. I'll admit that I'm not currently familiar with the "Abstract
Factory" concept, so it would probably be worth my while to do a little
research, but I'm pretty sure I see what I need to do to make it work. The
big thing I'll be lacking over what I was originally aiming for is that I'll
have to make sure that every form has the code to register itself, as
opposed to the presence of said code being guaranteed by extending an
abstract class.

Thanks
 
B

Boudewijn Dijkstra

Elrod said:
I'm not entirely sure I understand what you're asking here, but from the way
I interpret it, what I DO want is for the class to be loaded and the Class
object created so that I can access the accessRights values. I DO NOT want
the actual form that the Class represents to be generated until the menu
item is clicked by the user.

OK, I understand now why you want that. But I have another approach:

The Input Method Framework uses abstract info-objects. If the IMF requests
it, the info object is asked to perform some actions, e.g. to give
information on the IM or to create an instance of the IM. You could do the
same thing with your forms: one class for the rights-information and one to
carry the form.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,839
Latest member
MartinaBur

Latest Threads

Top