View Javadoc

1   /*
2    * Created on May 18, 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.dialogs;
13  
14  import java.util.ArrayList;
15  import java.util.List;
16  import java.util.Map;
17  import java.util.Set;
18  import org.eclipse.swt.widgets.Composite;
19  import org.eclipse.swt.widgets.Shell;
20  import org.w3c.dom.Attr;
21  import org.w3c.dom.Entity;
22  import sk.baka.ikslibs.ref.EntityManager;
23  import sk.baka.xml.gene.exportgraph.ExportGraph;
24  import sk.baka.xml.gene.exportgraph.GraphNode;
25  import sk.baka.xml.schematic.rules.AttributeRule;
26  import sk.baka.xml.schematic.rules.INameList;
27  import sk.baka.xml.schematic.rules.NewElementRule;
28  import sk.uniba.euromath.document.NamespaceManager;
29  import sk.uniba.euromath.document.XMLAccess;
30  import sk.uniba.euromath.editor.lang.Messages;
31  import sk.uniba.euromath.editor.widgets.EntityList;
32  import sk.uniba.euromath.editor.widgets.GraphSelectorWidget;
33  import sk.uniba.euromath.editor.widgets.namelist.DisplayableNameList;
34  import sk.uniba.euromath.editor.widgets.namelist.NameListItemChooser;
35  
36  /***
37   * Instantiate various dialogs.
38   * 
39   * @author Martin Vysny
40   */
41  public final class Dialogs {
42      /***
43       * Constructor.
44       */
45      private Dialogs() {
46          // prevent instantiation.
47      }
48  
49      /***
50       * Constructs an instance of a dialog, that will be used to create an
51       * attribute. Component will not create the attribute itself.
52       * 
53       * @param parent
54       *            the parent widget. should not be <code>null</code>.
55       * @param nameList
56       *            list of attributes that can be created.
57       * @param xmlAccess
58       *            the <code>XMLAccess</code> instance.
59       * @param nsManager
60       *            the namespace manager. If <code>null</code> then the one
61       *            from <code>xmlAccess</code> will be used.
62       * @return namelist item chooser, intented for creating an attribute
63       */
64      public static WidgetWrapperDialog<NameListItemChooser<AttributeRule>> createAttributeCreator(
65              Shell parent, final INameList<AttributeRule> nameList,
66              final XMLAccess xmlAccess, final NamespaceManager nsManager) {
67          final IWidgetFactory<NameListItemChooser<AttributeRule>> factory = new IWidgetFactory<NameListItemChooser<AttributeRule>>() {
68              /*
69               * (non-Javadoc)
70               * 
71               * @see sk.uniba.euromath.editor.dialogs.IWidgetFactory#create(org.eclipse.swt.widgets.Composite)
72               */
73              public NameListItemChooser<AttributeRule> create(Composite parent) {
74                  return NameListItemChooser.createAttributeCreator(parent,
75                          nameList, xmlAccess, nsManager);
76              }
77          };
78          return new WidgetWrapperDialog<NameListItemChooser<AttributeRule>>(
79  				parent, Messages.getString("CREATE_ATTRIBUTE"), //$NON-NLS-1$
80  				Messages.getString("CREATE_ATTRIBUTE"), factory); //$NON-NLS-1$
81      }
82  
83      /***
84  	 * Constructs an instance of a dialog, that will be used to modify an
85  	 * attribute value. Component will not modify the attribute itself.
86  	 * @param parent the parent widget. should not be <code>null</code>.
87  	 * @param attr the attribute. The window will not change the attribute value
88  	 * itself.
89  	 * @param rule the rule for the attribute.
90  	 * @param xmlAccess the <code>XMLAccess</code> instance.
91  	 * @param nsManager namespace manager. If <code>null</code> then the one
92  	 * from <code>xmlAccess</code> will be used.
93  	 * @return namelist item chooser, intented for modifying attribute value
94  	 * only
95  	 */
96      public static WidgetWrapperDialog<NameListItemChooser<AttributeRule>> createAttributeEditor(
97              Shell parent, final Attr attr, final AttributeRule rule,
98              final XMLAccess xmlAccess, final NamespaceManager nsManager) {
99          final IWidgetFactory<NameListItemChooser<AttributeRule>> factory = new IWidgetFactory<NameListItemChooser<AttributeRule>>() {
100             /*
101              * (non-Javadoc)
102              * 
103              * @see sk.uniba.euromath.editor.dialogs.IWidgetFactory#create(org.eclipse.swt.widgets.Composite)
104              */
105             public NameListItemChooser<AttributeRule> create(Composite parent) {
106                 return NameListItemChooser.createAttributeEditor(parent, attr,
107                         rule, xmlAccess, nsManager);
108             }
109         };
110         return new WidgetWrapperDialog<NameListItemChooser<AttributeRule>>(
111 				parent, Messages.getString("MODIFY_ATTRIBUTE"), //$NON-NLS-1$
112 				Messages.getString("MODIFY_ATTRIBUTE_DESC"), factory); //$NON-NLS-1$
113     }
114 
115     /***
116 	 * Constructs an instance of a dialog, that will be used to select an
117 	 * entity. Component will not modify the attribute itself.
118 	 * @param parent the parent widget. should not be <code>null</code>.
119 	 * @param xmlAccess the <code>XMLAccess</code> instance.
120 	 * @param caption caption of the dialog.
121 	 * @param filter filters out unwanted entities. If <code>null</code> then
122 	 * no entities are filtered out.
123 	 * @return an entity selector.
124 	 */
125     public static WidgetWrapperDialog<EntityList> createEntityLister(
126             Shell parent, final XMLAccess xmlAccess, String caption,
127             final IEntityFilter filter) {
128         final IWidgetFactory<EntityList> factory = new IWidgetFactory<EntityList>() {
129             /*
130              * (non-Javadoc)
131              * 
132              * @see sk.uniba.euromath.editor.dialogs.IWidgetFactory#create(org.eclipse.swt.widgets.Composite)
133              */
134             public EntityList create(Composite parent) {
135                 // create a list of entities that shall be displayed.
136                 final EntityManager m = xmlAccess.getEntityManager();
137                 final List<String> entityNames = new ArrayList<String>(m
138                         .getEntityNames().size());
139                 for (String name : m.getEntityNames()) {
140                     final Entity e = m.getEntity(name);
141                     if ((filter == null) || filter.accept(name, e))
142                         entityNames.add(name);
143                 }
144                 return new EntityList(parent, m, entityNames);
145             }
146         };
147         return new WidgetWrapperDialog<EntityList>(parent, caption, caption, factory);
148     }
149 
150     /***
151      * Constructs an instance of a dialog, that will be used to select an
152      * entity. Component will not modify the attribute itself.
153      * 
154      * @param parent
155      *            the parent widget. should not be <code>null</code>.
156      * @param xmlAccess
157      *            the <code>XMLAccess</code> instance.
158      * @param caption
159      *            caption of the dialog.
160      * @param entityNames
161      *            only these entities are shown.
162      * @return an entity selector.
163      */
164     public static WidgetWrapperDialog<EntityList> createEntityLister(
165             Shell parent, final XMLAccess xmlAccess, String caption,
166             final List<? extends String> entityNames) {
167         final IWidgetFactory<EntityList> factory = new IWidgetFactory<EntityList>() {
168             /*
169              * (non-Javadoc)
170              * 
171              * @see sk.uniba.euromath.editor.dialogs.IWidgetFactory#create(org.eclipse.swt.widgets.Composite)
172              */
173             public EntityList create(Composite parent) {
174                 return new EntityList(parent, xmlAccess.getEntityManager(),
175                         entityNames);
176             }
177         };
178         return new WidgetWrapperDialog<EntityList>(parent, caption, caption, factory);
179     }
180 
181     /***
182      * Constructs an instance of a dialog, that will be used to select an
183      * element name. Component will not modify the attribute itself.
184      * 
185      * @param parent
186      *            the parent widget. should not be <code>null</code>.
187      * @param xmlAccess
188      *            the <code>XMLAccess</code> instance.
189      * @param caption
190      *            dialog caption
191      * @param dnl
192      *            name list holding element names.
193      * @return an entity selector.
194      */
195     public static WidgetWrapperDialog<NameListItemChooser<NewElementRule>> createElementCreator(
196             Shell parent, final XMLAccess xmlAccess, final String caption,
197             final DisplayableNameList<NewElementRule> dnl) {
198         final IWidgetFactory<NameListItemChooser<NewElementRule>> factory = new IWidgetFactory<NameListItemChooser<NewElementRule>>() {
199             /*
200              * (non-Javadoc)
201              * 
202              * @see sk.uniba.euromath.editor.dialogs.IWidgetFactory#create(org.eclipse.swt.widgets.Composite)
203              */
204             public NameListItemChooser<NewElementRule> create(Composite parent) {
205                 return new NameListItemChooser<NewElementRule>(parent,
206                         xmlAccess, dnl, Messages
207                                 .getString("SELECT_ELEMENT_NAME"), //$NON-NLS-1$
208                         xmlAccess.getNsManager());
209             }
210         };
211         return new WidgetWrapperDialog<NameListItemChooser<NewElementRule>>(
212                 parent, caption, caption, factory);
213     }
214 
215     /***
216      * Creates dialog that allows you to choose exporters.
217      * 
218      * @param parent
219      *            the parent widget. Should not be <code>null</code>.
220      * @param editableChoices
221      *            all choice nodes. May be empty or <code>null</code>.
222      * @param graph
223      *            offer this graph to user. Must not be empty.
224      * @return the export graph chooser dialog instance.
225      * @throws IllegalArgumentException
226      *             if graphList or editableChoices is empty, or editableChoices
227      *             refers to non-choosable nodes.
228      */
229     public static WidgetWrapperDialog<GraphSelectorWidget> createGraphSelector(
230             final Shell parent,
231             final Map<GraphNode, Set<String>> editableChoices,
232             final ExportGraph graph) {
233         final IWidgetFactory<GraphSelectorWidget> factory = new IWidgetFactory<GraphSelectorWidget>() {
234             /*
235              * (non-Javadoc)
236              * 
237              * @see sk.uniba.euromath.editor.dialogs.IWidgetFactory#create(org.eclipse.swt.widgets.Composite)
238              */
239             public GraphSelectorWidget create(Composite parent) {
240                 return new GraphSelectorWidget(parent, editableChoices, graph);
241             }
242         };
243         return new WidgetWrapperDialog<GraphSelectorWidget>(parent, Messages
244                 .getString("PRESENTATION_SELECTOR"), Messages //$NON-NLS-1$
245                 .getString("PRESENTATION_SELECTOR_DESC"), factory); //$NON-NLS-1$
246     }
247 }