Have icon for "reset" entry in reflog
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / preferences / StorageSizeFieldEditor.java
blob2e5013ebf3a3f5aee0dba8a4d128c69bf94ad062
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,
31 int min, int max) {
32 Assert.isTrue(min > 0 && min < max);
33 init(name, labelText);
34 setTextLimit(10);
35 setEmptyStringAllowed(false);
36 setErrorMessage(
37 JFaceResources.format("IntegerFieldEditor.errorMessageRange", //$NON-NLS-1$
38 new Object[] { Integer.valueOf(min),
39 Integer.valueOf(max) }));
40 createControl(parent);
41 minValidValue = min;
42 maxValidValue = max;
45 @Override
46 protected boolean checkState() {
47 final Text text = getTextControl();
48 if (text == null)
49 return false;
51 final String numberString = text.getText();
52 final int number = parse(numberString);
53 if (checkValue(number)) {
54 clearErrorMessage();
55 return true;
57 showErrorMessage();
58 return false;
61 /**
62 * Verify this value is acceptable.
64 * @param number
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;
72 @Override
73 protected void doLoad() {
74 final Text text = getTextControl();
75 if (text != null) {
76 int value = getPreferenceStore().getInt(getPreferenceName());
77 text.setText(format(value));
81 @Override
82 protected void doLoadDefault() {
83 final Text text = getTextControl();
84 if (text != null) {
85 int value = getPreferenceStore().getDefaultInt(getPreferenceName());
86 text.setText(format(value));
88 valueChanged();
91 @Override
92 protected void doStore() {
93 final Text text = getTextControl();
94 if (text != null) {
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();
112 if (n.length() == 0)
113 return 0;
115 int mul = 1;
116 char lastChar = n.charAt(n.length() - 1);
117 switch (Character.toLowerCase(lastChar)) {
118 case 'g':
119 mul = GB;
120 break;
121 case 'm':
122 mul = MB;
123 break;
124 case 'k':
125 mul = KB;
126 break;
127 default:
128 if (Character.isDigit(lastChar)) {
129 break;
131 return 0; // Invalid input
133 if (mul > 1)
134 n = n.substring(0, n.length() - 1).trim();
135 if (n.length() == 0)
136 return 0;
138 try {
139 return mul * Integer.parseInt(n);
140 } catch (NumberFormatException nfe) {
141 return 0;