📄 synthcomboboxui.java
字号:
/** * From BasicComboBoxRenderer v 1.18. * * Be aware that SynthFileChooserUIImpl relies on the fact that the default * renderer installed on a Synth combo box is a JLabel. If this is changed, * then an assert will fail in SynthFileChooserUIImpl */ private class SynthComboBoxRenderer extends JLabel implements ListCellRenderer, UIResource { public SynthComboBoxRenderer() { super(); setName("ComboBox.renderer"); setText(" "); } @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { setName("ComboBox.listRenderer"); SynthLookAndFeel.resetSelectedUI(); if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); if (!useListColors) { SynthLookAndFeel.setSelectedUI( (SynthLabelUI)SynthLookAndFeel.getUIOfType(getUI(), SynthLabelUI.class), isSelected, cellHasFocus, list.isEnabled(), false); } } else { setBackground(list.getBackground()); setForeground(list.getForeground()); } setFont(list.getFont()); if (value instanceof Icon) { setIcon((Icon)value); setText(""); } else { String text = (value == null) ? " " : value.toString(); if ("".equals(text)) { text = " "; } setText(text); } // The renderer component should inherit the enabled and // orientation state of its parent combobox. This is // especially needed for GTK comboboxes, where the // ListCellRenderer's state determines the visual state // of the combobox. if (comboBox != null){ setEnabled(comboBox.isEnabled()); setComponentOrientation(comboBox.getComponentOrientation()); } return this; } @Override public void paint(Graphics g) { super.paint(g); SynthLookAndFeel.resetSelectedUI(); } } /** * From BasicCombBoxEditor v 1.24. */ private static class SynthComboBoxEditor implements ComboBoxEditor, UIResource { protected JTextField editor; private Object oldValue; public SynthComboBoxEditor() { editor = new JTextField("",9); editor.setName("ComboBox.textField"); } @Override public Component getEditorComponent() { return editor; } /** * Sets the item that should be edited. * * @param anObject the displayed value of the editor */ @Override public void setItem(Object anObject) { String text; if ( anObject != null ) { text = anObject.toString(); oldValue = anObject; } else { text = ""; } // workaround for 4530952 if (!text.equals(editor.getText())) { editor.setText(text); } } @Override public Object getItem() { Object newValue = editor.getText(); if (oldValue != null && !(oldValue instanceof String)) { // The original value is not a string. Should return the value in it's // original type. if (newValue.equals(oldValue.toString())) { return oldValue; } else { // Must take the value from the editor and get the value and cast it to the new type. Class cls = oldValue.getClass(); try { Method method = cls.getMethod("valueOf", new Class[]{String.class}); newValue = method.invoke(oldValue, new Object[] { editor.getText()}); } catch (Exception ex) { // Fail silently and return the newValue (a String object) } } } return newValue; } @Override public void selectAll() { editor.selectAll(); editor.requestFocus(); } @Override public void addActionListener(ActionListener l) { editor.addActionListener(l); } @Override public void removeActionListener(ActionListener l) { editor.removeActionListener(l); } } /** * Handles all the logic for treating the combo as a button when it is * not editable, and when shouldActLikeButton() is true. This class is a * special ButtonModel, and installed on the arrowButton when appropriate. * It also is installed as a mouse listener and mouse motion listener on * the combo box. In this way, the state between the button and combo * are in sync. Whenever one is "over" both are. Whenever one is pressed, * both are. */ private final class ButtonHandler extends DefaultButtonModel implements MouseListener, PopupMenuListener { /** * Indicates that the mouse is over the combo or the arrow button. * This field only has meaning if buttonWhenNotEnabled is true. */ private boolean over; /** * Indicates that the combo or arrow button has been pressed. This * field only has meaning if buttonWhenNotEnabled is true. */ private boolean pressed; //------------------------------------------------------------------ // State Methods //------------------------------------------------------------------ /** * <p>Updates the internal "pressed" state. If shouldActLikeButton() * is true, and if this method call will change the internal state, * then the combo and button will be repainted.</p> * * <p>Note that this method is called either when a press event * occurs on the combo box, or on the arrow button.</p> */ private void updatePressed(boolean p) { this.pressed = p && isEnabled(); if (shouldActLikeButton()) { comboBox.repaint(); } } /** * <p>Updates the internal "over" state. If shouldActLikeButton() * is true, and if this method call will change the internal state, * then the combo and button will be repainted.</p> * * <p>Note that this method is called either when a mouseover/mouseoff event * occurs on the combo box, or on the arrow button.</p> */ private void updateOver(boolean o) { boolean old = isRollover(); this.over = o && isEnabled(); boolean newo = isRollover(); if (shouldActLikeButton() && old != newo) { comboBox.repaint(); } } //------------------------------------------------------------------ // DefaultButtonModel Methods //------------------------------------------------------------------ /** * {@inheritDoc} * * Ensures that isPressed() will return true if the combo is pressed, * or the arrowButton is pressed, <em>or</em> if the combo popup is * visible. This is the case because a combo box looks pressed when * the popup is visible, and so should the arrow button. */ @Override public boolean isPressed() { boolean b = shouldActLikeButton() ? pressed : super.isPressed(); return b || (pressedWhenPopupVisible && comboBox.isPopupVisible()); } /** * {@inheritDoc} * * Ensures that the armed state is in sync with the pressed state * if shouldActLikeButton is true. Without this method, the arrow * button will not look pressed when the popup is open, regardless * of the result of isPressed() alone. */ @Override public boolean isArmed() { boolean b = shouldActLikeButton() || (pressedWhenPopupVisible && comboBox.isPopupVisible()); return b ? isPressed() : super.isArmed(); } /** * {@inheritDoc} * * Ensures that isRollover() will return true if the combo is * rolled over, or the arrowButton is rolled over. */ @Override public boolean isRollover() { return shouldActLikeButton() ? over : super.isRollover(); } /** * {@inheritDoc} * * Forwards pressed states to the internal "pressed" field */ @Override public void setPressed(boolean b) { super.setPressed(b); updatePressed(b); } /** * {@inheritDoc} * * Forwards rollover states to the internal "over" field */ @Override public void setRollover(boolean b) { super.setRollover(b); updateOver(b); } //------------------------------------------------------------------ // MouseListener/MouseMotionListener Methods //------------------------------------------------------------------ @Override public void mouseEntered(MouseEvent mouseEvent) { updateOver(true); } @Override public void mouseExited(MouseEvent mouseEvent) { updateOver(false); } @Override public void mousePressed(MouseEvent mouseEvent) { updatePressed(true); } @Override public void mouseReleased(MouseEvent mouseEvent) { updatePressed(false); } @Override public void mouseClicked(MouseEvent e) {} //------------------------------------------------------------------ // PopupMenuListener Methods //------------------------------------------------------------------ /** * @inheritDoc * * Ensures that the combo box is repainted when the popup is closed. * This avoids a bug where clicking off the combo wasn't causing a repaint, * and thus the combo box still looked pressed even when it was not. * * This bug was only noticed when acting as a button, but may be generally * present. If so, remove the if() block */ @Override public void popupMenuCanceled(PopupMenuEvent e) { if (shouldActLikeButton() || pressedWhenPopupVisible) { comboBox.repaint(); } } @Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) {} @Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {} } /** * Handler for repainting combo when editor component gains/looses focus */ private static class EditorFocusHandler implements FocusListener, PropertyChangeListener { private JComboBox comboBox; private ComboBoxEditor editor = null; private Component editorComponent = null; private EditorFocusHandler(JComboBox comboBox) { this.comboBox = comboBox; editor = comboBox.getEditor(); if (editor != null){ editorComponent = editor.getEditorComponent(); if (editorComponent != null){ editorComponent.addFocusListener(this); } } comboBox.addPropertyChangeListener("editor",this); } public void unregister(){ comboBox.removePropertyChangeListener(this); if (editorComponent!=null){ editorComponent.removeFocusListener(this); } } /** Invoked when a component gains the keyboard focus. */ public void focusGained(FocusEvent e) { // repaint whole combo on focus gain comboBox.repaint(); } /** Invoked when a component loses the keyboard focus. */ public void focusLost(FocusEvent e) { // repaint whole combo on focus loss comboBox.repaint(); } /** * Called when the combos editor changes * * @param evt A PropertyChangeEvent object describing the event source and * the property that has changed. */ public void propertyChange(PropertyChangeEvent evt) { ComboBoxEditor newEditor = comboBox.getEditor(); if (editor != newEditor){ if (editorComponent!=null){ editorComponent.removeFocusListener(this); } editor = newEditor; if (editor != null){ editorComponent = editor.getEditorComponent(); if (editorComponent != null){ editorComponent.addFocusListener(this); } } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -