Refactor the class GitDecoratorPreferencePage
[egit/chris.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / preferences / StorageSizeFieldEditor.java
blob584dcc81bd0d101a68a7305d4b1752a1e95515ed
1 /*******************************************************************************
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9 package org.eclipse.egit.ui.internal.preferences;
11 import org.eclipse.jface.preference.StringFieldEditor;
12 import org.eclipse.jface.resource.JFaceResources;
13 import org.eclipse.swt.widgets.Composite;
14 import org.eclipse.swt.widgets.Text;
16 class StorageSizeFieldEditor extends StringFieldEditor {
17 private static final int KB = 1024;
19 private static final int MB = 1024 * KB;
21 private static final int GB = 1024 * MB;
23 private final int minValidValue;
25 private final int maxValidValue;
27 StorageSizeFieldEditor(String name, String labelText, Composite parent,
28 int min, int max) {
29 init(name, labelText);
30 setTextLimit(10);
31 setEmptyStringAllowed(false);
32 setErrorMessage(JFaceResources
33 .getString("IntegerFieldEditor.errorMessage"));
34 createControl(parent);
35 minValidValue = min;
36 maxValidValue = max;
39 protected boolean checkState() {
40 final Text text = getTextControl();
41 if (text == null)
42 return false;
44 final String numberString = text.getText();
45 final int number = parse(numberString);
46 if (checkValue(number)) {
47 clearErrorMessage();
48 return true;
50 showErrorMessage();
51 return false;
54 /**
55 * Verify this value is acceptable.
57 * @param number
58 * the value parsed from the input.
59 * @return true if the value is OK; false otherwise.
61 protected boolean checkValue(final int number) {
62 return number >= minValidValue && number <= maxValidValue;
65 protected void doLoad() {
66 final Text text = getTextControl();
67 if (text != null) {
68 int value = getPreferenceStore().getInt(getPreferenceName());
69 text.setText(format(value));
73 protected void doLoadDefault() {
74 final Text text = getTextControl();
75 if (text != null) {
76 int value = getPreferenceStore().getDefaultInt(getPreferenceName());
77 text.setText(format(value));
79 valueChanged();
82 protected void doStore() {
83 final Text text = getTextControl();
84 if (text != null) {
85 final int v = parse(text.getText());
86 getPreferenceStore().setValue(getPreferenceName(), v);
90 private String format(int value) {
91 if (value > GB && (value / GB) * GB == value)
92 return String.valueOf(value / GB) + " g";
93 if (value > MB && (value / MB) * MB == value)
94 return String.valueOf(value / MB) + " m";
95 if (value > KB && (value / KB) * KB == value)
96 return String.valueOf(value / KB) + " k";
97 return String.valueOf(value);
100 private int parse(final String str) {
101 String n = str.trim();
102 if (n.length() == 0)
103 return 0;
105 int mul = 1;
106 switch (Character.toLowerCase(n.charAt(n.length() - 1))) {
107 case 'g':
108 mul = GB;
109 break;
110 case 'm':
111 mul = MB;
112 break;
113 case 'k':
114 mul = KB;
115 break;
117 if (mul > 1)
118 n = n.substring(0, n.length() - 1).trim();
119 if (n.length() == 0)
120 return 0;
122 try {
123 return mul * Integer.parseInt(n);
124 } catch (NumberFormatException nfe) {
125 return 0;