Skip to content

Instantly share code, notes, and snippets.

@hedefalk
Created February 24, 2011 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hedefalk/842960 to your computer and use it in GitHub Desktop.
Save hedefalk/842960 to your computer and use it in GitHub Desktop.
object Temperature {
def main(args: Array[String]): Unit = {
val display = Display.getDefault
val window = new Shell(display)
window.setMinimumSize(400, 150)
window.pack
window.setLayout(new GridLayout());
new Temperature(window, SWT.NONE)
window.open
while (!window.isDisposed) {
if (!display.readAndDispatch) display.sleep
}
}
class Temperature(parent: Composite, style: Int) extends Composite(parent, style) {
def applyLayoutData(c: Control) = GridDataFactory.defaultsFor(c).applyTo(c)
def parseDouble(s: String) = java.lang.Double.parseDouble(s)
setLayout(new GridLayout(2, true))
new Label(this, SWT.NULL).setText("Fahrenheit")
new Label(this, SWT.NULL).setText("Celcius")
val fahrenheit = new Text(this, SWT.BORDER)
applyLayoutData(fahrenheit)
val celcius = new Text(this, SWT.BORDER)
applyLayoutData(celcius)
val fahrenheitToCelcius = new Button(this, SWT.PUSH)
fahrenheitToCelcius.setText("Fareinheight -> Celcius")
fahrenheitToCelcius.addSelectionListener(new SelectionAdapter() {
override def widgetSelected(e: SelectionEvent) = {
val fDouble = parseDouble(fahrenheit.getText())
val cDouble = (5.0 / 9.0) * (fDouble - 32)
celcius.setText(cDouble.toString)
}
})
applyLayoutData(fahrenheitToCelcius)
val celciusToFahrenheit = new Button(this, SWT.PUSH)
celciusToFahrenheit.setText("Celcius -> Fareinheight")
celciusToFahrenheit.addSelectionListener(new SelectionAdapter() {
override def widgetSelected(e: SelectionEvent) = {
val cDouble = parseDouble(celcius.getText())
val fDouble = (9.0 / 5.0) * cDouble + 32
fahrenheit.setText(fDouble.toString)
}
})
applyLayoutData(celciusToFahrenheit)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment