scala/ScalaBook/chapter-06/textEdit.scala
import java.util.regex.{Pattern,Matcher}
import io._
import swing._
import event._
import Swing._
import Dialog._
object myeditor extends SimpleGUIApplication {
var currentFile : java.io.File = null
var backup = ""
var search_text = ""
var p : java.util.regex.Pattern = null
var m : java.util.regex.Matcher = null
def top = new MainFrame {
title = "Rudimentary Editor"
val editor = new TextArea {
font = new java.awt.Font("UM Typewriter", java.awt.Font.PLAIN, 12)
columns = 40
rows = 20
editable = true
}
var transferHandler = new javax.swing.TransferHandler("")
editor.peer.setDragEnabled(true)
menuBar = new MenuBar
val menu_file = new Menu("File")
menu_file.contents += new MenuItem(Action("New") {
check_on_exit(1)
})
val file_IO = new FileChooser(new java.io.File(".")) {
fileSelectionMode = FileChooser.SelectionMode.FilesOnly
}
menu_file.contents += new MenuItem(Action("Open") {
if (file_IO.showOpenDialog(menuBar) ==
FileChooser.Result.Approve) {
currentFile = file_IO.selectedFile
editor.text = new String
for (line <- Source.fromFile(currentFile).getLines)
editor.append(line)
backup = editor.text
}
})
menu_file.contents += new MenuItem(Action("Save") {
if (currentFile == null) {
if (file_IO.showSaveDialog(menuBar) ==
FileChooser.Result.Approve) {
currentFile = file_IO.selectedFile
var out = new java.io.OutputStreamWriter(
new java.io.BufferedOutputStream(
new java.io.FileOutputStream(currentFile)))
backup = editor.text
out.write(backup)
out.flush()
out.close()
}
}
else {
var out = new java.io.OutputStreamWriter(
new java.io.BufferedOutputStream(
new java.io.FileOutputStream(currentFile)))
backup = editor.text
out.write(backup)
out.flush()
out.close()
}
})
menu_file.contents += new MenuItem(Action("Save as") {
if (file_IO.showSaveDialog(menuBar) ==
FileChooser.Result.Approve) {
currentFile = file_IO.selectedFile
var out = new java.io.OutputStreamWriter(
new java.io.BufferedOutputStream(
new java.io.FileOutputStream(currentFile)))
backup = editor.text
out.write(backup)
out.flush()
out.close()
}
})
menu_file.contents += new MenuItem(Action("Print") {
editor.peer.print()
})
//menu_file.contents += new MenuItem("Close")
menu_file.contents += new MenuItem(Action("Exit") {
check_on_exit(0)
})
val menu_edit = new Menu("Edit")
menu_edit.contents += new MenuItem(Action("Find") {
val s = showInput(menuBar,
"Search text for:",
"Search text for a word",
Message.Plain, Swing.EmptyIcon,
Nil, "")
if ((s.isDefined) && (s.get.length > 0)) {
search_text = s.get
p = Pattern.compile(search_text)
m = p.matcher(editor.text)
if (m.find())
editor.caret.dot = m.start()
else showMessage(menuBar, "Pattern not found!",
"String not found",Message.Info, null)
}
})
menu_edit.contents += new MenuItem(Action("Find Next") {
if (m != null && m.find())
editor.caret.dot = m.start()
else
m = null
})
val menu_help = new Menu("Help")
menu_help.contents += new MenuItem("Contents")
menu_help.contents += new MenuItem("About")
menuBar.contents += menu_file
menuBar.contents += menu_edit
menuBar.contents += menu_help
//
contents = new ScrollPane { contents = editor }
//
def check_on_exit(oper : Int) = {
if ( backup != editor.text ) {
var s = showConfirmation(menuBar,
"File is not saved,\n would like to save it?",
"Save File",
Options.YesNoCancel,
Message.Question, null)
if ( s == Result.Yes ) {
if (file_IO.showSaveDialog(menuBar) ==
FileChooser.Result.Approve) {
currentFile = file_IO.selectedFile
var out = new java.io.OutputStreamWriter(
new java.io.BufferedOutputStream(
new java.io.FileOutputStream(currentFile)))
backup = editor.text
out.write(backup)
out.flush()
out.close()
}
cleanUp(oper)
}
else if ( s == Result.No )
cleanUp(oper)
}
else
cleanUp(oper)
}
//
def cleanUp(oper:Int) = {
currentFile = null
if (oper == 0 )
exit(0)
else {
editor.text = ""
backup = ""
}
}
//
}
}