Archive for October, 2008

Spooky Java Code

Okay, file this under, “Holy crap!”, because I just had an odd “I wonder if…” thought last night
that turned into something both interesting and frightening at the same time. I will say this in
it’s own little text box to bring home the oddness:

In Java you can name a member with dollar sign ($) or underscore (_) alone.

You might want to read that again so that it sinks in. This means that you can have the following
code that will compile and run along happily with no errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Odd {

    private static final String $ = "Dollar-sign: ";
    private static final String _ = "Underscore: ";

    public static void $(final String name){
        System.out.println($ + name);
    }

    public static void _(final String name){
        System.out.println(_ + name);
    }
}

Now, the underscore is not all that far out. I knew it could be used __in__ member names, but not
__as__ the member name by itself. The dollar sign just blew my mind.

What I need to find out is whether this is “to spec” or if it’s just some oddity of the Mac JVM. I will need to give this a try on my Windows box at home.

Now, in no way do I want to promote the use of these as method or variable names as they lead
to functionally obfuscated code. They should probably never be used, but it seemed like an
appropriate pre-Halloween posting.

Popularity: 1% [?]

  • Share/Bookmark

Tags: , ,

Floating and Following Div

I needed one of those DIVs that appears on call and then stays in view even when you scroll, until you close it. For lack of a better name, I call it the floating following div, and it’s pretty easy to make. With a little help from Prototype we can even make it work across the major browsers.

First you need to put the div to be floated somewhere on your page. The page itself can be anything you want.

1
<div id="movable">This is my floating area</div>

and then you need to give it some initial style:

1
2
3
4
5
6
7
#movable {
    position: absolute;
    left: 100px;
    width: 200px;
    height: 200px;
    background-color: red;
}

Once all that is on the page, you will need some JavaScript to do the fancy stuff:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript">          
    Event.observe(window,'load',function(evt){
        $('movable').hide();
           
        Event.observe('showme','click',showDiv);
             
        Event.observe(window,'scroll', function(evt){
            $('movable').setStyle({ top: 8 + document.viewport.getScrollOffsets().top + 'px' });
        });
    });
           
    function showDiv(evt){
        $('movable').show();
    }
</script>

This causes the “movable” element to be hidden. Once the button with an id of “showme” is clicked, the element will be shown and will then follow along with vertical scrolling, staying up near the top of the view port. The key to this following motion is the function mapped to the window scrolling event:

1
$('movable').setStyle({ top: 8 + document.viewport.getScrollOffsets().top + 'px' });

The document.viewport.getScrollOffsets() function is provided by Prototype.

It’s nothing exciting, but it works… just another thing posted here for future reference.

Popularity: 57% [?]

  • Share/Bookmark

Tags: , , ,

Ant Input Prompting and Private Targets

I have found the Ant input tag useful lately for setting up runtime parameters of an Ant build. We have a few different server configuration settings that vary based on which server the artifact is being built for and the input tag makes this really easy:

1
<input message="Enter configuration name: " addproperty="config.name" defaultvalue="${config.name.default}" />

The downside of this is that it will prompt you to enter this every time you run the build, which can become annoying and really prohibits automated building. This is where the unless attribute of the target tag comes into play.

First create a private target (one whose name starts with “-”) that will prompt for the config name:

1
2
3
<target name="-prompt-for-config">
    <input message="Enter configuration name: " addproperty="config.name" defaultvalue="${config.name.default}" />
</target>

Then add the unless attribute to check for the presence of the config.name property:

1
2
3
<target name="-prompt-for-config" unless="config.name">
    <input message="Enter configuration name: " addproperty="config.name" defaultvalue="${config.name.default}" />
</target>

which will cause this task to be run only if the specified property is not set. The you can have other tasks depend on this private task, which will only run if you have not specified the config.name property on the ant command line.

1
2
3
<target name="compile" depends="-prompt-for-config" description="Compiles the java sources.">
    <!-- do stuff -->
</target>

Calling ant with the following will not prompt the user for the config.name:

ant compile -Dconfig.name=foo

I have used this in a few places now to make the build a bit more flexible, such as for doing server deployments, artifact installations, etc. It is a handy ant trick to keep in mind.

Popularity: 34% [?]

  • Share/Bookmark

Tags: , ,

Calling Parent Code from IFrame

Every now and then I have needed to access the parent page enclosing an IFrame and I have never really found a good straight-forward example of how to do it, so I end up having to work it out each time I need it. So, for a parent (enclosing page) with an IFrame as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
    <head>
        <title>IFrame test</title>
        <script type="text/javascript">
        function closeIFrame(){
            if(confirm("Are you sure you want to close the iframe?")){
                document.getElementById("frame").style.display = 'none';
            }
        }
        </script>
    </head>
    <body>
        <iframe id="frame" src="visitor.html" width="200" height="200"></iframe>
    </body>
</html>

where the “visitor.html” page called by the IFrame is given as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<html>
    <head>
        <title>Visitor</title>
        <script type="text/javascript">
        function closeMe(){
            parent.closeIFrame();
        }
        </script>
    </head>
    <body>
        <p>Hello, just visiting.</p>
        <button onclick="closeMe()">Close Me</button>
    </body>
</html>

When the “Close Me” button is clicked, the parent page will dispose of the IFrame, but the JavaScript function to do this actually resides in the parent page.

One thing to note, is that both pages must reside on the same domain (or sub-domain) or else the script will not work, due to security restrictions.

Popularity: 1% [?]

  • Share/Bookmark

Tags: , ,

Switch to our mobile site