ScalaでSwing ファイルを開く・保存する
import scala.swing._ import java.io.{ File, IOException } import javax.swing.filechooser.FileNameExtensionFilter import org.apache.commons.lang.SystemUtils import org.apache.commons.io.FileUtils
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") } }
ファイルを開く
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 => ; }
ファイルを保存する
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 => ; }
ScalaでSwing メニュー
- MenuBar
- Menu
- MenuItem
- Action (abstract class)
- Action (object)
- CheckMenuItem
- ButtonGroup
- RadioMenuItem
- Menu
- Separator
- MenuItem
- Menu
import
import scala.swing._ import scala.swing.event.WindowClosing import java.awt.event.{ KeyEvent, InputEvent } import javax.swing.{ KeyStroke, ImageIcon }
Menu
val menuFile = new Menu("メニュー(M)") //TODO menuFile.mnemonic = Key.M
MenuItem
val menuOpen = new MenuItem(new Action("開く(O)") { mnemonic = KeyEvent.VK_O accelerator = Some(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK)) icon = new ImageIcon("build/icon.png") toolTip = "ファイルを開く" def apply() {} }) val menuSave = new MenuItem(new Action("保存(S)") { mnemonic = KeyEvent.VK_S accelerator = Some(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK)) enabled = false def apply() {} }) val menuExit = new MenuItem(new Action("終了(X)") { mnemonic = KeyEvent.VK_X def apply() { top.publish(new WindowClosing(top)) } })
CheckMenuItem
val menuLine = new CheckMenuItem("行番号を表示")
ButtonGroup, RadioMenuItem
val menuLang = new Menu("言語(L)") val menuLangScala = new RadioMenuItem("Scala") val menuLangRuby = new RadioMenuItem("Ruby") val mutexLang = new ButtonGroup(menuLangScala, menuLangRuby) menuLang.contents ++= mutexLang.buttons
menuFile.contents += menuOpen menuFile.contents += menuSave menuFile.contents += menuLine menuFile.contents += menuLang menuFile.contents += new Separator menuFile.contents += menuExit menuBar = new MenuBar { contents += menuFile }
順序付きのHash OrderedHash
$ script/console Loading development environment (Rails 2.3.2) >> RUBY_VERSION => "1.8.7" >> ActiveSupport::OrderedHash.new(:a => 1, :b => 2) => #<OrderedHash {}> >> ActiveSupport::OrderedHash.new([[:a, 1], [:b, 2]]) => #<OrderedHash {}> >> hash = ActiveSupport::OrderedHash.new => #<OrderedHash {}> >> hash[:a] = 1 => 1 >> hash[:b] = 2 => 2 >> hash => #<OrderedHash {:a=>1, :b=>2}>
Rails と Ruby の Ordered Hash について
HashとActiveSupport::OrderedHashのmergeの挙動が異なる
Rails 2.3 の ActiveSupport::OrderedHash がなんか変
@reminders.map(&:id)
app/controllers/reminders_controller.rb from authorNari's brushup - GitHub
@reminders.map(&:id)
動作 | ||
ruby 1.8.7 | irb | ○ |
ruby 1.8.6でRails 2.3.2 | script/console | ○ |
ruby 1.8.6 | irb | TypeError: wrong argument type Symbol (expected Proc) |
別の書き方では
@reminders.map {|reminder| reminder.id}
Authlogic 2.0.11
Authlogic Example App - Tutorial on how to create this app and easily setup Authlogic
Ruby | ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32] |
Ruby on Rails | 2.3.2 |
Authlogic | 2.0.11 |
Firefox3で不要なメニューを消すuserChrome.css
消したいメニューのidの見つけ方
Firefox | 3.0.8 |
DOM Inspector | 2.0.3 |
- DOM Inspector のインストール
- DOM Inspector の起動
- ツール → DOM Inspector
- アドレス「chrome://browser/content/browser.xul」を入力
- 検索
- Search → Find Nodes → Attr をチェック
- Attrに「label」、Valueに見つけたいメニューの表示名(「すべて選択」など)を入力
- 最初の検索は失敗するので、もう一度検索する
id | |
メニューバー | main-menubar |
ナビゲーションツールバー | nav-bar |
タブのコンテキストメニュー | context_newTab 周辺 |
HTML部分のコンテキストメニュー | contentAreaContextMenu |