[prog] NetBeans custom code generation (was: Java/Swing and antialiased fonts)

Almut Behrens almut-behrens at gmx.net
Sun Apr 4 13:09:07 EST 2004

On Sun, Apr 04, 2004 at 12:15:33PM +0200, Riccarda Cassini wrote:
> The only problem I can see is that it seems to be inserting a bit too
> much. Is there an easy way to limit the editing done to certain widgets
> only?

Hi Riccarda!

Sure there is a way :)  The modified script:

#!/usr/bin/perl -i
              # -i = inplace editing
undef $/;
$xml = <>;    # read in whole file

$auxvalues = q{
        <AuxValue name="JavaCodeGenerator_CreateCodeCustom" type="java.lang.String" value="#CODE"/>

%customcode = (
    # widget name   # custom instantiation code
    JLabel       => "new AA.JLabel();",
    JButton      => "new AA.JButton();",
    JCheckBox    => "new AA.JCheckBox();",
    JRadioButton => "new AA.JRadioButton();",
    # ...
    # any other widgets you need custom code for, here

$xml =~ s|(<Component\ class="javax.swing.     # start string (fixed)
          (J[^"]+)               # extract the widget name
          ".*?</Properties>)     # anything up to the point after which
                                 # we want to insert the <AuxValues> tag
         |$1.auxval($2)          # the replacement string, consisting of
                                 # the whole matching substring plus the
                                 # dynamically determined <AuxValues>
         |xgse;   # options: x = extended regex syntax (allows comments)
                  #          g = apply globally (i.e. repeatedly)
                  #          s = multiline matching
                  #          e = evaluate replacement string as perlcode

print $xml;       # write out file

sub auxval {
    my $widget = shift;
    my $code = $customcode{$widget};    # lookup custom code
    return '' if !$code;     # do nothing if widget not found in table
    my $s = $auxvalues;
    $s =~ s/#CODE/$code/;    # place code in XML fragment to insert
    return $s;

A nice side effect of using a lookup/replacement table is that you can
easily have arbitrary custom code strings, if you should ever need to.

More detailed description available upon request :)


> Bravissima, amica mia, ti abbraccio!

I guess I wouldn't mind about the latter... (if I figured this out correctly)

More information about the Programming mailing list