1 30 31 package com.jgoodies.looks.common; 32 33 import java.awt.AWTEvent ; 34 import java.awt.Component ; 35 import java.awt.Container ; 36 import java.awt.FocusTraversalPolicy ; 37 import java.awt.KeyboardFocusManager ; 38 import java.awt.event.ActionEvent ; 39 import java.awt.event.MouseEvent ; 40 import java.awt.event.MouseListener ; 41 import java.text.AttributedCharacterIterator ; 42 import java.text.CharacterIterator ; 43 import java.text.DateFormat ; 44 import java.text.Format ; 45 import java.text.ParseException ; 46 import java.util.Calendar ; 47 import java.util.Map ; 48 49 import javax.swing.*; 50 import javax.swing.text.InternationalFormatter ; 51 52 74 public final class ExtBasicArrowButtonHandler extends AbstractAction implements MouseListener { 75 76 private final javax.swing.Timer autoRepeatTimer; 77 private final boolean isNext; 78 79 private JSpinner spinner = null; 80 81 82 public ExtBasicArrowButtonHandler(String name, boolean isNext) { 83 super(name); 84 this.isNext = isNext; 85 autoRepeatTimer = new javax.swing.Timer (60, this); 86 autoRepeatTimer.setInitialDelay(300); 87 } 88 89 90 private JSpinner eventToSpinner(AWTEvent e) { 91 Object src = e.getSource(); 92 while ((src instanceof Component ) && !(src instanceof JSpinner)) { 93 src = ((Component ) src).getParent(); 94 } 95 return (src instanceof JSpinner) ? (JSpinner) src : null; 96 } 97 98 99 public void actionPerformed(ActionEvent e) { 100 JSpinner aSpinner = this.spinner; 101 102 if (!(e.getSource() instanceof javax.swing.Timer )) { 103 aSpinner = eventToSpinner(e); 105 } 106 if (aSpinner != null) { 107 try { 108 int calendarField = getCalendarField(aSpinner); 109 aSpinner.commitEdit(); 110 if (calendarField != -1) { 111 ((SpinnerDateModel) aSpinner.getModel()).setCalendarField(calendarField); 112 } 113 Object value = (isNext) ? aSpinner.getNextValue() : aSpinner.getPreviousValue(); 114 if (value != null) { 115 aSpinner.setValue(value); 116 select(aSpinner); 117 } 118 } catch (IllegalArgumentException iae) { 119 UIManager.getLookAndFeel().provideErrorFeedback(aSpinner); 120 } catch (ParseException pe) { 121 UIManager.getLookAndFeel().provideErrorFeedback(aSpinner); 122 } 123 } 124 } 125 126 127 131 private void select(JSpinner aSpinner) { 132 JComponent editor = aSpinner.getEditor(); 133 134 if (editor instanceof JSpinner.DateEditor) { 135 JSpinner.DateEditor dateEditor = (JSpinner.DateEditor) editor; 136 JFormattedTextField ftf = dateEditor.getTextField(); 137 Format format = dateEditor.getFormat(); 138 Object value; 139 140 if (format != null && (value = aSpinner.getValue()) != null) { 141 SpinnerDateModel model = dateEditor.getModel(); 142 DateFormat.Field field = DateFormat.Field.ofCalendarField(model.getCalendarField()); 143 144 if (field != null) { 145 try { 146 AttributedCharacterIterator iterator = 147 format.formatToCharacterIterator(value); 148 if (!select(ftf, iterator, field) && field == DateFormat.Field.HOUR0) { 149 select(ftf, iterator, DateFormat.Field.HOUR1); 150 } 151 } catch (IllegalArgumentException iae) { 152 } 154 } 155 } 156 } 157 } 158 159 160 163 private boolean select( 164 JFormattedTextField ftf, 165 AttributedCharacterIterator iterator, 166 DateFormat.Field field) { 167 int max = ftf.getDocument().getLength(); 168 169 iterator.first(); 170 do { 171 Map attrs = iterator.getAttributes(); 172 173 if (attrs != null && attrs.containsKey(field)) { 174 int start = iterator.getRunStart(field); 175 int end = iterator.getRunLimit(field); 176 177 if (start != -1 && end != -1 && start <= max && end <= max) { 178 ftf.select(start, end); 179 } 180 return true; 181 } 182 } while (iterator.next() != CharacterIterator.DONE); 183 return false; 184 } 185 186 187 192 private int getCalendarField(JSpinner aSpinner) { 193 JComponent editor = aSpinner.getEditor(); 194 195 if (editor instanceof JSpinner.DateEditor) { 196 JSpinner.DateEditor dateEditor = (JSpinner.DateEditor) editor; 197 JFormattedTextField ftf = dateEditor.getTextField(); 198 int start = ftf.getSelectionStart(); 199 JFormattedTextField.AbstractFormatter formatter = ftf.getFormatter(); 200 201 if (formatter instanceof InternationalFormatter ) { 202 Format.Field [] fields = ((InternationalFormatter ) formatter).getFields(start); 203 204 for (int counter = 0; counter < fields.length; counter++) { 205 if (fields[counter] instanceof DateFormat.Field ) { 206 int calendarField; 207 208 if (fields[counter] == DateFormat.Field.HOUR1) { 209 calendarField = Calendar.HOUR; 210 } else { 211 calendarField = ((DateFormat.Field ) fields[counter]).getCalendarField(); 212 } 213 if (calendarField != -1) { 214 return calendarField; 215 } 216 } 217 } 218 } 219 } 220 return -1; 221 } 222 223 public void mousePressed(MouseEvent e) { 224 if (SwingUtilities.isLeftMouseButton(e) && e.getComponent().isEnabled()) { 225 spinner = eventToSpinner(e); 226 autoRepeatTimer.start(); 227 focusSpinnerIfNecessary(); 228 } 229 } 230 231 public void mouseReleased(MouseEvent e) { 232 autoRepeatTimer.stop(); 233 spinner = null; 234 } 235 236 public void mouseClicked(MouseEvent e) { 237 } 239 240 public void mouseEntered(MouseEvent e) { 241 } 243 244 public void mouseExited (MouseEvent e) { 245 } 247 248 249 253 private void focusSpinnerIfNecessary() { 254 Component fo = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 255 if (spinner.isRequestFocusEnabled() 256 && (fo == null || !SwingUtilities.isDescendingFrom(fo, spinner))) { 257 Container root = spinner; 258 259 if (!root.isFocusCycleRoot()) { 260 root = root.getFocusCycleRootAncestor(); 261 } 262 if (root != null) { 263 FocusTraversalPolicy ftp = root.getFocusTraversalPolicy(); 264 Component child = ftp.getComponentAfter(root, spinner); 265 266 if (child != null && SwingUtilities.isDescendingFrom(child, spinner)) { 267 child.requestFocus(); 268 } 269 } 270 } 271 } 272 273 } | Popular Tags |