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