Skip to main content

Welcome to Geoff Hayward's Weblog

Commenting on Java, JavaFX, Java EE, Joomla, and IoT.

Some context: I'v just been bug fixing a JavaFX 8 application. The application has a JavaFX ComboBox that lets a user select a file name from a list. When a selection is made the file is read into a TextBox. This functionality worked fine until a clear button's event was added to the same controller.

@FXML
public void clear(ActionEvent event) {
    [...]
    myCombobox.getSelectionModel().clearSelection();
}

When a JavaFX ComboBox is cleared with .getSelectionModel().clearSelection() the onAction events of the ComboBox is called. This makes it important to null check within the onAction event.

@FXML
public void selected(ActionEvent event) {
    if (myCombobox.getValue() != null) {
        [...]
    }
}

To conclude: If the onAction event of a ComboBox uses the ComboBox's value within the event it pays to null check the ComboBox's value.



Read

After several weeks of programming a bespoke blogging platform it's finished. Well kind of finished. I will get to that. The blogging platform is for Web Developers, who need the back-end application part of a platform but do not wish to be tied to one language and template engine for their own front-end development work.

Why the need? I decided to create my new website with Java EE 7. I am a huge fan of Java EE 7 and JSF 2.2. The blogging platform is simply a by-product of that wish.

In the next iteration I intend to crate a REST API so that the front-end can be an authenticated REST client. Thus achieving the goal of language and template engine independence. In the meantime I'm road testing the blog platform with my own website. Once the next iteration is ready I will probably commit the platform to a public GitHub repository. You can find more information about the blog platform over at javablogplatform.blogspot.co.uk/.

Why the new website? My blogging goal has changed since creating the old website. I wish to have more of a weblog. The subject area of this new website will be anything Java EE, Software Development, and MSc study related. I have of course imported the content from my older blog.

So here goes



Read

My blogging goal has changed. I used to try and write complete 'How to' articles. Now I wish to find a voice in short observational posts. More of a weblog. The subject area that I would like to pursue in my new blog will be web application development. Anything Java related.

I am building the replacement website with Java and Java Server Faces (JSF). I am going with a Bootstrap look too. I like Bootstrap; it's easy to work with.

I cannot wait to start blogging again now I have formed a plan so I have started a blog about the creation of the new blog platform over a Blogspot.

I will be getting the first iteration of my new blog and website ready for the end of June 2015. Ready for Devoxx Krakow. I have my ticket and I cannot wait to go.

Devoxx Poland Attendee Badge

Read

Using Apache Module mod_rewrite to do a character replace is a little fiddly. It's particularly fiddly when you need to send back a single 301 redirect. But, it's not impossible.

mod_rewrite Underscores to Hyphens

In this solution all underscores are replaced with hyphens before the final 301 redirect is sent to the browser.

RewriteRule ^(.*)_(.*)$ $1-$2 [N,E=redirect:true]
RewriteCond %{ENV:redirect} ^true$
RewriteRule (.*) $1 [R=301]

The solution I created above looks for a match that includes anything either side of an underscore plus an underscore. Both sides of the underscore are captured at the same time. At the end of the match the capture groups are put back together, this time with a hyphen between them. The 'N' flag indicates to mod_rewrite that the rewrite rules should be run agents the subject all over again. The subject is changed to reflect the last run before it's re-evaluated. This rerunning continues until there are no more matches.

Unfortunately, you cannot just add the 'R' redirect flag to the end of the rewrite rule akin to this:

RewriteRule ^(.*)_(.*)$ $1-$2 [N,R=301]

In this shorter unfit solution the 'R' flag is evaluated and the redirect is sent before the rewrite rule can be rerun. Therefore for each replaceable character a redirect is sent back over and over until all the characters have been replaced. This will create needless round trips between the client and the server.

The redirect 'R' flag cannot be on the same line as the 'N' flag in order for the 'N' flag to do its job. Therefore, the 'R' flag needs to follow another rewrite rule in order for the redirect to be sent back only once. The problem is there actually is no more rewriting to be done. The following rewrite rule works as anything will match the pattern triggering the 'R' flags function to send a redirect.

RewriteRule (.*) $1 [R=301]

However, having this rewrite rule on its own will make all requests that are filtered via this set of rules (.htaccess file etc.) redirect. Consequently, a rewrite condition needs to prevent all other request from being redirected and only allowing redirects where a character has been replaced in the main rewrite rule.

Using the 'E' flag allows you to set an environment variable. In this case the environment variable is set after the 'N' flag. It is set for each time the rewrite rule is run: don't forget the last run will not match as all replacements have completed.

The variable in my solution is named 'redirect' and given a String value of true. The solution uses the environment variable as a means to prevent or trigger the 'match all' rewrite rule so that the redirect can be sent back appropriately.



Read

In this post I describe how the logic needed to add a CSS current marker to a webpage can be achieved very simply with very little logic on the website's server. I look at why a current page marker is important and why we need to use them.

Good User Centered Design

When a website user sees a distinctly marked menu item he/she is instantly orientated to where within the website they are in terms of the website structure. Without a distinct marking a user cannot easily determine where they are within the website. A lack of marking leads to avoidable extra cognitive work on the user's part.

Basic illustration

In providing a good user experience the burden of identifying where a user is, should be the concern of the website itself. Using CSS, the information can be conveyed to a normal sighted user with colour. The colour needs to unambiguously highlight the corresponding menu item related to the page the user is currently on.

It's common practice to achieve this by adding a marker via the use of a CSS class within the mark-up. The CSS class may have a name akin to 'current'. The CSS class is added, often server side, to the menu item during the creation of the menu. For example:

<ul id="nav">
	<li class="current"><a href="home">Home</a></li>
	<li><a href="about">About</a></li>
</ul>

Then using CSS such as the example below, the item will appear highlighted.

.current a
.current a:link
.current a:visited{
	Background-colour: # 

}

Braking the Rules Elegantly

Here is the problem with adding a 'current' class. During the menu creation you have to get the server to repeat the decision: is this the item to add the class to? Ok, not a big problem and if you have already implemented this logic in old projects, it is probably better to leave it in.

However before you implement this logic into your next project, be aware there is a second way. I had this idea recently looking at this problem for myself for the first time. The solution that dawned uses less logic and not one 'if' statement.

I normally would say 'no way' to using a style attribute directly in a webpage's source because it breaks the rule of separation. Sometimes, however, breaking the rules can lead to a very elegant solutions. Elegant in this case because of a large reduction in the logic is removed from the menuing process.

How to Reduce the Logic

First, give a unique name to each page. Then simply use the unique name as a CSS class on every menu item.

<ul id="nav">
	<li class="home-page"><a href="home">Home</a></li>
	<li class="about-page"><a href="about">About</a></li>
</ul>

Second, output the name of the current page, as a CSS class, in the head element of the page's HTML each time the page is dispatched. Here is an example of how this may look using EL in JSP:

<style>
.${page.clsss} a
.${page.clsss} a:link
.${page.clsss} a:visited{
	Background-colour: # 

}
</style>

Then, using the homepage as an example, the output would look like this:

<style>
.home-page a
.home-page a:link
.home-page a:visited{
	Background-colour: # 

}
</style>

The CSS would then highlight the homepage as the current page. As soon as the user moves to another page, the class name within the style tag will change to reflect the name of the next current page.

This also means that the menu can be created and cached once for every time the menu is edited.


Tags: CSSJSP

Read

Mailing List


Responsive Media

With the ResponsiveMedia plugin for Joomla it is easy to add 3rd party content from YouTube, Vimeo, and Instagram right in to any Joomla! article.

ResponsiveMedia