⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 assignmentpanel.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
字号:
//You can redistribute this software and/or modify it under the terms of
//the Ozone Library License version 1 published by ozone-db.org.
//
//The original code and portions created by SMB are
//Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
//
package org.ozoneDB.adminGui.widget;

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.util.Collections;
import java.util.Iterator;
import java.util.Vector;


//#############################################################################
/**
 * This class creates an assignment panel which is used to assign/remove
 * values from one list into another.
 *
 * @author  <p align=center>Ibsen Ramos-Bonilla
 * <br>Copyright &copy 1997-@year@ by SMB GmbH. All Rights Reserved.</p>
 *
 * @version 1.0
 */
//#############################################################################

public class AssignmentPanel extends JPanel {

    /** The list of starting values. */
    private Vector startingValues;
    /** The list of currently assigned values. */
    private Vector assignedValues;
    /** List holding all the values. */
    private JList allValuesList = new JList();
    /** List holding the assigned values. */
    private JList assignedValuesList = new JList();


    /**
     * Default constructor not used.
     */
    private AssignmentPanel() {
    }

    /**
     * Overloaded constructor sets the panel tag and gets a list of values.
     *
     * @param tagLabel - the panel tag label.
     * @param startingValues - the values to display for assigment.
     * @param assignedValues - the values assigned
     */
    public AssignmentPanel(String tagLabel, Vector startingValues,
                           Vector assignedValues) {
        try {
            this.assignedValues = assignedValues;
            this.startingValues = startingValues;
            this.init(tagLabel);
            //this.setSize(390, 290);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * This method initializes the dialog.
     *
     * @param tagLabel - the panel tag label.
     */
    private void init(String tagLabel) throws Exception {
        //set the panel attributes
        this.setLayout(null);

        //set label attributes
        JLabel allLabel = new JLabel(tagLabel);
        allLabel.setBounds(new Rectangle(15, 10, 41, 17));
        JLabel assignedLabel = new JLabel("Assigned");
        assignedLabel.setBounds(new Rectangle(226, 10, 96, 17));

        //button to assign one value
        JButton assignOneButton = new JButton(">");
        assignOneButton.setBounds(new Rectangle(167, 31, 49, 27));
        assignOneButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                assignValues();
            }
        });

        //button to assign multiple values
        JButton assignAllButton = new JButton(">>");
        assignAllButton.setBounds(new Rectangle(167, 63, 49, 27));
        assignAllButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                assignAllValues();
            }
        });

        //button to remove an assigned value
        JButton removeOneButton = new JButton("<");
        removeOneButton.setBounds(new Rectangle(167, 111, 49, 27));
        removeOneButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                removeValues();
            }
        });

        //button to remove all assigned values
        JButton removeAllButton = new JButton("<<");
        removeAllButton.setBounds(new Rectangle(167, 146, 49, 27));
        removeAllButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                removeAllValues();
            }
        });

        //scroll panels
        JScrollPane assignedValuesScrollPane = new JScrollPane();
        assignedValuesScrollPane.setBounds(new Rectangle(226, 27, 143, 148));
        assignedValuesScrollPane.getViewport().add(assignedValuesList, null);

        JScrollPane allValuesScrollPane = new JScrollPane();
        allValuesScrollPane.setBounds(new Rectangle(15, 27, 143, 148));
        allValuesScrollPane.getViewport().add(allValuesList, null);

        //add components to the dialog
        this.add(allLabel, null);
        this.add(assignedLabel, null);
        this.add(assignOneButton, null);
        this.add(assignAllButton, null);
        this.add(removeOneButton, null);
        this.add(removeAllButton, null);
        this.add(allValuesScrollPane, null);
        this.add(assignedValuesScrollPane, null);

        //populate the values list
        populateLists();
    }

    /**
     * This method loads all the values into the "all" list.
     */
    public void populateLists() {
        Vector values = startingValues;

        //check if the assigned values is empty
        if (assignedValues == null)
            assignedValues = new Vector();

        //get the list of assigned values
        Collections.sort(assignedValues);
        assignedValuesList.setListData(assignedValues);

        //remove the values from the "all" list that are in the
        //"assigned" list.
        if (values != null) {
            //remove the values that are in the "assigned" list
            Iterator it = assignedValues.iterator();

            while (it.hasNext()) {
                Object value = it.next();

                if (values.contains(value))
                    values.removeElement(value);
            }

            //sort the list and put it in the "all" list
            Collections.sort(values);
            allValuesList.setListData(values);
        }
    }

    /**
     * This method removes the selected values from the "all" list and places
     * them in the "assigned" list.
     */
    private void assignValues() {
        //make sure something is selected in the all value list
        if (!allValuesList.isSelectionEmpty()) {
            //get the selected values
            Object[] value = allValuesList.getSelectedValues();

            //add it to the assigned list
            Vector values = this.addToVector(assignedValuesList, value);
            assignedValuesList.removeAll();
            assignedValuesList.setListData(values);
            assignedValues = values;

            //remove the selected values from the all value list
            Vector allUsers = this.removeFromVector(allValuesList, value);
            allValuesList.removeAll();
            allValuesList.setListData(allUsers);
        }
    }

    /**
     * This method removes all values in the "all" list and adds them to the
     * "assigned" list.
     */
    private void assignAllValues() {
        //get all assigned values
        Vector values = assignedValues;

        //add everything in "all" to the values vector
        for (int i = 0; i < allValuesList.getModel().getSize(); i++) {
            values.addElement(allValuesList.getModel().getElementAt(i));
        }

        //clear the "all" list
        Vector nothing = new Vector();
        allValuesList.removeAll();
        allValuesList.setListData(nothing);

        //sort the vector and set the "assigned" data to all values
        Collections.sort(values);
        assignedValuesList.removeAll();
        assignedValuesList.setListData(values);
        assignedValues = values;
    }

    /**
     * This method removes values from the "assigned" list and puts them in the
     * "all" list.
     */
    private void removeValues() {
        //make sure something is selected in the assigned value list
        if (!assignedValuesList.isSelectionEmpty()) {
            //get the selected values
            Object[] value = assignedValuesList.getSelectedValues();

            //add it to the all list
            Vector allValues = this.addToVector(allValuesList, value);
            allValuesList.removeAll();
            allValuesList.setListData(allValues);

            //remove the selected values from the assigned value list
            Vector values = this.removeFromVector(assignedValuesList, value);
            assignedValuesList.removeAll();
            assignedValuesList.setListData(values);
            assignedValues = values;
        }
    }

    /**
     * This method removes all values in the "assigned" list and adds them to
     * the "all" list.
     */
    private void removeAllValues() {
        //get all assigned values
        Vector values = assignedValues;

        //clear the "assigned" list
        Vector nothing = new Vector();
        assignedValuesList.removeAll();
        assignedValuesList.setListData(nothing);
        assignedValues = nothing;

        //get all the items in the "all users" list and add them to the items
        //removed from the assigned users list.
        for (int i = 0; i < allValuesList.getModel().getSize(); i++) {
            values.addElement(allValuesList.getModel().getElementAt(i));
        }

        Collections.sort(values);
        allValuesList.removeAll();
        allValuesList.setListData(values);
    }

    /**
     * This method returns a vector created from the elements in the given
     * list, adds the given string to the vector, and sorts the results.
     *
     * @param list - list of values.
     * @param value - an array of values.
     * @return Vector - the created vector after merging the list and array.
     */
    private Vector addToVector(JList list, Object[] value) {
        Vector values = new Vector();

        //first go ahead and add the values
        if (value != null) {
            for (int i = 0; i < value.length; i++) {
                values.addElement(value[i]);
            }
        }

        //next go ahead and add the list entries
        if (list != null) {
            for (int j = 0; j < list.getModel().getSize(); j++) {
                values.addElement(list.getModel().getElementAt(j));
            }
        }

        //sort and return
        Collections.sort(values);
        return values;
    }

    /**
     * This method returns a vector created from the elements in the given
     * list, removes the given string from the vector, and sorts the results.
     *
     * @param list - list of values.
     * @param value - an array of values.
     * @return Vector - the created vector after merging the list and array.
     */
    private Vector removeFromVector(JList list, Object[] value) {
        Vector values = new Vector();

        //first go ahead and remove the values
        if (value != null) {

        }

        //add all the elements in the list to the vector EXCEPT for "item"
        for (int i = 0; i < list.getModel().getSize(); i++) {

            int loopCount = 0;
            boolean found = false;
            String listItem = (String) (list.getModel().getElementAt(i));

            while (!found && loopCount < value.length) {

                if (value[loopCount].equals(listItem)) {
                    found = true;
                }
                loopCount++;
            }

            //add the element if not in both lists
            if (!found) {
                values.addElement(listItem);
            }
        }

        //sort and return
        Collections.sort(values);
        return values;
    }

    /**
     * This method returns the currently selected values.
     *
     * @return Vector - list of assigned values.
     */
    public Vector getAssignedValues() {
        return this.assignedValues;
    }

} //--------------------------------- E O F -----------------------------------

⌨️ 快捷键说明

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