Created
October 27, 2009 01:59
-
-
Save mpen/219238 to your computer and use it in GitHub Desktop.
ScalaでSwing ファイルを開く・保存
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ScalaでSwing ファイルを開く・保存 | |
| import scala.swing._ | |
| import scala.swing.event.ButtonClicked | |
| import java.io.{ File, IOException } | |
| import java.awt.Font | |
| import javax.swing.{ UIManager, SwingUtilities } | |
| import javax.swing.filechooser.FileNameExtensionFilter | |
| import org.apache.commons.lang.SystemUtils | |
| import org.apache.commons.io.FileUtils | |
| object ScalaSwingFile extends SimpleGUIApplication { | |
| val monoFont = new Font("monospaced", Font.PLAIN, 14) | |
| var currentFile: File = null | |
| val mainPanel = new BoxPanel(Orientation.Vertical) { | |
| val buttonPanel = new BoxPanel(Orientation.Horizontal) { | |
| val openButton = new Button { text = "開く" } | |
| val saveButton = new Button { text = "上書き保存" } | |
| val saveAsButton = new Button { text = "名前を付けて保存" } | |
| contents += openButton | |
| contents += saveButton | |
| contents += saveAsButton | |
| } | |
| val editScroll = new ScrollPane { | |
| preferredSize = (600, 400) | |
| val editArea = new TextArea { font = monoFont } | |
| contents = editArea | |
| } | |
| contents += buttonPanel | |
| contents += editScroll | |
| listenTo(buttonPanel.openButton) | |
| listenTo(buttonPanel.saveButton) | |
| listenTo(buttonPanel.saveAsButton) | |
| def getFileChooser = { | |
| val dir = if (currentFile == null) SystemUtils.getUserDir else currentFile | |
| new FileChooser(dir) { | |
| fileFilter = new FileNameExtensionFilter("テキストファイル", "txt") | |
| fileFilter = new FileNameExtensionFilter("Scala / Ruby", "scala", "rb") | |
| //TODO FileChooser#accessory= / JFileChooser#setAccessory | |
| } | |
| } | |
| def openFile: Unit = { | |
| val fc = getFileChooser | |
| fc.title = "ファイルを開く" | |
| fc.showOpenDialog(this) match { | |
| case FileChooser.Result.Approve => | |
| try { | |
| if (fc.selectedFile.exists) { | |
| editScroll.editArea.text = FileUtils.readFileToString(fc.selectedFile) | |
| currentFile = fc.selectedFile | |
| } else { | |
| editScroll.editArea.text = "" | |
| currentFile = fc.selectedFile | |
| } | |
| } catch { | |
| case e: IOException => | |
| Dialog.showMessage(this, "ファイルを開けませんでした。\n" + e.getMessage) | |
| } | |
| case FileChooser.Result.Cancel => ; | |
| case FileChooser.Result.Error => ; | |
| } | |
| } | |
| def saveFile(f: File): Unit = { | |
| try { | |
| FileUtils.writeStringToFile(f, editScroll.editArea.text) | |
| } catch { | |
| case e: IOException => | |
| Dialog.showMessage(this, "ファイルを保存できませんでした。\n" + e.getMessage) | |
| } | |
| } | |
| def saveAsFile: Unit = { | |
| val fc = getFileChooser | |
| fc.title = "名前を付けて保存" | |
| fc.showSaveDialog(this) match { | |
| case FileChooser.Result.Approve => | |
| val sf = fc.selectedFile | |
| val confResult = if (sf.exists) { | |
| Dialog.showConfirmation(this, | |
| sf.getName + " は既に存在します。\n上書きしますか?", | |
| "名前を付けて保存の確認", Dialog.Options.YesNo) | |
| } else { | |
| Dialog.Result.Yes | |
| } | |
| if (confResult == Dialog.Result.Yes) { | |
| try { | |
| FileUtils.writeStringToFile(sf, editScroll.editArea.text) | |
| currentFile = sf | |
| } catch { | |
| case e: IOException => | |
| Dialog.showMessage(this, "ファイルを保存できませんでした。\n" + e.getMessage) | |
| } | |
| } | |
| case FileChooser.Result.Cancel => ; | |
| case FileChooser.Result.Error => ; | |
| } | |
| } | |
| reactions += { | |
| case ButtonClicked(buttonPanel.openButton) => openFile | |
| case ButtonClicked(buttonPanel.saveButton) => | |
| currentFile match { | |
| case f: File => saveFile(f) | |
| case null => saveAsFile; | |
| } | |
| case ButtonClicked(buttonPanel.saveAsButton) => saveAsFile | |
| } | |
| } | |
| def top = new MainFrame { | |
| UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()) | |
| SwingUtilities.updateComponentTreeUI(mainPanel.peer) | |
| title = "ScalaでSwing ファイルを開く・保存" | |
| contents = mainPanel | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment