1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor.wizards.document;
13 import java.util.List;
14 import java.util.NoSuchElementException;
15 import java.util.Set;
16 import javax.xml.namespace.QName;
17 import org.eclipse.swt.graphics.RGB;
18 import sk.uniba.euromath.document.NamespaceManager;
19 import sk.uniba.euromath.document.XMLAccess;
20 import sk.uniba.euromath.document.schema.InsertList;
21 import sk.uniba.euromath.document.schema.plug.IValueRule;
22 import sk.uniba.euromath.editor.lang.Messages;
23 import sk.uniba.euromath.editor.wizards.BaseWizardPage;
24 import sk.uniba.euromath.editor.wizards.IWizard;
25 import sk.uniba.euromath.tools.StringTools;
26 /***
27 * <p>
28 * Allows the user to choose between multiple insertlists, and to choose the
29 * name of each element. These user settings are returned. Alternatively, user
30 * may choose to enter some text if it is permitted by the rule.
31 * </p>
32 * <p>
33 * If required, then new prefixes are queried aswell.
34 * </p>
35 * @author Martin Vysny
36 */
37 public class InsertListChooserWizard implements IWizard {
38 /***
39 * First wizard page. Never <code>null</code>.
40 */
41 public final InsertListChooserWizardPage ilcPage;
42 /***
43 * Second wizard page. <code>null</code> when wizard is on the first page.
44 * Must not be changed.
45 */
46 private NewPrefixesQueryWizardPage npqPage;
47 /***
48 * Returns second wizard page. <code>null</code> when wizard is on the
49 * first page.
50 * @return second wizard page instance.
51 */
52 public NewPrefixesQueryWizardPage getNpqPage() {
53 return npqPage;
54 }
55 /***
56 * The constructor.
57 * @param xmlAccess the XML Access instance.
58 * @param insertLists the list of choosable insertlists.
59 * @param nsManager the map of namespace>prefix mapping. It will not get
60 * modified. If <code>null</code> then manager from <code>xmlAccess</code>
61 * will be used.
62 * @param parentName the displayable qname of the parent. It is only
63 * displayed in a window as a text - it is not used in other way.
64 * @param textRule if not <code>null</code> then it is possible to choose
65 * a text value also. This value must comply this rule.
66 * @param name the wizard title. Each page will receive this title.
67 */
68 public InsertListChooserWizard(XMLAccess xmlAccess,
69 List<InsertList> insertLists, NamespaceManager nsManager,
70 String parentName, IValueRule textRule, String name) {
71 super();
72 this.currentManager = nsManager;
73 this.title = name;
74
75 ilcPage = new InsertListChooserWizardPage(xmlAccess, insertLists,
76 nsManager, parentName, textRule, Messages
77 .getString("SELECT_INSERTLIST"));
78 }
79 /***
80 * Current namespace manager.
81 */
82 protected final NamespaceManager currentManager;
83 /***
84 * The title.
85 */
86 protected final String title;
87 /***
88 * Creates new namespace manager, with new namespace-prefix mappings. If no
89 * new mappings are defined, function simply returns old namespace manager.
90 * @return new namespace manager, or instance given to the object
91 * constructor if no new namespaces are introduced.
92 * @throws IllegalArgumentException if the page contains errors thus some
93 * prefixes cannot be registered.
94 * @throws IllegalStateException if the page contains errors thus some
95 * prefixes cannot be registered.
96 */
97 public NamespaceManager newNsManager() {
98 if (npqPage == null)
99 return currentManager;
100 return npqPage.npq.newUpdatedManager();
101 }
102
103
104
105
106 public boolean canFinish() {
107 if (npqPage != null)
108 return !npqPage.hasErrors();
109
110
111 if (ilcPage.hasErrors())
112 return false;
113 int selected = ilcPage.getWidget().getSelected();
114 if (selected == -2)
115 return true;
116
117
118 assert selected >= 0;
119 List<QName> newElementNames = ilcPage.getWidget().getInsertListNames();
120 Set<String> allNamespaces = currentManager.getAllNamespaces();
121 for (QName qname : newElementNames) {
122
123
124 if (qname == null)
125 return false;
126 if ((StringTools.nullStr(qname.getNamespaceURI()) != null)
127 && !allNamespaces.contains(qname.getNamespaceURI()))
128 return false;
129 }
130 return true;
131 }
132
133
134
135
136 public void dispose() {
137
138 }
139
140
141
142
143 public boolean hasNext() {
144 if (npqPage != null)
145 return false;
146
147
148 if (ilcPage.hasErrors())
149 return false;
150 int selected = ilcPage.getWidget().getSelected();
151 if (selected == -2)
152 return false;
153
154
155 assert selected >= 0;
156 List<QName> newElementNames = ilcPage.getWidget().getInsertListNames();
157 Set<String> allNamespaces = currentManager.getAllNamespaces();
158 for (QName qname : newElementNames) {
159
160
161 if (qname == null)
162 return false;
163 if ((StringTools.nullStr(qname.getNamespaceURI()) != null)
164 && !allNamespaces.contains(qname.getNamespaceURI()))
165 return true;
166 }
167 return false;
168 }
169
170
171
172
173 public boolean hasPrevious() {
174 return npqPage != null;
175 }
176
177
178
179
180 public BaseWizardPage next() {
181 if (!hasNext())
182 throw new NoSuchElementException();
183
184
185
186 Set<String> namespaces = ilcPage.getWidget().getAllNamespaces();
187 namespaces.removeAll(currentManager.getAllNamespaces());
188 npqPage = new NewPrefixesQueryWizardPage(namespaces, currentManager);
189 return npqPage;
190 }
191
192
193
194
195 public BaseWizardPage previous() {
196 if (!hasPrevious())
197 throw new NoSuchElementException();
198 npqPage = null;
199 return ilcPage;
200 }
201
202
203
204
205 public boolean performCancel() {
206 return true;
207 }
208
209
210
211
212 public boolean performFinish() {
213 return true;
214 }
215 /***
216 * Returns qnames of new elements, with appropriate prefixes when needed.
217 * Throws exception when no insertlist is selected.
218 * @return qnames of new elements. Never <code>null</code>.
219 */
220 public List<QName> getNames() {
221 List<QName> qnames = ilcPage.getWidget().getInsertListNames();
222 if ((npqPage == null) || (npqPage.npq == null))
223 return qnames;
224 return npqPage.npq.setPrefixes(qnames);
225 }
226
227
228
229
230 public BaseWizardPage current() {
231 if (npqPage != null)
232 return npqPage;
233 return ilcPage;
234 }
235
236
237
238
239 public String getName() {
240 return Messages.getString("NEW_ELEMENTS_CHOOSER");
241 }
242
243
244
245
246 public RGB getTitleBarColor() {
247 return null;
248 }
249 }