usereditui.java

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

JAVA
178
字号
/*--------------------------------------------------------------------------*
 | 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.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.rapla.entities.Category;
import org.rapla.entities.User;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.gui.toolkit.RaplaButton;

/****************************************************************
 * This is the controller-class for the User-Edit-Panel         *
 ****************************************************************/

/*User
  1. username, string
  2. name,string
  3. email,string,
  4. isadmin,boolean
*/

class UserEditUI  extends AbstractEditUI {

    /**
     * @param sm
     * @throws RaplaException
     */
    public UserEditUI(RaplaContext sm) throws RaplaException {
        super(sm);
        EditField[] fields = new EditField[] {
            new TextField(sm,"username")
            ,new TextField(sm,"name")
            ,new TextField(sm,"email")
            ,new BooleanField(sm,"admin")
            ,new GroupListField(sm)
        };
        setFields(fields);
    }

    class GroupListField extends AbstractEditField implements ChangeListener, ActionListener {
        User user;
        DefaultListModel model = new DefaultListModel();

        JPanel panel = new JPanel();
        JToolBar toolbar = new JToolBar();

        CategorySelectField newCategory;
        RaplaButton removeButton = new RaplaButton(RaplaButton.SMALL);
        RaplaButton newButton;
        JList list = new JList();

        /**
         * @param sm
         * @throws RaplaException
         */
        public GroupListField(RaplaContext sm) throws RaplaException {
            super(sm);
        	final Category rootCategory = getQuery().getUserGroupsCategory();
            if ( rootCategory == null )
                return;
            newCategory = new CategorySelectField(sm,"group", rootCategory );
            newButton = newCategory.getButton();
            toolbar.add( newButton  );
            toolbar.add( removeButton );
            toolbar.setFloatable( false );
            panel.setLayout( new BorderLayout() );
            panel.add( toolbar, BorderLayout.NORTH );
            list.setPreferredSize( new Dimension( 300, 100 ) );
            panel.add( new JScrollPane(list), BorderLayout.CENTER );
            newButton.setText( getString( "group" ) + " " + getString( "add" ) );
            removeButton.setText( getString( "group" ) + " " + getString( "remove" ) );
            newButton.setIcon( getIcon( "icon.new" ) );
            removeButton.setIcon( getIcon( "icon.remove" ) );
            newCategory.changeButtonText = false;
            newCategory.addChangeListener( this );
            removeButton.addActionListener( this );

            list.setCellRenderer(new DefaultListCellRenderer() {
                private static final long serialVersionUID = 1L;

                public Component getListCellRendererComponent(JList list,
                                                              Object value,
                                                              int index,
                                                              boolean isSelected,
                                                              boolean cellHasFocus)
                    {
                        if ( value != null ) {
                            Category category = (Category) value;
                            value = category.getPath( rootCategory
                                                      , getI18n().getLocale());
                        }
                        return super.getListCellRendererComponent(list,value,index,isSelected,cellHasFocus);
                    }
                }
                                 );
        }

        public JComponent getComponent() {
            return panel;
        }

        public boolean isBlock() {
            return true;
        }

        public boolean isVariableSized() {
            return false;
        }

        public void mapFrom(Object o) throws RaplaException {
            user = (User) o;
            model.clear();
            Category[] groups = user.getGroups();;
            for ( int i = 0; i < groups.length; i++ ) {
                model.addElement( groups[i] );
            }
            list.setModel(model);
        }

        protected Object getValue() {
            return null;
        }

        protected void setValue(Object object) {
        }

        public void mapTo(Object o) throws RaplaException {
        }

        public void actionPerformed(ActionEvent evt) {
            Category group = (Category) list.getSelectedValue();
            if (group != null) {
                user.removeGroup( group );
                model.removeElement( group );
                fireContentChanged();
            }
        }

        public void stateChanged(ChangeEvent evt) {
            Category newGroup = (Category) newCategory.getValue();
            if ( ! user.belongsTo( newGroup ) ) {
                user.addGroup( newGroup );
                model.addElement( newGroup );
                fireContentChanged();
            }
        }


    }
}

⌨️ 快捷键说明

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