View Javadoc
1   /**
2    * 
3    * Copyright (c) 2014, Openflexo
4    * 
5    * This file is part of Gina-swing, a component of the software infrastructure 
6    * developed at Openflexo.
7    * 
8    * 
9    * Openflexo is dual-licensed under the European Union Public License (EUPL, either 
10   * version 1.1 of the License, or any later version ), which is available at 
11   * https://joinup.ec.europa.eu/software/page/eupl/licence-eupl
12   * and the GNU General Public License (GPL, either version 3 of the License, or any 
13   * later version), which is available at http://www.gnu.org/licenses/gpl.html .
14   * 
15   * You can redistribute it and/or modify under the terms of either of these licenses
16   * 
17   * If you choose to redistribute it and/or modify under the terms of the GNU GPL, you
18   * must include the following additional permission.
19   *
20   *          Additional permission under GNU GPL version 3 section 7
21   *
22   *          If you modify this Program, or any covered work, by linking or 
23   *          combining it with software containing parts covered by the terms 
24   *          of EPL 1.0, the licensors of this Program grant you additional permission
25   *          to convey the resulting work. * 
26   * 
27   * This software is distributed in the hope that it will be useful, but WITHOUT ANY 
28   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
29   * PARTICULAR PURPOSE. 
30   *
31   * See http://www.openflexo.org/license.html for details.
32   * 
33   * 
34   * Please contact Openflexo (openflexo-contacts@openflexo.org)
35   * or visit www.openflexo.org if you need additional information.
36   * 
37   */
38  
39  package org.openflexo.gina.swing.utils;
40  
41  import java.awt.BorderLayout;
42  import java.awt.Color;
43  import java.awt.Dimension;
44  import java.awt.event.ActionEvent;
45  import java.awt.event.ActionListener;
46  import java.util.logging.Level;
47  
48  import javax.swing.JButton;
49  
50  import org.openflexo.connie.DataBinding;
51  import org.openflexo.gina.model.FIBModelObject.FIBModelObjectImpl;
52  import org.openflexo.swing.ButtonsControlPanel;
53  import org.openflexo.toolbox.ToolBox;
54  
55  /**
56   * This panel is the way a DataBinding is edited when it takes the form of a complex expression (responds true to
57   * {@link DataBinding#isExpression()}). This panel is always instanciated in the context of a {@link BindingSelector}
58   * 
59   * @author sylvain
60   * 
61   */
62  @SuppressWarnings("serial")
63  public class BindingExpressionSelectorPanel extends AbstractBindingSelectorPanel {
64  
65  	private final BindingSelector bindingSelector;
66  	protected ButtonsControlPanel _controlPanel;
67  	protected JButton _applyButton;
68  	protected JButton _cancelButton;
69  	protected JButton _resetButton;
70  	private BindingExpressionPanel _expressionPanel;
71  
72  	/**
73  	 * @param bindingSelector
74  	 */
75  	BindingExpressionSelectorPanel(BindingSelector bindingSelector) {
76  		super();
77  		this.bindingSelector = bindingSelector;
78  	}
79  
80  	@Override
81  	public void delete() {
82  		if (_expressionPanel != null) {
83  			_expressionPanel.delete();
84  			_expressionPanel = null;
85  		}
86  	}
87  
88  	@Override
89  	public Dimension getDefaultSize() {
90  		return new Dimension(520, 250);
91  	}
92  
93  	@Override
94  	protected void init() {
95  		setLayout(new BorderLayout());
96  
97  		_controlPanel = new ButtonsControlPanel() {
98  			@Override
99  			public String localizedForKeyAndButton(String key, JButton component) {
100 				return FIBModelObjectImpl.GINA_LOCALIZATION.localizedForKey(key, component);
101 			}
102 		};
103 		_applyButton = _controlPanel.addButton("apply", new ActionListener() {
104 			@Override
105 			public void actionPerformed(ActionEvent e) {
106 				BindingExpressionSelectorPanel.this.bindingSelector.apply();
107 			}
108 		});
109 		_cancelButton = _controlPanel.addButton("cancel", new ActionListener() {
110 			@Override
111 			public void actionPerformed(ActionEvent e) {
112 				BindingExpressionSelectorPanel.this.bindingSelector.cancel();
113 			}
114 		});
115 		_resetButton = _controlPanel.addButton("reset", new ActionListener() {
116 			@Override
117 			public void actionPerformed(ActionEvent e) {
118 				BindingExpressionSelectorPanel.this.bindingSelector.getEditedObject().reset();
119 				BindingExpressionSelectorPanel.this.bindingSelector.apply();
120 			}
121 		});
122 
123 		_controlPanel.applyFocusTraversablePolicyTo(_controlPanel, false);
124 
125 		// BindingExpression newBindingExpression = new BindingExpression(new BindingExpression.BindingValueVariable(new
126 		// Variable("parent")));
127 
128 		/*if (bindingSelector.getEditedObject().getExpression() == null || !(bindingSelector.getEditedObject().isExpression())) {
129 			BindingSelector.logger.severe("Unexpected object " + bindingSelector.getEditedObject().getClass().getSimpleName()
130 					+ " found instead of BindingExpression");
131 			bindingSelector.getEditedObject().setExpression(bindingSelector.makeBindingExpression());
132 		}*/
133 
134 		/*if (_bindingSelector.getEditedObject() == null || ((BindingExpression) _bindingSelector.getEditedObject()).getExpression() == null) {
135 			_bindingSelector._editedObject = _bindingSelector.makeBindingExpression();
136 		}*/
137 
138 		if (BindingSelector.LOGGER.isLoggable(Level.FINE)) {
139 			BindingSelector.LOGGER.fine("init() called in BindingExpressionSelectorPanel with " + bindingSelector.getEditedObject()
140 					+ " expression=" + bindingSelector.getEditedObject().getExpression());
141 		}
142 
143 		_expressionPanel = new BindingExpressionPanel(bindingSelector.getEditedObject()) {
144 			@Override
145 			protected void fireEditedExpressionChanged(DataBinding<?> expression) {
146 				// Called when the binding represented by the panel has changed
147 				super.fireEditedExpressionChanged(expression);
148 				bindingSelector.fireEditedObjectChanged();
149 				updateStatus(expression);
150 
151 			}
152 		};
153 
154 		add(_expressionPanel, BorderLayout.CENTER);
155 		add(_controlPanel, BorderLayout.SOUTH);
156 
157 		update();
158 	}
159 
160 	@Override
161 	protected void processTabPressed() {
162 		// TODO Auto-generated method stub
163 
164 	}
165 
166 	@Override
167 	protected void processBackspace() {
168 		// TODO Auto-generated method stub
169 
170 	}
171 
172 	@Override
173 	protected void processDelete() {
174 		// TODO Auto-generated method stub
175 
176 	}
177 
178 	@Override
179 	protected void processDownPressed() {
180 		// TODO Auto-generated method stub
181 
182 	}
183 
184 	@Override
185 	protected void processEnterPressed() {
186 		// TODO Auto-generated method stub
187 
188 	}
189 
190 	@Override
191 	protected void processUpPressed() {
192 		// TODO Auto-generated method stub
193 
194 	}
195 
196 	@Override
197 	protected void synchronizePanelWithTextFieldValue(String textValue) {
198 	}
199 
200 	@Override
201 	protected void update() {
202 		if (BindingSelector.LOGGER.isLoggable(Level.FINE)) {
203 			BindingSelector.LOGGER.fine("update() called for BindingExpressionSelectorPanel");
204 		}
205 
206 		_expressionPanel.setEditedExpression(bindingSelector.getEditedObject());
207 
208 		updateStatus(bindingSelector.getEditedObject());
209 
210 	}
211 
212 	@Override
213 	protected void updateStatus(DataBinding<?> bindingExpression) {
214 
215 		boolean isValid = bindingExpression.forceRevalidate();
216 
217 		// Update apply button state
218 		_applyButton.setEnabled(bindingExpression != null && isValid);
219 		if (bindingExpression != null && isValid) {
220 			if (ToolBox.isMacOSLaf()) {
221 				_applyButton.setSelected(true);
222 			}
223 		}
224 		if (bindingExpression != null) {
225 			bindingSelector.getTextField().setForeground(bindingExpression.isValid() ? Color.BLACK : Color.RED);
226 		}
227 	}
228 
229 	@Override
230 	protected void fireBindableChanged() {
231 		update();
232 	}
233 
234 	@Override
235 	protected void willApply() {
236 	}
237 }