Changeset 362

Show
Ignore:
Timestamp:
10-04-12 14:53:02 (2 years ago)
Author:
tim.te.beek@…
Message:

Add a setCharacter option to DelimiterModel?
Listen to document changes and change button selection based on that rather than in filter

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/alttableimport-model-rewrite/src/main/java/nl/nbic/brs/alttableimport/models/DelimiterModel.java

    r361 r362  
    2727        DelimiterButtonModel    spaceButtonModel                = new DelimiterButtonModel(' '); 
    2828 
    29         SingleCharacterDocument otherDocument                   = new SingleCharacterDocument(new SingleCharacterFilterExcludingKnownDelimiters()); 
     29        SingleCharacterDocument otherDocument                   = new SingleCharacterDocument(new NonEmptySingleCharacterFilter()); 
    3030        // A cheeky override so we can call getDelimiter on any delimiter group selection 
    3131        ButtonModel                             otherButtonModel                = new DelimiterButtonModel('\0') { 
     
    5959 
    6060        /** 
     61         * Select the correct option out of the five delimiter button models, based on the delimiterChar. 
     62         *  
     63         * @param delimiterChar 
     64         */ 
     65        void selectCorrectOption(final char delimiterChar) { 
     66                // Be default, we mark otherButtonModel as selected 
     67                otherButtonModel.setSelected(true); 
     68                // Look to see if any of the other buttons should be selected instead 
     69                commaButtonModel.setSelected(commaButtonModel.getDelimiter() == delimiterChar); 
     70                semicolonButtonModel.setSelected(semicolonButtonModel.getDelimiter() == delimiterChar); 
     71                tabButtonModel.setSelected(tabButtonModel.getDelimiter() == delimiterChar); 
     72                spaceButtonModel.setSelected(spaceButtonModel.getDelimiter() == delimiterChar); 
     73        } 
     74 
     75        /** 
    6176         * Listen (exclusively) for changes to otherDocument, through adapter, to update selection based on text content. 
    6277         */ 
    6378        @Override 
    6479        public void stateChanged(final ChangeEvent e) { 
    65                 fireItemEvents(); 
     80                Character character = otherDocument.getCharacter(); 
     81                // Replace in AbstractDocument is implemented as a remove followed by an insert: Do nothing on initial remove 
     82                if (character == null) 
     83                        return; 
     84 
     85                selectCorrectOption(character); 
     86 
     87                // Only update listeners if the other option was selected 
     88                if (otherButtonModel.isSelected()) 
     89                        fireItemEvents(); 
    6690        } 
    6791 
     
    81105        } 
    82106 
     107        protected void setCharacter(final char delimiterChar) { 
     108                selectCorrectOption(delimiterChar); 
     109 
     110                // Only when other button was selected: Update document with the new value 
     111                if (otherButtonModel.isSelected()) 
     112                        otherDocument.setCharacter(delimiterChar); 
     113 
     114                // No need to trigger any events: they're already handled through item & document events 
     115        } 
     116 
    83117        @Override 
    84118        public Character[] getSelectedObjects() { 
     
    90124 
    91125        private void fireItemEvents() { 
    92                 // Replace is really just remove followed by an insertion: we should not trigger anything when remove is called 
    93                 if (getCharacter() == null) 
    94                         return; 
    95  
    96126                ItemEvent itemEvent = new ItemEvent(this, 0, getCharacter(), 0); 
    97127                for (ItemListener listener : listeners) 
     
    110140 
    111141        /** 
    112          * When one of a knows set of delimiters is typed into 
     142         * Do not allow null or empty text to pass through this filter. 
    113143         */ 
    114         class SingleCharacterFilterExcludingKnownDelimiters extends SingleCharacterFilter { 
     144        class NonEmptySingleCharacterFilter extends SingleCharacterFilter { 
    115145                @Override 
    116146                public void retainLast(final FilterBypass fb, final String text) throws BadLocationException { 
    117                         // In this case text removal should not yet trigger anything 
    118                         if (text == null || text.isEmpty()) 
    119                                 return; 
    120  
    121                         // Be default, we mark otherButtonModel as selected 
    122                         otherButtonModel.setSelected(true); 
    123  
    124                         // Look to see if any of the other buttons should be selected instead 
    125                         char lastChar = text.charAt(text.length() - 1); 
    126                         commaButtonModel.setSelected(commaButtonModel.getDelimiter() == lastChar); 
    127                         semicolonButtonModel.setSelected(semicolonButtonModel.getDelimiter() == lastChar); 
    128                         tabButtonModel.setSelected(tabButtonModel.getDelimiter() == lastChar); 
    129                         spaceButtonModel.setSelected(spaceButtonModel.getDelimiter() == lastChar); 
    130  
    131                         // If none other the other buttonModel stole focus, allow text to pass through 
    132                         if (otherButtonModel.isSelected()) 
     147                        // In this case text removal should not yet trigger anything: But do we really need this anymore? 
     148                        if (text != null && !text.isEmpty()) 
    133149                                super.retainLast(fb, text); 
    134150                }