Package com.nexirius.framework.application

Source Code of com.nexirius.framework.application.DataModelPopupEditor$CancelListener

//{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.application;

import com.nexirius.framework.datamodel.DataModel;
import com.nexirius.framework.dataviewer.DataViewer;
import com.nexirius.framework.dataviewer.LayoutItem;
import com.nexirius.framework.dataviewer.ViewerCreator;
import com.nexirius.framework.dataviewer.ViewerFactory;
import com.nexirius.framework.gadgets.ArrayPanel;
import com.nexirius.framework.onlinehelp.OnlineHelp;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class DataModelPopupEditor {



    DataModel model = null;
    LayoutItem layoutItem = null;
    DataModel original;
    ViewerFactory factory;
    ViewerCreator viewerCreator = null;
    Frame parentFrame;
    Dialog parentDialog;
    JDialog dialog = null;
    JComponent dialogComponent = null;
    boolean okPressed = false;
    boolean noDuplicate = false;
    JButton okButton = null;
    boolean newOriginal = false;

    public DataModelPopupEditor(DataModel original, ViewerFactory factory) {
        this.original = original;
        this.factory = factory;
        this.parentFrame = null;
        this.parentDialog = null;
    }

    public DataModelPopupEditor(DataModel original, ViewerFactory factory, Frame parentFrame) {
        this.original = original;
        this.factory = factory;
        this.parentFrame = parentFrame;
        this.parentDialog = null;
    }

    public DataModelPopupEditor(DataModel original, ViewerFactory factory, Dialog parentDialog) {
        this.original = original;
        this.factory = factory;
        this.parentFrame = null;
        this.parentDialog = parentDialog;
    }

    public void setDataModel(DataModel original) {
        this.original = original;
        newOriginal = true;
    }

    public void setNoDuplicate(boolean on) {
        noDuplicate = on;
    }

    public void setLayout(LayoutItem l) {
        layoutItem = l;
    }

    /**
     * Need the associated ViewerCreator to create the editor in the dialog window
     */
    public void setViewerCreator(ViewerCreator viewerCreator) {
        this.viewerCreator = viewerCreator;
    }

    /**
     * Returns the associated viewer creator
     */
    public ViewerCreator getViewerCreator() {
        return viewerCreator;
    }

    public boolean hasDefaultParent() {
        return parentFrame == null && parentDialog == null;
    }

    public boolean popup() {
        if (newOriginal) {
            try {
                model.reset();
                model.dropData(original.dragData());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else {
            if (noDuplicate) {
                model = original;
            } else {
                model = original.duplicate(null, null);
            }
        }

        okPressed = false;

        try {
            createWindow();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException("Can't create dialog window");
        }

        DialogManager.popup(dialog);

        return okPressed;
    }

    /**
     * Removes the current data viewer
     */
    public void popdown() {
        if (dialog == null) {

            return;
        }

        dialog.getContentPane().removeAll();
        dialog.getContentPane().setLayout(null);
        dialog.dispose();
        dialog = null;

        okButton.requestFocus();

        Component comps[] = new Component[1];

        comps[0] = dialogComponent;
        resetSubComponents(comps);
    }

    private void resetSubComponents(Component comps[]) {
        JComponent ret = null;

        if (comps != null && comps.length > 0) {

            for (int i = 0; i < comps.length; ++i) {
                Component c = comps[i];

                if (c instanceof JScrollPane) {
                    try {
                        c = ((JScrollPane) c).getViewport().getView();
                    } catch (Exception ex) {
                        //ignore
                    }
                }

                if (c instanceof JComponent) {

                    if (c instanceof JTabbedPane) {
                        ((JTabbedPane) c).setSelectedIndex(0);
                    }

                    resetSubComponents(((JComponent) c).getComponents());
                }
            }
        }
    }

    /**
     * Create the dialog window
     */
    private void createWindow()
            throws Exception {
        String title = factory.getText(model.getFieldName());

        if (parentFrame != null) {
            dialog = new JDialog(parentFrame, title, true);
        } else if (parentDialog != null) {
            dialog = new JDialog(parentDialog, title, true);
        } else {
            dialog = DialogManager.createJDialog(title, true);
        }

        dialog.addWindowListener(new MyWindowListener());
        dialog.getContentPane().setLayout(new BorderLayout());
        dialog.getContentPane().add(BorderLayout.SOUTH, createButtons());

        if (okButton != null) {
            okButton.setDefaultCapable(true);
            dialog.getRootPane().setDefaultButton(okButton);
        }

        if (dialogComponent == null) {
            DataViewer dataViewer;

            if (viewerCreator == null) {
                dataViewer = factory.createDefaultEditor(model);
            } else {
                dataViewer = factory.createViewer(viewerCreator, model);
            }

            if (layoutItem != null) {
                dataViewer.setLayout(layoutItem);
            }

            try {
                JComponent panel = dataViewer.getJComponent();
                Dimension size = panel.getPreferredSize();
                Dimension scrdim = Toolkit.getDefaultToolkit().getScreenSize();

                // insert a scroll pane if needed
                if (size.width * 1.1 > scrdim.width || size.height * 1.2 > scrdim.height) {
                    JScrollPane scroll = new JScrollPane(panel);

                    scroll.getHorizontalScrollBar().setUnitIncrement(30);
                    scroll.getVerticalScrollBar().setUnitIncrement(30);

                    dialogComponent = scroll;
                } else {
                    dialogComponent = panel;
                }
            } catch (Exception ex) {
                //FIX
                ex.printStackTrace();
                dialogComponent = new JLabel(ex.getMessage());
            }
        }

        dialog.getContentPane().add(BorderLayout.CENTER, dialogComponent);

        dialog.pack();
    }

    /**
     * Creates the dialog window buttons (Ok and Cancel)
     */
    public JComponent createButtons() {
        JButton buttons[];
        JPanel panel = new ArrayPanel(true);

        if (factory == null) {
            buttons = new JButton[3];
            panel.add(buttons[0] = new JButton("OK"));
            panel.add(buttons[1] = new JButton("Cancel"));
            panel.add(buttons[2] = new JButton("Help"));
        } else {
            String button_labels[] = new String[2];

            if (getHelp() == null) {
                if (noDuplicate) {
                    button_labels = new String[1];

                    button_labels[0] = ViewerFactory.CLOSE_BUTTON;
                } else {
                    button_labels = new String[2];

                    button_labels[0] = ViewerFactory.OK_BUTTON;
                    button_labels[1] = ViewerFactory.CANCEL_BUTTON;
                }
            } else {
                if (noDuplicate) {
                    button_labels = new String[2];

                    button_labels[0] = ViewerFactory.CLOSE_BUTTON;
                    button_labels[2] = ViewerFactory.HELP_BUTTON;
                } else {
                    button_labels = new String[3];

                    button_labels[0] = ViewerFactory.OK_BUTTON;
                    button_labels[1] = ViewerFactory.CANCEL_BUTTON;
                    button_labels[2] = ViewerFactory.HELP_BUTTON;
                }
            }

            buttons = factory.createButtons(button_labels, panel);
        }

        int buttonIndex = 0;

        okButton = buttons[buttonIndex++];
        okButton.addActionListener(new OkListener());

        if (!noDuplicate && buttons.length > 1) {
            buttons[buttonIndex++].addActionListener(new CancelListener());
        }

        if (buttons.length > buttonIndex) {
            buttons[buttonIndex++].addActionListener(new HelpListener());
        }

        return panel;
    }

    public String getHelp() {
        return factory.getClientResource().getHelpString(model.getFieldName());
    }

    /**
     * calls dispose on the associated JDialog
     */
    public void dispose() {
        if (dialog != null) {
            dialog.dispose();
            dialog = null;
        }
    }

    class OkListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            try {
                if (!model.isValid()) {

                    return;
                }
                if (original != model) {
                    original.dropData(model.dragData());
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
//      model.finishEdit();
            okPressed = true;
            popdown();
        }
    }

    class CancelListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            okPressed = false;
//      model.cancelEdit();
            popdown();
        }
    }

    class HelpListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            OnlineHelp.help(getHelp());
        }
    }

    class MyWindowListener implements WindowListener {
        public void windowActivated(WindowEvent e) {
        }

        public void windowClosed(WindowEvent e) {
        }

        public void windowClosing(WindowEvent e) {
            okPressed = false;
            popdown();
        }

        public void windowDeactivated(WindowEvent e) {
        }

        public void windowDeiconified(WindowEvent e) {
        }

        public void windowIconified(WindowEvent e) {
        }

        public void windowOpened(WindowEvent e) {
        }
    }
}
TOP

Related Classes of com.nexirius.framework.application.DataModelPopupEditor$CancelListener

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.