/*
 * Copyright (c) 2010 Clio Cidral Junior
 * 
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 * 
 *        http://www.apache.org/licenses/LICENSE-2.0
 * 
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.tomighty.ui.util;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.NumberFormat;

import javax.swing.JFormattedTextField;
import javax.swing.JTextField;

public class FieldFactory {
	
	public static JFormattedTextField createIntegerField(int minValue, int maxLength) {
		JFormattedTextField field = new JFormattedTextField(NumberFormat.getIntegerInstance());
		field.setHorizontalAlignment(JTextField.CENTER);
		field.addKeyListener(new FieldListener(maxLength));
		return field;
	}
	
	private static class FieldListener extends KeyAdapter {
		
		private final int maxLength;

		public FieldListener(int maxLength) {
			this.maxLength = maxLength;
		}

		@Override
		public void keyTyped(KeyEvent e) {
			if(!Character.isDigit(e.getKeyChar())) {
				e.consume();
				return;
			}
			JTextField field = (JTextField) e.getSource();
			String text = field.getText();
			if(text == null) return;
			boolean reachedMaxLength = text.length() >= maxLength;
			boolean noTextSelected = field.getSelectedText() == null;
			if(reachedMaxLength && noTextSelected) {
				e.consume();
			}
		}
		
	}

}

@@ -778,16 +778,50 @@
 Format;%0D
+%0Aimport java.text.ParseException;%0D
 %0A%0D%0Aimpor
@@ -876,25 +876,126 @@
 .swing.J
-TextField
+FormattedTextField.AbstractFormatter;%0D%0Aimport javax.swing.JTextField;%0D%0Aimport javax.swing.text.NumberFormatter
 ;%0D%0A%0D%0Apub
@@ -1109,16 +1109,80 @@
 gth) %7B%0D%0A
+%09%09IntegerFormatter formatter = new IntegerFormatter(minValue);%0D%0A
 %09%09JForma
@@ -1231,41 +1231,17 @@
 eld(
-NumberFormat.getIntegerInstance()
+formatter
 );%0D%0A
@@ -1371,16 +1371,795 @@
 %0A%09%7D%0D%0A%09%0D%0A
+%09@SuppressWarnings(%22serial%22)%0D%0A%09private static class IntegerFormatter extends AbstractFormatter %7B%0D%0A%09%09%0D%0A%09%09private final int minValue;%0D%0A%09%09private final NumberFormatter numberFormatter;%0D%0A%0D%0A%09%09public IntegerFormatter(int minValue) %7B%0D%0A%09%09%09this.minValue = minValue;%0D%0A%09%09%09numberFormatter = new NumberFormatter(NumberFormat.getIntegerInstance());%0D%0A%09%09%7D%0D%0A%0D%0A%09%09@Override%0D%0A%09%09public Object stringToValue(String text) throws ParseException %7B%0D%0A%09%09%09Number value = (Number) numberFormatter.stringToValue(text);%0D%0A%09%09%09if (value.intValue() %3C minValue) %7B%0D%0A%09%09%09%09throw new ParseException(%22Value cannot be less than %22+minValue, 0);%0D%0A%09%09%09%7D%0D%0A%09%09%09return value;%0D%0A%09%09%7D%0D%0A%0D%0A%09%09@Override%0D%0A%09%09public String valueToString(Object value) throws ParseException %7B%0D%0A%09%09%09return numberFormatter.valueToString(value);%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%7D%0D%0A%09%0D%0A
 %09private

Fix user being able to save times with zero minutes