Class: Clevic::Clipboard

Inherits:
Object show all
Defined in:
lib/clevic/qt/clipboard.rb,
lib/clevic/swing/clipboard.rb

Overview

Wrapper for framework-specigfic clipboard code. Used by TableView.

could also use a javax.activation.DataHandler for a more sophisticated API TODO use javaJVMLocalObjectMimeType file:///usr/share/doc/java-sdk-docs-1.6.0.10/html/api/java/awt/datatransfer/DataFlavor.html#javaJVMLocalObjectMimeType also use a DataFlavor with mimetype application/x-java-serialized-object to transfer between cells.

Instance Method Summary collapse

Instance Method Details

#[](mime_type) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/clevic/swing/clipboard.rb', line 126

def []( mime_type )
  contents( mime_type ).unpack('U*').inject([]) do |collect,byte|
    # ignore BOM
    collect << byte unless byte == 65533
    collect
  end.pack( "U*" )
end

#contents(mime_type) ⇒ Object

try a bunch of encodings for the given mime_type, and give back a String containing the result



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/clevic/swing/clipboard.rb', line 98

def contents( mime_type )
  case
    # try UTF-8 first because it seems more robust
    when has?( %r{#{mime_type}.*String.*utf-8}i )
      data "#{mime_type}; class=java.lang.String; charset=UTF-8"

    # now string Unicode, just in case
    when has?( %r{#{mime_type}.*String.*unicode}i )
      data "#{mime_type}; class=java.lang.String; charset=unicode"

    # This is to handle clevic-clevic pastes
    when has?( %r{#{mime_type}.*Stream.*unicode}i )
      stream( "#{mime_type}; class=java.io.InputStream; charset=unicode" ).read

    else
      raise "Don't know how to get #{mime_type}"
  end
end

#data(full_mime_type) ⇒ Object



117
118
119
120
# File 'lib/clevic/swing/clipboard.rb', line 117

def data( full_mime_type )
  flavor = java.awt.datatransfer.DataFlavor.new( full_mime_type )
  system.getData( flavor )
end

#flavoursObject



61
62
63
# File 'lib/clevic/swing/clipboard.rb', line 61

def flavours
  system.available_data_flavors.to_a
end

#full_mime_typesObject



86
87
88
# File 'lib/clevic/swing/clipboard.rb', line 86

def full_mime_types
  flavours.map( &:mime_type )
end

#has?(matcher) ⇒ Boolean

matcher is either a string or a regex, ie something that can be passed to Array#grep

Returns:

  • (Boolean)


92
93
94
# File 'lib/clevic/swing/clipboard.rb', line 92

def has?( matcher )
  !full_mime_types.grep( matcher ).empty?
end

#htmlObject

TODO figure out why Qt never has anything other than text. Could be because the event loop isn’t running when testing from irb.



30
31
32
# File 'lib/clevic/qt/clipboard.rb', line 30

def html
  system.mime_data.html
end

#html?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/clevic/qt/clipboard.rb', line 23

def html?
  system.mime_data.has_html
end

#mime_typesObject



82
83
84
# File 'lib/clevic/swing/clipboard.rb', line 82

def mime_types
  flavours.map( &:simple_type ).sort.uniq
end

#stream(full_mime_type) ⇒ Object



122
123
124
# File 'lib/clevic/swing/clipboard.rb', line 122

def stream( full_mime_type )
  Stream.new( data( full_mime_type ) )
end

#systemObject



7
8
9
# File 'lib/clevic/qt/clipboard.rb', line 7

def system
  Qt::Application::clipboard
end

#textObject



15
16
17
# File 'lib/clevic/qt/clipboard.rb', line 15

def text
  system.text
end

#text=(value) ⇒ Object



11
12
13
# File 'lib/clevic/qt/clipboard.rb', line 11

def text=( value )
  system.text = value
end

#text?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/clevic/qt/clipboard.rb', line 19

def text?
  system.mime_data.has_text
end