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

PanelGrid.java

Go to the documentation of this file.
00001 // -*-Java-*-
00010 
00012 package pkgMcj;
00013 
00014 import java.io.*;
00015 import java.util.*;
00016 import java.lang.reflect.*;
00017 
00018 import pkgView.*;
00019 import pkgUtil.*;
00020 import pkgMcj.*;
00021 
00023 public class PanelGrid  {
00024 
00025    private Mcj mcj;
00026    private ScreenKit sk = ScreenKit.getSingleton();
00027    private Keyboard kb = Keyboard.getSingleton();
00028    private String path_to_file;
00029    private UtilDate utilDate = new UtilDate(mcj);
00030    private int anchor = sk.PANEL_LS; // upper left corner of panel
00031 
00032    private int
00033       panelWidth = 39,
00034       vlen = 17;                // length of vertical line
00035       
00036    private TextItem 
00037       pthName  = new TextItem(" ",37),
00038       fullName  = new TextItem(" ",37),
00039       titleName = new TextItem(" ",4),
00040       titleSize = new TextItem(" ",4), 
00041       titleTime = new TextItem(" ",5);
00042 
00043    private String saved_path = null;
00044 
00045    private final static int NUM_TXT_ROWS = 16;
00046    private Folder folder1;
00047    private PanelRow prow;
00048    private PanelRow [] rows = new PanelRow[NUM_TXT_ROWS];
00049    private int panelId;
00050    private int displayRow=0; // position of highlighted item
00051    
00052    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00058    public PanelGrid( Mcj parent, int panelId ){
00059       int i;
00060 
00061       mcj = parent;
00062       this.panelId = panelId;
00063 
00064       if( panelId == mcj.PANEL_LEFT ){
00065          this.anchor = 0;
00066       }
00067       else{
00068          this.anchor = 40;
00069       }
00070 
00071       int y1 = sk.PANEL_TS+1, ya2, xa2;
00072       int defaultColor = sk.PAIR_WHITE_BLUE;
00073       int activeColor = sk.PAIR_BLACK_WHITE;
00074 
00075       pthName.setText("<");
00076       pthName.setColors( defaultColor, activeColor);
00077       pthName.setAttribute( sk.SK_NORMAL );
00078 
00079       pthName.setPosition(y1-1, anchor+1);
00080       pthName.setSelected(false);
00081 
00082       // path of selected file which appears in the last row
00083       fullName.setText("../");
00084       fullName.setColors( defaultColor, activeColor);
00085       fullName.setAttribute( sk.SK_NORMAL );
00086       fullName.setSelected(false);
00087 
00088       defaultColor = sk.COLOR_TITLE;
00089       activeColor = sk.COLOR_TITLE;
00090       titleName.setText("Name");
00091       titleName.setColors( defaultColor, activeColor);
00092       titleName.setPosition(y1, anchor+8);
00093       titleName.setAttribute(sk.SK_BOLD);
00094       titleName.setSelected(false);
00095 
00096       titleSize.setText("Size");
00097       titleSize.setColors( defaultColor, activeColor);
00098       titleSize.setPosition(y1, anchor+20);
00099       titleSize.setAttribute(sk.SK_BOLD);
00100       titleSize.setSelected(false);
00101 
00102       titleTime.setText("MTime");
00103       titleTime.setColors( defaultColor, activeColor);
00104       titleTime.setPosition(y1, anchor+30);
00105       titleTime.setAttribute(sk.SK_BOLD);
00106       titleTime.setSelected(false);
00107 
00108       // install default settings for file attribute strings
00109       xa2 = anchor+1;
00110       ya2 = sk.PANEL_TS+2;
00111 
00112       defaultColor = sk.PAIR_WHITE_BLUE;
00113       activeColor = sk.PAIR_BLACK_WHITE;
00114 
00115       for( i = 0; i < NUM_TXT_ROWS; i++ ){
00116          prow = new PanelRow(mcj);
00117          prow.itemName.setPosition( i+ya2, xa2 );
00118          prow.itemSize.setPosition( i + ya2, anchor+19 );
00119          prow.itemTime.setPosition( i + ya2, anchor+27 );
00120          prow.setAttributes(sk.SK_NORMAL);
00121          prow.setColors( defaultColor, activeColor);
00122 
00123          if( i == 0 ){
00124             prow.itemName.setText("/..");
00125             prow.itemSize.setText("UP--DIR");
00126             prow.itemTime.setText(" ");
00127             prow.fileRef = "$";
00128             rows[i] = prow;
00129             continue;
00130          }
00131          prow.itemName.setText("row_" + i );
00132          prow.itemSize.setText("r_" + i);
00133          prow.itemTime.setText("r_" + i);
00134 
00135          rows[i] = prow;
00136       }
00137       fullName.setPosition((++i) + ya2, xa2);
00138 
00139       saved_path = mcj.getSystemProperty( "user.dir" );
00140       if( saved_path == null ) saved_path = "/";
00141       
00142       this.loadPage( saved_path );
00143 
00144    }
00145 
00146    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00147    public String getFullPath(){
00148       if( path_to_file == null ){
00149          path_to_file = "?path?";
00150          mcj.errorMsg("PanelGrid.getFullPath is null");
00151       }
00152       return path_to_file;
00153    }
00154 
00155    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00160    private void setRowFile(int rowNum){
00161       if(rowNum > 0 ){
00162          prow = rows[rowNum];
00163          path_to_file = folder1.stringToPath(prow.fileRef);
00164       }
00165       else{
00166          path_to_file = folder1.getFolderName();
00167       }
00168 
00169       if( path_to_file == null ){
00170          mcj.errorMsg("PanelGrid.path_to_file is null");
00171       }
00172    }
00173    
00174    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00175    public String getDir(){
00176       return saved_path;
00177    }
00178 
00179    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00183    public void redraw( boolean clearFlag ){
00184       int xa2, i, ya1, ya2;
00185 
00186       sk.ttyDrawBox(sk.PANEL_TS,anchor,sk.PANEL_BS,panelWidth,sk.COLOR_BOX);
00187 
00188       sk.ttyDrawVline( sk.PANEL_TS+1,anchor+18,0,vlen,sk.COLOR_BOX);
00189       sk.ttyDrawVline( sk.PANEL_TS+1,anchor+26,0,vlen,sk.COLOR_BOX);
00190 
00191       ya1 = sk.PANEL_BS-1;
00192       sk.ttyDrawHline(ya1,anchor+1,0,panelWidth-2,sk.COLOR_BOX);
00193 
00194       titleName.redraw();
00195       titleSize.redraw();
00196       titleTime.redraw();
00197 
00198       xa2 = anchor+1;
00199       ya2 = sk.PANEL_TS+2;
00200       boolean flag;
00201       for( i = 0; i < NUM_TXT_ROWS; i++ ){
00202          prow  = rows[i];
00203          if( prow == null ){
00204             mcj.errorMsg("redraw prow is null " + i);
00205          }
00206          flag = ( clearFlag==false && i==displayRow && hasFocus() );
00207          prow.itemName.setSelected(flag);
00208          prow.itemSize.setSelected(flag);
00209          prow.itemTime.setSelected(flag);
00210          if( i == displayRow ){
00211             fullName.setText( prow.fileRef );
00212             setRowFile(i);
00213          }
00214          
00215          prow.redraw();
00216       }
00217 
00218       if( pthName.getText() == null ){
00219             mcj.errorMsg("redraw getText is null ");
00220       }
00221       if( fullName.getText() == null ){
00222             mcj.errorMsg("redraw fullName is null ");
00223       }
00224 
00225       pthName.setSelected( hasFocus() );
00226       pthName.redraw();
00227       fullName.redraw();
00228    }
00229 
00230    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00231    
00237    private boolean updateRow( int k ){
00238       if( !hasFocus() ) return false;
00239       int now = displayRow;
00240       switch (k){
00241 
00242       case kb.KEY_DOWN:
00243          if( displayRow < (NUM_TXT_ROWS-1) ){
00244             prow = rows[displayRow+1];
00245             if( prow.useFlag == true ){ 
00246                displayRow++;
00247             }
00248             else{
00249                displayRow=0;
00250             }
00251          }
00252          break;
00253 
00254       case kb.KEY_UP:
00255          if( --displayRow < 0 ){
00256             displayRow = 0;
00257          }
00258          break;
00259          
00260       case kb.KEY_ENTER:
00261          if( displayRow == 0 ){
00262             loadPage(folder1.getParentName());
00263 
00264          }else{
00265             // get the filename from the current row, make it the root
00266             // of a new page
00267             prow = rows[displayRow];
00268             if( folder1.isDirectoryName( prow.fileRef)){
00269                String path = folder1.stringToPath(prow.fileRef);
00270                mcj.logFile.putString( "New Page=" + path );
00271                loadPage( path );
00272             }
00273          }
00274          now = -1;
00275          displayRow = 0;
00276          break;
00277 
00278       case kb.KEY_HOME:
00279          now = -1;
00280          folder1.setAnchor(0);
00281          loadView();
00282          break;
00283 
00284       case kb.KEY_PGDN:
00285          folder1.setAnchor( folder1.getAnchor() + (NUM_TXT_ROWS-1));
00286          now = -1;
00287          loadView();
00288          break;
00289 
00290       case kb.KEY_PGUP:
00291          folder1.setAnchor( folder1.getAnchor() - (NUM_TXT_ROWS-1));
00292          now = -1;
00293          loadView();
00294          break;
00295 
00296       case kb.KEY_F5:
00297          if(displayRow > 0 ){
00298             prow = rows[displayRow];
00299             String path = folder1.stringToPath(prow.fileRef);
00300             mcj.logFile.putString( "pressed F5 for.. " + path);
00301          }
00302          break;
00303 
00304       default:
00305          break;
00306       }
00307       return (now != displayRow);
00308    }
00309 
00310    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00316    public boolean hasFocus( ){
00317       int tv = mcj.currentMode;
00318       if( tv == mcj.STATE_1PL || tv == mcj.STATE_2PL ){
00319          return (panelId == mcj.PANEL_LEFT);
00320       }
00321       if( tv == mcj.STATE_1PR || tv == mcj.STATE_2PR ){
00322          return (panelId == mcj.PANEL_RIGHT);
00323       }
00324       return false;
00325    }
00326 
00327    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00332    private boolean updateActive(){
00333       int tv = mcj.currentMode;
00334       if( tv == mcj.STATE_2PL || tv == mcj.STATE_2PR ) {
00335          return true;
00336       }
00337       if( tv == mcj.STATE_1PL && panelId == mcj.PANEL_LEFT ){
00338          return true;
00339       }
00340       if( tv == mcj.STATE_1PR && panelId == mcj.PANEL_RIGHT ){
00341          return true;
00342       }
00343       return false;
00344    }
00345 
00346    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00353    private int updateTabbing( int k ){
00354       if( k != kb.KEY_TAB ){
00355          return mcj.STATE_NULL;
00356       }
00357       int tv = mcj.currentMode;
00358       if( tv == mcj.STATE_2PL && panelId == mcj.PANEL_LEFT ){
00359          return mcj.STATE_2PR;
00360       }
00361       if( tv == mcj.STATE_2PR && panelId == mcj.PANEL_RIGHT ){
00362          return mcj.STATE_2PL;
00363       }
00364       return mcj.STATE_NULL;
00365    }
00366 
00367    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00374    public boolean checkKey( boolean flag,int keyItem ){
00375       if( flag ) {
00376          // indicates key already handled
00377          return flag;
00378       }
00379 
00380       if( !updateActive() ) return false;
00381 
00382       int tv;
00383 
00384       if( keyItem == kb.KEY_NEWMODE ){
00385          redraw(false);
00386          return false;
00387       }
00388 
00389       if( updateRow(keyItem) ){
00390          redraw(false);
00391          return true;
00392       }
00393 
00394       tv = updateTabbing( keyItem );
00395       if( tv != mcj.STATE_NULL ){
00396          redraw(true);          // clear highlights
00397          mcj.setState( tv );
00398          return true;           // tab to other panel
00399       }
00400       return false;
00401    }
00402 
00403    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00407    public void setCurrentPath( String pathName ){
00408       pthName.setText("<" + pathName + ">");
00409       saved_path = pathName;
00410    }
00411 
00412 
00413    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00417    public void loadPage( String dirName ){
00418       folder1 = new Folder(mcj,dirName);
00419       if( folder1.loadNames() == true ){
00420          setCurrentPath(dirName);
00421          loadView();
00422       }
00423       else{
00424          mcj.errorMsg( "loadNames failed for.. " + dirName);
00425       }
00426    }
00427 
00428    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00432    private void loadView(){
00433       int i; //,y,x,ya2,xa2,attr=0;
00434 
00435       displayRow = 0;
00436       prow = rows[0];
00437       prow.itemName.setText(
00438                             "../ (" + 
00439                             Integer.toString(folder1.getAnchor()) + "/" +
00440                             Integer.toString(folder1.getNumEntries()) + ")"
00441                             );
00442       prow.fileRef = "$";
00443       rows[0] = prow;
00444       folder1.saveAnchor();
00445       for( i = 1; i < NUM_TXT_ROWS; i++ ){
00446          prow = rows[i];
00447          
00448          String f1 = folder1.getNextName(1);
00449          if( f1 == null ) {
00450             prow.clearRow();
00451             rows[i] = prow;
00452 
00453             continue;
00454          }
00455          prow.useFlag = true;
00456          prow.fileRef = f1;
00457          String tmp = folder1.getLength(f1,false);
00458          if( tmp == null ){
00459             mcj.errorMsg("null length in " + i);
00460          }
00461          prow.itemSize.setText(tmp);
00462 
00463          //tmp = folder1.getStringDate(f1);
00464          long  ldate = folder1.getLongDate(f1);
00465          if( ldate == 0 ){
00466             mcj.errorMsg("null date in " + i);
00467          }
00468 
00469          tmp = utilDate.getDateString(ldate);
00470          prow.itemTime.setText(tmp);
00471 
00472          if( f1 != null && folder1.isDirectoryName(f1) ){
00473             prow.itemName.setText(File.separator + f1);
00474             prow.setAttributes(sk.SK_BOLD);
00475          }
00476          else{
00477             prow.itemName.setText(f1);
00478             prow.setAttributes(sk.SK_NORMAL);
00479          }
00480          rows[i] = prow;
00481 
00482          
00483       }
00484       folder1.restoreAnchor();
00485    
00486    } // loadView
00487 
00488 
00489    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00490    public void reload_page(){
00491       displayRow = 0;
00492       loadPage(saved_path);
00493    }
00494 
00495 
00496    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00497    
00498 } // end class PanelGrid
00499 
00500 

SourceForge.net Logo