/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Copyright (C) 2011-2013 Marchand Eric <ricoh51@free.fr>
This file is part of Freegressi.
Freegressi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Freegressi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Freegressi. If not, see <http://www.gnu.org/licenses/>.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package freegressi.utils;
import freegressi.tableur.JDialogFunctions;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.text.DecimalFormat;
import javax.swing.text.JTextComponent;
/**
* Quelques méthodes appelées partout
* @author marchand
*/
public class Utils {
/**
* Insert un caractère grec dans un JTextComponent, en proposant
* la boite de dialogue d'insertion d'un caractère grec.
* @param textComponent le JTextComponent
*/
public static void insertGreekCaracter(JTextComponent textComponent){
JDialogCarGrecs jdcg = new JDialogCarGrecs(null);
jdcg.setVisible(true);
if (jdcg.isOk()) {
char carGrec = jdcg.getCaractereGrec();
int n = textComponent.getCaretPosition();
StringBuilder sb = new StringBuilder(textComponent.getText());
sb.insert(n, carGrec);
textComponent.setText(sb.toString());
// rendre le focus et positionner le caret
textComponent.requestFocus();
textComponent.setCaretPosition(n + 1);
}
jdcg.dispose();
}
/**
* Insert une fonction dans un JTextComponent, en proposant
* la boite de dialogue d'insertion d'une fonction.
* @param textComponent le JTextComponent
*/
public static void insertFunction(JTextComponent textComponent){
JDialogFunctions jdf = new JDialogFunctions(null, true);
jdf.setVisible(true);
String strFunc = jdf.getText();
int n = textComponent.getCaretPosition();
StringBuilder sb = new StringBuilder(textComponent.getText());
sb.insert(n, strFunc);
textComponent.setText(sb.toString());
// rendre le focus et positionner le caret
textComponent.requestFocus();
textComponent.setCaretPosition(n + strFunc.length());
jdf.dispose();
}
/**
* Renvoit une chaine composée de n zéro
* @param n le nombre de zéro
* @return la chaine composée de n zéro
*/
private static String donneNZero(int n) {
String str = "";
for (int i = 0; i < n; i++) {
str += "0";
}
return str;
}
/**
* Remplace les virgules par des points dans une chaîne
* @param number la chaîne à traiter
* @return une chaîne avec les virgules remplacées par des points
*/
public static String replaceVirguleToPoint(String number){
return number.replace(',', '.');
}
/**
* Formatte un double pour l'affichage
* @param nombre le double
* @param nombreCS le nombre de chiffres significatifs
* @return
*/
public static String formatteNombre(double nombre, int nombreCS) {
//@TODO découper à 0.005 0.05 0.5 ...
DecimalFormat df;
// faire l'arrondi
DecimalFormat df1 = new DecimalFormat("0" + ((nombreCS > 1) ? "." + donneNZero(nombreCS - 1) : "") + "E0");
String str = df1.format(nombre);
//System.out.println(str);
str = str.replace(',', '.');
//System.out.println(str);
Double dou;
try{
dou = Double.parseDouble(str);
}catch (NumberFormatException e){
return "";
}
// if (dou == Double.POSITIVE_INFINITY){
// dou = Double.MAX_VALUE;
// } else if (dou == Double.NEGATIVE_INFINITY){
// dou = Double.MIN_VALUE;
// }
//System.out.println(dou);
double abs = Math.abs(dou);
if (abs < 0.01) {
df = new DecimalFormat("0" + ((nombreCS > 1) ? "." + donneNZero(nombreCS - 1) : "") + "E0");
} else if (abs < 0.1) {
df = new DecimalFormat("0.0" + donneNZero(nombreCS));
} else if (abs < 1) {
df = new DecimalFormat("0." + donneNZero(nombreCS));
} //else if (abs < 10) df = new DecimalFormat("0."+donneNZero(nombreCS - 1));
else if (abs < 10) {
//System.out.println("Inférieur à 10");
df = new DecimalFormat("0" + ((nombreCS > 1) ? "." + donneNZero(nombreCS - 1) : "E0"));
} else if (abs < 100) {
if (nombreCS == 1) {
df = new DecimalFormat("0E0");
} else if (nombreCS == 2) {
df = new DecimalFormat("00E0");
} else {
df = new DecimalFormat("00." + donneNZero(nombreCS - 2));
}
} else if (abs < 1000) {
if (nombreCS == 1) {
df = new DecimalFormat("0E0");
} else if (nombreCS == 2) {
df = new DecimalFormat("0.0E0");
} else if (nombreCS == 3) {
df = new DecimalFormat("000E0");
} else {
df = new DecimalFormat("000" + donneNZero(nombreCS - 3));
}
} else {
if (nombreCS == 1) {
df = new DecimalFormat("0E0");
} else {
df = new DecimalFormat("0." + donneNZero(nombreCS - 1) + "E0");
}
}
str = df.format(dou).toString();
// Retirer le E0 en fin de chaine s'il existe
if (str.endsWith("E0")) {
//System.out.println("Retire E0");
str = str.substring(0, str.length() - 2);
}
return str;
}
private static String convertStreamToString(InputStream is) throws IOException {
if (is != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
return writer.toString();
}else {
return "";
}
}
public static String fileToString(String fileName){
String result;
try {
InputStream is = new FileInputStream(fileName);
result = convertStreamToString(is);
} catch (Exception e) {
throw new RuntimeException("Impossible de lire la ressource : " + fileName);
}
return result;
}
}