dynamictypeeditui.java

来自「Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI」· Java 代码 · 共 222 行

JAVA
222
字号
/*--------------------------------------------------------------------------*
 | Copyright (C) 2006 Christopher Kohlhaas                                  |
 |                                                                          |
 | This program is free software; you can redistribute it and/or modify     |
 | it under the terms of the GNU General Public License as published by the |
 | Free Software Foundation. A copy of the license has been included with   |
 | these distribution in the COPYING file, if not go to www.fsf.org         |
 |                                                                          |
 | As a special exception, you are granted the permissions to link this     |
 | program with every library, which license fulfills the Open Source       |
 | Definition as published by the Open Source Initiative (OSI).             |
 *--------------------------------------------------------------------------*/
package org.rapla.gui.internal.edit;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.rapla.components.layout.TableLayout;
import org.rapla.components.util.Assert;
import org.rapla.components.util.Tools;
import org.rapla.entities.IllegalAnnotationException;
import org.rapla.entities.UniqueKeyException;
import org.rapla.entities.dynamictype.Attribute;
import org.rapla.entities.dynamictype.DynamicType;
import org.rapla.entities.dynamictype.DynamicTypeAnnotations;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.gui.EditComponent;
import org.rapla.gui.RaplaGUIComponent;


/****************************************************************
 * This is the controller-class for the DynamicType-Edit-Panel   *
 ****************************************************************/
class DynamicTypeEditUI extends RaplaGUIComponent
    implements
     EditComponent
{
    public static String WARNING_SHOWED = DynamicTypeEditUI.class.getName() + "/Warning";
    DynamicType dynamicType;
    JPanel editPanel = new JPanel();
    JPanel annotationPanel = new JPanel();
    JLabel nameLabel = new JLabel();
    MultiLanguageField name;
    JCheckBox editAdvanced = new JCheckBox();
    JLabel elementKeyLabel = new JLabel();
    TextField elementKey;
    AttributeEdit attributeEdit;

    JLabel annotationLabel = new JLabel();
    JLabel annotationDescription = new JLabel();
    JTextField annotationText = new JTextField();


    public DynamicTypeEditUI(RaplaContext sm) throws RaplaException {
        super(sm);
        name = new MultiLanguageField(sm,"name");
        elementKey = new TextField(sm,"elementKey");
        attributeEdit = new AttributeEdit(sm);

        nameLabel.setText(getString("dynamictype.name") + ":");
        editAdvanced.setText(getString("edit_advanced"));
        elementKey.getComponent().setVisible(false);
        elementKeyLabel.setText(getString("elementkey"));
        elementKeyLabel.setVisible(false);
        ActionListener listener = new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    if (editAdvanced.isSelected() && getSessionMap().get(WARNING_SHOWED) == null) {
                        showWarning(getString("warning.experienced_users_only")
                                    ,getComponent()
                                    );
                        getSessionMap().put(WARNING_SHOWED,Boolean.TRUE);
                    }
                    boolean visible = editAdvanced.isSelected();
                    elementKey.getComponent().setVisible( visible );
                    elementKeyLabel.setVisible( visible );
                    attributeEdit.setEditKeys( visible );
                    annotationPanel.setVisible( visible );
                }
            };
        editAdvanced.addActionListener(listener);
        double[][] sizes = new double[][] {
            {5,TableLayout.PREFERRED,5,TableLayout.FILL,5}
            ,{TableLayout.PREFERRED,5,TableLayout.PREFERRED,5,TableLayout.PREFERRED,5,TableLayout.PREFERRED,5,TableLayout.FILL}
        };
        TableLayout tableLayout = new TableLayout(sizes);
        editPanel.setLayout(tableLayout);
        editPanel.add(editAdvanced,"1,0,3,0");
        editPanel.add(nameLabel,"1,2");
        editPanel.add(name.getComponent(),"3,2");
        editPanel.add(elementKeyLabel,"1,4");
        editPanel.add(elementKey.getComponent(),"3,4");
        editPanel.add(attributeEdit.getComponent(),"1,6,3,6");

        // #FIXM Should be replaced by generic solution
        tableLayout.insertRow(7,5);
        tableLayout.insertRow(8,TableLayout.PREFERRED);
        editPanel.add(annotationPanel,"1,8,3,8");
        annotationPanel.setLayout(new TableLayout(new double[][] {
            {TableLayout.PREFERRED,5,TableLayout.FILL}
            ,{TableLayout.PREFERRED,5,TableLayout.PREFERRED}
        }));
        annotationPanel.add(annotationText,"2,0");
        annotationPanel.add(annotationLabel,"0,0");
        annotationPanel.add(annotationDescription,"2,2");
        annotationLabel.setText(getString("dynamictype.annotation.nameformat") + ":");
        annotationDescription.setText(getString("dynamictype.annotation.nameformat.description"));
        float newSize = (float) (annotationDescription.getFont().getSize() * 0.8);
        annotationDescription.setFont(annotationDescription.getFont().deriveFont( newSize));
        annotationPanel.setVisible(false);
        attributeEdit.addChangeListener( new ChangeListener() {
            public void stateChanged( ChangeEvent e )
            {
                updateAnnotations();
            }

        });
        annotationText.addFocusListener( new FocusAdapter() {

            public void focusLost( FocusEvent e )
            {
                try
                {
                    setAnnotations();
                }
                catch ( RaplaException ex )
                {
                    showException( ex, getComponent());
                }
            }

        });
    }

    public JComponent getComponent() {
        return editPanel;
    }

    public void mapTo(Object object) {
        throw new UnsupportedOperationException();
    }

    public void mapToObject() throws RaplaException {
    	validate();
        name.mapTo(dynamicType);
        elementKey.mapTo(dynamicType);
        attributeEdit.confirmEdits();
        setAnnotations();
    }

    private void setAnnotations() throws RaplaException
    {
        try {
            dynamicType.setAnnotation(DynamicTypeAnnotations.KEY_NAME_FORMAT, annotationText.getText().trim());
        } catch (IllegalAnnotationException ex) {
            throw new RaplaException(ex.getMessage() + "<br>Switch on experienced settings to show the keys. "
                                     ,ex);

        }
    }
    public Object getObject() {
        return dynamicType;
    }

    public void setObject(Object o) throws RaplaException {
        dynamicType = (DynamicType) o;
        name.mapFrom(o);
        elementKey.mapFrom(o);
        attributeEdit.setDynamicType(dynamicType);
        updateAnnotations();
    }

    private void updateAnnotations() {
        annotationText.setText( dynamicType.getAnnotation( DynamicTypeAnnotations.KEY_NAME_FORMAT ) );
    }

    private void validate() throws RaplaException {
        Assert.notNull(dynamicType);
        if ( getName( dynamicType ).length() == 0)
            throw new RaplaException(getString("error.no_name"));

        if (dynamicType.getElementKey().equals("")) {
            throw new RaplaException(getI18n().format("error.no_key",""));
        }
        checkKey(dynamicType.getElementKey());
        Attribute[] attributes = dynamicType.getAttributes();
        for (int i=0;i<attributes.length;i++) {
            String key = attributes[i].getKey();
            if (key == null || key.trim().equals(""))
                throw new RaplaException(getI18n().format("error.no_key","(" + i + ")"));
            checkKey(key);
            for (int j=i+1;j<attributes.length;j++) {
                if ((key.equals(attributes[j].getKey()))) {
                    throw new UniqueKeyException(getI18n().format("error.not_unique",key));
                }
            }
        }
    }

    private void checkKey(String key) throws RaplaException {
        if (key.length() ==0)
            throw new RaplaException(getString("error.no_key"));
        if (!Tools.isKey(key))
            throw new RaplaException(getI18n().format("error.invalid_key"
                                                      ,key
                                                      ,"'-', '_'"));
    }


}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?