Archive for the ‘Programming’ Category

OpenLaszlo Project Blog » OpenLaszlo 4 Programming Tutorial

June 4, 2007 in Programming | Comments (0)

I’ve been looking at OpenLaszlo a little more, and found this great video introduction on the project blog. 

Link to OpenLaszlo Project Blog ยป OpenLaszlo 4 Programming Tutorial


OpenLaszlo | the premier open-source platform for rich internet applications

June 3, 2007 in Programming | Comments (0)

OpenLaszlo lets you write Flex 2 applications without shelling out cash to Adobe. Plus, you can compile it to run as a Flash swf file, or as DHTML. Looks pretty good. 

Link to OpenLaszlo | the premier open-source platform for rich internet applications

One thing to watch out for, though… at least on the Windows platform. It creates a batch file that sets the JAVA_HOME environment variable and then starts the Tomcat server (on my machine, it was here: “C:\Program Files\OpenLaszlo Server 4.0.2\Server\lps-4.0.2\lps\utils\startup.bat”). If you had the wrong JAVA_HOME value set during the install (I upgraded my JDK after I installed OpenLaszlo, so it was pointing to an old folder), the Tomcat startup will always point it at the old value, so go rip this line out of the batch file before you stumble over it like I did.


Powerful Printing in Flash

April 19, 2007 in Programming | Comments (0)

I haven’t messed around with Flash in a long time, and when I implied that I wanted to do a presentation with it I was warned that you couldn’t print a “deck” from a flash presentation like you could from PowerPoint. I figured this probably wasn’t true, so I started poking around for articles on how to go about printing from Flash.

Sure enough, it’s doable with a little scripting. What I found interesting was that none of the articles I stumbled across were newer than 2002 or so. I’m hoping that means that the printing mechanism just hasn’t changed much in a long time.

This article gave a nice, simple overview. If you’ve had any experience printing with Flash, especially dumping a bunch of key “slides” out as a deck for distribution at a meeting, feel free to comment and post links. I wouldn’t turn down that kind of help.

Link to Powerful Printing in Flash


The REAL Rails AJAX Select Control Tutorial

April 1, 2006 in Javascript,Programming,Ruby,Ruby on Rails | Comments (0)

I’ve been trying for a while to implement functionality in Rails where, when the value of one selector is changed by the user, a related selector is refilled with associated data. I’m rewriting one of my PHP applications (The CIty of Villains Registry) in Rails, so I’ve already implemented similar functionality in PHP. Unfortunately, the amount of documentation out there for this sort of thing is rather sparse, and leaves a couple of wide holes open for developers to fall into. I finally got it to work, but it was tough pulling all of the information together. I figured it would be worth my while to post what I did to help others wrestling with the same thing (and there appear to be quite a few).

Create a Partial Template to Render your Select Control

For my application, there is one select control for choosing a hero archetype. Two other selectors let you choose primary and secondary powersets. We’ll just concentrate on the primary powerset selector for this exercise. When the page loads, whatever value is in the archetype selector will determine what values should be available in the powerset selector, so my template does a quick find based on whatever archetype ID is buried in the hero model and generates a select control with the matching powerset names and ID’s.

<%=select(“hero”, “primary_powerset_id”, ArchetypePrimary.find_all_by_archetype_id(archetype_id).collect {|p| [ p.powerset.name, p.powerset_id ] })%>

For my application, this got saved as app/views/hero/_primary_powerset_selector.rhtml

Modify your Page Template to render your Select Control

Now that we have a partial template, we need to get it to render in the parent page. Keep in mind, though, that with an AJAX call, we’re going to be replacing the content of an object on the page with the rendered output, so we’re going to bury the render inside a span tag. We’ll use that name of that tag later on when we make our AJAX call.

<span id=’primary_powerset_selector’>
<%= render :partial => ‘primary_powerset_selector’, :locals => {:archetype_id => @hero.archetype_id}%>
</span>

In my application, this code goes in the app/view/hero/_form.rhtml file where I want the select control to appear.

Create an Action to Render your Partial Template

What we’ve done so far will make the filtered selector appear on the page, loaded with the filtered set of results. In order to make it dynamic, we have to build a custom action into our controller that knows how to refresh the select control. It will take the new archetype ID as an argument, then render our template to produce a select control. It’s a pretty simple call, it just takes the passed argument and renders the partial:

def fill_primary_powerset_box
render(:partial => ‘primary_powerset_selector’, :locals => {:archetype_id => @params[:archetype_id]})
end

In my application, this method was added to the app/controllers/hero_controller.rb file.

Add the AJAX Call to your Page Template

Everything is in place, so now we can ask Rails to render the AJAX call that will dynamically render the select control into our span. Somewhere after the span, drop something like this into your template:

<%= observe_field(
‘hero_archetype_id’,
:update => :primary_powerset_selector,
:url => {:controller => ‘hero’, :action => :fill_primary_powerset_box},
:with => “‘archetype_id=’ + value”)
%>

So what’s worth noticing is the following: I’m passing in the name of the selector that is being watched (when my hero_archetype_selector changes I want this event to be fired), I’m giving it the name of the span I want to be updated with the new content (primary_power_selector is the name of my span), I’m telling it to user the hero controller and call the fill_primary_powerset_box action, and I’m handing the value of the control that changed as an argument named archetype_id.

If you’ve been trying to figure this stuff out for any time at all you’re going to notice one that is missing: The frequency parameter. Every example I have seen for observe_field passes in a frequency parameter, but you only need to use this if you really need to be polling, like maybe you’re pulling in search result as the user types, or dynamically rendering the text being typed somewhere else on the page. You don’t need it to link two select controls together. Leaving this parameter out makes the event fire when the value in the selector changes. I had started out using a frequency of zero, which would make the event fire when it should, but the Prototype library would throw an error. This is because it was sending my zero as the callback instead of a function reference. Just leave it out and you’ll be fine.

It was a lot of working figuring out how to set this up right for the incredibly small amount of code it actually takes to pull it off. Hopefully this will make it easier for you set up a similar configuration in your own applications.

Caveats

Now that I have it working I do notice that the callback is considerably slower than my homebrew PHP version was. Maybe it’s because I’m on a shared server, and maybe it’s because I’m running in dev mode right now. I have to poke around at it a little bit more.

Helpful resources:

  • Rails Hosting – This site has some good examples of using the Prototype library for AJAX and client side scripting with Ruby.
  • Using Prototype v1.4.0 – This helped me ultimately deduce the problem in my rendered calls since it describes the actual constructors and arguments to the Prototype objects. I could see where Form.Element.EventObserver did not take a frequency argument, and that my frequency value of zero was being passed in place of the callback.
  • Rory Hanson’s – This was where I started, since it came up first in Google. It gives some good examples of how to set up the code. You’ll notice, however, the number of people having difficulty making it work.
  • Adam C. Hegedus’s Blog Entry – This one really helped me get started, picking up where Rory’s blog left off.

Gambas – Gambas Almost Means Basic

January 8, 2005 in General,Programming | Comments (0)

Found a reference to this development environment on Slashdot. It is a BASIC-like language and development environment. All of my development efforts on Linux are web applications or back end tools, so lots of scripting in PHP, Ruby, and Perl, but not a lot of client development, so this is pretty interesting. I’ve also got Eclipse and Mono… I guess the only thing really missing is a project to work on.

Rather than think of a new project, I should do some more work on the City of Heroes Registry, or enabling my .NET application in Mono, but I love playing with new development tools.


Flash FocusManager Hell

June 17, 2004 in Programming,Rants | Comments (0)

I’ve been experiencing a lot of pain working with Flash lately, most recently with the FocusManager object. I posted this article in their unofficial support forums and figured it would be worth replicating here, especially since City of Heroes has got me too busy to update my log much lately. ๐Ÿ™‚

Here’s what I wrote:

Here are a few things I learned during my painful experience with the FocusManager…

While the FocusManager appears to be a static class (like Math, for instance), it is not, and you need to use what is apparently an instance on the _root to access it, and it doesn’t have the same case, so:

FocusManager.setFocus(MyField); // Does not work
focusManager.setFocus(MyField); // May work, if your script is on the _root element
_root.focusManager.setFocus(MyField); // Will most likely work if the path to your field is correct

While you might assume that you can use the FocusManager to manage the focus of regular input fields and buttons, this is not the case. You have to use the component classes TextInput and Button from the Flash UI Components set (probably TextArea and such as well, but I haven’t used those yet). This one kept me stuck for a while, until something in one of the examples finally penetrated my skull.

You should remember to set the tab index of the fields you want to manage. Flash will use the z-order (in other words, the creation order) of the objects to determine tab order as long as none of them have a tab index value. If you set one, it will be the only one that gets focus from the manager. You can do this in your code with the .tabindex property of the input text fields and buttons, or you can set it in the GUI. It’s hidden in an unlikely spot, in the Accessibility properties for the component. Open that little dialog and you can click through the components and set it. I’m assuming that if you place a tie, the z-order is the tie-breaker.

Aside from that, it was mainly just discovering the right path to put in to reach the objects. I’m using several forms in my app, with functionality built into a custom class on one of the screens (the rest are just forms or dialogs), requiring this._parent._parent.BaseForm.DoSomething() types of relative paths in the ActionScript editor.

My biggest complaint about this whole FocusManager setup is that it is so silent. It would have saved me a lot of time if, the first time I tried to hand it a regular input text object, it had piped up and said, “I can’t set focus to that! Try a component from the UI Components set!” It wouldn’t have hurt, the first time I tried FocusManager.setFocus(MyField) if it had said, “FocusManager is not a static object – use the focusManager property of _root.”

The problem with Flash is not that the bugs and weird UI and syntax inconveniences and inconsistencies are so awful, it’s just that it’s so damned difficult to figure out where the problem is in the first place. The least they could do is make trace() work from an .swf, since it’s much more useful than the built-in debugger if you’re working on a serious application. The built in debugger is all but useless if you’re building a forms application, and the tricks for breaking into an external script file don’t work well in that instance either. Besides, my app won’t do much in the player, so I need better ways to catch runtime bugs in the hosted environment.

The Flash tool set is great for making toys and ad banners, but for real business application development, it leaves a great deal to be desired. What I wouldn’t give for a strict setting in my script.


Points back on for Windows: Microsoft’s WIX MSI/MSM Tool

April 6, 2004 in Programming | Comments (0)

I downloaded WIX and took a look at it. It’s not all that I had hoped for, but I think I might like it better than the stuff built into VS.NET. We’ll see.

It isn’t really an installer per se… it only builds MSI and MSM files. I didn’t dig in deep enough to see how much programmatic control you have over it, and the docs are very scant so far, but I think it might be useful in the long run.

In the meantime, I’m still toying with the Nullsoft Scriptable Install System. I think that it’s superior to the “programming under rocks” pardigm of tools like Setup Factory. Besides, Setup Factory has crap for .NET support.

The comments on Slashdot about this item leave me laughing. I love to go there and read the articles, but all of the Linux trolls on the planet live under that particular log, and if any article mentions Microsoft in any way they come lurching out of the shadows to complain about it, whether it’s good or bad. It’s quite comical.


Apache module mod_proxy

March 11, 2004 in Programming | Comments (0)

This is one of the greatest features of Apache that I’ve found yet. We have an application that offers some web services, but runs on a Windows server. We don’t want to put the Windows server on the front lines, so we proxy requests from a Linux Apache server to the Windows server behind the firewall.

To make this work, you have to enable the proxy. Here’s what the proxy directives look like in our httpd.conf file:

<IfModule mod_proxy.c>
    ProxyRequests On
    <Directory proxy:*>
      Order deny,allow
      Deny from all
      Allow from 10.0.1.0/16
    </Directory>
</IfModule>

The nice thing about this configuration is that it will allow anything on our internal network to be proxied through, but external requests get denied (that’s what the Allow from part does). We only consume the web services internally (server processes or wrapper pages that control access), so this lets us make the output from the services available without actually exposing the Windows web server on the wild web.

To make the services description language available, you can add a <files> section and specify the extensions you want to let through from an external source (add this inside the IfModule block):

<Files "*.wsdl">
  Order deny,allow
  Deny from none
  Allow from all
</Files>

Now we can advertise the services publicly, even though we only satisfy them locally.

You then need to set up the virtual host sections for the proxied web sites. The virtual directory for the web services application should already be set up on the Windows server. In this directive, you specify the name of the directory to watch for, and then the http mapping to remote virtual directory on the Windows box.

<VirtualHost 10.0.1.1:80 ###.###.###.###:80>
  ServerName server.name.com
  DocumentRoot /www/services
  ProxyPass /RemoteServiceVirtualDir/ http://windows_server/RemoteServiceVirtualDir/
  <Directory /www/services>
    <Files *.htm*>
      SetHandler perl-script
      PerlHandler Apache::SSI
      PerlSetVar SSIPerlPass_Request no
    </Files>
    Options SymLinksIfOwnerMatch +ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

Any requests for http://server.name.com/RemoteServiceVirtualDir will be forwarded to the Windows machine for resolution.


All I need is another programming language to learn…

February 9, 2004 in General,Programming | Comments (0)

But I just can’t help myself.

I was searching for something else on Amazon when I stumbled across some book about using Lua for game programming. Now the book apparently sucked, but the language appears to be pretty powerful, especially for game programming and application extensions.

Since most of my projects tend to string together a whole slew of different technologies, anything that can help me glue them together more tightly is of interest. I currently use Ruby for scripting my metadata code generation tools, but Lua sounds like it can be compiled and delivered as a standalone executable more cleanly. You can do this with Ruby, but it packs the whole interpreter into the executable. In Windows this all tends to break down if you hook the wrong API’s, most specifically any of the ODBC stuff (although I haven’t tried it lately).

Anyway, something else to think about and poke around with in my “spare time.”


Transact SQL Loops

January 22, 2004 in Programming,Transact-SQL | Comments (0)

I noticed that someone had pinged my site searching for Transact SQL Loops. I figured maybe I ought to post one, since I’m sure my blog disappointed the searcher.

The first thing you have to do is declare a cursor. The cursor will allow you to walk through the results of a query one row at a time, so declare the cursor with a meaningful name and the query that will provide the results you’re looking for:

declare cust_cursor cursor for
  select
    customer_number
  from
    customers
  order by
    customer_number

You need to have some variables declared to catch the results of the fetch.

declare @customer_number as int

Now you can open the cursor and fetch the results. The system variable @@fetch_status will tell you when there is no data left in the cursor. You need to fetch one time before you start the loop so you’ll know if there’s any data to retreive at all, then again at the bottom of the loop to get the next record (if any). Your loop condition is based on @@fetch_status, so the minute it returns anything but zero (for success), you bail out of the loop.

Notice the fetch next/into syntax that reads the values from the next row in the cursor into the local variables you’ve created.

open cust_cursor
fetch next from cust_cursor into @log_date
while @@fetch_status = 0
begin
  ' Do stuff with the value you retrieved
  select @customer_number
  fetch next from cust_cursor into @customer_number
end

Once you’re through with your loop, you need to close the cursor and deallocate to free the resources that it was using.

close cust_cursor
deallocate cust_cursor