1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class TableModelEx extends AbstractTableModel {
private Object[][] data; // données de la JTable
private String[] title = {"InOut", "Type", "Nom", "Description"}; // entete de la JTable
private ArrayList<TabLine> plist; // list de toutes les lignes récupérée
private JTable jtableLink; // lien vers le JTree
// *************************************
//*
public TableModelEx() {
data = new String[0][0];
plist = new ArrayList<TabLine>();
}
@Override
public int getColumnCount() {
return this.title.length;
}
@Override
public int getRowCount() {
return this.data.length;
}
@Override
public Object getValueAt(int row, int col) {
return this.data[row][col];
}
public String getColumnName(int col) {
return this.title[col];
}
// */
// **********************************
// ajoute ligne au tableau
public void addRow(String str){
TabLine tabLine = new TabLine(str);
plist.add(tabLine);
// on n'affiche pas les entrées non valide
if(!tabLine.pIsValid){
return;
}
int indice = 0, nbRow = this.getRowCount(), nbCol = this.getColumnCount();
Object temp[][] = this.data;
this.data = new Object[nbRow+1][nbCol];
for(Object[] value : temp)
this.data[indice++] = value;
this.data[indice][0] = tabLine.pinOut;
this.data[indice][1] = tabLine.ptype;
this.data[indice][2] = tabLine.pfuncName;
this.data[indice][3] = tabLine.pfuncDesc;
temp = null;
//Cette méthode permet d'avertir le tableau que les données ont été modifiées
//Ce qui permet une mise à jours complète du tableau
this.fireTableDataChanged();
if (jtableLink != null){
addElementToJTree(tabLine); // TODO : ajout de l'element au JTree
}
}
// *******************************************
// *******************************************
// objet pour gérer une ligne
private class TabLine {
// *************************
// constructeurs
public String pline;
public String pinOut;
public String ptype;
public String pfuncName;
public String pfuncDesc;
public Boolean pIsValid;
public TabLine(){
initVal();
}
public TabLine(String str){
initVal();
setLine(str);
}
// ************************
// initialisation des valeurs
private void initVal(){
pline = "";
pinOut = "";
ptype = "";
pfuncName = "";
pfuncDesc = "";
pIsValid = false;
}
// *************************
// accesseur
void setLine(String line){
pline = line;
parseLine();
}
// *************************
// parse la ligne
private void parseLine(){
// ***********************************************
String pattern = "(IN|OUT|)\t" // la ligne commence par "IN" "OUT" ou ""
+ "(ERR|)\t" // suivit de "ERR ou ""
+ "(\\w+)\t" // suivit de Un mot
+ "(\\w+[^\t]*)"; // suivit de Un mot, éventuellement suivi de n'importe quel caractère sauf \t(tab).
pinOut = "";
ptype = "";
pfuncName = "";
pfuncDesc = "";
pIsValid = false;
Matcher matcher = Pattern.compile(pattern).matcher(pline); // les parenthèses permettent de créer des groupes
if (matcher.matches()){ // on peut utiliser aussi .find() qui ne teste pas entierrement la chaine
pinOut = matcher.group(1).toString();
ptype = matcher.group(2).toString();
pfuncName = matcher.group(3);
pfuncDesc = matcher.group(4);
pIsValid = true;
System.out.println(pline + " => " + pinOut + " / " + ptype + " / " + pfuncName + " / " + pfuncDesc);
} else {
System.err.println("Parsage de la ligne non reconnu");
return;
}
}
}
} |
Partager