KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > handlers > DialogIntegerValueConverter


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.cheatsheets.handlers;
12
13 import org.eclipse.core.commands.AbstractParameterValueConverter;
14 import org.eclipse.core.commands.ParameterValueConversionException;
15
16 /**
17  * A command parameter value converter to convert between Integers and their
18  * String representations for use in the open dialog commands.
19  *
20  * @since 3.2
21  */

22 public class DialogIntegerValueConverter extends
23         AbstractParameterValueConverter {
24
25     public Object JavaDoc convertToObject(String JavaDoc parameterValue)
26             throws ParameterValueConversionException {
27
28         try {
29             int i = Integer.parseInt(parameterValue);
30             return new Integer JavaDoc(i);
31         } catch (NumberFormatException JavaDoc ex) {
32             throw new ParameterValueConversionException(
33                     "error converting to integer: " + parameterValue); //$NON-NLS-1$
34
}
35     }
36
37     public String JavaDoc convertToString(Object JavaDoc parameterValue)
38             throws ParameterValueConversionException {
39
40         if (!(parameterValue instanceof Integer JavaDoc)) {
41             throw new ParameterValueConversionException(
42                     "value for conversion must be an Integer"); //$NON-NLS-1$
43
}
44
45         Integer JavaDoc i = (Integer JavaDoc) parameterValue;
46         return Integer.toString(i.intValue());
47     }
48
49 }
50
Popular Tags