Package muntjac :: Package addon :: Package codemirror :: Module code_mirror_application
[hide private]
[frames] | no frames]

Source Code for Module muntjac.addon.codemirror.code_mirror_application

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  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  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 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. 
 15  from muntjac.ui.check_box import CheckBox 
 16   
 17  from muntjac.api \ 
 18      import Application, Window, GridLayout, Select, Button 
 19   
 20  from muntjac.data.property \ 
 21      import IValueChangeListener 
 22   
 23  from muntjac.ui.button \ 
 24      import IClickListener 
 25   
 26  from muntjac.addon.codemirror.client.code_mode \ 
 27      import CodeMode 
 28   
 29  from muntjac.addon.codemirror.client.code_theme \ 
 30      import CodeTheme 
 31   
 32  from muntjac.addon.codemirror.code_mirror \ 
 33      import CodeMirror 
 34   
 35   
36 -class CodeMirrorApplication(Application):
37 38 _NL = '\n\n' 39 40 _SAMPLE_CODE = ('<xml is="fun"></xml>' + _NL 41 + 'function js(isMoreFun) {alert("Yay!");}' + _NL 42 + 'public void java(String isAlsoCool) {\n\twith("Vaadin!");\n}' 43 + _NL + 'def python(isCooler): print "with Muntjac!"' 44 + _NL + 'select * from web where you = i;') 45
46 - def init(self):
47 mainWindow = Window('CodeMirror Sample Application') 48 49 hl = GridLayout(2, 5) 50 hl.setSpacing(True) 51 mainWindow.addComponent(hl) 52 53 # #1 54 code = CodeMirror('Your Code', CodeMode.TEXT) 55 code.setValue(self._SAMPLE_CODE) 56 code.setWidth('500px') 57 code.setHeight('350px') 58 hl.addComponent(code) 59 60 # #2 61 code2 = CodeMirror('Your Code Too', CodeMode.PYTHON) 62 code2.setValue(self._SAMPLE_CODE) 63 # code2.setWidth('400px') 64 # code2.setHeight('300px') 65 hl.addComponent(code2) 66 67 68 codeMode = Select('Select your mode') 69 for cs in CodeMode.values(): 70 codeMode.addItem(cs) 71 codeMode.setNewItemsAllowed(False) 72 codeMode.setNullSelectionAllowed(False) 73 codeMode.setImmediate(True) 74 hl.addComponent(codeMode) 75 76 l = CodeModeChangeListener(code, codeMode) 77 codeMode.addListener(l, IValueChangeListener) 78 codeMode.setValue(CodeMode.TEXT) 79 80 codeMode = Select('Select your mode too') 81 for cs in CodeMode.values(): 82 codeMode.addItem(cs) 83 codeMode.setNewItemsAllowed(False) 84 codeMode.setNullSelectionAllowed(False) 85 codeMode.setImmediate(True) 86 hl.addComponent(codeMode) 87 88 l = CodeModeChangeListener(code2, codeMode) 89 codeMode.addListener(l, IValueChangeListener) 90 codeMode.setValue(CodeMode.PYTHON) 91 92 93 codeTheme = Select('Select your theme') 94 for ct in CodeTheme.values(): 95 codeTheme.addItem(ct) 96 codeTheme.setNewItemsAllowed(False) 97 codeTheme.setImmediate(True) 98 hl.addComponent(codeTheme) 99 100 l = CodeThemeChangeListener(code, codeTheme) 101 codeTheme.addListener(l, IValueChangeListener) 102 codeTheme.setValue(CodeTheme.DEFAULT) 103 104 codeTheme = Select('Select your theme too') 105 for ct in CodeTheme.values(): 106 codeTheme.addItem(ct) 107 codeTheme.setNewItemsAllowed(False) 108 codeTheme.setImmediate(True) 109 hl.addComponent(codeTheme) 110 111 l = CodeThemeChangeListener(code2, codeTheme) 112 codeTheme.addListener(l, IValueChangeListener) 113 codeTheme.setValue(CodeTheme.ECLIPSE) 114 115 116 l = CopyClickListener(code, code2) 117 hl.addComponent(Button('copy to -->', l)) 118 119 l = CopyClickListener(code2, code) 120 hl.addComponent(Button('<- copy to', l)) 121 122 123 l = ShowLineNumbersListener(code) 124 cb = CheckBox("Show line numbers", l) 125 cb.setImmediate(True) 126 hl.addComponent(cb) 127 128 l = ShowLineNumbersListener(code2) 129 cb = CheckBox("Show line numbers", l) 130 cb.setImmediate(True) 131 hl.addComponent(cb) 132 133 134 self.setMainWindow(mainWindow)
135 136
137 -class CodeModeChangeListener(IValueChangeListener):
138
139 - def __init__(self, code, codeMode):
140 self._code = code 141 self._codeMode = codeMode
142
143 - def valueChange(self, event):
144 self._code.setCodeMode(self._codeMode.getValue())
145 146
147 -class CopyClickListener(IClickListener):
148
149 - def __init__(self, code1, code2):
150 self._code1 = code1 151 self._code2 = code2
152
153 - def buttonClick(self, event):
154 self._code2.setValue(self._code1.getValue())
155 156
157 -class CodeThemeChangeListener(IValueChangeListener):
158
159 - def __init__(self, code, codeTheme):
160 self._code = code 161 self._codeTheme = codeTheme
162
163 - def valueChange(self, event):
164 self._code.setCodeTheme(self._codeTheme.getValue())
165 166
167 -class ShowLineNumbersListener(IClickListener):
168
169 - def __init__(self, code):
170 self._code = code
171
172 - def buttonClick(self, event):
174 175 176 if __name__ == '__main__': 177 from muntjac.main import muntjac 178 muntjac(CodeMirrorApplication, nogui=True, forever=True, debug=True) 179