//see: http://groovy.codehaus.org/GUI+Programming+with+Groovy
import groovy.swing.*
import java.awt.*
import javax.swing.*
import javax.swing.border.*
import groovy.util.slurpersupport.NodeChild as Node
public class Notify {
def sb = new SwingBuilder()
public static void main(String[] args) {
// creates and displays itself
new Notify(args)
}
public Notify(String[] args){
// arg is path to xml message file,
// will be deleted after popup displayed
if (args.size() != 1) return
String appTitle = ".:. Notification .:."
String filePath = args[0]
Color backgroundColor = Color.yellow
Border emptyBorder = BorderFactory.createMatteBorder(5, 5, 5, 5, backgroundColor)
Toolkit tk = Toolkit.getDefaultToolkit()
Dimension screenSize = tk.getScreenSize()
final int WIDTH = screenSize.width
final int HEIGHT = screenSize.height
int w = WIDTH / 2
int h = HEIGHT / 2
int x = (WIDTH / 2) - (w / 2)
int y = (HEIGHT / 2) - (h / 2) - 50
Font fontSubject = new Font("Serif", Font.PLAIN, 24)
Font fontMessage = new Font("Serif", Font.PLAIN, 16)
//generate frame//
sb.dialog(id:"popupBox", title:appTitle, visible:true, alwaysOnTop:true, defaultCloseOperation:JFrame.DISPOSE_ON_CLOSE, resizable:false, location:[x, y], size:[w, h])
{
borderLayout()
label(id:"subject", constraints:BorderLayout.NORTH, horizontalAlignment:JTextField.CENTER, font:fontSubject, "SUBJ")
panel(constraints:BorderLayout.CENTER){
gridLayout(cols:1, rows:1, hgap:1, vgap:1)
scrollPane(border:emptyBorder) {
textArea(id:"message", editable:false, lineWrap:true, wrapStyleWord:true, font:fontMessage, "MESG")
}
}
}
def frame = sb.popupBox
//decorate frame color//
def cp = frame.contentPane
cp.background = backgroundColor
sb.message.background = cp.background
//populate frame data//
File xmlFile
Node root
try {
xmlFile = new File(filePath)
root = new XmlSlurper().parse(xmlFile)
//println root.getClass().name
}
catch (Exception) {
xmlFile = new File("")
root = new XmlSlurper().parseText('''
Error
Sorry, file path or XML syntax error.
''')
}
sb.subject.text = root.subject.text() // if not found, subject UI label is hidden
sb.message.text = root.message.text() ?
root.message.text().replaceAll('\\\\n', System.getProperty( "line.separator" )) :
'Sorry, empty notification.'
//display form//
frame.show()
try {
xmlFile.delete()
} catch (Exception ignore) {}
}
}
Thursday, February 23, 2012
Swing Notification Popup
Labels:
popup,
swing,
swingbuilder,
ui,
xml
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment