All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.ij_plugins.scala.console.ScalaConsolePlugin.scala Maven / Gradle / Ivy

Go to download

Simple user interface for executing Scala scripts. Can be run stand-alone or embedded in a desktop application.

There is a newer version: 1.5.1
Show newest version
/*
 *  ImageJ Plugins
 *  Copyright (C) 2002-2016 Jarek Sacha
 *  Author's email: jpsacha at gmail dot com
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *   Latest release available at https://github.com/ij-plugins
 */

package net.sf.ij_plugins.scala.console

import java.io.File

import ij.IJ
import ij.plugin.PlugIn
import net.sf.ij_plugins.scala.console
import org.scalafx.extras._

import scalafx.Includes._
import scalafx.scene.Scene
import scalafx.scene.image.Image
import scalafx.stage.Stage


private object ScalaConsolePlugin {
  console.addPluginsJarsToClassPath()
}


/**
  * ImageJ plugin for starting Scala Console.
  */
class ScalaConsolePlugin extends PlugIn {

  def run(arg: String): Unit = {
    IJ.showStatus("Starting Scala Console...")

    initFX()

    val scripFile = if (arg != null && !arg.isEmpty) {
      val file = new File(arg.trim)
      if (file.exists) Some(file) else None
    } else {
      None
    }

    onFX {
      try {
        val iconImages = {
          val names = Array("scala16.png", "scala32.png", "scala48.png", "scala64.png")
          val path = "/net/sf/ij_plugins/scala/console/resources/"
          names.map { n => new Image(s"$path$n").delegate }
        }

        val scalaConsole = new ScalaConsolePane()
        new Stage {
          scene = new Scene(640, 480) {
            title = "Scala Console"
            root = scalaConsole.view
          }
          icons ++= iconImages
        }.show()

        scripFile.foreach { file => scalaConsole.loadScriptFile(file) }
      } catch {
        case ex: Throwable =>
          ex.printStackTrace()
          IJ.error("Scala Console Plugin", "Failed to start Scala Console. " + ex.getMessage)
      } finally {
        IJ.showStatus("")
      }
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy