// KLL editgrid // processing IDE 3.5.3 // forum: string editor question // v0.1 CSV file to table, table to rect grid class array // v0.2 add cell mini editor // v0.3 allow cell header edit and key [a] add row with auto index // v0.4 allow ID edit / numbercheck and already exist check // if ID changed to 'd' that row is deleted!! // OPEN add / delete column //_______________________________________________________ TABLE String filename = "data/mydata.csv"; String bckfilename = "data/mydata_csv.bck"; /* id,name,street,town,code, 1,you,yourstreet,yourtown,1174, 0,me,mystreet,mytown,4711, */ // if lines end with a "," a optional new column is available Table mytable; int tcols, trows, numcol = 0; boolean dprint = false;//false;//true; // color setup: color bg = color(200, 200, 0); color head = color(0, 200, 200); color fillsel = color(0, 200, 0); color fillnsel= color(200, 0, 200); int tosel = -1; String edittxt = ""; boolean IDedit = false, editmode = false; String info = "use: \n[s] for save data back to file\n[a] add row\nin cell edit change ID number to 'd' for delete row"; //_______________________________________________________ TABLE void get_tabledata() { // get tabledata from file println("__open file "+filename); mytable = loadTable(filename, "header, csv"); tcols = mytable.getColumnCount(); trows = mytable.getRowCount(); if (dprint) { println("total cols in table: "+tcols); println("total rows in table: "+trows+", header line not counted "); println("header: "); for ( int c = 0; c < tcols; c++) println("headerline col: "+c+" "+ mytable.getColumnTitle(c)); for ( int r = 0; r < trows; r++) { TableRow thisrow = mytable.getRow(r); for ( int c = 0; c< tcols; c++) println("row "+r+ " col "+c+" "+mytable.getColumnTitle(c)+" : "+thisrow.getString(c)); } } } void sort_tabledata() { // sort numerically a specific column mytable.setColumnType(numcol, Table.INT); mytable.sort(numcol); println("__column "+numcol+" _"+mytable.getColumnTitle(numcol)+"_ sorted by number"); if (dprint) { for ( int r = 0; r < mytable.getRowCount(); r++ ) { TableRow thisrow = mytable.getRow(r); println("row "+r+" "+thisrow.getInt(numcol)+" "+thisrow.getString(1)); } } } void save_tabledata() { // save to file saveTable(mytable, filename, "csv"); println("__saved to "+filename); } void save_backup() { // save to file saveTable(mytable, bckfilename, "csv"); println("__created "+bckfilename); } //_______________________________________________________ GRID int x = 2, y = x, w = 25, h = w, offset = x; int grid = 20, many = grid*grid; Mycell[] myrects;// = new Myrect[many]; void auto_scale() { grid = tcols; many = grid*(trows + 1); // with header line myrects = new Mycell[many]; w = (width - 2*x)/grid - offset; h = 20; //(height - 2*y)/(trows + 1) - offset; if (dprint) println("x "+x+" y "+y+" w "+w+" h "+h+" offset "+offset+" grid "+grid+" many "+many); } class Mycell { int x, y, w, h, id; String txt; boolean selected=false; Mycell (int x, int y, int w, int h, int id, String txt) { this.x = x; this.y = y; this.w = w; this.h = h; this.id = id; this.txt = txt; // table column name or cell content } void drawit() { if ( id < grid ) fill(head); else fill(fillnsel); if ( selected ) fill(fillsel); rect(x, y, w, h); fill(0); // black text if ( txt != null ) text(txt, x+5, y+15); //sel(); } boolean over() { return( x < mouseX && mouseX < x+w && y < mouseY && mouseY < y+h ); } void sel() { selected = true; } void unsel() { selected = false; } } //___________________________________________ CELL SELECT EDIT void sel_cell() { // like option group, select one only for (int i = 0; i < many; i++) if ( myrects[i].over( ) && mousePressed && ( i != tosel ) ) { tosel = i; // int thisrow = floor((i -grid )/grid); int thiscol = i%grid; if ( thiscol > 0 && (i > grid )) { // not edit first colum ID myrects[i].sel(); edittxt = myrects[i].txt; editmode = true; if (dprint) println("selected: i "+tosel+" row "+thisrow+" col "+thiscol+" txt "+edittxt); } else if ( i < grid) { // header line myrects[i].sel(); edittxt = myrects[i].txt; //mytable.getColumnTitle(thiscol); editmode = true; if (dprint) println("selected: i "+tosel+ " header title"+" row "+thisrow+" col "+thiscol+" txt "+edittxt); } else if (thiscol == 0 ) { println("row select or ID change / delete with [d] "); myrects[i].sel(); edittxt = myrects[i].txt; editmode = true; IDedit = true; if (dprint) println("selected: i "+tosel+" row "+thisrow+" col "+thiscol+" txt "+edittxt); } else { editmode = false; IDedit = false; } } for (int i = 0; i < many; i++) { if ( i != tosel ) myrects[i].unsel(); } editcell(); } void editcell() { if ( tosel > -1 && editmode ) { fill(255); stroke(0); rect(2, height-18, width-5, 16); fill(0); text(edittxt, 10, height-6); } } boolean isInteger(String s) { boolean result = false; try { Integer.parseInt(s); result = true; } catch(NumberFormatException e) { } if (dprint) { print(" The string '" + s +"' is "); if (result) println("an integer"); else println("not an integer"); } return result; } // mini editor void keyPressed() { if (dprint) println("key "+key+" keyCode "+keyCode); if ( editmode ) { if ( keyCode == ENTER || keyCode == RETURN ) { // store back to array class if ( IDedit ) { if ( isInteger(edittxt) ) { TableRow result = mytable.findRow(edittxt, 0); if ( result != null ) { if (dprint) println(result.getString(0)+" have this id already"); edittxt = myrects[tosel].txt; // restore original } } else { // ROW delete option OR restore if ( edittxt.equals("d") ) { int thisRowDelete = floor((tosel -grid )/grid); println("row delete "+thisRowDelete); mytable.removeRow(thisRowDelete); save_tabledata(); reload("nobackup"); tosel = -1; } else { edittxt = myrects[tosel].txt; // restore original println("__restore original cell content"); } } IDedit = false; } if ( tosel >-1 ) myrects[tosel].txt = edittxt; if (dprint) println("editcell back to array"); editmode = false; tosel = -1; edittxt = ""; if (dprint) println(info); } else if (keyCode==BACKSPACE) { if ( edittxt.length() > 0 ) edittxt = edittxt.substring( 0, edittxt.length()-1 ); } else if (key==ESC) { key=0; // kill ESC editmode = false; } else if (keyCode>30) { // add LETTERS edittxt += key; // normal text input } // IGNORE else { // ignore } } else { // not edit mode if ( key == 's' ) { // first save array to table, then table to file saveAndreload(); } if ( key == 'a' ) { // ADD ROW, but first save array to table, add table ROW, then table to file save_array_to_table(); //save_tabledata(); TableRow newRow = mytable.addRow(); newRow.setInt(0, mytable.lastRowIndex()); saveAndreload(); } } } void saveAndreload() { save_array_to_table(); save_tabledata(); reload("nobackup"); } void reload(String backup) { get_tabledata(); if ( backup.equals("backup") ) save_backup(); sort_tabledata(); auto_scale(); make_grid(); } void save_array_to_table() { mytable.setColumnType(numcol, Table.STRING); for (int i = 0; i < many; i++) { if ( i >= grid ) { int thisrow = floor((i -grid )/grid); int thiscol = i%grid; String newtxt = myrects[i].txt; String oldtxt = mytable.getString(thisrow, thiscol); if ( !oldtxt.equals(newtxt) ) { if (dprint) println(i + " " + thisrow + " " + thiscol +" new: "+newtxt +" old: "+ oldtxt); mytable.setString(thisrow, thiscol, myrects[i].txt ); } } else { // header line save String newtxt = myrects[i].txt; String oldtxt = mytable.getColumnTitle(i); if ( !oldtxt.equals(newtxt) ) { if (dprint) println(i + " header line new: "+newtxt +" old: "+ oldtxt); mytable.setColumnTitle(i, newtxt ); } } } } //_______________________________________________________ TABLE to ARRAY of CLASS void make_grid() { String ttext=""; TableRow thisrow = mytable.getRow(0); for ( int r = 0; r <= trows; r++) { if ( r > 0 ) { thisrow = mytable.getRow(r-1); } for ( int c = 0; c < tcols; c++) { int posx = x; for ( int ci = 0; ci < c; ci++) posx += w+offset; int posy = y + r * (h+offset); if ( r == 0 ) ttext=mytable.getColumnTitle(c); else ttext=thisrow.getString(c); int i = r * grid +c; myrects[i]=new Mycell(posx, posy, w, h, i, ttext); } } } void draw_grid() { for (int i = 0; i < many; i++) myrects[i].drawit(); } //_______________________________________________________ SETUP void setup() { size(500, 500); reload("backup"); println(info); } //_______________________________________________________ DRAW void draw() { background(bg); draw_grid(); sel_cell(); }