View Javadoc

1   /*
2    * Created on Apr 29, 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.widgets;
13  import java.util.ArrayList;
14  import java.util.HashMap;
15  import java.util.List;
16  import java.util.Map;
17  import org.eclipse.swt.widgets.Composite;
18  /***
19   * <p>
20   * Encapsulates multiple widgets into one.
21   * </p>
22   * <p>
23   * The widget's model is a {@link List} containing models of all child widgets,
24   * in the same order as in the {@link #getWidgets()} list. You may override this
25   * behaviour to return some composition bean or the like.
26   * </p>
27   * @author Martin Vysny
28   */
29  public class MultiWidget extends AbstractUserInputWidget {
30  	/***
31  	 * Creates the encapsulator.
32  	 * @param parent composite where all widgets are already placed
33  	 * @param widgets list of all encapsulated widgets.
34  	 */
35  	public MultiWidget(final Composite parent,
36  			final List< ? extends IUserInputWidget> widgets) {
37  		super();
38  		this.parent = parent;
39  		this.widgets = widgets;
40  		// register modify listener
41  		for (final IUserInputWidget widget : widgets) {
42  			widget.addModifyListener(modifyListener);
43  		}
44  	}
45  	/***
46  	 * Modify listener that simply fires dataModified event.
47  	 */
48  	private final IModifyListener modifyListener = new IModifyListener() {
49  		/*
50  		 * (non-Javadoc)
51  		 * @see sk.uniba.euromath.editor.widgets.IModifyListener#dataModified()
52  		 */
53  		public void dataModified() {
54  			fireDataModified();
55  		}
56  	};
57  	/***
58  	 * Returns a list of widgets provided to the constructor.
59  	 * @return list of widgets.
60  	 */
61  	public final List< ? extends IUserInputWidget> getWidgets() {
62  		return widgets;
63  	}
64  	/***
65  	 * composite where all widgets are placed
66  	 */
67  	protected final Composite parent;
68  	/***
69  	 * list of all encapsulated widgets.
70  	 */
71  	protected final List< ? extends IUserInputWidget> widgets;
72  	/*
73  	 * (non-Javadoc)
74  	 * @see sk.uniba.euromath.editor.widgets.IUserInputWidget#getComposite()
75  	 */
76  	public final Composite getComposite() {
77  		return parent;
78  	}
79  	/*
80  	 * (non-Javadoc)
81  	 * @see sk.uniba.euromath.editor.widgets.IUserInputWidget#getLastError()
82  	 */
83  	public ValidityMessages getMessages() {
84  		final Map<MessageLevelEnum, ValidityMessages> msgsLevels = new HashMap<MessageLevelEnum, ValidityMessages>();
85  		// search for first widget with errors
86  		for (final IUserInputWidget widget : widgets) {
87  			final ValidityMessages msgs = widget.getMessages();
88  			if (ValidityMessages.isError(msgs))
89  				return msgs;
90  			final MessageLevelEnum level = (msgs == null) ? null : msgs
91  					.getLevel();
92  			if ((level == MessageLevelEnum.INFO)
93  					|| (level == MessageLevelEnum.WARNING))
94  				if (msgsLevels.get(level) == null)
95  					msgsLevels.put(level, msgs);
96  		}
97  		// no error. return first warning
98  		if (msgsLevels.get(MessageLevelEnum.WARNING) != null)
99  			return msgsLevels.get(MessageLevelEnum.WARNING);
100 		return msgsLevels.get(MessageLevelEnum.INFO);
101 	}
102 	/*
103 	 * (non-Javadoc)
104 	 * @see sk.uniba.euromath.editor.widgets.IUserInputWidget#getModel()
105 	 */
106 	public List<Object> getState() {
107 		final List<Object> result = new ArrayList<Object>(widgets.size());
108 		for (final IUserInputWidget widget : widgets) {
109 			result.add(widget.getState());
110 		}
111 		return result;
112 	}
113 	/*
114 	 * (non-Javadoc)
115 	 * @see sk.uniba.euromath.editor.widgets.IUserInputWidget#getModelClass()
116 	 */
117 	public Class< ? > getStateClass() {
118 		return List.class;
119 	}
120 	/*
121 	 * (non-Javadoc)
122 	 * @see sk.uniba.euromath.editor.widgets.IUserInputWidget#setModel(java.lang.Object)
123 	 */
124 	@SuppressWarnings("unchecked")
125 	public void setState(Object model) {
126 		final List<Object> newModel = (List<Object>) model;
127 		for (int i = 0; i < newModel.size(); i++) {
128 			widgets.get(i).setState(newModel.get(i));
129 		}
130 	}
131 }