Package com.nexirius.ulc.ulcviewer.viewer

Source Code of com.nexirius.ulc.ulcviewer.viewer.ArrayULCViewer$MyTableSelectionListener

//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.ulc.ulcviewer.viewer;

import com.nexirius.framework.datamodel.*;
import com.nexirius.ulc.ulcviewer.ULCViewer;
import com.nexirius.ulc.ulcviewer.ULCViewerFactory;
import com.ulcjava.base.application.ULCComponent;
import com.ulcjava.base.application.ULCTable;
import com.ulcjava.base.application.ULCScrollPane;
import com.ulcjava.base.application.event.IListSelectionListener;
import com.ulcjava.base.application.event.ListSelectionEvent;
import com.ulcjava.base.application.table.AbstractTableModel;

public class ArrayULCViewer implements ULCViewer {

    private ULCComponent component;
    private ArrayModel model;
    private DataModelListener arrayListener = new ArrayListener();
    private ArrayTableModel arrayTableModel;
    protected ULCTable table;

    public ULCComponent getULCComponent(ULCViewerFactory factory) {
        if (component != null) {
            return component;
        }

        arrayTableModel = new ArrayTableModel(factory);
        table = new ULCTable(arrayTableModel);
        component = new ULCScrollPane(table);
        table.getSelectionModel().addListSelectionListener(new MyTableSelectionListener());

        return component;
    }

    public boolean isEditor() {
        return false;
    }

    public void updateModel() throws Exception {

    }

    public void setDataModel(Viewable model) {
        this.model = (ArrayModel) model;
        this.model.addSoftDataModelListener(arrayListener);
    }

    public Viewable getDataModel() {
        return model;
    }

    private class ArrayTableModel extends AbstractTableModel {
        private ULCViewerFactory factory;

        public ArrayTableModel(ULCViewerFactory factory) {
            this.factory = factory;
        }

        public Object getValueAt(int row, int column) {
            FieldName fieldName = model.getMemberFieldNames().getChildren().getFieldName(column);
            return model.getDataModel(row).getChildText(fieldName.toString());
        }

        public int getColumnCount() {
            return model.getMemberFieldNames().getChildren().size();
        }

        public int getRowCount() {
            return model.getSize();
        }

        public String getColumnName(int column) {
            return factory.getClientResource().getLabel(model.getMemberFieldNames().getChildren().getFieldName(column).toString());
        }
    }

    private class ArrayListener extends DataModelAdaptor {
        public void dataModelChangeValue(DataModelEvent event) {
            System.out.println("event = " + event);
            if (event.getId() == DataModelEvent.CHILD_VALUE_CHANGE) {
                try {
                    event.getTransmitterVector().lastItem();
                    int childIndex = model.getChildIndex(event.getTransmitterVector().previousItem());
                    arrayTableModel.fireTableRowsUpdated(childIndex, childIndex);
                } catch (Throwable e) {
                    e.printStackTrace();
                    arrayTableModel.fireTableDataChanged();
                }
            } else if (event.getSubtype() == DataModelEvent.SUBTYPE_CLEAR) {
                arrayTableModel.fireTableDataChanged();
            } else if (event.getSubtype() == DataModelEvent.SUBTYPE_ITEM_ADDED) {
                arrayTableModel.fireTableRowsInserted(event.getIndex(), event.getIndex());
            } else if (event.getSubtype() == DataModelEvent.SUBTYPE_ITEM_REMOVED) {
                arrayTableModel.fireTableRowsDeleted(event.getIndex(), event.getIndex());
            } else {
                arrayTableModel.fireTableDataChanged();
            }
        }

        public void dataModelEdit(DataModelEvent event) {
            if (event.getId() == DataModelEvent.HIGHLIGHT_CHANGED) {
                table.getSelectionModel().setSelectionInterval(model.getHighlightedItemIndex(), model.getHighlightedItemIndex());
            }
        }
    }

    private class MyTableSelectionListener implements IListSelectionListener {
        public void valueChanged(ListSelectionEvent event) {
            model.setHighlightedItem(table.getSelectedRow());
        }
    }
}
TOP

Related Classes of com.nexirius.ulc.ulcviewer.viewer.ArrayULCViewer$MyTableSelectionListener

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.