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

📄 comboboxui.java

📁 用于java swing的皮肤软件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
				}
				else
				{
					comboBox.setRequestFocusEnabled(true);
					removeEditor();
				}

				updateToolTipTextForChildren();

				comboBox.revalidate();
			}
			else if (propertyName.equals("enabled"))
			{
				boolean enabled= comboBox.isEnabled();
				if (editor != null)
					editor.setEnabled(enabled);
				if (arrowButton != null)
					arrowButton.setEnabled(enabled);
				comboBox.repaint();
			}
			else if (propertyName.equals("maximumRowCount"))
			{
				if (isPopupVisible(comboBox))
				{
					setPopupVisible(comboBox, false);
					setPopupVisible(comboBox, true);
				}
			}
			else if (propertyName.equals("font"))
			{
				listBox.setFont(comboBox.getFont());
				if (editor != null)
				{
					editor.setFont(comboBox.getFont());
				}
				isMinimumSizeDirty= true;
				comboBox.validate();
			}
			else if (propertyName.equals(JComponent.TOOL_TIP_TEXT_KEY))
			{
				updateToolTipTextForChildren();
			}
			else if (propertyName.equals(IS_TABLE_CELL_EDITOR))
			{
				Boolean inTable= (Boolean) e.getNewValue();
				isTableCellEditor= inTable.equals(Boolean.TRUE) ? true : false;
			}
			else if (propertyName.equals("prototypeDisplayValue"))
			{
				isMinimumSizeDirty= true;
				isDisplaySizeDirty= true;
				comboBox.revalidate();
			}
			else if (propertyName.equals("renderer"))
			{
				isMinimumSizeDirty= true;
				isDisplaySizeDirty= true;
				comboBox.revalidate();
			}
		}
	}

	// Syncronizes the ToolTip text for the components within the combo box to be the 
	// same value as the combo box ToolTip text.
	private void updateToolTipTextForChildren()
	{
		Component[] children= comboBox.getComponents();
		for (int i= 0; i < children.length; ++i)
		{
			if (children[i] instanceof JComponent)
			{
				((JComponent) children[i]).setToolTipText(
					comboBox.getToolTipText());
			}
		}
	}

	/**
	 * This layout manager handles the 'standard' layout of combo boxes.  It puts
	 * the arrow button to the right and the editor to the left.  If there is no
	 * editor it still keeps the arrow button to the right.
	 *
	 * This public inner class should be treated as protected. 
	 * Instantiate it only within subclasses of 
	 * <code>BasicComboBoxUI</code>.
	 */
	public class ComboBoxLayoutManager implements LayoutManager
	{
		public void addLayoutComponent(String name, Component comp)
		{
		}

		public void removeLayoutComponent(Component comp)
		{
		}

		public Dimension preferredLayoutSize(Container parent)
		{
			JComboBox cb= (JComboBox) parent;
			return parent.getPreferredSize();
		}

		public Dimension minimumLayoutSize(Container parent)
		{
			JComboBox cb= (JComboBox) parent;
			return parent.getMinimumSize();
		}

		public void layoutContainer(Container parent)
		{
			JComboBox cb= (JComboBox) parent;
			int width= cb.getWidth();
			int height= cb.getHeight();

			Insets insets= getInsets();
			int buttonSize= height - (insets.top + insets.bottom);
			Rectangle cvb;

			if (arrowButton != null)
			{
				if (isLeftToRight(cb))
				{
					arrowButton.setBounds(
						width - (insets.right + buttonSize),
						insets.top,
						buttonSize,
						buttonSize);
				}
				else
				{
					arrowButton.setBounds(
						insets.left,
						insets.top,
						buttonSize,
						buttonSize);
				}
			}
			if (editor != null)
			{
				cvb= rectangleForCurrentValue();
				cvb.x+=3;
				cvb.width-=6;
				editor.setBounds(cvb);
			}
		}
	}

	//
	// end Inner classes
	//====================

	//===============================
	// begin Sub-Component Management
	//

	/**
	 * Creates and initializes the components which make up the
	 * aggregate combo box. This method is called as part of the UI
	 * installation process.
	 */
	protected void installComponents()
	{
		arrowButton= createArrowButton();
		comboBox.add(arrowButton);

		if (arrowButton != null)
		{
			configureArrowButton();
		}

		if (comboBox.isEditable())
		{
			addEditor();
		}

		comboBox.add(currentValuePane);
		
		if(arrowButton!=null)	
			arrowButton.setBorder(null);
		if(editor!=null && (editor instanceof JComponent))
			((JComponent)editor).setBorder(null);
	}

	/**
	 * The aggregate components which compise the combo box are 
	 * unregistered and uninitialized. This method is called as part of the
	 * UI uninstallation process.
	 */
	protected void uninstallComponents()
	{
		if (arrowButton != null)
		{
			unconfigureArrowButton();
		}
		if (editor != null)
		{
			unconfigureEditor();
		}
		comboBox.removeAll(); // Just to be safe.
		arrowButton= null;
	}

	/**
	 * This public method is implementation specific and should be private.
	 * do not call or override. To implement a specific editor create a
	 * custom <code>ComboBoxEditor</code>
	 *
	 * @see javax.swing.JComboBox#setEditor
	 * @see javax.swing.ComboBoxEditor
	 */
	public void addEditor()
	{
		removeEditor();
		editor= comboBox.getEditor().getEditorComponent();
		if (editor != null)
		{
			configureEditor();
			comboBox.add(editor);
		}
	}

	/**
	 * This public method is implementation specific and should be private.
	 * do not call or override. 
	 */
	public void removeEditor()
	{
		if (editor != null)
		{
			unconfigureEditor();
			comboBox.remove(editor);
			editor= null;
		}
	}

	/**
	 * This protected method is implementation specific and should be private.
	 * do not call or override.
	 * 
	 * @see addEditor
	 */
	public void configureEditor()
	{
		// Should be in the same state as the combobox
		editor.setEnabled(comboBox.isEnabled());
		
		editor.setFont(comboBox.getFont());

		if(editor instanceof JComponent)
			((JComponent)editor).setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
			
		if (editor instanceof Accessible)
		{
			AccessibleContext ac= ((Accessible) editor).getAccessibleContext();
			if (ac != null)
			{
				ac.setAccessibleParent(comboBox);
			}
		}

		if (focusListener != null)
		{
			editor.addFocusListener(focusListener);
		}

		if (editorFocusListener == null)
		{
			editorFocusListener= new EditorFocusListener(comboBox);
			editor.addFocusListener(editorFocusListener);
		}

		comboBox.configureEditor(
			comboBox.getEditor(),
			comboBox.getSelectedItem());
	}

	/**
	 * This protected method is implementation specific and should be private.
	 * Do not call or override.
	 */
	public void unconfigureEditor()
	{
		if (focusListener != null)
		{
			editor.removeFocusListener(focusListener);
		}

		if (editorFocusListener != null)
		{
			editor.removeFocusListener(editorFocusListener);
			editorFocusListener= null;
		}
	}

	/**
	 * This public method is implementation specific and should be private. Do
	 * not call or override.
	 */
	public void configureArrowButton()
	{
		if (arrowButton != null)
		{
			arrowButton.setEnabled(comboBox.isEnabled());
			arrowButton.setRequestFocusEnabled(false);
			arrowButton.addMouseListener(popup.getMouseListener());
			arrowButton.addMouseMotionListener(popup.getMouseMotionListener());
			arrowButton.resetKeyboardActions();
		}
	}

	/**
	 * This public method is implementation specific and should be private. Do
	 * not call or override.
	 */
	public void unconfigureArrowButton()
	{
		if (arrowButton != null)
		{
			arrowButton.removeMouseListener(popup.getMouseListener());
			arrowButton.removeMouseMotionListener(popup.getMouseMotionListener());
		}
	}


	protected JButton createArrowButton() 
	{
		JButton button = new ComboBoxButton(comboBox, 
			UIManager.getIcon("ComboBox.icon"), comboBox.isEditable(), currentValuePane, listBox );
		button.setMargin( new Insets( 2, 3, 2, 5 ) );
		return button;
	}
		 

	//
	// end Sub-Component Management
	//===============================

	//================================
	// begin ComboBoxUI Implementation
	//

	/**
	 * Tells if the popup is visible or not.
	 */
	public boolean isPopupVisible(JComboBox c)
	{
		return popup.isVisible();
	}

	/**
	 * Hides the popup.
	 */
	public void setPopupVisible(JComboBox c, boolean v)
	{
		if (v)
		{
			popup.show();
		}
		else
		{
			popup.hide();
		}
	}

	/**
	 * Determines if the JComboBox is focus traversable.  If the JComboBox is editable
	 * this returns false, otherwise it returns true.
	 */
	public boolean isFocusTraversable(JComboBox c)
	{
		return true; //!comboBox.isEditable();
	}

	//
	// end ComboBoxUI Implementation
	//==============================

	//=================================
	// begin ComponentUI Implementation

	public void paint(Graphics g, JComponent c)
	{
		hasFocus= comboBox.hasFocus();
		if (!comboBox.isEditable())
		{
			Rectangle r= rectangleForCurrentValue();
			paintCurrentValueBackground(g, r, hasFocus);
			paintCurrentValue(g, r, hasFocus);
		}
	}
	

	public Dimension getPreferredSize(JComponent c)
	{
		return getMinimumSize(c);
	}

	/** 
	 * The minumum size is the size of the display area plus insets plus the button.
	 */
	public Dimension getMinimumSize(JComponent c)
	{
		if (!isMinimumSizeDirty)
		{
			return new Dimension(cachedMinimumSize);
		}
		Dimension size= getDisplaySize();
		Insets insets= getInsets();
		size.height += insets.top + insets.bottom;
		int buttonSize= size.height - (insets.top + insets.bottom);
		size.width += insets.left + insets.right + buttonSize;
		size.width+=6;

		cachedMinimumSize.setSize(size.width, size.height);
		isMinimumSizeDirty= false;

		return new Dimension(size);
	}

	public Dimension getMaximumSize(JComponent c)
	{
		return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
	}

	// This is currently hacky...
	public int getAccessibleChildrenCount(JComponent c)
	{
		if (comboBox.isEditable())
		{
			return 2;
		}
		else
		{
			return 1;
		}
	}

	// This is currently hacky...
	public Accessible getAccessibleChild(JComponent c, int i)
	{
		// 0 = the popup
		// 1 = the editor
		switch (i)
		{
			case 0 :
				if (popup instanceof Accessible)
				{
					AccessibleContext ac=
						((Accessible) popup).getAccessibleContext();
					ac.setAccessibleParent(comboBox);
					return (Accessible) popup;
				}
				break;
			case 1 :
				if (comboBox.isEditable() && (editor instanceof Accessible))
				{
					AccessibleContext ac=
						((Accessible) editor).getAccessibleContext();
					ac.setAccessibleParent(comboBox);
					return (Accessible) editor;
				}
				break;
		}
		return null;
	}

	//
	// end ComponentUI Implementation
	//===============================

	//======================
	// begin Utility Methods
	//

	/**
	 * Returns whether or not the supplied keyCode maps to a key that is used for
	 * navigation.  This is used for optimizing key input by only passing non-
	 * navigation keys to the type-ahead mechanism.  Subclasses should override this
	 * if they change the navigation keys.
	 */
	protected boolean isNavigationKey(int keyCode)
	{
		return keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN ||
		// This is horrible, but necessary since these aren't 
		// supported until JDK 1.2
		keyCode == KeyStroke.getKeyStroke("KP_UP").getKeyCode()
			|| keyCode == KeyStroke.getKeyStroke("KP_DOWN").getKeyCode();
	}

	/**
	 * Selects the next item in the list.  It won't change the selection if the
	 * currently selected item is already the last item.
	 */
	protected void selectNextPossibleValue()
	{
		int si;

		if (isTableCellEditor)
		{
			si= listBox.getSelectedIndex();
		}
		else
		{
			si= comboBox.getSelectedIndex();
		}

		if (si < comboBox.getModel().getSize() - 1)
		{
			if (isTableCellEditor)
			{
				listBox.setSelectedIndex(si + 1);
				listBox.ensureIndexIsVisible(si + 1);
			}
			else
			{
				comboBox.setSelectedIndex(si + 1);
			}
			comboBox.repaint();
		}
	}

	/**
	 * Selects the previous item in the list.  It won't change the selection if the
	 * currently selected item is already the first item.
	 */
	protected void selectPreviousPossibleValue()
	{
		int si;

		if (isTableCellEditor)
		{
			si= listBox.getSelectedIndex();
		}
		else
		{
			si= comboBox.getSelectedIndex();
		}

		if (si > 0)
		{
			if (isTableCellEditor)
			{
				listBox.setSelectedIndex(si - 1);
				listBox.ensureIndexIsVisible(si - 1);
			}
			else
			{
				comboBox.setSelectedIndex(si - 1);
			}

			comboBox.repaint();
		}
	}

	/**
	 * Hides the popup if it is showing and shows the popup if it is hidden.
	 */
	protected void toggleOpenClose()
	{
		setPopupVisible(comboBox, !isPopupVisible(comboBox));
	}

	/**
	 * Returns the area that is reserved for drawing the currently selected item.
	 */
	protected Rectangle rectangleForCurrentValue()
	{
		int width= comboBox.getWidth();
		int height= comboBox.getHeight();
		Insets insets= getInsets();
		int buttonSize= height - (insets.top + insets.bottom);
		if (arrowButton != null)
		{
			buttonSize= arrowButton.getWidth();
		}
		if (isLeftToRight(comboBox))
		{
			return new Rectangle(
				insets.left,
				insets.top,

⌨️ 快捷键说明

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