Package muntjac :: Package addon :: Package refresher :: Module refresher_application
[hide private]
[frames] | no frames]

Source Code for Module muntjac.addon.refresher.refresher_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   
16  from time import sleep 
17   
18  from threading import Thread 
19   
20  from muntjac.application import Application 
21  from muntjac.ui.window import Window 
22  from muntjac.ui.label import Label 
23  from muntjac.addon.refresher.refresher import Refresher, RefreshListener 
24   
25   
26 -class RefresherApplication(Application):
27
28 - def __init__(self):
29 super(RefresherApplication, self).__init__() 30 31 self._databaseResult = None 32 self._content = None
33 34
35 - def init(self):
36 mainWindow = Window('Refresher Database Example') 37 self.setMainWindow(mainWindow) 38 # present with a loading contents. 39 self._content = Label('please wait while the database is queried') 40 mainWindow.addComponent(self._content) 41 # the Refresher polls automatically 42 refresher = Refresher() 43 refresher.addListener(DatabaseListener(self)) 44 mainWindow.addComponent(refresher) 45 DatabaseQueryProcess(self).start()
46 47 48
49 -class DatabaseListener(RefreshListener):
50
51 - def __init__(self, app):
52 self._app = app
53
54 - def refresh(self, source):
55 if self._app._databaseResult is not None: 56 # stop polling 57 source.setEnabled(False) 58 # replace the "loading" with the actual fetched result 59 self._app._content.setValue('Database result was: ' + 60 self._app._databaseResult)
61 62
63 -class DatabaseQueryProcess(Thread):
64
65 - def __init__(self, app):
66 super(DatabaseQueryProcess, self).__init__() 67 self._app = app
68
69 - def run(self):
70 self._app._databaseResult = self.veryHugeDatabaseCalculation()
71
73 # faux long lasting database query 74 try: 75 sleep(2000) 76 except KeyboardInterrupt: 77 pass # ignore 78 79 return 'huge!'
80 81 82 if __name__ == '__main__': 83 from muntjac.main import muntjac 84 muntjac(RefresherApplication, nogui=True, debug=True, 85 contextRoot='.') 86