001package org.vishia.guiBzr;
002
003import java.io.File;
004
005import org.vishia.gral.ifc.GralUserAction;
006import org.vishia.gral.ifc.GralWidget_ifc;
007import org.vishia.util.FileSystem;
008import org.vishia.util.KeyCode;
009
010public class MainAction
011{
012  final MainData mainData;
013  
014  final GuiStatusPanel guiStatusPanel;
015
016  final GuiCommitPanel guiCommitPanel;
017  
018  final GuiFilesDiffPanel guiFilesDiffPanel;
019  
020  final PanelOutput panelOutput;
021  
022  MainAction(MainData mainData, GuiStatusPanel guiStatusPanel
023      , final GuiCommitPanel guiCommitPanel, 
024      final GuiFilesDiffPanel guiFilesDiffPanel
025      , PanelOutput panelOutput
026      )
027  {
028    this.mainData = mainData;
029    this.guiStatusPanel = guiStatusPanel;
030    this.guiCommitPanel = guiCommitPanel;
031    this.guiFilesDiffPanel = guiFilesDiffPanel;
032    this.panelOutput = panelOutput;  
033  }
034  
035  Runnable initNewComponent = new Runnable()
036  {
037    @Override public void run()
038    { mainData.getterStatus.initListFiles();
039      guiFilesDiffPanel.fillFileTable(mainData.currCmpn);
040    }
041  };
042  
043  
044  /**Stores the commit text in a file and returns this File-object.
045   * Reads the text of the commit text box in the commit panel
046   * and writes it to the file "_bzrCommit.txt" in the current source component folder.
047   * @return null if the text in the box is empty, else the File where the commit text is stored.
048   */
049  File getContentofCommitText()
050  { final File fileCommitText;
051    String sCommitText = guiCommitPanel.widgdCommitText.getValue();
052    if(sCommitText.trim().length()>0 ){
053      fileCommitText = new File(mainData.currCmpn.dirWorkingtree, "_bzrCommit.txt");
054      FileSystem.writeFile(sCommitText, fileCommitText);
055    } else {
056      fileCommitText = null;
057    }
058    return fileCommitText;
059  }
060  
061
062  
063  String findBazaarExe(){
064    String sBzrExe = "D:/Progs/Bazaar/bzr.exe";
065    File bzrExe = new File(sBzrExe);
066    if(bzrExe.exists()) return sBzrExe;
067    sBzrExe = "D:/Programme/Bazaar/bzr.exe";
068    bzrExe = new File(sBzrExe);
069    if(bzrExe.exists()) return sBzrExe;
070    throw new IllegalArgumentException("BzrGui.MainAction - findBazaarExe; don't find bazaar.exe");
071  }
072  
073  
074  
075  
076  void revertWithTimestamps(){
077    String sBazaarExe = findBazaarExe();
078    try{
079      File dirWorkingTree = mainData.currCmpn.dirWorkingtree;
080      File fileBzr = new File(dirWorkingTree, ".bzr");
081      //search whether a .bzr or .bzr.bat exists and change to parent dir till it is found.
082      if(!fileBzr.exists()) throw new IllegalArgumentException(
083          "BzrRevertAllWithTimestamp - arg1 does not refer to a .bzr;");
084      //
085      File dirParent = dirWorkingTree.getParentFile();
086      mainData.cmdExec.setCurrentDir(dirParent);
087      File tempBzrDir = new File(dirParent, "tmpBzr");
088      if(tempBzrDir.exists()){
089        if(!FileSystem.cleandir(tempBzrDir)) throw new IllegalArgumentException(
090            "BzrRevertAllWithTimestamp - temp bzr dir can't remove -try it manually; " + tempBzrDir.getAbsolutePath());
091      }
092      File fileBzrTmp = new File(tempBzrDir, ".bzr");
093      FileSystem.mkDirPath(fileBzrTmp);   //creates the tempBzrDir if not exists
094      //Now move cmpn/.bzr to tmpBzr/.bzr
095      if(!fileBzr.renameTo(fileBzrTmp))  throw new IllegalArgumentException(
096          "BzrRevertAllWithTimestamp - .bzr can't move -check manually; " + fileBzrTmp.getAbsolutePath());
097      if(!FileSystem.rmdir(dirWorkingTree)){
098        boolean bMovedBack = fileBzrTmp.renameTo(fileBzr);
099        String text = "BzrRevertAllWithTimestamp - can't delete working tree -check manually; " + dirWorkingTree.getAbsolutePath();
100        if(!bMovedBack){
101          text += "; Note: The .bzr is yet temporary moved to " + fileBzrTmp.getAbsolutePath();  
102        }
103        throw new IllegalArgumentException(text);
104      }
105      //call the bazaar mainData.cmdExec:
106      String[] cmdarg = new String[5];
107      cmdarg[0] = sBazaarExe;
108      cmdarg[1] = "export";
109      cmdarg[2] = dirWorkingTree.getAbsolutePath();
110      cmdarg[3] = tempBzrDir.getAbsolutePath();
111      cmdarg[4] = "--per-file-timestamps";
112      mainData.cmdExec.execute(cmdarg, null, System.err, System.err);
113      //it is reverted.
114      boolean bMovedBack = fileBzrTmp.renameTo(fileBzr);
115      if(!bMovedBack){
116        throw new IllegalArgumentException("BzrRevertAllWithTimestamp - The .bzr is yet temporary moved to;" + fileBzrTmp.getAbsolutePath());
117      }
118      //Not moves the .bzr return
119      //assemble the bzrGetCmpn text.
120      StringBuilder uVersion = new StringBuilder();
121      String sCurrDirName = dirParent.getName();
122      //main.mainData.cmdExec.execute("D:/Progs/Bazaar/bzr.exe log -l 1", null, uVersion, null);
123      /*
124       * I don't know, it does not work:
125      String[] mainData.cmdExec = {"D:/Progs/Bazaar/bzr.exe", "version-info --custom --template=\"_bzrGetCmpn " + sCurrDirName + " {revision_id}\\n\""};
126      main.mainData.cmdExec.execute(mainData.cmdExec, null, uVersion, null, false);
127      main.mainData.cmdExec.execute("D:/Progs/Bazaar/bzr.exe version-info --custom --template=\"_bzrGetCmpn " + sCurrDirName + " {revision_id}\"", null, uVersion, null);
128      */
129      //instead:
130      mainData.cmdExec.setCurrentDir(dirWorkingTree);
131      mainData.cmdExec.execute("D:/Progs/Bazaar/bzr.exe version-info", null, uVersion, null);
132      //uVersion contains the output of bzr version-info. The second line is the revision id!
133      int pos1 = uVersion.indexOf("revision-id:");
134      int pos2 = uVersion.indexOf("\n", pos1+12);
135      String sRevisionId = uVersion.substring(pos1+13, pos2).trim();
136      String sGetCmpn = "bzrGetCmpn " + sCurrDirName + " " + sRevisionId + "\n"; 
137      System.out.println("jbatch/BazaarExplorer.java - write " + sGetCmpn);
138      File fileVersion = new File(dirParent, "_bzrGetCmpn-" + sCurrDirName + ".bat");
139      FileSystem.writeFile(sGetCmpn, fileVersion);
140      //Assert.stop();
141    } catch(Exception exc){
142      mainData.mainCmdifc.writeError(exc.getMessage());
143    }
144  }
145  
146  
147  /**Gets the status for the Component of the actual line, refresh the line. */
148  GralUserAction actionRevertWithTimestamp = new GralUserAction("actionRevertWithTimestamp"){
149    @Override public boolean exec(int actionCode, GralWidget_ifc widgi, Object... oArgs){
150      if(KeyCode.isControlFunctionMouseUpOrMenu(actionCode)){
151        if(mainData.currTableline !=null){
152          revertWithTimestamps();
153          //mainData.currTableline.setCellText(formatter.getContent(), 1);
154        }
155        return true;
156      } else return false;
157    }
158  };
159
160  
161  
162
163  
164}