1 /*
2 * Copyright 1999-2006 Faculty of Mathematics, Physics
3 * and Informatics, Comenius University, Bratislava. This file is protected by
4 * the Mozilla Public License version 1.1 (the License); you may not use this
5 * file except in compliance with the License. You may obtain a copy of the
6 * License at http://euromath2.sourceforge.net/license.html Unless required by
7 * applicable law or agreed to in writing, software distributed under the
8 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9 * OF ANY KIND, either express or implied. See the License for the specific
10 * language governing permissions and limitations under the License.
11 */
12 package sk.uniba.euromath.foRenderer.figures;
13
14 import org.apache.fop.area.Area;
15 import org.eclipse.draw2d.Figure;
16 import org.eclipse.draw2d.geometry.Rectangle;
17
18 import sk.uniba.euromath.editor.figures.IEMFigure;
19 import sk.uniba.euromath.foRenderer.config.GlobalFOPFigureCfg;
20
21 /***
22 * @author TV Created on 14.1.2004
23 *
24 */
25 public class BasicFOPFigure extends Figure implements FOPFigure, IEMFigure {
26
27 /***
28 * Stores id node associated with this figure.
29 */
30 private final String id;
31
32 protected final Area area;
33
34 private Boolean needsEditPart = true;
35
36 // Object from object source or Node from transformed document
37 private Object model = null;
38
39 /***
40 * Constructor
41 *
42 * @param area
43 * Area object from FOP
44 */
45 public BasicFOPFigure(Area area) {
46 super();
47 // Ids were preprocessed by FOP so they contains unqueing sufix
48 // ,N
49 String fullId = FOPFigureBuilder.retrieveID(area);
50 if ((fullId != null) && (fullId.indexOf(",") != -1))
51 this.id = fullId.substring(0, fullId.indexOf(","));
52 else
53 this.id = fullId;
54
55 if (area == null)
56 throw new IllegalArgumentException("null area"); //$NON-NLS-1$
57 setOpaque(false);
58 this.area = area;
59 }
60
61 /***
62 * Constructor
63 *
64 * @param area
65 * Area object from FOP
66 * @param needsEditPart
67 * <code>true</code> if Figure is editable
68 * @param model
69 * model for the editPart
70 */
71 public BasicFOPFigure(Area area, Boolean needsEditPart, Object model) {
72 this(area);
73 this.needsEditPart = needsEditPart;
74 this.model = model;
75 }
76
77 /***
78 * Returns Area from that was this figure created
79 *
80 * @return Area
81 */
82 public Area getArea() {
83 return this.area;
84 }
85
86 /***
87 * @return model that will be model of the editPart
88 */
89 public Object getModel() {
90 return this.model;
91 }
92
93 /***
94 * @return <code>true</code> if to this figure is needet to create
95 * EditPart and then this figure can be edited
96 */
97 public boolean needsEditPart() {
98 return this.needsEditPart;
99 }
100
101 /***
102 * @see sk.uniba.euromath.fop.figures.FOPFigure#getID()
103 * @return value of id trait
104 */
105 public String getID() {
106 return this.id;
107 }
108
109 /***
110 * @param key
111 * @return configuration object
112 */
113 public Object getConfig(Object key) {
114 return GlobalFOPFigureCfg.getConfig().getData(key,
115 this.getClass());
116 }
117
118 /*
119 * protected boolean isOn(Object key){ Object data = getConfig(key); if
120 * (data != null) return data.equals(FOViewConsts.ON);
121 *
122 * return false; } protected Color getMarkingColor() { RGB rgb =
123 * (RGB)getConfig(FOViewConsts.BOUNDING_RGB); if (rgb != null) { return
124 * new Color(null, rgb); } else return new Color(null,
125 * ColorConstants.green.getRGB()); }
126 *
127 * public static String getCoordinatesString(IFigure f, boolean
128 * clientArea){ Rectangle r; if (clientArea) r= f.getClientArea(); else
129 * r = f.getBounds();
130 *
131 * return "L: [" + r.x + "," + r.y + "]\nS: [" + r.width + "," +
132 * r.height + "]"; }
133 *
134 * private static final String X_POS = "__X__"; private static final
135 * String Y_POS = "__Y__"; private static final String W_SIZ = "__W__";
136 * private static final String H_SIZ = "__H__";
137 *
138 * public static String getCoordinatesString(IFigure f, String mask){
139 * Rectangle r = f.getBounds(); String result = mask;
140 *
141 * if (result.indexOf(X_POS) >= 0){ result.replaceAll(X_POS,
142 * Integer.toString(r.x)); } if (result.indexOf(Y_POS) >= 0){
143 * result.replaceAll(Y_POS, Integer.toString(r.y)); } if
144 * (result.indexOf(W_SIZ) >= 0){ result.replaceAll(W_SIZ,
145 * Integer.toString(r.width)); } if (result.indexOf(H_SIZ) >= 0){
146 * result.replaceAll(H_SIZ, Integer.toString(r.height)); } return
147 * result; }
148 *
149 * protected void paintBoundingBox(Graphics g){ Color old =
150 * g.getForegroundColor(); Color markClr = getMarkingColor();
151 * g.setForegroundColor(markClr); Rectangle r = getBounds(); //
152 * BUG_REPORT_DONE: Argument is not valid !!! // (marking color disposed
153 * in LineAreaFigure) g.drawRectangle(r.x, r.y , r.width-1, r.height-1);
154 * markClr.dispose(); g.setForegroundColor(old); }
155 *
156 * protected void paintFigure(Graphics g) { super.paintFigure(g); if
157 * (isOn(FOViewConsts.BOUNDING_BOX)) paintBoundingBox(g); if
158 * (isOn(FOViewConsts.BOUNDING_TXT)) paintSizeData(g, -1); } // position =
159 * 0 --> left // position = 1 --> center // position = 2 --> right
160 *
161 * private void paintSizeData(Graphics g, int position) {
162 *
163 * if (position < 0) return;
164 *
165 * String str = BasicFOPFigure.getCoordinatesString(this, true);
166 *
167 * g.translate(getClientArea().getLocation()); g.drawText(str, 1, 1);
168 * g.translate(getClientArea().getLocation().getNegated()); }
169 */
170
171 /***
172 * Sets size of the Figure, x and y coordinates leaves as was.
173 *
174 * @param r
175 * Rectangle with new bounds
176 */
177 public void setPixelDimensions(Rectangle r) {
178 setSize(r.width, r.height);
179 }
180
181 @Override
182 public String toString() {
183 return "[id: " + this.id + "]";
184 }
185
186 }