001package org.vishia.commander;
002
003import java.io.File;
004import java.util.Map;
005import java.util.TreeMap;
006
007import org.vishia.fileRemote.FileRemote;
008import org.vishia.gral.base.GralTable;
009import org.vishia.gral.base.GralWidget;
010import org.vishia.gral.ifc.GralTableLine_ifc;
011import org.vishia.gral.ifc.GralUserAction;
012import org.vishia.gral.widget.GralFileSelector;
013import org.vishia.gral.widget.GralSelectList;
014import org.vishia.util.KeyCode;
015
016/**This is one table of favorite pathes in the file commander.  
017 */
018public class FcmdFavorCard  extends GralSelectList<FcmdFavorPathSelector.FavorPath>
019{
020  /**The component */
021  final Fcmd main;
022
023  /**The tabbed panel where this class is member of. */
024  final FcmdLeftMidRightPanel mainPanel;
025  
026  final FcmdFileCard fileTable;
027  
028  /**Stores the last selected and used favor path (pressing enter or double click)
029   * to select the same line if the favor path will be re-opened.
030   */
031  String sActSelectedFavorPath = "";
032  
033  /**Index of all entries in the visible list. */
034  Map<String, FcmdFavorPathSelector.FavorPath> indexFavorPaths = new TreeMap<String, FcmdFavorPathSelector.FavorPath>();
035  
036  public FcmdFavorCard(Fcmd main, String name, FcmdFileCard fileTable, FcmdLeftMidRightPanel panel)
037  { //super(name, mng);
038    super(name, 20, new int[]{2,15,0}, 'C');
039    this.main = main;
040    this.mainPanel = panel;
041    this.fileTable = fileTable;
042
043  }
044
045
046  /**Adds a line to this table.
047   * @param ix Show which index is used for a local table, 0..2 for left, mid, right,
048   *   than show the label in the left cell (column)
049   * @param favorPathInfo The favorite info
050   */
051  GralTableLine_ifc<FcmdFavorPathSelector.FavorPath> add(FcmdFavorPathSelector.FavorPath favorPathInfo)
052  {
053    if(indexFavorPaths.get(favorPathInfo.selectName) == null){
054      indexFavorPaths.put(favorPathInfo.selectName, favorPathInfo);
055      GralTableLine_ifc<FcmdFavorPathSelector.FavorPath> line = wdgdTable.addLine(null, null, favorPathInfo);
056      line.setCellText(favorPathInfo.selectName, 1);
057      line.setCellText(favorPathInfo.path, 2);
058      line.repaint(100,0);
059      return line;
060    }
061    else return null;
062  }
063  
064  
065  void clear()
066  {
067    indexFavorPaths.clear();
068    wdgdTable.clearTable();
069  }
070  
071
072  
073  /**Add all favor paths from the SelectTab newly
074   * @param favorTabInfo
075   */
076  void fillFavorPaths(FcmdFavorPathSelector.FavorFolder favorTabInfo)
077  {
078    clear();
079    int lineCt =0;
080
081    GralTableLine_ifc<FcmdFavorPathSelector.FavorPath> currentLine = null;
082    for( FcmdFavorPathSelector.FavorPath favorPathInfo: favorTabInfo.listfavorPaths){
083      GralTableLine_ifc<FcmdFavorPathSelector.FavorPath> line = add(favorPathInfo);
084      if(currentLine == null){ currentLine = line; }  //first line
085      if(favorPathInfo.selectName.equals(sActSelectedFavorPath)){
086        currentLine = line;  //or the last selected one.
087      }
088      lineCt +=1;
089    }
090    wdgdTable.setCurrentLine(currentLine, 3, 1);
091
092  }
093  
094  
095  /**Overrides the {@link GralFileSelector#setFocus()} and calls him, before that sets the color
096   * of the current line of table of all 3 current file panels to the 3-stage color
097   * to see which table has the focus. 
098   */
099  @Override public boolean setFocus(){ 
100    mainPanel.bFavorCardHasFocus = true;
101    mainPanel.bFavorThemeCardHasFocus = false;
102    main.setLastSelectedPanel(mainPanel);
103    //setActFilePanel_setColorCurrLine();
104    return super.setFocus(); 
105  }
106  
107  
108
109  
110  
111  /**Removes this file card with its widgets and data. It is 'close tab'. */
112  @Override public boolean remove(){
113    super.remove();
114    indexFavorPaths.clear();
115    return true;
116  }
117  
118  
119  @Override protected boolean actionOk(Object userData, GralTableLine_ifc line)
120  {
121    //before changing the content of this fileTable, store the current directory
122    //to restore if this favor respectively selection is used ones more.
123    FileRemote dir = null;
124    String currentDir;
125    if(fileTable.favorPathInfo !=null){
126      dir = fileTable.getCurrentDir();
127      if(dir != null){
128        currentDir = dir.getAbsolutePath();
129        if(currentDir !=null){
130          mainPanel.indexActualDir.put(fileTable.favorPathInfo.selectName, currentDir);
131    } } }
132    //
133    //Now switch to the new favor in the file panel: 
134    FcmdFavorPathSelector.FavorPath favorPathInfo = (FcmdFavorPathSelector.FavorPath)line.getUserData();
135    //new: fileTable.actionSetFromTabSelection.exec(KeyCode.menuEntered, null, favorPathInfo);
136    main.favorPathSelector.actFavorPathInfo = favorPathInfo; //The last used selection (independent of tab left, middle, right)
137    this.sActSelectedFavorPath = favorPathInfo.selectName;
138    if(  wdgdTable.name.startsWith(FcmdWidgetNames.tableFavoritesMain)) {
139      //use the root dir any time if the main favor path table is used.
140      currentDir = favorPathInfo.path;
141    } else {
142      currentDir  = mainPanel.indexActualDir.get(favorPathInfo.selectName);
143      if(currentDir == null){
144        currentDir = favorPathInfo.path;
145      }
146    }
147    //dir = new FileRemote(currentDir);  
148    //TODO this is exec in the graphic thread. Fill the card, access the file system in another thread!!!
149    dir = main.fileCluster.getFile(currentDir, null);
150    fileTable.setNewContent(favorPathInfo, dir);
151    
152    if(!fileTable.wdgCardSelector.setActItem(favorPathInfo.selectName)){
153      fileTable.wdgCardSelector.addItem(favorPathInfo.selectName, -1, favorPathInfo);
154    }
155    return true;
156  }
157
158  @Override
159  protected void actionLeft(Object userData, GralTableLine_ifc line)
160  {
161    // TODO Auto-generated method stub
162    
163  }
164
165  @Override
166  protected void actionRight(Object userData, GralTableLine_ifc line)
167  {
168    // TODO Auto-generated method stub
169    
170  }
171
172  /**Handle the keys for the JavaCommander-Selection of favorites
173   * <ul>
174   * <li>sh-F1 .. shF3: activates fileSelector for left, middle and right panel.
175   * </ul>
176   * @see org.vishia.gral.widget.GralSelectList#actionUserKey(int, java.lang.Object, org.vishia.gral.ifc.GralTableLine_ifc)
177   */
178  @Override protected boolean actionUserKey(int key, Object userData,
179      GralTableLine_ifc line)
180  { boolean ret = true;
181    FcmdFavorPathSelector.FavorPath favorPathInfo = (FcmdFavorPathSelector.FavorPath)userData;
182    //TODO not used no more
183    if(key ==KeyCode.shift + KeyCode.F1){
184      File dir = new File(favorPathInfo.path);
185      //panelLeft.fileSelectorMain.fillIn(dir);
186    } else if (key ==KeyCode.shift + KeyCode.F2){
187      File dir = new File(favorPathInfo.path);
188      //panelMid.fileSelectorMain.fillIn(dir);
189    } else if (key ==KeyCode.shift + KeyCode.F3){
190        //panelRight.fileSelectorMain.fillIn(new File(data.path));
191    } else if (key ==KeyCode.shift + KeyCode.F5){
192      //reread the configuration file.
193      main.favorPathSelector.readCfg(main.favorPathSelector.fileCfg);
194      main.favorPathSelector.panelLeft.fillCards();
195      
196    /*
197    } else if (key == main.keyActions.keyPanelLeft){
198      //sets focus to left
199      FcmdFileCard fileTableLeft = null;
200      boolean found = false;
201      for(FcmdFileCard fileTable: mainPanel.listTabs){
202        if(fileTable.favorCard == this){ found = true;  break;}
203        fileTableLeft = fileTable;  //save this table as table left, use if found.
204      }
205      if(found){
206        if(fileTableLeft !=null){
207          fileTableLeft.favorCard.wdgdTable.setFocus();
208        } else {  //left from first is the selectAllTable of this panel.
209          mainPanel.cardFavorThemes.wdgdTable.setFocus();
210        }
211      }
212    } else if (key == main.keyActions.keyPanelRight){
213      //sets focus to right
214      FcmdFileCard fileTableRight = null;
215      boolean found = false; 
216      for(FcmdFileCard fileTable: mainPanel.listTabs){
217        if(found){ fileTableRight = fileTable; break; }  //use this next table if found before.
218        if(fileTable.favorCard == this) { found = true; }
219      }
220      if(fileTableRight !=null){
221        fileTableRight.favorCard.wdgdTable.setFocus();
222      }
223    } else if (key == main.keyActions.keyMainPanelLeft){
224      FcmdLeftMidRightPanel dstPanel = mainPanel == main.favorPathSelector.panelRight ?
225          main.favorPathSelector.panelMid : main.favorPathSelector.panelLeft;
226      if(dstPanel.actFileCard !=null){ dstPanel.actFileCard.favorCard.setFocus(); }
227    } else if (key == main.keyActions.keyMainPanelRight){
228      FcmdLeftMidRightPanel dstPanel = mainPanel == main.favorPathSelector.panelLeft ?
229          main.favorPathSelector.panelMid : main.favorPathSelector.panelRight;
230      if(dstPanel.actFileCard !=null){ dstPanel.actFileCard.favorCard.setFocus(); }
231    */
232    } else if (key == main.keyActions.keyCreateFavorite){
233      main.favorPathSelector.actFavorPathInfo = favorPathInfo; //info in the line of table.
234      main.favorPathSelector.windAddFavorite.widgLabel.setText("file3");
235      main.favorPathSelector.windAddFavorite.widgShortName.setText("alias");
236      //File lastSelectedFile = panelRight.fileSelectorMain.getSelectedFile();
237      //String pathDir = FileSystem.getCanonicalPath(lastSelectedFile.getParentFile());
238      //windAddFavorite.widgPath.setText(pathDir);
239      main.favorPathSelector.windAddFavorite.window.setFocus(); //WindowVisible(true);
240    } else {
241      ret = false;
242    }//
243    return ret;
244  }
245  
246  /**Action is called any time if a line was focused in the favor table. */
247  GralUserAction actionFavorSelected = new GralUserAction("actionFavorSelected"){
248    @Override public boolean userActionGui(int actionCode, GralWidget widgd, Object... params) {
249      if(actionCode == KeyCode.userSelect){
250        mainPanel.bFavorCardHasFocus = true;
251        mainPanel.bFavorThemeCardHasFocus = false;
252        main.lastFavorCard = FcmdFavorCard.this;
253        main.currentFileCard = FcmdFavorCard.this.fileTable;
254        main.setLastSelectedPanel(mainPanel);
255        GralTable.TableLineData line = (GralTable.TableLineData) params[0];
256        //Object oData = line.getUserData();
257        //System.out.println("FcmdFavorCard.actionFavorSelected: " + fileTable.label);
258      }
259      return true;
260    }
261  };
262  
263
264  
265  @Override public String toString(){ return fileTable.toString(); }
266
267}