001    /*
002     * Copyright (C) 2012 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    package org.crsh.console;
020    
021    import jline.console.Operation;
022    
023    import java.util.HashMap;
024    
025    /**
026     * @author Julien Viet
027     */
028    public enum KeyType {
029    
030      A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,
031      a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,
032      _0,_1,_2,_3,_4,_5,_6,_7,_8,_9,
033    
034      SPACE,
035    
036      UP,DOWN,LEFT,RIGHT,
037      
038      DELETE,BACKSPACE,ENTER,
039    
040      UNKNOWN
041    
042      ;
043    
044      private static KeyType[] INDEX = new KeyType[256];
045    
046      static {
047        INDEX[' '] = SPACE;
048        INDEX['0'] = _0;
049        INDEX['1'] = _1;
050        INDEX['2'] = _2;
051        INDEX['3'] = _3;
052        INDEX['4'] = _4;
053        INDEX['5'] = _5;
054        INDEX['6'] = _6;
055        INDEX['7'] = _7;
056        INDEX['8'] = _8;
057        INDEX['9'] = _9;
058        INDEX['A'] = A;
059        INDEX['B'] = B;
060        INDEX['C'] = C;
061        INDEX['D'] = D;
062        INDEX['E'] = E;
063        INDEX['F'] = F;
064        INDEX['G'] = G;
065        INDEX['H'] = H;
066        INDEX['I'] = I;
067        INDEX['J'] = J;
068        INDEX['K'] = K;
069        INDEX['L'] = L;
070        INDEX['M'] = M;
071        INDEX['N'] = N;
072        INDEX['O'] = O;
073        INDEX['P'] = P;
074        INDEX['Q'] = Q;
075        INDEX['R'] = R;
076        INDEX['S'] = S;
077        INDEX['T'] = T;
078        INDEX['U'] = U;
079        INDEX['V'] = V;
080        INDEX['W'] = W;
081        INDEX['X'] = X;
082        INDEX['Y'] = Y;
083        INDEX['Z'] = Z;
084        INDEX['a'] = a;
085        INDEX['b'] = b;
086        INDEX['c'] = c;
087        INDEX['d'] = d;
088        INDEX['e'] = e;
089        INDEX['f'] = f;
090        INDEX['g'] = g;
091        INDEX['h'] = h;
092        INDEX['i'] = i;
093        INDEX['j'] = j;
094        INDEX['k'] = k;
095        INDEX['l'] = l;
096        INDEX['m'] = m;
097        INDEX['n'] = n;
098        INDEX['o'] = o;
099        INDEX['p'] = p;
100        INDEX['q'] = q;
101        INDEX['r'] = r;
102        INDEX['s'] = s;
103        INDEX['t'] = t;
104        INDEX['u'] = u;
105        INDEX['v'] = v;
106        INDEX['w'] = w;
107        INDEX['x'] = x;
108        INDEX['y'] = y;
109        INDEX['z'] = z;
110      }
111    
112      /** . */
113    
114    
115      static KeyType map(Operation operation, int[] sequence) {
116        switch (operation) {
117          case SELF_INSERT:
118            if (sequence.length == 1) {
119              int index = sequence[0];
120              if (index >= 0 && index < INDEX.length) {
121                KeyType found = INDEX[index];
122                if (found != null) {
123                  return found;
124                }
125              }
126            }
127            break;
128          case BACKWARD_CHAR:
129            return LEFT;
130          case FORWARD_CHAR:
131            return RIGHT;
132          case PREVIOUS_HISTORY:
133            return UP;
134          case NEXT_HISTORY:
135            return DOWN;
136          case BACKWARD_DELETE_CHAR:
137            return BACKSPACE;
138          case DELETE_CHAR:
139            return DELETE;
140          case ACCEPT_LINE:
141            return ENTER;
142        }
143        return UNKNOWN;
144      }
145    
146    }