attributeedit.java

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

JAVA
438
字号
        ChangeListener[] listeners = getChangeListeners();
        for (int i = 0;i<listeners.length; i++) {
            listeners[i].stateChanged(evt);
        }
    }

}

class DefaultConstraints extends AbstractEditField
    implements
        ActionListener
        ,ChangeListener
{
    JPanel panel = new JPanel();
    JLabel nameLabel = new JLabel();
    JLabel keyLabel = new JLabel();
    JLabel typeLabel = new JLabel();
    JLabel categoryLabel = new JLabel();
    JLabel tabLabel = new JLabel();
    JLabel expectedColumnsLabel = new JLabel();
    JLabel expectedRowsLabel = new JLabel();
    AttributeType types[] = {
        AttributeType.BOOLEAN
        ,AttributeType.STRING
        ,AttributeType.INT
        ,AttributeType.CATEGORY
        ,AttributeType.DATE
    };

    String tabs[] = {
            AttributeAnnotations.VALUE_MAIN_VIEW
            ,AttributeAnnotations.VALUE_ADDITIONAL_VIEW
            ,AttributeAnnotations.VALUE_NO_VIEW
    };

    boolean mapping = false;
    MultiLanguageField name ;
    TextField key;
    JComboBox classSelect = new JComboBox();
    CategorySelectField categorySelect;
    RaplaNumber expectedRows = new RaplaNumber(new Long(1),new Long(1),null, false);
    RaplaNumber expectedColumns = new RaplaNumber(new Long(1),new Long(1),null, false);
    JComboBox tabSelect = new JComboBox();

    Category rootCategory;

    DefaultConstraints(RaplaContext sm) throws RaplaException  {
        super( sm );
        key = new TextField(sm,"key");
        name = new MultiLanguageField(sm,"name");
        rootCategory = getQuery().getSuperCategory();

        categorySelect = new CategorySelectField(sm,"choose_root_category"
                                                 ,rootCategory);

        double fill = TableLayout.FILL;
        double pre = TableLayout.PREFERRED;
        panel.setLayout( new TableLayout( new double[][]
            {{5, pre, 5, fill },  // Columns
             {5, pre ,5, pre, 5, pre, 5, pre, 5, pre, 5, pre, 5}} // Rows
                                          ));
        panel.add("1,1,l,f", nameLabel);
        panel.add("3,1,f,f", name.getComponent() );
        panel.add("1,3,l,f", keyLabel);
        panel.add("3,3,f,f", key.getComponent() );
        panel.add("1,5,l,f", typeLabel);
        panel.add("3,5,l,f", classSelect);
        panel.add("1,7,l,t", categoryLabel);
        panel.add("3,7,l,t", categorySelect.getComponent());
        panel.add("1,7,l,t", expectedRowsLabel);
        panel.add("3,7,l,t", expectedRows);
        panel.add("1,9,l,t", expectedColumnsLabel);
        panel.add("3,9,l,t", expectedColumns);
        panel.add("1,11,l,t", tabLabel);
        panel.add("3,11,l,t", tabSelect);


        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for ( int i = 0; i < types.length; i++ ) {
            model.addElement(getString("type." + types[i]));
        }
        classSelect.setModel( model );

        model = new DefaultComboBoxModel();
        for ( int i = 0; i < tabs.length; i++ ) {
            model.addElement(getString(tabs[i]));
        }
        tabSelect.setModel( model );

        nameLabel.setText(getString("name") + ":");
        keyLabel.setText(getString("key") + ":");
        typeLabel.setText(getString("type") + ":");
        categoryLabel.setText(getString("root") + ":");
        expectedRowsLabel.setText(getString("expected_rows") + ":");
        expectedColumnsLabel.setText(getString("expected_columns") + ":");
        tabLabel.setText(getString("edit-view") + ":");

        categorySelect.addChangeListener ( this );
        name.addChangeListener ( this );
        key.addChangeListener ( this );
        classSelect.addActionListener ( this );
        tabSelect.addActionListener( this);
        expectedRows.addChangeListener( this );
        expectedColumns.addChangeListener( this );
    }

    public void setEditKeys(boolean editKeys) {
        keyLabel.setVisible( editKeys );
        key.getComponent().setVisible( editKeys );
    }

    public JComponent getComponent() {
        return panel;
    }

    public Object getValue() {
        return null;
    }

    public void setValue(Object object) {
    }

    public void mapFrom(Object object) throws RaplaException {
        try {
            mapping = true;
            Attribute attribute = (Attribute) object;
            name.mapFrom(attribute);
            key.mapFrom(attribute);
            classSelect.setSelectedItem(getString("type." + attribute.getType()));
            if (attribute.getType().equals(AttributeType.CATEGORY)) {
                categorySelect.setValue( attribute.getConstraint(ConstraintIds.KEY_ROOT_CATEGORY) );
            }
            Long rows = new Long(attribute.getAnnotation(AttributeAnnotations.KEY_EXPECTED_ROWS, "1"));
            expectedRows.setNumber( rows );
            Long columns = new Long(attribute.getAnnotation(AttributeAnnotations.KEY_EXPECTED_COLUMNS, String.valueOf(TextField.DEFAULT_LENGTH)));
            expectedColumns.setNumber( columns );
            
            String selectedTab = attribute.getAnnotation(AttributeAnnotations.KEY_EDIT_VIEW, AttributeAnnotations.VALUE_MAIN_VIEW);
            tabSelect.setSelectedItem(getString(selectedTab));
            update();
        } finally {
            mapping = false;
        }
    }

    public void mapTo(Object object) throws RaplaException {
        Attribute attribute = (Attribute) object;
        name.mapTo( attribute );
        key.mapTo( attribute );
        AttributeType type = types[classSelect.getSelectedIndex()];
        attribute.setType( type );
        if ( type.equals(AttributeType.CATEGORY)) {
            attribute.setConstraint(ConstraintIds.KEY_ROOT_CATEGORY, categorySelect.getValue() );
        } else {
            attribute.setConstraint(ConstraintIds.KEY_ROOT_CATEGORY, null);
        }

        if (attribute.getType().equals(AttributeType.STRING)) {
            Long size = (Long) expectedRows.getNumber();
            String newRows = null;
            if ( size != null && size.longValue() > 1)
                newRows = size.toString();

            size = (Long) expectedColumns.getNumber();
            String newColumns = null;
            if ( size != null && size.longValue() > 1)
                newColumns = size.toString();

            attribute.setAnnotation(AttributeAnnotations.KEY_EXPECTED_ROWS ,  newRows);
            attribute.setAnnotation(AttributeAnnotations.KEY_EXPECTED_COLUMNS,  newColumns);
        } else {
            attribute.setAnnotation(AttributeAnnotations.KEY_EXPECTED_ROWS,  null);
            attribute.setAnnotation(AttributeAnnotations.KEY_EXPECTED_COLUMNS,  null);
        }

        String selectedTab = tabs[tabSelect.getSelectedIndex()];
        if ( selectedTab != null && !selectedTab.equals(AttributeAnnotations.VALUE_MAIN_VIEW)) {
            attribute.setAnnotation(AttributeAnnotations.KEY_EDIT_VIEW,  selectedTab);
        } else {
            attribute.setAnnotation(AttributeAnnotations.KEY_EDIT_VIEW,  null);
        }
    }

    private void update() {
        AttributeType type = types[classSelect.getSelectedIndex()];
        boolean categoryVisible = type.equals(AttributeType.CATEGORY);
        boolean expectedRowsVisible = type.equals(AttributeType.STRING);
        boolean expectedColumnsVisible = type.equals(AttributeType.STRING);
        categoryLabel.setVisible( categoryVisible );
        categorySelect.getComponent().setVisible( categoryVisible );
        expectedRowsLabel.setVisible( expectedRowsVisible );
        expectedRows.setVisible( expectedRowsVisible );
        expectedColumnsLabel.setVisible( expectedColumnsVisible );
        expectedColumns.setVisible( expectedColumnsVisible );
    }

    public void actionPerformed(ActionEvent evt) {
        if (mapping)
            return;
        if ( evt.getSource() == classSelect) {
            AttributeType newType = types[classSelect.getSelectedIndex()];
            if (newType.equals(AttributeType.CATEGORY)) {
                categorySelect.setValue( rootCategory );
            }
        }
        fireContentChanged();
        update();
    }

    public void stateChanged(ChangeEvent e) {
        if (mapping)
            return;

        fireContentChanged();
    }

}

⌨️ 快捷键说明

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