001package org.vishia.commander;
002
003import java.io.File;
004import java.util.LinkedList;
005import java.util.List;
006import java.util.Map;
007import java.util.TreeMap;
008
009import org.vishia.fileRemote.FileRemote;
010import org.vishia.gral.base.GralPanelContent;
011import org.vishia.gral.base.GralTabbedPanel;
012import org.vishia.gral.base.GralWidget;
013import org.vishia.gral.base.GralMng;
014import org.vishia.gral.ifc.GralMngBuild_ifc;
015import org.vishia.gral.ifc.GralTableLine_ifc;
016import org.vishia.gral.ifc.GralUserAction;
017import org.vishia.gral.widget.GralSelectList;
018import org.vishia.mainCmd.MainCmd;
019import org.vishia.util.Assert;
020import org.vishia.util.KeyCode;
021
022/**Data for one panel left, middle or right.
023 * It contains the tab-card for the favor folder selection.
024 */
025public class FcmdLeftMidRightPanel
026{
027  
028  /**Version and history
029   * <ul>
030   * <li>2012-01-08 Hartmut the extra class FcmdFavorTabCard is now an inner class here because
031   *   it needs some elements shared with this outer class.
032   * <li>2011-11-00 Hartmut creation
033   * </ul>
034   */
035  public static final int version = 0x20120113;
036  
037  final Fcmd main;
038  
039  /**The container for all tabs of this TabbedPanel. It is a {@link GralPanelContent} of the primaryWindow 
040   * associated to one of the frame areas of the GralArea9_ifc. */
041  GralTabbedPanel tabbedPanelFileCards;
042  
043  /**The container for the tabs for selection. 
044   * It is the first {@link GralPanelContent} inside the {@link #tabbedPanelFileCards} designated with "a-F1" etc. 
045   */
046  GralTabbedPanel tabbedPanelFavorCards;
047  
048  /**Table widget for the select table.*/
049  FcmdFavorThemeCard cardFavorThemes;
050
051  /**List of all Tabs of this Panel, used and unused. This tabs are presented in the {@link FcmdFavorThemeCard} table*/
052  List<FcmdFileCard> listTabs = new LinkedList<FcmdFileCard>();
053  
054  /**The current opened file card. */
055  FcmdFileCard actFileCard;
056  
057  /**If the favor card is in foreground, it contains true. If the file card is in foreground, it contains false.
058   * The information will be set in the select line methods of the table.
059   */
060  boolean bFavorCardHasFocus = true;
061  
062  boolean bFavorThemeCardHasFocus= true;
063  
064  //final FcmdFavorPathSelector.FavorFolder actFavorFolder;
065  
066  /**Stores the current directory for all actual file panels. */
067  final Map<String, String> indexActualDir = new TreeMap<String, String>();
068  
069  /**File panel. */
070  //FcmdFileTable fileSelectorMain;
071  
072  //String actualDir;
073  
074  /**Characteristic char for the panel: l, m, r*/
075  final char cc;
076  
077  /**Characteristic number for the panel: 1, 2, 3 */
078  final char cNr;
079  
080  /**Index of panel 0, 1, 2. */
081  final int ixMainPanel;
082  
083  
084  /**The order number of the panel in order of usage.
085   * 1 = actual focused panel, 2 = last focused, 3 = third focused, 0 = no file panel. */
086  int orderMainPanel;
087  
088  //final int[] widthSelecttableMain = new int[]{6, 20, 30};
089
090  final int[] widthSelecttableSub = new int[]{2, 20, 30};
091
092  
093  FcmdLeftMidRightPanel(Fcmd javaCmd, char cc, char cNr, GralMng mng){
094    this.main = javaCmd;
095    this.cc = cc;
096    this.cNr = cNr;
097    this.ixMainPanel = cNr - '1';
098    cardFavorThemes = new FcmdFavorThemeCard(main, FcmdWidgetNames.tableFavoritesMain + cNr, this);
099    
100  }
101  
102  
103  
104  /**Build the initial content of one of the three tabbed panels, called in the build phase of the GUI.
105   * @param which Number 1 2 3 for left, mid, right
106   */
107  void buildInitialTabs()
108  {
109    GralMng mng = main._gralMng;
110    String sName = "Sel" + cNr;
111    //inside the left/mid/right tabbed panel: create the panel which contains a tabbed panel for selection
112    String nameGridPanel = FcmdWidgetNames.tabFavoritesLeftMidRight + cNr;
113    String tabLabelGridPanel = "a-F"+cNr;
114    mng.setPosition(tabbedPanelFileCards.pos(), 0, 0, 0, 0, 1, 'd');
115    tabbedPanelFileCards.addGridPanel(nameGridPanel, tabLabelGridPanel,1,1,10,10);
116    mng.setPosition(0, 0, 0, -0, 1, 'd');
117    //A tabbed panel inside the left, middle or right tab for selection.
118    String nameTabPanel = FcmdWidgetNames.panelFavoritesLeftMidRight + cNr;
119    mng.setPosition(tabbedPanelFileCards.pos(), 0, 0, 0, 0, 1, 'd');
120    tabbedPanelFavorCards = mng.addTabbedPanel(nameTabPanel, null, GralMngBuild_ifc.propZoomedPanel);
121    //The panel for selection from all favorites: 
122    nameGridPanel = FcmdWidgetNames.tabMainFavorites + cNr;
123    tabLabelGridPanel = "a-F"+cNr;
124    mng.setPosition(tabbedPanelFavorCards.pos(), 0, 0, 0, 0, 1, 'd');
125    tabbedPanelFavorCards.addGridPanel(nameGridPanel, tabLabelGridPanel, 1,1,10,10);
126    mng.setPosition(0, 0, 0, -0, 1, 'd'); //inside the new tab
127    cardFavorThemes.createImplWidget_Gthread();
128    fillCards();  //build the rest of all tabs and panels depending on content of favorites.
129    
130    if(cNr == '1'){ //commands only in the left panel.
131      tabbedPanelFileCards.addGridPanel("cmd", "Cmd",1,1,10,10);
132      mng.setPosition(2, -2, 0, -0, 1, 'd');
133      main.executer.cmdSelector.createImplWidget_Gthread();
134    }
135    
136  }
137  
138
139  void setFocus(){
140    main.setLastSelectedPanel(this);
141    if(actFileCard !=null){
142      actFileCard.setFocusFavorOrFile();
143    } else {
144      cardFavorThemes.setFocus();
145    }
146  }
147  
148
149  
150  /**
151   * @param which Number 1 2 3 for left, mid, right
152   */
153  void fillCards(){
154    
155    cardFavorThemes.clear();   //the common favor path table.
156    //clear index of entries, it is a mirror of content of the GUI-visible table and prevents
157    //twice adding.
158    cardFavorThemes.indexFavorFolders.clear();  
159    //clear all GUI tables of this main tab.
160    for(FcmdFileCard fileTabs: listTabs){
161      fileTabs.favorCard.clear();
162      fileTabs.favorCard.indexFavorPaths.clear();
163    }
164    //insert from left, from right etc. with the panels as first elements in the list:
165    final FcmdLeftMidRightPanel favorFolderFrom1, favorFolderFrom2;
166    final String from1, from2;
167    switch(cc){
168      case 'l':{ 
169        favorFolderFrom1 = main.favorPathSelector.panelMid;
170        favorFolderFrom2 = main.favorPathSelector.panelRight;
171        from1 = "from mid"; from2 = "from right";
172      } break;  
173      case 'm':{ 
174        favorFolderFrom1 = main.favorPathSelector.panelLeft;
175        favorFolderFrom2 = main.favorPathSelector.panelRight;
176        from1 = "from left"; from2 = "from rigth";
177      } break;  
178      case 'r':{ 
179        favorFolderFrom1 = main.favorPathSelector.panelLeft;
180        favorFolderFrom2 = main.favorPathSelector.panelMid;
181        from1 = "from left"; from2 = "from mid";
182      } break;  
183      default: favorFolderFrom1 = favorFolderFrom2 = null;
184      from1 = from2 = null;
185    }
186    final String[] cells = new String[2];
187    cells[0] = "";
188    cells[1] = from1;
189    cardFavorThemes.wdgdTable.addLine(null, cells, favorFolderFrom1);
190    cells[1] = from2;
191    cardFavorThemes.wdgdTable.addLine(null, cells, favorFolderFrom2);
192
193    //List of favor pathes for this main panel
194    for(FcmdFavorPathSelector.FavorFolder favorFolder: main.favorPathSelector.listAllFavorPathFolders){ //panel specific favorites
195      int mMainTab = 0x1 << (cNr-'1');  //1, 2 or 4
196      if((favorFolder.mMainPanel & mMainTab) !=0 && favorFolder.label !=null && favorFolder.label.length() >0){
197        //create Panels for the file table and favor path table if not found yet, otherwise search it.
198        FcmdFileCard fileTab = searchOrCreateFileCard(favorFolder.label);
199          //Favor select list of the associated File table
200        fileTab.favorCard.fillFavorPaths(favorFolder);
201      } else {
202        //The fileTable may be existend, then 
203        FcmdFileCard fileTab = searchFileCard(favorFolder.label);
204        if(fileTab !=null && fileTab.label.equals(favorFolder.label)){
205          fileTab.favorCard.fillFavorPaths(favorFolder);
206        }
207      }
208      cardFavorThemes.addFavorFolder(favorFolder);
209      //tabSelector.initActDir(indexActualDir, info.selectName, info.path);
210     
211    }
212  }
213  
214  
215  
216  FcmdFileCard searchOrCreateFileCard(String label){
217  //search or create the tab
218    FcmdFileCard fileCard = null;
219    String labelTab = label + "." + cNr;
220    for(FcmdFileCard item: listTabs){
221      if(item.nameFilePanel.equals(labelTab)){ 
222        fileCard = item; break;
223      }
224    } 
225    if(fileCard == null){
226      fileCard = new FcmdFileCard(this, label);
227      listTabs.add(fileCard);
228    }
229    return fileCard;
230  }
231  
232    
233    
234  FcmdFileCard searchFileCard(String label){
235  //search or create the tab
236    FcmdFileCard fileCard = null;
237    String labelTab = label + "." + cNr;
238    for(FcmdFileCard item: listTabs){
239      if(item.nameFilePanel.equals(labelTab)){ 
240        fileCard = item; break;
241      }
242    } 
243    return fileCard;
244  }
245    
246  
247  void checkRefresh(long since){
248    if(actFileCard !=null) actFileCard.checkRefresh(since);
249  }
250  
251  
252  @Override public String toString(){ return "panel " + cc; }
253  
254  
255  boolean focusLeftCard(){ 
256    FcmdFileCard fileTableLeft = null;
257    boolean found = false;
258    for(FcmdFileCard fileTable: listTabs){
259      if(fileTable == actFileCard){ found = true;  break;}
260      fileTableLeft = fileTable;  //save this table as table left, use if found.
261    }
262    if(found){
263      if(fileTableLeft !=null){
264        actFileCard = fileTableLeft;
265        fileTableLeft.setFocusFavorOrFile(); 
266      } else {  //left from first is the selectAllTable of this panel.
267        cardFavorThemes.setFocus();
268      }
269    }
270    return true; 
271  }
272  
273  
274  boolean focusRightCard(){ 
275    //sets focus to right
276    FcmdFileCard fileTableRight = null;
277    boolean found = bFavorThemeCardHasFocus; //true if the left card is focused.
278    for(FcmdFileCard fileTable: listTabs){
279      if(found){ fileTableRight = fileTable; break; }  //use this next table if found before.
280      if(fileTable == actFileCard) { found = true; }
281    }
282    if(fileTableRight !=null){
283      actFileCard = fileTableRight;
284      fileTableRight.setFocusFavorOrFile(); 
285    } else {
286      //remain the last
287    }
288    return true; 
289  }
290  
291  
292    
293  static class FcmdFavorThemeCard extends GralSelectList
294  {
295    
296    private final FcmdLeftMidRightPanel mainPanel;
297    
298    /**Index of all entries in the visible list. */
299    Map<String, FcmdFavorPathSelector.FavorFolder> indexFavorFolders = new TreeMap<String, FcmdFavorPathSelector.FavorFolder>();
300    
301    //final int[] widthSelecttableMain = new int[]{10, 30};
302
303    public FcmdFavorThemeCard(Fcmd main, String name, FcmdLeftMidRightPanel panel)
304    { //super(name, mng);
305      super(name, 20, new int[]{10, 30}, 'C');
306
307      mainPanel = panel;
308    }
309
310    
311    
312    @Override
313    public void createImplWidget_Gthread(){
314      super.createImplWidget_Gthread();
315      wdgdTable.specifyActionOnLineSelected(actionFavorThemeLineSelected);
316      wdgdTable.setHtmlHelp(mainPanel.main.cargs.dirHtmlHelp + "/Fcmd.html#Topic.FcmdHelp.favorpath.tabSelect.");
317    }
318
319    /**Adds a line to this table.
320     * @param ix Show which index is used for a local table, 0..2 for left, mid, right,
321     *   than show the label in the left cell (column)
322     * @param favorTabInfo The favorite info
323     */
324    void addFavorFolder(FcmdFavorPathSelector.FavorFolder favorTabInfo)
325    {
326      if(indexFavorFolders.get(favorTabInfo.label) == null){
327        indexFavorFolders.put(favorTabInfo.label, favorTabInfo);
328        String[] cells = new String[2];
329        cells[0] = favorTabInfo.label;
330        cells[1] = favorTabInfo.selectNameTab;
331        GralTableLine_ifc line = wdgdTable.addLine(null, cells, favorTabInfo);
332        //line.setUserData(favorTabInfo);
333        //line.setCellText(favorTabInfo.label, 0);
334        //line.setCellText(favorTabInfo.selectNameTab, 1);
335        line.repaint(100, 0);
336      }
337    }
338    
339    
340    @Override public boolean setFocus(){
341      mainPanel.bFavorThemeCardHasFocus = true;
342      return super.setFocus();
343    }
344    
345    void clear()
346    {
347      wdgdTable.clearTable();
348    }
349    
350    
351    @Override protected boolean actionOk(Object userData, GralTableLine_ifc line)
352    {
353      FcmdFavorPathSelector.FavorFolder favorTabInfo = null;
354      final String label;
355      final FileRemote currentDir;
356      Object oLineData = line.getUserData();
357      if(oLineData instanceof FcmdFavorPathSelector.FavorFolder){
358        favorTabInfo = (FcmdFavorPathSelector.FavorFolder)line.getUserData();
359        label = favorTabInfo.label;  //from favorite list
360        //label is known in the favorite list, use it. The panel should be existing or it is created.
361        //search or create the tab, representing by its fileTable:
362        mainPanel.actFileCard = mainPanel.searchOrCreateFileCard(label);
363        //before changing the content of this fileTable, store the current directory
364        //to restore if this favor respectively selection is used ones more.
365        if(mainPanel.actFileCard.favorPathInfo !=null){
366          currentDir = mainPanel.actFileCard.getCurrentDir(); //.getAbsolutePath();
367        } else {
368          //nothing selected, its a new tab. Don't show files.
369          currentDir = null;
370          //FcmdFavorPathSelector.FavorPath favorPathInfo = favorTabInfo.listfavorPaths.get(0);
371          //actFileCard.favorPathInfo = favorPathInfo;
372          //currentDir = favorPathInfo.getOriginDir();
373        }
374      } else {
375        //it have to be a:
376        final FcmdLeftMidRightPanel panel = (FcmdLeftMidRightPanel)oLineData;  //from left, from mid etc
377        final FcmdFileCard fileCard = panel.actFileCard;  //the current filecard in the other panel
378        label = fileCard.label;   
379        mainPanel.actFileCard = mainPanel.searchOrCreateFileCard(label);     //search or create such filecard with this label here.
380        mainPanel.actFileCard.favorPathInfo = fileCard.favorPathInfo;  //copy it, it is the same instance for all 3 panels.
381        //mainPanel.actFileCard.currentFile = fileCard.currentFile();      //select the same file.
382        mainPanel.actFileCard.fillIn(fileCard.currentDir(), false);      //select the same file.
383        currentDir = fileCard.getCurrentDir(); //.getAbsolutePath();
384        if(mainPanel.actFileCard == null){
385          Assert.check(false);
386        }
387        //search the proper FavorFolder for the label. 
388        //Note it isn't stored in the file card yet though the file card is associated to the label.
389        for(FcmdFavorPathSelector.FavorFolder folder: mainPanel.main.favorPathSelector.listAllFavorPathFolders){
390          if(folder.label.equals(label)){
391            favorTabInfo = folder;
392            break;  //found.
393          }
394        }
395        Assert.check(favorTabInfo != null);
396      }
397    
398      //adds all favorite pathes to it newly.
399      mainPanel.actFileCard.favorCard.fillFavorPaths(favorTabInfo);
400      if(currentDir !=null){
401        mainPanel.indexActualDir.put(mainPanel.actFileCard.favorPathInfo.selectName, currentDir.getPath());
402        mainPanel.actFileCard.fillIn(currentDir, false);
403        mainPanel.actFileCard.setFocus();
404      } else { 
405        mainPanel.actFileCard.favorCard.setFocus();
406      }
407      return true;
408    }
409
410    @Override
411    protected void actionLeft(Object userData, GralTableLine_ifc line)
412    {
413      // TODO Auto-generated method stub
414      
415    }
416
417    @Override
418    protected void actionRight(Object userData, GralTableLine_ifc line)
419    {
420      // TODO Auto-generated method stub
421      
422    }
423
424    /**Handle the keys for the JavaCommander-Selection of favorites
425     * <ul>
426     * <li>sh-F1 .. shF3: activates fileSelector for left, middle and right panel.
427     * </ul>
428     * @see org.vishia.gral.widget.GralSelectList#actionUserKey(int, java.lang.Object, org.vishia.gral.ifc.GralTableLine_ifc)
429     */
430    @Override protected boolean actionUserKey(int key, Object userData, GralTableLine_ifc line)
431    { boolean ret = true;
432      ret = false;
433      return ret;
434    }
435
436    /**Action is called any time if a line was focused in the favor theme table. */
437    GralUserAction actionFavorThemeLineSelected = new GralUserAction("actionFavorThemeLineSelected"){
438      @Override public boolean userActionGui(int actionCode, GralWidget widgd, Object... params) {
439        if(actionCode == KeyCode.userSelect){
440          mainPanel.bFavorThemeCardHasFocus = true;
441          //GralTableLine_ifc line = (GralTableLine_ifc) params[0];
442          //Object oData = line.getUserData();
443          //System.out.println("FcmdFavorCard.actionFavorSelected: " + fileTable.label);
444        }
445        return true;
446      }
447    };
448    
449
450    
451  }
452  
453  
454  
455}