Package de.FeatureModellingTool.ElementInfoEditor

Source Code of de.FeatureModellingTool.ElementInfoEditor.FeatureInfoEditorImplementation$FeatureDescriptionPanel

package de.FeatureModellingTool.ElementInfoEditor;

import de.reuse.ContextImplementation;
import de.reuse.Context;
import de.FeatureModellingTool.FeatureModel.*;
import de.FeatureModellingTool.GraphicalEditor.GraphicalEditor;

import javax.swing.*;
import javax.swing.text.PlainDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.awt.*;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.TreeMap;
import java.util.Map;
import java.util.Iterator;
import java.util.ArrayList;

import research.DrawingEditor;

/**
* author: zhangwei
* Date: 2003-6-6
* Time: 16:46:31
*/
class FeatureInfoEditorImplementation extends JPanel implements FeatureInfoEditor, FeatureInfoEditorUI, ConstantDefinition {

    protected FeatureModel featureModel = null;
    protected FeatureEditor featureEditor = null;
    protected FeatureModelEditor featureModelEditor = null;
    protected FeatureRelationManager featureRelationManager = null;

    protected GraphicalEditor graphicalEditor = null;

    protected final ContextImplementation context = new ContextImplementation();

    protected final FigureSelectionChangeListener figureSelectionChangeListener = new FigureSelectionChangeListener();

    protected final CurrentViewChangeListener currentViewChangeListener = new CurrentViewChangeListener();

    protected final FeatureModelPropertyChangeListener featureModelPropertyChangeListener = new FeatureModelPropertyChangeListener();

    public Action getEntryAction(){
        return null;
    }

    public Context getContext() {
        return context;
    }

    protected class ContextChangeListener implements PropertyChangeListener {
        public void propertyChange(PropertyChangeEvent e) {
            contextChange(e);
        }
    }

    protected void contextChange(PropertyChangeEvent e) {
        String propertyName = e.getPropertyName();

        if (FEATURE_MODEL.equals(propertyName)) {
            if (featureModel != null)
                featureModel.removePropertyChangeListener(featureModelPropertyChangeListener);

            featureModel = (FeatureModel) e.getNewValue();

            if (featureModel != null)
                featureModel.addPropertyChangeListener(featureModelPropertyChangeListener);

        } else if (FEATURE_EDITOR.equals(propertyName)) {
            featureEditor = (FeatureEditor) e.getNewValue();
        } else if (FEATURE_MODEL_EDITOR.equals(propertyName)) {
            featureModelEditor = (FeatureModelEditor) e.getNewValue();
        } else if (GRAPHICAL_EDITOR.equals(propertyName)) {
            if (graphicalEditor != null) {
                graphicalEditor.removePropertyChangeListener(DrawingEditor.SELECTION_CHANGED, figureSelectionChangeListener);
                graphicalEditor.removePropertyChangeListener(DrawingEditor.CURRENT_VIEW_CHANGED, currentViewChangeListener);
            }
            graphicalEditor = (GraphicalEditor) e.getNewValue();
            if (graphicalEditor != null) {
                graphicalEditor.addPropertyChangeListener(DrawingEditor.SELECTION_CHANGED, figureSelectionChangeListener);
                graphicalEditor.addPropertyChangeListener(DrawingEditor.CURRENT_VIEW_CHANGED, currentViewChangeListener);
            }
        } else if (FEATURE_RELATION_MANAGER.equals(propertyName)) {
            featureRelationManager = (FeatureRelationManager) e.getNewValue();
        } else if (CONTAINER.equals(propertyName)) {
            container = (Component) e.getNewValue();
        }

    }

    protected class CurrentViewChangeListener implements PropertyChangeListener {
        public void propertyChange(PropertyChangeEvent e) {
            String propertyName = e.getPropertyName();

            if (DrawingEditor.CURRENT_VIEW_CHANGED.equals(propertyName)) {
                //System.out.println("DrawingEditor.CURRENT_VIEW_CHANGED");
                updateContent(graphicalEditor.getSelection());
            }
        }
    }

    protected class FigureSelectionChangeListener implements PropertyChangeListener {
        public void propertyChange(PropertyChangeEvent e) {
            String propertyName = e.getPropertyName();

            if (DrawingEditor.SELECTION_CHANGED.equals(propertyName)) {
                ArrayList selection = (ArrayList) e.getNewValue();
                //System.out.println("DrawingEditor.SELECTION_CHANGED");
                updateContent(selection);
            }
        }
    }

    protected void updateContent(ArrayList selection) {
        if ((selection == null) || (selection.size() <= 0)) {
            setFeature(null);
            return;
        }

        if (featureModel == null) {
            setFeature(null);
            return;
        }

        String id = null;
        int num = 0;
        for (int i = 0; i < selection.size(); i++) {
            String aID = (String) selection.get(i);
            if (featureModel.containsFeature(aID)) {
                id = aID;
                num++;
            }
        }

        if (num == 1)
            setFeature(featureModel.getFeature(id));
        else
            setFeature(null);
    }

    public JComponent getEditorComponent() {
        return this;
    }

    ///////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////

    protected JPanel basicAttributePanel = null;

    protected FeatureIDField id = null;
    protected FeatureNameField name = null;
    protected FeatureAttributeComboBox category = null;
    protected FeatureAttributeComboBox variability = null;
    protected FeatureAttributeComboBox bindTime = null;

    protected FeatureDescriptionPanel descriptionPanel = null;

    protected JPanel requirePanel = null;
    protected JPanel mutexPanel = null;

    protected JList require = null;
    protected AddButton addRequire = null;
    protected RemoveButton removeRequire = null;

    protected JList mutex = null;
    protected AddButton addMutex = null;
    protected RemoveButton removeMutex = null;

    protected DefaultListModel requireModel = null;
    protected DefaultListModel mutexModel = null;

    protected JScrollPane featureListScrollPane = null;
    protected JList featureList = null;
    protected DefaultListModel featureListModel = null;

    protected JPanel regularPane = null;

    private Component container = null;

    //private final EventListenerList listenerList = new EventListenerList();

    private ItemListener variabilityComboBoxListener = new InerItemListener();

    protected Feature feature = null;

    protected TreeMap editMemory = new TreeMap();

    private Color background = new Color(236, 233, 216);

    public FeatureInfoEditorImplementation() {

        context.addContextChangeListener(new ContextChangeListener());

        setLayout(new BorderLayout());
        setFont(new Font("Dialog", Font.PLAIN, 12));

        init();

        assembleGUIComponent();

        setEditable(false);

        int minWidth = this.getMinimumSize().width;
        this.setMinimumSize(new Dimension(minWidth, 170));
        this.setPreferredSize(this.getMinimumSize());
        this.setMaximumSize(this.getMinimumSize());

    }

    /**
     ** This function is added because of a fault of the interface of Feature.
     ** In the Feature, it only can get a feature's ID, such as function "getRequireFeatureIDSet", but
     ** in FeatureInfoPanel2��a feature's name should be display to users,not the feature ID. Only the
     ** FeatureModel has the fucntion that translates a feature's ID to its name.
     **/

    private Component getContainer() {
        return container;
    }

    private Feature getCurrentFeature() {
        return feature;
    }

    //��ʼ����Ϣ����и���GUI����
    protected void init() {
        id = new FeatureIDField();
        name = new FeatureNameField();
        category = new FeatureAttributeComboBox(FeatureProperties.CATEGORY, FeatureCategory.getAllTypeName());
        variability = new FeatureAttributeComboBox(FeatureProperties.VARIABILITY, Variability.getAllTypeName());
        bindTime = new FeatureAttributeComboBox(FeatureProperties.BINDINGTIME, BindingTime.getAllTypeName());

        variability.addItemListener(variabilityComboBoxListener);

        requireModel = new DefaultListModel();
        mutexModel = new DefaultListModel();

        require = new JList(requireModel);
        addRequire = new AddButton("�����������", requireModel, FeatureRelationProperties.REQUIRE);
        removeRequire = new RemoveButton("ɾ����������", requireModel, require.getSelectionModel(), FeatureRelationProperties.REQUIRE);

        mutex = new JList(mutexModel);
        addMutex = new AddButton("��ӻ�������", mutexModel, FeatureRelationProperties.EXCLUDE);
        removeMutex = new RemoveButton("ɾ����������", mutexModel, mutex.getSelectionModel(), FeatureRelationProperties.EXCLUDE);

        //featureList provides users a way to select feature in feature model from GUI directly.
        //A aspect is that features may be added to and removed from feature model at any time.
        //Because of that, featureList should listen to the featureAdded or featureRemoved events.
        //In current time, the implemantation is only reconstruct featureList before it dispalys on the screen.
        //In the near feature, it will be improved.

        featureListModel = new DefaultListModel();
        featureList = new JList(featureListModel);
        featureListScrollPane = new JScrollPane(featureList);

        featureListScrollPane.setPreferredSize(new Dimension(300, 150));

    }

    //��װ��Ϣ����еĸ���GUI����
    protected void assembleGUIComponent() {
        createBasicAttributePanel();

        createDescriptionPanel();

        createRequirePanel();
        createMutexPanel();

        createRegularPane();

        add(regularPane, BorderLayout.CENTER);

    }

    protected void createBasicAttributePanel() {
        basicAttributePanel = new JPanel();

        GridBagLayout gbl = new GridBagLayout();

        GridBagConstraints gbc = new GridBagConstraints();

        basicAttributePanel.setLayout(gbl);

        //row 0
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.gridwidth = 1;
        basicAttributePanel.add(Box.createHorizontalStrut(10), gbc);
        basicAttributePanel.add(new JLabel("ID"), gbc);
        basicAttributePanel.add(Box.createHorizontalStrut(15), gbc);
        basicAttributePanel.add(id, gbc);
        basicAttributePanel.add(Box.createHorizontalStrut(10), gbc);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 1.0;
        basicAttributePanel.add(Box.createHorizontalGlue(), gbc);

        gbc.weightx = 0;
        basicAttributePanel.add(Box.createVerticalStrut(7), gbc);

        //row 1
        gbc.gridwidth = 1;
        basicAttributePanel.add(Box.createHorizontalStrut(10), gbc);
        basicAttributePanel.add(new JLabel("����"), gbc);
        basicAttributePanel.add(Box.createHorizontalStrut(15), gbc);
        basicAttributePanel.add(name, gbc);
        basicAttributePanel.add(Box.createHorizontalStrut(10), gbc);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 1.0;
        basicAttributePanel.add(Box.createHorizontalGlue(), gbc);

        gbc.weightx = 0;
        basicAttributePanel.add(Box.createVerticalStrut(5), gbc);

        //row 2
        gbc.gridwidth = 1;
        basicAttributePanel.add(Box.createHorizontalStrut(10), gbc);
        basicAttributePanel.add(new JLabel("����"), gbc);
        basicAttributePanel.add(Box.createHorizontalStrut(15), gbc);
        basicAttributePanel.add(category, gbc);
        basicAttributePanel.add(Box.createHorizontalStrut(10), gbc);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 1.0;
        basicAttributePanel.add(Box.createHorizontalGlue(), gbc);

        gbc.weightx = 0;
        basicAttributePanel.add(Box.createVerticalStrut(5), gbc);

        //row 3
        gbc.gridwidth = 1;
        basicAttributePanel.add(Box.createHorizontalStrut(10), gbc);
        basicAttributePanel.add(new JLabel("�仯����"), gbc);
        basicAttributePanel.add(Box.createHorizontalStrut(15), gbc);
        basicAttributePanel.add(variability, gbc);
        basicAttributePanel.add(Box.createHorizontalStrut(10), gbc);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 1.0;
        basicAttributePanel.add(Box.createHorizontalGlue(), gbc);

        gbc.weightx = 0;
        basicAttributePanel.add(Box.createVerticalStrut(5), gbc);

        //row 4
        gbc.gridwidth = 1;
        basicAttributePanel.add(Box.createHorizontalStrut(10), gbc);
        basicAttributePanel.add(new JLabel("��ʱ��"), gbc);
        basicAttributePanel.add(Box.createHorizontalStrut(15), gbc);
        basicAttributePanel.add(bindTime, gbc);
        basicAttributePanel.add(Box.createHorizontalStrut(10), gbc);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 1.0;
        basicAttributePanel.add(Box.createHorizontalGlue(), gbc);

        gbc.weightx = 0;
        basicAttributePanel.add(Box.createVerticalStrut(5), gbc);

        int minWidth = basicAttributePanel.getMinimumSize().width;
        Dimension preferredSize = basicAttributePanel.getPreferredSize();
        Dimension maximumSize = basicAttributePanel.getMaximumSize();

        preferredSize.width = minWidth;
        maximumSize.width = minWidth;

        basicAttributePanel.setPreferredSize(preferredSize);
        basicAttributePanel.setMaximumSize(maximumSize);
    }

    protected void createDescriptionPanel() {
        descriptionPanel = new FeatureDescriptionPanel();
    }

    protected void createRequirePanel() {
        DefaultListModel listModel = (DefaultListModel) (require.getModel());

        requirePanel = new JPanel();
        requirePanel.setLayout(new BorderLayout());

        JScrollPane scrollPane;
        scrollPane = new JScrollPane(require);
        requirePanel.add(scrollPane, BorderLayout.CENTER);

        JPanel requireControlPane;
        requireControlPane = new JPanel();
        requireControlPane.setLayout(new FlowLayout());

        //requireControlPane.add(addRequire);
        //requireControlPane.add(removeRequire);

        requirePanel.add(requireControlPane, BorderLayout.SOUTH);
    }

    protected void createMutexPanel() {
        DefaultListModel listModel = (DefaultListModel) (mutex.getModel());

        mutexPanel = new JPanel();
        mutexPanel.setLayout(new BorderLayout());

        JScrollPane scrollPane;
        scrollPane = new JScrollPane(mutex);
        //scrollPane.setMinimumSize(new Dimension(200,100));
        //scrollPane.setPreferredSize(scrollPane.getMinimumSize());
        mutexPanel.add(scrollPane, BorderLayout.CENTER);

        JPanel mutexControlPane;
        mutexControlPane = new JPanel();
        mutexControlPane.setLayout(new FlowLayout());

        //mutexControlPane.add(addMutex);
        //mutexControlPane.add(removeMutex);

        mutexPanel.add(mutexControlPane, BorderLayout.SOUTH);
    }

    protected void createRegularPane() {

        basicAttributePanel.setBorder(BorderFactory.createTitledBorder("��������--basic"));
        descriptionPanel.setBorder(BorderFactory.createTitledBorder("��������--description"));
        requirePanel.setBorder(BorderFactory.createTitledBorder("��������--require"));
        mutexPanel.setBorder(BorderFactory.createTitledBorder("��������--mutex"));

        JPanel cons = new JPanel();
        cons.setBorder(BorderFactory.createEmptyBorder());
        cons.setLayout(new GridLayout(1,2));
        cons.add(requirePanel);
        cons.add(mutexPanel);
        //cons.setLayout(new BoxLayout(cons, BoxLayout.X_AXIS));
        //cons.add(requirePanel);
        //cons.add(mutexPanel);
        //cons.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
        //cons.setLeftComponent(requirePanel);
        //cons.setRightComponent(mutexPanel);
        //cons.setDividerSize(4);
        //cons.setDividerLocation(0.5);

        JTabbedPane two = new JTabbedPane(JTabbedPane.RIGHT);
        two.addTab("��������", descriptionPanel);
        two.addTab("Լ����ϵ", cons);

        regularPane = new JPanel();
        regularPane.setLayout(new BoxLayout(regularPane, BoxLayout.X_AXIS));
        regularPane.add(basicAttributePanel);
        regularPane.add(two);

    }


    protected void setEnableFeaturePropertyChangeEvent(boolean option) {
        name.setEnableFeaturePropertyChangeEvent(option);
        category.setEnableFeaturePropertyChangeEvent(option);
        variability.setEnableFeaturePropertyChangeEvent(option);
        bindTime.setEnableFeaturePropertyChangeEvent(option);

        descriptionPanel.setEnableFeaturePropertyChangeEvent(option);

        addRequire.setEnableFeaturePropertyChangeEvent(option);
        removeRequire.setEnableFeaturePropertyChangeEvent(option);
        addMutex.setEnableFeaturePropertyChangeEvent(option);
        removeMutex.setEnableFeaturePropertyChangeEvent(option);
    }


    protected void setEditable(boolean option) {
        id.setEditable(option);
        name.setEditable(option);
        category.setEnabled(option);
        variability.setEnabled(option);

        if (option) {
            if (!variability.getSelectedItem().equals(Variability.Mandatory.getName()))
                bindTime.setEnabled(true);
            else{
                bindTime.setEnabled(false);
            }
        } else {
            bindTime.setEnabled(false);
        }

        descriptionPanel.setEditable(option);

        addRequire.setEnabled(option);
        removeRequire.setEnabled(option);
        addMutex.setEnabled(option);
        removeMutex.setEnabled(option);

        require.setEnabled(option);
        mutex.setEnabled(option);

        if (option) {
            require.setBackground(Color.white);
            mutex.setBackground(Color.white);
        } else {
            require.setBackground(background);
            mutex.setBackground(background);
        }
    }


    //������Ϣ���ĵ�ǰ����
    public void setFeature(Feature feature) {
        if (this.feature == feature)
            return;

        setEnableFeaturePropertyChangeEvent(false);

        Feature old = this.feature;
        this.feature = feature;

        //store old feature's description attribute if it is editing now
        if ((old != null) && descriptionPanel.isEditing()) {
            FeatureEditStateInfo memoryInfo = (FeatureEditStateInfo) editMemory.get(old.getID());

            if (memoryInfo != null) {
                memoryInfo.setCurrentDescription(descriptionPanel.getUnsubmitDescription());
            } else {
                FeatureEditStateInfo info = new FeatureEditStateInfo(old.getID(), descriptionPanel.getUnsubmitDescription(), true);
                editMemory.put(old.getID(), info);
            }
        }

        //remove old feature's require and mutex infomation from constraints panel
        if (old != null) {
            requireModel.removeAllElements();
            mutexModel.removeAllElements();
        }

        //display current feature's infomation
        if (feature != null) {
            FeatureEditStateInfo memoryInfo = (FeatureEditStateInfo) editMemory.get(feature.getID());

            if (memoryInfo == null) {
                id.setText(feature.getID());
                name.setText(feature.getName());
                category.setSelectedItem(feature.getCategory().getName());
                variability.setSelectedItem(feature.getVariability().getName());
                bindTime.setSelectedItem(feature.getBindingTime().getName());
                descriptionPanel.setDescription(feature.getDescription());

                if (old == null)
                    setEditable(true);

                descriptionPanel.setEditing(false);

            } else {
                id.setText(feature.getID());
                name.setText(feature.getName());
                category.setSelectedItem(feature.getCategory().getName());
                variability.setSelectedItem(feature.getVariability().getName());
                bindTime.setSelectedItem(feature.getBindingTime().getName());
                descriptionPanel.setDescription(feature.getDescription());

                descriptionPanel.setUnsubmitDescription(memoryInfo.getCurrentDescription());

                if (old == null)
                    setEditable(true);

                descriptionPanel.setEditing(true);

            }

            //add current feature's require and mutex infomation to the constraints panel

            //Set requireFSet = feature.getRequireFeatureIDSet();
            //Set mutexFSet = feature.getMutexFeatureIDSet();

            Map requireRelations = featureModel.getFeatureRelation(FeatureRelationProperties.REQUIRE, feature, true);
            Map mutesRelations = featureModel.getFeatureRelation(FeatureRelationProperties.EXCLUDE, feature);

            Iterator requireFI = null;
            Iterator mutexFI = null;

            if (requireRelations != null)
                requireFI = requireRelations.keySet().iterator();
            if (mutesRelations != null)
                mutexFI = mutesRelations.keySet().iterator();

            //if ((requireFI != null) || (mutexFI != null)) {
            //    allFeatures = featureModel.getAllFeature();
            //}

            if (requireFI != null) {
                for (; requireFI.hasNext();) {
                    String id = (String) requireFI.next();
                    FeatureRelation fr = (FeatureRelation) requireRelations.get(id);

                    if (fr != null) {
                        requireModel.addElement(fr.getEndFeature());
                    }
                }
            }

            if (mutexFI != null) {
                for (; mutexFI.hasNext();) {
                    String id = (String) mutexFI.next();
                    FeatureRelation fr = (FeatureRelation) mutesRelations.get(id);

                    if (fr != null) {
                        Feature start = fr.getStartFeature();
                        Feature end = fr.getEndFeature();

                        if (start != feature)
                            mutexModel.addElement(start);
                        else
                            mutexModel.addElement(end);
                    }
                }
            }

            setEnableFeaturePropertyChangeEvent(true);
        } else {
            setEditable(false);
            id.setText("");
            name.setText("");
            category.setSelectedItem(FeatureCategory.Null.getName());
            variability.setSelectedItem(Variability.Null.getName());
            bindTime.setSelectedItem(BindingTime.Null.getName());
            descriptionPanel.setDescription("");
        }
    }

    protected class FeatureModelPropertyChangeListener implements PropertyChangeListener {

        public void propertyChange(PropertyChangeEvent evt) {

            Object src = evt.getSource();

            if (!(src instanceof Feature))
                return;

            Feature target = (Feature) src;

            if ((feature == null) || !feature.getID().equals(target.getID()))
                return;

            String propertyName = evt.getPropertyName();

            if (propertyName.equals(FeatureProperties.NAME)) {

                String currentName = name.getText();
                if (currentName.equals(feature.getName()))
                    return;

                setEnableFeaturePropertyChangeEvent(false);
                name.setText(feature.getName());
                setEnableFeaturePropertyChangeEvent(true);

            } else if (propertyName.equals(FeatureProperties.CATEGORY)) {
                String currentCategory = (String) category.getSelectedItem();

                if (currentCategory.equals(feature.getCategory().getName())) {
                    return;
                }

                setEnableFeaturePropertyChangeEvent(false);
                category.setSelectedItem(feature.getCategory().getName());
                setEnableFeaturePropertyChangeEvent(true);

            } else if (propertyName.equals(FeatureProperties.VARIABILITY)) {
                String currentVariability = (String) variability.getSelectedItem();

                if (currentVariability.equals(feature.getVariability().getName())) {
                    return;
                }

                setEnableFeaturePropertyChangeEvent(false);
                variability.setSelectedItem(feature.getVariability().getName());
                setEnableFeaturePropertyChangeEvent(true);

            } else if (propertyName.equals(FeatureProperties.BINDINGTIME)) {
                String currentBindTime = (String) bindTime.getSelectedItem();

                if (currentBindTime.equals(feature.getBindingTime().getName())) {
                    return;
                }

                setEnableFeaturePropertyChangeEvent(false);
                bindTime.setSelectedItem(feature.getBindingTime().getName());
                setEnableFeaturePropertyChangeEvent(true);
            } else if (propertyName.equals(FeatureProperties.DESCRIPTION)) {
                String currentDescription = descriptionPanel.getDescription();

                if (currentDescription.equals(feature.getDescription())) {
                    return;
                }

                setEnableFeaturePropertyChangeEvent(false);
                descriptionPanel.setDescription(feature.getDescription());
                setEnableFeaturePropertyChangeEvent(true);

            } else {
                System.out.println("In current time, no process prepared for property " + evt.getPropertyName() + "change event");
            }
        }
    }

    public void addFeaturePropertyChangeListener(PropertyChangeListener listener) {
        listenerList.add(PropertyChangeListener.class, listener);
    }

    public void removeFeaturePropertyChangeListener(PropertyChangeListener listener) {
        listenerList.remove(PropertyChangeListener.class, listener);
    }

    protected void fireFeaturePropertyChangeEvent(PropertyChangeEvent event) {
        final Object[] listeners = listenerList.getListenerList();
        PropertyChangeListener listener = null;
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
            if (listeners[i] == PropertyChangeListener.class) {
                listener = (PropertyChangeListener) listeners[i + 1];
                listener.propertyChange(event);
            }
        }
    }

    private class InerItemListener implements ItemListener {
        public void itemStateChanged(ItemEvent event) {
            if (event.getStateChange() == ItemEvent.SELECTED) {
                String item = (String) event.getItem();

                if (item.equals(Variability.Mandatory.getName())
                  ||item.equals(Variability.Null.getName())) {
                    bindTime.setEnabled(false);
                } else {
                    bindTime.setEnabled(true);
                }
            }
        }
    };


    private class FeatureIDField extends JTextField {

        boolean editFlag = false;

        class ReadOnlyDocument extends PlainDocument {
            public void insertString(int offset, String s, AttributeSet as)
                    throws BadLocationException {
                if (editFlag)
                    super.insertString(offset, s, as);
            }

            public void remove(int offs, int len) throws BadLocationException{
                if (editFlag)
                    super.remove(offs, len);
            }
        }

        public FeatureIDField() {
            super(15);

            FeatureIDField.this.setMargin(new Insets(0, 3, 0, 0));
            FeatureIDField.this.setMinimumSize(FeatureIDField.this.getPreferredSize());
            FeatureIDField.this.setDocument(new ReadOnlyDocument());
            closeEdit();

        }

        protected void openEdit() {
            editFlag = true;
        }

        protected void closeEdit() {
            editFlag = false;
        }

        public void setText(String text) {
            openEdit();
            super.setText(text);
            closeEdit();
        }

    }

    private class FeatureNameField extends JTextField {
        protected DocumentListener fnListener = new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                updateModel();
            }

            public void removeUpdate(DocumentEvent e) {
                updateModel();
            }

            public void changedUpdate(DocumentEvent e) {
                updateModel();
            }
        };

        protected boolean enableFeaturePropertyChangeEvent = false;

        public FeatureNameField() {
            super(15);

            FeatureNameField.this.setMargin(new Insets(0, 3, 0, 0));

            FeatureNameField.this.setMinimumSize(FeatureNameField.this.getPreferredSize());

            FeatureNameField.this.getDocument().addDocumentListener(fnListener);
        }

        public void setEnableFeaturePropertyChangeEvent(boolean option) {
            if (option != enableFeaturePropertyChangeEvent) {
                enableFeaturePropertyChangeEvent = option;
            }
        }

        private void updateModel() {
            if (enableFeaturePropertyChangeEvent == false)
                return;

            if (featureEditor != null) {
                featureEditor.setName(feature, FeatureNameField.this.getText());
            }
        }

    }

    private class FeatureAttributeComboBox extends JComboBox {
        protected ItemListener inerListener = new ItemListener() {
            public void itemStateChanged(ItemEvent event) {
                if (event.getStateChange() == ItemEvent.SELECTED) {
                    //String item = (String) event.getItem();
                    //dispatchEvent(item);
                    updateModel();
                }
            }
        };

        protected String propertyName = null;

        protected boolean enableFeaturePropertyChangeEvent = false;

        public FeatureAttributeComboBox(String propertyName, final Object items[]) {
            super(items);
            this.propertyName = propertyName;

            addItemListener(inerListener);
        }

        public void setEnableFeaturePropertyChangeEvent(boolean option) {
            if (option != enableFeaturePropertyChangeEvent) {
                enableFeaturePropertyChangeEvent = option;
            }
        }

        protected void updateModel() {
            if (enableFeaturePropertyChangeEvent == false)
                return;

            if (featureEditor != null) {
                String attName = (String) this.getSelectedItem();

                if (propertyName.equals(FeatureProperties.CATEGORY)) {
                    featureEditor.setCategory(feature, FeatureCategory.getInstance(attName));
                } else if (propertyName.equals(FeatureProperties.VARIABILITY)) {
                    featureEditor.setVariability(feature, Variability.getInstance(attName));
                } else if (propertyName.equals(FeatureProperties.BINDINGTIME)) {
                    featureEditor.setBindingTime(feature, BindingTime.getInstance(attName));
                }

            }
        }

    }


    private class FeatureDescriptionPanel extends JPanel {

        protected JTextArea descriptionArea = null;

        protected JButton start = null;
        protected JButton conform = null;
        protected JButton cancel = null;

        protected JScrollPane scrollPane = null;
        protected JPanel controlPane = null;

        protected String description = "";

        protected boolean enableFeaturePropertyChangeEvent = false;

        protected boolean editable = false;


        public void setEnableFeaturePropertyChangeEvent(boolean option) {
            if (option != enableFeaturePropertyChangeEvent) {
                enableFeaturePropertyChangeEvent = option;
            }
        }

        public boolean isEditing() {
            return !start.isEnabled();
        }

        public void setEditing(boolean editing) {
            if (!isEditable())
                return;

            if (editing == true) {
                start.setEnabled(false);
                conform.setEnabled(true);
                cancel.setEnabled(true);
                descriptionArea.setEditable(true);
                descriptionArea.setBackground(Color.white);
            } else {
                start.setEnabled(true);
                conform.setEnabled(false);
                cancel.setEnabled(false);
                descriptionArea.setEditable(false);
                descriptionArea.setBackground(background);
            }
        }

        public boolean isEditable() {
            return editable;
        }

        public void setEditable(boolean option) {
            editable = option;

            if (option == true) {
                start.setEnabled(true);
                descriptionArea.setEditable(false);
                descriptionArea.setBackground(background);
            } else {
                start.setEnabled(false);
                conform.setEnabled(false);
                cancel.setEnabled(false);

                descriptionArea.setEditable(false);
                descriptionArea.setBackground(background);
            }


        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;

            setUnsubmitDescription(description);

        }

        public void setUnsubmitDescription(String description) {
            descriptionArea.setText(description);
        }

        public String getUnsubmitDescription() {
            return descriptionArea.getText();
        }

        public FeatureDescriptionPanel() {
            //1
            descriptionArea = new JTextArea(6, 45);
            scrollPane = new JScrollPane();

            descriptionArea.setLineWrap(true);
            descriptionArea.setWrapStyleWord(false);

            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            scrollPane.setViewportView(descriptionArea);

            //2
            start = new JButton("��ʼ�༭");
            conform = new JButton("�ύ����");
            cancel = new JButton("ȡ���༭");

            controlPane = new JPanel();
            controlPane.setLayout(new FlowLayout());

            controlPane.add(start);
            controlPane.add(conform);
            controlPane.add(cancel);

            //3
            setLayout(new BorderLayout());

            add(scrollPane, BorderLayout.CENTER);
            add(controlPane, BorderLayout.SOUTH);

            //4
            descriptionArea.setEditable(false);
            descriptionArea.setBackground(background);

            start.setEnabled(false);
            conform.setEnabled(false);
            cancel.setEnabled(false);

            start.addActionListener(
                    new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            start.setEnabled(false);

                            conform.setEnabled(true);
                            cancel.setEnabled(true);

                            descriptionArea.setBackground(Color.white);
                            descriptionArea.setEditable(true);
                        }
                    }
            );

            conform.addActionListener(
                    new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            conform.setEnabled(false);
                            cancel.setEnabled(false);


                            descriptionArea.setEditable(false);
                            descriptionArea.setBackground(background);

                            description = descriptionArea.getText();

                            //fire description change event
                            //dispatchEvent();
                            updateModel();

                            //remove from edit mempry
                            editMemory.remove(feature.getID());

                            start.setEnabled(true);

                        }
                    }
            );


            cancel.addActionListener(
                    new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            cancel.setEnabled(false);
                            conform.setEnabled(false);

                            descriptionArea.setEditable(false);
                            descriptionArea.setBackground(background);

                            descriptionArea.setText(description);

                            //remove from edit mempry
                            editMemory.remove(feature.getID());

                            start.setEnabled(true);

                        }
                    }
            );

        }

        protected void updateModel() {
            if (enableFeaturePropertyChangeEvent == false)
                return;

            if (featureEditor != null) {
                featureEditor.setDescription(feature, description);
            }
        }

    }

    private class AddButton extends JButton {
        protected DefaultListModel model = null;

        protected ActionListener inerListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Component container = getContainer();

                if (container == null) return;

                featureListModel.removeAllElements();

                Map allFeatures = featureModel.getAllFeature();

                Iterator iterator = allFeatures.values().iterator();

                if (iterator.hasNext() == false)
                    return;

                while (iterator.hasNext()) {
                    featureListModel.addElement(iterator.next());
                }

                featureList.getSelectionModel().clearSelection();

                int result = JOptionPane.showConfirmDialog(container, featureListScrollPane, "��ѡ����������", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

                if (result == JOptionPane.OK_OPTION) {
                    ListSelectionModel selectionModel = featureList.getSelectionModel();

                    int min = selectionModel.getMinSelectionIndex();
                    int max = selectionModel.getMaxSelectionIndex();

                    if ((min != -1) && (max != -1)) {
                        for (int i = min; i <= max; i++) {
                            if (selectionModel.isSelectedIndex(i))
                                addFeature((Feature) (featureListModel.get(i)));
                        }
                    }
                }
            }
        };

        protected boolean enableFeaturePropertyChangeEvent = false;

        protected String propertyName = null;

        public AddButton(String name, DefaultListModel model, String propertyName) {
            super(name);
            AddButton.this.model = model;
            this.propertyName = propertyName;

            addActionListener(inerListener);
        }

        public void setEnableFeaturePropertyChangeEvent(boolean option) {
            if (option != enableFeaturePropertyChangeEvent) {
                enableFeaturePropertyChangeEvent = option;
            }
        }

        protected void addFeature(Feature feature) {
            if ((feature == null) || (getCurrentFeature().getID().equals(feature.getID())))
                return;

            boolean contains = false;

            for (int i = 0; i < model.getSize(); i++) {
                Feature fe = (Feature) model.get(i);
                if (fe.getID().equals(feature.getID())) {
                    contains = true;
                    break;
                }
            }

            if (!contains) {
                model.add(model.getSize(), feature);
                //dispatchEvent(feature);
                updateModel(feature);
            }
        }

        protected void updateModel(Feature feature) {
            if (enableFeaturePropertyChangeEvent == false)
                return;

            featureModelEditor.addRelation(propertyName, getCurrentFeature(), feature);
        }

    }


    private class RemoveButton extends JButton {
        protected DefaultListModel model = null;
        protected ListSelectionModel selectionModel = null;

        protected ActionListener inerListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                removeFeature();
            }
        };

        protected boolean enableFeaturePropertyChangeEvent = false;

        protected String propertyName = null;

        public RemoveButton(String name, DefaultListModel model, ListSelectionModel selectionModel, String propertyName) {
            super(name);
            this.model = model;
            this.selectionModel = selectionModel;
            this.propertyName = propertyName;

            addActionListener(inerListener);
        }

        public void setEnableFeaturePropertyChangeEvent(boolean option) {
            if (option != enableFeaturePropertyChangeEvent) {
                enableFeaturePropertyChangeEvent = option;
            }
        }

        protected void removeFeature() {
            int index = selectionModel.getMinSelectionIndex();

            if (index >= 0) {
                Feature feature = (Feature) (model.remove(index));
                updateModel(feature);
            }
        }

        protected void updateModel(Feature feature) {
            if (enableFeaturePropertyChangeEvent == false)
                return;

            if (featureRelationManager.contains(propertyName) && featureRelationManager.isSymmetric(propertyName)) {
                featureModelEditor.removeRelation(propertyName, getCurrentFeature(), feature);
                featureModelEditor.removeRelation(propertyName, feature, getCurrentFeature());
            } else {
                featureModelEditor.removeRelation(propertyName, getCurrentFeature(), feature);
            }
        }

    }

    /**
     * ��������ҪĿ���Ǽ�¼�����������Եı༭״̬��
     * ������������ͨ������Ϊһ�����֣��û��ڱ༭����������ʱ����ܻ���ʱ��ת����������Բ鿴��Ӧ����Ϣ��
     * ����б�Ҫ��¼���������ĵ�ǰ�༭״̬��������û���ɷ��С�
     *
     * Ŀǰ���������������������⣬�����������������������ƣ������仯�Եȣ��Ƿ����Щ����Ҳ��¼״̬��һ��
     * ֵ�ÿ��ǵ����⡣������Ŀǰȷʵû�м�¼��Щ���Ե�״̬�������DZ��޸�ʱֱ�ӽ����޸Ľ��д������ģ�͡�
     *
     *
     * ����Ŀǰ�Ķ��岻��֤�����ŵģ�Ŀ�Ľ�������֤ԭ��ϵͳ�Ŀ����ԡ�
     */
    protected class FeatureEditStateInfo {
        protected String featureID = null;
        protected String currentDescription = null;
        protected boolean isEditing = false;

        public FeatureEditStateInfo(String featureID, String currentDescription, boolean isEditing) {
            this.featureID = featureID;
            this.currentDescription = currentDescription;
            this.isEditing = isEditing;
        }

        public void setFeatureID(String featureID) {
            this.featureID = featureID;
        }

        public void setCurrentDescription(String currentDescription) {
            this.currentDescription = currentDescription;
        }

        public void setEditing(boolean isEditing) {
            this.isEditing = isEditing;
        }

        public String getFeatureID() {
            return featureID;
        }

        public String getCurrentDescription() {
            return currentDescription;
        }

        public boolean isEditing() {
            return isEditing;
        }
    }

}
TOP

Related Classes of de.FeatureModellingTool.ElementInfoEditor.FeatureInfoEditorImplementation$FeatureDescriptionPanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.