View Javadoc

1   /*
2    * Created on Mar 20, 2005. Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the "License"); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
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&gt;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  		// create first page
75  		ilcPage = new InsertListChooserWizardPage(xmlAccess, insertLists,
76  				nsManager, parentName, textRule, Messages
77  						.getString("SELECT_INSERTLIST")); //$NON-NLS-1$
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 	 * (non-Javadoc)
104 	 * @see org.eclipse.jface.wizard.Wizard#canFinish()
105 	 */
106 	public boolean canFinish() {
107 		if (npqPage != null)
108 			return !npqPage.hasErrors();
109 		// we are on first page. we can finish only if this page does not
110 		// introduce no new namespaces
111 		if (ilcPage.hasErrors())
112 			return false;
113 		int selected = ilcPage.getWidget().getSelected();
114 		if (selected == -2)
115 			return true;
116 		// an insertlist should be selected; otherwise,
117 		// ilcPage.canFlipToNextPage() should return false when selected==-1
118 		assert selected >= 0;
119 		List<QName> newElementNames = ilcPage.getWidget().getInsertListNames();
120 		Set<String> allNamespaces = currentManager.getAllNamespaces();
121 		for (QName qname : newElementNames) {
122 			// if qname is null then some comboboxes are not selected. Can't
123 			// finish
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 	 * (non-Javadoc)
134 	 * @see sk.uniba.euromath.editor.wizards.IWizard#dispose()
135 	 */
136 	public void dispose() {
137 		// do nothing
138 	}
139 	/*
140 	 * (non-Javadoc)
141 	 * @see sk.uniba.euromath.editor.wizards.IWizard#hasNext()
142 	 */
143 	public boolean hasNext() {
144 		if (npqPage != null)
145 			return false;
146 		// we are on first page. we can finish only if this page does not
147 		// introduce no new namespaces
148 		if (ilcPage.hasErrors())
149 			return false;
150 		int selected = ilcPage.getWidget().getSelected();
151 		if (selected == -2)
152 			return false;
153 		// an insertlist should be selected; otherwise,
154 		// ilcPage.canFlipToNextPage() should return false when selected==-1
155 		assert selected >= 0;
156 		List<QName> newElementNames = ilcPage.getWidget().getInsertListNames();
157 		Set<String> allNamespaces = currentManager.getAllNamespaces();
158 		for (QName qname : newElementNames) {
159 			// if qname is null then some comboboxes are not selected. Can't
160 			// finish
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 	 * (non-Javadoc)
171 	 * @see sk.uniba.euromath.editor.wizards.IWizard#hasPrevious()
172 	 */
173 	public boolean hasPrevious() {
174 		return npqPage != null;
175 	}
176 	/*
177 	 * (non-Javadoc)
178 	 * @see sk.uniba.euromath.editor.wizards.IWizard#next()
179 	 */
180 	public BaseWizardPage next() {
181 		if (!hasNext())
182 			throw new NoSuchElementException();
183 		// the wizard has next page. This implies that we are on the first page,
184 		// and the second one is requested.
185 		// ok, prefixes must be queried. Create wizard page for that.
186 		Set<String> namespaces = ilcPage.getWidget().getAllNamespaces();
187 		namespaces.removeAll(currentManager.getAllNamespaces());
188 		npqPage = new NewPrefixesQueryWizardPage(namespaces, currentManager);
189 		return npqPage;
190 	}
191 	/*
192 	 * (non-Javadoc)
193 	 * @see sk.uniba.euromath.editor.wizards.IWizard#previous()
194 	 */
195 	public BaseWizardPage previous() {
196 		if (!hasPrevious())
197 			throw new NoSuchElementException();
198 		npqPage = null;
199 		return ilcPage;
200 	}
201 	/*
202 	 * (non-Javadoc)
203 	 * @see sk.uniba.euromath.editor.wizards.IWizard#performCancel()
204 	 */
205 	public boolean performCancel() {
206 		return true;
207 	}
208 	/*
209 	 * (non-Javadoc)
210 	 * @see sk.uniba.euromath.editor.wizards.IWizard#performFinish()
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 	 * (non-Javadoc)
228 	 * @see sk.uniba.euromath.editor.wizards.IWizard#current()
229 	 */
230 	public BaseWizardPage current() {
231 		if (npqPage != null)
232 			return npqPage;
233 		return ilcPage;
234 	}
235 	/*
236 	 * (non-Javadoc)
237 	 * @see sk.uniba.euromath.editor.wizards.IWizard#getName()
238 	 */
239 	public String getName() {
240 		return Messages.getString("NEW_ELEMENTS_CHOOSER"); //$NON-NLS-1$
241 	}
242 	/*
243 	 * (non-Javadoc)
244 	 * @see sk.uniba.euromath.editor.wizards.IWizard#getTitleBarColor()
245 	 */
246 	public RGB getTitleBarColor() {
247 		return null;
248 	}
249 }