Package com.nexirius.framework.datamodel.persistence

Source Code of com.nexirius.framework.datamodel.persistence.PersistenceChooserModel$SelectCommand

//{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.framework.datamodel.persistence;

import com.nexirius.framework.application.DialogManager;
import com.nexirius.framework.datamodel.*;
import com.nexirius.framework.datamodel.ComboBoxModel;
import com.nexirius.framework.dataviewer.SimpleViewerCreator;
import com.nexirius.framework.dataviewer.ViewerCreator;
import com.nexirius.framework.layout.*;
import com.nexirius.util.StringVector;
import com.nexirius.util.XString;

import javax.swing.*;
import java.awt.*;

/**
* This class is used as basis to display a list of loadable elements
*
* @author Marcel Baumann
*/
public class PersistenceChooserModel extends StructModel {



    public static final String FIELDNAME_SHOW_ENVIRONMENT_SELECTOR = "showEnvironmentSelector";
    public static final String FIELDNAME_ENVIRONMENTS = "environments";
    public static final String FIELDNAME_CURRENT_ENVIRONMENT = "currentEnvironment";
    public static final String FIELDNAME_ELEMENTS = "elements";
    public static final String FIELDNAME_NAME_FILTER = "nameFilter";
    public static final String FIELDNAME_SELECTED_ELEMENT = "selectedElement";
    public static final String RELOAD_EVENT = "reload";

    DataModel arrayElementModel;
    DataModelVector directory;
    PersistenceManager manager;
    PersistenceOpenAdapter openAdapter = null;
    BooleanModel showEnvironmentSelector;
    SimpleArrayModel environments;
    ComboBoxModel currentEnvironment;
    ArrayModel elements;
    StringModel nameFilter;
    StringModel selectedElement;
    StringModel title;

    public PersistenceChooserModel(PersistenceManager manager, PersistenceOpenAdapter openAdapter, DataModel arrayElementModel, String initialEnvironment) {
        this.arrayElementModel = arrayElementModel;
        this.manager = manager;
        setOpenAdapter(openAdapter);
        init(initialEnvironment);
    }

    public void setShowEnvironment(boolean on) {
        showEnvironmentSelector.setBoolean(on);
    }

    public DataModel getHighlightedDataModel() {
        return elements.getHighlightedItem();
    }

    public void setHighlightedDataModel(String identifier) {
        if (identifier == null) {
            elements.setHighlightedItem(-1);
            return;
        }

        DataModelEnumeration e = elements.getEnumeration();
        int i = 0;

        while (e.hasMore()) {
            DataModel m = e.next();

            if (identifier.equals(m.getInstanceName())) {
                elements.setHighlightedItem(i);

                return;
            }
            ++i;
        }
    }

    public String getSelectedElement() {
        return selectedElement.getText();
    }

    public void setSelectedElement(String identifier) {
        setHighlightedDataModel(identifier);
        select();
    }

    public void setOpenAdapter(PersistenceOpenAdapter openAdapter) {
        this.openAdapter = openAdapter;
    }

    private void init(String initialEnvironment) {
        showEnvironmentSelector = new BooleanModel(true, FIELDNAME_SHOW_ENVIRONMENT_SELECTOR);
        append(showEnvironmentSelector);

        environments = new SimpleArrayModel(manager.getEnvironments().toArray(), FIELDNAME_ENVIRONMENTS);
        append(environments);

        currentEnvironment = new ComboBoxModel(0, environments, FIELDNAME_CURRENT_ENVIRONMENT);
        append(currentEnvironment);
        try {
            currentEnvironment.setText(initialEnvironment);
        } catch (Exception e) {
            //ignore
        }
        currentEnvironment.addDataModelListener(new EnvironmentListener());

        elements = new ArrayModel(arrayElementModel, "Elements");
        elements.setFieldName(FIELDNAME_ELEMENTS);
        append(elements);
        elements.addDataModelListener(new ElementSelectionListener());

        nameFilter = new StringModel("*", FIELDNAME_NAME_FILTER);
        append(nameFilter);
        nameFilter.addDataModelListener(new NameFilterListener());

        selectedElement = new StringModel("", FIELDNAME_SELECTED_ELEMENT);
        append(selectedElement);

        appendMethod(new OpenCommand());
        appendMethod(new SelectCommand());
        appendMethod(new NewCommand());
    }

    public void setEnvironment(String environmentName)
            throws Exception {
        try {
            currentEnvironment.setText(environmentName);
        } catch (Exception ex) {
            StringVector env = manager.getEnvironments();
            env.add(environmentName);
            String[] envArray = env.getArray();
            environments.setArray(envArray);
            currentEnvironment.setValueArray(environments);
            currentEnvironment.setText(environmentName);
//TODO            ex.printStackTrace();
//           throw new Exception("No such environment '" + environmentName + "'");
        }
    }

    public void loadEnvironment()
            throws Exception {
        String ce = currentEnvironment.getText();

        directory = manager.loadAll(ce);

        listElements();

        fireCallMethod(RELOAD_EVENT);
    }

    public void setFilter(String filter) {
        nameFilter.setText(filter);
    }

    public void listElements() {
        DataModel highlighted = elements.getHighlightedItem();
        String highlightedId = null;

        if (highlighted != null) {
            highlightedId = highlighted.getInstanceName();
        }

        elements.removeChildren();
        DataModelEnumeration e = directory.getEnumeration();

        while (e.hasMore()) {
            DataModel element = (DataModel) e.next();
            XString name = new XString(element.getInstanceName());

            if (name.match(nameFilter.getText())) {
                element.setParentDataModelContainer(null);
                elements.append(element);

                if (highlightedId != null && element.getInstanceName().equals(highlightedId)) {
                    elements.setHighlightedItem(elements.getSize() - 1);
                }
            }
        }
    }

    public DataModel loadElement(String identifier) throws Exception {
        return manager.load(currentEnvironment.getText(), identifier);
    }

    public void storeElement(DataModel model) throws Exception {
        manager.store(model, currentEnvironment.getText());
    }

    public ArrayModel getElements() {

        return elements;
    }

    class NameFilterListener implements DataModelListener {
        public void dataModelChangeValue(DataModelEvent event) {
            listElements();
        }

        public void dataModelChangeStructure(DataModelEvent event) {
        }

        public void dataModelGrabFocus(DataModelEvent event) {
        }
    }

    class EnvironmentListener implements DataModelListener {
        public void dataModelChangeValue(DataModelEvent event) {
            try {
                selectedElement.setText("");
                loadEnvironment();
            } catch (Exception ex) {
                ex.printStackTrace();
                throw new RuntimeException("Unexpected Exception: " + ex.getMessage());
            }
        }

        public void dataModelChangeStructure(DataModelEvent event) {
        }

        public void dataModelGrabFocus(DataModelEvent event) {
        }
    }

    class ElementSelectionListener implements DataModelListener {
        public void dataModelChangeValue(DataModelEvent event) {
        }

        public void dataModelChangeStructure(DataModelEvent event) {
        }

        public void dataModelGrabFocus(DataModelEvent event) {
            DataModel selected = event.getTransmitterVector().getItem(1);

            selectedElement.setText(selected.getInstanceName());

            open();
        }
    }

    public void open() {
        if (openAdapter != null) {
            DataModel model = getHighlightedDataModel();

            if (model != null) {
                String identifier = model.getInstanceName();

                if (identifier != null && identifier.length() > 0) {
                    openAdapter.openPersistentModel(manager, currentEnvironment.getText(), identifier);
                }
            }
        }
    }

    public String getNewPersistentElementCommand() {
        return "getNewPersistentElementCommand";
    }

    protected void select() {
        DataModel highlightedDataModel = getHighlightedDataModel();
        if (highlightedDataModel != null) {
            selectedElement.setText(highlightedDataModel.getInstanceName());
        } else {
            selectedElement.setText(null);
        }
    }

    class OpenCommand extends DefaultDataModelCommand {
        public OpenCommand() {
            super("PersistenceOpen");
        }

        public void doAction() {
            open();
        }
    }

    class SelectCommand extends DefaultDataModelCommand {
        public SelectCommand() {
            super("PersistenceSelect");
        }

        public void doAction() {
            select();
            Component window = DialogManager.getToplevelFrame();

            if (window instanceof JDialog) {
                window.setVisible(false);
            }
        }
    }

    class NewCommand extends DefaultDataModelCommand {
        public NewCommand() {
            super(getNewPersistentElementCommand());
        }

        public void doAction() {
        }
    }

    public static class LayoutWithEnvironment extends StructureLayoutItem {
        int x = 0;
        int y = 0;
        int wcell = 10;
        int hcell = 10;
        int gap = 5;
        int htotalpix;
        int wtotalpix;

        public LayoutWithEnvironment() {
            this(400, 300);
        }

        public LayoutWithEnvironment(int wtotalpix, int htotalpix) {
            this.htotalpix = htotalpix;
            this.wtotalpix = wtotalpix;

            double htotal = htotalpix / (double) (hcell + gap);
            double wtotal = wtotalpix / (double) (wcell + gap);

            double hframe = 1.0;
            double wframe = hframe;

            double hinner = htotal - hframe;
            double winner = wtotal - wframe;

            double wlabel = winner / 3.0;
            double wfield = winner - wlabel;
            double hfield = 2.0;
            double hlabel = 2.0;

            double hlist = hinner - 3.0 * hfield;
            double wlist = winner;

            ViewerCreator svc = new SimpleViewerCreator();

            MultiplePropertyEntry labelProps = new MultiplePropertyEntry();
            MultiplePropertyEntry fieldProps = new MultiplePropertyEntry();

            pos(wframe / 2.0, hframe / 2.0);
            x += gap / 2;
            y += gap / 2;
            append(new LabelLayoutItem(PersistenceChooserModel.FIELDNAME_CURRENT_ENVIRONMENT, x, y, w(wlabel), h(hlabel), labelProps));
            right(wlabel);
            append(new StructureFieldLayout(PersistenceChooserModel.FIELDNAME_CURRENT_ENVIRONMENT, x, y, w(wfield), h(hfield), null, null, false, null, fieldProps));

            left(wlabel);
            down(hlabel);
            append(new StructureFieldLayout(PersistenceChooserModel.FIELDNAME_ELEMENTS, x, y, w(wlist), h(hlist), null, null, false, null, fieldProps));

            down(hlist);
            append(new LabelLayoutItem(PersistenceChooserModel.FIELDNAME_SELECTED_ELEMENT, x, y, w(wlabel), h(hlabel), labelProps));
            right(wlabel);
            append(new StructureFieldLayout(PersistenceChooserModel.FIELDNAME_SELECTED_ELEMENT, x, y, w(wfield), h(hfield), null, null, false, null, fieldProps));

            left(wlabel);
            down(hlabel);
            append(new LabelLayoutItem(PersistenceChooserModel.FIELDNAME_NAME_FILTER, x, y, w(wlabel), h(hlabel), labelProps));
            right(wlabel);
            append(new StructureFieldLayout(PersistenceChooserModel.FIELDNAME_NAME_FILTER, x, y, w(wfield), h(hfield), null, null, false, null, fieldProps));

            append(new TitledBorderLayoutItem("", 0, 0, wtotalpix, htotalpix));

            setSize(wtotalpix, htotalpix);
        }

        public void pos(double u, double v) {
            x = (int) (u * (gap + wcell));
            y = (int) (v * (gap + hcell));
        }

        int w(double v) {
            return (int) (v * (wcell + gap)) - gap;
        }

        int h(double v) {
            return (int) (v * (hcell + gap)) - gap;
        }

        void right(double v) {
            x += (int) (v * (wcell + gap));
        }

        void left(double v) {
            x -= (int) (v * (wcell + gap));
        }

        void down(double v) {
            y += (int) (v * (hcell + gap));
        }
    }
}
TOP

Related Classes of com.nexirius.framework.datamodel.persistence.PersistenceChooserModel$SelectCommand

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.