PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/ diy-layout-creator --username bancika@gmail.com/diylc-library/src/org/diylc/components/tube/TubeSocket.java

http://diy-layout-creator.googlecode.com/
Java | 327 lines | 275 code | 34 blank | 18 comment | 23 complexity | e0a9bf1bb3c48807ee18aac392ae63d8 MD5 | raw file
  1. package org.diylc.components.tube;
  2. import java.awt.AlphaComposite;
  3. import java.awt.Color;
  4. import java.awt.Composite;
  5. import java.awt.Graphics2D;
  6. import java.awt.Point;
  7. import java.awt.Shape;
  8. import java.awt.geom.Area;
  9. import java.awt.geom.Ellipse2D;
  10. import org.diylc.appframework.miscutils.ConfigurationManager;
  11. import org.diylc.common.IPlugInPort;
  12. import org.diylc.common.ObjectCache;
  13. import org.diylc.common.Orientation;
  14. import org.diylc.components.AbstractTransparentComponent;
  15. import org.diylc.core.ComponentState;
  16. import org.diylc.core.IDIYComponent;
  17. import org.diylc.core.IDrawingObserver;
  18. import org.diylc.core.Project;
  19. import org.diylc.core.Theme;
  20. import org.diylc.core.VisibilityPolicy;
  21. import org.diylc.core.annotations.ComponentDescriptor;
  22. import org.diylc.core.annotations.EditableProperty;
  23. import org.diylc.core.measures.Size;
  24. import org.diylc.core.measures.SizeUnit;
  25. import org.diylc.utils.Constants;
  26. @ComponentDescriptor(name = "Tube Socket", author = "Branislav Stojkovic", category = "Tubes", instanceNamePrefix = "V", description = "Various types of tube/valve sockets", stretchable = false, zOrder = IDIYComponent.COMPONENT)
  27. public class TubeSocket extends AbstractTransparentComponent<String> {
  28. private static final long serialVersionUID = 1L;
  29. private static Color BODY_COLOR = Color.decode("#FFFFE0");
  30. private static Color BORDER_COLOR = Color.decode("#8E8E38");
  31. public static Color PIN_COLOR = Color.decode("#00B2EE");
  32. public static Color PIN_BORDER_COLOR = PIN_COLOR.darker();
  33. public static Size PIN_SIZE = new Size(1d, SizeUnit.mm);
  34. public static Size HOLE_SIZE = new Size(5d, SizeUnit.mm);
  35. public static Size TICK_SIZE = new Size(2d, SizeUnit.mm);
  36. private Base base = Base.B9A;
  37. private String type = "";
  38. private Orientation orientation = Orientation.DEFAULT;
  39. // private Mount mount = Mount.CHASSIS;
  40. private Point[] controlPoints = new Point[] { new Point(0, 0) };
  41. transient private Shape body;
  42. public TubeSocket() {
  43. super();
  44. updateControlPoints();
  45. }
  46. @EditableProperty
  47. public Base getBase() {
  48. return base;
  49. }
  50. public void setBase(Base base) {
  51. this.base = base;
  52. updateControlPoints();
  53. // Reset body shape
  54. body = null;
  55. }
  56. @EditableProperty
  57. public Orientation getOrientation() {
  58. return orientation;
  59. }
  60. public void setOrientation(Orientation orientation) {
  61. this.orientation = orientation;
  62. updateControlPoints();
  63. // Reset body shape
  64. body = null;
  65. }
  66. // @EditableProperty
  67. // public Mount getMount() {
  68. // return mount;
  69. // }
  70. //
  71. // public void setMount(Mount mount) {
  72. // this.mount = mount;
  73. // }
  74. private void updateControlPoints() {
  75. Point firstPoint = controlPoints[0];
  76. int pinCount;
  77. int pinCircleDiameter;
  78. boolean hasEmptySpace;
  79. switch (base) {
  80. case B7G:
  81. pinCount = 7;
  82. pinCircleDiameter = getClosestOdd(new Size(12d, SizeUnit.mm).convertToPixels());
  83. hasEmptySpace = true;
  84. break;
  85. case OCTAL:
  86. pinCount = 8;
  87. pinCircleDiameter = getClosestOdd(new Size(17.5d, SizeUnit.mm).convertToPixels());
  88. hasEmptySpace = false;
  89. break;
  90. case B9A:
  91. pinCount = 9;
  92. pinCircleDiameter = getClosestOdd(new Size(21d, SizeUnit.mm).convertToPixels());
  93. hasEmptySpace = true;
  94. break;
  95. default:
  96. throw new RuntimeException("Unexpected base: " + base);
  97. }
  98. double angleIncrement = Math.PI * 2 / (hasEmptySpace ? (pinCount + 1) : pinCount);
  99. double initialAngleOffset = hasEmptySpace ? angleIncrement : (angleIncrement / 2);
  100. double initialAngle;
  101. switch (orientation) {
  102. case DEFAULT:
  103. initialAngle = Math.PI / 2 + initialAngleOffset;
  104. break;
  105. case _90:
  106. initialAngle = Math.PI + initialAngleOffset;
  107. break;
  108. case _180:
  109. initialAngle = 3 * Math.PI / 2 + initialAngleOffset;
  110. break;
  111. case _270:
  112. initialAngle = initialAngleOffset;
  113. break;
  114. default:
  115. throw new RuntimeException("Unexpected orientation: " + orientation);
  116. }
  117. controlPoints = new Point[pinCount + 1];
  118. double angle = initialAngle;
  119. controlPoints[0] = firstPoint;
  120. for (int i = 0; i < pinCount; i++) {
  121. controlPoints[i + 1] = new Point((int) (firstPoint.getX() + Math.cos(angle)
  122. * pinCircleDiameter / 2), (int) (firstPoint.getY() + Math.sin(angle)
  123. * pinCircleDiameter / 2));
  124. angle += angleIncrement;
  125. }
  126. }
  127. public Shape getBody() {
  128. if (body == null) {
  129. int bodyDiameter;
  130. switch (base) {
  131. case B7G:
  132. bodyDiameter = getClosestOdd(new Size(17d, SizeUnit.mm).convertToPixels());
  133. break;
  134. case B9A:
  135. bodyDiameter = getClosestOdd(new Size(19d, SizeUnit.mm).convertToPixels());
  136. break;
  137. case OCTAL:
  138. bodyDiameter = getClosestOdd(new Size(24.5d, SizeUnit.mm).convertToPixels());
  139. break;
  140. default:
  141. throw new RuntimeException("Unexpected base: " + base);
  142. }
  143. body = new Ellipse2D.Double(controlPoints[0].x - bodyDiameter / 2, controlPoints[0].y
  144. - bodyDiameter / 2, bodyDiameter, bodyDiameter);
  145. Area bodyArea = new Area(body);
  146. int holeSize = getClosestOdd(HOLE_SIZE.convertToPixels());
  147. bodyArea.subtract(new Area(new Ellipse2D.Double(controlPoints[0].x - holeSize / 2,
  148. controlPoints[0].y - holeSize / 2, holeSize, holeSize)));
  149. if (base == Base.OCTAL) {
  150. int tickSize = getClosestOdd(TICK_SIZE.convertToPixels());
  151. double angle = 0;
  152. switch (orientation) {
  153. case DEFAULT:
  154. angle = Math.PI / 2;
  155. break;
  156. case _90:
  157. angle = Math.PI;
  158. break;
  159. case _180:
  160. angle = 3 * Math.PI / 2;
  161. break;
  162. case _270:
  163. angle = 0;
  164. break;
  165. }
  166. int centerX = (int) (controlPoints[0].x + Math.cos(angle) * holeSize / 2);
  167. int centerY = (int) (controlPoints[0].y + Math.sin(angle) * holeSize / 2);
  168. bodyArea.subtract(new Area(new Ellipse2D.Double(centerX - tickSize / 2, centerY
  169. - tickSize / 2, tickSize, tickSize)));
  170. }
  171. body = bodyArea;
  172. }
  173. return body;
  174. }
  175. @Override
  176. public void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode,
  177. Project project, IDrawingObserver drawingObserver) {
  178. // g2d.setColor(Color.black);
  179. g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1));
  180. // for (int i = 0; i < controlPoints.length; i++) {
  181. // g2d.drawString(Integer.toString(i), controlPoints[i].x,
  182. // controlPoints[i].y);
  183. // }
  184. // Draw body
  185. Shape body = getBody();
  186. Composite oldComposite = g2d.getComposite();
  187. if (alpha < MAX_ALPHA) {
  188. g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f * alpha
  189. / MAX_ALPHA));
  190. }
  191. if (componentState != ComponentState.DRAGGING) {
  192. g2d.setColor(outlineMode ? Constants.TRANSPARENT_COLOR : BODY_COLOR);
  193. g2d.fill(body);
  194. }
  195. g2d.setComposite(oldComposite);
  196. Color finalBorderColor;
  197. if (outlineMode) {
  198. Theme theme = (Theme) ConfigurationManager.getInstance().readObject(
  199. IPlugInPort.THEME_KEY, Constants.DEFAULT_THEME);
  200. finalBorderColor = componentState == ComponentState.SELECTED
  201. || componentState == ComponentState.DRAGGING ? SELECTION_COLOR : theme
  202. .getOutlineColor();
  203. } else {
  204. finalBorderColor = componentState == ComponentState.SELECTED
  205. || componentState == ComponentState.DRAGGING ? SELECTION_COLOR : BORDER_COLOR;
  206. }
  207. g2d.setColor(finalBorderColor);
  208. g2d.draw(body);
  209. // Draw pins
  210. if (!outlineMode) {
  211. int pinSize = getClosestOdd(PIN_SIZE.convertToPixels());
  212. for (int i = 1; i < controlPoints.length; i++) {
  213. g2d.setColor(PIN_COLOR);
  214. g2d.fillOval(controlPoints[i].x - pinSize / 2, controlPoints[i].y - pinSize / 2,
  215. pinSize, pinSize);
  216. g2d.setColor(PIN_BORDER_COLOR);
  217. g2d.drawOval(controlPoints[i].x - pinSize / 2, controlPoints[i].y - pinSize / 2,
  218. pinSize, pinSize);
  219. }
  220. }
  221. }
  222. @Override
  223. public void drawIcon(Graphics2D g2d, int width, int height) {
  224. Area area = new Area(new Ellipse2D.Double(1, 1, width - 2, width - 2));
  225. int center = width / 2;
  226. area.subtract(new Area(new Ellipse2D.Double(center - 2, center - 2, 5, 5)));
  227. g2d.setColor(BODY_COLOR);
  228. g2d.fill(area);
  229. g2d.setColor(BORDER_COLOR);
  230. g2d.draw(area);
  231. int radius = width / 2 - 6;
  232. for (int i = 0; i < 8; i++) {
  233. int x = (int) (center + Math.cos(i * Math.PI / 4) * radius);
  234. int y = (int) (center + Math.sin(i * Math.PI / 4) * radius);
  235. g2d.setColor(PIN_COLOR);
  236. g2d.fillOval(x - 1, y - 1, 3, 3);
  237. g2d.setColor(PIN_BORDER_COLOR);
  238. g2d.drawOval(x - 1, y - 1, 3, 3);
  239. }
  240. }
  241. @Override
  242. public Point getControlPoint(int index) {
  243. return controlPoints[index];
  244. }
  245. @Override
  246. public int getControlPointCount() {
  247. return controlPoints.length;
  248. }
  249. @Override
  250. public VisibilityPolicy getControlPointVisibilityPolicy(int index) {
  251. return VisibilityPolicy.NEVER;
  252. }
  253. @Override
  254. @EditableProperty(name = "Type")
  255. public String getValue() {
  256. return type;
  257. }
  258. @Override
  259. public void setValue(String value) {
  260. this.type = value;
  261. }
  262. @Override
  263. public boolean isControlPointSticky(int index) {
  264. return index > 0;
  265. }
  266. @Override
  267. public void setControlPoint(Point point, int index) {
  268. controlPoints[index].setLocation(point);
  269. body = null;
  270. }
  271. static enum Base {
  272. B9A("Noval B9A"), OCTAL("Octal"), B7G("Small-button B7G");
  273. String name;
  274. private Base(String name) {
  275. this.name = name;
  276. }
  277. @Override
  278. public String toString() {
  279. return name;
  280. }
  281. }
  282. static enum Mount {
  283. CHASSIS("Chassis"), PCB("PCB");
  284. String name;
  285. private Mount(String name) {
  286. this.name = name;
  287. }
  288. @Override
  289. public String toString() {
  290. return name;
  291. }
  292. }
  293. }