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.logging.Logger;
50  
51  import javax.swing.JLabel;
52  import javax.swing.SwingConstants;
53  
54  import org.openflexo.gina.model.FIBComponent;
55  import org.openflexo.logging.FlexoLogger;
56  
57  public abstract class PlaceHolder {
58  
59  	static final Logger logger = FlexoLogger.getLogger(PlaceHolder.class.getPackage().getName());
60  
61  	private final FIBSwingEditableContainerView<?, ?> view;
62  	private final String text;
63  
64  	private final Rectangle bounds;
65  
66  	public PlaceHolder(FIBSwingEditableContainerView<?, ?> view, String text, Rectangle bounds) {
67  		this.view = view;
68  		this.text = text;
69  		JLabel label = new JLabel(text);
70  		label.setForeground(Color.DARK_GRAY);
71  		label.setHorizontalAlignment(SwingConstants.CENTER);
72  		label.setVerticalAlignment(SwingConstants.CENTER);
73  		this.bounds = new Rectangle(bounds);
74  		visible = false;
75  	}
76  
77  	public PlaceHolder(FIBSwingEditableContainerView<?, ?> view, String text) {
78  		this(view, text, new Rectangle(200, 200));
79  	}
80  
81  	public Rectangle getBounds() {
82  		return bounds;
83  	}
84  
85  	private boolean visible;
86  
87  	public boolean isVisible() {
88  		return visible;
89  	}
90  
91  	public void setVisible(boolean visible) {
92  		this.visible = visible;
93  	}
94  
95  	public FIBSwingEditableContainerView<?, ?> getView() {
96  		return view;
97  	}
98  
99  	@Override
100 	public String toString() {
101 		return "PlaceHolder:[" + text + "]";
102 	}
103 
104 	/**
105 	 * Insert supplied component in placeholder<br>
106 	 * If this is a move, originalIndex stores the index of component when move was initialized
107 	 * 
108 	 * @param newComponent
109 	 * @param originalIndex
110 	 */
111 	public abstract void insertComponent(FIBComponent newComponent, int originalIndex);
112 
113 	public void willDelete() {
114 	}
115 
116 	public void hasDeleted() {
117 	}
118 
119 	public void paint(Graphics g) {
120 
121 		Graphics2D g2 = (Graphics2D) g;
122 		float alpha = isVisible() ? 0.2f : 0.1f;
123 		int type = AlphaComposite.SRC_OVER;
124 		AlphaComposite composite = AlphaComposite.getInstance(type, alpha);
125 		g2.setComposite(composite);
126 		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
127 
128 		g2.setColor(isVisible() ? Color.GREEN : Color.YELLOW);
129 		g2.fillRoundRect(bounds.x + 1, bounds.y + 1, bounds.width - 2, bounds.height - 2, 15, 15);
130 		g2.setStroke(new BasicStroke(2.0f));
131 		g2.setColor(Color.GRAY);
132 		g2.drawRoundRect(bounds.x + 1, bounds.y + 1, bounds.width - 2, bounds.height - 2, 15, 15);
133 
134 	}
135 
136 }