1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.elementclass.button;
14 import java.awt.AlphaComposite;
16 import java.awt.FontMetrics;
17 import java.awt.Graphics2D;
18 import java.awt.geom.AffineTransform;
19 import java.awt.geom.Line2D;
20 import java.awt.geom.Rectangle2D;
23 import org.simantics.g2d.element.ElementClass;
24 import org.simantics.g2d.element.ElementHints;
25 import org.simantics.g2d.element.ElementUtils;
26 import org.simantics.g2d.element.IElement;
27 import org.simantics.g2d.element.SceneGraphNodeKey;
28 import org.simantics.g2d.element.handler.Clickable;
29 import org.simantics.g2d.element.handler.SceneGraph;
30 import org.simantics.g2d.element.handler.Stateful;
31 import org.simantics.g2d.element.handler.Text;
32 import org.simantics.g2d.element.handler.Clickable.PressStatus;
33 import org.simantics.g2d.element.handler.impl.AbstractClickable;
34 import org.simantics.g2d.element.handler.impl.AbstractTogglable;
35 import org.simantics.g2d.element.handler.impl.DefaultTransform;
36 import org.simantics.g2d.element.handler.impl.Resizeable;
37 import org.simantics.scenegraph.Node;
38 import org.simantics.scenegraph.g2d.G2DNode;
39 import org.simantics.scenegraph.g2d.G2DParentNode;
40 import org.simantics.utils.datastructures.hints.HintListenerAdapter;
41 import org.simantics.utils.datastructures.hints.IHintObservable;
42 import org.simantics.utils.datastructures.hints.IHintContext.Key;
45 * @author Toni Kalajainen
47 public class ButtonClass {
50 public static final ElementClass BUTTON_CLASS =
54 Resizeable.UNCONSTRICTED,
55 DefaultTransform.INSTANCE,
57 Stateful.ENABLED_BY_DEFAULT
62 static class ButtonPaint implements SceneGraph {
64 private static final long serialVersionUID = 5484741334876153022L;
66 public static final ButtonPaint INSTANCE = new ButtonPaint();
68 public static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE");
72 public void cleanup(IElement e) {
73 Node node = e.removeHint(SG_NODE);
79 public void init(IElement e, G2DParentNode parent) {
81 ButtonNode node = (ButtonNode) e.getHint(SG_NODE);
83 node = parent.addNode(ButtonNode.class);
84 e.setHint(SG_NODE, node);
85 e.addKeyHintListener(ElementHints.KEY_TEXT, new HintListenerAdapter() {
88 public void hintChanged(IHintObservable sender, Key key, Object oldValue,
90 if (sender instanceof IElement) {
91 ButtonNode node = (ButtonNode) ((IElement)sender).getHint(SG_NODE);
92 node.buttonText = (String)newValue;
98 e.addKeyHintListener(AbstractClickable.PRESS_STATUS_KEY, new HintListenerAdapter() {
101 public void hintChanged(IHintObservable sender, Key key, Object oldValue,
103 if (sender instanceof IElement) {
104 ButtonNode node = (ButtonNode) ((IElement)sender).getHint(SG_NODE);
105 node.status = (PressStatus)newValue;
112 Rectangle2D rect = ElementUtils.getElementBounds(e);
113 ButtonColorProfile colors = ButtonColorProfile.DEFAULT;
114 double height = rect.getHeight();
115 boolean enabled = isEnabled(e);
117 // FIXME: context not supported by scenegraph
118 PressStatus status = PressStatus.NORMAL;//ElementUtils.getPressStatus(e, ctx);
120 Font font = new Font("Tahoma", 0, (int) height-6);
121 Font selFont = new Font("Tahoma", Font.BOLD, (int) height-6);
122 Text text = e.getElementClass().getAtMostOneItemOfClass(Text.class);
123 String buttonText = null;
124 if (text!=null) buttonText = text.getText(e);
125 if (buttonText==null) buttonText = "";
126 boolean checked = isChecked(e);
128 System.out.println("ButtonClass.init " + e + " " + status + " " + checked);
129 node.init(status, rect, enabled, checked, colors, font, selFont, buttonText);
131 // e.addKeyHintListener(AbstractClickable.PRESS_STATUS_KEY, new HintListenerAdapter() {
134 // public void hintChanged(IHintObservable sender, Key key, Object oldValue,
135 // Object newValue) {
136 // if (sender instanceof IElement) {
137 // ButtonNode node = (ButtonNode) ((IElement)sender).getHint(SG_NODE);
138 // node.setPressStatus((PressStatus)newValue);
146 public static class ButtonNode extends G2DNode {
147 private static final long serialVersionUID = 2291569851866542945L;
148 PressStatus status = null;
149 Rectangle2D rect = null;
150 Boolean enabled = null;
151 Boolean checked = null;
152 ButtonColorProfile colors = null;
155 String buttonText = null;
158 public Rectangle2D getBoundsInLocal() {
162 @SyncField("pressStatus") void setPressStatus(PressStatus pressStatus) {
163 this.status = pressStatus;
166 public void init(PressStatus status, Rectangle2D rect, boolean enabled, boolean checked,
167 ButtonColorProfile colors, Font font, Font selFont, String buttonText) {
168 this.status = status;
170 this.enabled = enabled;
171 this.checked = checked;
172 this.colors = colors;
174 this.selFont = selFont;
175 this.buttonText = buttonText;
179 public void render(Graphics2D g) {
180 AffineTransform ot = g.getTransform();
183 drawNormal(g, rect, enabled, checked, colors, font, selFont, buttonText);
186 drawHeld(g, rect, enabled, colors, font, selFont, buttonText);
189 drawPressed(g, rect, enabled, colors, font, selFont, buttonText);
192 drawHover(g, rect, enabled, colors, font, selFont, buttonText);
198 protected void drawNormal(Graphics2D g, Rectangle2D rect, boolean enabled, boolean checked, ButtonColorProfile colors, Font font, Font selectedFont, String text)
201 drawPressed(g, rect, enabled, colors, font, selectedFont, text);
204 g.translate(rect.getX(), rect.getY());
205 double width = rect.getWidth();
206 double height = rect.getHeight();
208 g.setColor(colors.BACKGROUND);
209 g.fill(new Rectangle2D.Double(0, 0, width-1, height-1));
211 g.setColor(colors.BORDER1);
212 Line2D line = new Line2D.Double();
213 line.setLine(0, 0, width-1, 0); g.draw(line);
214 line.setLine(0, 0, 0, height-1); g.draw(line);
216 g.setColor(colors.BORDER2);
217 line.setLine(1, height-2, width-2, height-2); g.draw(line);
218 line.setLine(width-2, height-2, width-2, 1); g.draw(line);
220 g.setColor(colors.BORDER3);
221 line.setLine(0, height-1, width-1, height-1); g.draw(line);
222 line.setLine(width-1, height-1, width-1, 0); g.draw(line);
225 g.setColor(colors.TEXT_BLACK);
226 FontMetrics fm = g.getFontMetrics(font);
227 Rectangle2D clipRect = new Rectangle2D.Double(2, 2, width-4, height-4);
230 Rectangle2D stringRect = fm.getStringBounds(text, g);
231 float x = ((float)((width - 4) / 2)) - ((float)stringRect.getWidth()) / 2;
232 g.drawString(text, x, (float)height-6);
234 Rectangle2D clipRect = new Rectangle2D.Double(2, 2, width-4, height-4);
236 g.setFont(selectedFont);
238 FontMetrics fm = g.getFontMetrics(selectedFont);
239 Rectangle2D stringRect = fm.getStringBounds(text, g);
240 float x = ((float)((width - 4) / 2)) - ((float)stringRect.getWidth()) / 2;
242 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
243 g.setColor(colors.TEXT_WHITE);
244 g.drawString(text, x+1, (float)height-6+1);
246 g.setColor(colors.TEXT_BLACK);
247 g.drawString(text, x, (float)height-6);
251 protected void drawHeld(Graphics2D g, Rectangle2D rect, boolean enabled, ButtonColorProfile colors, Font font, Font selectedFont, String text)
253 g.translate(rect.getX(), rect.getY());
254 double width = rect.getWidth();
255 double height = rect.getHeight();
257 g.setColor(colors.BACKGROUND);
258 g.fill(new Rectangle2D.Double(0, 0, width-1, height-1));
260 Line2D line = new Line2D.Double();
262 g.setColor(colors.BORDER3);
263 line.setLine(0, 0, width-2, 0); g.draw(line);
264 line.setLine(0, 0, 0, height-2); g.draw(line);
266 g.setColor(colors.BORDER2);
267 line.setLine(1, 1, width-3, 1); g.draw(line);
268 line.setLine(1, 1, 1, height-3); g.draw(line);
270 g.setColor(colors.BORDER1);
271 line.setLine(0, height-1, width-1, height-1); g.draw(line);
272 line.setLine(width-1, height-1, width-1, 0); g.draw(line);
274 g.setColor(colors.TEXT_BLACK);
275 FontMetrics fm = g.getFontMetrics(font);
276 Rectangle2D clipRect = new Rectangle2D.Double(2, 2, width-4, height-4);
279 Rectangle2D stringRect = fm.getStringBounds(text, g);
280 float x = ((float)((width - 4) / 2)) - ((float)stringRect.getWidth()) / 2;
281 g.drawString(text, x+1, (float)height-6+1);
285 protected void drawPressed(Graphics2D g, Rectangle2D rect, boolean enabled, ButtonColorProfile colors, Font font, Font selectedFont, String text)
287 g.translate(rect.getX(), rect.getY());
288 double width = rect.getWidth();
289 double height = rect.getHeight();
291 g.setColor(colors.SELECTEDBACKGROUND);
292 g.fill(new Rectangle2D.Double(0, 0, width-1, height-1));
294 Line2D line = new Line2D.Double();
296 g.setColor(colors.BORDER3);
297 line.setLine(0, 0, width-2, 0); g.draw(line);
298 line.setLine(0, 0, 0, height-2); g.draw(line);
300 g.setColor(colors.BORDER2);
301 line.setLine(1, 1, width-3, 1); g.draw(line);
302 line.setLine(1, 1, 1, height-3); g.draw(line);
304 g.setColor(colors.BORDER1);
305 line.setLine(0, height-1, width-1, height-1); g.draw(line);
306 line.setLine(width-1, height-1, width-1, 0); g.draw(line);
309 g.setColor(colors.TEXT_BLACK);
311 FontMetrics fm = g.getFontMetrics(font);
312 Rectangle2D clipRect = new Rectangle2D.Double(2, 2, width-4, height-4);
315 Rectangle2D stringRect = fm.getStringBounds(text, g);
316 float x = ((float)((width - 4) / 2)) - ((float)stringRect.getWidth()) / 2;
317 g.drawString(text, x+1, (float)height-6+1);
319 Rectangle2D clipRect = new Rectangle2D.Double(2, 2, width-4, height-4);
321 g.setFont(selectedFont);
323 FontMetrics fm = g.getFontMetrics(selectedFont);
324 Rectangle2D stringRect = fm.getStringBounds(text, g);
325 float x = ((float)((width - 4) / 2)) - ((float)stringRect.getWidth()) / 2;
327 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
328 g.setColor(colors.TEXT_WHITE);
329 g.drawString(text, x+2, (float)height-6+2);
331 g.setColor(colors.TEXT_BLACK);
332 g.drawString(text, x+1, (float)height-6+1);
337 protected void drawHover(Graphics2D g, Rectangle2D rect, boolean enabled, ButtonColorProfile colors, Font font, Font selectedFont, String text)
339 g.translate(rect.getX(), rect.getY());
340 double width = rect.getWidth();
341 double height = rect.getHeight();
343 g.setColor(colors.HOVERBACKGROUND);
344 g.fill(new Rectangle2D.Double(0, 0, width-1, height-1));
346 //g.setStroke(HOVER_STROKE);
348 g.setColor(colors.BORDER1);
349 Line2D line = new Line2D.Double();
350 line.setLine(0, 0, width-1, 0); g.draw(line);
351 line.setLine(0, 0, 0, height-1); g.draw(line);
353 g.setColor(colors.BORDER2);
354 line.setLine(1, height-2, width-2, height-2); g.draw(line);
355 line.setLine(width-2, height-2, width-2, 1); g.draw(line);
357 g.setColor(colors.BORDER3);
358 line.setLine(0, height-1, width-1, height-1); g.draw(line);
359 line.setLine(width-1, height-1, width-1, 0); g.draw(line);
362 g.setColor(colors.TEXT_BLACK);
363 FontMetrics fm = g.getFontMetrics(font);
364 Rectangle2D clipRect = new Rectangle2D.Double(2, 2, width-4, height-4);
367 Rectangle2D stringRect = fm.getStringBounds(text, g);
368 float x = ((float)((width - 4) / 2)) - ((float)stringRect.getWidth()) / 2;
369 g.drawString(text, x, (float)height-6);
371 Rectangle2D clipRect = new Rectangle2D.Double(2, 2, width-4, height-4);
373 g.setFont(selectedFont);
375 FontMetrics fm = g.getFontMetrics(selectedFont);
376 Rectangle2D stringRect = fm.getStringBounds(text, g);
377 float x = ((float)((width - 4) / 2)) - ((float)stringRect.getWidth()) / 2;
379 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
380 g.setColor(colors.TEXT_WHITE);
381 g.drawString(text, x+1, (float)height-6+1);
383 g.setColor(colors.TEXT_BLACK);
384 g.drawString(text, x, (float)height-6);
390 protected ButtonColorProfile getColorProfile(IElement e)
392 return ButtonColorProfile.DEFAULT;
395 public boolean isEnabled(IElement e) {
396 Stateful enabled = e.getElementClass().getAtMostOneItemOfClass(Stateful.class);
397 if (enabled==null) return true;
398 return enabled.isEnabled(e);
401 public boolean isChecked(IElement e){
402 Boolean b = e.getHint(AbstractTogglable.TOGGLE_KEY);
410 static class ButtonPaint implements Paint {
412 private static final long serialVersionUID = 7339681670484169461L;
413 public static final ButtonPaint INSTANCE = new ButtonPaint();
415 public void paint(IElement e, ICanvasContext ctx, GraphicsContext elementGC, GraphicsContext controlGC) {
417 // Graphics2D g = elementGC.createClone();
418 // Rectangle2D rect = elementGC.getBounds();
419 // ButtonColorProfile colors = ButtonColorProfile.DEFAULT;
420 // double height = rect.getHeight();
421 // boolean enabled = isEnabled(e);
423 // PressStatus status = ElementUtils.getPressStatus(e, ctx);
425 // Font font = new Font("Tahoma", 0, (int) height-6);
426 // Font selFont = new Font("Tahoma", Font.BOLD, (int) height-6);
427 // Text text = e.getElementClass().getAtMostOneItemOfClass(Text.class);
428 // String buttonText = null;
429 // if (text!=null) buttonText = text.getText(e);
430 // if (buttonText==null) buttonText = "";
434 // drawNormal(g, rect, enabled, colors, font, selFont, buttonText);
437 // drawHeld(g, rect, enabled, colors, font, selFont, buttonText);
440 // drawPressed(g, rect, enabled, colors, font, selFont, buttonText);
443 // drawHover(g, rect, enabled, colors, font, selFont, buttonText);
449 protected ButtonColorProfile getColorProfile(IElement e)
451 return ButtonColorProfile.DEFAULT;
454 protected boolean isToggleButton()
459 public boolean isEnabled(IElement e) {
460 Stateful enabled = e.getElementClass().getAtMostOneItemOfClass(Stateful.class);
461 if (enabled==null) return true;
462 return enabled.isEnabled(e);
465 protected void drawNormal(Graphics2D g, Rectangle2D rect, boolean enabled, ButtonColorProfile colors, Font font, Font selectedFont, String text)
467 g.translate(rect.getX(), rect.getY());
468 double width = rect.getWidth();
469 double height = rect.getHeight();
471 g.setColor(colors.BACKGROUND);
472 g.fill(new Rectangle2D.Double(0, 0, width-1, height-1));
474 g.setColor(colors.BORDER1);
475 Line2D line = new Line2D.Double();
476 line.setLine(0, 0, width-1, 0); g.draw(line);
477 line.setLine(0, 0, 0, height-1); g.draw(line);
479 g.setColor(colors.BORDER2);
480 line.setLine(1, height-2, width-2, height-2); g.draw(line);
481 line.setLine(width-2, height-2, width-2, 1); g.draw(line);
483 g.setColor(colors.BORDER3);
484 line.setLine(0, height-1, width-1, height-1); g.draw(line);
485 line.setLine(width-1, height-1, width-1, 0); g.draw(line);
488 g.setColor(colors.TEXT_BLACK);
489 FontMetrics fm = g.getFontMetrics(font);
490 Rectangle2D clipRect = new Rectangle2D.Double(2, 2, width-4, height-4);
493 Rectangle2D stringRect = fm.getStringBounds(text, g);
494 float x = ((float)((width - 4) / 2)) - ((float)stringRect.getWidth()) / 2;
495 g.drawString(text, x, (float)height-6);
497 Rectangle2D clipRect = new Rectangle2D.Double(2, 2, width-4, height-4);
499 g.setFont(selectedFont);
501 FontMetrics fm = g.getFontMetrics(selectedFont);
502 Rectangle2D stringRect = fm.getStringBounds(text, g);
503 float x = ((float)((width - 4) / 2)) - ((float)stringRect.getWidth()) / 2;
505 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
506 g.setColor(colors.TEXT_WHITE);
507 g.drawString(text, x+1, (float)height-6+1);
509 g.setColor(colors.TEXT_BLACK);
510 g.drawString(text, x, (float)height-6);
514 protected void drawHeld(Graphics2D g, Rectangle2D rect, boolean enabled, ButtonColorProfile colors, Font font, Font selectedFont, String text)
516 g.translate(rect.getX(), rect.getY());
517 double width = rect.getWidth();
518 double height = rect.getHeight();
520 g.setColor(colors.BACKGROUND);
521 g.fill(new Rectangle2D.Double(0, 0, width-1, height-1));
523 Line2D line = new Line2D.Double();
525 g.setColor(colors.BORDER3);
526 line.setLine(0, 0, width-2, 0); g.draw(line);
527 line.setLine(0, 0, 0, height-2); g.draw(line);
529 g.setColor(colors.BORDER2);
530 line.setLine(1, 1, width-3, 1); g.draw(line);
531 line.setLine(1, 1, 1, height-3); g.draw(line);
533 g.setColor(colors.BORDER1);
534 line.setLine(0, height-1, width-1, height-1); g.draw(line);
535 line.setLine(width-1, height-1, width-1, 0); g.draw(line);
537 g.setColor(colors.TEXT_BLACK);
538 FontMetrics fm = g.getFontMetrics(font);
539 Rectangle2D clipRect = new Rectangle2D.Double(2, 2, width-4, height-4);
542 Rectangle2D stringRect = fm.getStringBounds(text, g);
543 float x = ((float)((width - 4) / 2)) - ((float)stringRect.getWidth()) / 2;
544 g.drawString(text, x+1, (float)height-6+1);
548 protected void drawPressed(Graphics2D g, Rectangle2D rect, boolean enabled, ButtonColorProfile colors, Font font, Font selectedFont, String text)
550 g.translate(rect.getX(), rect.getY());
551 double width = rect.getWidth();
552 double height = rect.getHeight();
554 g.setColor(colors.SELECTEDBACKGROUND);
555 g.fill(new Rectangle2D.Double(0, 0, width-1, height-1));
557 Line2D line = new Line2D.Double();
559 g.setColor(colors.BORDER3);
560 line.setLine(0, 0, width-2, 0); g.draw(line);
561 line.setLine(0, 0, 0, height-2); g.draw(line);
563 g.setColor(colors.BORDER2);
564 line.setLine(1, 1, width-3, 1); g.draw(line);
565 line.setLine(1, 1, 1, height-3); g.draw(line);
567 g.setColor(colors.BORDER1);
568 line.setLine(0, height-1, width-1, height-1); g.draw(line);
569 line.setLine(width-1, height-1, width-1, 0); g.draw(line);
572 g.setColor(colors.TEXT_BLACK);
574 FontMetrics fm = g.getFontMetrics(font);
575 Rectangle2D clipRect = new Rectangle2D.Double(2, 2, width-4, height-4);
578 Rectangle2D stringRect = fm.getStringBounds(text, g);
579 float x = ((float)((width - 4) / 2)) - ((float)stringRect.getWidth()) / 2;
580 g.drawString(text, x+1, (float)height-6+1);
582 Rectangle2D clipRect = new Rectangle2D.Double(2, 2, width-4, height-4);
584 g.setFont(selectedFont);
586 FontMetrics fm = g.getFontMetrics(selectedFont);
587 Rectangle2D stringRect = fm.getStringBounds(text, g);
588 float x = ((float)((width - 4) / 2)) - ((float)stringRect.getWidth()) / 2;
590 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
591 g.setColor(colors.TEXT_WHITE);
592 g.drawString(text, x+2, (float)height-6+2);
594 g.setColor(colors.TEXT_BLACK);
595 g.drawString(text, x+1, (float)height-6+1);
600 protected void drawHover(Graphics2D g, Rectangle2D rect, boolean enabled, ButtonColorProfile colors, Font font, Font selectedFont, String text)
602 g.translate(rect.getX(), rect.getY());
603 double width = rect.getWidth();
604 double height = rect.getHeight();
606 g.setColor(colors.HOVERBACKGROUND);
607 g.fill(new Rectangle2D.Double(0, 0, width-1, height-1));
609 //g.setStroke(HOVER_STROKE);
611 g.setColor(colors.BORDER1);
612 Line2D line = new Line2D.Double();
613 line.setLine(0, 0, width-1, 0); g.draw(line);
614 line.setLine(0, 0, 0, height-1); g.draw(line);
616 g.setColor(colors.BORDER2);
617 line.setLine(1, height-2, width-2, height-2); g.draw(line);
618 line.setLine(width-2, height-2, width-2, 1); g.draw(line);
620 g.setColor(colors.BORDER3);
621 line.setLine(0, height-1, width-1, height-1); g.draw(line);
622 line.setLine(width-1, height-1, width-1, 0); g.draw(line);
625 g.setColor(colors.TEXT_BLACK);
626 FontMetrics fm = g.getFontMetrics(font);
627 Rectangle2D clipRect = new Rectangle2D.Double(2, 2, width-4, height-4);
630 Rectangle2D stringRect = fm.getStringBounds(text, g);
631 float x = ((float)((width - 4) / 2)) - ((float)stringRect.getWidth()) / 2;
632 g.drawString(text, x, (float)height-6);
634 Rectangle2D clipRect = new Rectangle2D.Double(2, 2, width-4, height-4);
636 g.setFont(selectedFont);
638 FontMetrics fm = g.getFontMetrics(selectedFont);
639 Rectangle2D stringRect = fm.getStringBounds(text, g);
640 float x = ((float)((width - 4) / 2)) - ((float)stringRect.getWidth()) / 2;
642 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
643 g.setColor(colors.TEXT_WHITE);
644 g.drawString(text, x+1, (float)height-6+1);
646 g.setColor(colors.TEXT_BLACK);
647 g.drawString(text, x, (float)height-6);