/*
 * Copyright 2010 JBoss Inc
 *
 * 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.drools.eclipse.dsl.editor;

import org.drools.lang.dsl.DSLMappingEntry;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;

/**
 * Content provider for the Domain Specific Language editor.
 */
public class DSLContentProvider
    implements
    IStructuredContentProvider, IMappingListViewer {

    private TableViewer viewer;
    private NLGrammarModel model;
    
    public DSLContentProvider(TableViewer viewer, NLGrammarModel model) {
        this.viewer = viewer;
        this.model = model;
    }
    
    public void dispose() {
        model.removeChangeListener( this );
    }
    
    public void inputChanged(Viewer viewer,
                             Object oldInput,
                             Object newInput) {
        if (newInput != null)
            ((NLGrammarModel) newInput).addChangeListener(this);
        if (oldInput != null)
            ((NLGrammarModel) oldInput).removeChangeListener(this);

    }

    public void addMapping(DSLMappingEntry item) {
        viewer.add( item );
        
    }

    public void removeMapping(DSLMappingEntry item) {

        viewer.remove( item );
    }

    public void updateMapping(DSLMappingEntry item) {
        viewer.update( item, null );
    }

    public Object[] getElements(Object inputElement) {
        return model.getEntries().toArray();
    }

}
