From 5534f643ebf0f2f23db30e8e8f103a19d63db0fa Mon Sep 17 00:00:00 2001 From: shemnon Date: Sat, 21 Jun 2008 03:55:02 +0000 Subject: [PATCH] GROOVY-2919 - Console as Applet support git-svn-id: http://svn.codehaus.org/groovy/trunk/groovy/groovy-core@12830 a5544e8c-8a19-0410-ba12-f9af4593a198 --- .../groovy/swing/factory/ImageIconFactory.groovy | 3 +- src/main/groovy/ui/Console.groovy | 63 +++++++++++++++++----- src/main/groovy/ui/ConsoleApplet.groovy | 23 ++++++++ src/main/groovy/ui/ConsoleView.groovy | 38 ++++++------- 4 files changed, 90 insertions(+), 37 deletions(-) create mode 100644 src/main/groovy/ui/ConsoleApplet.groovy diff --git a/src/main/groovy/swing/factory/ImageIconFactory.groovy b/src/main/groovy/swing/factory/ImageIconFactory.groovy index feeb693..ecafea3 100644 --- a/src/main/groovy/swing/factory/ImageIconFactory.groovy +++ b/src/main/groovy/swing/factory/ImageIconFactory.groovy @@ -18,7 +18,6 @@ package groovy.swing.factory import java.awt.Image import javax.swing.ImageIcon - class ImageIconFactory extends AbstractFactory { public Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) { @@ -65,7 +64,7 @@ class ImageIconFactory extends AbstractFactory { // we may need to extract the byte[] for some packaging cases value = klass.getResource(resource) if (value == null) { - throw new RuntimeException("In $name the value argument '$origValue' does not refer to a file or a class resource") + throw new RuntimeException("In $name the resource '$value' does not refer to a file or a class resource") } } diff --git a/src/main/groovy/ui/Console.groovy b/src/main/groovy/ui/Console.groovy index 30e1581..3d362c6 100644 --- a/src/main/groovy/ui/Console.groovy +++ b/src/main/groovy/ui/Console.groovy @@ -68,7 +68,7 @@ class Console implements CaretListener { // UI SwingBuilder swing - JFrame frame + RootPaneContainer frame ConsoleTextEditor inputEditor JTextPane inputArea JTextPane outputArea @@ -156,7 +156,42 @@ class Console implements CaretListener { void run() { + run([ + rootContainerDelegate:{ + frame( + title: 'GroovyConsole', + //location: [100,100], // in groovy 2.0 use platform default location + iconImage: imageIcon("/groovy/ui/ConsoleIcon.png").image, + defaultCloseOperation: JFrame.DO_NOTHING_ON_CLOSE, + ) { + try { + current.locationByPlatform = true + } catch (Exception e) { + current.location = [100, 100] // for 1.4 compatibility + } + containingWindows += current + } + }, + menuBarDelegate: {arg-> + current.JMenuBar = build(arg)} + ]) + } + + void run(JApplet applet) { + run([ + rootContainerDelegate:{ + containingWindows += SwingUtilities.getRoot(applet.getParent()) + applet + }, + menuBarDelegate: {arg-> + current.JMenuBar = build(arg)} + ]) + } + + void run(Map defaults) { + swing = new SwingBuilder() + defaults.each{k, v -> swing[k] = v} // tweak what the stack traces filter out to be fairly broad System.setProperty("groovy.sanitized.stacktraces", """org.codehaus.groovy.runtime. @@ -184,8 +219,10 @@ class Console implements CaretListener { swing.bind(source:swing.inputEditor.undoAction, sourceProperty:'enabled', target:swing.undoAction, targetProperty:'enabled') swing.bind(source:swing.inputEditor.redoAction, sourceProperty:'enabled', target:swing.redoAction, targetProperty:'enabled') - swing.consoleFrame.pack() - swing.consoleFrame.show() + if (swing.consoleFrame instanceof java.awt.Window) { + swing.consoleFrame.pack() + swing.consoleFrame.show() + } installInterceptor() swing.doLater inputArea.&requestFocus } @@ -297,8 +334,10 @@ class Console implements CaretListener { void exit(EventObject evt = null) { if (askToSaveFile()) { - frame.hide() - frame.dispose() + if (frame instanceof java.awt.Window) { + frame.hide() + frame.dispose() + } FindReplaceUtility.dispose() consoleControllers.remove(this) if (!consoleControllers) { @@ -665,9 +704,7 @@ class Console implements CaretListener { // Shows the 'wait' dialog void showRunWaitDialog() { runWaitDialog.pack() - int x = frame.x + (frame.width - runWaitDialog.width) / 2 - int y = frame.y + (frame.height - runWaitDialog.height) / 2 - runWaitDialog.setLocation(x, y) + runWaitDialog.setLocationRelativeTo(frame) runWaitDialog.show() } @@ -680,10 +717,12 @@ class Console implements CaretListener { } void updateTitle() { - if (scriptFile != null) { - frame.title = scriptFile.name + (dirty?" * ":"") + " - GroovyConsole" - } else { - frame.title = "GroovyConsole" + if (frame.properties.containsKey('title')) { + if (scriptFile != null) { + frame.title = scriptFile.name + (dirty?" * ":"") + " - GroovyConsole" + } else { + frame.title = "GroovyConsole" + } } } diff --git a/src/main/groovy/ui/ConsoleApplet.groovy b/src/main/groovy/ui/ConsoleApplet.groovy new file mode 100644 index 0000000..d790254 --- /dev/null +++ b/src/main/groovy/ui/ConsoleApplet.groovy @@ -0,0 +1,23 @@ +package groovy.ui + +import javax.swing.JApplet + +/** + * Created by IntelliJ IDEA. + * User: Danno.Ferrin + * Date: Jun 20, 2008 + * Time: 7:12:09 PM + */ +public class ConsoleApplet extends JApplet { + + Console console + + public void start() { + console = new Console() + console.run this + } + + public void stop() { + console.exit() + } +} diff --git a/src/main/groovy/ui/ConsoleView.groovy b/src/main/groovy/ui/ConsoleView.groovy index 7ff6a21..fe9d6c0 100644 --- a/src/main/groovy/ui/ConsoleView.groovy +++ b/src/main/groovy/ui/ConsoleView.groovy @@ -16,9 +16,13 @@ package groovy.ui -import groovy.ui.view.* +import groovy.ui.view.Defaults +import groovy.ui.view.GTKDefaults +import groovy.ui.view.MacOSXDefaults +import groovy.ui.view.WindowsDefaults +import java.awt.datatransfer.DataFlavor +import java.awt.dnd.* import javax.swing.UIManager -import static javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE import javax.swing.event.DocumentListener switch (UIManager.getSystemLookAndFeelClassName()) { @@ -40,19 +44,13 @@ switch (UIManager.getSystemLookAndFeelClassName()) { break } -consoleFrame = frame( - title: 'GroovyConsole', - //location: [100,100], // in groovy 2.0 use platform default location - iconImage: imageIcon("/groovy/ui/ConsoleIcon.png").image, - defaultCloseOperation: DO_NOTHING_ON_CLOSE, -) { - try { - current.locationByPlatform = true - } catch (Exception e) { - current.location = [100, 100] // for 1.4 compatibility - } +binding.rootContainerDelegate.delegate = this + +consoleFrame = binding['rootContainerDelegate']() +container(consoleFrame) { - build(menuBarClass) + binding.menuBarDelegate.delegate = delegate + binding['menuBarDelegate'](menuBarClass) build(contentPaneClass) @@ -83,7 +81,9 @@ controller.outputStyle = outputStyle controller.resultStyle = resultStyle // add the window close handler -consoleFrame.windowClosing = controller.&exit +if (consoleFrame instanceof java.awt.Window) { + consoleFrame.windowClosing = controller.&exit +} // link in references to the controller controller.inputEditor = inputEditor @@ -108,14 +108,6 @@ controller.inputArea.document.addDocumentListener({ controller.setDirty(true) } controller.rootElement = inputArea.document.defaultRootElement -import java.awt.dnd.DropTargetEvent -import java.awt.dnd.DropTarget -import java.awt.dnd.DropTargetListener -import java.awt.datatransfer.DataFlavor -import java.awt.dnd.DropTargetDragEvent -import java.awt.dnd.DropTargetDropEvent -import java.awt.dnd.DnDConstants - def dtListener = [ dragEnter:{DropTargetDragEvent evt -> if (evt.dropTargetContext.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { -- 2.11.4.GIT