classifiablefilteredit.java

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

JAVA
644
字号
/*--------------------------------------------------------------------------*
 | 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.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.border.Border;

import org.rapla.components.layout.TableLayout;
import org.rapla.components.util.Assert;
import org.rapla.entities.Category;
import org.rapla.entities.dynamictype.Attribute;
import org.rapla.entities.dynamictype.AttributeType;
import org.rapla.entities.dynamictype.ClassificationFilter;
import org.rapla.entities.dynamictype.ClassificationFilterRule;
import org.rapla.entities.dynamictype.ConstraintIds;
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.RaplaGUIComponent;
import org.rapla.gui.internal.common.CalendarSelectionModel;
import org.rapla.gui.internal.common.ClassifiableFilter;
import org.rapla.gui.internal.common.NamedListCellRenderer;
import org.rapla.gui.toolkit.RaplaButton;
import org.rapla.gui.toolkit.RaplaWidget;


class ClassifiableFilterEdit extends RaplaGUIComponent
    implements
        ActionListener
        ,RaplaWidget
{
    JPanel content = new JPanel();
    JScrollPane scrollPane;
    JCheckBox[] checkBoxes;
    ClassificationEdit filterEdit[];
    DynamicType[] types;
    boolean isResourceOnly;

    ClassifiableFilterEdit(RaplaContext sm, boolean isResourceOnly) throws RaplaException {
        super( sm);
        content.setBackground(UIManager.getColor("List.background"));
        scrollPane = new JScrollPane(content
                                     ,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
                                     ,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
                                     );
        scrollPane.setPreferredSize(new Dimension(600,430));
        this.isResourceOnly = isResourceOnly;
        content.setBorder( BorderFactory.createEmptyBorder(5,5,5,5));

    }

    public JComponent getClassificationTitle(String classificationType) {
        JLabel title = new JLabel( classificationType );
        title.setFont( title.getFont().deriveFont( Font.BOLD ));
        title.setText( getString( classificationType) + ":" );
        return title;
    }

    public void setTypes(DynamicType[] types) throws RaplaException {
        this.types = types;
        content.removeAll();
        TableLayout tableLayout = new TableLayout();
        content.setLayout(tableLayout);
        tableLayout.insertColumn(0,TableLayout.PREFERRED);
        tableLayout.insertColumn(1,10);
        tableLayout.insertColumn(2,TableLayout.FILL);
        tableLayout.insertRow(0, TableLayout.PREFERRED);
        if (checkBoxes != null) {
            for (int i=0;i<checkBoxes.length;i++) {
                checkBoxes[i].removeActionListener(this);
            }
        }
        checkBoxes = new JCheckBox[types.length];
        filterEdit = new ClassificationEdit[types.length];

        String lastClassificationType= null;
        int row = 0;
        for (int i=0;i<types.length;i++) {
            String classificationType = types[i].getAnnotation( DynamicTypeAnnotations.KEY_CLASSIFICATION_TYPE);
            if ( !classificationType.equals( lastClassificationType)) {
                tableLayout.insertRow( row, 2);
                row ++;
                lastClassificationType = classificationType;
                tableLayout.insertRow( row, TableLayout.MINIMUM);
                content.add( getClassificationTitle( classificationType),"0,"+ row +",2," + row) ;
                row ++;
                tableLayout.insertRow( row, 4);
                row ++;
                tableLayout.insertRow( row, 2);
                content.add( new JPanel() , "0," + row  + ",2,"  + row );
                row ++;

            }
            tableLayout.insertRow( row, 3);
            tableLayout.insertRow( row + 1, TableLayout.MINIMUM);
            tableLayout.insertRow( row + 2, TableLayout.MINIMUM);
            tableLayout.insertRow( row + 3, 3);
            tableLayout.insertRow( row + 4, 2);
            checkBoxes[i] = new JCheckBox(getName(types[i]));
            checkBoxes[i].setBorder( BorderFactory.createEmptyBorder(0,10,0,0));
            checkBoxes[i].setOpaque( false );
            checkBoxes[i].addActionListener(this);
            checkBoxes[i].setSelected( true );
            content.add( checkBoxes[i] , "0," + (row + 1) + ",l,t");
            filterEdit[i] = new ClassificationEdit(getContext());
            content.add( filterEdit[i].getNewComponent() , "2," + (row + 1));
            content.add( filterEdit[i].getRulesComponent() , "0," + (row + 2) + ",2,"+ (row + 2));
            content.add( new JPanel() , "0," + (row + 4) + ",2,"  + (row + 4));
            row += 5;
        }
    }


    private ClassificationFilter findFilter(int index,ClassificationFilter[] filters) {
        for (int i=0;i<filters.length;i++)
            if (filters[i].getType().equals(types[index]))
                return filters[i];
        return null;
    }

    public void setFilter(CalendarSelectionModel o) throws RaplaException {
        CalendarSelectionModel filter =  o;

        List list = new ArrayList();
        if ( !isResourceOnly) {
            list.addAll( Arrays.asList( getQuery().getDynamicTypes( DynamicTypeAnnotations.VALUE_RESERVATION_CLASSIFICATION )));
        }
        list.addAll( Arrays.asList( getQuery().getDynamicTypes( DynamicTypeAnnotations.VALUE_RESOURCE_CLASSIFICATION )));
        list.addAll( Arrays.asList( getQuery().getDynamicTypes( DynamicTypeAnnotations.VALUE_PERSON_CLASSIFICATION )));
        setTypes( (DynamicType[])list.toArray( DynamicType.DYNAMICTYPE_ARRAY));

        mapFrom( filter );
    }

    public void mapFrom(ClassifiableFilter classifiableFilter) throws RaplaException {
        ClassificationFilter[] filters = classifiableFilter.getFilter();
        for (int i=0;i<types.length;i++) {
            ClassificationFilter filter = (filters.length > 0) ?
                findFilter(i, filters)
                : types[i].newClassificationFilter();
            checkBoxes[i].setSelected( filter != null);
            filterEdit[i].mapFrom(filter);
        }
        scrollPane.revalidate();
        scrollPane.repaint();
    }

    public ClassificationFilter[] getFilters() throws RaplaException {
        boolean isDefault = true;
        ArrayList list = new ArrayList();
        for (int i=0; i< filterEdit.length; i++) {
            ClassificationFilter filter = filterEdit[i].getFilter();
            if (filter != null) {
                list.add(filter);
                if ( filter.ruleSize() > 0 ) {
                    isDefault = false;
                }
            } else {
                isDefault = false;
            }
        }

        // An empty array indicates default settings
        if ( isDefault ) {
            return ClassificationFilter.CLASSIFICATIONFILTER_ARRAY;
        }
        return (ClassificationFilter[]) list.toArray(new ClassificationFilter[] {});
    }

    public void actionPerformed(ActionEvent evt) {
        try {
            for (int i=0;i<checkBoxes.length;i++) {
                if (checkBoxes[i] == evt.getSource()) {
                    if (checkBoxes[i].isSelected())
                        filterEdit[i].mapFrom(types[i].newClassificationFilter());
                    else
                        filterEdit[i].mapFrom(null);
                    // activate the i. filter
                }
            }
        } catch (RaplaException ex) {
            showException(ex, getComponent());
        }
        content.revalidate();
        content.repaint();
    }

    public JComponent getComponent() {
        return scrollPane;
    }

}


class ClassificationEdit extends RaplaGUIComponent implements ActionListener {
    JPanel ruleListPanel = new JPanel();
    JPanel newPanel = new JPanel();
    List ruleList = new ArrayList();
    JComboBox attributeSelector;
    JButton newLabel = new JButton();
    DynamicType type;

    ClassificationEdit(RaplaContext sm) throws RaplaException {
        super(sm );
        ruleListPanel.setOpaque( false );
        ruleListPanel.setLayout(new BoxLayout(ruleListPanel,BoxLayout.Y_AXIS));
        newPanel.setOpaque( false );
        newPanel.setLayout(new TableLayout(new double[][] {{TableLayout.PREFERRED},{TableLayout.PREFERRED}}));
    }

    public JComponent getRulesComponent() {
        return ruleListPanel;
    }

    public JComponent getNewComponent() {
        return newPanel;
    }

    public void mapFrom(ClassificationFilter filter) throws RaplaException {
        getRulesComponent().removeAll();
        getNewComponent().removeAll();
        if ( filter == null) {
            type = null;
            return;
        }
        this.type = filter.getType();
        Attribute[] attributes = type.getAttributes();
        if (attributes.length == 0 )
            return;

        if (attributeSelector != null)
            attributeSelector.removeActionListener(this);
        attributeSelector = new JComboBox(attributes);

        attributeSelector.setRenderer(new NamedListCellRenderer(getI18n().getLocale()) {
            private static final long serialVersionUID = 1L;

                public Component getListCellRendererComponent(JList list,
                                                              Object value,
                                                              int index,
                                                              boolean isSelected,
                                                              boolean cellHasFocus) {
                    if (value == null) {
                        setText(getString("new_rule"));
                        return this;
                    } else {
                        return super.getListCellRendererComponent(list, value,index,isSelected,cellHasFocus);
                    }
                }
            });

        attributeSelector.addActionListener(this);
        newPanel.add(newLabel,"0,0,f,c");
        newPanel.add(attributeSelector,"0,0,f,c");
        newLabel.setText(getString("new_rule"));
        newLabel.setVisible(false);
        attributeSelector.setSelectedItem(null);
        Iterator it = filter.ruleIterator();
        while (it.hasNext()) {
            addRuleComponent().setRule((ClassificationFilterRule) it.next());
        }
        update();
    }

    public void update() {
        ruleListPanel.removeAll();
        int i=0;
        for (Iterator it = ruleList.iterator();it.hasNext();) {
            RuleComponent rule = (RuleComponent) it.next();
            ruleListPanel.add( rule);
            rule.setAndVisible( i > 0);
            i++;
        }

        ruleListPanel.revalidate();
        ruleListPanel.repaint();
    }


    public void actionPerformed(ActionEvent evt) {
        if (evt.getSource() == attributeSelector) {
            Attribute att = (Attribute)attributeSelector.getSelectedItem();
            try {
                if (att != null) {
                    RuleComponent ruleComponent = getComponent(att);
                    if (ruleComponent == null) {
                        addRuleComponent().newRule( att);
                    } else {
                        ruleComponent.addOr();
                    }

⌨️ 快捷键说明

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