/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/dbdialog.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/Thuban/UI/dbdialog.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1630 - (hide annotations)
Fri Aug 22 15:59:57 2003 UTC (21 years, 6 months ago) by bh
Original Path: trunk/thuban/Thuban/UI/dbdialog.py
File MIME type: text/x-python
File size: 13832 byte(s)
(DBPwdDlg): Removed because it's not used.
(DBFrame.OnEdit): Removed because it's not used and wouldn't work
at the moment. The functionality should probably be implemented
some time, though.

1 bh 1620 # Copyright (c) 2001, 2003 by Intevation GmbH
2     # Authors:
3     # Martin Mueller <[email protected]>
4     # Bernhard Herzog <[email protected]>
5     #
6     # This program is free software under the GPL (>=v2)
7     # Read the file COPYING coming with Thuban for details.
8    
9    
10     """Dialogs to manage database connections"""
11    
12     import sys, traceback
13    
14     from wxPython.wx import *
15    
16 bh 1625 try:
17     import psycopg
18     except ImportError:
19     psycopg = None
20 bh 1620
21     from Thuban import _
22     from Thuban.UI.dialogs import NonModalDialog
23     from Thuban.Model.postgisdb import PostGISConnection
24     from Thuban.Model.messages import DBCONN_ADDED, DBCONN_REMOVED
25     from messages import SESSION_REPLACED
26    
27     ID_DBADD_OK = 9001
28     ID_DBADD_CANCEL = 9002
29    
30     ID_DB_ADD = 9101
31     ID_DB_REMOVE = 9102
32    
33     ID_DBCHOOSE_RETRIEVE = 9201
34     ID_DBCHOOSE_OK = 9202
35     ID_DBCHOOSE_CANCEL = 9203
36     ID_LB_DCLICK = 9204
37    
38    
39     class ChooseDBTableDialog(wxDialog):
40    
41     def __init__(self, session, *args, **kwds):
42     kwds["style"] = wxDIALOG_MODAL|wxCAPTION
43     wxDialog.__init__(self, *args, **kwds)
44     self.session = session
45     self.dbconns = self.session.DBConnections()
46     self.tables = []
47     self.list_box_4 = wxListBox(self, -1)
48     for i in range(len(self.dbconns)):
49     self.list_box_4.Append(self.dbconns[i].BriefDescription())
50     self.DB_CHOOSE_RETRIEVE = wxButton(self, ID_DBCHOOSE_RETRIEVE,
51     _("Retrieve"))
52     self.list_box_5 = wxListBox(self, ID_LB_DCLICK)
53     self.DB_CHOOSE_OK = wxButton(self, ID_DBCHOOSE_OK, _("OK"))
54     self.DB_CHOOSE_CANCEL = wxButton(self, ID_DBCHOOSE_CANCEL, _("Cancel"))
55     self.__set_properties()
56     self.__do_layout()
57    
58     EVT_BUTTON(self, ID_DBCHOOSE_OK, self.OnOK)
59     EVT_BUTTON(self, ID_DBCHOOSE_CANCEL, self.OnCancel)
60     EVT_BUTTON(self, ID_DBCHOOSE_RETRIEVE, self.OnRetrieve)
61     EVT_LISTBOX_DCLICK(self, ID_LB_DCLICK, self.OnLBDClick)
62    
63    
64     def __set_properties(self):
65     self.SetTitle(_("Choose layer from database"))
66     self.list_box_4.SetSelection(0)
67     self.list_box_5.SetSelection(0)
68    
69     def __do_layout(self):
70     grid_sizer_1 = wxFlexGridSizer(2, 1, 0, 0)
71     grid_sizer_3 = wxFlexGridSizer(1, 2, 0, 0)
72     grid_sizer_2 = wxFlexGridSizer(1, 3, 0, 0)
73     sizer_4 = wxStaticBoxSizer(wxStaticBox(self, -1, _("Tables")),
74     wxHORIZONTAL)
75     grid_sizer_4 = wxFlexGridSizer(3, 1, 0, 0)
76     sizer_3 = wxStaticBoxSizer(wxStaticBox(self, -1, _("Databases")),
77     wxHORIZONTAL)
78     sizer_3.Add(self.list_box_4, 0, wxEXPAND, 0)
79     grid_sizer_2.Add(sizer_3, 1, wxEXPAND, 0)
80     grid_sizer_4.Add(20, 80, 0, wxEXPAND, 0)
81     grid_sizer_4.Add(self.DB_CHOOSE_RETRIEVE, 0, wxALL
82     |wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4)
83     grid_sizer_4.Add(20, 80, 0, wxEXPAND, 0)
84     grid_sizer_2.Add(grid_sizer_4, 1, wxEXPAND, 0)
85     sizer_4.Add(self.list_box_5, 0, wxEXPAND, 0)
86     grid_sizer_2.Add(sizer_4, 1, wxEXPAND, 0)
87     grid_sizer_1.Add(grid_sizer_2, 1, wxEXPAND, 0)
88     grid_sizer_3.Add(self.DB_CHOOSE_OK, 0, wxALL|wxALIGN_RIGHT, 4)
89     grid_sizer_3.Add(self.DB_CHOOSE_CANCEL, 0, wxALL, 4)
90     grid_sizer_1.Add(grid_sizer_3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL, 4)
91     self.SetAutoLayout(1)
92     self.SetSizer(grid_sizer_1)
93     grid_sizer_1.Fit(self)
94     grid_sizer_1.SetSizeHints(self)
95     self.Layout()
96    
97     def GetTable(self):
98     i = self.list_box_5.GetSelection()
99     if i >= 0:
100     return self.selected_conn, self.tables[i]
101     return None
102    
103     def OnRetrieve(self, event):
104     i = self.list_box_4.GetSelection()
105     if i >= 0:
106     self.selected_conn = self.dbconns[i]
107     self.tables = self.selected_conn.GeometryTables()
108     self.list_box_5.Set(self.tables)
109    
110     def OnLBDClick(self, event):
111     if self.list_box_5.GetSelection() >= 0:
112     self.EndModal(wxID_OK)
113     self.Show(false)
114    
115     def OnOK(self, event):
116     self.EndModal(wxID_OK)
117     self.Show(false)
118    
119     def OnCancel(self, event):
120     self.EndModal(wxID_CANCEL)
121     self.Show(false)
122    
123    
124     class DBDialog(wxDialog):
125    
126     """Dialog for the parameters of a database connection"""
127    
128     def __init__(self, selection=None, *args, **kwds):
129     kwds["style"] = wxDEFAULT_DIALOG_STYLE
130     wxDialog.__init__(self, *args, **kwds)
131     self.DBAdd_Label_Host = wxStaticText(self, -1, _("Hostname:"))
132     self.DBAdd_Host = wxTextCtrl(self, -1, "")
133     self.DBAdd_Label_Port = wxStaticText(self, -1, _("Port:"))
134     self.DBAdd_Port = wxTextCtrl(self, -1, "")
135     self.DBAdd_Label_Database = wxStaticText(self, -1, _("Database:"))
136     self.DBAdd_Database = wxTextCtrl(self, -1, "")
137     self.DBAdd_Label_User = wxStaticText(self, -1, _("User:"))
138     self.DBAdd_User = wxTextCtrl(self, -1, "")
139     self.DBAdd_Label_Pwd = wxStaticText(self, -1, _("Password:"))
140     self.DBAdd_Pwd = wxTextCtrl(self, -1, "", style=wxTE_PASSWORD)
141     self.DBAdd_OK = wxButton(self, ID_DBADD_OK, _("OK"))
142     self.DBAdd_Cancel = wxButton(self, ID_DBADD_CANCEL, _("Cancel"))
143     if selection != None:
144     self.DBAdd_Host.SetEditable(false)
145     self.DBAdd_Host.SetValue(selection.host)
146     self.DBAdd_Port.SetValue(selection.port)
147     self.DBAdd_Database.SetEditable(false)
148     self.DBAdd_Database.SetValue(selection.dbname())
149     self.DBAdd_User.SetValue(selection.user)
150     self.__set_properties()
151     self.__do_layout()
152     EVT_BUTTON(self, ID_DBADD_OK, self.OnOK)
153     EVT_BUTTON(self, ID_DBADD_CANCEL, self.OnCancel)
154    
155     def __set_properties(self):
156     self.SetTitle(_("Add database"))
157     self.SetSize((480, 185))
158     self.DBAdd_Label_Host.SetSize((60, 16))
159     self.DBAdd_Host.SetSize((200, 22))
160     self.DBAdd_Label_Database.SetSize((60, 16))
161     self.DBAdd_Label_User.SetSize((60, 16))
162    
163     def __do_layout(self):
164     grid_sizer_1 = wxFlexGridSizer(4, 1, 0, 0)
165     grid_sizer_2 = wxGridSizer(1, 2, 0, 0)
166     grid_sizer_5 = wxFlexGridSizer(1, 5, 0, 0)
167     grid_sizer_6 = wxFlexGridSizer(1, 5, 0, 0)
168     grid_sizer_3 = wxFlexGridSizer(1, 5, 0, 0)
169     grid_sizer_3.Add(self.DBAdd_Label_Host, 0,
170     wxALL|wxALIGN_CENTER_VERTICAL, 4)
171     grid_sizer_3.Add(self.DBAdd_Host, 0, wxALL, 4)
172     grid_sizer_3.Add(20, 20, 0, wxALL|wxEXPAND, 4)
173     grid_sizer_3.Add(self.DBAdd_Label_Port, 0,
174     wxALL|wxALIGN_CENTER_VERTICAL, 4)
175     grid_sizer_3.Add(self.DBAdd_Port, 0, wxALL, 4)
176     grid_sizer_1.Add(grid_sizer_3, 1, wxALL|wxEXPAND, 4)
177     grid_sizer_6.Add(self.DBAdd_Label_Database, 0,
178     wxALL|wxALIGN_CENTER_VERTICAL, 4)
179     grid_sizer_6.Add(self.DBAdd_Database, 0, wxALL, 4)
180     grid_sizer_6.Add(20, 20, 0, wxALL|wxEXPAND, 4)
181     grid_sizer_1.Add(grid_sizer_6, 1, wxALL|wxEXPAND, 4)
182     grid_sizer_5.Add(self.DBAdd_Label_User, 0,
183     wxALL|wxALIGN_CENTER_VERTICAL, 4)
184     grid_sizer_5.Add(self.DBAdd_User, 0, wxALL|wxALIGN_CENTER_VERTICAL, 4)
185     grid_sizer_5.Add(20, 20, 0, wxALL|wxEXPAND, 4)
186     grid_sizer_5.Add(self.DBAdd_Label_Pwd, 0,
187     wxALL|wxALIGN_CENTER_VERTICAL, 4)
188     grid_sizer_5.Add(self.DBAdd_Pwd, 0, wxALL|wxALIGN_CENTER_VERTICAL, 4)
189     grid_sizer_1.Add(grid_sizer_5, 1, wxALL|wxEXPAND, 4)
190     grid_sizer_2.Add(self.DBAdd_OK, 0,
191     wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 4)
192     grid_sizer_2.Add(self.DBAdd_Cancel, 0, wxALL|wxALIGN_CENTER_VERTICAL,
193     4)
194     grid_sizer_1.Add(grid_sizer_2, 1, wxALL|wxEXPAND, 4)
195     self.SetAutoLayout(1)
196     self.SetSizer(grid_sizer_1)
197     self.Layout()
198    
199    
200     def GetHostname(self):
201     return self.DBAdd_Host.GetValue()
202    
203     def GetPort(self):
204     return self.DBAdd_Port.GetValue()
205    
206     def GetDatabase(self):
207     return self.DBAdd_Database.GetValue()
208    
209     def GetUser(self):
210     return self.DBAdd_User.GetValue()
211    
212     def GetPassword(self):
213     return self.DBAdd_Pwd.GetValue()
214    
215     def OnOK(self, event):
216     self.EndModal(wxID_OK)
217     self.Show(false)
218    
219     def OnCancel(self, event):
220     self.EndModal(wxID_CANCEL)
221     self.Show(false)
222    
223    
224     class DBFrame(NonModalDialog):
225    
226     """Databse connection management dialog"""
227    
228     def __init__(self, parent, name, session, *args, **kwds):
229     kwds["style"] = wxICONIZE|wxCAPTION|wxMINIMIZE
230     NonModalDialog.__init__(self, parent, name, "")
231     self.session = session
232     self.app = self.parent.application
233    
234     self.app.Subscribe(SESSION_REPLACED, self.session_replaced)
235     self.subscribe_session()
236    
237     self.DB_ListBox = wxListBox(self, -1,
238     style=wxLB_SINGLE|wxLB_HSCROLL|wxLB_ALWAYS_SB)
239     self.DB_Add = wxButton(self, ID_DB_ADD, _("Add"))
240     self.DB_Remove = wxButton(self, ID_DB_REMOVE, _("Remove"))
241     self.DB_CLOSE = wxButton(self, wxID_CLOSE, _("Close"))
242     self.__set_properties()
243     self.__do_layout()
244     EVT_BUTTON(self, ID_DB_ADD, self.OnAdd)
245     EVT_BUTTON(self, ID_DB_REMOVE, self.OnRemove)
246     EVT_BUTTON(self, wxID_CLOSE, self.OnClose)
247    
248     self.conns_changed()
249    
250     def __set_properties(self):
251     self.SetTitle(_("Database Management"))
252     self.DB_ListBox.SetSize((200, 157))
253     self.DB_ListBox.SetSelection(0)
254    
255     def __do_layout(self):
256     top = wxBoxSizer(wxVERTICAL)
257    
258     box = wxBoxSizer(wxHORIZONTAL)
259    
260     box.Add(self.DB_ListBox, 1, wxALL|wxEXPAND
261     |wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4)
262    
263     buttons = wxBoxSizer(wxVERTICAL)
264     buttons.Add(self.DB_Add, 0, wxALL, 4)
265     buttons.Add(self.DB_Remove, 0, wxALL, 4)
266    
267     box.Add(buttons, 0, wxEXPAND)
268     top.Add(box, 1, wxEXPAND)
269    
270     buttons = wxBoxSizer(wxHORIZONTAL)
271     buttons.Add(self.DB_CLOSE, 1, wxALL|wxALIGN_RIGHT, 4)
272     top.Add(buttons, 0, wxALIGN_RIGHT)
273    
274     self.SetAutoLayout(1)
275     self.SetSizer(top)
276     top.Fit(self)
277     top.SetSizeHints(self)
278    
279     def subscribe_session(self):
280     """Internal: Subscribe to some session messages"""
281     self.session.Subscribe(DBCONN_ADDED, self.conns_changed)
282     self.session.Subscribe(DBCONN_REMOVED, self.conns_changed)
283    
284     def unsubscribe_session(self):
285     """Internal: Subscribe from messages subscribe in subscribe_session"""
286     self.session.Subscribe(DBCONN_ADDED, self.conns_changed)
287     self.session.Subscribe(DBCONN_REMOVED, self.conns_changed)
288    
289     def session_replaced(self, *args):
290     """Internal: resubscribe the session messages
291    
292     This method is a subscriber for the application's
293     SESSION_REPLACED messages.
294     """
295     self.unsubscribe_session()
296     self.session = self.app.Session()
297     self.subscribe_session()
298     self.conns_changed()
299    
300     def conns_changed(self, *args):
301     """Internal: update the db connection list box
302    
303     Subscribed to the DBCONN_REMOVED and DBCONN_REMOVED.
304     """
305     self.DB_ListBox.Clear()
306     for conn in self.session.DBConnections():
307     self.DB_ListBox.Append(conn.BriefDescription(), conn)
308    
309     def OnClose(self, event):
310     self.unsubscribe_session()
311     self.app.Unsubscribe(SESSION_REPLACED, self.session_replaced)
312     NonModalDialog.OnClose(self, event)
313    
314     def RunMessageBox(self, title, text, flags = wxOK | wxICON_INFORMATION):
315     """Run a modal message box with the given text, title and flags
316     and return the result"""
317     dlg = wxMessageDialog(self, text, title, flags)
318     dlg.CenterOnParent()
319     result = dlg.ShowModal()
320     dlg.Destroy()
321     return result
322    
323     def OnAdd(self, event):
324     adddialog = DBDialog(None, self, -1, "")
325     while 1:
326     if adddialog.ShowModal() == wxID_OK:
327     host = adddialog.GetHostname()
328     port = adddialog.GetPort()
329     database = adddialog.GetDatabase()
330     user = adddialog.GetUser()
331     password = adddialog.GetPassword()
332     for conn in self.session.DBConnections():
333     if (host == conn.host and
334     database == conn.dbname):
335     self.RunMessageBox(_("Add Database"),
336     _("Connection to '%s' already exists")
337     % database)
338     break
339     try:
340     conn = PostGISConnection(database, host = host, port=port,
341     user = user, password = password)
342     self.session.AddDBConnection(conn)
343     break
344     except psycopg.OperationalError, strerror:
345     self.RunMessageBox(_("Error"), str(strerror))
346     except:
347     self.RunMessageBox(_("Add Database"),
348     _("Can't establish connection to '%s'."
349     % database))
350     else:
351     break
352     adddialog.Destroy()
353    
354     def OnRemove(self, event):
355     i = self.DB_ListBox.GetSelection()
356     if i >= 0:
357 bh 1630 conn = self.DB_ListBox.GetClientData(i)
358     if self.session.CanRemoveDBConnection(conn):
359     self.session.RemoveDBConnection(conn)
360     else:
361     self.RunMessageBox(_("Remove Database Connection"),
362     _("The connection %s\nis still in use")
363     % conn.BriefDescription())

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26