I've got a few embellishments to that idea that others might enjoy.  These
include the ability to use on non-string properties, and caching the last
compiled pattern (maybe uses a bit less CPU than replaceAll each time).

    protected static class HighlightingModel extends LoadableDetachableModel
{
        private static final long serialVersionUID = 1L;

        private transient IModel text;
        private transient IModel searchWord;
        private transient Pattern lastp;
        private transient String lastSearch;

        HighlightingModel(IModel text, IModel searchWord) {
            this.text = text;
            this.searchWord = searchWord;
        }

        @Override
        protected String load() {
            String srch = (String)searchWord.getObject();
            Object object = text.getObject();
            String txt = "";
            if (object != null) {
                IConverter converter =
Application.get().getConverterLocator().getConverter(object.getClass());
                txt = converter.convertToString(object,
Session.get().getLocale());
            }
            if (srch != null) {
                Pattern p = null;
                if (lastp != null && srch.equals(lastSearch) )
                    p = lastp;
                else {
                    try {
                        p = Pattern.compile(srch, Pattern.CASE_INSENSITIVE);
                    } catch (PatternSyntaxException e) {
                        if (e.getIndex()>-1) {
                            srch = srch.substring(0, e.getIndex());
                            try {
                                p = Pattern.compile(srch,
Pattern.CASE_INSENSITIVE);
                            } catch (Exception e2) {
                                srch = null;
                            }
                        } else
                            srch = null;
                    }
                }
                if (p != null) {
                    Matcher m = p.matcher(txt);
                    return m.replaceFirst("<span
class=\"search\">$0</span>");
                }
            }
            return txt;
        }

        @Override
        protected void onDetach() {
            super.onDetach();
            text = null;
            searchWord = null;
        }
    }

-- Jim Pinkham

On Thu, Oct 16, 2008 at 3:24 PM, Thomas Singer <[EMAIL PROTECTED]> wrote:

> Thanks all. The first tests work fine now, still need to handle the
> ignore-case searching...
>
> Is there anybody else interested in this feature? Then I will try to
> prepare the IResponseFilter implementation for public viewing/integration in
> Wicket.
>
>  as far as i know the response filter runs within the request cycle and
>> request cycle has metadata facility, so....
>>
>
> We already make use of RequestCycle.get(), but if you don't work on Wicket
> the full day like you, then one forgets such secrets.
>
>
> --
> Cheers,
> Tom
>
>
> Igor Vaynberg wrote:
>
>> as far as i know the response filter runs within the request cycle and
>> request cycle has metadata facility, so....
>>
>> -igor
>>
>> On Wed, Oct 15, 2008 at 10:07 AM, Thomas Singer <[EMAIL PROTECTED]> wrote:
>>
>>  Thanks. But how the filter should know about the request which contains
>>> the
>>> information about what to highlight?
>>>
>>> --
>>> Cheers,
>>> Tom
>>>
>>>
>>>
>>> Igor Vaynberg wrote:
>>>
>>>  iresponsefilter
>>>>
>>>> -igor
>>>>
>>>> On Wed, Oct 15, 2008 at 8:22 AM, Thomas Singer <[EMAIL PROTECTED]>
>>>> wrote:
>>>>
>>>>  OK, this looks trivial, but were should I place this code to replace
>>>> all
>>>>
>>>>> content, no matter whether it comes from a page template, border or
>>>>> fragment?
>>>>>
>>>>> --
>>>>> Cheers,
>>>>> Tom
>>>>>
>>>>>
>>>>>
>>>>> Martijn Dashorst wrote:
>>>>>
>>>>>  tekst.replaceAll(searchword, "<span class="search">" + searchword +
>>>>>
>>>>>> "</span>");
>>>>>>
>>>>>> label.setEscapeModelStrings(false);
>>>>>>
>>>>>> Martijn
>>>>>>
>>>>>> On Wed, Oct 15, 2008 at 3:56 PM, Thomas Singer <[EMAIL PROTECTED]>
>>>>>> wrote:
>>>>>>
>>>>>>  We are using Wicket as base for our website (www.syntevo.com). We
>>>>>> have
>>>>>>
>>>>>>> a
>>>>>>> simple search feature on the website, but want to extend it like it
>>>>>>> is
>>>>>>> known
>>>>>>> to work in forums: the searched words should be highlighted.
>>>>>>>
>>>>>>> Does someone already has implemented such a feature to surround plain
>>>>>>> words
>>>>>>> in the markup by a special tag?
>>>>>>>
>>>>>>> --
>>>>>>> Cheers,
>>>>>>> Tom
>>>>>>>
>>>>>>> ---------------------------------------------------------------------
>>>>>>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>>>>>>> For additional commands, e-mail: [EMAIL PROTECTED]
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>  ---------------------------------------------------------------------
>>>>>>
>>>>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>>>>> For additional commands, e-mail: [EMAIL PROTECTED]
>>>>>
>>>>>
>>>>>
>>>>>  ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>>> For additional commands, e-mail: [EMAIL PROTECTED]
>>>
>>>
>>>
>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>

Reply via email to