Gtk constants in Java
Mo DeJong
mdejong@cygnus.com
Sat Jul 15 07:35:00 GMT 2000
On Sat, 15 Jul 2000, Oskar Liljeblad wrote:
> I have been studying Java-Gnome a bit this afternoon and it definitely
> looks convertable to CNI. There are some issues of design vs. speed which
> I would like to discuss before proceeding.
>
> The main question is: How are enumeration values/constans to be referred
> to? For example, in gtk.defs there are entries like this:
...
> The most obvious way is to translate this into the following Java code:
>
> public class GtkArrowType .. {
> public static final int UP = ..;
> public static final int DOWN = ..;
> public static final int LEFT = ..;
> public static final int RIGHT = ..;
> ..
> }
...
> Java-Gnome takes another approach though. For each flag- or enumeration-set,
> (introduced by 'define-flags' or 'define-enums' in the .defs file), a
> separate class is created. Not only this, each constant is also a separate
> object. The GtkArrowType flag-set above would be generated into something
> like this:
>
> public class GtkArrowType {
> private static final int _UP = ..;
> public static final GtkArrowType UP = new GtkArrowType(_UP);
> private static final int _DOWN = ..;
> public static final GtkArrowType DOWN = new GtkArrowType(_DOWN);
>
> int value;
> private GtkArrowType(int value) { this.value = value; }
> }
>
> I consider this a bit over-kill. However it has an advantage: type checking.
> You would not be able to pass invalid constants to a method that needs only
> GtkArrowType.
Looks like over-kill to me too. They seems to be going for type
safety in enums by using objects in this way.
> Something the author of Java-Gnome probably didn't think of is that
> flags may need to be OR'ed. The above wouldn't work if GtkArrowType's
> could be combined. In my opinion this makes this alternative unusable.
>
> As I see it, there are only three viable alternatives:
>
> 1. All constants are kept in a "static" class called Gtk
> (Gtk.ARROW_UP, Gtk.ARROW_DOWN, ..). This is probably the
> most "economic" solution.
>
> 2. One "static" class for each flag/enum-set, with int-constants as
> the first alternative above (GtkArrowType.UP, GtkArrowType.DOWN,
> ..). This is the nicest solution IMHO. It is also efficient if
> the run-time needn't keep the classes in memory. (Is this true?)
>
> 3. Constants are kept in the object class which they belong to.
> I.e. GTK_BUTTON_IGNORED from the GtkButtonAction flag-set would
> be GtkButton.IGNORED in Java. This would not work for all
> enumerations, because they are used in more than one gtk
> widget. An example is the GtkMatchType enumeration (there is no
> GtkMatch). This could be solved by putting these ambiguous constants
> in a class Gtk (like alt. 2), or putting then in all classes they
> apply to.
>
> I would like to know what you think about these alternatives.
You forgot the interface option.
public interface GtkEnums {
public static final int UP = ..;
public static final int DOWN = ..;
public static final int LEFT = ..;
public static final int RIGHT = ..;
...
}
class MyClass implements GtkEnums {
void foo() {
bar(null, UP);
}
}
That way you do not need to prefix things like GtkEnums.UP.
You could also have multiple interfaces that defined
values like this, you just implement an interface to get
access to that set of enums.
later
Mo DeJong
Red Hat Inc
More information about the Java
mailing list