Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala SWT project with SBT

Tags:

scala

sbt

swt

How do you create Scala SWT project in SBT?

I know that you can use GIT repositories:

RootProject(uri("http://git.eclipse.org/gitroot/platform/eclipse.platform.swt.binaries.git"))

But I don't know how and if it is possible with SWT.

Thanks in advance, Etam.

EDIT:

I had to download it manually. It compiles but while running I get Invalid thread access error:

***WARNING: Display must be created on main thread due to Cocoa restrictions.
[error] (run-main) org.eclipse.swt.SWTException: Invalid thread access

Even if I use:

javaOptions := Seq("-XstartOnFirstThread", "-d64")

This is the main class:

import org.eclipse.swt._
import org.eclipse.swt.layout._
import org.eclipse.swt.widgets._

object Main extends App {
    val display = new Display
    val shell = new Shell(display)
    shell.setLayout(new GridLayout())
    shell.pack
    shell.open
    while (!shell.isDisposed) {
        if (!display.readAndDispatch)
            display.sleep
    }
    display.dispose
}

Thanks again, Etam.

like image 744
Etam Avatar asked Jan 27 '12 12:01

Etam


1 Answers

Add this to your build.sbt:

resolvers += "swt-repo" at "http://maven-eclipse.github.io/maven"

libraryDependencies += {
  val os = (sys.props("os.name"), sys.props("os.arch")) match {
    case ("Linux", _) => "gtk.linux.x86"
    case ("Mac OS X", "amd64" | "x86_64") => "cocoa.macosx.x86_64"
    case ("Mac OS X", _) => "cocoa.macosx.x86"
    case (os, "amd64") if os.startsWith("Windows") => "win32.win32.x86_64"
    case (os, _) if os.startsWith("Windows") => "win32.win32.x86"
    case (os, arch) => sys.error("Cannot obtain lib for OS '" + os + "' and architecture '" + arch + "'")
  }
  val artifact = "org.eclipse.swt." + os
  "org.eclipse.swt" % artifact % "4.6.1"
}

It will first add a resolver for the SWT artifact repository. It will then detect your OS version and download an appropriate JAR for it.

As for the thread access problem, I solved this on Mac OS X by using JDK 1.6 with it - when I specify -XstartOnFirstThread there, it works fine. I've found no solution for JDK 1.7.

like image 113
axel22 Avatar answered Oct 21 '22 08:10

axel22