Loading files now adds the namespaces.
[dom-editor.git] / Dome / Namespaces.py
blob9b6d4da075295b35de083b8da498f4ab8fa4320e
1 from xml.dom import XMLNS_NAMESPACE, XML_NAMESPACE
3 import rox
4 from rox import g
6 fixed_ns = {'xml': XML_NAMESPACE, 'xmlns': XMLNS_NAMESPACE}
8 class Namespaces(g.GenericTreeModel):
9 def __init__(self):
10 g.GenericTreeModel.__init__(self)
11 self.uri = dict(fixed_ns)
12 self.update_list()
14 def update_list(self):
15 self.list = self.uri.keys()
16 self.list.sort()
17 pre = self.prefix = {}
18 for p, u in self.uri.iteritems():
19 pre[u] = p
21 def on_get_n_columns(self):
22 return 2
24 def on_get_iter(self, path):
25 assert len(path) == 1
26 return path[0]
28 def on_get_value(self, iter, column):
29 if column == 0:
30 return self.list[iter]
31 return self.uri[self.list[iter]]
33 def on_iter_nth_child(self, iter, n):
34 if iter == None:
35 return n
36 return None
38 def on_get_column_type(self, col):
39 return str
41 def on_iter_has_child(self, iter):
42 return False
44 def on_iter_next(self, iter):
45 if iter < len(self.list) - 1:
46 return iter + 1
48 def __setitem__(self, prefix, uri):
49 if prefix in self.uri and self.uri[prefix] == uri:
50 return
52 assert prefix
53 if prefix in fixed_ns:
54 raise Exception('That namespace prefix cannot be changed')
56 modifed = prefix in self.uri
58 self.uri[prefix] = uri
59 self.update_list()
60 path = (self.list.index(prefix),)
62 if modifed:
63 self.emit('row-changed', path, self.get_iter(path))
64 else:
65 self.emit('row-inserted', path, self.get_iter(path))
67 def __delitem__(self, iter):
68 prefix = self[iter][0]
69 if prefix in fixed_ns:
70 raise Exception('This is a built-in namespace and cannot be deleted')
71 path = (self.list.index(prefix),)
72 del self.uri[prefix]
73 self.update_list()
74 self.emit('row-deleted', path)
76 def ensure_ns(self, suggested_prefix, uri):
77 """Return the prefix for this URI. If none is set choose one (using suggested_prefix
78 if possible)."""
79 try:
80 return self.prefix[uri]
81 except KeyError:
82 if not suggested_prefix:
83 suggested_prefix = 'ns'
84 if suggested_prefix in self.uri:
85 x = 1
86 while suggested_prefix + `x` in self.uri: x += 1
87 suggested_prefix += `x`
88 self[suggested_prefix] = uri
89 print "Added", suggested_prefix, uri
90 return suggested_prefix
92 class GUI(rox.Dialog):
93 def __init__(self, model):
94 rox.Dialog.__init__(self)
95 self.model = model
97 self.add_button(g.STOCK_ADD, 1)
98 self.add_button(g.STOCK_DELETE, 2)
99 self.add_button(g.STOCK_CLOSE, g.RESPONSE_OK)
101 tree = g.TreeView(model.namespaces)
103 def response(dialog, resp):
104 if resp == 1:
105 dialog.add_new(model.namespaces)
106 elif resp == 2:
107 dialog.delete_selected(tree.get_selection())
108 else:
109 dialog.destroy()
110 self.connect('response', response)
111 self.set_position(g.WIN_POS_MOUSE)
112 self.set_title('Namespaces for ' + `model.uri`)
114 frame = g.Frame()
115 frame.add(tree)
116 frame.set_shadow_type(g.SHADOW_IN)
118 cell = g.CellRendererText()
119 column = g.TreeViewColumn('Prefix', cell, text = 0)
120 tree.append_column(column)
122 column = g.TreeViewColumn('URI', cell, text = 1)
123 tree.append_column(column)
125 frame.show_all()
127 self.vbox.pack_start(frame, True, True)
128 self.set_default_size(400, 200)
130 self.set_has_separator(False)
132 def delete_selected(self, sel):
133 model, iter = sel.get_selected()
134 if not iter:
135 rox.alert('Select a namespace binding to delete')
136 return
137 try:
138 del model[iter]
139 except:
140 rox.report_exception()
142 def add_new(self, ns):
143 d = rox.Dialog()
145 hbox = g.HBox(False, 2)
146 d.vbox.pack_start(hbox, False, True)
147 hbox.pack_start(g.Label('Prefix:'), False, True, 0)
148 prefix = g.Entry()
149 hbox.pack_start(prefix, True, True, 0)
151 hbox = g.HBox(False, 2)
152 d.vbox.pack_start(hbox, False, True)
153 hbox.pack_start(g.Label('Namespace URI:'), False, True, 0)
154 uri = g.Entry()
155 hbox.pack_start(uri, True, True, 0)
156 uri.set_activates_default(True)
158 d.vbox.show_all()
159 d.set_position(g.WIN_POS_MOUSE)
161 prefix.grab_focus()
163 d.add_button(g.STOCK_CANCEL, g.RESPONSE_CANCEL)
164 d.add_button(g.STOCK_OK, g.RESPONSE_OK)
166 d.set_default_response(g.RESPONSE_OK)
168 if d.run() != g.RESPONSE_OK:
169 d.destroy()
170 return
171 try:
172 ns[prefix.get_text()] = uri.get_text()
173 except:
174 rox.report_exception()
175 d.destroy()