class AtfAreaView(JPanel): ''' Initializes the ATF (edit/model) view and sets its layout. ''' def __init__(self, controller): ''' Creates default empty text area in a panel for ATF edition. It has syntax highlighting based on the ATF parser (pyoracc). It also highlights line numbers where there are validations errors returned by the ORACC server. ''' # Give reference to controller to delegate action response self.controller = controller # Make text area occupy all available space and resize with parent # window self.setLayout(BorderLayout()) # Short hand for edit area and line numbers area self.edit_area = self.controller.edit_area self.line_numbers_area = self.controller.line_numbers_area # Create secondary text area for split view self.secondary_area = self.controller.secondary_area self.secondary_line_numbers = self.controller.secondary_line_numbers # Set undo/redo manager to edit area self.undo_manager = UndoManager() self.undo_manager.limit = 3000 self.edit_listener = AtfUndoableEditListener(self.undo_manager) self.edit_area.getDocument().addUndoableEditListener( self.edit_listener) # Sort out layout by synch-ing line numbers and text area and putting # only the text area in a scroll pane as indicated in the # TextLineNumber tutorial. self.edit_area.setPreferredSize(Dimension(1, 500)) self.container = JScrollPane(self.edit_area) self.container.setRowHeaderView(self.line_numbers_area) self.add(self.container, BorderLayout.CENTER) self.vert_scroll = self.container.getVerticalScrollBar() self.vert_scroll.addAdjustmentListener(atfAreaAdjustmentListener(self)) # Key listener that triggers syntax highlighting, etc. upon key release self.edit_area.addKeyListener(AtfAreaKeyListener(self)) # Also needed in secondary area: self.secondary_area.addKeyListener(AtfAreaKeyListener(self)) # Add a document listener to track changes to files docListener = atfAreaDocumentListener(self) self.edit_area.getDocument().addDocumentListener(docListener) # instance variable to store a record of the text contents prior to the # most recent change. Needed so that the different listeners can access # this to handle error line updating. self.oldtext = '' def toggle_split(self, split_orientation=None): ''' Clear ATF edit area and repaint chosen layout (splitscreen/scrollpane). ''' # Remove all existent components in parent JPanel self.removeAll() # Check what editor view to toggle self.setup_edit_area(split_orientation) # Revalitate is needed in order to repaint the components self.revalidate() self.repaint() def setup_edit_area(self, split_orientation=None): ''' Check if the ATF text area is being displayed in a split editor. If so, resets to normal JScrollPane. If not, splits the screen. ''' if isinstance(self.container, JSplitPane): # If Nammu is already displaying a split pane, reset to original # setup self.container = JScrollPane(self.edit_area) self.container.setRowHeaderView(self.line_numbers_area) self.container.setVisible(True) self.add(self.container, BorderLayout.CENTER) else: # If there is not a split pane, create both panels and setup view main_editor = JScrollPane(self.edit_area) main_editor.setRowHeaderView(self.line_numbers_area) secondary_editor = JScrollPane(self.secondary_area) secondary_editor.setRowHeaderView(self.secondary_line_numbers) self.container = JSplitPane(split_orientation, main_editor, secondary_editor) self.container.setDividerSize(5) self.container.setVisible(True) self.container.setDividerLocation(0.5) self.container.setResizeWeight(0.5) self.add(self.container, BorderLayout.CENTER) def get_viewport_carets(self): ''' Get the top left and bottom left caret position of the current viewport ''' extent = self.container.getViewport().getExtentSize() top_left_position = self.container.getViewport().getViewPosition() top_left_char = self.edit_area.viewToModel(top_left_position) bottom_left_position = Point(top_left_position.x, top_left_position.y + extent.height) bottom_left_char = self.edit_area.viewToModel(bottom_left_position) # Something has gone wrong. Assume that top_left should be at the start # of the file if top_left_char >= bottom_left_char: top_left_char = 0 # Get the text in the full edit area text = self.controller.edit_area.getText() # Pad the top of the viewport to capture up to the nearest header and # the bottom by 2 lines top_ch = self.controller.pad_top_viewport_caret(top_left_char, text) bottom_ch = self.controller.pad_bottom_viewport_caret(bottom_left_char, text) return top_ch, bottom_ch def refresh(self): ''' Restyle edit area using user selected appearance settings. ''' config = self.controller.controller.config # Create a new font with the new size font = set_font(config['edit_area_style']['fontsize']['user']) # Update the sytnax highlighter font params, so our changes are not # superceded self.controller.syntax_highlighter.font = font self.controller.syntax_highlighter.setup_attribs() attrs = self.controller.edit_area.getInputAttributes() StyleConstants.setFontSize(attrs, font.getSize()) # Get the Styledoc so we can update it doc = self.controller.edit_area.getStyledDocument() # Apply the new fontsize to the whole document doc.setCharacterAttributes(0, doc.getLength() + 1, attrs, False)
class AtfAreaView(JPanel): ''' Initializes the ATF (edit/model) view and sets its layout. ''' def __init__(self, controller): ''' Creates default empty text area in a panel for ATF edition. It has syntax highlighting based on the ATF parser (pyoracc). It also highlights line numbers where there are validations errors returned by the ORACC server. ''' # Give reference to controller to delegate action response self.controller = controller # Make text area occupy all available space and resize with parent # window self.setLayout(BorderLayout()) # Short hand for edit area and line numbers area self.edit_area = self.controller.edit_area self.line_numbers_area = self.controller.line_numbers_area # Create secondary text area for split view self.secondary_area = self.controller.secondary_area self.secondary_line_numbers = self.controller.secondary_line_numbers # Set undo/redo manager to edit area self.undo_manager = UndoManager() self.undo_manager.limit = 3000 self.edit_listener = AtfUndoableEditListener(self.undo_manager) self.edit_area.getDocument().addUndoableEditListener( self.edit_listener) # Sort out layout by synch-ing line numbers and text area and putting # only the text area in a scroll pane as indicated in the # TextLineNumber tutorial. self.edit_area.setPreferredSize(Dimension(1, 500)) self.container = JScrollPane(self.edit_area) self.container.setRowHeaderView(self.line_numbers_area) self.add(self.container, BorderLayout.CENTER) # Key listener that triggers syntax highlighting, etc. upon key release self.edit_area.addKeyListener(AtfAreaKeyListener(self.controller)) # Also needed in secondary area: self.secondary_area.addKeyListener(AtfAreaKeyListener(self.controller)) def toggle_split(self, split_orientation=None): ''' Clear ATF edit area and repaint chosen layout (splitscreen/scrollpane). ''' # Remove all existent components in parent JPanel self.removeAll() # Check what editor view to toggle self.setup_edit_area(split_orientation) # Revalitate is needed in order to repaint the components self.revalidate() self.repaint() def setup_edit_area(self, split_orientation=None): ''' Check if the ATF text area is being displayed in a split editor. If so, resets to normal JScrollPane. If not, splits the screen. ''' if isinstance(self.container, JSplitPane): # If Nammu is already displaying a split pane, reset to original # setup self.container = JScrollPane(self.edit_area) self.container.setRowHeaderView(self.line_numbers_area) self.container.setVisible(True) self.add(self.container, BorderLayout.CENTER) else: # If there is not a split pane, create both panels and setup view main_editor = JScrollPane(self.edit_area) main_editor.setRowHeaderView(self.line_numbers_area) secondary_editor = JScrollPane(self.secondary_area) secondary_editor.setRowHeaderView(self.secondary_line_numbers) self.container = JSplitPane(split_orientation, main_editor, secondary_editor) self.container.setDividerSize(5) self.container.setVisible(True) self.container.setDividerLocation(0.5) self.container.setResizeWeight(0.5) self.add(self.container, BorderLayout.CENTER)
class AtfAreaView(JPanel): ''' Initializes the ATF (edit/model) view and sets its layout. ''' def __init__(self, controller): ''' Creates default empty text area in a panel for ATF edition. It has syntax highlighting based on the ATF parser (pyoracc). It also highlights line numbers where there are validations errors returned by the ORACC server. ''' # Give reference to controller to delegate action response self.controller = controller # Make text area occupy all available space and resize with parent # window self.setLayout(BorderLayout()) # Short hand for edit area and line numbers area self.edit_area = self.controller.edit_area self.line_numbers_area = self.controller.line_numbers_area # Create secondary text area for split view self.secondary_area = self.controller.secondary_area self.secondary_line_numbers = self.controller.secondary_line_numbers # Set undo/redo manager to edit area self.undo_manager = UndoManager() self.undo_manager.limit = 3000 self.edit_listener = AtfUndoableEditListener(self.undo_manager) self.edit_area.getDocument().addUndoableEditListener( self.edit_listener) # Sort out layout by synch-ing line numbers and text area and putting # only the text area in a scroll pane as indicated in the # TextLineNumber tutorial. self.edit_area.setPreferredSize(Dimension(1, 500)) self.container = JScrollPane(self.edit_area) self.container.setRowHeaderView(self.line_numbers_area) self.add(self.container, BorderLayout.CENTER) self.vert_scroll = self.container.getVerticalScrollBar() self.vert_scroll.addAdjustmentListener(atfAreaAdjustmentListener(self)) # Key listener that triggers syntax highlighting, etc. upon key release self.edit_area.addKeyListener(AtfAreaKeyListener(self)) # Also needed in secondary area: self.secondary_area.addKeyListener(AtfAreaKeyListener(self)) # Add a document listener to track changes to files docListener = atfAreaDocumentListener(self) self.edit_area.getDocument().addDocumentListener(docListener) # instance variable to store a record of the text contents prior to the # most recent change. Needed so that the different listeners can access # this to handle error line updating. self.oldtext = '' def toggle_split(self, split_orientation=None): ''' Clear ATF edit area and repaint chosen layout (splitscreen/scrollpane). ''' # Remove all existent components in parent JPanel self.removeAll() # Check what editor view to toggle self.setup_edit_area(split_orientation) # Revalitate is needed in order to repaint the components self.revalidate() self.repaint() def setup_edit_area(self, split_orientation=None): ''' Check if the ATF text area is being displayed in a split editor. If so, resets to normal JScrollPane. If not, splits the screen. ''' if isinstance(self.container, JSplitPane): # If Nammu is already displaying a split pane, reset to original # setup self.container = JScrollPane(self.edit_area) self.container.setRowHeaderView(self.line_numbers_area) self.container.setVisible(True) self.add(self.container, BorderLayout.CENTER) else: # If there is not a split pane, create both panels and setup view main_editor = JScrollPane(self.edit_area) main_editor.setRowHeaderView(self.line_numbers_area) secondary_editor = JScrollPane(self.secondary_area) secondary_editor.setRowHeaderView(self.secondary_line_numbers) self.container = JSplitPane(split_orientation, main_editor, secondary_editor) self.container.setDividerSize(5) self.container.setVisible(True) self.container.setDividerLocation(0.5) self.container.setResizeWeight(0.5) self.add(self.container, BorderLayout.CENTER) def get_viewport_carets(self): ''' Get the top left and bottom left caret position of the current viewport ''' extent = self.container.getViewport().getExtentSize() top_left_position = self.container.getViewport().getViewPosition() top_left_char = self.edit_area.viewToModel(top_left_position) bottom_left_position = Point(top_left_position.x, top_left_position.y + extent.height) bottom_left_char = self.edit_area.viewToModel(bottom_left_position) # Something has gone wrong. Assume that top_left should be at the start # of the file if top_left_char >= bottom_left_char: top_left_char = 0 # Get the text in the full edit area text = self.controller.edit_area.getText() # Pad the top of the viewport to capture up to the nearest header and # the bottom by 2 lines top_ch = self.controller.pad_top_viewport_caret(top_left_char, text) bottom_ch = self.controller.pad_bottom_viewport_caret( bottom_left_char, text) return top_ch, bottom_ch def refresh(self): ''' Restyle edit area using user selected appearance settings. ''' config = self.controller.controller.config # Create a new font with the new size font = set_font(config['edit_area_style']['fontsize']['user']) # Update the sytnax highlighter font params, so our changes are not # superceded self.controller.syntax_highlighter.font = font self.controller.syntax_highlighter.setup_attribs() attrs = self.controller.edit_area.getInputAttributes() StyleConstants.setFontSize(attrs, font.getSize()) # Get the Styledoc so we can update it doc = self.controller.edit_area.getStyledDocument() # Apply the new fontsize to the whole document doc.setCharacterAttributes(0, doc.getLength() + 1, attrs, False)