File: wb_bookmarks_dialogs.py

package info (click to toggle)
svn-workbench 1.5.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,400 kB
  • ctags: 1,585
  • sloc: python: 12,163; sh: 74; makefile: 46; ansic: 9
file content (277 lines) | stat: -rw-r--r-- 10,498 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
'''
 ====================================================================
 Copyright (c) 2005-2006 Barry A Scott.  All rights reserved.

 This software is licensed as described in the file LICENSE.txt,
 which you should have received as part of this distribution.

 ====================================================================

    wb_bookmarks_dialogs.py

'''
import wx

import wb_exceptions

#
# all dialog changes are recorded in the BookmarkProperties
# object when the user clicks OK the changes are set into the
# preferences. Cancel leaves the prefernces unchanged.
#
class BookmarkProperties:
    def __init__( self, pi ):
        self.pi = pi
        self.wc_path = pi.wc_path
        self.menu_name = pi.menu_name
        self.menu_folder = pi.menu_folder

class BookmarkManageDialog(wx.Dialog):
    COL_FOLDER = 0
    COL_NAME = 1
    COL_WC_PATH = 2

    def __init__( self, parent, app, bookmark_prefs ):
        wx.Dialog.__init__( self, parent, -1, "Manage Bookmarks" )
        self.app = app

        self.bookmark_prefs = bookmark_prefs

        self.selection = None

        self.initControls()

    def initControls( self ):
        self.v_sizer = wx.BoxSizer( wx.VERTICAL )
        self.h_sizer1 = wx.BoxSizer( wx.HORIZONTAL )
        self.h_sizer2 = wx.BoxSizer( wx.HORIZONTAL )
        self.g_sizer = wx.FlexGridSizer( 0, 2, 0, 0 )
        self.g_sizer.AddGrowableCol( 1 )

        self.bookmark_list_ctrl = BookmarkListCtrl( self )
        self.bookmark_list_ctrl.InsertColumn( self.COL_FOLDER, "Folder" )
        self.bookmark_list_ctrl.InsertColumn( self.COL_NAME, "Bookmark Name" )
        self.bookmark_list_ctrl.InsertColumn( self.COL_WC_PATH, "WC Path" )
        self.bookmark_list_ctrl.SetColumnWidth( self.COL_FOLDER, 100 )
        self.bookmark_list_ctrl.SetColumnWidth( self.COL_NAME, 200 )
        self.bookmark_list_ctrl.SetColumnWidth( self.COL_WC_PATH, 600 )

        self.all_bookmark_props = [BookmarkProperties( self.bookmark_prefs.getBookmark( name ) )
                                        for name in self.bookmark_prefs.getBookmarkNames()
                                            if name != 'last position']
        self.sortBookmarks()

        self.bookmark_deleted_list = []

        self.bookmark_list_ctrl.SetItemCount( len( self.all_bookmark_props ) )

        self.button_delete = wx.Button( self, wx.NewId(), " Delete Bookmark " )
        self.button_delete.Enable( False )

        self.button_props = wx.Button( self, wx.NewId(), " Properties " )
        self.button_props.Enable( False )

        self.button_ok = wx.Button( self, wx.ID_OK, " OK " )
        self.button_cancel = wx.Button( self, wx.ID_CANCEL, " Cancel " )
        self.button_ok.SetDefault()

        self.h_sizer1.Add( self.button_props, 0, wx.EXPAND|wx.WEST, 15)
        self.h_sizer1.Add( self.button_delete, 0, wx.EXPAND|wx.WEST, 15)
        self.h_sizer1.Add( (60, 20), 1, wx.EXPAND)

        self.h_sizer2.Add( (60, 20), 1, wx.EXPAND)
        self.h_sizer2.Add( self.button_ok, 0, wx.EXPAND|wx.EAST, 15)
        self.h_sizer2.Add( self.button_cancel, 0, wx.EXPAND|wx.EAST, 2 )

        self.v_sizer.Add( self.g_sizer, 0, wx.EXPAND|wx.ALL, 5 )
        self.v_sizer.Add( self.bookmark_list_ctrl, 0, wx.EXPAND|wx.ALL, 5 )
        self.v_sizer.Add( self.h_sizer1, 0, wx.EXPAND|wx.ALL, 5 )
        self.v_sizer.Add( self.h_sizer2, 0, wx.EXPAND|wx.ALL, 5 )

        self.SetAutoLayout( True )
        self.SetSizer( self.v_sizer )
        self.v_sizer.Fit( self )
        self.Layout()

        self.CentreOnParent()

        try_wrapper = wb_exceptions.TryWrapperFactory( self.app.log )

        wx.EVT_LIST_ITEM_ACTIVATED( self, self.bookmark_list_ctrl.GetId(), try_wrapper( self.OnActivateBookmark ) )
        wx.EVT_LIST_ITEM_SELECTED( self, self.bookmark_list_ctrl.GetId(), try_wrapper( self.OnSelectBookmark ) )
        wx.EVT_LIST_ITEM_DESELECTED( self, self.bookmark_list_ctrl.GetId(), try_wrapper( self.OnDeselectBookmark ) )

        wx.EVT_BUTTON( self, self.button_delete.GetId(), try_wrapper( self.OnDeleteBookmark ) )
        wx.EVT_BUTTON( self, self.button_props.GetId(), try_wrapper( self.OnPropertiesBookmark ) )
        wx.EVT_BUTTON( self, wx.ID_OK, try_wrapper( self.OnOk ) )
        wx.EVT_BUTTON( self, wx.ID_CANCEL, try_wrapper( self.OnCancel ) )

    def sortBookmarks( self ):
        self.all_bookmark_props.sort( self.__cmpBookmarks )

    def __cmpBookmarks( self, a, b ):
        rc = cmp( a.menu_folder.lower(), b.menu_folder.lower() )
        if rc == 0:
            rc = cmp( a.menu_name.lower(), b.menu_name.lower() )
        return rc

    def OnGetItemText( self, item, col ):
        props = self.all_bookmark_props[ item ]
        if col == self.COL_NAME:
            return props.menu_name
        elif col == self.COL_FOLDER:
            return props.menu_folder
        elif col == self.COL_WC_PATH:
            return props.wc_path
        else:
            return ''

    def OnGetItemImage( self, item ):
        return -1

    def OnGetItemAttr( self, item ):
        return None

    def OnOk( self, event ):
        self.EndModal( wx.ID_OK )

    def OnCancel( self, event ):
        self.EndModal( wx.ID_CANCEL )

    def OnActivateBookmark( self, event ):
        self.OnSelectBookmark( event )
        self.OnPropertiesBookmark( event )

    def OnSelectBookmark( self, event ):
        self.selection = event.m_itemIndex
        self.button_delete.Enable( True )
        self.button_props.Enable( True )

    def OnDeselectBookmark( self, event ):
        self.selection = None
        self.button_delete.Enable( False )
        self.button_props.Enable( False )

    def OnDeleteBookmark( self, event ):
        self.bookmark_deleted_list.append( self.all_bookmark_props.pop( self.selection ) )

        self.bookmark_list_ctrl.SetItemCount( len(self.all_bookmark_props ) )
        self.bookmark_list_ctrl.RefreshItems( 0, len(self.all_bookmark_props )-1 )

        self.button_delete.Enable( False )
        self.button_props.Enable( False )

    def OnPropertiesBookmark( self, event ):
        props = self.all_bookmark_props[ self.selection ]
        dialog = BookmarkPropertiesDialog( self, self.app, props )

        rc = dialog.ShowModal()
        if rc != wx.ID_OK:
            return

        self.sortBookmarks()
        self.bookmark_list_ctrl.RefreshItems( 0, len(self.all_bookmark_props )-1 )

    def setPreferences( self ):
        for props in self.getDeletedBookmarkList():
            self.bookmark_prefs.delBookmark( props.wc_path )

        for props in self.all_bookmark_props:
            props.pi.menu_name = props.menu_name
            props.pi.menu_folder = props.menu_folder

    def getDeletedBookmarkList( self ):
        return self.bookmark_deleted_list

class BookmarkListCtrl(wx.ListCtrl):
    def __init__( self, parent ):
        wx.ListCtrl.__init__( self, parent, -1,
                                size=(700,400),
                                style=wx.LC_REPORT|wx.LC_VIRTUAL )
        self.parent = parent

    def OnGetItemText( self, item, col ):
        return self.parent.OnGetItemText( item, col )

    def OnGetItemImage( self, item ):
        return self.parent.OnGetItemImage( item )

    def OnGetItemAttr( self, item ):
        return self.parent.OnGetItemAttr( item )

class BookmarkPropertiesDialog(wx.Dialog):
    def __init__( self, parent, app, props ):
        wx.Dialog.__init__( self, parent, -1, "Bookmark Properties" )

        self.parent = parent
        self.app = app
        self.props = props

        self.initControls()

    def initControls( self ):
        self.v_sizer = wx.BoxSizer( wx.VERTICAL )
        self.h_sizer2 = wx.BoxSizer( wx.HORIZONTAL )
        self.g_sizer = wx.FlexGridSizer( 0, 2, 0, 0 )
        self.g_sizer.AddGrowableCol( 1 )

        self.static_text1 = wx.StaticText( self, -1, "WC Path: ", style=wx.ALIGN_RIGHT)
        self.wc_path_ctrl = wx.TextCtrl( self, -1, self.props.wc_path, size=(500, -1), style=wx.TE_READONLY )

        self.g_sizer.Add( self.static_text1, 1, wx.EXPAND|wx.ALL|wx.ALIGN_RIGHT, 3 )
        self.g_sizer.Add( self.wc_path_ctrl, 1, wx.EXPAND|wx.ALL|wx.ALIGN_RIGHT, 3 )

        self.static_text2 = wx.StaticText( self, -1, "Menu Name: ", style=wx.ALIGN_RIGHT)
        self.menu_name_ctrl = wx.TextCtrl( self, -1, self.props.menu_name, size=(300, -1) )

        self.g_sizer.Add( self.static_text2, 1, wx.EXPAND|wx.ALL|wx.ALIGN_RIGHT, 3 )
        self.g_sizer.Add( self.menu_name_ctrl, 1, wx.EXPAND|wx.ALL|wx.ALIGN_RIGHT, 3 )

        all_menu_folders = {}
        for props in self.parent.all_bookmark_props:
            all_menu_folders[ props.menu_folder ] = None

        all_menu_folders = all_menu_folders.keys()
        all_menu_folders.sort( lambda a, b: cmp( a.lower(), b.lower() ) )

        self.static_text3 = wx.StaticText( self, -1, "Menu Folder: ", style=wx.ALIGN_RIGHT)
        self.menu_folder_ctrl = wx.ComboBox( self, -1, self.props.menu_folder,
                                                style=wx.CB_DROPDOWN, choices=all_menu_folders,
                                                size=(300, -1) )

        self.g_sizer.Add( self.static_text3, 1, wx.EXPAND|wx.ALL|wx.ALIGN_RIGHT, 3 )
        self.g_sizer.Add( self.menu_folder_ctrl, 1, wx.EXPAND|wx.ALL|wx.ALIGN_RIGHT, 3 )

        self.button_ok = wx.Button( self, wx.ID_OK, " OK " )
        self.button_cancel = wx.Button( self, wx.ID_CANCEL, " Cancel " )
        self.button_ok.SetDefault()

        self.h_sizer2.Add( (60, 20), 1, wx.EXPAND)
        self.h_sizer2.Add( self.button_ok, 0, wx.EXPAND|wx.EAST, 15)
        self.h_sizer2.Add( self.button_cancel, 0, wx.EXPAND|wx.EAST, 2 )

        self.v_sizer.Add( self.g_sizer, 0, wx.EXPAND|wx.ALL, 5 )
        self.v_sizer.Add( self.h_sizer2, 0, wx.EXPAND|wx.ALL, 5 )

        self.SetAutoLayout( True )
        self.SetSizer( self.v_sizer )
        self.v_sizer.Fit( self )
        self.Layout()

        self.CentreOnParent()

        try_wrapper = wb_exceptions.TryWrapperFactory( self.app.log )

        wx.EVT_BUTTON( self, wx.ID_OK, try_wrapper( self.OnOk ) )
        wx.EVT_BUTTON( self, wx.ID_CANCEL, try_wrapper( self.OnCancel ) )

    def updateProps( self ):
        self.props.menu_name = self.menu_name_ctrl.GetValue()
        self.props.menu_folder = self.menu_folder_ctrl.GetValue()

    def OnOk( self, event ):
        self.updateProps()
        self.EndModal( wx.ID_OK )

    def OnCancel( self, event ):
        self.EndModal( wx.ID_CANCEL )