]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/gallery/GalleryTooltipProvider.java
Fixed invalid comparisons which were identified by Eclipse IDE 2018-09
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / gallery / GalleryTooltipProvider.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.gallery;
13
14 import java.awt.Canvas;
15 import java.awt.Color;
16 import java.awt.Dimension;
17 import java.awt.Frame;
18 import java.awt.Graphics;
19 import java.awt.Image;
20
21 import javax.swing.BorderFactory;
22 import javax.swing.BoxLayout;
23 import javax.swing.JLabel;
24 import javax.swing.JPanel;
25
26 import org.simantics.g2d.element.IElement;
27 import org.simantics.g2d.tooltip.AWTTooltipProvider;
28 import org.simantics.utils.datastructures.hints.IHintContext.Key;
29 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
30
31 /**
32  * GalleryViewers tooltip provider
33  * 
34  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
35  *
36  */
37 public class GalleryTooltipProvider extends AWTTooltipProvider {
38         
39         public static Key TOOLTIP_TEXT = new KeyOf(String.class, "TOOLTIP_TEXT");
40         public static Key TOOLTIP_IMAGE = new KeyOf(Image.class, "TOOLTIP_IMAGE");
41
42         @Override
43         public void constructPopup(Frame frame,IElement element) {
44                 String text = element.getHint(TOOLTIP_TEXT);
45                 final Image image = element.getHint(TOOLTIP_IMAGE);
46                 if (text == null && image == null)
47                         return;
48
49                 frame.setLayout(new BoxLayout(frame, BoxLayout.X_AXIS));
50                 JPanel panel = new JPanel();
51                 panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
52                 Color c = new Color(255, 255, 220);
53                 panel.setBackground(c);
54                 panel.setBorder(BorderFactory.createLineBorder(Color.black));
55                 
56                 if (image != null) {
57                         Canvas canvas = new Canvas() {
58                                 private static final long serialVersionUID = -7357041194563374338L;
59
60                                 @Override
61                                 public void paint(Graphics g) {
62                                         super.paint(g);
63                                         g.drawImage(image, 0, 0, null);
64                                 }
65                                 
66                                 @Override
67                                 public Dimension getSize() {
68                                         return new Dimension(image.getWidth(null), image.getHeight(null));
69                                 }
70                                 
71                                 @Override
72                                 public Dimension getSize(Dimension rv) {
73                                         if (rv == null)
74                                                 rv = new Dimension();
75                                         rv.width = image.getWidth(null);
76                                         rv.height = image.getHeight(null);
77                                         return rv;
78                                 }
79                         };
80                         panel.add(canvas);
81                 }
82                 if (text != null) {
83                         JLabel label = new JLabel(text);
84                         panel.add(label);
85                 }
86                 frame.add(panel);
87
88         }
89
90 }