Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

Mcj.java

Go to the documentation of this file.
00001 // -*-Java-*-
00007 
00008 package pkgMcj;
00009 
00010 import pkgView.*;
00011 import pkgUtil.*;
00012 import pkgView.*;
00013 
00014 import java.io.*;
00015 
00017 public class Mcj {
00018 
00019    static FileOut logFile; 
00020    private static final String LOG_FILE_NAME = ".mcj_log";
00021 
00022    private ScreenKit sk;
00023    private Keyboard keyboard;
00024    private TextItem hintLine  = new TextItem(" ",75);
00025    private TextItem bashLine  = new TextItem(" ",75);
00026 
00027    private PanelGrid grid1;
00028    private PanelGrid grid2;
00029    private MenuBar menuBar;
00030 
00031    private static final String shellPrefix = "Sh: ";
00032    private TextItem curLine = new TextItem(" ",70);
00033 
00037    public static void main(String[] args) {
00038       Mcj m = new Mcj();
00039       goodbye("goodbye");
00040    }
00041 
00042 
00043    public static void goodbye( String s ) {
00044       logFile.putString(s);
00045       logFile.closeFile();
00046       System.exit(0);
00047    }
00048 
00049    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00053    public Mcj(){
00054       sk = ScreenKit.getSingleton();
00055       keyboard = Keyboard.getSingleton();
00056 
00057       logFile = new FileOut();
00058       logFile.openFile(LOG_FILE_NAME);
00059 
00060       hintLine.setText(" ");
00061       hintLine.setColors( sk.PAIR_WHITE_BLACK,sk.PAIR_WHITE_BLACK);
00062       hintLine.setAttribute( sk.SK_NORMAL );
00063 
00064       hintLine.setPosition(sk.PANEL_HINT_ROW, 0);
00065       hintLine.setSelected(false);
00066 
00067       FunctionBar fbar = new FunctionBar(); // bottom menu functions
00068 
00069       int defaultColor = sk.PAIR_BLACK_CYAN;
00070       int activeColor = sk.PAIR_WHITE_BLACK;
00071 
00072       fbar.redraw();
00073 
00074       String dirName = getSystemProperty( "user.dir" );
00075 
00076       // ------ start drawing the screen -------------- //
00077       // grid1 is the left panel
00078       grid1 = new PanelGrid(this,PANEL_LEFT);
00079 
00080       grid1.setCurrentPath( dirName );
00081       grid1.redraw(true);
00082 
00083       // grid2 is the rightpanel
00084       grid2 = new PanelGrid(this,PANEL_RIGHT);
00085       grid2.setCurrentPath( dirName );
00086       grid2.redraw(true);
00087 
00088       // construct the top menu bar, add the menubar items
00089       menuBar = new MenuBar(this);
00090       menuBar.redraw();
00091 
00092       hintMsg(" setup folders ..." );
00093 
00094       int i,y,x,attr=0;
00095 
00096       bashLine.setText(" ");
00097       bashLine.setColors( sk.PAIR_WHITE_BLACK,sk.PAIR_WHITE_BLACK);
00098       bashLine.setAttribute( sk.SK_NORMAL );
00099 
00100       bashLine.setPosition(sk.PANEL_SHELL_ROW, 0);
00101       bashLine.setSelected(false);
00102       
00103       //curLine = new TextItem( shellPrefix, shellPrefix.length());
00104       curLine.setColors( sk.PAIR_WHITE_BLACK,sk.PAIR_WHITE_BLACK);
00105       curLine.setAttribute( sk.SK_NORMAL );
00106 
00107       curLine.setPosition(sk.PANEL_SHELL_ROW, 0);
00108       curLine.setSelected(true);
00109 
00110       sk.ttyRefresh();  // draw the initial screen
00111       // -------- end of initial screen draw ------------------- //
00112 
00113       
00114       // since Mcj runs on a single thread, loop forever here
00115       // distributing key presses.
00116 
00117       boolean bdone = false;
00118       keyboard.setKey(0);       // initially, no keypress
00119       while( !bdone ){
00120          boolean flag, changed;
00121          int modeNow = currentMode;
00122          int k;
00123 
00124          k = keyboard.getLastKey();     
00125          bdone = ( k == keyboard.KEY_F10 );
00126          if( bdone ){
00127             break;
00128          }
00129 
00130          changed = eventChain( k );
00131          if( changed ){
00132             grid1.reload_page();
00133             grid2.reload_page();
00134          }
00135 
00136          if( modeNow != currentMode || changed ){
00137             // if a mode change is needed, then all views must
00138             // reset themselves as indicated by a defined token
00139             eventChain( keyboard.KEY_NEWMODE );
00140          }
00141          hintMsg( getFromFileName() );
00142 
00143          shellMsg();  // the shell line is done last
00144 
00145       }
00146       
00147       sk.ttyEndWin();
00148    }
00149    
00150 
00151    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00155    private void hintMsg( String msg ){
00156       String hintStr = "Hint: " + msg + "|";
00157       hintLine.setText(hintStr);
00158       hintLine.redraw();
00159    }   
00160 
00176    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00180    private void shellMsg(){
00181 
00182       int k = keyboard.inputKey(0);
00183       if( keyboard.isAlpha(k) || keyboard.isDigit(k) || k==keyboard.KEY_SPACE){
00184          curLine.putChar( (char)k  );
00185          k = 0;
00186       }
00187 
00188       if( keyboard.isLeftArrow(k)  ) { 
00189          curLine.moveLeft(); 
00190          k = 0;
00191       }
00192 
00193       if( keyboard.isRightArrow(k) ) { 
00194          curLine.moveRight();
00195          k = 0;
00196       }
00197 
00198       if( keyboard.isBackSpace(k) ) {
00199          curLine.moveLeft(); 
00200          curLine.putChar( (char)keyboard.KEY_SPACE  );
00201          curLine.moveLeft(); 
00202          k = 0;
00203       }
00204 
00205       if( k == keyboard.KEY_CTRL_K ) { 
00206          curLine.eraseLine();
00207          k = 0;
00208       }
00209 
00210       bashLine.setText(shellPrefix + curLine.getText() );
00211       //bashLine.setText(shellPrefix + Integer.toString(k) );
00212       bashLine.redraw();
00213       if( k == 0 ){
00214          keyboard.setKey(0); // clear the keyboard, if key handled
00215       }
00216       sk.ttyRefresh();
00217 
00218    }   
00219 
00220    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00223    public void errorMsg( String msg ){
00224       String bashStr = "Error: " + msg + "| <press any key>";
00225       bashLine.setText(bashStr);
00226       bashLine.redraw();
00227       sk.ttyRefresh();
00228       keyboard.inputKey(0);
00229    }   
00230 
00231    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00234    private String getFromFilePath() {
00235       String fromName;
00236       if( grid1.hasFocus() ){
00237          fromName = grid1.getDir();
00238       }
00239       else{
00240          fromName = grid2.getDir();
00241       }
00242       return fromName;
00243    }
00244 
00245    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00246    private String getFromFileName() {
00247       String fromName;
00248       if( grid1.hasFocus() ){
00249          fromName = grid1.getFullPath();
00250       }
00251       else{
00252          fromName = grid2.getFullPath();
00253       }
00254       return fromName;
00255    }
00256 
00257    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00258    private String getToFileName() {
00259       String toName;
00260       if( grid1.hasFocus() ){
00261          toName = grid2.getFullPath();
00262       }
00263       else{
00264          toName = grid1.getFullPath();
00265       }
00266       return toName;
00267    }
00268 
00269 
00270    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00304    private boolean eventChain( int token ){
00305       boolean chainFlag = false;
00306       final boolean changed = true;
00307       Cmd cmd = new Cmd(this);
00308 
00309       chainFlag = menuBar.checkKey( chainFlag, token );
00310       chainFlag = grid1.checkKey( chainFlag, token );
00311       chainFlag = grid2.checkKey( chainFlag, token );
00312 
00313       if( token == keyboard.KEY_F5 ){
00314          String fromName = getFromFileName();
00315          String toName =  getToFileName();
00316          cmd.doCmd( "cp " + fromName + " " + toName);
00317          return changed;
00318       }
00319 
00320       if( token == keyboard.KEY_F6 ){
00321          String fromName = getFromFileName();
00322          String toName =  getToFileName();
00323          cmd.doCmd( "mv " + fromName + " " + toName);
00324          //cmd.doCmd( "rm -f " + fromName );
00325          return changed;
00326       }
00327 
00328       if( token == keyboard.KEY_F3 || token == keyboard.KEY_F4 ){
00329          String fromName = getFromFileName();
00330          String s = new String("vi " + fromName  );
00331          logFile.putString(s);
00332          sk.ttyShellScreen(s);
00333          return changed;
00334       }
00335 
00336       if( token == keyboard.KEY_CTRL_O){
00337          sk.ttyShellScreen("/bin/sh");
00338          return changed;
00339       }
00340 
00341       if( token == keyboard.KEY_CTRL_R){
00342          return changed;
00343       }
00344 
00345       if( token == keyboard.KEY_F8 ){
00346          String fromName = getFromFileName();
00347          cmd.doCmd( "rm -f " + fromName );
00348          return changed;
00349       }
00350 
00351       if( token == keyboard.KEY_F7 ){
00352          //String fromName = getFromFileName();
00353          //cmd.doCmd( "mkdir -p " + fromName );
00354          errorMsg("You can't do mkdir");
00355          return changed;
00356       }
00357 
00358 
00359       return false;             // no change
00360    }
00361    
00362 
00366    void testFolder(){
00367       //System.out.println(">>" + "hello world");
00368       logFile.putString("Folder test..");
00369 
00370       String dirName = getSystemProperty( "user.dir" );
00371       if( dirName == null ) dirName = "/";
00372 
00373       Folder aFolder = new Folder( this,dirName );
00374       //aFolder.setFolderName( dirName );
00375       logFile.putString( aFolder.getFolderName() );
00376 
00377       if( aFolder.loadNames() == true ){
00378          logFile.putString( "entries=" + aFolder.getNumEntries() );
00379          for( int i = 0; i < aFolder.getNumEntries(); i++ ){
00380             String f1 = aFolder.getNextName(0);
00381             if( f1 != null ){
00382                logFile.putString( aFolder.getLength(f1,true));
00383             }
00384          }
00385       }
00386    }
00387 
00388    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00394    public String getSystemProperty( String key ){
00395       String value = null;
00396       try{
00397          value = System.getProperty( key );
00398       }
00399       catch( Exception e ) {
00400          logFile.putString(e.getMessage());
00401          value = "/";
00402       }
00403       return value;
00404    }
00405 
00406    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00410    public final int STATE_NULL=0;       
00411    public final int STATE_VIEW1=1;      
00412    public final int STATE_2PL=2;        
00413    public final int STATE_2PR=3;        
00414    public final int STATE_1PL=4;        
00415    public final int STATE_1PR=5;        
00416    public final int STATE_HELP=6;       
00417    public final int STATE_MENU=7;       
00418    public final int STATE_VIEW=8;       
00419    public final int STATE_EDIT=9;       
00420    public final int STATE_COPY=10;      
00421    public final int STATE_MOVE=11;      
00422    public final int STATE_MKDIR=12;     
00423    public final int STATE_DEL=13;       
00424    public final int STATE_PULDN=14;     
00425    public final int STATE_EXIT=15;      
00426 
00427    public final int PANEL_LEFT=1;       
00428    public final int PANEL_RIGHT=2;      
00429    public final int DROP_LEFT=4;        
00430    public final int DROP_FILE=5;        
00431    public final int DROP_OPTION=6;      
00432    public final int DROP_CMD=7;         
00433    public final int DROP_RIGHT=8;       
00434    
00435    public int currentMode = STATE_2PL; 
00436 
00437    
00438    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00443    public void setState(int stateArg ){
00444       currentMode = stateArg;
00445    }
00446 
00448 } //class Mcj
00449 
00450 
00451 //--------------------------------------------------------------------
00798 //--------------------------------------------------------------------

SourceForge.net Logo