View Javadoc
1   /**
2    * 
3    * Copyright (c) 2013-2014, Openflexo
4    * Copyright (c) 2012-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.Dimension;
43  import java.awt.Graphics;
44  import java.awt.Rectangle;
45  import java.util.ArrayList;
46  import java.util.Collections;
47  import java.util.List;
48  import java.util.logging.Logger;
49  
50  import javax.swing.JComponent;
51  
52  import org.openflexo.gina.model.FIBComponent;
53  import org.openflexo.gina.model.container.FIBMultiSplitLayoutFactory.FIBDivider;
54  import org.openflexo.gina.model.container.FIBMultiSplitLayoutFactory.FIBLeaf;
55  import org.openflexo.gina.model.container.FIBMultiSplitLayoutFactory.FIBNode;
56  import org.openflexo.gina.model.container.FIBMultiSplitLayoutFactory.FIBRowSplit;
57  import org.openflexo.gina.model.container.FIBMultiSplitLayoutFactory.FIBSplit;
58  import org.openflexo.gina.model.container.FIBSplitPanel;
59  import org.openflexo.gina.model.container.layout.SplitLayoutConstraints;
60  import org.openflexo.gina.swing.editor.controller.FIBEditorController;
61  import org.openflexo.gina.swing.editor.view.FIBSwingEditableContainerView;
62  import org.openflexo.gina.swing.editor.view.FIBSwingEditableContainerViewDelegate;
63  import org.openflexo.gina.swing.editor.view.FIBSwingEditableView;
64  import org.openflexo.gina.swing.editor.view.OperatorDecorator;
65  import org.openflexo.gina.swing.editor.view.PlaceHolder;
66  import org.openflexo.gina.swing.view.container.JFIBSplitPanelView;
67  import org.openflexo.logging.FlexoLogger;
68  import org.openflexo.swing.layout.JXMultiSplitPane;
69  import org.openflexo.swing.layout.MultiSplitLayout.Node;
70  
71  public class JFIBEditableSplitPanelView extends JFIBSplitPanelView
72  		implements FIBSwingEditableContainerView<FIBSplitPanel, JXMultiSplitPane> {
73  
74  	private static final Logger logger = FlexoLogger.getLogger(JFIBEditableSplitPanelView.class.getPackage().getName());
75  
76  	private final FIBSwingEditableContainerViewDelegate<FIBSplitPanel, JXMultiSplitPane> delegate;
77  
78  	private final FIBEditorController editorController;
79  
80  	@Override
81  	public JComponent getDraggableComponent() {
82  		return getTechnologyComponent();
83  	}
84  
85  	@Override
86  	public FIBEditorController getEditorController() {
87  		return editorController;
88  	}
89  
90  	public JFIBEditableSplitPanelView(FIBSplitPanel model, FIBEditorController editorController) {
91  		super(model, editorController.getController());
92  		this.editorController = editorController;
93  		delegate = new FIBSwingEditableContainerViewDelegate<>(this);
94  	}
95  
96  	@Override
97  	public void delete() {
98  		delegate.delete();
99  		super.delete();
100 	}
101 
102 	@Override
103 	protected void paintAdditionalInfo(Graphics g) {
104 		if (getDelegate().getPlaceholders() != null) {
105 			for (PlaceHolder ph : getDelegate().getPlaceholders()) {
106 				ph.paint(g);
107 			}
108 		}
109 	}
110 
111 	private void appendPlaceHolder(final FIBLeaf n, Rectangle current, List<PlaceHolder> placeHolders) {
112 		boolean found = false;
113 		for (FIBComponent subComponent : getComponent().getSubComponents()) {
114 			if (n.getName().equals(((SplitLayoutConstraints) subComponent.getConstraints()).getSplitIdentifier())) {
115 				found = true;
116 			}
117 		}
118 		if (!found) {
119 			final SplitLayoutConstraints splitLayoutConstraints = SplitLayoutConstraints.makeSplitLayoutConstraints(n.getName());
120 			PlaceHolder newPlaceHolder = new PlaceHolder(this, "<" + n.getName() + ">", current) {
121 				@Override
122 				public void insertComponent(FIBComponent newComponent, int oldIndex) {
123 					System.out.println("getComponent=" + JFIBEditableSplitPanelView.this.getComponent());
124 					JFIBEditableSplitPanelView.this.getComponent().addToSubComponents(newComponent, splitLayoutConstraints);
125 				}
126 			};
127 			newPlaceHolder.setVisible(false);
128 			placeHolders.add(newPlaceHolder);
129 		}
130 	}
131 
132 	private void appendPlaceHolders(FIBSplit<?> s, Rectangle current, List<PlaceHolder> placeHolders) {
133 		int size = 0;
134 		for (FIBNode<?> node : s.getChildren()) {
135 			if (!(node instanceof FIBDivider)) {
136 				size += 1;
137 			}
138 		}
139 
140 		boolean vertical = s instanceof FIBRowSplit;
141 		int width = vertical ? current.width / size : current.width;
142 		int height = vertical ? current.height : current.height / size;
143 
144 		int x = 0, y = 0;
145 		for (FIBNode<?> node : s.getChildren()) {
146 			if (!(node instanceof FIBDivider)) {
147 				Rectangle childSize = new Rectangle(x, y, width, height);
148 				appendPlaceHolders(node, childSize, placeHolders);
149 				if (vertical) {
150 					x += width;
151 				}
152 				else {
153 					y += height;
154 				}
155 			}
156 		}
157 	}
158 
159 	private void appendPlaceHolders(Node<?> n, Rectangle current, List<PlaceHolder> placeHolders) {
160 		if (n instanceof FIBSplit) {
161 			appendPlaceHolders((FIBSplit<?>) n, current, placeHolders);
162 		}
163 		else if (n instanceof FIBLeaf) {
164 			appendPlaceHolder((FIBLeaf) n, current, placeHolders);
165 		}
166 	}
167 
168 	@Override
169 	public FIBSwingEditableContainerViewDelegate<FIBSplitPanel, JXMultiSplitPane> getDelegate() {
170 		return delegate;
171 	}
172 
173 	@Override
174 	public List<PlaceHolder> makePlaceHolders(Dimension preferredSize) {
175 		List<PlaceHolder> result = new ArrayList<>();
176 		Dimension size = getJComponent().getSize();
177 		Rectangle origin = new Rectangle(0, 0, size.width, size.height);
178 		appendPlaceHolders(this.getLayout().getModel(), origin, result);
179 		return result;
180 	}
181 
182 	@Override
183 	public List<OperatorDecorator> makeOperatorDecorators() {
184 		return Collections.emptyList();
185 	}
186 
187 	private boolean operatorContentsStart = false;
188 
189 	// TODO: avoid code duplication in FIBSwingEditableView
190 	@Override
191 	public boolean isOperatorContentsStart() {
192 		return operatorContentsStart;
193 	}
194 
195 	// TODO: avoid code duplication in FIBSwingEditableView
196 	@Override
197 	public void setOperatorContentsStart(boolean operatorContentsStart) {
198 		if (operatorContentsStart != this.operatorContentsStart) {
199 			this.operatorContentsStart = operatorContentsStart;
200 			FIBSwingEditableView.updateOperatorContentsStart(this, operatorContentsStart);
201 			getPropertyChangeSupport().firePropertyChange("operatorContentsStart", !operatorContentsStart, operatorContentsStart);
202 		}
203 	}
204 
205 }