hbyint / lanterna

Automatically exported from code.google.com/p/lanterna
GNU Lesser General Public License v3.0
0 stars 0 forks source link

Ability to customize AbstractListBox #59

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This issue is linked to issue 56
I have implemented a "quick search" feature allowing to refresh the items in a 
list box matching a string provided by the user.

In order to "highlight" the part of items matching the search string, I decided 
to override the "printItem" method. However, the method is private, so as a 
workaround I had to copy the whole class inside my app.

Do you mind making this method protected instead ? Or do you have a better 
suggestion than overriding printItem ? (I can further describe if the previous 
statements sound obscure)

Thanks for your feedback !

Original issue reported on code.google.com by jli...@gmail.com on 3 Jan 2013 at 4:12

GoogleCodeExporter commented 9 years ago
Here is the (ugly ?) hack I made in order to support text highlighing:

    private void printItem(TextGraphics graphics, int x, int y, int index) {
        String asText = createItemString(index);

        if(asText.length() > graphics.getWidth())
            asText = asText.substring(0, graphics.getWidth());

        if (asText.startsWith("*")) {
            graphics.setBoldMask(true);
            asText = asText.substring(1, asText.length());
        }

        if (asText.contains("#red#")) {
            int i=0;
            for (String pieceOfString: asText.split("#red#")) {
                if (((i++) % 2) == 0) {
                    graphics.drawString(x, y, pieceOfString);
                    x += pieceOfString.length();
                }
                else {
                    Color previousColor = graphics.getBackgroundColor();
                    graphics.setBackgroundColor(Color.RED);
                    graphics.drawString(x, y, pieceOfString);
                    x += pieceOfString.length();
                    graphics.setBackgroundColor(previousColor);
                }
            }
        }
        else
            graphics.drawString(x, y, asText);
    }

Original comment by jli...@gmail.com on 3 Jan 2013 at 4:13

GoogleCodeExporter commented 9 years ago
I've changed it to protected in the upcoming 2.1.2 release and on default branch

Original comment by mab...@gmail.com on 4 Jan 2013 at 2:15

GoogleCodeExporter commented 9 years ago
Thanks :)

Original comment by jli...@gmail.com on 4 Jan 2013 at 2:17