challenge クリップボードへの転送

クリップボード(や同等の機能)へテキストを転送するプログラムをお願いします。 また可能でしたらクリップボードのデータを取り出すプログラムもお願いします。

システムに依存する内容ですが、応用範囲が広いと思いましたので出題させてもらいました。

Posted feedbacks - Scala

Javaと同じ処理になりますが...。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import    java.awt.BorderLayout
import    java.awt.Container
import    java.awt.Dimension
import    java.awt.FlowLayout
import    java.awt.Toolkit
import    java.awt.datatransfer.Clipboard
import    java.awt.datatransfer.DataFlavor
import    java.awt.datatransfer.StringSelection
import    java.awt.event.ActionEvent
import    java.awt.event.ActionListener
import    javax.swing.JButton
import    javax.swing.JFrame
import    javax.swing.JMenu
import    javax.swing.JMenuBar
import    javax.swing.JMenuItem
import    javax.swing.JPanel
import    javax.swing.JTextArea

class SButton(l:String,c:ActionEvent=>Unit) extends JButton with ActionListener {
    
    setText(l)
    addActionListener(this)
    
    override def actionPerformed(e:ActionEvent) = c(e)
}

class SMenuItem(l:String,c:ActionEvent=>Unit) extends JMenuItem with ActionListener {
    
    setText(l)
    addActionListener(this)
    
    override def actionPerformed(e:ActionEvent) = c(e)
}

class ClipboardFrame(title:String) extends JFrame {
    
    val    mb:JMenuBar = new JMenuBar
    val    fm:JMenu = new JMenu("file")
    val    em:JMenu = new JMenu("edit")
    val    c:Container = getContentPane
    val    t:JTextArea = new JTextArea(20,40);
    val    p:JPanel = new JPanel;
    val    clipboard:Clipboard = Toolkit.getDefaultToolkit.getSystemClipboard;
    
    c.setLayout(new BorderLayout)
    c.add(t,BorderLayout.CENTER)
    
    p.setLayout(new FlowLayout)
    p.add(new SButton("copy",copy))
    p.add(new SButton("paste",paste))
    c.add(p,BorderLayout.SOUTH)
    
    fm.add(new SMenuItem("quit",quit))
    mb.add(fm)
    em.add(new SMenuItem("copy",copy))
    em.add(new SMenuItem("paste",paste))
    mb.add(em)
    setJMenuBar(mb)
    
    setResizable(false)
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
    setTitle(title)
    
    pack
    
    def this() = this("clipboad")
    
    def quit(e:ActionEvent):Unit = {
        System.exit(0)
    }
    
    def copy(e:ActionEvent):Unit = {
        val    s:StringSelection = new StringSelection(t.getSelectedText);
        clipboard.setContents(s,s);
    }
    
    def paste(e:ActionEvent):Unit = {
        t.replaceSelection(clipboard.getData(DataFlavor.stringFlavor).asInstanceOf[String])
    }
}

object Main extends Application {
    (new ClipboardFrame).setVisible(true)
}

scala.swingで書いてみました。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import swing._
import swing.event._
import java.awt.Toolkit
import java.awt.datatransfer.Clipboard
import java.awt.datatransfer.DataFlavor
import java.awt.datatransfer.StringSelection

object Main extends SimpleGUIApplication {
  val t  = new TextArea(20,40)
  val clipboard = Toolkit.getDefaultToolkit.getSystemClipboard;
  
  def top=new MainFrame{
      title = "Clipboard"
      val menu = new MenuBar{
            contents += new Menu("file"){
               contents += new MenuItem( Action("quit"){System.exit(0)} )
            }// end of file-menu
          contents += new Menu("edit"){
               contents += new MenuItem( Action("copy" ){copy})
               contents += new MenuItem( Action("paste"){paste})
            }// end of edit-menu
          }// end of menu
       menuBar = menu
       // add components 
      contents = new BorderPanel{
        add(t,BorderPanel.Position.Center)
        add(new FlowPanel{
              contents += new Button(Action("copy"){copy})
              contents += new Button(Action("paste"){paste})
           },BorderPanel.Position.South)
      } // end of contents 
      
    } // end of ctor of MainFrame
  
  def copy():Unit = {
    val s = new StringSelection(t.peer.getSelectedText);
    clipboard.setContents(s,s);
  }
  
  def paste():Unit = {
    t.peer.replaceSelection(clipboard.getData(DataFlavor.stringFlavor).asInstanceOf[String])
  }
  
}

コマンド引数 inで標準入力をクリップボードに転送
out でクリップボードの内容を標準出力に出力。
(クリップボードアクセスはswing版と同じ)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import scala.io.Source

import java.awt.Toolkit
import java.awt.datatransfer.Clipboard
import java.awt.datatransfer.DataFlavor
import java.awt.datatransfer.StringSelection

object Main {
  def main(args : Array[String]) : Unit = {
    try{
      val clipboard = Toolkit.getDefaultToolkit.getSystemClipboard;
      args(0) match {
        case "out" =>{
            print( clipboard.getData(DataFlavor.stringFlavor).asInstanceOf[String] )
          }
        case "in" =>{
            val s = new StringSelection( Source.fromInputStream(System.in).getLines.mkString )
            clipboard.setContents(s,s)
          }
          
      }
      
    }catch{
      case _ =>
    }
    ()
  }
}

Index

Feed

Other

Link

Pathtraq

loading...