Index: lutinutil/src/java/org/codelutin/util/FileCompletion.java diff -u /dev/null lutinutil/src/java/org/codelutin/util/FileCompletion.java:1.1 --- /dev/null Wed Jun 20 18:33:37 2007 +++ lutinutil/src/java/org/codelutin/util/FileCompletion.java Wed Jun 20 18:33:32 2007 @@ -0,0 +1,244 @@ +/** + *

+ * Class d'aide a la saisie de chemin de fichier + *

+ *

+ * Integration : + *

+ *
+ * (le 1er argument definit si l'on peut creer un fichier (ex : pour enregistrer))
+ * (le 2eme definit si la sorti est possible sans saisir un fichier)
+ * FileCompletion fc = new FileCompletion(true,true);
+ * if (fc.consoleAvailable())
+ *   System.out.print(fc.read());
+ * 
+ *

+ * Utilisation : + *

+ *

+ * ".." pour annuler ou pour revenir au repertoire précédent + *

+ *

+ * Entrer pour afficher la liste des fichiers, ou pour compléter le chemin + *

+ *

+ * Entrer pour afficher la liste des fichiers, ou pour compléter le chemin + *

+ * Saisir "!s" a la fin du nom de fichier pour l'enregistrer (si l'option est active) + *

+ *

+ * Saisir "!q" pour quitter et renvoyer null (si l'option est active) + *

+ * Limitation : + *

+ *

+ * FIXME: Si l'enregistrement est active, enregistrer un fichier finissant par "!s" est impossible + *

+ *

+ * FIXME: Ouvrir un fichier finissant par ".." l'est égualement + *

+ *

+ * FIXME: Si la sortie est ecive, un fichier "!q" ne peut pas etre utilise + *

+ * @autor Letellier Sylvain + */ + +package org.codelutin.util; + +import java.io.Console; +import java.io.File; +import java.io.FilenameFilter; +import java.io.IOException; +import java.util.Locale; +import java.util.ResourceBundle; + +import org.codelutin.i18n.I18n; + +public class FileCompletion { + private String line = null; + private boolean exit; + private boolean creation; + private ResourceBundle bundle; + private boolean consoleAvailable; + + private Console console; + + /** + * + * + * private Console console; /** constructeur + * + */ + public FileCompletion(Boolean creation, boolean exit) { + this.exit = exit; + this.creation = creation; + if (Locale.getDefault().getLanguage() == "fr") + I18n.init("fr", "FR"); + else + I18n.init("en", "US"); + bundle = ResourceBundle + .getBundle("org.codelutin.i18n.I18nBundleBridge"); + console = System.console(); + consoleAvailable = (console != null); + } + + /** + * demande a l'utilisateur de saisir un chemin + * + * @return + * @throws IOException + */ + public String read() throws IOException { + if (creation) + System.out.println(bundle.getString("save")); + System.out.println(bundle.getString("annuler")); + System.out.println(bundle.getString("entrer")); + line = System.getProperty("user.dir"); + File f = new File(line); + line = line + readLine("> " + line + f.separator); + f = new File(line); + String moreLastLine = line; + boolean isDirectory = true; + do { + if (f.isDirectory() || !f.exists()) { + String lastLine = line; + if (!f.exists() && !f.isDirectory()) { + File fTmp = f.getParentFile(); + String file = f.getName(); + if (file.matches("^.*\\!s$") && creation) + return f.getCanonicalPath().substring(0, + f.getCanonicalPath().length() - 2); + if (file.matches("^.*\\!q$") && exit) + return null; + if (file.matches("^.*\\.\\.$")) + line = fTmp.getCanonicalPath(); + else { + Filter filtre = new Filter(); + filtre.setFilter(file); + String[] listFichier = fTmp.list(filtre); + if (listFichier.length == 1) { + if (!(fTmp.getParentFile()==null)) + line = fTmp.getCanonicalPath() + f.separator + + listFichier[0]; + else + line = fTmp.getCanonicalPath() + listFichier[0]; + } else if (listFichier.length > 1) { + afficherListe(listFichier); + } else { + line = moreLastLine; + } + + } + } else if (f.exists() && f.isDirectory() + && !(f.getParentFile() == null)) { + Filter filtre = new Filter(); + filtre.setFilter(f.getName()); + String[] listFichier = f.getParentFile().list(filtre); + if (listFichier.length <= 1 + || line.charAt(line.length() - 1) == f.separatorChar) { + + line = f.getCanonicalPath(); + isDirectory = true; + } else if (listFichier.length > 1) { + isDirectory = false; + afficherListe(listFichier); + } + + } + moreLastLine = line; + f = new File(line); + if (f.isDirectory() && isDirectory + && !(line.charAt(line.length() - 1) == f.separatorChar)) + line = line + f.separator; + String read = readLine("> " + line); + line = line + read; + f = new File(line); + if (read.equals("") || read.equals(f.separator)) + afficherListe(f.list()); + + if (line.matches(".* " + f.separator + ".*") + || line.matches(".*" + f.separator + " .*")) + line = lastLine; + f = new File(line); + } + } while (!f.exists() || f.isDirectory()); + return line; + } + + /** + * + * Filtre les fichiers a afficher + * + */ + class Filter implements FilenameFilter { + private String filtre; + + /** + * @param File + * dir, String name + * @return boolean + */ + public boolean accept(File dir, String name) { + return name.startsWith(filtre); + } + + /** + * le parametre est le debut des noms de fichiers a afficher + * + * @param String + */ + public void setFilter(String s) { + filtre = s; + } + } + + /** + * + * @param String + * path + * @return String + */ + private String readLine(String path) { + String lineNonNull = null; + while (lineNonNull == null) + lineNonNull = console.readLine(path); + return lineNonNull; + } + + /** + * + * @return boolean + */ + public boolean consoleAvailable() { + return consoleAvailable; + } + + /** + * + * @param String[] + * listefichiers + */ + private void afficherListe(String[] listefichiers) { + if (listefichiers != null) { + int i = 0; + int nomMax = 0; + for (String l : listefichiers) + nomMax = Math.max(nomMax, l.length()); + // System.out.println(); + for (String l : listefichiers) { + String space = ""; + int nbSpace = (nomMax + 1) - l.length(); + for (int j = 0; j < nbSpace; j++) { + space += " "; + } + if (i++ < 2) + System.out.print(l + space); + else { + i = 0; + System.out.println(l); + } + } + System.out.println(); + } + } +}