This is the mail archive of the java-discuss@sources.redhat.com mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Gtk constants in Java


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:

  (define-enum GtkArrowType                                                                                                               
     (up GTK_ARROW_UP)                                                                                                                    
     (down GTK_ARROW_DOWN)                                                                                                                
     (left GTK_ARROW_LEFT)                                                                                                                
     (right GTK_ARROW_RIGHT))                                                                                                             

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.

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.

Oskar Liljeblad (osk@hem.passagen.se)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]