G4UIGAG.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // G4UIGAG.cc
00030 // 18.Feb.98 M.Nagamatu and T.Kodama created G4UIGAG from G4UIterminal
00031 
00032 #include "G4UIGAG.hh"
00033 #include "G4StateManager.hh"
00034 #include "G4UIcommandTree.hh"
00035 #include "G4UIcommand.hh"
00036 #include "G4UIcommandStatus.hh"
00037 #include <sstream>
00038 
00039 G4UIGAG::G4UIGAG()//: TVersion("T1.0a"), JVersion("J1.0a")
00040 {
00041  TVersion="T1.0a"; JVersion="J1.0a";
00042   //G4cout << "G4UIGAG: Apr15,98." << G4endl;
00043   prefix = "/";
00044   UI = G4UImanager::GetUIpointer();
00045   UI->SetSession(this);
00046   UI->SetCoutDestination(this); 
00047   G4StateManager * statM = G4StateManager::GetStateManager();
00048   promptCharacter = statM->GetStateString(statM->GetCurrentState());
00049   uiMode = terminal_mode; // GAG
00050   iExit = false;
00051   iCont = false;
00052   // -- Initialize Notify routines begin
00053   G4UIcommandTree * tree = UI->GetTree();
00054   GetNewTreeStructure(tree,0);
00055   GetNewTreeValues(tree,0);
00056   previousTreeCommands = newTreeCommands;
00057   previousTreeParams = newTreeParams;
00058   previousTreePCP = newTreePCP;
00059   // -- end
00060 }
00061 
00062 G4UIGAG::~G4UIGAG()
00063 {
00064   if( G4UImanager::GetUIpointer() != 0)
00065   {
00066      UI->SetSession(NULL);
00067      UI->SetCoutDestination(NULL);
00068      //     G4cout << "GAG session deleted" << G4endl;
00069   }
00070 }                                                 
00071 
00072 G4UIsession * G4UIGAG::SessionStart()
00073 {
00074   iExit = true;
00075   G4StateManager * statM = G4StateManager::GetStateManager();
00076   promptCharacter = statM->GetStateString(statM->GetCurrentState());
00077   G4String newCommand = GetCommand();
00078   while( iExit )
00079   {
00080     ExecuteCommand(newCommand);
00081     promptCharacter = statM->GetStateString(statM->GetCurrentState());
00082     newCommand = GetCommand();
00083   }
00084   return NULL;
00085 }
00086 
00087 void G4UIGAG::PauseSessionStart(const G4String& msg)
00088 {
00089   promptCharacter = msg;
00090   G4cout << "@@PROMPT \"" << promptCharacter << "\"" << G4endl;
00091   iCont = true;
00092   G4String newCommand = GetCommand();
00093   while( iCont )
00094   {
00095     ExecuteCommand(newCommand);
00096     newCommand = GetCommand();
00097   }
00098 }
00099 
00100 void G4UIGAG::ExecuteCommand(const G4String& aCommand)
00101 {
00102   G4UIcommandTree * tree = UI->GetTree();
00103   if(aCommand.length()<2) return;
00104   G4int returnVal = UI->ApplyCommand(aCommand);
00105   G4int paramIndex = returnVal % 100;
00106   G4int commandStatus  = returnVal - paramIndex;
00107 
00108   UpdateState();
00109   if ( uiMode == terminal_mode){
00110     switch(commandStatus) {
00111     case fCommandSucceeded:
00112       break;
00113     case fCommandNotFound:
00114       //      G4cerr << "command not found" << G4endl;
00115     G4cerr << "command <" << UI->SolveAlias(aCommand) << "> not found" << G4endl;
00116       break;
00117     case fIllegalApplicationState:
00118       G4cerr << "illegal application state -- command refused" << G4endl;
00119       break;
00120     case fParameterOutOfRange:
00121     case fParameterUnreadable:
00122       G4cerr << "Parameter is wrong type and/or is not omittable (index " << paramIndex << ")" << G4endl;
00123       break;
00124     case fParameterOutOfCandidates:
00125       G4cerr << "Parameter is out of candidate list (index " << paramIndex << ")" << G4endl;
00126       //      G4cerr << "Candidates : " << cmd->GetParameter(paramIndex)->GetParameterCandidates() << G4endl;
00127       break;
00128     case fAliasNotFound:
00129     default:
00130       G4cerr << "command refused (" << commandStatus << ")" << G4endl;
00131     }
00132   }else{
00133     switch(commandStatus) {
00134     case fCommandSucceeded:
00135       {
00136         GetNewTreeStructure(tree,0);
00137         GetNewTreeValues(tree,0);
00138         if (CommandUpdated()) {
00139            NotifyCommandUpdate();
00140         } else {
00141            UpdateParamVal();  // if param is updated, call notifyPara...
00142         } 
00143         previousTreeCommands = newTreeCommands;
00144         previousTreeParams = newTreeParams;
00145         previousTreePCP = newTreePCP;
00146       }
00147       break;
00148     case fCommandNotFound:
00149       G4cout << "@@ErrResult \" <" << UI->SolveAlias(aCommand) << "> command not found.\"" << G4endl;
00150       break;
00151     case fIllegalApplicationState:
00152       G4cout << "@@ErrResult \"Illegal application state -- command refused\"" << G4endl;
00153       break;
00154     case fParameterOutOfRange:
00155       G4cout << "@@ErrResult \"Parameter Out of Range.\"" << G4endl;
00156       break;
00157     case fParameterUnreadable:
00158       G4cout << "@@ErrResult \"Parameter is wrong type and/or is not omittable.\"" << G4endl;
00159       break;
00160     case fParameterOutOfCandidates:
00161 //      G4cout << "@@ErrResult \"Parameter Out of Candidates. Candidates : " << cmd->GetParameter(paramIndex)->GetParameterCandidates()<< "\"" << G4endl;
00162       G4cout << "@@ErrResult \"Parameter Out of Candidates.\"" << G4endl;
00163       break;
00164     case fAliasNotFound:
00165     default:
00166       G4cout << "@@ErrResult \"command refused (" << commandStatus << ")\"" << G4endl;
00167     }
00168   }
00169 }
00170 
00171 
00172 G4int G4UIGAG::ReceiveG4cout(const G4String& coutString)
00173 {
00174   std::cout << coutString << std::flush;
00175   return 0;
00176 }
00177 
00178 G4int G4UIGAG::ReceiveG4cerr(const G4String& cerrString)
00179 {
00180   std::cerr << cerrString << std::flush;
00181   return 0;
00182 }                                                    
00183 
00184 void G4UIGAG::Prompt(const G4String& aPrompt)
00185 {
00186   promptCharacter = aPrompt;
00187 }
00188 
00189 G4String G4UIGAG::GetCommand()
00190 {
00191   G4String newCommand;
00192   G4String nullString;
00193   while( 1 )
00194   {
00195     G4UIcommandTree* tree = UI->GetTree();
00196     if ( uiMode != terminal_mode ){
00197       G4cout << "@@PROMPT \"" << promptCharacter << "\"" << G4endl;
00198     }
00199     if ( uiMode != java_mode ){
00200       G4cout << promptCharacter << "> " << std::flush;
00201     }else{
00202       G4cout << "@@Ready" << G4endl;
00203     }
00204     newCommand.readLine( G4cin, FALSE );
00205     if (!G4cin.good()) { G4cin.clear(); newCommand = nullString; iExit=false;break;}
00206 
00207     newCommand = newCommand.strip(G4String::leading);
00208     if( newCommand.length() < 1) { break; }
00209 
00210     while( newCommand(newCommand.length()-1) == '_' )
00211     {
00212       G4String newLine;
00213       newCommand.remove(newCommand.length()-1);
00214       newLine.readLine( G4cin );
00215       if (!G4cin.good()) { G4cin.clear(); newCommand = nullString; iExit=false;break;}
00216       newCommand.append(newLine);
00217     }
00218 
00219     G4String nC = newCommand.strip(G4String::leading);
00220     if( nC.length() < 1) { break; }
00221 
00222     // -------------------- nC.toUpper();
00223     if( nC == "@@GAGmodeJAVA" ) {
00224       uiMode = java_mode;
00225       G4cout << G4endl << "@@Version " << JVersion << G4endl;
00226       SendCommandProperties(tree);
00227       NotifyStateChange();
00228     }
00229     else if( nC == "@@GAGmodeTcl" ) {
00230       uiMode = tcl_mode;
00231       G4cout << G4endl << "@@Version " << TVersion << G4endl;
00232       SendCommandProperties(tree);
00233       NotifyStateChange();
00234     }
00235     else if( nC(0) == '#' )
00236       { G4cout << nC << G4endl; }
00237 
00238     else if( nC == "ls"  || nC(0,3) == "ls " )
00239     { ListDirectory( nC ); }
00240     else if( nC == "pwd" )
00241     { G4cout << "Current Working Directory : " << prefix << G4endl; }
00242     else if( nC(0,2) == "cd"  || nC(0,3) == "cd " )
00243     { ChangeDirectory( nC ); }
00244     else if(  nC == "help" || nC(0,5) == "help ")
00245     { TerminalHelp( nC ); }
00246     else if( nC(0) == '?' )
00247     { ShowCurrent( nC ); }
00248     else if( nC(0,4) == "hist"   || nC == "history")
00249     {
00250       G4int nh = UI->GetNumberOfHistory();
00251       for(int i=0;i<nh;i++)
00252       { G4cout << i << ": " << UI->GetPreviousCommand(i) << G4endl; }
00253     }
00254     else if( nC(0) == '!' )
00255     {
00256       G4String ss = nC(1,nC.length()-1);
00257       G4int vl;
00258       const char* tt = ss;
00259       std::istringstream is((char*)tt);
00260       is >> vl;
00261       G4int nh = UI->GetNumberOfHistory();
00262       if(vl>=0 && vl<nh)
00263       { 
00264         newCommand = UI->GetPreviousCommand(vl); 
00265         G4cout << newCommand << G4endl;
00266         break;
00267       }
00268       else
00269       { G4cerr << "history " << vl << " is not found." << G4endl; }
00270     }
00271     else if( nC(0,4) == "exit" )
00272     { 
00273       if( iCont )
00274       { 
00275         if ( uiMode == terminal_mode){
00276           G4cerr << "You are now processing RUN." << G4endl;
00277           G4cerr << "Please abrot it using \"/run/abort\" command first" << G4endl;
00278           G4cerr << " and use \"continue\" command until the application" << G4endl;
00279           G4cerr << " becomes to Idle." << G4endl;
00280         }else{
00281           G4cout << "@@ErrResult \"You are now processing RUN.\"" << G4endl;
00282         }
00283       }
00284       else
00285       {
00286         iExit = false;
00287         newCommand = nullString;
00288         break;
00289       }
00290     }
00291     else if(  nC == "cont" || nC == "continue" )
00292     { 
00293       iCont = false;
00294       newCommand = nullString;
00295       break;
00296     }
00297     else
00298     { break; }
00299   }
00300   return GetFullPath(newCommand);
00301 }
00302 
00303 G4String G4UIGAG::GetFullPath(const G4String& aNewCommand )
00304 {
00305   G4String newCommand = aNewCommand;
00306   newCommand.strip(G4String::both);
00307 
00308   G4String tmpString;
00309   if( newCommand(0) == '/' ) 
00310   { tmpString = newCommand; }
00311   else if( newCommand(0,3) == "../" )
00312   {
00313     G4String tmpPrefix = prefix;
00314     /*G4int*/ unsigned i_direc = 0;
00315     while( i_direc < newCommand.length() )
00316     {
00317       if( newCommand(i_direc,3) == "../" )
00318       {
00319         i_direc += 3;
00320         prefix = ModifyPrefix( G4String("../") );
00321       }
00322       else
00323       { break; }
00324     }
00325     tmpString = prefix;
00326     tmpString.append( newCommand( i_direc, newCommand.length()-i_direc ) );
00327     prefix = tmpPrefix;
00328   }
00329   else
00330   {
00331     tmpString = prefix;
00332     tmpString.append( newCommand );
00333   }
00334   return tmpString;
00335 }
00336 
00337 void G4UIGAG::SessionTerminate()
00338 {
00339   G4cout << "***** Terminal session end *****" << G4endl;
00340 }
00341 
00342 void G4UIGAG::ShowCurrent(const G4String& newCommand )
00343 {
00344   G4String theCommand = GetFullPath(newCommand.substr(1,newCommand.length()-1));
00345   G4String curV = UI->GetCurrentValues(theCommand);
00346   if( ! (curV.isNull()||curV(0)=='\0' ) ) {
00347     if (uiMode == terminal_mode){
00348       G4cout << "Current value(s) of the parameter(s) : " << curV << G4endl;
00349     }else{
00350       G4cout << "@@CurrentValue " << curV << G4endl;
00351     }
00352   } else if (uiMode == terminal_mode){
00353       G4cout << "Current value is not available." << G4endl;
00354     } else {
00355       G4cout << "@@ErrResult \"Current value is not available.\"" << G4endl;
00356     }
00357 }
00358 
00359 void G4UIGAG::ChangeDirectory(const G4String& newCommand )
00360 {
00361   G4String savedPrefix = prefix;
00362   if( newCommand.length() <= 3 )
00363   { prefix = "/"; }
00364   else
00365   {
00366     G4String aNewPrefix = newCommand.substr(3,newCommand.length()-3);
00367     G4String newPrefix = aNewPrefix.strip(G4String::both);
00368     if( newPrefix(0) == '/' )
00369     { prefix = newPrefix; }
00370     else if( newPrefix(0) != '.' )
00371     { 
00372       prefix += newPrefix;
00373     }
00374     else
00375     { prefix = ModifyPrefix( newPrefix ); }
00376   }
00377   if( prefix( prefix.length() - 1 ) != '/' )
00378   { prefix += "/"; }
00379   if( FindDirPath( prefix ) == NULL )
00380   {
00381     G4cout << "Directory <" << prefix << "> is not found." << G4endl;
00382     prefix = savedPrefix;
00383   }
00384 }
00385 
00386 void G4UIGAG::ListDirectory(const G4String& newCommand )
00387 {
00388   G4String targetDir('\0');
00389   if( newCommand.length() <= 3 )
00390   { targetDir = prefix; }
00391   else
00392   {
00393     G4String newPrefix = newCommand.substr(3,newCommand.length()-3);
00394     newPrefix.strip(G4String::both);
00395     if( newPrefix(0) == '/' )
00396     { targetDir = newPrefix; }
00397     else if( newPrefix(0) != '.' )
00398     { 
00399       targetDir = prefix;
00400       targetDir += newPrefix;
00401     }
00402     else
00403     { targetDir = ModifyPrefix(newPrefix); }
00404   }
00405   if( targetDir( targetDir.length() - 1 ) != '/' )
00406   { targetDir += "/"; }
00407   G4UIcommandTree * commandTree = FindDirPath( targetDir );
00408   if( commandTree == NULL )
00409   { G4cout << "Directory <" << targetDir << "> is not found." << G4endl; }
00410   else
00411   { commandTree->ListCurrent(); }
00412 }
00413 
00414 void G4UIGAG::TerminalHelp(const G4String& newCommand)
00415 {
00416   G4UIcommandTree * treeTop = UI->GetTree();
00417   /*int*/str_size i = newCommand.index(" ");
00418   if( i != std::string::npos )
00419   {
00420     G4String newValue = newCommand.substr(i+1,newCommand.length()-(i+1));
00421     newValue.strip(G4String::both);
00422     if( newValue(0) != '/' )
00423     { newValue.prepend( prefix ); }
00424     G4UIcommand * theCommand = treeTop->FindPath( newValue );
00425     if( theCommand != NULL ) 
00426     { 
00427       theCommand->List();
00428       return;
00429     }
00430     else
00431     {
00432       G4cout << "Command <" << newValue << " is not found." << G4endl;
00433       return;
00434     }
00435   }
00436 
00437   G4UIcommandTree * floor[10];
00438   floor[0] = treeTop;
00439   int iFloor = 0;
00440   /*int*/ unsigned prefixIndex = 1;
00441   while( prefixIndex < prefix.length()-1 )
00442   {
00443     int ii = prefix.index("/",prefixIndex);
00444     floor[iFloor+1] =
00445       floor[iFloor]->GetTree(G4String(prefix(0,ii+1)));
00446     prefixIndex = ii+1;
00447     iFloor++;
00448   }
00449   floor[iFloor]->ListCurrentWithNum();
00450   // 1998 Oct 2 non-number input
00451   while(1){
00452     int j;
00453     G4cout << G4endl << "Type the number ( 0:end, -n:n level back ) : "<<std::flush;
00454     G4cin >> j;
00455     if(!G4cin.good()){
00456       G4cin.clear();
00457       G4cin.ignore(30,'\n');
00458       G4cout << G4endl << "Not a number, once more" << G4endl; continue;}
00459     else if( j < 0 ){
00460       iFloor += j;
00461       if( iFloor < 0 ) iFloor = 0;
00462       floor[iFloor]->ListCurrentWithNum(); continue;}
00463     else if(j == 0) { break;}
00464     else if( j > 0 ) {
00465       int  n_tree = floor[iFloor]->GetTreeEntry();
00466       if( j > n_tree )
00467       {
00468         if( j <= n_tree + floor[iFloor]->GetCommandEntry() )
00469         {
00470           floor[iFloor]->GetCommand(i-n_tree)->List();
00471           //iFloor++;
00472         }
00473       }
00474       else
00475       {
00476         floor[iFloor+1] = floor[iFloor]->GetTree(i);
00477         iFloor++;
00478         floor[iFloor]->ListCurrentWithNum();
00479       }
00480     }
00481 
00482   }
00483   G4cout << "Exit from HELP." << G4endl << G4endl;
00484   G4cout << G4endl;
00485   // G4cin.flush();
00486   char temp[100];
00487   G4cin.getline( temp, 100 );
00488 }
00489 
00490 G4String G4UIGAG::ModifyPrefix(G4String newCommand)
00491 {
00492   G4String newPrefix = prefix;
00493   while( 1 )
00494   {
00495     if( newCommand.substr(0,2) == ".." )
00496     {
00497       if( newPrefix != "/" )
00498       { 
00499         G4String tmpString = newPrefix(0,newPrefix.length()-1);
00500         newPrefix = newPrefix(0,tmpString.last('/')+1); 
00501       }
00502     }
00503     else
00504     {
00505       newPrefix += newCommand;
00506       break;
00507     }
00508     if( newCommand == ".." || newCommand == "../" )
00509     { break; }
00510     newCommand = newCommand.substr(3,newCommand.length()-3);
00511   }
00512   return newPrefix;
00513 }
00514 
00515 G4UIcommandTree * G4UIGAG::FindDirPath(const G4String& newCommand)
00516 {
00517   G4UIcommandTree * comTree = UI->GetTree();
00518   /*int*/ unsigned idx = 1;
00519   while( idx < newCommand.length()-1 )
00520   {
00521     int i = newCommand.index("/",idx);
00522     comTree = comTree->GetTree(G4String(newCommand.substr(0,i+1)));
00523     if( comTree == NULL ) 
00524     { return NULL; }
00525     idx = i+1;
00526   }
00527   return comTree;
00528 }
00529 
00530 // ----- for JAVA GAG (by T.Kodama)
00531 
00532 void G4UIGAG::SendCommandProperties(G4UIcommandTree * tree)
00533 {
00534   if( tree == NULL ) { 
00535     G4cerr << "GetTree() returnes null." << G4endl;
00536     return;
00537   }
00538   if (uiMode == java_mode){
00539     G4cout << "@@JTreeBegin" << G4endl;
00540     CodeGenJavaTree(tree, 0);  
00541     G4cout << "@@JTreeEnd" << G4endl;
00542     CodeGenJavaParams(tree, 0);
00543   }else{
00544     G4cout << G4endl << "@@maketree_start" << G4endl;
00545     CodeGenTclTree(tree,0);  
00546     G4cout << "@@maketree_end" << G4endl;
00547     CodeGenTclParams(tree, 0);
00548   }
00549 }
00550 void G4UIGAG::SendParameterProperties(G4UIcommandTree * tree)
00551 {
00552   if( tree == NULL ) { 
00553     G4cerr << "GetTree() returnes null." << G4endl;
00554     return;
00555   }
00556   if (uiMode == java_mode){
00557     CodeGenJavaParams(tree, 0);
00558   }else{
00559     CodeGenTclParams(tree, 0);
00560   }
00561 }
00562 
00563 void G4UIGAG::CodeGenJavaTree(G4UIcommandTree * tree, int level)
00564 { 
00565   int treeEntry, commandEntry;
00566   treeEntry = tree->GetTreeEntry();
00567   commandEntry = tree->GetCommandEntry();
00568 
00569   if(level!=0) {
00570     for(int i=0; i<commandEntry; i++){
00571       G4cout << tree->GetCommand(i+1)->GetCommandPath() << G4endl;
00572     }
00573   }
00574   if(treeEntry == 0) return; //end recursion
00575   
00576   for(int j=0; j<treeEntry; j++){
00577     CodeGenJavaTree(tree->GetTree(j+1), level+1);
00578   }
00579 }
00580 
00581 void G4UIGAG::CodeGenJavaParams(G4UIcommandTree * tree, int level) //recursive
00582 {
00583   int treeEntry, commandEntry, i;
00584   //G4UIcommand * Comp;
00585   G4UIcommandTree * treeLink;
00586 
00587   treeEntry = tree->GetTreeEntry();
00588   commandEntry = tree->GetCommandEntry();
00589 
00590   for(i=0; i<commandEntry; i++) {
00591     SendAParamProperty(tree->GetCommand(i+1));
00592   }
00593   if( treeEntry == 0 )  return;     // end recursion
00594 
00595   for(i=0; i< treeEntry; i++) {
00596     treeLink = tree->GetTree(i+1);
00597     G4cout << "@@JDirGuideBegin" << G4endl;
00598     G4cout << treeLink->GetPathName() << G4endl << treeLink->GetTitle() << G4endl;
00599     G4cout << "@@JDirGuideEnd" << G4endl;
00600     CodeGenJavaParams(treeLink, level+1); 
00601   }
00602 }
00603 
00604 void G4UIGAG::SendAParamProperty(G4UIcommand * Comp)
00605 {
00606   int guidanceEntry, parameterEntry;
00607   G4String title, title2;
00608   G4UIparameter * prp; 
00609   char c[2];
00610   guidanceEntry = Comp->GetGuidanceEntries();
00611   parameterEntry = Comp->GetParameterEntries();
00612   G4cout << "@@JParamBegin" << G4endl;
00613   G4cout << Comp->GetCommandPath() << G4endl;
00614   G4cout << guidanceEntry << G4endl;
00615   for (int j=0; j<guidanceEntry; j++){
00616     title = Comp->GetGuidanceLine(j);
00617     title2 = "";
00618     if (title != ""){
00619       for(int i=0; i< (int)title.length(); i++){
00620         c[0]=title(i);
00621         c[1]= '\0';
00622         if ( c[0] == '\n' || c[0] == '\r') {
00623           c[0]= ' ';
00624         }
00625         title2.append(c);
00626       }
00627     }
00628     G4cout << title2 << G4endl;
00629   }
00630   G4cout << Comp->GetRange() << G4endl;
00631   G4cout << parameterEntry << G4endl;
00632   for( int par=0; par<parameterEntry; par++) {
00633     prp = (G4UIparameter *)Comp->GetParameter(par);
00634     G4cout << prp->GetParameterName() << G4endl;
00635     G4cout << prp->GetParameterGuidance() << G4endl;
00636     G4cout << prp->GetParameterType() << G4endl;
00637     G4cout << prp->IsOmittable() << G4endl;
00638     G4cout << prp->GetDefaultValue() << G4endl;
00639     G4cout << prp->GetParameterRange() << G4endl;
00640     G4cout << prp->GetParameterCandidates() << G4endl;
00641   }
00642   G4cout << "@@JParamEnd" << G4endl;
00643 }
00644 
00645 void G4UIGAG::SendDisableList(G4UIcommandTree * tree, int level)
00646 { 
00647   int treeEntry, commandEntry;
00648   G4UIcommand * Comp;
00649   treeEntry = tree->GetTreeEntry();
00650   commandEntry = tree->GetCommandEntry();
00651 
00652   for(int com=0; com<commandEntry; com++) {
00653     Comp = tree->GetCommand(com+1);
00654     if( Comp->IsAvailable()==false ) {
00655        G4cout << Comp->GetCommandPath()<<G4endl;
00656     }
00657   }
00658   if( treeEntry == 0 ) return;     // end recursion
00659 
00660   for( int i=0; i<treeEntry; i++) {
00661     SendDisableList(tree->GetTree(i+1), level+1); 
00662     // be sure the function name is the same
00663   }
00664 }
00665 
00666 // ----- for Tcl GAG
00667 
00668 void G4UIGAG::CodeGenTclTree(G4UIcommandTree * tree, int level)
00669 { 
00670   int i, j;
00671   int treeEntry, commandEntry, guidanceEntry;
00672   treeEntry = tree->GetTreeEntry();
00673   commandEntry = tree->GetCommandEntry();
00674   G4String commandPath, pathName, title1, title2;
00675   G4UIcommandTree * t;
00676   G4UIcommand * Comp;
00677 
00678   for(int com=0; com<commandEntry; com++){
00679     Comp = tree->GetCommand(com+1);
00680     commandPath = Comp->GetCommandPath();
00681     G4cout << commandPath << " @@command" << G4endl;
00682     guidanceEntry = Comp->GetGuidanceEntries();
00683     if (guidanceEntry == 0){
00684       title2 = "...Title not available...";
00685     } else {
00686       title2 = "";
00687       j = 0;
00688       while(1){
00689         title1 = Comp->GetGuidanceLine(j);
00690         for(i=0; i< (int)title1.length(); i++){
00691           char c[2];
00692           c[0]=title1(i);
00693           c[1]= '\0';
00694           if( c[0] == '\"') {
00695             title2.append("\\\""); // a Backslash and a double quote
00696           } else if ( c[0] == '\n' || c[0] == '\r') {
00697             title2.append("\\n");
00698           } else title2.append(c);
00699         }
00700         j++;
00701         if (j >= guidanceEntry) break;
00702         title2.append("\\n");
00703       }
00704     }
00705     G4cout << commandPath << " @@title \""<< title2 <<"\""<< G4endl;
00706   }
00707 
00708   if(treeEntry == 0) return; //end recursion
00709 
00710   for(i=0; i< treeEntry; i++){
00711     t = tree->GetTree(i+1);
00712     pathName =  t->GetPathName();   
00713     title1 = t->GetTitle();
00714     title2 = "";
00715     for(int k=0; k<(int)title1.length(); k++){
00716       char c[2];
00717       c[0]=title1(k);
00718       c[1]= '\0';
00719       if( c[0] == '\"') 
00720         title2.append("\\\""); // a Backslash and a double quote
00721       else title2.append(c);
00722     }
00723     if(level==0) G4cout << pathName<< G4endl;
00724     else G4cout << pathName<< "  @@cascade"<<G4endl;
00725     G4cout << pathName << "  @@title \"" << title1  << "\""<<G4endl;
00726     CodeGenTclTree(t, level+1);
00727   }
00728 }
00729 
00730 void G4UIGAG::CodeGenTclParams( G4UIcommandTree * tree, int level) // recursive
00731 {
00732   int treeEntry, commandEntry;
00733   G4UIcommand * Comp;
00734   treeEntry = tree->GetTreeEntry();
00735   commandEntry = tree->GetCommandEntry();
00736 
00737   for(int com=0; com<commandEntry; com++) {
00738     Comp = tree->GetCommand(com+1);
00739     SendATclParamProperty(Comp);
00740   }
00741   if( treeEntry == 0 ) return;     // end recursion
00742 
00743   for( int i=0; i<treeEntry; i++) {
00744     CodeGenTclParams(tree->GetTree(i+1), level+1); 
00745     // be sure the function name is the same
00746   }
00747 }
00748 
00749 void G4UIGAG::SendATclParamProperty(G4UIcommand * Comp)
00750 {
00751     G4UIparameter * prp; 
00752     int parameterEntry = Comp->GetParameterEntries();
00753     G4String commandPath = Comp->GetCommandPath();
00754     G4String commandRange = Comp->GetRange();
00755     G4cout << "@@parameter_start" << G4endl;
00756     G4cout << commandPath << " @@param " << parameterEntry << G4endl;
00757     G4cout << "@@command_range \"" << commandRange << "\"" << G4endl;
00758     for( int par=0; par<parameterEntry; par++) {
00759       prp = (G4UIparameter *)Comp->GetParameter(par);
00760       G4cout << "{" ;
00761       G4cout << "@@param_name : \"" << prp->GetParameterName() <<"\""<<G4endl;
00762       G4String  guide1,guide2;
00763       guide1 = prp->GetParameterGuidance();
00764       guide2 = "";
00765       for(int i=0; i<(int)guide1.length(); i++){
00766         char c[2];
00767         c[0]=guide1(i);
00768         c[1]= '\0';
00769         if( c[0] == '\"') 
00770         guide2.append("\\\""); // a Backslash and a double quote
00771         else guide2.append(c);
00772       }
00773       G4cout << " @@param_guide : \"" << guide2 << "\""<<G4endl; 
00774       G4cout << " @@param_type : \"" << prp->GetParameterType()<<"\""<<G4endl;
00775       G4cout << " @@param_omit : \"" << prp->IsOmittable()<<"\""<<G4endl;
00776       G4cout << " @@param_default : \""<< prp->GetDefaultValue()<<"\""<<G4endl;
00777       G4cout << " @@param_range : \""<< prp->GetParameterRange()<<"\""<<G4endl;
00778       G4cout << " @@param_candidate : \"" << prp->GetParameterCandidates()<< "\""<<G4endl;
00779       G4cout << "}" << G4endl;
00780     }
00781     G4cout << "@@parameter_end" << G4endl;
00782 }
00783 
00784 void G4UIGAG::NotifyStateChange(void)
00785 {
00786    G4String stateString;
00787    G4StateManager * statM = G4StateManager::GetStateManager();
00788    G4UIcommandTree * tree = UI->GetTree();
00789    stateString = statM->GetStateString(statM->GetCurrentState());
00790    if ( uiMode != terminal_mode ){
00791      G4cout << "@@State \"" << stateString << "\"" << G4endl;
00792      G4cout << "@@DisableListBegin"<<G4endl;
00793      SendDisableList(tree, 0);
00794      G4cout << "@@DisableListEnd" <<G4endl;
00795    }
00796 }
00797 
00798 void G4UIGAG::NotifyCommandUpdate(void)
00799 {
00800   G4UIcommandTree * tree = UI->GetTree();
00801   SendCommandProperties(tree);
00802 }
00803 
00804 void G4UIGAG::NotifyParameterUpdate(G4UIcommand* com)
00805 {
00806   if (uiMode == java_mode) 
00807     SendAParamProperty(com);
00808   else
00809     SendATclParamProperty(com);
00810 }
00811 
00812 //####### update check routines ####################################
00813 void G4UIGAG::UpdateState(void)
00814 {
00815    static G4ApplicationState previousState= G4State_PreInit;
00816    G4ApplicationState  newState;
00817    G4StateManager *statM = G4StateManager::GetStateManager();
00818    newState = statM->GetCurrentState();
00819    if( newState != previousState ) 
00820    {
00821       NotifyStateChange();
00822       previousState = newState; 
00823    }
00824 }
00825 
00826 int G4UIGAG::CommandUpdated(void)
00827 {
00828   int added=0, deleted=0;
00829   int pEntry= previousTreeCommands.size();
00830   int nEntry= newTreeCommands.size();
00831   int i,j;
00832   for( i=0; i<pEntry; i++) {      // check deleted command(s)
00833       for( j=0; j<nEntry; j++) {
00834          if( previousTreeCommands[i] == newTreeCommands[j]) break;
00835       }
00836       if( j==nEntry ) { 
00837          deleted = 1;
00838          //G4cout <<"deleted: "<< previousTreeCommands(i) << G4endl;
00839       }
00840   }
00841   for( i=0; i<nEntry; i++) {      // check added command(s)
00842       for( j=0; j<pEntry; j++) {
00843          if( newTreeCommands[i] == previousTreeCommands[j]) break;
00844       }
00845       if( j==pEntry ) { 
00846          added = 1;
00847       //   G4cout <<"added: "<< newTreeCommands(i) << G4endl;
00848       }
00849   }
00850   if( added    && deleted==0 ) {G4cout<<"c added"<<G4endl;return added;}
00851   if( added==0 && deleted ) {G4cout<<"c deleted"<<G4endl;return deleted;}
00852   if( added    && deleted ) {G4cout<<"c add/deleted"<<G4endl;return addedAndDeleted;}
00853   return notChanged;
00854 }
00855 
00856 void G4UIGAG::GetNewTreeStructure(G4UIcommandTree * tree, int level)
00857 { 
00858   G4String commandPath;
00859   G4String title; 
00860   G4String pathName; //tree name
00861   G4UIcommandTree * t;
00862   int treeEntry    = tree->GetTreeEntry();
00863   int commandEntry = tree->GetCommandEntry();
00864 
00865   if( level==0 ) { newTreeCommands.clear();}
00866   for(int com=0; com<commandEntry; com++){
00867       commandPath = tree->GetCommand(com+1)->GetCommandPath();
00868       title = tree->GetCommand(com+1)->GetTitle();
00869       newTreeCommands.push_back( commandPath + " " + title );
00870   }
00871 
00872   if(treeEntry == 0) return; //end recursion
00873 
00874   for(int i=0; i< treeEntry; i++){
00875     t = tree->GetTree(i+1);
00876     pathName =  t->GetPathName();   
00877     title = t->GetTitle();
00878     newTreeCommands.push_back( pathName + " " + title );
00879     GetNewTreeStructure(t, level+1);
00880   }
00881 }
00882 
00883 void G4UIGAG::UpdateParamVal(void)
00884 {
00885   // call NotifyParameterUpdate() if the value of each
00886   //  command/parameter is updated.
00887   //  assuming the command structure is not changed.
00888   int pEntry= previousTreeParams.size();
00889   int nEntry= newTreeParams.size();
00890   int i;
00891   G4UIcommand* Comp;
00892   if (pEntry != nEntry) return; 
00893   for( i=0; i<nEntry; i++) {
00894     if( previousTreeParams[i] != newTreeParams[i]){
00895        Comp = newTreePCP[i];
00896        G4cout << Comp->GetCommandPath()
00897             << " command is updated." <<G4endl; 
00898        NotifyParameterUpdate(Comp);
00899     }
00900   }
00901 }
00902 
00903 //void G4UIGAG::paramUpdate(void)
00904 //{  
00905 //  int added=0, deleted=0;
00906 //  int pEntry= previousTreeParams.entries();
00907 //  int nEntry= newTreeParams.entries();
00908 //  int i,j;
00909 //
00910 //  if (pEntry != nEntry)  return NULL;
00911 //  for( i=0; i<pEntry; i++) {       // check deleted param(s)
00912 //    for( j=0; j<nEntry; j++) {
00913 //       if( previousTreeParams(i) == newTreeParams(j)) break;
00914 //    }
00915 //    if( j==nEntry ) { 
00916 //       deleted = 1;
00917 //       //G4cout <<"para deleted: "<< previousTreeParams(i) << G4endl;
00918 //    }
00919 //  }
00920 //  for( i=0; i<nEntry; i++) {      // check added param(s)
00921 //    for( j=0; j<pEntry; j++) {
00922 //       if( newTreeParams(i) == previousTreeParams(j)) break;
00923 //    }
00924 //    if( j==pEntry ) { 
00925 //       added = 1;
00926 //       //G4cout <<"para added: "<< newTreeParams(i) << G4endl;
00927 //    }
00928 //  }
00929 //  if( added    && deleted==0 ) {G4cout<<"p added"<<G4endl;return added;}
00930 // if( added==0 && deleted )  {G4cout<<"p deleted"<<G4endl;return deleted;}
00931 //  if( added    && deleted )  {G4cout<<"p add/deleted"<<G4endl; return addedAndDeleted;}
00932 //  return notChanged;
00933 //}
00934 
00935 void G4UIGAG::GetNewTreeValues( G4UIcommandTree * tree, int level) // recursive
00936 {
00937    G4String commandPath;
00938    G4String pathName; //tree name
00939    G4UIcommandTree * t;
00940    int parameterEntry;
00941    int treeEntry    = tree->GetTreeEntry();
00942    int commandEntry = tree->GetCommandEntry();
00943    G4UIcommand * Comp;
00944    G4UIparameter * prp; 
00945    G4String param, str(" ");
00946 
00947    if( level==0 ) { newTreeParams.clear(); }
00948    for(int com=0; com<commandEntry; com++) {
00949       Comp = tree->GetCommand(com+1);
00950       commandPath    = Comp->GetCommandPath();
00951       parameterEntry = Comp->GetParameterEntries();
00952       param = commandPath +" ";
00953       for( int par=0; par< parameterEntry; par++) {
00954          prp = (G4UIparameter *)Comp->GetParameter(par);
00955          param += prp->GetParameterName() +" ";
00956          str(0) = prp->GetParameterType();
00957          param += str + " ";
00958          param += prp->GetDefaultValue()  +" ";
00959          param += prp->GetParameterRange() +" ";
00960          param += prp->GetParameterCandidates();
00961       }
00962      newTreeParams.push_back( param + "\n"); 
00963      newTreePCP.push_back( Comp ); 
00964    }
00965    if( treeEntry == 0 )  return;     // end recursion
00966    for( int i=0; i< treeEntry; i++) {
00967       t = tree->GetTree(i+1);
00968       GetNewTreeValues(t, level+1);
00969    }
00970 }
00971 //######################################################

Generated on Mon May 27 17:50:06 2013 for Geant4 by  doxygen 1.4.7