Class: Clevic::ConfirmDialog

Inherits:
Object
  • Object
show all
Defined in:
lib/clevic/swing/confirm_dialog.rb

Overview

keeps a list of names => results mappings and returns

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ ConfirmDialog

Returns a new instance of ConfirmDialog.

Yields:

  • (_self)

Yield Parameters:



5
6
7
8
9
# File 'lib/clevic/swing/confirm_dialog.rb', line 5

def initialize
  @names = []
  @results = []
  yield self if block_given?
end

Instance Attribute Details

#dialog_resultObject

Returns the value of attribute dialog_result.



11
12
13
# File 'lib/clevic/swing/confirm_dialog.rb', line 11

def dialog_result
  @dialog_result
end

#namesObject

Returns the value of attribute names.



11
12
13
# File 'lib/clevic/swing/confirm_dialog.rb', line 11

def names
  @names
end

#parentObject

Returns the value of attribute parent.



11
12
13
# File 'lib/clevic/swing/confirm_dialog.rb', line 11

def parent
  @parent
end

#questionObject

Returns the value of attribute question.



11
12
13
# File 'lib/clevic/swing/confirm_dialog.rb', line 11

def question
  @question
end

#resultsObject

Returns the value of attribute results.



11
12
13
# File 'lib/clevic/swing/confirm_dialog.rb', line 11

def results
  @results
end

#titleObject

Returns the value of attribute title.



11
12
13
# File 'lib/clevic/swing/confirm_dialog.rb', line 11

def title
  @title
end

Instance Method Details

#[]=(name, *args) ⇒ Object

To create a an Ok button that has the focus, and causes the class to return true from accepted?

dialog['Ok'] = :accept, true

To create a Cancel button that returns true from rejected?

dialog['Cancel'] = :reject


27
28
29
30
31
32
33
# File 'lib/clevic/swing/confirm_dialog.rb', line 27

def []=( name, *args )
  result, default = *args.flatten
  raise "Result is not in #{@canonical_results.inspect}" unless canonical_results.include?( result.to_sym )
  names << name.to_s
  results << result.to_sym
  @default = name.to_s if default
end

#accepted?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/clevic/swing/confirm_dialog.rb', line 35

def accepted?
  results[dialog_result] == :accept
end

#canonical_resultsObject



17
18
19
# File 'lib/clevic/swing/confirm_dialog.rb', line 17

def canonical_results
  @canonical_results ||= [:accept, :reject]
end

#rejected?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/clevic/swing/confirm_dialog.rb', line 39

def rejected?
  results[dialog_result] == :reject
end

#showObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/clevic/swing/confirm_dialog.rb', line 43

def show
  self.dialog_result = javax.swing.JOptionPane.showOptionDialog(
    parent,
    question,
    title,
    javax.swing.JOptionPane::DEFAULT_OPTION,
    javax.swing.JOptionPane::QUESTION_MESSAGE,
    nil, # icon. Not used here
    names.to_java( :object ),
    @default
  )
  self
end

#to_java(arg) ⇒ Object



13
14
15
# File 'lib/clevic/swing/confirm_dialog.rb', line 13

def to_java( arg )
  @options.keys.to_java( arg )
end