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.container;
41  
42  import java.awt.BorderLayout;
43  import java.awt.Dimension;
44  import java.awt.Graphics;
45  import java.beans.PropertyChangeEvent;
46  import java.util.List;
47  import java.util.logging.Logger;
48  
49  import javax.swing.BorderFactory;
50  import javax.swing.JComponent;
51  import javax.swing.JLabel;
52  import javax.swing.JPanel;
53  
54  import org.openflexo.gina.model.FIBModelObject;
55  import org.openflexo.gina.model.bindings.RuntimeContext;
56  import org.openflexo.gina.model.operator.FIBIteration;
57  import org.openflexo.gina.swing.editor.controller.FIBEditorController;
58  import org.openflexo.gina.swing.editor.view.FIBSwingEditableContainerView;
59  import org.openflexo.gina.swing.editor.view.FIBSwingEditableContainerViewDelegate;
60  import org.openflexo.gina.swing.editor.view.FIBSwingEditableView;
61  import org.openflexo.gina.swing.editor.view.OperatorDecorator;
62  import org.openflexo.gina.swing.editor.view.PlaceHolder;
63  import org.openflexo.gina.swing.view.container.JFIBIterationView;
64  import org.openflexo.logging.FlexoLogger;
65  import org.openflexo.toolbox.StringUtils;
66  
67  public class JFIBEditableIterationView extends JFIBIterationView implements FIBSwingEditableContainerView<FIBIteration, JPanel> {
68  
69  	private static final Logger logger = FlexoLogger.getLogger(JFIBEditableIterationView.class.getPackage().getName());
70  
71  	private final FIBSwingEditableContainerViewDelegate<FIBIteration, JPanel> delegate;
72  
73  	private final FIBEditorController editorController;
74  
75  	@Override
76  	public JComponent getDraggableComponent() {
77  		return getTechnologyComponent();
78  	}
79  
80  	@Override
81  	public FIBEditorController getEditorController() {
82  		return editorController;
83  	}
84  
85  	public JFIBEditableIterationView(final FIBIteration model, FIBEditorController editorController, RuntimeContext context) {
86  		super(model, editorController.getController(), context);
87  		this.editorController = editorController;
88  		delegate = new FIBSwingEditableContainerViewDelegate<>(this);
89  	}
90  
91  	@Override
92  	public boolean handleIteration() {
93  		// In edit mode, we don't want to have to iterate
94  		return false;
95  	}
96  
97  	private JLabel iterationInfoLabel;
98  
99  	@Override
100 	protected JPanel makeTechnologyComponent() {
101 		if (getComponent().getSubComponents().size() == 0) {
102 			JPanel returned = new JPanel(new BorderLayout());
103 			returned.add(iterationInfoLabel = new JLabel(
104 					(StringUtils.isNotEmpty(getComponent().getName()) ? getComponent().getName() : "Iteration")));
105 			returned.add(iterationInfoLabel, BorderLayout.CENTER);
106 			returned.setBorder(BorderFactory.createEmptyBorder(0, FIBSwingEditableView.OPERATOR_ICON_SPACE, 0, 0));
107 			return returned;
108 		}
109 		else {
110 			return null;
111 		}
112 	}
113 
114 	@Override
115 	public void propertyChange(PropertyChangeEvent evt) {
116 		if (evt.getPropertyName().equals(FIBModelObject.NAME_KEY)) {
117 			if (iterationInfoLabel != null) {
118 				iterationInfoLabel.setText(StringUtils.isNotEmpty(getComponent().getName()) ? getComponent().getName() : "Iteration");
119 			}
120 		}
121 		super.propertyChange(evt);
122 	}
123 
124 	@Override
125 	public JComponent getJComponent() {
126 		return getTechnologyComponent();
127 	}
128 
129 	@Override
130 	public JComponent getResultingJComponent() {
131 		return getTechnologyComponent();
132 	}
133 
134 	@Override
135 	public void delete() {
136 		delegate.delete();
137 		super.delete();
138 	}
139 
140 	@Override
141 	public FIBSwingEditableContainerViewDelegate<FIBIteration, JPanel> getDelegate() {
142 		return delegate;
143 	}
144 
145 	@Override
146 	protected void paintAdditionalInfo(Graphics g) {
147 		if (getDelegate().getPlaceholders() != null) {
148 			for (PlaceHolder ph : getDelegate().getPlaceholders()) {
149 				ph.paint(g);
150 			}
151 		}
152 	}
153 
154 	/*@Override
155 	public FIBLayoutManager<JPanel, JComponent, ?> makeFIBLayoutManager(Layout layoutType) {
156 		if (layoutType == null) {
157 			return super.makeFIBLayoutManager(layoutType);
158 		}
159 		switch (layoutType) {
160 			case none:
161 				return new JAbsolutePositionningLayout(this);
162 			case border:
163 				return new JEditableBorderLayout(this);
164 			case box:
165 				return new JEditableBoxLayout(this);
166 			case flow:
167 				return new JEditableFlowLayout(this);
168 			case buttons:
169 				return new JButtonLayout(this);
170 			case twocols:
171 				return new JEditableTwoColsLayout(this);
172 			case grid:
173 				return new JEditableGridLayout(this);
174 			case gridbag:
175 				return new JEditableGridBagLayout(this);
176 			default:
177 				return super.makeFIBLayoutManager(layoutType);
178 		}
179 	
180 	}*/
181 
182 	/*@Override
183 	public List<PlaceHolder> makePlaceHolders(Dimension preferredSize) {
184 	
185 		List<PlaceHolder> returned = new ArrayList<>();
186 		returned.add(new PlaceHolder(this, getComponent().getName() != null ? getComponent().getName() : "Iteration") {
187 	
188 			@Override
189 			public void insertComponent(FIBComponent newComponent, int originalIndex) {
190 				System.out.println("Adding inside iteration");
191 				getComponent().addToSubComponents(newComponent);
192 			}
193 	
194 		});
195 	
196 		return returned;
197 		// return Collections.emptyList();
198 	}*/
199 
200 	@Override
201 	public List<PlaceHolder> makePlaceHolders(Dimension preferredSize) {
202 		return null;
203 	}
204 
205 	@Override
206 	public List<OperatorDecorator> makeOperatorDecorators() {
207 		return null;
208 	}
209 
210 	private boolean operatorContentsStart = false;
211 
212 	// TODO: avoid code duplication in FIBSwingEditableView
213 	@Override
214 	public boolean isOperatorContentsStart() {
215 		return operatorContentsStart;
216 	}
217 
218 	// TODO: avoid code duplication in FIBSwingEditableView
219 	@Override
220 	public void setOperatorContentsStart(boolean operatorContentsStart) {
221 		if (operatorContentsStart != this.operatorContentsStart) {
222 			this.operatorContentsStart = operatorContentsStart;
223 			FIBSwingEditableView.updateOperatorContentsStart(this, operatorContentsStart);
224 			getPropertyChangeSupport().firePropertyChange("operatorContentsStart", !operatorContentsStart, operatorContentsStart);
225 		}
226 	}
227 
228 }