Home

Awesome

<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=85 /> Glimmer DSL for Swing 0.0.5

JRuby Swing Desktop Development GUI Library

Gem Version Join the chat at https://gitter.im/AndyObtiva/glimmer

Glimmer DSL for Swing enables building desktop applications with Java Swing, Java AWT, Java Foundation Classes and Java 2D via JRuby.

There has been a great divide between two big GUI toolkits in Java:

Glimmer intentionally avoided Swing-like GUI toolkits in the past because they produced non-native looking graphical user interfaces that not only looked out of place in various operating systems, but also degraded usability, user experience, and the professional look of applications significantly, especially given that unlike SWT, Swing performance is affected by Java Garbage Collection pauses. As such, Glimmer DSL for SWT was initially born as the premiere Glimmer DSL.

That said, from a balanced software engineering point of view, there are sometimes non-functional requirements that might target Swing as an appropriate GUI toolkit solution. Like in the case of extending legacy Swing applications or developing rare applications that require fully custom looking graphical user interfaces (typically not recommended), such as traffic control planning or diagramming applications. In the latter case, it would not matter whether to use SWT or Swing as they both provide support for building non-native components (in addition to native widgets in the case of SWT).

Glimmer DSL for Swing aims to supercharge productivity and maintainability in developing Swing applications by providing a DSL similar to Glimmer DSL for SWT having:

Hello, World!

screenshots/glimmer-dsl-swing-mac-hello-world.png

jframe('Hello, World!') {
  jlabel('Hello, World!')
}.show

NOTE: Glimmer DSL for Swing is currently in early alpha mode (incomplete proof-of-concept). If you want it developed faster, then open an issue report. I have completed some GitHub project features much faster before due to issue reports and pull requests. Please help make better by contributing, adopting for small or low risk projects, and providing feedback. It is still an early alpha, so the more feedback and issues you report the better.Please help make better by contributing, adopting for small or low risk projects, and providing feedback. It is still an early alpha, so the more feedback and issues you report the better.

Learn more about the differences between various Glimmer DSLs by looking at the Glimmer DSL Comparison Table.

Prerequisites

Note: On the Mac, if you have Glimmer DSL for SWT installed, and it added export JRUBY_OPTS="$JRUBY_OPTS -J-XstartOnFirstThread" to your .zprofile, .zshrc, .bash_profile, or .bashrc, make sure to disable it before using Glimmer DSL for Swing. Unfortunately, it is not compatible with it and will hang its apps until disabled.

Setup

Option 1: Install

Run this command to install directly:

gem install glimmer-dsl-swing -v0.0.5

Option 2: Bundler

Add the following to Gemfile:

gem 'glimmer-dsl-swing', '0.0.5'

And, then run:

bundle

Usage

Require the library and mixin the Glimmer module to utilize the Glimmer GUI DSL for Swing:

require 'glimmer-dsl-swing'

include Glimmer

jframe('Hello, World!') {
  jlabel('Hello, World!')
}.show

For actual application development outside of simple demos, mixin the Glimmer module into a custom application class instead:

require 'glimmer-dsl-swing'

class SomeApplication
  include Glimmer

  def launch
    jframe('Hello, World!') {
      jlabel('Hello, World!')
    }.show
  end
end

SomeApplication.new.launch

Glimmer GUI DSL

The Glimmer GUI DSL enables development of desktop graphical user interfaces in a manner similar to HTML, but in one language, Ruby, thus avoiding the multi-language separation dissonance encountered on the web, especially given that Ruby looping/conditional constructs do not need scriptlets to be added around View code. This makes desktop development extremely productive.

1 - Keywords

You may declare any swing/awt component with its keyword, which is the underscored version of the class name. For example, jframe is the keyword for javax.swing.JFrame (j_frame is acceptable too)

Examples:

jframe
jbutton
jlabel

2 - Arguments

You may pass any arguments that a swing/awt component constructor accepts to its Glimmer keyword.

Example (JFrame, JButton, and JLabel have a constructor signature that accepts a string representing title or text):

jframe('Hello, World!')
jbutton('Push Me')
jlabel('Name')

The recommended style is to always wrap arguments with parentheses for component keywords.

3 - Content Block

You may pass a content block to any swing/awt component keyword, which contains properties and/or nested components.

Example:

jframe('Hello, World!') {
  minimum_size 320, 240

  jlabel('Hello, World!')
}

The recommended style for the content block is always curly braces {} to denote as View nesting code different from looping/conditional logic, which utilizes do;end instead.

Property arguments never have parentheses.

4 - Listeners

You may declare listeners with their on_-prefixed event method name on the swing/awt listener class (these are the classes in the signatures of AddXYZListener methods on swing/awt component classes).

For example, JButton has an AddXYZListener method called AddActionListener, which accepts an ActionListener class. That class has one event method: actionPerformed. In Glimmer, you simply underscore that and prefix with on_:

jframe('Hello, Button!') {
  jbutton('Click') {
    on_action_performed do
      puts 'Clicked!'
    end
  }
}

The recommended style for listeners is always a do; end block.

5 - Component Proxy & Methods

When utilizing the Glimmer GUI DSL, you get back proxy objects that wrap swing/awt components. To access the original component wrapped by the proxy object, you may call the #original method.

Furthermore, you may invoke any method available on the component on the proxy object, like the #show method on JFrame.

frame1 = jframe('Hello, World!') {
  # ...
}
frame1.show

Despite #show being deprecated in the Java API, it is recommended to use #show instead of visible= in the Glimmer GUI DSL because it has less awkward syntax (it calls visible= behind the scenes to avoid the deprecated API). #show also invokes pack automatically on first run, utilizing SwingUtilities.invokeLater.

6 - Observe Model Attributes

In Smalltalk-MVC (Model View Controller Architectural Pattern), the View is an active View that observes the Model for changes and updates itself.

MVC

This can be achieved with the Glimmer GUI DSL using the observe keyword, which takes a model (any object, including self) and attribute Symbol or String expression (e.g. :count or 'address.street').

The model is automatically enhanced as an Glimmer::DataBinding::ObservableModel / Glimmer::DataBinding::ObservableHash / Glimmer::DataBinding::ObservableArray depending on its type to support notifying observers of attribute changes (when performed using the attribute writer, which automatically calls added method notify_observers(attribute))

Note that it is usually recommended to observe external model objects (not self), but self is OK in very simple cases or presentation-related attributes only.

Example:

require 'glimmer-dsl-swing'

class Counter
  attr_accessor :count

  def initialize
    self.count = 0
  end
end

class HelloButton
  include Glimmer
  
  def initialize
    @counter = Counter.new

    observe(@counter, :count) do |new_count|
      @button.text = "Click To Increment: #{new_count}"
    end
  end
  
  def launch
    jframe('Hello, Button!') {
      @button = jbutton('Click To Increment: 0') {
        on_action_performed do
          @counter.count += 1
        end
      }
    }.show
  end
end

HelloButton.new.launch

screenshots/glimmer-dsl-swing-mac-hello-button.png

Shape DSL

Glimmer DSL for Swing might be the only Ruby Swing DSL out there that supports an additional Shape DSL.

This enables declarative painting of arbitrary shapes using Java 2D, which is similar to how SVG works on the web.

Simply utilize underscored shape names from the java.awt.geom package classes minus the 2D suffix, following the same general rules of the Glimmer GUI DSL.

For example, Arc2D becomes simply arc.

Glimmer utilizes the Double variation of shape classes.

Additionally, you can set these shape properties:

path can have the following operations nested:

Example:

require 'glimmer-dsl-swing'

include Glimmer

jframe('Hello, Shapes!') {
  minimum_size 400, 400
  
  arc(40, 40, 90, 90, 30, 230, 0) {
    fill_paint 255, 0, 0
    draw_paint 0, 128, 255
    stroke 3
  }
  
  arc(40, 140, 90, 90, 30, 230, 1) {
    fill_paint 255, 0, 0
    draw_paint 0, 128, 255
    stroke 3
  }
  
  arc(40, 240, 90, 90, 30, 230, 2) {
    fill_paint 255, 0, 0
    draw_paint 0, 128, 255
    stroke 3
  }
  
  rectangle(140, 40, 180, 90) {
    fill_paint 255, 255, 0
    draw_paint 255, 0, 0
    stroke 3
  }
  
  round_rectangle(140, 140, 180, 90, 60, 40) {
    fill_paint 255, 255, 0
    draw_paint 255, 0, 0
    stroke 3
  }

  ellipse(140, 240, 180, 90) {
    fill_paint 255, 255, 0
    draw_paint 255, 0, 0
    stroke 3
  }
  
  line(180, 60, 280, 110) {
    draw_paint 255, 0, 0
    stroke 3
  }
  
  quad_curve(170, 60, 180, 90, 220, 100) {
    draw_paint 0, 255, 0
    stroke 3
  }
  
  cubic_curve(190, 60, 240, 40, 220, 80, 260, 70) {
    draw_paint 0, 0, 255
    stroke 3
  }
  
  path {
    move_to 200, 150
    line_to 270, 170
    line_to 250, 220
    line_to 220, 190
    line_to 200, 200
    line_to 180, 170
    
    close_path
    
    fill_paint 0, 255, 0
    draw_paint 0, 0, 255
    stroke 3
  }
  
  path {
    move_to 160, 300
    curve_to 190, 260, 200, 280, 210, 270
    curve_to 240, 280, 250, 300, 260, 290
    curve_to 290, 290, 300, 310, 310, 300
    
    fill_paint 0, 255, 0
    draw_paint 0, 0, 255
    stroke 3
  }
}.show

screenshots/glimmer-dsl-swing-mac-hello-shapes.png

Smart Defaults and Conventions

Girb (Glimmer IRB)

You can run the girb command (bin/girb if you cloned the project locally):

girb

This gives you irb with the glimmer-dsl-swing gem loaded and the Glimmer module mixed into the main object for easy experimentation with GUI.

Samples

Hello Samples

Hello, World!

Run with gem installed:

jruby -r glimmer-dsl-swing -e "require 'samples/hello/hello_world'"

Or run from locally cloned project directory:

jruby -r ./lib/glimmer-dsl-swing samples/hello/hello_world.rb

screenshots/glimmer-dsl-swing-mac-hello-world.png

samples/hello/hello_world.rb:

require 'glimmer-dsl-swing'

include Glimmer

jframe('Hello, World!') {
  jlabel('Hello, World!')
}.show

Hello, Button!

Run with gem installed:

jruby -r glimmer-dsl-swing -e "require 'samples/hello/hello_button'"

Or run from locally cloned project directory:

jruby -r ./lib/glimmer-dsl-swing samples/hello/hello_button.rb

screenshots/glimmer-dsl-swing-mac-hello-button.png

Version 1 (without model) - samples/hello/hello_button.rb:

require 'glimmer-dsl-swing'

include Glimmer

jframe('Hello, Button!') {
  @button = jbutton('Click To Increment: 0') {
    on_action_performed do
      button_text_match = @button.text.match(/(.*)(\d+)$/)
      count = button_text_match[2].to_i + 1
      @button.text = "#{button_text_match[1]}#{count}"
    end
  }
}.show

Version 2 (with model) - samples/hello/hello_button2.rb:

require 'glimmer-dsl-swing'

class Counter
  attr_accessor :count

  def initialize
    self.count = 0
  end
end

class HelloButton
  include Glimmer
  
  def initialize
    @counter = Counter.new

    observe(@counter, :count) do |new_count|
      @button.text = "Click To Increment: #{new_count}"
    end
  end
  
  def launch
    jframe('Hello, Button!') {
      @button = jbutton('Click To Increment: 0') {
        on_action_performed do
          @counter.count += 1
        end
      }
    }.show
  end
end

HelloButton.new.launch

Hello, Shapes!

Run with gem installed:

jruby -r glimmer-dsl-swing -e "require 'samples/hello/hello_shapes'"

Or run from locally cloned project directory:

jruby -r ./lib/glimmer-dsl-swing samples/hello/hello_shapes.rb

screenshots/glimmer-dsl-swing-mac-hello-shapes.png

samples/hello/hello_shapes.rb:

require 'glimmer-dsl-swing'

include Glimmer

jframe('Hello, Shapes!') {
  minimum_size 400, 400
  
  arc(40, 40, 90, 90, 30, 230, 0) {
    fill_paint 255, 0, 0
    draw_paint 0, 128, 255
    stroke 3
  }
  
  arc(40, 140, 90, 90, 30, 230, 1) {
    fill_paint 255, 0, 0
    draw_paint 0, 128, 255
    stroke 3
  }
  
  arc(40, 240, 90, 90, 30, 230, 2) {
    fill_paint 255, 0, 0
    draw_paint 0, 128, 255
    stroke 3
  }
  
  rectangle(140, 40, 180, 90) {
    fill_paint 255, 255, 0
    draw_paint 255, 0, 0
    stroke 3
  }
  
  round_rectangle(140, 140, 180, 90, 60, 40) {
    fill_paint 255, 255, 0
    draw_paint 255, 0, 0
    stroke 3
  }

  ellipse(140, 240, 180, 90) {
    fill_paint 255, 255, 0
    draw_paint 255, 0, 0
    stroke 3
  }
  
  line(180, 60, 280, 110) {
    draw_paint 255, 0, 0
    stroke 3
  }
  
  quad_curve(170, 60, 180, 90, 220, 100) {
    draw_paint 0, 255, 0
    stroke 3
  }
  
  cubic_curve(190, 60, 240, 40, 220, 80, 260, 70) {
    draw_paint 0, 0, 255
    stroke 3
  }
  
  path {
    move_to 200, 150
    line_to 270, 170
    line_to 250, 220
    line_to 220, 190
    line_to 200, 200
    line_to 180, 170
    
    close_path
    
    fill_paint 0, 255, 0
    draw_paint 0, 0, 255
    stroke 3
  }
  
  path {
    move_to 160, 300
    curve_to 190, 260, 200, 280, 210, 270
    curve_to 240, 280, 250, 300, 260, 290
    curve_to 290, 290, 300, 310, 310, 300
    
    fill_paint 0, 255, 0
    draw_paint 0, 0, 255
    stroke 3
  }
}.show

Resources

Process

Glimmer Process

Help

Issues

If you encounter issues that are not reported, discover missing features that are not mentioned in TODO.md, or think up better ways to use Swing than what is possible with Glimmer DSL for Swing, you may submit an issue or pull request on GitHub. In the meantime while waiting for a fix, you may try older gem versions of Glimmer DSL for Swing in case you find one that does not have the issue and actually works.

Chat

If you need live help, try to Join the chat at https://gitter.im/AndyObtiva/glimmer

Planned Features and Feature Suggestions

These features have been planned or suggested. You might see them in a future version of Glimmer DSL for Swing. You are welcome to contribute more feature suggestions.

TODO.md

Change Log

CHANGELOG.md

Contributing

Contributors

Click here to view contributor commits.

Copyright

MIT

Copyright (c) 2021 Andy Maleh.

--

<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=40 /> Built for Glimmer (DSL Framework).