jncombobox.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 296 行

JAVA
296
字号
package org.jnode.wt.components;

//import org.jnode.wt.*;

import org.jnode.wt.desktop.JNDesktop;
import org.jnode.wt.events.JNActionEvent;
import org.jnode.wt.events.JNItemEvent;
import org.jnode.wt.events.JNActionListener;
import org.jnode.wt.events.JNItemListener;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.peer.ChoicePeer;
import java.util.ArrayList;

/**
 * @author Kishore
 */
public class JNComboBox extends JNPanel implements ChoicePeer, JNActionListener, JNItemListener, ItemSelectable {


    private JNLabel comboLabel = null;
    private JNTextField comboTextFeild = null;

    private JNButton comboDropDownButton = null;

    protected int len = 28;
    protected Dimension DROPDOWNIMAGESIZE = new Dimension(28, 28);
//	protected Dimension DROPDOWNIMAGESIZE = new Dimension(24,24);

    private boolean isEditable = false;

//	protected static JNImage dropDownImage = null;

    protected JNList internalList = new JNList();

    protected boolean isPopupVisible = false;

    protected ArrayList itemListeners = new ArrayList();

    /**
     * JNComboBox constructor comment.
     */
    public JNComboBox() {
        super();

        setEditable(false);

        init();

        setText("ComboBox");
    }

    public void actionPerformed(JNActionEvent ae) {
        if (isPopupVisible) {
            setVisibleList(false);
            isPopupVisible = false;
        } else {
            setVisibleList(true);
            isPopupVisible = true;
        }

    }

    public void add(String str) {
        internalList.add(str);
    }

    public void addItemListener(JNItemListener lis) {
        itemListeners.add(lis);
    }

    private JNButton getDropDownButton() {
        if (null == comboDropDownButton) {
            comboDropDownButton = new JNButton("");
            comboDropDownButton.setButtonStyle(JNDefaultLookAndFeel.JAVA_STYLE_BUTTON);
//		comboDropDownButton.setImage( getDropDownImage() );

            int w = (int) DROPDOWNIMAGESIZE.getWidth() - 8;
            int h = (int) DROPDOWNIMAGESIZE.getHeight() - 8;
            comboDropDownButton.setSize(w, h);
            comboDropDownButton.setImage(org.jnode.wt.components.UIManager.getLookAndFeel().getUnSelectedDropDownImage(w, h));
            comboDropDownButton.setSelectedImage(org.jnode.wt.components.UIManager.getLookAndFeel().getSelectedDropDownImage(w, h));
            comboDropDownButton.setImageInsets(new Insets(5, 5, 3, 3));
            comboDropDownButton.addActionListener(this);
        }

        return comboDropDownButton;
    }

    private JNLabel getLabel() {

        if (null == comboLabel) {
            comboLabel = new JNLabel("");
            comboLabel.setAlignment(JNLabel.LEFT);
        }
        return comboLabel;
    }

    public Object[] getSelectedObjects() {
        return itemListeners.toArray();
    }

    private org.jnode.wt.components.JNComponent getTextComponent() {
        if (isEditable) {
            return comboTextFeild;

        } else {

            return comboLabel;
        }
    }

    /*private JNTextField getTextField() {
        return comboTextFeild;
    }*/

    private void init() {
        getDropDownButton().setSize(DROPDOWNIMAGESIZE);

        this.add(getTextComponent());
        this.add(getDropDownButton());

        resize();

        internalList.addItemListener(this);


    }

    public void itemStateChanged(JNItemEvent e) {

//	JNAbstractButton item = (JNAbstractButton)e.getItem();
        JNLabel item = (JNLabel) e.getItem();

        int id = JNItemEvent.ITEM_STATE_CHANGED;

        int state = -1;

        if (item.isSelected()) {
            state = JNItemEvent.SELECTED;
        } else {
            state = JNItemEvent.DESELECTED;
        }

        JNItemEvent ce = new JNItemEvent(this, id, item, state);

        for (int i = 0; i < itemListeners.size(); i++) {
            ((JNItemListener) itemListeners.get(i)).itemStateChanged(ce);
        }

        // set the text.
        setComboLabel(item.getText());


        setVisibleList(false);
        isPopupVisible = false;

    }

    public void removeItemListener(JNItemListener lis) {
        itemListeners.remove(lis);
    }

    public void resize() {
        internalList.setSize(this.getWidth(), 140);
    }

    private void setComboLabel(String s) {
        if (isEditable) {
            comboTextFeild.setText(s);
            comboTextFeild.repaint();

        } else {

            comboLabel.setText(s);
            comboLabel.repaint();
        }
    }

    private void setEditable(boolean b) {
        isEditable = b;

        if (isEditable) {
            comboTextFeild = new JNTextField();

        } else {

            comboLabel = getLabel();
        }
    }

    public void setSize(int w, int h) {
//	For Combo only width is considered, not height.


        int tsw = (int) DROPDOWNIMAGESIZE.getWidth();
        int tsh = (int) DROPDOWNIMAGESIZE.getHeight();

        super.setSize(w, tsh);

        int rw = w - tsw;

        if (isEditable) {
            comboTextFeild.setSize(rw, tsh);

        } else {

            comboLabel.setSize(rw, tsh);
        }

        getDropDownButton().setLocation(rw, 0);

        resize();
    }

    private void setText(String txt) {
        org.jnode.wt.components.JNComponent comp = getTextComponent();

        if (comp instanceof JNLabel) {
            ((JNLabel) comp).setText(txt);
        } else if (comp instanceof JNTextField) {
            ((JNTextField) comp).setText(txt);
        }
    }

    public void setVisible(boolean b) {
        super.setVisible(b);

        if (b == false) {
            if (isPopupVisible) {
                setVisibleList(false);
                isPopupVisible = false;
            }
        }

    }

    protected void setVisibleList(boolean visible) {
        JNDesktop desktop = getDesktopManager().getCurrentDesktop();

        if (true == visible) {
            int x = (int) (this.getAbsLocation().getX());
            int y = (int) (this.getAbsLocation().getY()) + this.getHeight();

            // [0] calculate the absoloute location of menu, with ref to Desktop.
            // [1] set the Location of MEnu
            internalList.setLocation(x, y);

            // [2] Add the menu to LayeredPAnel.
            desktop.add(internalList, org.jnode.wt.components.JNLayeredContainer.MENU_COMBO_LAYER);
            // [3] setVisible of menu to true
            internalList.setVisible(true);
            // [4] request for Desktop.repaint();
//			desktop.repaint();
/*			internalList.repaint();
			this.repaint();*/
//			desktop.getLayer(JNLayeredContainer.MENU_COMBO_LAYER).repaint();
        } else {

            if (internalList != null) {
                // [2] Remove the menu to LayeredPAnel.
                desktop.remove(internalList, org.jnode.wt.components.JNLayeredContainer.MENU_COMBO_LAYER);
                // [3] setVisible of menu to false
                internalList.setVisible(false);
                // [4] request for Desktop.repaint();
//			desktop.repaint();
                getParent().repaint();
/*			internalList.repaint();
			this.repaint();*/
                //desktop.getLayer(JNLayeredContainer.MENU_COMBO_LAYER).repaint();

            }

        }
//	System.out.println("[TODO: D17] Complete desktop repainting should be avoided");

    }

    public void add(String item, int index) {
        //TODO: implement it
    }

    public void addItem(String item, int index) {
        //TODO: implement it
    }

    public void select(int index) {
        //TODO: implement it
    }

    public void removeAll(){
        //TODO: implement it
    }
}

⌨️ 快捷键说明

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