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

Source Code for Module muntjac.addon.refresher.refresher_demo_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  import time 
 17   
 18  from threading import Thread 
 19   
 20  from muntjac.application import Application 
 21  from muntjac.ui.window import Window 
 22  from muntjac.ui.panel import Panel 
 23  from muntjac.ui.horizontal_layout import HorizontalLayout 
 24  from muntjac.addon.refresher.refresher import Refresher 
 25  from muntjac.ui.label import Label 
 26  from muntjac.ui import button 
 27  from muntjac.ui.button import Button 
 28   
 29  SLEEP_TIME_IN_MILLIS = 1000  # a second 
 30   
31 -class RefresherApplication(Application):
32
33 - def init(self):
34 mainWindow = Window('Refresher') 35 self.setMainWindow(mainWindow) 36 panel = Panel('Refresher example') 37 layout = HorizontalLayout() 38 refresher = Refresher() 39 label = Label('0') 40 thread = CounterThread(label) 41 thread.start() 42 label.setData(0) 43 panel.addComponent(refresher) 44 panel.addComponent(Label("<div style='margin-bottom:10px'>" 45 + "The Refresher allows you to affect the UI " 46 + "from external threads without " 47 + "<a href='http://vaadin.com/forum/-/message_boards/message/69792' target='_blank'>" 48 + "the ProgressIndicator hack</a>.</div>", Label.CONTENT_XHTML)) 49 panel.addComponent(layout) 50 layout.setSpacing(True) 51 layout.addComponent(Button('Start Counting', 52 StartClickListener(refresher, thread))) 53 layout.addComponent(Button('Stop Counting', 54 StopClickListener(refresher, thread))) 55 layout.addComponent(label) 56 mainWindow.setContent(panel)
57 58
59 -class StartClickListener(button.IClickListener):
60
61 - def __init__(self, refresher, thread):
62 self.refresher = refresher 63 self.thread = thread
64
65 - def buttonClick(self, event):
68 69
70 -class StopClickListener(button.IClickListener):
71
72 - def __init__(self, refresher, thread):
73 self.refresher = refresher 74 self.thread = thread
75
76 - def buttonClick(self, event):
77 self.refresher.setRefreshInterval(0) 78 self.thread.stopCounting()
79 80
81 -class CounterThread(Thread):
82
83 - def __init__(self, renderLabel):
84 super(CounterThread, self).__init__() 85 self._renderLabel = renderLabel 86 renderLabel.setData(1) 87 self._running = False
88
89 - def run(self):
90 startTime = 1000 * time.time() 91 lifetime = 1000 * 60 92 # live for a minute. 93 try: 94 while 1000 * time.time() < startTime + lifetime: 95 if self._running: 96 # synchronize with the application, to avoid concurrent 97 # edits on the label's value. 98 number = self._renderLabel.getData() 99 self._renderLabel.setValue(number) 100 self._renderLabel.setData(number + 1) 101 time.sleep(SLEEP_TIME_IN_MILLIS) 102 self._renderLabel.setValue('[ counter thread expired ]') 103 except KeyboardInterrupt: 104 self._renderLabel.setValue('[ counter thread interrupted ]')
105
106 - def startCounting(self):
107 self._running = True
108
109 - def stopCounting(self):
110 self._running = False
111 112 113 if __name__ == '__main__': 114 from muntjac.main import muntjac 115 muntjac(RefresherApplication, nogui=True, debug=True, 116 contextRoot='.') 117