<commit_before>/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.springframework.richclient.text;

import javax.swing.JComponent;
import javax.swing.text.JTextComponent;

import org.springframework.binding.form.CommitListener;
import org.springframework.binding.form.FormModel;
import org.springframework.binding.value.ValueChangeListener;
import org.springframework.binding.value.support.CommitTrigger;
import org.springframework.richclient.form.builder.FormComponentInterceptor;
import org.springframework.richclient.form.builder.FormComponentInterceptorFactory;
import org.springframework.richclient.form.builder.support.AbstractFormComponentInterceptor;

/**
 * @author oliverh
 */
public class TextComponentPopupInterceptorFactory implements
        FormComponentInterceptorFactory {

    public TextComponentPopupInterceptorFactory() {
    }

    public FormComponentInterceptor getInterceptor(FormModel formModel) {
        return new TextComponentPopupInterceptor(formModel);
    }

    private class TextComponentPopupInterceptor extends
            AbstractFormComponentInterceptor {
        private CommitTrigger resetTrigger;
        
        protected TextComponentPopupInterceptor(FormModel formModel) {
            super(formModel);
        }

        public JComponent processComponent(String propertyName,
                JComponent component) {
            JComponent innerComp = getInnerComponent(component);
            if (innerComp instanceof JTextComponent) {
                TextComponentPopup.attachPopup((JTextComponent)innerComp,
                        getResetTrigger());
            }
            return component;
        }

        private CommitTrigger getResetTrigger() {
            if (resetTrigger == null) {
                resetTrigger = new CommitTrigger();
                registerListeners();
            }
            return resetTrigger;
        }

        private void registerListeners() {
            FormModel formModel = getFormModel();
            formModel.addCommitListener(new CommitListener() {
                public boolean preEditCommitted(Object formObject) {
                    return true;
                }

                public void postEditCommitted(Object formObject) {
                    resetTrigger.commit();
                }
            });
            formModel.addFormObjectChangeListener(new ValueChangeListener() {
                public void valueChanged() {
                    resetTrigger.commit();
                }
            });
        }
    }
}<commit_msg>Added javadocs<commit_after>/*
 * Copyright 2002-2004 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.springframework.richclient.text;

import javax.swing.*;
import javax.swing.text.JTextComponent;

import org.springframework.binding.form.CommitListener;
import org.springframework.binding.form.FormModel;
import org.springframework.binding.value.ValueChangeListener;
import org.springframework.binding.value.support.CommitTrigger;
import org.springframework.richclient.form.builder.FormComponentInterceptor;
import org.springframework.richclient.form.builder.FormComponentInterceptorFactory;
import org.springframework.richclient.form.builder.support.AbstractFormComponentInterceptor;

/**
 * Adds a right-click popup-menu to JTextComponents.
 *
 * @author oliverh
 * @see TextComponentPopup#attachPopup
 */
public class TextComponentPopupInterceptorFactory implements
    FormComponentInterceptorFactory {

    public TextComponentPopupInterceptorFactory() {
    }

    public FormComponentInterceptor getInterceptor(FormModel formModel) {
        return new TextComponentPopupInterceptor(formModel);
    }

    private class TextComponentPopupInterceptor extends
        AbstractFormComponentInterceptor {

        private CommitTrigger resetTrigger;

        protected TextComponentPopupInterceptor(FormModel formModel) {
            super(formModel);
        }

        public JComponent processComponent(String propertyName,
                                           JComponent component) {
            JComponent innerComp = getInnerComponent(component);
            if (innerComp instanceof JTextComponent) {
                TextComponentPopup.attachPopup((JTextComponent)innerComp,
                    getResetTrigger());
            }
            return component;
        }

        private CommitTrigger getResetTrigger() {
            if (resetTrigger == null) {
                resetTrigger = new CommitTrigger();
                registerListeners();
            }
            return resetTrigger;
        }

        private void registerListeners() {
            FormModel formModel = getFormModel();
            formModel.addCommitListener(new CommitListener() {
                public boolean preEditCommitted(Object formObject) {
                    return true;
                }

                public void postEditCommitted(Object formObject) {
                    resetTrigger.commit();
                }
            });
            formModel.addFormObjectChangeListener(new ValueChangeListener() {
                public void valueChanged() {
                    resetTrigger.commit();
                }
            });
        }
    }

}
{'fraction_non_alphanumeric': 0.0686011670083583, 'fraction_numerical': 0.003784891972874941, 'mean_word_length': 2.6177980604677695, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2281704', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1413, 'n_words': 385}