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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/thuban/Thuban/UI/dbdialog.py revision 1630 by bh, Fri Aug 22 15:59:57 2003 UTC branches/WIP-pyshapelib-bramz/Thuban/UI/dbdialog.py revision 2734 by bramz, Thu Mar 1 12:42:59 2007 UTC
# Line 1  Line 1 
1  # Copyright (c) 2001, 2003 by Intevation GmbH  # Copyright (c) 2001, 2003, 2004 by Intevation GmbH
2  # Authors:  # Authors:
3  # Martin Mueller <[email protected]>  # Martin Mueller <[email protected]>
4  # Bernhard Herzog <[email protected]>  # Bernhard Herzog <[email protected]>
# Line 11  Line 11 
11    
12  import sys, traceback  import sys, traceback
13    
14  from wxPython.wx import *  import wx
15    
16  try:  try:
17      import psycopg      import psycopg
# Line 20  except ImportError: Line 20  except ImportError:
20    
21  from Thuban import _  from Thuban import _
22  from Thuban.UI.dialogs import NonModalDialog  from Thuban.UI.dialogs import NonModalDialog
23  from Thuban.Model.postgisdb import PostGISConnection  from Thuban.Model.table import FIELDTYPE_INT
24    from Thuban.Model.postgisdb import ConnectionError, PostGISConnection
25  from Thuban.Model.messages import DBCONN_ADDED, DBCONN_REMOVED  from Thuban.Model.messages import DBCONN_ADDED, DBCONN_REMOVED
26  from messages import SESSION_REPLACED  from messages import SESSION_REPLACED
27    
 ID_DBADD_OK     = 9001  
 ID_DBADD_CANCEL = 9002  
28    
29  ID_DB_ADD    = 9101  ID_DB_ADD    = 9101
30  ID_DB_REMOVE = 9102  ID_DB_REMOVE = 9102
# Line 36  ID_DBCHOOSE_CANCEL   = 9203 Line 35  ID_DBCHOOSE_CANCEL   = 9203
35  ID_LB_DCLICK         = 9204  ID_LB_DCLICK         = 9204
36    
37    
38  class ChooseDBTableDialog(wxDialog):  class ChooseDBTableDialog(wx.Dialog):
39    
40      def __init__(self, session, *args, **kwds):      def __init__(self, parent, session):
41          kwds["style"] = wxDIALOG_MODAL|wxCAPTION          wx.Dialog.__init__(self, parent, -1, _("Choose layer from database"),
42          wxDialog.__init__(self, *args, **kwds)                            style = wx.DIALOG_MODAL|wx.CAPTION)
43          self.session = session          self.session = session
44          self.dbconns = self.session.DBConnections()          self.dbconns = self.session.DBConnections()
45          self.tables = []          self.tables = []
         self.list_box_4 = wxListBox(self, -1)  
         for i in range(len(self.dbconns)):  
             self.list_box_4.Append(self.dbconns[i].BriefDescription())  
         self.DB_CHOOSE_RETRIEVE = wxButton(self, ID_DBCHOOSE_RETRIEVE,  
                                            _("Retrieve"))  
         self.list_box_5 = wxListBox(self, ID_LB_DCLICK)  
         self.DB_CHOOSE_OK = wxButton(self, ID_DBCHOOSE_OK, _("OK"))  
         self.DB_CHOOSE_CANCEL = wxButton(self, ID_DBCHOOSE_CANCEL, _("Cancel"))  
         self.__set_properties()  
         self.__do_layout()  
   
         EVT_BUTTON(self, ID_DBCHOOSE_OK, self.OnOK)  
         EVT_BUTTON(self, ID_DBCHOOSE_CANCEL, self.OnCancel)  
         EVT_BUTTON(self, ID_DBCHOOSE_RETRIEVE, self.OnRetrieve)  
         EVT_LISTBOX_DCLICK(self, ID_LB_DCLICK, self.OnLBDClick)  
46    
47            #
48            # Build the dialog
49            #
50    
51            # Sizer for the entire dialog
52            top = wx.FlexGridSizer(2, 1, 0, 0)
53    
54            # Sizer for the main part with the list boxes
55            main_sizer = wx.BoxSizer(wx.HORIZONTAL)
56            top.Add(main_sizer, 1, wx.EXPAND, 0)
57    
58            # The list box with the connections
59            static_box = wx.StaticBoxSizer(wx.StaticBox(self, -1, _("Databases")),
60                                       wx.HORIZONTAL)
61            self.lb_connections = wx.ListBox(self, -1)
62            static_box.Add(self.lb_connections, 0, wx.EXPAND, 0)
63            main_sizer.Add(static_box, 1, wx.EXPAND, 0)
64    
65      def __set_properties(self):          for i in range(len(self.dbconns)):
66          self.SetTitle(_("Choose layer from database"))              self.lb_connections.Append(self.dbconns[i].BriefDescription())
67          self.list_box_4.SetSelection(0)          if self.lb_connections.GetCount() > 0:
68          self.list_box_5.SetSelection(0)              self.lb_connections.SetSelection(0, True)
69    
70            # The button box between the connections list box and the table
71            # list box
72            buttons = wx.FlexGridSizer(3, 1, 0, 0)
73            buttons.Add( (20, 80), 0, wx.EXPAND, 0)
74            retrieve_button = wx.Button(self, ID_DBCHOOSE_RETRIEVE, _("Retrieve"))
75            self.Bind(wx.EVT_BUTTON, self.OnRetrieve, id=ID_DBCHOOSE_RETRIEVE)
76            buttons.Add(retrieve_button, 0, wx.ALL
77                        |wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 4)
78            buttons.Add( (20, 80), 0, wx.EXPAND, 0)
79            main_sizer.Add(buttons, 0, wx.EXPAND, 0)
80    
81            # The list box with the tables
82            static_box = wx.StaticBoxSizer(wx.StaticBox(self, -1, _("Tables")),
83                                       wx.HORIZONTAL)
84            self.lb_tables = wx.ListBox(self, ID_LB_DCLICK)
85            self.Bind(wx.EVT_LISTBOX, self.OnTableSelect, id=ID_LB_DCLICK)
86            self.Bind(wx.EVT_LISTBOX_DCLICK, self.OnLBDClick, id=ID_LB_DCLICK)
87            static_box.Add(self.lb_tables, 0, wx.EXPAND, 0)
88            main_sizer.Add(static_box, 1, wx.EXPAND, 0)
89    
90            # id column and geometry column selection
91            box = wx.BoxSizer(wx.VERTICAL)
92            box.Add(wx.StaticText(self, -1, _("ID Column")), 0,
93                    wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
94            self.text_id_column = wx.ComboBox(self, -1, "")
95            box.Add(self.text_id_column, 0,
96                    wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 4)
97    
98            box.Add(wx.StaticText(self, -1, _("Geometry Column")), 0,
99                    wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
100            self.text_geo_column = wx.ComboBox(self, -1, "")
101            box.Add(self.text_geo_column, 0,
102                    wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 4)
103            main_sizer.Add(box, 1, wx.EXPAND, 0)
104    
105            # The standard button box at the bottom of the dialog
106            buttons = wx.FlexGridSizer(1, 2, 0, 0)
107            ok_button = wx.Button(self, ID_DBCHOOSE_OK, _("OK"))
108            self.Bind(wx.EVT_BUTTON, self.OnOK, id=ID_DBCHOOSE_OK)
109            buttons.Add(ok_button, 0, wx.ALL|wx.ALIGN_RIGHT, 4)
110            cancel_button = wx.Button(self, ID_DBCHOOSE_CANCEL, _("Cancel"))
111            self.Bind(wx.EVT_BUTTON, self.OnCancel, id=ID_DBCHOOSE_CANCEL)
112            buttons.Add(cancel_button, 0, wx.ALL, 4)
113            top.Add(buttons, 1, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 4)
114    
115      def __do_layout(self):          # Autosizing
         grid_sizer_1 = wxFlexGridSizer(2, 1, 0, 0)  
         grid_sizer_3 = wxFlexGridSizer(1, 2, 0, 0)  
         grid_sizer_2 = wxFlexGridSizer(1, 3, 0, 0)  
         sizer_4 = wxStaticBoxSizer(wxStaticBox(self, -1, _("Tables")),  
                                    wxHORIZONTAL)  
         grid_sizer_4 = wxFlexGridSizer(3, 1, 0, 0)  
         sizer_3 = wxStaticBoxSizer(wxStaticBox(self, -1, _("Databases")),  
                                    wxHORIZONTAL)  
         sizer_3.Add(self.list_box_4, 0, wxEXPAND, 0)  
         grid_sizer_2.Add(sizer_3, 1, wxEXPAND, 0)  
         grid_sizer_4.Add(20, 80, 0, wxEXPAND, 0)  
         grid_sizer_4.Add(self.DB_CHOOSE_RETRIEVE, 0, wxALL  
                         |wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4)  
         grid_sizer_4.Add(20, 80, 0, wxEXPAND, 0)  
         grid_sizer_2.Add(grid_sizer_4, 1, wxEXPAND, 0)  
         sizer_4.Add(self.list_box_5, 0, wxEXPAND, 0)  
         grid_sizer_2.Add(sizer_4, 1, wxEXPAND, 0)  
         grid_sizer_1.Add(grid_sizer_2, 1, wxEXPAND, 0)  
         grid_sizer_3.Add(self.DB_CHOOSE_OK, 0, wxALL|wxALIGN_RIGHT, 4)  
         grid_sizer_3.Add(self.DB_CHOOSE_CANCEL, 0, wxALL, 4)  
         grid_sizer_1.Add(grid_sizer_3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL, 4)  
116          self.SetAutoLayout(1)          self.SetAutoLayout(1)
117          self.SetSizer(grid_sizer_1)          self.SetSizer(top)
118          grid_sizer_1.Fit(self)          top.Fit(self)
119          grid_sizer_1.SetSizeHints(self)          top.SetSizeHints(self)
120          self.Layout()          self.Layout()
121    
122    
123      def GetTable(self):      def GetTable(self):
124          i = self.list_box_5.GetSelection()          i = self.lb_tables.GetSelection()
125          if i >= 0:          if i >= 0:
126              return self.selected_conn, self.tables[i]              return (self.selected_conn, self.tables[i],
127                        self.text_id_column.GetValue(),
128                        self.text_geo_column.GetValue())
129          return None          return None
130    
131      def OnRetrieve(self, event):      def OnRetrieve(self, event):
132          i = self.list_box_4.GetSelection()          i = self.lb_connections.GetSelection()
133          if i >= 0:          if i >= 0:
134              self.selected_conn = self.dbconns[i]              self.selected_conn = self.dbconns[i]
135              self.tables = self.selected_conn.GeometryTables()              self.tables = self.selected_conn.GeometryTables()
136              self.list_box_5.Set(self.tables)              self.lb_tables.Set(self.tables)
137    
138        def OnTableSelect(self, event):
139            i = self.lb_tables.GetSelection()
140            self.text_id_column.Clear()
141            self.text_geo_column.Clear()
142            if i >= 0:
143                for name, typ in self.selected_conn.table_columns(self.tables[i]):
144                    if typ == "geometry":
145                        self.text_geo_column.Append(name)
146                    elif typ == FIELDTYPE_INT:
147                        self.text_id_column.Append(name)
148    
149      def OnLBDClick(self, event):      def OnLBDClick(self, event):
150          if self.list_box_5.GetSelection() >= 0:          if self.lb_tables.GetSelection() >= 0:
151              self.EndModal(wxID_OK)              self.EndModal(wx.ID_OK)
152              self.Show(false)              self.Show(False)
153    
154      def OnOK(self, event):      def OnOK(self, event):
155          self.EndModal(wxID_OK)          self.EndModal(wx.ID_OK)
156          self.Show(false)          self.Show(False)
157    
158      def OnCancel(self, event):      def OnCancel(self, event):
159          self.EndModal(wxID_CANCEL)          self.EndModal(wx.ID_CANCEL)
160          self.Show(false)          self.Show(False)
161    
162    
163  class DBDialog(wxDialog):  class DBDialog(wx.Dialog):
164    
165      """Dialog for the parameters of a database connection"""      """Dialog for the parameters of a database connection"""
166    
167      def __init__(self, selection=None, *args, **kwds):      def __init__(self, parent, title, parameters, message = ""):
168          kwds["style"] = wxDEFAULT_DIALOG_STYLE          """Initialize the dialog box.
         wxDialog.__init__(self, *args, **kwds)  
         self.DBAdd_Label_Host = wxStaticText(self, -1, _("Hostname:"))  
         self.DBAdd_Host = wxTextCtrl(self, -1, "")  
         self.DBAdd_Label_Port = wxStaticText(self, -1, _("Port:"))  
         self.DBAdd_Port = wxTextCtrl(self, -1, "")  
         self.DBAdd_Label_Database = wxStaticText(self, -1, _("Database:"))  
         self.DBAdd_Database = wxTextCtrl(self, -1, "")  
         self.DBAdd_Label_User = wxStaticText(self, -1, _("User:"))  
         self.DBAdd_User = wxTextCtrl(self, -1, "")  
         self.DBAdd_Label_Pwd = wxStaticText(self, -1, _("Password:"))  
         self.DBAdd_Pwd = wxTextCtrl(self, -1, "", style=wxTE_PASSWORD)  
         self.DBAdd_OK = wxButton(self, ID_DBADD_OK, _("OK"))  
         self.DBAdd_Cancel = wxButton(self, ID_DBADD_CANCEL, _("Cancel"))  
         if selection != None:  
             self.DBAdd_Host.SetEditable(false)  
             self.DBAdd_Host.SetValue(selection.host)  
             self.DBAdd_Port.SetValue(selection.port)  
             self.DBAdd_Database.SetEditable(false)  
             self.DBAdd_Database.SetValue(selection.dbname())  
             self.DBAdd_User.SetValue(selection.user)  
         self.__set_properties()  
         self.__do_layout()  
         EVT_BUTTON(self, ID_DBADD_OK, self.OnOK)  
         EVT_BUTTON(self, ID_DBADD_CANCEL, self.OnCancel)  
169    
170      def __set_properties(self):          The parameters argument should be a dictionary containing known
171          self.SetTitle(_("Add database"))          connection parameters.
         self.SetSize((480, 185))  
         self.DBAdd_Label_Host.SetSize((60, 16))  
         self.DBAdd_Host.SetSize((200, 22))  
         self.DBAdd_Label_Database.SetSize((60, 16))  
         self.DBAdd_Label_User.SetSize((60, 16))  
   
     def __do_layout(self):  
         grid_sizer_1 = wxFlexGridSizer(4, 1, 0, 0)  
         grid_sizer_2 = wxGridSizer(1, 2, 0, 0)  
         grid_sizer_5 = wxFlexGridSizer(1, 5, 0, 0)  
         grid_sizer_6 = wxFlexGridSizer(1, 5, 0, 0)  
         grid_sizer_3 = wxFlexGridSizer(1, 5, 0, 0)  
         grid_sizer_3.Add(self.DBAdd_Label_Host, 0,  
                          wxALL|wxALIGN_CENTER_VERTICAL, 4)  
         grid_sizer_3.Add(self.DBAdd_Host, 0, wxALL, 4)  
         grid_sizer_3.Add(20, 20, 0, wxALL|wxEXPAND, 4)  
         grid_sizer_3.Add(self.DBAdd_Label_Port, 0,  
                          wxALL|wxALIGN_CENTER_VERTICAL, 4)  
         grid_sizer_3.Add(self.DBAdd_Port, 0, wxALL, 4)  
         grid_sizer_1.Add(grid_sizer_3, 1, wxALL|wxEXPAND, 4)  
         grid_sizer_6.Add(self.DBAdd_Label_Database, 0,  
                          wxALL|wxALIGN_CENTER_VERTICAL, 4)  
         grid_sizer_6.Add(self.DBAdd_Database, 0, wxALL, 4)  
         grid_sizer_6.Add(20, 20, 0, wxALL|wxEXPAND, 4)  
         grid_sizer_1.Add(grid_sizer_6, 1, wxALL|wxEXPAND, 4)  
         grid_sizer_5.Add(self.DBAdd_Label_User, 0,  
                          wxALL|wxALIGN_CENTER_VERTICAL, 4)  
         grid_sizer_5.Add(self.DBAdd_User, 0, wxALL|wxALIGN_CENTER_VERTICAL, 4)  
         grid_sizer_5.Add(20, 20, 0, wxALL|wxEXPAND, 4)  
         grid_sizer_5.Add(self.DBAdd_Label_Pwd, 0,  
                          wxALL|wxALIGN_CENTER_VERTICAL, 4)  
         grid_sizer_5.Add(self.DBAdd_Pwd, 0, wxALL|wxALIGN_CENTER_VERTICAL, 4)  
         grid_sizer_1.Add(grid_sizer_5, 1, wxALL|wxEXPAND, 4)  
         grid_sizer_2.Add(self.DBAdd_OK, 0,  
                          wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 4)  
         grid_sizer_2.Add(self.DBAdd_Cancel, 0, wxALL|wxALIGN_CENTER_VERTICAL,  
                          4)  
         grid_sizer_1.Add(grid_sizer_2, 1, wxALL|wxEXPAND, 4)  
         self.SetAutoLayout(1)  
         self.SetSizer(grid_sizer_1)  
         self.Layout()  
172    
173            The optional message parameter will be displayed at the top of
174            the dialog box and can be used to display error messages when
175            using the dialog to ask for correct parameters when the
176            connection can't be established.
177            """
178            wx.Dialog.__init__(self, parent, -1, title)
179    
180      def GetHostname(self):          top = wx.BoxSizer(wx.VERTICAL)
         return self.DBAdd_Host.GetValue()  
181    
182      def GetPort(self):          if message:
183          return self.DBAdd_Port.GetValue()              top.Add(wx.StaticText(self, -1, message), 0,
184                        wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
185    
186            box = wx.BoxSizer(wx.HORIZONTAL)
187            box.Add(wx.StaticText(self, -1, _("Hostname:")), 0,
188                    wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
189            self.text_host = wx.TextCtrl(self, -1, parameters.get("host", ""))
190            box.Add(self.text_host, 2, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 4)
191            box.Add(wx.StaticText(self, -1, _("Port:")), 0,
192                    wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
193            self.text_port = wx.TextCtrl(self, -1, parameters.get("port", ""))
194            box.Add(self.text_port, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 4)
195            top.Add(box, 0, wx.EXPAND)
196    
197            box = wx.BoxSizer(wx.HORIZONTAL)
198            box.Add(wx.StaticText(self, -1, _("Database Name:")), 0,
199                    wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
200            self.text_dbname = wx.TextCtrl(self, -1,
201                                          parameters.get("dbname", ""))
202            box.Add(self.text_dbname, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 4)
203            top.Add(box, 0, wx.EXPAND)
204    
205            box = wx.BoxSizer(wx.HORIZONTAL)
206            box.Add(wx.StaticText(self, -1, _("User:")), 0,
207                    wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
208            self.text_user = wx.TextCtrl(self, -1,
209                                        parameters.get("user", ""))
210            box.Add(self.text_user, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 4)
211            box.Add(wx.StaticText(self, -1, _("Password:")), 0,
212                    wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
213            self.text_password = wx.TextCtrl(self, -1,
214                                            parameters.get("password", ""),
215                                            style = wx.TE_PASSWORD)
216            box.Add(self.text_password, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND,
217                    4)
218            top.Add(box, 0, wx.EXPAND)
219    
220            buttons = wx.BoxSizer(wx.HORIZONTAL)
221            button = wx.Button(self, wx.ID_OK, _("OK"))
222            buttons.Add(button, 0, wx.ALL, 4)
223            button = wx.Button(self, wx.ID_CANCEL, _("Cancel"))
224            buttons.Add(button, 0, wx.ALL, 4)
225            top.Add(buttons, 0, wx.ALIGN_RIGHT, 4)
226    
227      def GetDatabase(self):          self.SetAutoLayout(1)
228          return self.DBAdd_Database.GetValue()          self.SetSizer(top)
229            top.Fit(self)
230            top.SetSizeHints(self)
231    
232      def GetUser(self):          self.Bind(wx.EVT_BUTTON, self.OnOK, id=wx.ID_OK)
233          return self.DBAdd_User.GetValue()          self.Bind(wx.EVT_BUTTON, self.OnCancel, id=wx.ID_CANCEL)
234    
235      def GetPassword(self):      def RunDialog(self):
236          return self.DBAdd_Pwd.GetValue()          self.ShowModal()
237            self.Destroy()
238            return self.result
239    
240        def end_dialog(self, result):
241            self.result = result
242            if result is not None:
243                self.EndModal(wx.ID_OK)
244            else:
245                self.EndModal(wx.ID_CANCEL)
246            self.Show(False)
247    
248      def OnOK(self, event):      def OnOK(self, event):
249          self.EndModal(wxID_OK)          result = {}
250          self.Show(false)          for name in ("host", "port", "dbname", "user", "password"):
251                result[name] = getattr(self, "text_" + name).GetValue()
252            self.end_dialog(result)
253    
254      def OnCancel(self, event):      def OnCancel(self, event):
255          self.EndModal(wxID_CANCEL)          self.end_dialog(None)
256          self.Show(false)  
257    
258    
259  class DBFrame(NonModalDialog):  class DBFrame(NonModalDialog):
# Line 226  class DBFrame(NonModalDialog): Line 261  class DBFrame(NonModalDialog):
261      """Databse connection management dialog"""      """Databse connection management dialog"""
262    
263      def __init__(self, parent, name, session, *args, **kwds):      def __init__(self, parent, name, session, *args, **kwds):
264          kwds["style"] = wxICONIZE|wxCAPTION|wxMINIMIZE          kwds["style"] = wx.ICONIZE|wx.CAPTION|wx.MINIMIZE
265          NonModalDialog.__init__(self, parent, name, "")          NonModalDialog.__init__(self, parent, name, "")
266          self.session = session          self.session = session
267          self.app = self.parent.application          self.app = self.parent.application
# Line 234  class DBFrame(NonModalDialog): Line 269  class DBFrame(NonModalDialog):
269          self.app.Subscribe(SESSION_REPLACED, self.session_replaced)          self.app.Subscribe(SESSION_REPLACED, self.session_replaced)
270          self.subscribe_session()          self.subscribe_session()
271    
272          self.DB_ListBox = wxListBox(self, -1,          self.DB_ListBox = wx.ListBox(self, -1,
273                                style=wxLB_SINGLE|wxLB_HSCROLL|wxLB_ALWAYS_SB)                                style=wx.LB_SINGLE|wx.LB_HSCROLL|wx.LB_ALWAYS_SB)
274          self.DB_Add = wxButton(self, ID_DB_ADD, _("Add"))          self.DB_Add = wx.Button(self, ID_DB_ADD, _("Add"))
275          self.DB_Remove = wxButton(self, ID_DB_REMOVE, _("Remove"))          self.DB_Remove = wx.Button(self, ID_DB_REMOVE, _("Remove"))
276          self.DB_CLOSE = wxButton(self, wxID_CLOSE, _("Close"))          self.DB_CLOSE = wx.Button(self, wx.ID_CLOSE, _("Close"))
277          self.__set_properties()          self.__set_properties()
278          self.__do_layout()          self.__do_layout()
279          EVT_BUTTON(self, ID_DB_ADD, self.OnAdd)          self.Bind(wx.EVT_BUTTON, self.OnAdd, id=ID_DB_ADD)
280          EVT_BUTTON(self, ID_DB_REMOVE, self.OnRemove)          self.Bind(wx.EVT_BUTTON, self.OnRemove, id=ID_DB_REMOVE)
281          EVT_BUTTON(self, wxID_CLOSE, self.OnClose)          self.Bind(wx.EVT_BUTTON, self.OnClose, id=wx.ID_CLOSE)
282    
283          self.conns_changed()          self.conns_changed()
284    
285      def __set_properties(self):      def __set_properties(self):
286          self.SetTitle(_("Database Management"))          self.SetTitle(_("Database Management"))
287          self.DB_ListBox.SetSize((200, 157))          self.DB_ListBox.SetSize((200, 157))
         self.DB_ListBox.SetSelection(0)  
288    
289      def __do_layout(self):      def __do_layout(self):
290          top = wxBoxSizer(wxVERTICAL)          top = wx.BoxSizer(wx.VERTICAL)
291    
292          box = wxBoxSizer(wxHORIZONTAL)          box = wx.BoxSizer(wx.HORIZONTAL)
293    
294          box.Add(self.DB_ListBox, 1, wxALL|wxEXPAND          box.Add(self.DB_ListBox, 1, wx.ALL|wx.EXPAND
295                  |wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4)                  |wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 4)
296    
297          buttons = wxBoxSizer(wxVERTICAL)          buttons = wx.BoxSizer(wx.VERTICAL)
298          buttons.Add(self.DB_Add, 0, wxALL, 4)          buttons.Add(self.DB_Add, 0, wx.ALL, 4)
299          buttons.Add(self.DB_Remove, 0, wxALL, 4)          buttons.Add(self.DB_Remove, 0, wx.ALL, 4)
300    
301          box.Add(buttons, 0, wxEXPAND)          box.Add(buttons, 0, wx.EXPAND)
302          top.Add(box, 1, wxEXPAND)          top.Add(box, 1, wx.EXPAND)
303    
304          buttons = wxBoxSizer(wxHORIZONTAL)          buttons = wx.BoxSizer(wx.HORIZONTAL)
305          buttons.Add(self.DB_CLOSE, 1, wxALL|wxALIGN_RIGHT, 4)          buttons.Add(self.DB_CLOSE, 1, wx.ALL|wx.ALIGN_RIGHT, 4)
306          top.Add(buttons, 0, wxALIGN_RIGHT)          top.Add(buttons, 0, wx.ALIGN_RIGHT)
307    
308          self.SetAutoLayout(1)          self.SetAutoLayout(1)
309          self.SetSizer(top)          self.SetSizer(top)
# Line 300  class DBFrame(NonModalDialog): Line 334  class DBFrame(NonModalDialog):
334      def conns_changed(self, *args):      def conns_changed(self, *args):
335          """Internal: update the db connection list box          """Internal: update the db connection list box
336    
337          Subscribed to the DBCONN_REMOVED and DBCONN_REMOVED.          Subscribed to the DBCONN_ADDED and DBCONN_REMOVED messages.
338          """          """
339          self.DB_ListBox.Clear()          self.DB_ListBox.Clear()
340          for conn in self.session.DBConnections():          for conn in self.session.DBConnections():
# Line 311  class DBFrame(NonModalDialog): Line 345  class DBFrame(NonModalDialog):
345          self.app.Unsubscribe(SESSION_REPLACED, self.session_replaced)          self.app.Unsubscribe(SESSION_REPLACED, self.session_replaced)
346          NonModalDialog.OnClose(self, event)          NonModalDialog.OnClose(self, event)
347    
348      def RunMessageBox(self, title, text, flags = wxOK | wxICON_INFORMATION):      def RunMessageBox(self, title, text, flags = wx.OK | wx.ICON_INFORMATION):
349          """Run a modal message box with the given text, title and flags          """Run a modal message box with the given text, title and flags
350          and return the result"""          and return the result"""
351          dlg = wxMessageDialog(self, text, title, flags)          dlg = wxMessageDialog(self, text, title, flags)
# Line 321  class DBFrame(NonModalDialog): Line 355  class DBFrame(NonModalDialog):
355          return result          return result
356    
357      def OnAdd(self, event):      def OnAdd(self, event):
358          adddialog = DBDialog(None, self, -1, "")          message = ""
359            parameters = {}
360          while 1:          while 1:
361              if adddialog.ShowModal() == wxID_OK:              dialog = DBDialog(self, _("Add Database"), parameters, message)
362                  host = adddialog.GetHostname()              parameters = dialog.RunDialog()
363                  port = adddialog.GetPort()              if parameters is not None:
                 database = adddialog.GetDatabase()  
                 user = adddialog.GetUser()  
                 password = adddialog.GetPassword()  
364                  for conn in self.session.DBConnections():                  for conn in self.session.DBConnections():
365                      if (host == conn.host and                      if conn.MatchesParameters(parameters):
                         database == conn.dbname):  
366                          self.RunMessageBox(_("Add Database"),                          self.RunMessageBox(_("Add Database"),
367                                         _("Connection to '%s' already exists")                                           _("Connection '%s' already exists")
368                                             % database)                                             % conn.BriefDescription())
369                            break
370                    else:
371                        try:
372                            conn = PostGISConnection(**parameters)
373                        except ConnectionError, val:
374                            message = str(val)
375                        else:
376                            self.session.AddDBConnection(conn)
377                          break                          break
                 try:  
                     conn = PostGISConnection(database, host = host, port=port,  
                                              user = user, password = password)  
                     self.session.AddDBConnection(conn)  
                     break  
                 except psycopg.OperationalError, strerror:  
                     self.RunMessageBox(_("Error"), str(strerror))  
                 except:  
                     self.RunMessageBox(_("Add Database"),  
                                        _("Can't establish connection to '%s'."  
                                          % database))  
378              else:              else:
379                  break                  break
         adddialog.Destroy()  
380    
381      def OnRemove(self, event):      def OnRemove(self, event):
382          i = self.DB_ListBox.GetSelection()          i = self.DB_ListBox.GetSelection()

Legend:
Removed from v.1630  
changed lines
  Added in v.2734

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26