ij-16.png   inspectorJ -- JavaTM Profiler
sf project site browse source checkout source
SourceForge.net Logo



src/inspectorj/client/sessionprofiledialog.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   inspectorJ - java profiler                                            *
00003  *   Copyright (C) 2007 by James May
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00027 #include "inspectorj/client/sessionprofiledialog.h"
00028 
00029 namespace inspectorj {
00030 namespace client {
00031     
00032 using inspectorj::toolset::ProfileToolSet;    
00033 
00040 SessionProfileDialog::SessionProfileDialog(QMap<QString, SessionProfile> &profs, QString profileName)
00041     :  profiles(profs)
00042 {
00043     SessionProfileDlg::setupUi(this);
00044     
00045     // initialize the wizard control
00046     pages.append(page1);
00047     pages.append(page2);
00048     wizard = new WizardControl(&pages, backBtn, nextBtn);
00049 
00050     addClassFilterBtn->setEnabled(false);
00051     removeClassFilterBtn->setEnabled(false);
00052     addClassPathBtn->setEnabled(false);
00053     removeClassPathBtn->setEnabled(false);    
00054     profile.setName(profileName);
00055     initDialog();
00056     updateNavigationBtns();
00057     
00058     connect(addClassFilterBtn, SIGNAL (clicked()), this, SLOT (addFilter()));
00059     connect(addClassPathBtn, SIGNAL (clicked()), this, SLOT (addClassPath()));
00060     connect(classFilterEdit, SIGNAL (textChanged(QString)), this, SLOT (updateAddBtn(QString)));
00061     connect(classPathEdit, SIGNAL (textChanged(QString)), this, SLOT (updateAddClassPathBtn(QString)));    
00062     connect(filterList, SIGNAL(itemSelectionChanged()),this, SLOT (updateRemoveBtn()));
00063     connect(classPathList, SIGNAL(itemSelectionChanged()),this, SLOT (updateRemoveClassPathBtn()));
00064     connect(removeClassFilterBtn, SIGNAL (clicked()), this, SLOT (removeFilter()));
00065     connect(removeClassPathBtn, SIGNAL (clicked()), this, SLOT (removeClassPath()));
00066     connect(finishBtn, SIGNAL(clicked()), this, SLOT(saveProfile()));   
00067     connect(wizard, SIGNAL(pageChanged(int)), this, SLOT(updateNavigationBtns()));
00068     connect(launchExternalRadBtn, SIGNAL(toggled(bool)), this, SLOT(updateNavigationBtns()));
00069     connect(mainClassRadBtn, SIGNAL(toggled(bool)), this, SLOT(updateNavigationBtns()));
00070     connect(jarFileRadBtn, SIGNAL(toggled(bool)), this, SLOT(updateNavigationBtns()));
00071     connect(tomcatRadBtn, SIGNAL(toggled(bool)), this, SLOT(updateNavigationBtns()));
00072     connect(hostNameEdit, SIGNAL(textChanged(QString)), this, SLOT(updateNavigationBtns()));
00073     connect(hostPortEdit, SIGNAL(textChanged(QString)), this, SLOT(updateNavigationBtns()));
00074     connect(jreHomeEdit, SIGNAL(textChanged(QString)), this, SLOT(updateNavigationBtns()));
00075     connect(mainClassEdit, SIGNAL(textChanged(QString)), this, SLOT(updateNavigationBtns()));
00076     connect(jarFileEdit, SIGNAL(textChanged(QString)), this, SLOT(updateNavigationBtns()));
00077     connect(profileNameEdit, SIGNAL(textChanged(QString)), this, SLOT(updateNavigationBtns()));
00078     connect(jreHomeBrowseBtn, SIGNAL(clicked()), this, SLOT(browseJreHome()));
00079     connect(tomcatBrowseBtn, SIGNAL(clicked()), this, SLOT(browseTomcatHome()));
00080     connect(browseClassPathBtn, SIGNAL(clicked()), this, SLOT(browseClassPath()));
00081     
00082     // Create a regExp validator for the hostPortEdit lineEdit box. This will
00083     // allow only numeric data to be entered in the hostPortEdit lineEdit.
00084     QRegExp regExp("[0-9]{1,6}");
00085     hostPortEdit->setValidator(new QRegExpValidator(regExp, this));    
00086 }
00087 
00088 
00092 SessionProfileDialog::~SessionProfileDialog()
00093 {
00094     if (wizard) {
00095         delete wizard;
00096         wizard = 0;
00097     }
00098 }
00099 
00104 void SessionProfileDialog::initDialog()
00105 {
00106     if (!profile.getName().isEmpty()) {
00107         profile = profiles.value(profile.getName());
00108         profileNameEdit->setText(profile.getName());
00109         QStringList filters = profile.getClassFilters();
00110         QStringListIterator iter(filters);
00111         while (iter.hasNext()) {
00112             filterList->addItem(iter.next());
00113         }
00114         
00115         QStringList classPaths = profile.getClassPath();
00116         QStringListIterator cpIter(classPaths);
00117         while (cpIter.hasNext()) {
00118             classPathList->addItem(cpIter.next());
00119         }                
00120         
00121         javaOptionsEdit->setText(profile.getJavaOptions());
00122         applicationArgsEdit->setText(profile.getApplicationArgs());
00123         
00124         if (profile.isExternalLaunch()) {
00125             launchExternalRadBtn->click();
00126         } else {
00127             launchInternalRadBtn->click();
00128         }
00129         
00130         ijConnection conn = profile.getConnection();
00131         hostNameEdit->insert(conn.host);
00132         hostPortEdit->insert(conn.port);
00133         
00134         if (profile.isJarFile()) {
00135             jarFileRadBtn->click();
00136             jarFileEdit->setText(profile.getApplicationEntry());
00137         } else if (profile.isTomcatApp()) {
00138             tomcatRadBtn->click();
00139             tomcatEdit->setText(profile.getApplicationEntry());
00140         } else {
00141             mainClassRadBtn->click();
00142             mainClassEdit->setText(profile.getApplicationEntry());
00143         }
00144         
00145     } else {
00146           profileNameEdit->clear();
00147           filterList->clear();
00148     }
00149     
00150     // set the jreHomeEdit field if it exists in the profile
00151     // otherwise try to default to the JAVA_HOME env variable
00152     if (!profile.getJreHome().isEmpty()) {
00153         jreHomeEdit->setText(profile.getJreHome());
00154     } else {
00155         char *jreHome = getenv("JAVA_HOME");
00156         if (jreHome) {
00157             jreHomeEdit->setText(QString(jreHome));
00158         }            
00159     }
00160           
00161     // set the tomcatHomeEdit field if it exists in the profile
00162     // otherwise try to default to the CATALINA_HOME env variable
00163     if (!tomcatRadBtn->isChecked()) {
00164         char *tomcatHome = getenv("CATALINA_HOME");
00165         if (tomcatHome) {
00166             tomcatEdit->setText(QString(tomcatHome));
00167         }            
00168     }    
00169 }  
00170 
00174 void SessionProfileDialog::updateNavigationBtns()
00175 {
00176     finishBtn->setEnabled(false);
00177     int i = wizard->getCurrentPageNum();
00178     bool enabled = true;
00179     
00180     if (i == wizard->getNumPages() || launchInternalRadBtn->isChecked()) {                
00181         for (int k = 1; k <= wizard->getNumPages(); k++) {
00182             enabled = enabled && isPageValid(k);
00183         }
00184         finishBtn->setEnabled(enabled);
00185     }
00186     
00187     if (i < wizard->getNumPages()) {
00188         enabled = isPageValid(i);
00189         nextBtn->setEnabled(enabled);
00190         if (launchExternalRadBtn->isChecked()) {
00191             finishBtn->setEnabled(enabled);
00192         }
00193     }
00194 }
00195 
00200 bool SessionProfileDialog::isPageValid(int pageNum)
00201 {
00202     bool valid = false;
00203     bool hasJreHome = false;
00204     bool hasAppEntry = false;
00205     switch (pageNum)
00206     {
00207         case 1: valid = (!hostNameEdit->text().isEmpty()
00208                     && !hostPortEdit->text().isEmpty()
00209                     && !profileNameEdit->text().isEmpty());
00210                 return valid;
00211                 
00212         case 2: hasJreHome = (!jreHomeEdit->text().isEmpty());
00213                 hasAppEntry = mainClassRadBtn->isChecked() && !mainClassEdit->text().isEmpty()
00214                         || jarFileRadBtn->isChecked() && !jarFileEdit->text().isEmpty()
00215                         || tomcatRadBtn->isChecked() && !tomcatEdit->text().isEmpty();
00216                 valid = hasJreHome && hasAppEntry;
00217                 return valid;
00218     }
00219 }
00220 
00224 void SessionProfileDialog::browseJreHome()
00225 {
00226     QFileDialog fileDialog(this);
00227     fileDialog.setFileMode(QFileDialog::DirectoryOnly);
00228     QStringList fileNames;
00229     if (fileDialog.exec()) {
00230         fileNames = fileDialog.selectedFiles();
00231         jreHomeEdit->setText(fileNames.at(0));
00232     }
00233 }
00234 
00238 void SessionProfileDialog::browseTomcatHome()
00239 {
00240     QFileDialog fileDialog(this);
00241     fileDialog.setFileMode(QFileDialog::DirectoryOnly);
00242     QStringList fileNames;
00243     if (fileDialog.exec()) {
00244         fileNames = fileDialog.selectedFiles();
00245         tomcatEdit->setText(fileNames.at(0));
00246     }
00247 }
00248 
00252 void SessionProfileDialog::browseClassPath()
00253 {
00254     QFileDialog fileDialog(this);
00255     fileDialog.setFileMode(QFileDialog::ExistingFiles);
00256     fileDialog.setFilter(tr("classpath archives (*.jar *.zip)"));
00257     QStringList fileNames;
00258     if (fileDialog.exec()) {
00259         fileNames = fileDialog.selectedFiles();
00260         for (int i = 0; i < fileNames.size(); i++) {
00261             classPathList->addItem(fileNames.at(i));               
00262         }
00263     }
00264 }
00265 
00266 void SessionProfileDialog::setProfileDir(QString dir)
00267 {
00268     profileDir = dir;
00269 }
00270 
00275 void SessionProfileDialog::saveProfile()
00276 {
00277     profiles.remove(profile.getName());
00278     profile.setName(profileNameEdit->text());  
00279     if (profile.getFileName().isEmpty()) {
00280         profile.setFileName(profile.getName().append(".xml")); 
00281     }   
00282 
00283     QStringList filters;
00284     for (int i = 0; i < filterList->count(); i++) {
00285         QListWidgetItem* item = filterList->item(i);
00286         if (item) {
00287             filters << item->text();
00288         }
00289     }
00290     profile.setClassFilters(filters);
00291     
00292     QStringList classPaths;
00293     for (int i = 0; i < classPathList->count(); i++) {
00294         QListWidgetItem* item = classPathList->item(i);
00295         if (item) {
00296             classPaths << item->text();
00297         }
00298     }
00299     profile.setClassPath(classPaths);    
00300     
00301     ijConnection conn;
00302     conn.host = hostNameEdit->text();
00303     conn.port = hostPortEdit->text();    
00304     profile.setConnection(conn);
00305     
00306     profile.setExternalLaunch(launchExternalRadBtn->isChecked());
00307     profile.setJreHome(jreHomeEdit->text());
00308     
00309     profile.setJarFile(jarFileRadBtn->isChecked());
00310     profile.setTomcatApp(tomcatRadBtn->isChecked());
00311     
00312     QString appEntry;
00313     if (jarFileRadBtn->isChecked()) {
00314         appEntry = jarFileEdit->text();
00315     } else if (mainClassRadBtn->isChecked()) {
00316         appEntry = mainClassEdit->text();
00317     } else if (tomcatRadBtn->isChecked()) {
00318         appEntry = tomcatEdit->text();
00319     }
00320     profile.setApplicationEntry(appEntry);
00321     
00322     profile.setJavaOptions(javaOptionsEdit->text());
00323     profile.setApplicationArgs(applicationArgsEdit->text());
00324     
00325     profiles.insert(profile.getName(), profile);
00326     
00327     ProfileToolSet::saveProfile(profile, profileDir);
00328     this->accept();
00329 }
00330 
00334 void SessionProfileDialog::addFilter()
00335 {
00336     filterList->addItem(classFilterEdit->text());
00337     classFilterEdit->clear();
00338 }
00339 
00343 void SessionProfileDialog::addClassPath()
00344 {
00345     classPathList->addItem(classPathEdit->text());
00346     classPathEdit->clear();
00347 }
00348             
00354 void SessionProfileDialog::updateAddBtn(QString str)
00355 {
00356     addClassFilterBtn->setEnabled(!str.isEmpty());
00357 }
00358 
00364 void SessionProfileDialog::updateAddClassPathBtn(QString str)
00365 {
00366     addClassPathBtn->setEnabled(!str.isEmpty());
00367 }
00368 
00373 void SessionProfileDialog::updateRemoveBtn()
00374 {
00375     QList<QListWidgetItem *> itemList = filterList->selectedItems();  
00376     bool enable = itemList.size() > 0;
00377     removeClassFilterBtn->setEnabled(enable);
00378 }
00379 
00384 void SessionProfileDialog::updateRemoveClassPathBtn()
00385 {
00386     QList<QListWidgetItem *> itemList = classPathList->selectedItems();  
00387     bool enable = itemList.size() > 0;
00388     removeClassPathBtn->setEnabled(enable);
00389 }
00390 
00394 void SessionProfileDialog::removeFilter()
00395 {
00396     QList<QListWidgetItem *> itemList = filterList->selectedItems(); 
00397     QList<QListWidgetItem *>::iterator iter;
00398     for (iter = itemList.begin(); iter != itemList.end(); ++iter) {        
00399         filterList->takeItem(filterList->row(*iter));
00400     }
00401 }
00402 
00406 void SessionProfileDialog::removeClassPath()
00407 {
00408     QList<QListWidgetItem *> itemList = classPathList->selectedItems(); 
00409     QList<QListWidgetItem *>::iterator iter;
00410     for (iter = itemList.begin(); iter != itemList.end(); ++iter) {        
00411         classPathList->takeItem(classPathList->row(*iter));
00412     }
00413 }
00414 
00415 } // end namespace client
00416 } // end namespace inspectorj

Generated on Sun Aug 19 17:07:53 2007 for inspectorJ by  doxygen 1.5.1