class PythonWindow(KeyListener): def __init__(self): self.frame = JFrame("Python Window") self.historyList = JList(DefaultListModel()) self.historyList.cellRenderer = MyListCellRenderer() #self.historyPanel.layout = BoxLayout( # self.historyPanel, # BoxLayout.Y_AXIS #) scrollpane = JScrollPane() # JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, # JScrollPane.HORIZONTAL_SCROLLBAR_NEVER #) # scrollpane.preferredSize = 400, 800 inputPanel = JPanel() inputPanel.layout = GridLayout(1, 1) self.input = JTextArea("") self.input.border = BorderFactory.createEmptyBorder(5, 5, 5, 5) self.input.tabSize = 4 self.input.font = Font("Monospaced", Font.PLAIN, 12) #self.input.preferredSize = 500, 200 self.input.addKeyListener(self) #self.button = JButton('Run', actionPerformed=self.run) inputPanel.add(self.input) #inputPanel.add(self.button) scrollpane.viewport.view = self.historyList self.frame.add(scrollpane, BorderLayout.CENTER) self.frame.add(inputPanel, BorderLayout.PAGE_END) self.frame.size = 500, 600 self.frame.visible = False def toggle_visibility(self): self.frame.visible = not self.frame.visible def add(self, text, type="input"): self.historyList.model.addElement({"text": text, "type": type}) self.historyList.validate() self.frame.validate() last = self.historyList.model.getSize() - 1 self.historyList.ensureIndexIsVisible(last) def write(self, text): self.add(text, "output") def run(self, evt): source = self.input.text if not source.strip(): self.input.text = "" return processed_source = source.replace("$", "geo.") code = interface.compileinteractive(processed_source) if code in ("continue", "error"): code = interface.compilemodule(processed_source) if code == "error": return self.add(source.strip()) result = interface.run(code) if result == "OK": self.input.text = "" def keyPressed(self, evt): pass def keyReleased(self, evt): pass def keyTyped(self, evt): if evt.keyChar == '\n': # Only try to run compound statements when they end with # two \n source = self.input.text lines = source.split("\n") if lines[0].rstrip().endswith(":") and not source.endswith("\n\n"): for i, c in enumerate(lines[-2]): if c not in ' \t': break else: self.run(evt) return prefix = lines[-2][:i] if lines[-2].endswith(":"): prefix += '\t' self.input.text = source + prefix else: self.run(evt)
if event.getValueIsAdjusting(): return # Must run later, when the event dispatch thread # has updated the selection def after(): rowIndex = table.getSelectionModel().getLeadSelectionIndex() textarea.setText(applicants[rowIndex][2]) SwingUtilities.invokeLater(after) table.getSelectionModel().addListSelectionListener(TableSelectionListener()) # Enable changing text size in the textarea class FontSizeAdjuster(KeyAdapter): def keyPressed(self, event): key = event.getKeyCode() if event.isControlDown() and event.isShiftDown(): # like in e.g. a web browser font = event.getSource().getFont() size = font.getSize2D() # floating-point: important for later use of deriveFont # Otherwise deriveFont with an integer alters the style instead. sign = 0 if KeyEvent.VK_MINUS == key: sign = -1 elif KeyEvent.VK_PLUS or KeyEvent.VK_EQUALS == key: sign = 1 if 0 != sign: size = max(8.0, size + sign * 0.5) if size != font.getSize2D(): event.getSource().setFont(font.deriveFont(size)) textarea.addKeyListener(FontSizeAdjuster())
class PythonWindow(KeyListener): def __init__(self): self.frame = JFrame("Python Window") #self.historyList = JList(DefaultListModel()) #self.historyList.cellRenderer = MyListCellRenderer() scrollpane = JScrollPane() inputPanel = JPanel() inputPanel.layout = GridLayout(1, 1) self.input = JTextArea("") self.input.border = BorderFactory.createEmptyBorder(5, 5, 5, 5) self.input.tabSize = 4 self.input.font = Font("Monospaced", Font.PLAIN, 12) self.input.addKeyListener(self) inputPanel.add(self.input) self.outputpane = OutputPane() scrollpane.viewport.view = self.outputpane.textpane #self.historyList self.frame.add(scrollpane, BorderLayout.CENTER) self.frame.add(inputPanel, BorderLayout.PAGE_END) self.frame.size = 500, 600 self.frame.visible = False self.component = None def toggle_visibility(self): self.frame.visible = not self.frame.visible def add_component(self, c): self.remove_component() self.frame.add(c, BorderLayout.PAGE_START) self.component = c def remove_component(self): if self.component is not None: self.frame.remove(self.component) self.component = None def add(self, text, type="input"): self.outputpane.addtext(text, type) #self.historyList.model.addElement({"text": text, "type": type}) #self.historyList.validate() self.frame.validate() #last = self.historyList.model.getSize() - 1 #self.historyList.ensureIndexIsVisible(last) def error(self, text): self.outputpane.addtext(text, "error", ensure_newline=True) def write(self, text): self.add(text, "output") def run(self, evt): source = self.input.text if not source.strip(): self.input.text = "" return processed_source = source.replace("$", "geo.") code = interface.compileinteractive(processed_source) if code in ("continue", "error"): code = interface.compilemodule(processed_source) if code == "error": return source = source.strip() + '\n' self.outputpane.addtext(source, "input", ensure_newline=True) result = interface.run(code) if result == "OK": self.input.text = "" def keyPressed(self, evt): pass def keyReleased(self, evt): pass def keyTyped(self, evt): if evt.keyChar == '\n': # Only try to run compound statements when they end with # two \n source = self.input.text lines = source.split("\n") if lines[0].rstrip().endswith(":") and not source.endswith("\n\n"): for i, c in enumerate(lines[-2]): if c not in ' \t': break else: self.run(evt) return prefix = lines[-2][:i] if lines[-2].endswith(":"): prefix += '\t' self.input.text = source + prefix else: self.run(evt)
open_imp_listener = OpenImageListener( ) # keep a reference for unregistering on window closing ImagePlus.addImageListener(open_imp_listener) # A listener to detect whether there have been any edits to the text note class TypingListener(KeyAdapter): def keyPressed(self, event): rowIndex = getSelectedRowIndex() if event.getSource().getText() != table_entries[rowIndex][-1]: note_status.setText("Unsaved changes.") textarea.addKeyListener(TypingListener()) # React to a row being selected by showing the corresponding note # in the textarea to the right class TableSelectionListener(ListSelectionListener): def valueChanged(self, event): if event.getValueIsAdjusting(): return askToSaveUnsavedChanges() # Must run later in the context of the event dispatch thread # when the latter has updated the table selection def after(): rowIndex = getSelectedRowIndex() path.setText(table_entries[rowIndex][-2])