File: ddSelectKindFksDialog.cpp

package info (click to toggle)
pgadmin3 1.20.0~beta2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 73,704 kB
  • ctags: 18,591
  • sloc: cpp: 193,786; ansic: 18,736; sh: 5,154; pascal: 1,120; yacc: 927; makefile: 516; lex: 421; xml: 126; perl: 40
file content (316 lines) | stat: -rw-r--r-- 9,407 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// ddSelectKindFksDialog.cpp - Utility dialog class to allow user select destination of fk: automatically generated or existing column
//
//////////////////////////////////////////////////////////////////////////

#include "pgAdmin3.h"

// wxWindows headers
#include <wx/wx.h>
#include <wx/statline.h>
#include "wx/stattext.h"


// App headers
#include "dd/dditems/utilities/ddSelectKindFksDialog.h"
#include "dd/dditems/figures/ddTableFigure.h"
#include "hotdraw/main/hdDrawingView.h"
#include "hotdraw/main/hdDrawingEditor.h"


IMPLEMENT_CLASS( ddSelectKindFksDialog, wxDialog )

BEGIN_EVENT_TABLE( ddSelectKindFksDialog, wxDialog )
	EVT_BUTTON(wxID_OK, ddSelectKindFksDialog::OnOkButtonClicked)
	EVT_BUTTON(wxID_CANCEL, ddSelectKindFksDialog::OnCancelButtonClicked)
	EVT_CHOICE(DDSELECTKINDFK, ddSelectKindFksDialog::OnChoiceFkKind)
END_EVENT_TABLE()

ddSelectKindFksDialog::ddSelectKindFksDialog(	wxWindow *parent,
        ddRelationshipFigure *relation,
        wxWindowID id,
        const wxPoint &pos,
        const wxSize &size,
        long style
                                            )
{
	tablesRelation = relation;
	Init();
	Create(parent, id, pos, size, style);
}

ddSelectKindFksDialog::~ddSelectKindFksDialog()
{
	//before delete all items inside to free memory
	deleteColsControls();
	delete ok;
	delete line;
}

ddSelectKindFksDialog::ddSelectKindFksDialog()
{
	Init();
}

void ddSelectKindFksDialog::Init( )
{
	//	choices.clear(); and delete all items inside hash map

}

bool ddSelectKindFksDialog::Create(	wxWindow *parent,
                                    wxWindowID id,
                                    const wxPoint &pos = wxDefaultPosition,
                                    const wxSize &size = wxDefaultSize,
                                    long style = wxCAPTION )
{
	if (!wxDialog::Create( parent, id, wxT("Select Foreign Key Mapping for each Column"), pos, size, style ))
		return false;

	CreateControls();

	// This fits the dialog to the minimum size dictated by
	// the sizers
	GetSizer()->Fit(this);

	// This ensures that the dialog cannot be sized smaller
	// than the minimum size

	GetSizer()->SetSizeHints(this);

	// Centre the dialog on the parent or (if none) screen

	Centre();

	return true;
}

void ddSelectKindFksDialog::CreateControls()
{
	// A top-level sizer

	topSizer = new wxBoxSizer(wxVERTICAL );
	this->SetSizer(topSizer);

	// A wxChoice to choice kind of foreign key: from Uk or from pk
	wxArrayString kindFks;
	kindFks.Add(_("From Primary"));

	int i, last = tablesRelation->getStartTable()->getUkConstraintsNames().Count();
	wxString tmp;

	for(i = 0; i < last; i++)
	{
		tmp = _("Unique Key: ");
		tmp += tablesRelation->getStartTable()->getUkConstraintsNames()[i];
		kindFks.Add(tmp);
	}

	kindFkCtrl = new wxChoice(this, DDSELECTKINDFK, wxDefaultPosition, wxDefaultSize, kindFks);
	kindFkCtrl->SetStringSelection(_("From Primary"));
	topSizer->Add(kindFkCtrl, 0, wxALIGN_CENTER, 5);

	// A dividing line before the mapping items and kind of mapping
	line = new wxStaticLine ( this, wxID_STATIC,
	                          wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
	topSizer->Add(line, 0, wxGROW | wxALL, 5);


	//Hack to allow multiple pairs of wxStaticText wxchoice at dialog
	colsTopSizer = new wxBoxSizer(wxVERTICAL );
	topSizer->Add(colsTopSizer);
	populateColumnsControls(true, -1);

	// A dividing line before the OK and Cancel buttons

	line = new wxStaticLine ( this, wxID_STATIC,
	                          wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
	topSizer->Add(line, 0, wxGROW | wxALL, 5);

	// A horizontal box sizer to contain Reset, OK, Cancel and Help

	okCancelBox = new wxBoxSizer(wxHORIZONTAL);
	topSizer->Add(okCancelBox, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);

	// The OK button

	ok = new wxButton ( this, wxID_OK, wxT("&OK"),
	                    wxDefaultPosition, wxDefaultSize, 0 );
	okCancelBox->Add(ok, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);

	// The Cancel button

	cancel = new wxButton ( this, wxID_CANCEL, wxT("&Cancel"),
	                        wxDefaultPosition, wxDefaultSize, 0 );
	okCancelBox->Add(cancel, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
}

void ddSelectKindFksDialog::OnChoiceFkKind(wxCommandEvent &event)
{
	int selectedUk = kindFkCtrl->GetSelection() == 0 ? -1 : kindFkCtrl->GetSelection() - 1;
	bool frompk = kindFkCtrl->GetSelection() == 0;

	populateColumnsControls(frompk, selectedUk);
}

void ddSelectKindFksDialog::deleteColsControls()
{
	choicesControlsHashMap::iterator it;
	bool repeat;
	ddSelectFkKindLine *columnLine;
	do
	{
		repeat = false;
		for (it = choices.begin(); it != choices.end(); ++it)
		{
			wxString key = it->first;
			columnLine = it->second;
			choices.erase(it);
			delete columnLine;
			repeat = true;
			if (repeat)
				break;
		}
	}
	while(repeat);
	colsTopSizer->Clear();
	choices.clear();
}
void ddSelectKindFksDialog::populateColumnsControls(bool primaryKey, int useUkIndex)
{
	// Adding all columns mapping items
	ddTableFigure *source = (ddTableFigure *) tablesRelation->getStartFigure();
	ddTableFigure *destination = (ddTableFigure *) tablesRelation->getEndFigure();

	//Delete existing controllers
	deleteColsControls();

	//Populate controllers
	if(primaryKey)
	{
		wxArrayString sourceCols = source->getAllFkSourceColsNames(true);
		wxArrayString destCols = destination->getAllColumnsNames();
		destCols.Insert(_("Automatically Generated"), 0);
		int i, last = sourceCols.Count();
		int eventID;
		for(i = 0; i < last; i++)
		{
			// A sizer to allow lines of controls
			linesSizer = new wxBoxSizer(wxHORIZONTAL );
			colsTopSizer->Add(linesSizer, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
			//Create controls and put inside linesizer
			eventID =  DDCHOICESELECTBASE + i;
			ddSelectFkKindLine *newLineControls = new ddSelectFkKindLine(this, sourceCols[i], destCols, eventID);
			choices[ sourceCols[i] ] = newLineControls;
			linesSizer->Add(newLineControls->sourceCtrl, 0, wxALIGN_LEFT | wxALL, 5);
			linesSizer->Add(newLineControls->destinationCtrl, 0, wxGROW | wxALL, 5);
		}
	}
	else
	{
		wxArrayString sourceCols = source->getAllFkSourceColsNames(false, useUkIndex);
		wxArrayString destCols = destination->getAllColumnsNames();
		destCols.Insert(_("Automatically Generated"), 0);
		int i, last = sourceCols.Count();
		int eventID;
		for(i = 0; i < last; i++)
		{
			// A sizer to allow lines of controls
			linesSizer = new wxBoxSizer(wxHORIZONTAL );
			colsTopSizer->Add(linesSizer, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
			//Create controls and put inside linesizer
			eventID =  DDCHOICESELECTBASE + i;
			ddSelectFkKindLine *newLineControls = new ddSelectFkKindLine(this, sourceCols[i], destCols, eventID);
			choices[ sourceCols[i] ] = newLineControls;
			linesSizer->Add(newLineControls->sourceCtrl, 0, wxALIGN_LEFT | wxALL, 5);
			linesSizer->Add(newLineControls->destinationCtrl, 0, wxGROW | wxALL, 5);
		}
	}
	this->Layout();
	this->InvalidateBestSize();
	this->Fit();
	topSizer->RecalcSizes();
}

//Transfer data to the window
bool ddSelectKindFksDialog::TransferDataToWindow()
{
	return true;
}

//Transfer data from the window
bool ddSelectKindFksDialog::TransferDataFromWindow()
{
	return true;
}

void ddSelectKindFksDialog::OnEnterPressed( wxCommandEvent &event )
{
	if (event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER)
	{
		if ( Validate() && TransferDataFromWindow() )
		{
			if ( IsModal() )
				EndModal(wxID_OK); // If modal
			else
			{
				SetReturnCode(wxID_OK);
				this->Show(false); // If modeless
			}
		}

	}
}

void ddSelectKindFksDialog::OnCancelButtonClicked( wxCommandEvent &event )
{
	//Do nothing, just return wxID_CANCEL to don't allow connection
	event.Skip();
}

void ddSelectKindFksDialog::OnOkButtonClicked( wxCommandEvent &event )
{
	choicesControlsHashMap::iterator it;
	ddSelectFkKindLine *lineOfCtrls;

	int selectedUk = kindFkCtrl->GetSelection() == 0 ? -1 : kindFkCtrl->GetSelection() - 1;
	bool fromPk = kindFkCtrl->GetSelection() == 0;
	tablesRelation->setFkFrom(fromPk, selectedUk);  //true or bigger from zero both are mutually exclusive

	for( it = choices.begin(); it != choices.end(); ++it )
	{
		wxString key = it->first;
		lineOfCtrls = it->second;
		if(lineOfCtrls->destinationCtrl->GetSelection() != 0) //No automatic Generated
		{
			ddColumnFigure *col = tablesRelation->getStartTable()->getColumnByName(lineOfCtrls->sourceCtrl->GetLabel());
			tablesRelation->addExistingColumnFk(col, lineOfCtrls->destinationCtrl->GetString(lineOfCtrls->destinationCtrl->GetSelection()));
		}
	}
	event.Skip();
}

ddSelectFkKindLine::ddSelectFkKindLine(wxWindow *parent, wxString sourceColumn, wxArrayString possibleTargets, wxWindowID eventId)
{
	sourceCtrl = new wxStaticText(parent, wxID_STATIC, sourceColumn, wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT);
	destinationCtrl = new wxChoice(parent, eventId, wxDefaultPosition, wxDefaultSize, possibleTargets);
	destinationCtrl->SetStringSelection(_("Automatically Generated"));
}

ddSelectFkKindLine::ddSelectFkKindLine()
{
	sourceCtrl = NULL;
	destinationCtrl = NULL;
}

ddSelectFkKindLine::~ddSelectFkKindLine()
{
	delete sourceCtrl;
	delete destinationCtrl;
}