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;
41  
42  import java.awt.AlphaComposite;
43  import java.awt.BasicStroke;
44  import java.awt.Color;
45  import java.awt.Graphics;
46  import java.awt.Graphics2D;
47  import java.awt.Rectangle;
48  import java.awt.RenderingHints;
49  import java.util.List;
50  import java.util.logging.Logger;
51  
52  import javax.swing.JLabel;
53  import javax.swing.SwingConstants;
54  
55  import org.openflexo.gina.model.FIBOperator;
56  import org.openflexo.gina.swing.editor.controller.FIBEditorIconLibrary;
57  import org.openflexo.gina.swing.view.JFIBView;
58  import org.openflexo.gina.view.FIBOperatorView;
59  import org.openflexo.gina.view.FIBView;
60  import org.openflexo.logging.FlexoLogger;
61  
62  public class OperatorDecorator {
63  
64  	static final Logger logger = FlexoLogger.getLogger(OperatorDecorator.class.getPackage().getName());
65  
66  	private final FIBSwingEditableContainerView<?, ?> containerView;
67  	private final FIBOperatorView<?, ?, ?> operatorView;
68  	private final List<FIBView<?, ?>> subViews;
69  	private final FIBOperator operator;
70  
71  	public OperatorDecorator(FIBSwingEditableContainerView<?, ?> containerView, FIBOperatorView<?, ?, ?> operatorView,
72  			List<FIBView<?, ?>> subViews) {
73  		this.containerView = containerView;
74  		this.operatorView = operatorView;
75  		this.operator = operatorView.getComponent();
76  		JLabel label = new JLabel(operator.getName());
77  		label.setForeground(Color.DARK_GRAY);
78  		label.setHorizontalAlignment(SwingConstants.CENTER);
79  		label.setVerticalAlignment(SwingConstants.CENTER);
80  		this.subViews = subViews;
81  
82  		if (subViews == null || subViews.size() == 0) {
83  			System.out.println("Pas normal ca");
84  			Thread.dumpStack();
85  		}
86  	}
87  
88  	public List<FIBView<?, ?>> getSubViews() {
89  		return subViews;
90  	}
91  
92  	public Rectangle getBounds() {
93  		Rectangle bounds = null;
94  		for (FIBView<?, ?> jfibView : getSubViews()) {
95  			Rectangle contentsBounds = ((JFIBView<?, ?>) jfibView).getResultingJComponent().getBounds();
96  			if (bounds == null) {
97  				bounds = contentsBounds;
98  			}
99  			else {
100 				bounds = bounds.union(contentsBounds);
101 			}
102 		}
103 		if (bounds != null) {
104 			return new Rectangle(bounds.x - 4, bounds.y - 4, bounds.width + 8, bounds.height + 8);
105 		}
106 		return null;
107 	}
108 
109 	public FIBOperator getOperator() {
110 		return operator;
111 	}
112 
113 	public FIBSwingEditableContainerView<?, ?> getContainerView() {
114 		return containerView;
115 	}
116 
117 	public FIBOperatorView<?, ?, ?> getOperatorView() {
118 		return operatorView;
119 	}
120 
121 	@Override
122 	public String toString() {
123 		return "OperatorDecorator:[" + getOperator() + "]";
124 	}
125 
126 	public void paint(Graphics g) {
127 
128 		Graphics2D g2 = (Graphics2D) g;
129 
130 		Rectangle bounds = getBounds();
131 		if (bounds == null) {
132 			return;
133 		}
134 
135 		g2.drawImage(FIBEditorIconLibrary.ITERATION_ICON.getImage(), bounds.x + 4, bounds.y + 4 /*bounds.height / 2 - 8*/, null);
136 
137 		float alpha = 0.1f;
138 		int type = AlphaComposite.SRC_OVER;
139 		AlphaComposite composite = AlphaComposite.getInstance(type, alpha);
140 		g2.setComposite(composite);
141 		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
142 
143 		g2.setColor(Color.YELLOW);
144 		g2.fillRect(bounds.x + 1, bounds.y + 1, bounds.width - 2, bounds.height - 2);
145 		g2.setStroke(new BasicStroke(1.0f));
146 		g2.setColor(Color.BLACK);
147 		g2.drawRect(bounds.x + 1, bounds.y + 1, bounds.width - 2, bounds.height - 2);
148 
149 	}
150 
151 }