Sha256: 497aa03b5c8036d1054bc4a9334ee24a737aac3538c120f82c9a7cabead581dd

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

class Shoes
  module Swt
    # In Swt radio groups are managed by composites which occupy space
    # and can interfere with users interacting with other controls. Here
    # we simulate radio groups so that they can all be in one composite.
    class RadioGroup
      extend Forwardable
      include Enumerable

      DEFAULT_RADIO_GROUP = "Default Radio Group".freeze

      @group_lookup = Hash.new { |h, k| h[k] = RadioGroup.new(k) }

      attr_reader :name
      def_delegators :@radio_buttons, :each, :length, :empty?

      def initialize(name = DEFAULT_RADIO_GROUP)
        @name = name
        @radio_buttons = []
        @selection_listeners = []
      end

      def add(radio_button)
        return if @radio_buttons.include?(radio_button)
        @radio_buttons << radio_button

        selection_listener = SelectionListener.new radio_button do |selected_radio, _event|
          select_only_one_radio_in_group(selected_radio)
        end
        @selection_listeners << selection_listener
        radio_button.real.add_selection_listener selection_listener
      end

      def remove(radio_button)
        index = @radio_buttons.index(radio_button)
        return if index.nil?
        @radio_buttons.delete_at(index)

        radio_button.real.remove_selection_listener @selection_listeners[index]
        @selection_listeners.delete_at(index)
      end

      class << self
        attr_reader :group_lookup
      end

      def select_only_one_radio_in_group(selected_radio)
        @radio_buttons.each do |radio|
          radio.real.set_selection(radio == selected_radio)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shoes-swt-4.0.0.pre8 lib/shoes/swt/radio_group.rb
shoes-swt-4.0.0.pre7 lib/shoes/swt/radio_group.rb