11/*
22 * Image/J Plugins
3- * Copyright (C) 2002-2012 Jarek Sacha
3+ * Copyright (C) 2002-2014 Jarek Sacha
44 * Author's email: jsacha at users dot sourceforge dot net
55 *
66 * This library is free software; you can redistribute it and/or
2222
2323package net .sf .ij_plugins .scala .console .editor
2424
25- import net .sf .ij_plugins .scala .console ._
2625import java .io .File
2726import java .util .prefs .Preferences
2827import javax .swing .filechooser .FileNameExtensionFilter
28+ import net .sf .ij_plugins .scala .console ._
2929import swing .{FileChooser , Action , Dialog , Component }
3030
3131/**
@@ -36,146 +36,144 @@ import swing.{FileChooser, Action, Dialog, Component}
3636private class EditorController (private val parentView : Component ,
3737 private val model : EditorModel ) {
3838
39- private val defaultExtension = " scala"
39+ private val defaultExtension = " scala"
4040
41- private lazy val fileChooser = new FileChooser (currentDirectory) {
42- fileFilter = new FileNameExtensionFilter (" *." + defaultExtension, defaultExtension)
43- multiSelectionEnabled = false
44- }
41+ private lazy val fileChooser = new FileChooser (currentDirectory) {
42+ fileFilter = new FileNameExtensionFilter (" *." + defaultExtension, defaultExtension)
43+ multiSelectionEnabled = false
44+ }
4545
4646
47- private val fileNewAction = new Action (" New..." ) {
47+ private val fileNewAction = new Action (" New..." ) {
4848
49- icon = loadIcon(this .getClass, " .. /resources/icons/page.png" )
49+ icon = loadIcon(this .getClass, " /net/sf/ij_plugins/scala/console /resources/icons/page.png" )
5050
51- def apply () {
51+ def apply () {
5252
53- askAndSave()
53+ askAndSave()
5454
55- // Reset editor
56- model.reset()
57- }
55+ // Reset editor
56+ model.reset()
5857 }
58+ }
5959
60- private val fileOpenAction = new Action (" Open..." ) {
61- icon = loadIcon(this .getClass, " .. /resources/icons/folder_page.png" )
60+ private val fileOpenAction = new Action (" Open..." ) {
61+ icon = loadIcon(this .getClass, " /net/sf/ij_plugins/scala/console /resources/icons/folder_page.png" )
6262
63- def apply () {
63+ def apply () {
6464
65- val status = fileChooser.showOpenDialog(parentView)
66- if (status != FileChooser .Result .Approve ) {
67- return
68- }
65+ val status = fileChooser.showOpenDialog(parentView)
66+ if (status != FileChooser .Result .Approve ) {
67+ return
68+ }
6969
70- val file = fileChooser.selectedFile
71- if (file == null ) {
72- return
73- }
70+ val file = fileChooser.selectedFile
71+ if (file == null ) {
72+ return
73+ }
7474
75- currentDirectory = file.getParentFile
76- read(file)
77- }
75+ currentDirectory = file.getParentFile
76+ read(file)
7877 }
78+ }
7979
80- private val fileSaveAction = new Action (" Save" ) {
81- icon = loadIcon(this .getClass, " .. /resources/icons/disk.png" )
80+ private val fileSaveAction = new Action (" Save" ) {
81+ icon = loadIcon(this .getClass, " /net/sf/ij_plugins/scala/console /resources/icons/disk.png" )
8282
83- def apply () {
84- save()
85- }
83+ def apply () {
84+ save()
8685 }
86+ }
8787
88- private val fileSaveAsAction = Action (" Save As..." ) {
89- saveAs()
90- }
91-
92- def fileActions = Array (fileNewAction, fileOpenAction, fileSaveAction, fileSaveAsAction)
88+ private val fileSaveAsAction = Action (" Save As..." ) {
89+ saveAs()
90+ }
9391
94- def read (file : File ) {
95- model.read(file)
96- }
92+ def fileActions = Array (fileNewAction, fileOpenAction, fileSaveAction, fileSaveAsAction)
9793
98- private def save () {
99- model.sourceFile match {
100- case Some (file) => {
101- model.save(file)
102- }
103- case None => saveAs()
104- }
94+ def read (file : File ) {
95+ model.read(file)
96+ }
10597
98+ private def save () {
99+ model.sourceFile match {
100+ case Some (file) => model.save(file)
101+ case None => saveAs()
106102 }
107103
108- private def saveAs (): Boolean = {
109- val status = fileChooser.showSaveDialog(parentView)
110- if (status != FileChooser .Result .Approve ) {
111- return false
112- }
104+ }
113105
114- val file = fileChooser.selectedFile
115- if (file == null ) {
116- return false
117- }
118-
119- currentDirectory = file.getParentFile
120- model.save(ensureExtension(file, defaultExtension))
121-
122- true
123- }
124-
125- private def ensureExtension (file : File , extension : String ): File = {
126- if (file.getName.toLowerCase.endsWith(" ." + extension))
127- file
128- else
129- new File (file.getPath + " ." + extension)
106+ private def saveAs (): Boolean = {
107+ val status = fileChooser.showSaveDialog(parentView)
108+ if (status != FileChooser .Result .Approve ) {
109+ return false
130110 }
131111
132- /**
133- * Return current directory saved in preferences, if cannot be retrieved return `null`.
134- * JFileChooser constructor is using `null` to indicate that starting directory is as user's default directory.
135- */
136- private def currentDirectory : File = {
137- try {
138- val prefNode = Preferences .userRoot.node(this .getClass.getName)
139- val currentDirectory = prefNode.get(" fileChooser.currentDirectory" , null )
140- if (currentDirectory == null )
141- null
142- else
143- new File (currentDirectory)
144- } catch {
145- case _ : Throwable => null
146- }
112+ val file = fileChooser.selectedFile
113+ if (file == null ) {
114+ return false
147115 }
148116
149- private def currentDirectory_= (dir : File ) {
150- try {
151- val prefNode = Preferences .userRoot.node(this .getClass.getName)
152- prefNode.put(" fileChooser.currentDirectory" , dir.getCanonicalPath)
153- } catch {
154- case _ : Throwable => {}
155- }
117+ currentDirectory = file.getParentFile
118+ model.save(ensureExtension(file, defaultExtension))
119+
120+ true
121+ }
122+
123+ private def ensureExtension (file : File , extension : String ): File = {
124+ if (file.getName.toLowerCase.endsWith(" ." + extension))
125+ file
126+ else
127+ new File (file.getPath + " ." + extension)
128+ }
129+
130+ /**
131+ * Return current directory saved in preferences, if cannot be retrieved return `null`.
132+ * JFileChooser constructor is using `null` to indicate that starting directory is as user's default directory.
133+ */
134+ private def currentDirectory : File = {
135+ try {
136+ val prefNode = Preferences .userRoot.node(this .getClass.getName)
137+ val currentDirectory = prefNode.get(" fileChooser.currentDirectory" , null )
138+ if (currentDirectory == null )
139+ null
140+ else
141+ new File (currentDirectory)
142+ } catch {
143+ case _ : Throwable => null
156144 }
157-
158- /**
159- * Perform operations needed to safely close the editor, save files, etc.
160- */
161- def prepareToClose () {
162- askAndSave()
145+ }
146+
147+ private def currentDirectory_= (dir : File ) {
148+ try {
149+ val prefNode = Preferences .userRoot.node(this .getClass.getName)
150+ prefNode.put(" fileChooser.currentDirectory" , dir.getCanonicalPath)
151+ } catch {
152+ case _ : Throwable =>
163153 }
164-
165- private def askAndSave () {
166- if (model.needsSave) {
167- // Check if current document needs to be saved
168- val status = Dialog .showConfirmation(
169- parentView,
170- " Do you want to save current script?" ,
171- " Editor Content Modified" ,
172- Dialog .Options .YesNo )
173- // If not cancelled, save
174- status match {
175- case Dialog .Result .Yes => save()
176- case Dialog .Result .No => {}
177- case _ => return
178- }
179- }
154+ }
155+
156+ /**
157+ * Perform operations needed to safely close the editor, save files, etc.
158+ */
159+ def prepareToClose () {
160+ askAndSave()
161+ }
162+
163+ private def askAndSave () {
164+ if (model.needsSave) {
165+ // Check if current document needs to be saved
166+ val status = Dialog .showConfirmation(
167+ parentView,
168+ " Do you want to save current script?" ,
169+ " Editor Content Modified" ,
170+ Dialog .Options .YesNo )
171+ // If not cancelled, save
172+ status match {
173+ case Dialog .Result .Yes => save()
174+ case Dialog .Result .No =>
175+ case _ => return
176+ }
180177 }
178+ }
181179}
0 commit comments