1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
|
/*
* Copyright 2007-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*/
import java.awt.Color
import java.awt.Font
import java.awt.Robot
import java.awt.FlowLayout
import java.awt.event.ActionEvent
import javax.swing.JComponent
import javax.swing.JFileChooser
import javax.swing.JOptionPane
import javax.swing.SwingConstants
import static javax.swing.JSplitPane.*
import javax.imageio.ImageIO
import java.util.prefs.Preferences
import groovy.ui.text.FindReplaceUtility
import org.codehaus.groovy.runtime.StackTraceUtils
class SwingPadController {
def model
def view
def builder
private prefs = Preferences.userNodeForPackage(SwingPadController)
private File currentFileChooserDir = new File(prefs.get('currentFileChooserDir', '.'))
private File currentClasspathJarDir = new File(prefs.get('currentClasspathJarDir', '.'))
private File currentClasspathDir = new File(prefs.get('currentClasspathDir', '.'))
private File currentSnapshotDir = new File(prefs.get('currentSnapshotDir', '.'))
private runThread = null
private GroovyClassLoader groovyClassLoader
private static int scriptCounter = 0
private Set factorySet = new TreeSet()
void mvcGroupInit( Map args ) {
groovyClassLoader = new GroovyClassLoader(this.class.classLoader)
}
def updateTitle = { ->
// TODO handle undo!
if( model.scriptFile ) {
return model.scriptFile.name + (model.dirty ? " *" : "") + " - SwingPad"
}
return "SwingPad"
}
def newScript = { evt = null ->
if( askToSaveFile(evt) ) {
model.scriptFile = null
model.dirty = false
view.editor.textEditor.text = ''
view.editor.textEditor.requestFocus()
}
}
def open = { evt = null ->
model.scriptFile = selectFilename()
if( !model.scriptFile ) return
doOutside {
def scriptText = model.scriptFile.readLines().join('n')
doLater {
if( !scriptText ) return
// need 2-way binding!
view.editor.textEditor.text = scriptText
model.dirty = false
view.editor.textEditor.caretPosition = 0
view.editor.textEditor.requestFocus()
}
}
}
def save = { evt = null ->
if( !model.scriptFile ) return saveAs(evt)
model.scriptFile.write(model.content)
model.dirty = false
return true
}
def saveAs = { evt = null ->
model.scriptFile = selectFilename("Save")
if( model.scriptFile ) {
model.scriptFile.write(model.content)
model.dirty = false
return true
}
return false
}
def exit = { evt = null ->
if( askToSaveFile() ) {
FindReplaceUtility.dispose()
app.shutdown()
}
}
def snapshot = { evt ->
def fc = new JFileChooser(currentSnapshotDir)
fc.fileSelectionMode = JFileChooser.FILES_ONLY
fc.acceptAllFileFilterUsed = true
if (fc.showDialog(app.appFrames[0], "Snapshot") == JFileChooser.APPROVE_OPTION) {
currentSnapshotDir = fc.currentDirectory
prefs.put('currentSnapshotDir', currentSnapshotDir.path)
def frameBounds = app.appFrames[0].bounds
def capture = new Robot().createScreenCapture(frameBounds)
def filename = fc.selectedFile.name
def dot = filename.lastIndexOf(".")
def ext = "png"
if( dot > 0 ) {
ext = filename[dot+1..-1]
} else {
filename += ".$ext"
}
def target = new File(currentSnapshotDir,filename)
ImageIO.write( capture, ext, target )
def pane = builder.optionPane()
pane.setMessage("Successfully saved snapshot tonn${target.absolutePath}")
def dialog = pane.createDialog(app.appFrames[0], 'Snapshot')
dialog.show()
}
}
private void invokeTextAction( evt, closure ) {
if( evt.source ) closure(view.editor.textEditor)
}
def cut = { evt = null -> invokeTextAction(evt, { source -> source.cut() }) }
def copy = { evt = null -> invokeTextAction(evt, { source -> source.copy() }) }
def paste = { evt = null -> invokeTextAction(evt, { source -> source.paste() }) }
def selectAll = { evt = null -> invokeTextAction(evt, { source -> source.selectAll() }) }
// TODO yet unconnected!!
def find = { evt = null -> FindReplaceUtility.showDialog() }
def findNext = { evt = null -> FindReplaceUtility.FIND_ACTION.actionPerformed(evt) }
def findPrevious = { evt = null ->
def reverseEvt = new ActionEvent( evt.source, evt.iD,
evt.actionCommand, evt.when,
ActionEvent.SHIFT_MASK) //reverse
FindReplaceUtility.FIND_ACTION.actionPerformed(reverseEvt)
}
def replace = { evt = null -> FindReplaceUtility.showDialog(true) }
def largerFont = { evt = null ->
modifyFont(view.editor.textEditor, {it > 40}, +2)
modifyFont(view.errors, {it > 40}, +2)
}
def smallerFont = { evt = null ->
modifyFont(view.editor.textEditor, {it < 5}, -2)
modifyFont(view.errors, {it < 5}, -2)
}
def packComponents = { evt = null ->
def newLayout = evt?.source?.state ? builder.flowLayout(alignment:FlowLayout.LEFT, hgap: 0, vgap: 0) : builder.borderLayout()
if( !newLayout.class.isAssignableFrom(view.canvas.layout.class) ) {
view.canvas.layout = newLayout
if( model.successfulScript ) runScript(evt)
}
}
def showRulers = { evt = null ->
def rh = evt?.source?.state ? view.rowHeader : view.emptyRowHeader
def ch = evt?.source?.state ? view.columnHeader : view.emptyColumnHeader
if( view.scroller.rowHeader.view != rh ) {
view.scroller.rowHeaderView = rh
view.scroller.columnHeaderView = ch
view.scroller.repaint()
}
}
def runScript = { evt = null ->
if( !model.content ) return
view.tabs.selectedIndex = 0 // sourceTab
runThread = Thread.start {
try {
doLater {
model.status = "Running Script ..."
if( model.errors != "" ) {
model.errors = ""
model.caretPosition = 0
}
showDialog( "runWaitDialog" )
}
model.content = model.content.replaceAll(/(?ms)(^s+applications*(.+?)s*{)(.+)(}s*)/){ all, p1, p2, p3 ->
p1.readLines().collect{ "// " + it }.join("n") + p2 + p3.readLines().collect{ "// " + it }.join("n")
}
edt{
view.inputArea.text = model.content
}
executeScript( model.content )
} catch( Throwable t ) {
doLater { finishWithException(t) }
} finally {
doLater {
hideDialog( "runWaitDialog" )
runThread = null
}
}
}
}
def runSampleScript = { evt = null ->
if( model.currentSample ) {
def builder = model.currentSample[0..-2].toLowerCase()
if( !model.builders[builder].enabled ) {
model.status = "Enabling ${model.builders[builder].type} ..."
view."${builder}Menu".selected = true
doOutside {
if(toggleBuilder([source:view."${builder}Menu"], builder, model.builders[builder].type)) {
doLater {
model.status = "Loading Script ..."
view.editor.textEditor.text = model.samples[model.currentSample]
view.editor.textEditor.caretPosition = 0
view.runAction.enabled = true
runScript(evt)
}
}
}
} else {
model.status = "Loading Script ..."
view.editor.textEditor.text = model.samples[model.currentSample]
view.editor.textEditor.caretPosition = 0
view.runAction.enabled = true
runScript(evt)
}
}
}
def about = { evt = null ->
def pane = builder.optionPane()
// work around GROOVY-1048
pane.setMessage('Welcome to SwingPad')
def dialog = pane.createDialog(app.appFrames[0], 'About SwingPad')
dialog.show()
}
def confirmRunInterrupt = { evt = null ->
def rc = JOptionPane.showConfirmDialog( app.appFrames[0], "Attempt to interrupt script?",
"SwingPad", JOptionPane.YES_NO_OPTION)
if( rc == JOptionPane.YES_OPTION && runThread ) {
runThread.interrupt()
}
}
// the folowing 4 actions taken from groovy.ui.Console
def addClasspathJar = { evt = null ->
def fc = new JFileChooser(currentClasspathJarDir)
fc.fileSelectionMode = JFileChooser.FILES_ONLY
fc.acceptAllFileFilterUsed = true
if (fc.showDialog(app.appFrames[0], "Add") == JFileChooser.APPROVE_OPTION) {
currentClasspathJarDir = fc.currentDirectory
prefs.put('currentClasspathJarDir', currentClasspathJarDir.path)
groovyClassLoader.addURL(fc.selectedFile.toURL())
}
}
def addClasspathDir = { evt = null ->
def fc = new JFileChooser(currentClasspathDir)
fc.fileSelectionMode = JFileChooser.DIRECTORIES_ONLY
fc.acceptAllFileFilterUsed = true
if (fc.showDialog(app.appFrames[0], "Add") == JFileChooser.APPROVE_OPTION) {
currentClasspathDir = fc.currentDirectory
prefs.put('currentClasspathDir', currentClasspathDir.path)
groovyClassLoader.addURL(fc.selectedFile.toURL())
}
}
def showToolbar = { evt = null ->
def showToolbar = evt.source.selected
prefs.putBoolean('showToolbar', showToolbar)
view.toolbar.visible = showToolbar
}
def suggestNodeName = { evt = null ->
if( !model.content ) return
def editor = view.editor.textEditor
def caret = editor.caretPosition
if( !caret ) return
def document = editor.document
def target = ""
def ch = document.getText(--caret,1)
while( ch =~ /[a-zA-Z]/ ) {
target = ch + target
if( caret ) ch = document.getText(--caret,1)
else break
}
if( target.size() != document.length ) caret++
if( !factorySet ) populateFactorySet()
def suggestions = factorySet.findAll{ it.startsWith(target) }
if( !suggestions ) return
if( suggestions.size() == 1 ) {
model.suggestion = [
start: caret,
end: caret + target.size(),
offset: target.size(),
text: suggestions.iterator().next()
]
writeSuggestion()
} else {
model.suggestion = [
start: caret,
end: caret + target.size(),
offset: target.size()
]
model.suggestions.clear()
model.suggestions.addAll(suggestions)
view.popup.showPopup(SwingConstants.CENTER, app.appFrames[0])
view.suggestionList.selectedIndex = 0
}
}
def codeComplete = { evt ->
model.suggestion.text = model.suggestions[view.suggestionList.selectedIndex]
view.popup.hidePopup(true)
writeSuggestion()
}
def toggleFlamingoBuilder = { evt = null ->
doOutside {
toggleBuilder(evt, "flamingo", model.builders.flamingo.type)
}
}
def toggleTrayBuilder = { evt = null ->
doOutside {
toggleBuilder(evt, "tray", model.builders.tray.type)
}
}
def toggleMacwidgetsBuilder = { evt = null ->
doOutside {
toggleBuilder(evt, "macwidgets", model.builders.macwidgets.type)
}
}
def toggleLayout = { evt = null ->
model.horizontalLayout = !model.horizontalLayout
view.splitPane.orientation = model.horizontalLayout ? HORIZONTAL_SPLIT : VERTICAL_SPLIT
view.toggleLayoutAction.putValue("SmallIcon", model.horizontalLayout ? view.verticalLayoutIcon : view.horizontalLayoutIcon )
}
private writeSuggestion() {
if( !model.suggestion ) return
def editor = view.editor.textEditor
def document = editor.document
def s = model.suggestion
def text = s.text.substring(s.offset)
document.insertString(s.start+s.offset, text, null)
editor.requestFocus()
// clear it!
model.suggestion = [:]
}
private void finishNormal( component ) {
model.successfulScript = true
doLater {
model.status = 'Execution complete.'
view.canvas.removeAll()
view.canvas.repaint()
if( component instanceof JComponent ) {
view.canvas.add(component)
} else {
model.status = "The script did not return a JComponent!"
}
}
}
private void finishWithException( Throwable t ) {
model.successfulScript = false
model.status = 'Execution terminated with exception.'
StackTraceUtils.deepSanitize(t)
t.printStackTrace()
def baos = new ByteArrayOutputStream()
t.printStackTrace(new PrintStream(baos))
doLater {
view.canvas.removeAll()
view.canvas.repaint()
view.tabs.selectedIndex = 1 // errorsTab
model.errors = baos.toString()
model.caretPosition = 0
}
}
private void showAlert(title, message) {
doLater {
JOptionPane.showMessageDialog(app.appFrames[0], message,
title, JOptionPane.WARNING_MESSAGE)
}
}
private void showMessage(title, message) {
doLater {
JOptionPane.showMessageDialog(app.appFrames[0], message,
title, JOptionPane.INFORMATION_MESSAGE)
}
}
private void showDialog( dialogName, pack = true ) {
def dialog = view."$dialogName"
if( pack ) dialog.pack()
int x = app.appFrames[0].x + (app.appFrames[0].width - dialog.width) / 2
int y = app.appFrames[0].y + (app.appFrames[0].height - dialog.height) / 2
dialog.setLocation(x, y)
dialog.show()
}
private void hideDialog( dialogName ) {
def dialog = view."$dialogName"
dialog.hide()
}
private selectFilename( name = "Open" ) {
// should use builder.fileChooser() ?
def fc = new JFileChooser(currentFileChooserDir)
fc.fileSelectionMode = JFileChooser.FILES_ONLY
fc.acceptAllFileFilterUsed = true
if( fc.showDialog(app.appFrames[0], name ) == JFileChooser.APPROVE_OPTION ) {
currentFileChooserDir = fc.currentDirectory
prefs.put('currentFileChooserDir', currentFileChooserDir.path)
return fc.selectedFile
}
return null
}
private boolean askToSaveFile(evt) {
if( !model.scriptFile || !model.dirty ) return true
switch( JOptionPane.showConfirmDialog( app.appFrames[0],
"Save changes to " + model.scriptFile.name + "?",
"SwingPad", JOptionPane.YES_NO_CANCEL_OPTION)){
case JOptionPane.YES_OPTION: return save(evt)
case JOptionPane.NO_OPTION: return true
}
return false
}
private void executeScript( codeSource ) {
try {
codeSource = codeSource.replaceAll(
/(?ms)binds*([^)]+?)/, """"
)
def script = groovyClassLoader.parseClass(codeSource,getScriptName()).newInstance()
def b = app.builders.Script
def component = null
b.edt{ component = b.build(script)}
// if( !(component instanceof JComponent) ) {
// throw new IllegalArgumentException("The script did not return a JComponent!")
// }
doLater { finishNormal(component) }
} catch( Throwable t ) {
doLater { finishWithException(t) }
}
}
private getScriptName() {
"SwingPad_script" + (scriptCounter++)
}
private modifyFont( target, sizeFilter, sizeMod ) {
def currentFont = target.font
if( sizeFilter(currentFont.size) ) return
target.font = new Font( 'Monospaced', currentFont.style, currentFont.size + sizeMod )
}
private populateFactorySet() {
// TODO filter factories coming from SwingXBuilder that have jxclassicSwing: on their name
def ub = app.builders.Script
factorySet.clear()
ub.builderRegistration.each { ubr ->
def builder = ubr.builder
def oldProxy = builder.proxyBuilder
try {
builder.proxyBuilder = builder
factorySet.addAll(ubr.builder.factories.keySet().sort().collect(){ (ubr.prefixString?:"")+it })
} finally {
builder.proxyBuilder = oldProxy
}
}
factorySet -= factorySet.grep{ it.startsWith("jxclassicSwing:") }
}
private toggleBuilder( evt, name, builder ) {
def cname = name[0].toUpperCase() + name[1..-1]
model.builders[name].enabled = evt.source.selected
if( model.builders[name].enabled ) {
app.builderConfig.root."$builder".view = "*"
} else {
app.builderConfig.root.remove(builder)
}
try {
// With no current way to unload an URL from the rootLoader
// we have to keep track if an URL has already been added to it
if( !model.builders[name].loaded ) {
def startDir = System.getProperty("griffon.start.dir")
if( startDir.startsWith('"') && startDir.endsWith('"') ) {
startDir = startDir[1..-2]
}
def jarDir = new File(startDir,"lib/$name")
jarDir.eachFileMatch({it.endsWith(".jar")}) { jar ->
// groovyClassLoader.addURL(jar.toURI().toURL())
this.class.classLoader.addURL(jar.toURI().toURL())
}
model.builders[name].loaded = true
}
def binding = new Binding()
binding.setVariable("controller", this)
def script = """def (m, v, c) = controller.createMVCGroup("Script","Script",[:])
return v
"""
app.builders.Script = new GroovyShell(groovyClassLoader,binding).evaluate(script)
} catch( ex ) {
StackTraceUtils.deepSanitize(ex)
ex.printStackTrace()
model.builders[name].enabled != model.builders[name].enabled
evt.source.selected = !evt.source.selected
showAlert( "Enable $cname".toString(),
"Couldn't enable $cname:nn$ex".toString())
} finally {
populateFactorySet()
}
return model.builders[name].enabled
}
}
|