Scripts/Edit Grid

From J Wiki
Jump to: navigation, search

The script below defines the editgrid verb which displays an editable Grid which, on close, assigns its data to a user-definable noun.

editgrid provides an easy way to create arrays containing miscellaneous contents for use in the session.

If no name is specified, the grid data is assigned to the noun celldata_ed in the locale editgrid was called from.


Example usage

   load '~temp/jegrid.ijs'
   editgrid 5 5$0
   NB. edit displayed grid with pattern of 1s & close
   celldata_ed
1 1 0 0 0
0 1 0 0 0
0 1 1 1 0
0 0 0 1 0
0 0 0 1 1

   'myarray' editgrid i.5 5
   NB. zero various items in displayed grid & close
   myarray
 0  1  0  3  4
 5  6  7  0  9
 0 11  0 13 14
15 16 17  0 19
20 21  0 23 24

   'mytxtarray_new_' editgrid <"0 ]3 4$' '
   NB. Add text to blank cells in displayed grid & close
   mytxtarray_new_
┌────────────┬────────────────────┬────────────────┬─┐
│How are you?│                    │                │ │
├────────────┼────────────────────┼────────────────┼─┤
│Fine thanks.│And yourself?       │                │ │
├────────────┼────────────────────┼────────────────┼─┤
│Not so good.│Feeling a bit crook.│Might be the flu│ │
└────────────┴────────────────────┴────────────────┴─┘

   (('GRIDESCCANCEL';1),:'CELLDNAME';'newarray') editgrid i.2 3 4
   NB. Edit 2nd column of both planes to be all 1s. Press Esc to close.
   newarray
 0 1  2  3
 4 1  6  7
 8 1 10 11

12 1 14 15
16 1 18 19
20 1 22 23

Code

jegrid.ijs below is an extension of the jzgrid class. Specifically it is a variant of the grid verb from the grid.ijs script that enables editing. Download script: jegrid.ijs

script_z_ '~system/classes/grid/jzgrid.ijs'

coclass 'jegrid'
coinsert 'jzgrid'

gridpdestroy=: 3 : 0
  assignGrid''
  wd 'pclose'
  destroy__grid''
  codestroy''
)

egridp=: 4 : 0
  a=. conew 'jegrid'
  x gridpshow__a y
)

NB.*editgrid v Displays editable grid containing array y.
NB. result: On close the grid data is assigned to a noun.
NB.         Noun is defined in locale editgrid was called from.
NB. eg: 'myarray' editgrid i. 4 3
NB. y is: an array displayable in a grid control
NB. x is: Optional literal or 2-column table of grid options.
NB.       Literal is name of noun grid data is assigned to.
NB.       Defaults to celldata_ed.
NB.       Grid option name is CELLDNAME.
editgrid_z_=: 3 : 0
  (0 0$0) editgrid y
  :
  if. (2>#$x) *. 2= 3!:0 x do.
    x=. ,:'CELLDNAME';x end.
  opts=. x,,:'CELLEDIT';1
  opts=. opts,'GRIDESCCANCEL';1
  (opts;coname'') egridp_jegrid_ y
)

NB. assignGrid v Assigns grid data to CELLDNAME
assignGrid=: 3 : 0
  errmsg=.''
  if. 0=4!:0 <'CELLDNAME' do.
    cdname=. CELLDNAME
  else. NB. CELLDNAME not defined
    cdname=. 'celldata_ed'
  end.
  if. -. (-: cofullname) cdname do. NB. add default locale
    cdname=. cdname,'__locD'
  elseif. (('_' ~: {:@])*. 1 e. '__' E. ]) cdname do. NB. get referenced locale
    try.
      idx=. _2 { I. '_' = cdname
      loc=. >((cdname }.~ 2+idx),'__locD')~
      cdname=. (idx{.cdname),'_',loc,'_'
    catch. NB. locale reference not found
      cdname=. (idx{.cdname),'__locD'
      errmsg=. 0{::<;._2 ]13!:12 ''
      errmsg=. errmsg,LF,'|Grid assigned to ',(idx{.cdname),'_',(>locD),'_'
    end.
  end.
  try. (cdname)=. CELLDATA__grid
  catch. NB. ill-formed name
    ('celldata_ed__locD')=. CELLDATA__grid
    errmsg=. 0{::<;._2 ]13!:12 ''
    errmsg=. errmsg,LF,'|Grid assigned to celldata_ed','_',(>locD),'_'
  end.
  if. #errmsg do. smoutput errmsg end.
)

Contributed by Ric Sherlock

See Also