GROOVY-2919 - Console as Applet support
[groovy.git] / src / main / groovy / ui / ConsoleView.groovy
blobfe9d6c018027a0c521c5bd09384cbf8b4810697a
1 /*
2 * Copyright 2003-2007 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package groovy.ui
19 import groovy.ui.view.Defaults
20 import groovy.ui.view.GTKDefaults
21 import groovy.ui.view.MacOSXDefaults
22 import groovy.ui.view.WindowsDefaults
23 import java.awt.datatransfer.DataFlavor
24 import java.awt.dnd.*
25 import javax.swing.UIManager
26 import javax.swing.event.DocumentListener
28 switch (UIManager.getSystemLookAndFeelClassName()) {
29 case 'com.sun.java.swing.plaf.windows.WindowsLookAndFeel':
30 case 'com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel':
31 build(WindowsDefaults)
32 break
34 case 'apple.laf.AquaLookAndFeel':
35 build(MacOSXDefaults)
36 break
38 case 'com.sun.java.swing.plaf.gtk.GTKLookAndFeel':
39 build(GTKDefaults)
40 break
42 default:
43 build(Defaults)
44 break
47 binding.rootContainerDelegate.delegate = this
49 consoleFrame = binding['rootContainerDelegate']()
50 container(consoleFrame) {
52 binding.menuBarDelegate.delegate = delegate
53 binding['menuBarDelegate'](menuBarClass)
55 build(contentPaneClass)
57 build(toolBarClass)
59 build(statusBarClass)
61 runWaitDialog = dialog(
62 title: 'Groovy executing',
63 modal: true,
64 pack:true,
65 ) {
66 vbox(border: emptyBorder(6)) {
67 label(text: "Groovy is now executing. Please wait.", alignmentX: 0.5f)
68 vstrut()
69 button(interruptAction,
70 margin: [10, 20, 10, 20],
71 alignmentX: 0.5f
78 controller.promptStyle = promptStyle
79 controller.commandStyle = commandStyle
80 controller.outputStyle = outputStyle
81 controller.resultStyle = resultStyle
83 // add the window close handler
84 if (consoleFrame instanceof java.awt.Window) {
85 consoleFrame.windowClosing = controller.&exit
88 // link in references to the controller
89 controller.inputEditor = inputEditor
90 controller.inputArea = inputEditor.textEditor
91 controller.outputArea = outputArea
92 controller.statusLabel = status
93 controller.frame = consoleFrame
94 controller.runWaitDialog = runWaitDialog
95 controller.rowNumAndColNum = rowNumAndColNum
96 controller.toolbar = toolbar
98 // link actions
99 controller.saveAction = saveAction
100 controller.prevHistoryAction = historyPrevAction
101 controller.nextHistoryAction = historyNextAction
102 controller.fullStackTracesAction = fullStackTracesAction
103 controller.showToolbarAction = showToolbarAction
105 // some more UI linkage
106 controller.inputArea.addCaretListener(controller)
107 controller.inputArea.document.addDocumentListener({ controller.setDirty(true) } as DocumentListener)
108 controller.rootElement = inputArea.document.defaultRootElement
111 def dtListener = [
112 dragEnter:{DropTargetDragEvent evt ->
113 if (evt.dropTargetContext.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
114 evt.acceptDrag(DnDConstants.ACTION_COPY)
115 } else {
116 evt.rejectDrag()
119 dragOver:{DropTargetDragEvent evt ->
120 //dragEnter(evt)
122 dropActionChanged:{DropTargetDragEvent evt ->
123 //dragEnter(evt)
125 dragExit:{DropTargetEvent evt ->
127 drop:{DropTargetDropEvent evt ->
128 evt.acceptDrop DnDConstants.ACTION_COPY
129 //println "Dropping! ${evt.transferable.getTransferData(DataFlavor.javaFileListFlavor)}"
130 if (controller.askToSaveFile()) {
131 controller.loadScriptFile(evt.transferable.getTransferData(DataFlavor.javaFileListFlavor)[0])
134 ] as DropTargetListener
136 [consoleFrame, inputArea, outputArea].each {
137 new DropTarget(it, DnDConstants.ACTION_COPY, dtListener)
140 // don't send any return value from the view, all items should be referenced via the bindings
141 return null