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

📄 comboboxui.java

📁 用于java swing的皮肤软件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
				width - (insets.left + insets.right + buttonSize),
				height - (insets.top + insets.bottom));
		}
		else
		{
			return new Rectangle(
				insets.left + buttonSize,
				insets.top,
				width - (insets.left + insets.right + buttonSize),
				height - (insets.top + insets.bottom));
		}
	}

	/**
	 * Gets the insets from the JComboBox.
	 */
	protected Insets getInsets()
	{
		return comboBox.getInsets();
	}

	//
	// end Utility Methods
	//====================

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

	/**
	 * Paints the currently selected item.
	 */
	public void paintCurrentValue(
		Graphics g,
		Rectangle bounds,
		boolean hasFocus)
	{
		ListCellRenderer renderer= comboBox.getRenderer();
		Component c;

		if (hasFocus && !isPopupVisible(comboBox))
		{
			c=
				renderer.getListCellRendererComponent(
					listBox,
					comboBox.getSelectedItem(),
					-1,
					true,
					false);
		}
		else
		{
			c=
				renderer.getListCellRendererComponent(
					listBox,
					comboBox.getSelectedItem(),
					-1,
					false,
					false);
			c.setBackground(UIManager.getColor("ComboBox.background"));
		}
		c.setFont(comboBox.getFont());
		if (hasFocus && !isPopupVisible(comboBox))
		{
			c.setForeground(listBox.getSelectionForeground());
			c.setBackground(listBox.getSelectionBackground());
		}
		else
		{
			if (comboBox.isEnabled())
			{
				c.setForeground(comboBox.getForeground());
				c.setBackground(comboBox.getBackground());
			}
			else
			{
				c.setForeground(UIManager.getColor("ComboBox.disabledForeground"));
				c.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
			}
		}

		// Fix for 4238829: should lay out the JPanel.
		boolean shouldValidate= false;
		if (c instanceof JPanel)
		{
			shouldValidate= true;
		}

		currentValuePane.paintComponent(
			g,
			c,
			comboBox,
			bounds.x+3,
			bounds.y,
			bounds.width,
			bounds.height,
			shouldValidate);
	}

	/**
	 * Paints the background of the currently selected item.
	 */
	public void paintCurrentValueBackground(
		Graphics g,
		Rectangle bounds,
		boolean hasFocus)
	{
		Color t= g.getColor();
		if (comboBox.isEnabled())
		{
			if (hasFocus && !isPopupVisible(comboBox))
				g.setColor(listBox.getSelectionBackground());
			else
				g.setColor(UIManager.getColor("ComboBox.background"));
		}
		else
			g.setColor(UIManager.getColor("ComboBox.disabledBackground"));
		g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
		g.setColor(t);
	}

	/**
	 * Repaint the currently selected item.
	 */
	void repaintMyCurrentValue()
	{
		Rectangle r= rectangleForCurrentValue();
		comboBox.repaint(r.x, r.y, r.width, r.height);
	}

	//
	// end Painting Utility Methods
	//=============================

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

	/** 
	 * Return the default size of an empty display area of the combo box using
	 * the current renderer and font.
	 *
	 * @return the size of an empty display area
	 * @see getDisplaySize
	 */
	protected Dimension getDefaultSize()
	{
		// Calculates the height and width using the default text renderer
		Component c=
			getDefaultListCellRenderer().getListCellRendererComponent(
				listBox,
				" ",
				-1,
				false,
				false);

		currentValuePane.add(c);
		c.setFont(comboBox.getFont());
		Dimension d= c.getPreferredSize();
		currentValuePane.remove(c);

		return new Dimension(d.width, d.height);
	}

	/** 
	 * Returns the calculated size of the display area. The display area is the
	 * portion of the combo box in which the selected item is displayed. This 
	 * method will use the prototype display value if it has been set. 
	 * <p>
	 * For combo boxes with a non trivial number of items, it is recommended to
	 * use a prototype display value to significantly speed up the display 
	 * size calculation.
	 * 
	 * @return the size of the display area calculated from the combo box items
	 * @see javax.swing.JComboBox#setPrototypeDisplayValue
	 */
	protected Dimension getDisplaySize()
	{
		if (!isDisplaySizeDirty)
		{
			return new Dimension(cachedDisplaySize);
		}
		Dimension result= new Dimension();

		ListCellRenderer renderer= comboBox.getRenderer();
		if (renderer == null)
		{
			renderer= new DefaultListCellRenderer();
		}

		Object prototypeValue= comboBox.getPrototypeDisplayValue();
		if (prototypeValue != null)
		{
			// Calculates the dimension based on the prototype value
			Component cpn=
				renderer.getListCellRendererComponent(
					listBox,
					prototypeValue,
					-1,
					false,
					false);
			currentValuePane.add(cpn);
			cpn.setFont(comboBox.getFont());
			result= cpn.getPreferredSize();
			currentValuePane.remove(cpn);

		}
		else
		{
			// Calculate the dimension by iterating over all the elements in the combo
			// box list.
			ComboBoxModel model= comboBox.getModel();
			int modelSize= model.getSize();

			Component cpn;
			Dimension d;

			if (modelSize > 0)
			{
				for (int i= 0; i < modelSize; i++)
				{
					// Calculates the maximum height and width based on the largest
					// element
					cpn=
						renderer.getListCellRendererComponent(
							listBox,
							model.getElementAt(i),
							-1,
							false,
							false);
					currentValuePane.add(cpn);
					cpn.setFont(comboBox.getFont());
					d= cpn.getPreferredSize();
					currentValuePane.remove(cpn);

					result.width= Math.max(result.width, d.width);
					result.height= Math.max(result.height, d.height);
				}
			}
			else
			{
				result= getDefaultSize();
				if (comboBox.isEditable())
				{
					result.width= 100;
				}
			}
			if (comboBox.isEditable())
			{
				d= editor.getPreferredSize();
				result.width= Math.max(result.width, d.width);
				result.height= Math.max(result.height, d.height);
			}

		}

		// Set the cached value
		cachedDisplaySize.setSize(result.width, result.height);
		isDisplaySizeDirty= false;

		return result;
	}

	//
	// end Size Utility Methods
	//=============================

	//=================================
	// begin Keyboard Action Management
	//

	/**
	 * Adds keyboard actions to the JComboBox.  Actions on enter and esc are already
	 * supplied.  Add more actions as you need them.
	 */
	protected void installKeyboardActions()
	{
		InputMap km= getMyInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
		SwingUtilities.replaceUIInputMap(
			comboBox,
			JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
			km);

		ActionMap am= getMyActionMap();
		if (am != null)
		{
			SwingUtilities.replaceUIActionMap(comboBox, am);
		}
	}

	InputMap getMyInputMap(int condition)
	{
		if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
		{
			return (InputMap) UIManager.get("ComboBox.ancestorInputMap");
		}
		return null;
	}

	ActionMap getMyActionMap()
	{
		return createMyActionMap();
	}

	static Action homeAction= new NavigationalAction(KeyEvent.VK_HOME);
	static Action endAction= new NavigationalAction(KeyEvent.VK_END);
	static Action pgUpAction= new NavigationalAction(KeyEvent.VK_PAGE_UP);
	static Action pgDownAction= new NavigationalAction(KeyEvent.VK_PAGE_DOWN);

	ActionMap createMyActionMap()
	{
		ActionMap map= new ActionMapUIResource();

		map.put("hidePopup", new HidePopupAction());
		map.put("pageDownPassThrough", pgDownAction);
		map.put("pageUpPassThrough", pgUpAction);
		map.put("homePassThrough", homeAction);
		map.put("endPassThrough", endAction);
		map.put("selectNext", new DownAction());
		map.put("togglePopup", new AltAction());
		map.put("spacePopup", new SpaceAction());
		map.put("selectPrevious", new UpAction());
		map.put("enterPressed", new EnterAction());

		return map;
	}

	boolean isMyTableCellEditor()
	{
		return isTableCellEditor;
	}

	/**
	 * Removes the focus InputMap and ActionMap.
	 */
	protected void uninstallKeyboardActions()
	{
		SwingUtilities.replaceUIInputMap(
			comboBox,
			JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
			null);
		SwingUtilities.replaceUIActionMap(comboBox, null);
	}

	//
	// Actions
	// 

	class HidePopupAction extends AbstractAction
	{
		public void actionPerformed(ActionEvent e)
		{
			JComboBox comboBox= (JComboBox) e.getSource();
			if (comboBox.isEnabled())
			{
				comboBox.setPopupVisible(false);
			}
		}

		public boolean isEnabled()
		{
			return comboBox.isPopupVisible();
		}
	}

	static class NavigationalAction extends AbstractAction
	{
		int keyCode;

		NavigationalAction(int keyCode)
		{
			this.keyCode= keyCode;
		}

		public void actionPerformed(ActionEvent ev)
		{
			JComboBox comboBox= (JComboBox) ev.getSource();
			int index= getNextIndex(comboBox);
			if (index >= 0 && index < comboBox.getItemCount())
			{
				comboBox.setSelectedIndex(index);
			}
		}

		int getNextIndex(JComboBox comboBox)
		{
			switch (keyCode)
			{
				case KeyEvent.VK_PAGE_UP :
					int listHeight= comboBox.getMaximumRowCount();
					int index= comboBox.getSelectedIndex() - listHeight;
					return (index < 0 ? 0 : index);
				case KeyEvent.VK_PAGE_DOWN :
					listHeight= comboBox.getMaximumRowCount();
					index= comboBox.getSelectedIndex() + listHeight;
					int max= comboBox.getItemCount();
					return (index < max ? index : max - 1);
				case KeyEvent.VK_HOME :
					return 0;
				case KeyEvent.VK_END :
					return comboBox.getItemCount() - 1;
				default :
					return comboBox.getSelectedIndex();
			}
		}
	}

	static class DownAction extends AbstractAction
	{
		public void actionPerformed(ActionEvent e)
		{
			JComboBox comboBox= (JComboBox) e.getSource();
			if (comboBox.isEnabled() && comboBox.isShowing())
			{
				if (comboBox.isPopupVisible())
				{
					ComboBoxUI ui= (ComboBoxUI) comboBox.getUI();
					ui.selectNextPossibleValue();
				}
				else
				{
					comboBox.setPopupVisible(true);
				}
			}
		}
	}

	static class EnterAction extends AbstractAction
	{
		public void actionPerformed(ActionEvent e)
		{
			JComboBox comboBox= (JComboBox) e.getSource();
			if (!comboBox.isEnabled())
			{
				return;
			}
			ComboBoxUI ui= (ComboBoxUI) comboBox.getUI();
			if (ui.isMyTableCellEditor())
			{
				// Forces the selection of the list item if the
				// combo box is in a JTable.
				comboBox.setSelectedIndex(ui.popup.getList().getSelectedIndex());
			}
			else
			{
				if (comboBox.isPopupVisible())
				{
					comboBox.setPopupVisible(false);
				}
				else
				{
					// Call the default button binding.
					// This is a pretty messy way of passing an event through
					// to the root pane.
					JRootPane root= SwingUtilities.getRootPane(comboBox);
					if (root != null)
					{
						InputMap im=
							root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
						ActionMap am= root.getActionMap();
						if (im != null && am != null)
						{
							Object obj=
								im.get(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
							if (obj != null)
							{
								Action action= am.get(obj);
								if (action != null)
								{
									action.actionPerformed(e);
								}
							}
						}
					}
				}
			}
		}
	}

	static class AltAction extends AbstractAction
	{
		public void actionPerformed(ActionEvent e)
		{
			JComboBox comboBox= (JComboBox) e.getSource();
			if (comboBox.isEnabled())
			{
				ComboBoxUI ui= (ComboBoxUI) comboBox.getUI();
				if (ui.isMyTableCellEditor())
				{
					// Forces the selection of the list item if the
					// combo box is in a JTable.
					comboBox.setSelectedIndex(ui.popup.getList().getSelectedIndex());
				}
				else
				{
					comboBox.setPopupVisible(!comboBox.isPopupVisible());
				}
			}
		}
	}

	// Same as the AltAction except that it doesn't invoke if
	// the space key is pressed in the editable text portion.
	static class SpaceAction extends AltAction
	{
		public void actionPerformed(ActionEvent e)
		{
			JComboBox comboBox= (JComboBox) e.getSource();
			if (!comboBox.isEditable())
			{
				super.actionPerformed(e);
			}
		}
	}

	static class UpAction extends AbstractAction
	{
		public void actionPerformed(ActionEvent e)
		{
			JComboBox comboBox= (JComboBox) e.getSource();
			if (comboBox.isEnabled())
			{
				ComboBoxUI ui= (ComboBoxUI) comboBox.getUI();
				if (ui.isPopupVisible(comboBox))
				{
					ui.selectPreviousPossibleValue();
				}
			}
		}
	}

	//
	// end Keyboard Action Management
	//===============================

	class EditorFocusListener extends FocusAdapter
	{
		private JComboBox comboBox;

		public EditorFocusListener(JComboBox combo)
		{
			this.comboBox= combo;
		}

		/**
		 * This will make the comboBox fire an ActionEvent if the editor
		 * value is different from the selected item in the model.  
		 * This allows for the entering of data in the combo box editor and
		 * sends notification when tabbing or clicking out of focus.
		 */
		public void focusLost(FocusEvent e)
		{
			ComboBoxEditor editor= comboBox.getEditor();
			Object item= editor.getItem();

			if (!e.isTemporary()
				&& item != null
				&& !item.equals(comboBox.getSelectedItem()))
			{
				comboBox.actionPerformed(
					new ActionEvent(
						editor,
						0,
						"",
						EventQueue.getMostRecentEventTime(),
						0));
			}
		}
	}


	/**	Convenience function for determining ComponentOrientation.  Helps us
	 * 	avoid having Munge directives throughout the code.
	 */
	static boolean isLeftToRight(Component c)
	{
		return c.getComponentOrientation().isLeftToRight();
	}
}

⌨️ 快捷键说明

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