00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "umlentityattributedialog.h"
00014
00015
00016 #include <qlayout.h>
00017 #include <qlineedit.h>
00018 #include <qcheckbox.h>
00019 #include <qgroupbox.h>
00020 #include <qbuttongroup.h>
00021 #include <qradiobutton.h>
00022 #include <qlabel.h>
00023
00024
00025 #include <kcombobox.h>
00026 #include <kcompletion.h>
00027 #include <klocale.h>
00028 #include <kmessagebox.h>
00029 #include <kapplication.h>
00030 #include <kdebug.h>
00031
00032
00033 #include "../entityattribute.h"
00034 #include "../classifier.h"
00035 #include "../umldoc.h"
00036 #include "../uml.h"
00037 #include "../codegenerator.h"
00038 #include "../dialog_utils.h"
00039 #include "../object_factory.h"
00040 #include "../umlclassifierlist.h"
00041
00042 UMLEntityAttributeDialog::UMLEntityAttributeDialog( QWidget * pParent, UMLEntityAttribute * pEntityAttribute )
00043 : KDialogBase( Plain, i18n("Entity Attribute Properties"), Help | Ok | Cancel , Ok, pParent, "_UMLENTITYATTRIBUTEDLG_", true, true) {
00044 m_pEntityAttribute = pEntityAttribute;
00045 setupDialog();
00046 }
00047
00048 UMLEntityAttributeDialog::~UMLEntityAttributeDialog() {}
00049
00050 void UMLEntityAttributeDialog::setupDialog() {
00051 UMLDoc * pDoc = UMLApp::app()->getDocument();
00052 int margin = fontMetrics().height();
00053
00054 QVBoxLayout * mainLayout = new QVBoxLayout( plainPage() );
00055
00056 m_pValuesGB = new QGroupBox(i18n("General Properties"), plainPage() );
00057 QGridLayout * valuesLayout = new QGridLayout(m_pValuesGB, 5, 2);
00058 valuesLayout -> setMargin(margin);
00059 valuesLayout -> setSpacing(10);
00060
00061 m_pTypeL = new QLabel(i18n("&Type:"), m_pValuesGB);
00062 valuesLayout -> addWidget(m_pTypeL, 0, 0);
00063
00064 m_pTypeCB = new KComboBox(true, m_pValuesGB);
00065 valuesLayout -> addWidget(m_pTypeCB, 0, 1);
00066 m_pTypeL->setBuddy(m_pTypeCB);
00067
00068 Dialog_Utils::makeLabeledEditField( m_pValuesGB, valuesLayout, 1,
00069 m_pNameL, i18n("&Name:"),
00070 m_pNameLE, m_pEntityAttribute->getName() );
00071
00072 Dialog_Utils::makeLabeledEditField( m_pValuesGB, valuesLayout, 2,
00073 m_pInitialL, i18n("&Default value:"),
00074 m_pInitialLE, m_pEntityAttribute->getInitialValue() );
00075
00076 Dialog_Utils::makeLabeledEditField( m_pValuesGB, valuesLayout, 3,
00077 m_pStereoTypeL, i18n("Stereotype name:"),
00078 m_pStereoTypeLE, m_pEntityAttribute->getStereotype() );
00079
00080 Dialog_Utils::makeLabeledEditField( m_pValuesGB, valuesLayout, 4,
00081 m_pValuesL, i18n("Length/Values:"),
00082 m_pValuesLE, m_pEntityAttribute->getValues() );
00083
00084 m_pAutoIncrementCB = new QCheckBox( i18n("&Auto increment"), m_pValuesGB );
00085 m_pAutoIncrementCB->setChecked( m_pEntityAttribute->getAutoIncrement() );
00086 valuesLayout->addWidget(m_pAutoIncrementCB, 5, 0);
00087
00088 m_pNullCB = new QCheckBox( i18n("Allow &null"), m_pValuesGB );
00089 m_pNullCB->setChecked( m_pEntityAttribute->getNull() );
00090 valuesLayout->addWidget(m_pNullCB, 6, 0);
00091
00092 m_pAttributesL = new QLabel(i18n("Attributes:"), m_pValuesGB);
00093 valuesLayout->addWidget(m_pAttributesL, 7, 0);
00094
00095 m_pAttributesCB = new KComboBox(true, m_pValuesGB);
00096 m_pAttributesCB->setCompletionMode( KGlobalSettings::CompletionPopup );
00097 valuesLayout->addWidget(m_pAttributesCB, 7, 1);
00098 m_pTypeL->setBuddy(m_pAttributesCB);
00099
00100 insertAttribute( m_pEntityAttribute->getAttributes() );
00101 insertAttribute("");
00102 insertAttribute("binary");
00103 insertAttribute("unsigned");
00104 insertAttribute("unsigned zerofill");
00105
00106 mainLayout -> addWidget(m_pValuesGB);
00107
00108 m_pScopeBG = new QButtonGroup(i18n("Indexing"), plainPage() );
00109 QHBoxLayout* scopeLayout = new QHBoxLayout(m_pScopeBG);
00110 scopeLayout->setMargin(margin);
00111
00112 m_pNoneRB = new QRadioButton(i18n("&None"), m_pScopeBG);
00113 scopeLayout->addWidget(m_pNoneRB);
00114
00115 m_pPublicRB = new QRadioButton(i18n("&Primary"), m_pScopeBG);
00116 scopeLayout->addWidget(m_pPublicRB);
00117
00118 m_pPrivateRB = new QRadioButton(i18n("&Index"), m_pScopeBG);
00119 scopeLayout->addWidget(m_pPrivateRB);
00120
00121 m_pProtectedRB = new QRadioButton(i18n("&Unique"), m_pScopeBG);
00122 scopeLayout->addWidget(m_pProtectedRB);
00123
00124 mainLayout->addWidget(m_pScopeBG);
00125 Uml::DBIndex_Type scope = m_pEntityAttribute->getIndexType();
00126 if ( scope == Uml::Primary )
00127 m_pPublicRB->setChecked( true );
00128 else if( scope == Uml::Index )
00129 m_pPrivateRB -> setChecked( true );
00130 else if( scope == Uml::Unique )
00131 m_pProtectedRB -> setChecked( true );
00132 else {
00133 m_pNoneRB->setChecked(true);
00134 }
00135
00136 m_pTypeCB->setDuplicatesEnabled(false);
00137 m_pTypeCB->setCompletionMode( KGlobalSettings::CompletionPopup );
00138
00139
00140 UMLClassifierList dataTypes = pDoc->getDatatypes();
00141 if (dataTypes.count() == 0) {
00142
00143 UMLApp::app()->setActiveLanguage("SQL");
00144 pDoc->addDefaultDatatypes();
00145 kapp->processEvents();
00146 dataTypes = pDoc->getDatatypes();
00147 }
00148 UMLClassifier *dat;
00149 for (UMLClassifierListIt dit(dataTypes); (dat = dit.current()) != NULL; ++dit) {
00150 insertType(dat->getName());
00151 }
00152
00153
00154 int typeBoxCount = 0;
00155 bool foundType = false;
00156 while (typeBoxCount < m_pTypeCB->count() && foundType == false) {
00157 QString typeBoxString = m_pTypeCB->text(typeBoxCount);
00158 if ( typeBoxString == m_pEntityAttribute->getTypeName() ) {
00159 foundType = true;
00160 m_pTypeCB->setCurrentItem(typeBoxCount);
00161 } else {
00162 typeBoxCount++;
00163 }
00164 }
00165
00166 if (!foundType) {
00167 insertType( m_pEntityAttribute->getTypeName(), 0 );
00168 m_pTypeCB->setCurrentItem(0);
00169 }
00170
00171 m_pNameLE->setFocus();
00172 connect( m_pNameLE, SIGNAL( textChanged ( const QString & ) ), SLOT( slotNameChanged( const QString & ) ) );
00173 slotNameChanged(m_pNameLE->text() );
00174 }
00175
00176 void UMLEntityAttributeDialog::slotNameChanged( const QString &_text )
00177 {
00178 enableButtonOK( !_text.isEmpty() );
00179 }
00180
00181 bool UMLEntityAttributeDialog::apply() {
00182 QString name = m_pNameLE->text();
00183 if (name.isEmpty()) {
00184 KMessageBox::error(this, i18n("You have entered an invalid entity attribute name."),
00185 i18n("Entity Attribute Name Invalid"), false);
00186 m_pNameLE->setText( m_pEntityAttribute->getName() );
00187 return false;
00188 }
00189 UMLClassifier * pConcept = dynamic_cast<UMLClassifier *>( m_pEntityAttribute->parent() );
00190 UMLObject *o = pConcept->findChildObject(name);
00191 if (o && o != m_pEntityAttribute) {
00192 KMessageBox::error(this, i18n("The entity attribute name you have chosen is already being used in this operation."),
00193 i18n("Entity Attribute Name Not Unique"), false);
00194 m_pNameLE->setText( m_pEntityAttribute->getName() );
00195 return false;
00196 }
00197 m_pEntityAttribute->setName(name);
00198 m_pEntityAttribute->setInitialValue( m_pInitialLE->text() );
00199 m_pEntityAttribute->setStereotype( m_pStereoTypeLE->text() );
00200 m_pEntityAttribute->setValues( m_pValuesLE->text() );
00201 m_pEntityAttribute->setAttributes( m_pAttributesCB->currentText() );
00202 m_pEntityAttribute->setAutoIncrement( m_pAutoIncrementCB->isChecked() );
00203 m_pEntityAttribute->setNull( m_pNullCB->isChecked() );
00204
00205 if ( m_pPublicRB->isChecked() ) {
00206 m_pEntityAttribute->setIndexType(Uml::Primary);
00207 } else if ( m_pPrivateRB -> isChecked() ) {
00208 m_pEntityAttribute->setIndexType(Uml::Index);
00209 } else if ( m_pProtectedRB -> isChecked() ) {
00210 m_pEntityAttribute->setIndexType(Uml::Unique);
00211 } else {
00212 m_pEntityAttribute->setIndexType(Uml::None);
00213 }
00214
00215 QString typeName = m_pTypeCB->currentText();
00216 UMLDoc *pDoc = UMLApp::app()->getDocument();
00217 UMLClassifierList dataTypes = pDoc->getDatatypes();
00218 UMLClassifier *dat;
00219 for (UMLClassifierListIt dit(dataTypes); (dat = dit.current()) != NULL; ++dit) {
00220 if (typeName == dat->getName()) {
00221 m_pEntityAttribute->setType(dat);
00222 return true;
00223 }
00224 }
00225 UMLObject *obj = pDoc->findUMLObject(typeName);
00226 UMLClassifier *classifier = dynamic_cast<UMLClassifier*>(obj);
00227 if (classifier == NULL) {
00228
00229
00230 Uml::Object_Type ot = (typeName.contains('*') ? Uml::ot_Datatype
00231 : Uml::ot_Class);
00232 obj = Object_Factory::createUMLObject(ot, typeName);
00233 if (obj == NULL)
00234 return false;
00235 classifier = static_cast<UMLClassifier*>(obj);
00236 }
00237 m_pEntityAttribute->setType( classifier );
00238 return true;
00239 }
00240
00241 void UMLEntityAttributeDialog::slotApply() {
00242 apply();
00243 }
00244
00245 void UMLEntityAttributeDialog::slotOk() {
00246 if ( apply() ) {
00247 accept();
00248 }
00249 }
00250
00251 void UMLEntityAttributeDialog::insertType( const QString& type, int index ) {
00252 m_pTypeCB->insertItem( type, index );
00253 m_pTypeCB->completionObject()->addItem( type );
00254 }
00255
00256 void UMLEntityAttributeDialog::insertAttribute( const QString& type, int index ) {
00257 m_pAttributesCB->insertItem( type, index );
00258 m_pAttributesCB->completionObject()->addItem( type );
00259 }
00260
00261
00262 #include "umlentityattributedialog.moc"