def invokeThreadsafe(fn, *args, **kwargs): if SwingUtilities.isEventDispatchThread(): return fn(*args, **kwargs) else: task = FunctionCall(fn, args, kwargs) SwingUtilities.invokeAndWait(task) return task.getResult()
def run(self): """ Execute the in-place scaling of the ImagePlus, here playing the role of a costly operation. """ if self.getState("restore"): print "Restoring original" ip = self.original_ip self.putState("restore", False) else: requested, last = self.getState("requested_scaling_factor", "last_scaling_factor") if requested == last: return # nothing to do print "Scaling to", requested new_width = int(self.original_ip.getWidth() * requested) ip = self.original_ip.resize(new_width) self.putState("last_scaling_factor", requested) # Request updating the ImageProcessor in the event dispatch thread, # given that the "setProcessor" method call will trigger # a change in the dimensions of the image window SwingUtilities.invokeAndWait(lambda: self.imp.setProcessor(ip)) # Terminate recurrent execution if so requested if self.getState("shutdown"): self.scheduled_executor.shutdown()
def action(self): node = ModelFactory.constructModel(constructable) if node is None: raise Exception("No model was created") elif isinstance(node, Node): SwingUtilities.invokeAndWait(make_runnable(self.add_and_open)) else: raise Exception("Can not add model of the type: " + node.class.simpleName)
def _swingWaitForResult(func, *args, **kwargs): if SwingUtilities.isEventDispatchThread(): return func(*args, **kwargs) wrappedCode = _JythonCallable(func, args, kwargs) task = FutureTask(wrappedCode) SwingUtilities.invokeAndWait(task) return task.get()
def flip(self): """ Repaint display. """ self._rect_list = self._surface_rect try: SwingUtilities.invokeAndWait(self) except InterruptedException: Thread.currentThread().interrupt()
def runOnEventDispatchThread(method, *args): class EDTRunnable(Runnable): def run(self): method(*args) if SwingUtilities.isEventDispatchThread(): method(*args) else: SwingUtilities.invokeAndWait(EDTRunnable())
def snapshot(frame, box): bi = BufferedImage(box.width, box.height, BufferedImage.TYPE_INT_RGB) g = bi.createGraphics() g.translate(-box.x, -box.y) #all black! # frame.paintAll(g) #only swing components! # frame.paint(g) #only swing components! # frame.update(g) #together, also only swing and with errors ##frame.update(g) ##frame.paint(g) # locks the entire graphics machinery # frame.printAll(g) # Finally, the right one: SwingUtilities.invokeAndWait(PrintAll(frame, g)) return bi
def snapshot(frame, box): bi = BufferedImage(box.width, box.height, BufferedImage.TYPE_INT_RGB) g = bi.createGraphics() g.translate(-box.x, -box.y) # all black! # frame.paintAll(g) # only swing components! # frame.paint(g) # only swing components! # frame.update(g) # together, also only swing and with errors ##frame.update(g) ##frame.paint(g) # locks the entire graphics machinery # frame.printAll(g) # Finally, the right one: SwingUtilities.invokeAndWait(PrintAll(frame, g)) return bi
def update(self, rect_list=None): """ Repaint display. Optional rect or rect list to specify regions to repaint. """ if isinstance(rect_list, list): self._rect_list = rect_list elif rect_list: self._rect_list = [rect_list] else: self._rect_list = self._surface_rect try: SwingUtilities.invokeAndWait(self) except InterruptedException: Thread.currentThread().interrupt()
def _swingWaitForResult(func, *args, **kwargs): # the naming of this function, along with _swingRunner, is kinda # misleading; both functions will "wait" for the result (due to the get()). # the difference between the two is that this function "wraps" # invokeAndWait, while _swingRunner "wraps" invokeLater. # # the real world difference is that this function puts its Task at the # beginning of the event dispatch thread, while _swingRunner appends to the # end of the queue. if SwingUtilities.isEventDispatchThread(): return func(*args, **kwargs) wrappedCode = _JythonCallable(func, args, kwargs) task = FutureTask(wrappedCode) SwingUtilities.invokeAndWait(task) return task.get()
def callSwing(func, *args, **kwargs): """ Runs the given function in the Event Dispatch Thread. If this is invoked inside the EDT, the given function will be run normally. Otherwise, it'll be queued to be run and the calling thread will block until the function has been executed. :return: func's return value """ if SwingUtilities.isEventDispatchThread(): return func(*args, **kwargs) callable = CallableWrapper(func, args, kwargs) task = FutureTask(callable) SwingUtilities.invokeAndWait(task) return task.get()
def readInput(self, message): #to be threadsafe on this call we need this to update the command window class outputPromptRunner(Runnable): def __init__(self, cw, m): self.commandWindow = cw self.message = m def run(self): #display the message and reenable the window to editing self.commandWindow.showText(self.message) self.commandWindow.setCaretPosition(self.commandWindow.document.getLength() ) self.commandWindow.setKeymap(self.commandWindow.my_keymap) #--------------The Actual work goes here------------------ if not message: message = "" #first assign value to null self.value = None #note that the this interpreter thread needs to wait now #until a result comes back from the command window self.waitingFlag = True #update the command window with the ouput SwingUtilities.invokeAndWait(outputPromptRunner(self.commandWindow, message)) #Now we wait for the user to input a value and press ENTER in the command window, #pausing a 10th of a second between checks. while self.waitingFlag: time.sleep(0.1) #clean up the return value to remove the prompt since the command #window will give us everything self.value = self.value[len(message):] #return the value to media.py return self.value
def run(self): SwingUtilities.invokeAndWait(make_runnable(self.createUINetwork(NengoGraphics())))
imageReader.setInput(inputStream) return imageReader.read(0) def resizeImage(self, fullSizeImage): bufferedImage = BufferedImage(SwingingMonkeyCommander._preferredWidth, SwingingMonkeyCommander._preferredHeight, BufferedImage.TRANSLUCENT) graphics2d = bufferedImage.createGraphics() graphics2d.addRenderingHints(RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)) graphics2d.drawImage(fullSizeImage, 0, 0, SwingingMonkeyCommander._preferredWidth, SwingingMonkeyCommander._preferredHeight, None) graphics2d.dispose() SwingingMonkeyCommander._widthScale = float(fullSizeImage.getWidth()) / float(bufferedImage.getWidth()) SwingingMonkeyCommander._heightScale = float(fullSizeImage.getHeight()) / float(bufferedImage.getHeight()) return bufferedImage def done(self): try: self.get() #raise exception if abnormal completion except ExecutionException, e: raise SystemExit, e.getCause() class Runnable(Runnable): def __init__(self, runFunction): self._runFunction = runFunction def run(self): self._runFunction() if __name__ == '__main__': SwingUtilities.invokeAndWait(Runnable(SwingingMonkeyCommander)) while True: time.sleep(1000)
def invokeAndWait(func, *args, **kwargs): """Convenience method for SwingUtilities.invokeAndWait().""" SwingUtilities.invokeAndWait(ProcRunnable(func, *args, **kwargs)) return