View Javadoc
1   /**
2    * 
3    * Copyright (c) 2013-2014, Openflexo
4    * Copyright (c) 2011-2012, AgileBirds
5    * 
6    * This file is part of Gina-swing-editor, a component of the software infrastructure 
7    * developed at Openflexo.
8    * 
9    * 
10   * Openflexo is dual-licensed under the European Union Public License (EUPL, either 
11   * version 1.1 of the License, or any later version ), which is available at 
12   * https://joinup.ec.europa.eu/software/page/eupl/licence-eupl
13   * and the GNU General Public License (GPL, either version 3 of the License, or any 
14   * later version), which is available at http://www.gnu.org/licenses/gpl.html .
15   * 
16   * You can redistribute it and/or modify under the terms of either of these licenses
17   * 
18   * If you choose to redistribute it and/or modify under the terms of the GNU GPL, you
19   * must include the following additional permission.
20   *
21   *          Additional permission under GNU GPL version 3 section 7
22   *
23   *          If you modify this Program, or any covered work, by linking or 
24   *          combining it with software containing parts covered by the terms 
25   *          of EPL 1.0, the licensors of this Program grant you additional permission
26   *          to convey the resulting work. * 
27   * 
28   * This software is distributed in the hope that it will be useful, but WITHOUT ANY 
29   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
30   * PARTICULAR PURPOSE. 
31   *
32   * See http://www.openflexo.org/license.html for details.
33   * 
34   * 
35   * Please contact Openflexo (openflexo-contacts@openflexo.org)
36   * or visit www.openflexo.org if you need additional information.
37   * 
38   */
39  
40  package org.openflexo.gina.swing.editor.view.widget;
41  
42  import java.awt.BorderLayout;
43  import java.awt.event.FocusEvent;
44  import java.awt.event.FocusListener;
45  import java.awt.event.KeyAdapter;
46  import java.awt.event.KeyEvent;
47  import java.awt.event.MouseAdapter;
48  import java.awt.event.MouseEvent;
49  import java.beans.PropertyChangeEvent;
50  import java.util.logging.Logger;
51  
52  import javax.swing.JComponent;
53  import javax.swing.JTextField;
54  import javax.swing.event.DocumentEvent;
55  import javax.swing.event.DocumentListener;
56  
57  import org.openflexo.gina.model.widget.FIBLabel;
58  import org.openflexo.gina.swing.editor.controller.FIBEditorController;
59  import org.openflexo.gina.swing.editor.view.FIBSwingEditableView;
60  import org.openflexo.gina.swing.editor.view.FIBSwingEditableViewDelegate;
61  import org.openflexo.gina.swing.view.widget.JFIBLabelWidget;
62  import org.openflexo.gina.swing.view.widget.JFIBLabelWidget.JLabelPanel;
63  import org.openflexo.logging.FlexoLogger;
64  
65  public class JFIBEditableLabelWidget extends JFIBLabelWidget implements FIBSwingEditableView<FIBLabel, JLabelPanel> {
66  
67  	@SuppressWarnings("unused")
68  	private static final Logger logger = FlexoLogger.getLogger(JFIBEditableLabelWidget.class.getPackage().getName());
69  
70  	private final FIBSwingEditableViewDelegate<FIBLabel, JLabelPanel> delegate;
71  
72  	/**
73  	 * A {@link RenderingAdapter} implementation dedicated for Swing JLabel in edition mode<br>
74  	 * (based on generic SwingTextRenderingAdapter)
75  	 * 
76  	 * @author sylvain
77  	 * 
78  	 */
79  	public static class EditableSwingLabelRenderingAdapter extends SwingLabelRenderingAdapter {
80  
81  		@Override
82  		public String getText(JLabelPanel component) {
83  			return component.getLabel().getText();
84  		}
85  
86  		@Override
87  		public void setText(JLabelPanel component, String aText) {
88  			System.out.println("??????? quelqu'un fait un set avec " + aText);
89  			super.setText(component, aText);
90  			((EditableJLabelPanel) component).getTextField().setText(aText);
91  		}
92  
93  	}
94  
95  	private final FIBEditorController editorController;
96  
97  	@Override
98  	public FIBEditorController getEditorController() {
99  		return editorController;
100 	}
101 
102 	public JFIBEditableLabelWidget(FIBLabel model, FIBEditorController editorController) {
103 		super(model, editorController.getController());
104 		this.editorController = editorController;
105 
106 		delegate = new FIBSwingEditableViewDelegate<>(this);
107 	}
108 
109 	@Override
110 	public JComponent getDraggableComponent() {
111 		return getTechnologyComponent().getLabel();
112 	}
113 
114 	@Override
115 	public synchronized void delete() {
116 		delegate.delete();
117 		super.delete();
118 	}
119 
120 	@Override
121 	public FIBSwingEditableViewDelegate<FIBLabel, JLabelPanel> getDelegate() {
122 		return delegate;
123 	}
124 
125 	@Override
126 	protected EditableJLabelPanel makeTechnologyComponent() {
127 		return new EditableJLabelPanel(this);
128 	}
129 
130 	@Override
131 	protected void updateLabel() {
132 		if (((EditableJLabelPanel) getTechnologyComponent()).isEditing()) {
133 			return;
134 		}
135 		super.updateLabel();
136 	}
137 
138 	@Override
139 	public void propertyChange(PropertyChangeEvent evt) {
140 		// System.out.println("Received event " + evt);
141 		/*if (evt.getPropertyName().equals(FIBLabel.LABEL_KEY)) {
142 			updateLabel();
143 			relayoutParentBecauseLabelChanged();
144 		}*/
145 		if (((EditableJLabelPanel) getTechnologyComponent()).isEditing()) {
146 			return;
147 		}
148 		super.propertyChange(evt);
149 	}
150 
151 	@SuppressWarnings("serial")
152 	public static class EditableJLabelPanel extends JLabelPanel {
153 
154 		private boolean isEditing = false;
155 		private JTextField textField;
156 
157 		public EditableJLabelPanel(JFIBEditableLabelWidget widget) {
158 			super(widget);
159 
160 			textField = new JTextField();
161 			textField.setBorder(null);
162 			textField.setBackground(null);
163 			textField.getDocument().addDocumentListener(new DocumentListener() {
164 				@Override
165 				public void changedUpdate(DocumentEvent e) {
166 					labelChanged();
167 				}
168 
169 				@Override
170 				public void insertUpdate(DocumentEvent e) {
171 					labelChanged();
172 				}
173 
174 				@Override
175 				public void removeUpdate(DocumentEvent e) {
176 					labelChanged();
177 				}
178 			});
179 			textField.addKeyListener(new KeyAdapter() {
180 				@Override
181 				public void keyPressed(KeyEvent e) {
182 					if (e.getKeyCode() == KeyEvent.VK_ENTER) {
183 						endEdition();
184 					}
185 				}
186 			});
187 			textField.addFocusListener(new FocusListener() {
188 
189 				@Override
190 				public void focusLost(FocusEvent e) {
191 					labelChanged();
192 					endEdition();
193 				}
194 
195 				@Override
196 				public void focusGained(FocusEvent e) {
197 				}
198 			});
199 
200 			getLabel().addMouseListener(new MouseAdapter() {
201 				@Override
202 				public void mouseClicked(MouseEvent e) {
203 					super.mouseClicked(e);
204 					if (e.getClickCount() == 2 && !isEditing) {
205 						startEdition();
206 					}
207 				}
208 			});
209 		}
210 
211 		public boolean isEditing() {
212 			return isEditing;
213 		}
214 
215 		public JTextField getTextField() {
216 			return textField;
217 		}
218 
219 		protected void startEdition() {
220 			// System.out.println("Start edition for" + getWidget().getComponent().getLabel());
221 			remove(getLabel());
222 			textField.setText(getWidget().getComponent().getLabel());
223 			textField.setFont(getLabel().getFont());
224 			add(textField, BorderLayout.CENTER);
225 			// textField.selectAll();
226 			getWidget().getRenderingAdapter().revalidateAndRepaint(this);
227 			isEditing = true;
228 		}
229 
230 		protected void endEdition() {
231 			// System.out.println("End edition du truc for " + getWidget().getComponent().getLabel());
232 			remove(textField);
233 			getLabel().setText(getWidget().getComponent().getLabel());
234 			getLabel().setFont(textField.getFont());
235 			add(getLabel(), BorderLayout.CENTER);
236 			getWidget().getRenderingAdapter().revalidateAndRepaint(this);
237 			isEditing = false;
238 		}
239 
240 		protected void labelChanged() {
241 			// System.out.println("labelChanged() called");
242 			getWidget().getComponent().setLabel(textField.getText());
243 			getParent().revalidate();
244 			getParent().repaint();
245 		}
246 
247 	}
248 
249 	private boolean operatorContentsStart = false;
250 
251 	// TODO: avoid code duplication in FIBSwingEditableView
252 	@Override
253 	public boolean isOperatorContentsStart() {
254 		return operatorContentsStart;
255 	}
256 
257 	// TODO: avoid code duplication in FIBSwingEditableView
258 	@Override
259 	public void setOperatorContentsStart(boolean operatorContentsStart) {
260 		if (operatorContentsStart != this.operatorContentsStart) {
261 			this.operatorContentsStart = operatorContentsStart;
262 			FIBSwingEditableView.updateOperatorContentsStart(this, operatorContentsStart);
263 			getPropertyChangeSupport().firePropertyChange("operatorContentsStart", !operatorContentsStart, operatorContentsStart);
264 		}
265 	}
266 
267 }