1 /*******************************************************************************
2 * Copyright (C) 2008, 2016, Shawn O. Pearce <spearce@spearce.org> and others
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
10 *******************************************************************************/
11 package org
.eclipse
.egit
.ui
.internal
.preferences
;
13 import org
.eclipse
.core
.runtime
.Assert
;
14 import org
.eclipse
.jface
.preference
.StringFieldEditor
;
15 import org
.eclipse
.jface
.resource
.JFaceResources
;
16 import org
.eclipse
.swt
.widgets
.Composite
;
17 import org
.eclipse
.swt
.widgets
.Text
;
19 class StorageSizeFieldEditor
extends StringFieldEditor
{
20 private static final int KB
= 1024;
22 private static final int MB
= 1024 * KB
;
24 private static final int GB
= 1024 * MB
;
26 private final int minValidValue
;
28 private final int maxValidValue
;
30 StorageSizeFieldEditor(String name
, String labelText
, Composite parent
,
32 Assert
.isTrue(min
> 0 && min
< max
);
33 init(name
, labelText
);
35 setEmptyStringAllowed(false);
37 JFaceResources
.format("IntegerFieldEditor.errorMessageRange", //$NON-NLS-1$
38 new Object
[] { Integer
.valueOf(min
),
39 Integer
.valueOf(max
) }));
40 createControl(parent
);
46 protected boolean checkState() {
47 final Text text
= getTextControl();
51 final String numberString
= text
.getText();
52 final int number
= parse(numberString
);
53 if (checkValue(number
)) {
62 * Verify this value is acceptable.
65 * the value parsed from the input.
66 * @return true if the value is OK; false otherwise.
68 protected boolean checkValue(final int number
) {
69 return number
>= minValidValue
&& number
<= maxValidValue
;
73 protected void doLoad() {
74 final Text text
= getTextControl();
76 int value
= getPreferenceStore().getInt(getPreferenceName());
77 text
.setText(format(value
));
82 protected void doLoadDefault() {
83 final Text text
= getTextControl();
85 int value
= getPreferenceStore().getDefaultInt(getPreferenceName());
86 text
.setText(format(value
));
92 protected void doStore() {
93 final Text text
= getTextControl();
95 final int v
= parse(text
.getText());
96 getPreferenceStore().setValue(getPreferenceName(), v
);
100 private String
format(int value
) {
101 if (value
> GB
&& (value
/ GB
) * GB
== value
)
102 return String
.valueOf(value
/ GB
) + " g"; //$NON-NLS-1$
103 if (value
> MB
&& (value
/ MB
) * MB
== value
)
104 return String
.valueOf(value
/ MB
) + " m"; //$NON-NLS-1$
105 if (value
> KB
&& (value
/ KB
) * KB
== value
)
106 return String
.valueOf(value
/ KB
) + " k"; //$NON-NLS-1$
107 return String
.valueOf(value
);
110 private int parse(final String str
) {
111 String n
= str
.trim();
116 char lastChar
= n
.charAt(n
.length() - 1);
117 switch (Character
.toLowerCase(lastChar
)) {
128 if (Character
.isDigit(lastChar
)) {
131 return 0; // Invalid input
134 n
= n
.substring(0, n
.length() - 1).trim();
139 return mul
* Integer
.parseInt(n
);
140 } catch (NumberFormatException nfe
) {