Start recording automatically after creating a new block.
[dom-editor.git] / Dome / Namespaces.py
blob3c7f704f01166c076c0813b635d2e5b2cac3764a
1 from xml.dom import XMLNS_NAMESPACE, XML_NAMESPACE
2 from constants import DOME_NS
4 import rox
5 from rox import g
7 fixed_ns = {'xml': XML_NAMESPACE, 'xmlns': XMLNS_NAMESPACE,
8 'dome': DOME_NS}
10 common_ns = {'http://www.w3.org/1999/xhtml': 'xhtml',
11 'http://www.w3.org/1999/02/22-rdf-syntax-ns#': 'rdf',
12 'http://xmlns.4suite.org/ext': 'ft',
13 'http://www.w3.org/1999/XSL/Transform': 'xsl',
14 'http://www.w3.org/1999/XMLSchema': 'xsd',
15 'http://www.w3.org/1999/XMLSchema-instance': 'xsi',
16 'http://schemas.xmlsoap.org/soap/envelope/': 'env',
17 'http://www.w3.org/1999/xlink': 'xlink'}
19 class Namespaces(g.GenericTreeModel):
20 def __init__(self):
21 g.GenericTreeModel.__init__(self)
22 self.uri = dict(fixed_ns)
23 self.update_list()
25 def update_list(self):
26 self.list = self.uri.keys()
27 self.list.sort()
28 pre = self.prefix = {}
29 for p, u in self.uri.iteritems():
30 pre[u] = p
32 def on_get_n_columns(self):
33 return 2
35 def on_get_iter(self, path):
36 assert len(path) == 1
37 return path[0]
39 def on_get_value(self, iter, column):
40 if column == 0:
41 return self.list[iter]
42 return self.uri[self.list[iter]]
44 def on_iter_nth_child(self, iter, n):
45 if iter == None:
46 return n
47 return None
49 def on_get_column_type(self, col):
50 return str
52 def on_iter_has_child(self, iter):
53 return False
55 def on_iter_next(self, iter):
56 if iter < len(self.list) - 1:
57 return iter + 1
59 def __setitem__(self, prefix, uri):
60 if prefix in self.uri and self.uri[prefix] == uri:
61 return
63 assert prefix
64 if prefix in fixed_ns:
65 raise Exception('That namespace prefix cannot be changed')
66 if uri in self.prefix:
67 raise Exception('That namespace already has a prefix (%s)' % self.prefix[uri])
69 modifed = prefix in self.uri
71 self.uri[prefix] = uri
72 self.update_list()
73 path = (self.list.index(prefix),)
75 if modifed:
76 self.emit('row-changed', path, self.get_iter(path))
77 else:
78 self.emit('row-inserted', path, self.get_iter(path))
80 def __delitem__(self, iter):
81 prefix = self[iter][0]
82 if prefix in fixed_ns:
83 raise Exception('This is a built-in namespace and cannot be deleted')
84 path = (self.list.index(prefix),)
85 del self.uri[prefix]
86 self.update_list()
87 self.emit('row-deleted', path)
89 def ensure_ns(self, suggested_prefix, uri):
90 """Return the prefix for this URI. If none is set choose one (using suggested_prefix
91 if possible)."""
92 try:
93 return self.prefix[uri]
94 except KeyError:
95 if not suggested_prefix:
96 suggested_prefix = common_ns.get(uri, 'ns')
97 if suggested_prefix in self.uri:
98 x = 1
99 while suggested_prefix + `x` in self.uri: x += 1
100 suggested_prefix += `x`
101 self[suggested_prefix] = uri
102 #print "Added", suggested_prefix, uri
103 return suggested_prefix
105 def to_xml(self, doc):
106 node = doc.createElementNS(DOME_NS, 'dome:namespaces')
107 for prefix, uri in self.uri.iteritems():
108 if prefix in fixed_ns: continue
109 ns = doc.createElementNS(DOME_NS, 'dome:ns')
110 ns.setAttributeNS(None, 'prefix', prefix)
111 ns.setAttributeNS(None, 'uri', uri)
112 node.appendChild(ns)
113 return node
115 class GUI(rox.Dialog):
116 def __init__(self, model):
117 rox.Dialog.__init__(self)
118 self.model = model
120 self.add_button(g.STOCK_ADD, 1)
121 self.add_button(g.STOCK_DELETE, 2)
122 self.add_button(g.STOCK_CLOSE, g.RESPONSE_OK)
124 tree = g.TreeView(model.namespaces)
126 def response(dialog, resp):
127 if resp == 1:
128 dialog.add_new(model.namespaces)
129 elif resp == 2:
130 dialog.delete_selected(tree.get_selection())
131 else:
132 dialog.destroy()
133 self.connect('response', response)
134 self.set_position(g.WIN_POS_MOUSE)
135 self.set_title('Namespaces for ' + `model.uri`)
137 frame = g.Frame()
138 frame.add(tree)
139 frame.set_shadow_type(g.SHADOW_IN)
141 cell = g.CellRendererText()
142 column = g.TreeViewColumn('Prefix', cell, text = 0)
143 tree.append_column(column)
145 column = g.TreeViewColumn('URI', cell, text = 1)
146 tree.append_column(column)
148 frame.show_all()
150 self.vbox.pack_start(frame, True, True)
151 self.set_default_size(400, 200)
153 self.set_has_separator(False)
155 def delete_selected(self, sel):
156 model, iter = sel.get_selected()
157 if not iter:
158 rox.alert('Select a namespace binding to delete')
159 return
160 try:
161 del model[iter]
162 except:
163 rox.report_exception()
165 def add_new(self, ns):
166 d = rox.Dialog()
168 hbox = g.HBox(False, 2)
169 d.vbox.pack_start(hbox, False, True)
170 hbox.pack_start(g.Label('Prefix:'), False, True, 0)
171 prefix = g.Entry()
172 hbox.pack_start(prefix, True, True, 0)
174 def check_prefix(entry):
175 if not uri.get_text():
176 pre = prefix.get_text()
177 for u, p in common_ns.iteritems():
178 if p == pre:
179 uri.set_text(u)
180 break
181 uri.grab_focus()
183 prefix.connect('activate', check_prefix)
185 hbox = g.HBox(False, 2)
186 d.vbox.pack_start(hbox, False, True)
187 hbox.pack_start(g.Label('Namespace URI:'), False, True, 0)
188 uri = g.Entry()
189 hbox.pack_start(uri, True, True, 0)
190 uri.set_activates_default(True)
192 d.vbox.show_all()
193 d.set_position(g.WIN_POS_MOUSE)
195 prefix.grab_focus()
197 d.add_button(g.STOCK_CANCEL, g.RESPONSE_CANCEL)
198 d.add_button(g.STOCK_OK, g.RESPONSE_OK)
200 d.set_default_response(g.RESPONSE_OK)
202 while 1:
203 if d.run() != g.RESPONSE_OK:
204 d.destroy()
205 return
206 try:
207 ns[prefix.get_text()] = uri.get_text()
208 break
209 except:
210 rox.report_exception()
211 d.destroy()