001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.soa.mappercore.model;
021:
022: import java.awt.Color;
023: import java.awt.Graphics2D;
024: import java.awt.Point;
025: import java.awt.Rectangle;
026: import java.awt.RenderingHints;
027: import java.awt.Stroke;
028: import java.awt.geom.GeneralPath;
029: import javax.swing.tree.TreePath;
030: import org.netbeans.modules.soa.mappercore.CanvasRendererContext;
031: import org.netbeans.modules.soa.mappercore.MapperStyle;
032: import org.netbeans.modules.soa.mappercore.utils.Utils;
033:
034: /**
035: *
036: * @author anjeleevich
037: */
038: public class VertexItem implements TargetPin, GraphItem {
039:
040: private Vertex vertex;
041:
042: private Link ingoingLink;
043:
044: private int x;
045: private int y;
046: private int width;
047: private int height;
048:
049: private boolean hairline;
050:
051: private Object dataObject;
052:
053: private Object value;
054: private Class valueType;
055: private String shortDescription;
056:
057: public VertexItem(Vertex vertex) {
058: this (vertex, null);
059: }
060:
061: public VertexItem(Vertex vertex, Object dataObject) {
062: this (vertex, dataObject, null, null, null, true);
063: }
064:
065: public VertexItem(Vertex vertex, String text) {
066: this (vertex, null, text, String.class, null, false);
067: }
068:
069: public VertexItem(Vertex vertex, Object value, Class valueType) {
070: this (vertex, null, value, valueType);
071: }
072:
073: public VertexItem(Vertex vertex, Object dataObject, Object value,
074: Class valueType) {
075: this (vertex, dataObject, value, valueType, null, false);
076: }
077:
078: public VertexItem(Vertex vertex, Object dataObject, Object value,
079: Class valueType, String shortDescription, boolean hairline) {
080: if (vertex == null)
081: throw new IllegalArgumentException();
082:
083: this .vertex = vertex;
084: this .hairline = hairline;
085:
086: this .value = value;
087: this .valueType = valueType;
088: this .shortDescription = shortDescription;
089:
090: this .dataObject = dataObject;
091: }
092:
093: public Object getDataObject() {
094: return dataObject;
095: }
096:
097: public void setDataObject(Object dataObject) {
098: this .dataObject = dataObject;
099: }
100:
101: public Object getValue() {
102: return value;
103: }
104:
105: public String getText() {
106: return (value == null) ? null : value.toString();
107: }
108:
109: public void setText(String text) {
110: setValue(text);
111: }
112:
113: public Class getValueType() {
114: return valueType;
115: }
116:
117: public String getShortDescription() {
118: return shortDescription;
119: }
120:
121: public void setValue(Object value) {
122: setValueAndType(value, getValueType());
123: }
124:
125: public void setValueType(Class valueType) {
126: setValueAndType(getValue(), valueType);
127: }
128:
129: public void setValueAndType(Object value, Class valueType) {
130: boolean changed = false;
131:
132: if (this .value != value) {
133: this .value = value;
134: changed = true;
135: }
136:
137: if (!Utils.equal(this .valueType, valueType)) {
138: this .valueType = valueType;
139: changed = true;
140: }
141:
142: if (changed) {
143: getVertex().fireGraphContentChanged();
144: }
145: }
146:
147: public boolean isHairline() {
148: return hairline;
149: }
150:
151: public Link getIngoingLink() {
152: return ingoingLink;
153: }
154:
155: public void setIngoingLink(Link link) {
156: if (this .ingoingLink != link) {
157: this .ingoingLink = link;
158: }
159: }
160:
161: public Vertex getVertex() {
162: return vertex;
163: }
164:
165: public void moveOnTop() {
166: getVertex().moveOnTop();
167: }
168:
169: public int getX() {
170: return x;
171: }
172:
173: public int getY() {
174: return y;
175: }
176:
177: public int getWidth() {
178: return width;
179: }
180:
181: public int getHeight() {
182: return height;
183: }
184:
185: public int getAnchorX() {
186: return vertex.getWidth();
187: }
188:
189: public int getAnchorY() {
190: return y + height / 2;
191: }
192:
193: public int getGlobalX() {
194: return vertex.getX() + x;
195: }
196:
197: public int getGlobalY() {
198: return vertex.getY() + y;
199: }
200:
201: public int getPinX() {
202: return 0;
203: }
204:
205: public int getPinY() {
206: return y + height / 2;
207: }
208:
209: public int getPinGlobalX() {
210: return vertex.getX() + getPinX();
211: }
212:
213: public int getPinGlobalY() {
214: return vertex.getY() + getPinY();
215: }
216:
217: public boolean contains(int graphX, int graphY, int step) {
218: int px = graphX - getGlobalX() * step;
219: int py = graphY - getGlobalY() * step;
220: return 0 <= px && 0 <= py && px <= width * step
221: && py <= height * step;
222: }
223:
224: public boolean targetPinContains(int graphX, int graphY, int step) {
225: int px = graphX - (getPinGlobalX() - 1) * step;
226: int py = graphY - (getPinGlobalY() * step - step / 2);
227: return 0 <= px && 0 <= py && px <= step && py <= step;
228: }
229:
230: public Rectangle getBounds() {
231: return new Rectangle(x, y, width, height);
232: }
233:
234: public Rectangle getBounds(int step) {
235: return new Rectangle(x * step, y * step, width * step, height
236: * step);
237: }
238:
239: public void paint(Graphics2D g2, TreePath treePath,
240: CanvasRendererContext rendererContext, int graphY) {
241: int step = rendererContext.getStep();
242: int x0 = rendererContext.getGraphX() + getGlobalX() * step;
243: int y0 = graphY + getGlobalY() * step;
244: int width = getWidth() * step;
245: int height = getHeight() * step;
246:
247: int labelX = x0 + 3;
248: int labelY = y0 + 1;
249: int labelHeight = height - 1;
250: int labelWidth = width - 5;
251:
252: if (labelWidth > 0) {
253: rendererContext.getCanvas().paintVertexItemText(treePath,
254: this , g2, labelX, labelY, labelWidth, labelHeight);
255: }
256: // label.setText(text);
257: // label.setHorizontalAlignment(JLabel.LEFT);
258: // label.setForeground(MapperStyle.VERTEX_ITEM_TEXT_COLOR);
259: // label.setFont(label.getFont().deriveFont(Font.PLAIN));
260: // label.setBounds(0, 0, labelWidth, labelHeight);
261: //
262: // g2.translate(labelX, labelY);
263: // label.paint(g2);
264: // g2.translate(-labelX, -labelY);
265:
266: if (rendererContext.isSelected(treePath, this )) {
267: Stroke oldStroke = g2.getStroke();
268: g2.setPaint(MapperStyle.SELECTION_COLOR);
269: g2.setStroke(MapperStyle.FOCUS_STROKE);
270: g2.drawRect(x0, y0, width, height);
271: g2.setStroke(oldStroke);
272: }
273: }
274:
275: public void paintTargetPin(Graphics2D g2, TreePath treePath,
276: CanvasRendererContext rendererContext, int graphY) {
277: int step = rendererContext.getStep();
278: int x0 = rendererContext.getGraphX() + getPinGlobalX() * step;
279: int y0 = graphY + getPinGlobalY() * step;
280:
281: Link link = getIngoingLink();
282: boolean selected = rendererContext.isSelected(treePath, this );
283:
284: if (rendererContext.paintVertexItemPin(treePath, this )) {
285: int x1 = x0 - step;
286: int x2 = x0 - 1;
287:
288: int size = step - 1;
289:
290: int y1 = y0 - size / 2;
291: int y2 = y1 + size;
292:
293: int off = (x2 - x1) / 3;
294: int d = Math.min(off, 2) * 2;
295:
296: g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
297: RenderingHints.VALUE_STROKE_PURE);
298: g2.setPaint((selected) ? MapperStyle.SELECTION_COLOR
299: : MapperStyle.PIN_BACKGROUND_COLOR);
300: g2.fillRoundRect(x1, y1, x2 - x1, y2 - y1, d, d);
301:
302: GeneralPath gp = new GeneralPath();
303: gp.moveTo(x1 + off, y1 + 0.5f);
304: gp.lineTo(x2 - off, 0.5f * (y1 + y2));
305: gp.lineTo(x1 + off, y2 - 0.5f);
306:
307: g2.setPaint(MapperStyle.PIN_FOREGROUND_COLOR);
308: g2.draw(gp);
309: g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
310: RenderingHints.VALUE_STROKE_NORMALIZE);
311: } else if (link != null
312: && rendererContext.paintLink(treePath, link)) {
313: Color color = MapperStyle.LINK_COLOR_UNSELECTED_NODE;
314:
315: if (rendererContext.isSelected(treePath)) {
316: color = (rendererContext.isSelected(treePath, link)) ? MapperStyle.SELECTION_COLOR
317: : MapperStyle.LINK_COLOR_SELECTED_NODE;
318: }
319:
320: link.paintTargetDecoration(g2, new Point(x0, y0), color,
321: step);
322: }
323: }
324:
325: public Point getTargetPinPoint(int graphX, int graphY, int step) {
326: return new Point(graphX + getPinGlobalX() * step, graphY
327: + getPinGlobalY() * step);
328: }
329:
330: void setBounds(int x, int y, int width, int height) {
331: this .x = x;
332: this .y = y;
333: this .width = width;
334: this .height = height;
335: }
336:
337: // public Navigable navigate(int direction, int modifier) {
338: // Vertex vertex = getVertex();
339: // int index = vertex.getItemIndex(this);
340: // int count = vertex.getItemCount();
341: //
342: // if (index < 0) return null;
343: //
344: // if (direction == DIRECTION_DOWN) {
345: // return (index + 1 == count) ? vertex : vertex.getItem(index + 1);
346: // }
347: //
348: // if (direction == DIRECTION_UP) {
349: // return (index == 0) ? vertex : vertex.getItem(index - 1);
350: // }
351: //
352: // if (direction == DIRECTION_LEFT || direction == DIRECTION_RIGHT) {
353: // return vertex.navigate(direction, modifier);
354: // }
355: //
356: // throw new IllegalArgumentException();
357: // }
358: }
|