~ubuntu-branches/ubuntu/vivid/drmips/vivid-backports

« back to all changes in this revision

Viewing changes to src/pc/DrMIPS/src/org/fife/ui/rsyntaxtextarea/focusabletip/TipUtil.java

  • Committer: Package Import Robot
  • Author(s): Bruno Nova
  • Date: 2014-09-27 12:24:17 UTC
  • Revision ID: package-import@ubuntu.com-20140927122417-2gadkwt9k0u7j4zu
Tags: upstream-1.2.3
Import upstream version 1.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * 08/13/2009
 
3
 *
 
4
 * TipUtil.java - Utility methods for homemade tool tips.
 
5
 * 
 
6
 * This library is distributed under a modified BSD license.  See the included
 
7
 * RSyntaxTextArea.License.txt file for details.
 
8
 */
 
9
package org.fife.ui.rsyntaxtextarea.focusabletip;
 
10
 
 
11
import java.awt.Color;
 
12
import java.awt.Font;
 
13
import java.awt.GraphicsConfiguration;
 
14
import java.awt.GraphicsDevice;
 
15
import java.awt.GraphicsEnvironment;
 
16
import java.awt.Rectangle;
 
17
import java.awt.SystemColor;
 
18
import javax.swing.BorderFactory;
 
19
import javax.swing.JEditorPane;
 
20
import javax.swing.UIManager;
 
21
import javax.swing.border.Border;
 
22
import javax.swing.plaf.ColorUIResource;
 
23
import javax.swing.text.html.HTMLDocument;
 
24
 
 
25
 
 
26
/**
 
27
 * Static utility methods for focusable tips.
 
28
 *
 
29
 * @author Robert Futrell
 
30
 * @version 1.0
 
31
 */
 
32
public class TipUtil {
 
33
 
 
34
 
 
35
        private TipUtil() {
 
36
        }
 
37
 
 
38
 
 
39
        /**
 
40
         * Returns a hex string for the specified color, suitable for HTML.
 
41
         *
 
42
         * @param c The color.
 
43
         * @return The string representation, in the form "<code>#rrggbb</code>",
 
44
         *         or <code>null</code> if <code>c</code> is <code>null</code>.
 
45
         */
 
46
        private static final String getHexString(Color c) {
 
47
 
 
48
                if (c==null) {
 
49
                        return null;
 
50
                }
 
51
 
 
52
                StringBuilder sb = new StringBuilder("#");
 
53
                int r = c.getRed();
 
54
                if (r<16) {
 
55
                        sb.append('0');
 
56
                }
 
57
                sb.append(Integer.toHexString(r));
 
58
                int g = c.getGreen();
 
59
                if (g<16) {
 
60
                        sb.append('0');
 
61
                }
 
62
                sb.append(Integer.toHexString(g));
 
63
                int b = c.getBlue();
 
64
                if (b<16) {
 
65
                        sb.append('0');
 
66
                }
 
67
                sb.append(Integer.toHexString(b));
 
68
 
 
69
                return sb.toString();
 
70
 
 
71
        }
 
72
 
 
73
 
 
74
        /**
 
75
         * Returns the screen coordinates for the monitor that contains the
 
76
         * specified point.  This is useful for setups with multiple monitors,
 
77
         * to ensure that popup windows are positioned properly.
 
78
         *
 
79
         * @param x The x-coordinate, in screen coordinates.
 
80
         * @param y The y-coordinate, in screen coordinates.
 
81
         * @return The bounds of the monitor that contains the specified point.
 
82
         */
 
83
        public static Rectangle getScreenBoundsForPoint(int x, int y) {
 
84
                GraphicsEnvironment env = GraphicsEnvironment.
 
85
                                                                                getLocalGraphicsEnvironment();
 
86
                GraphicsDevice[] devices = env.getScreenDevices();
 
87
                for (int i=0; i<devices.length; i++) {
 
88
                        GraphicsConfiguration[] configs = devices[i].getConfigurations();
 
89
                        for (int j=0; j<configs.length; j++) {
 
90
                                Rectangle gcBounds = configs[j].getBounds();
 
91
                                if (gcBounds.contains(x, y)) {
 
92
                                        return gcBounds;
 
93
                                }
 
94
                        }
 
95
                }
 
96
                // If point is outside all monitors, default to default monitor (?)
 
97
                return env.getMaximumWindowBounds();
 
98
        }
 
99
 
 
100
 
 
101
        /**
 
102
         * Returns the default background color to use for tool tip windows.
 
103
         *
 
104
         * @return The default background color.
 
105
         */
 
106
        public static Color getToolTipBackground() {
 
107
 
 
108
                Color c = UIManager.getColor("ToolTip.background");
 
109
 
 
110
                // Tooltip.background is wrong color on Nimbus (!)
 
111
                boolean isNimbus = isNimbusLookAndFeel();
 
112
                if (c==null || isNimbus) {
 
113
                        c = UIManager.getColor("info"); // Used by Nimbus (and others)
 
114
                        if (c==null || (isNimbus && isDerivedColor(c))) {
 
115
                                c = SystemColor.info; // System default
 
116
                        }
 
117
                }
 
118
 
 
119
                // Workaround for a bug (?) with Nimbus - calling JLabel.setBackground()
 
120
                // with a ColorUIResource does nothing, must be a normal Color
 
121
                if (c instanceof ColorUIResource) {
 
122
                        c = new Color(c.getRGB());
 
123
                }
 
124
 
 
125
                return c;
 
126
 
 
127
        }
 
128
 
 
129
 
 
130
        /**
 
131
         * Returns the border used by tool tips in this look and feel.
 
132
         *
 
133
         * @return The border.
 
134
         */
 
135
        public static Border getToolTipBorder() {
 
136
 
 
137
                Border border = UIManager.getBorder("ToolTip.border");
 
138
 
 
139
                if (border==null || isNimbusLookAndFeel()) {
 
140
                        border = UIManager.getBorder("nimbusBorder");
 
141
                        if (border==null) {
 
142
                                border = BorderFactory.createLineBorder(SystemColor.controlDkShadow);
 
143
                        }
 
144
                }
 
145
 
 
146
                return border;
 
147
 
 
148
        }
 
149
 
 
150
 
 
151
        /**
 
152
         * Returns whether a color is a Nimbus DerivedColor, which is troublesome
 
153
         * in that it doesn't use its RGB values (uses HSB instead?) and so
 
154
         * querying them is useless.
 
155
         *
 
156
         * @param c The color to check.
 
157
         * @return Whether it is a DerivedColor
 
158
         */
 
159
        private static final boolean isDerivedColor(Color c) {
 
160
                return c!=null && c.getClass().getName().endsWith(".DerivedColor");
 
161
        }
 
162
 
 
163
 
 
164
        /**
 
165
         * Returns whether the Nimbus Look and Feel is installed.
 
166
         *
 
167
         * @return Whether the current LAF is Nimbus.
 
168
         */
 
169
        private static final boolean isNimbusLookAndFeel() {
 
170
                return UIManager.getLookAndFeel().getName().equals("Nimbus");
 
171
        }
 
172
 
 
173
 
 
174
        /**
 
175
         * Tweaks a <code>JEditorPane</code> so it can be used to render the
 
176
         * content in a focusable pseudo-tool tip.  It is assumed that the editor
 
177
         * pane is using an <code>HTMLDocument</code>.
 
178
         *
 
179
         * @param textArea The editor pane to tweak.
 
180
         */
 
181
        public static void tweakTipEditorPane(JEditorPane textArea) {
 
182
 
 
183
                // Jump through a few hoops to get things looking nice in Nimbus
 
184
                boolean isNimbus = isNimbusLookAndFeel();
 
185
                if (isNimbus) {
 
186
                        Color selBG = textArea.getSelectionColor();
 
187
                        Color selFG = textArea.getSelectedTextColor();
 
188
                        textArea.setUI(new javax.swing.plaf.basic.BasicEditorPaneUI());
 
189
                        textArea.setSelectedTextColor(selFG);
 
190
                        textArea.setSelectionColor(selBG);
 
191
                }
 
192
 
 
193
                textArea.setEditable(false); // Required for links to work!
 
194
                textArea.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
 
195
 
 
196
                // Make selection visible even though we are not (initially) focusable.
 
197
                textArea.getCaret().setSelectionVisible(true);
 
198
 
 
199
                // Set the foreground color.  Important because when rendering HTML,
 
200
                // default foreground becomes black, which may not match all LAF's
 
201
                // (e.g. Substance).
 
202
                Color fg = UIManager.getColor("Label.foreground");
 
203
                if (fg==null || (isNimbus && isDerivedColor(fg))) {
 
204
                        fg = SystemColor.textText;
 
205
                }
 
206
                textArea.setForeground(fg);
 
207
 
 
208
                // Make it use the "tool tip" background color.
 
209
                textArea.setBackground(TipUtil.getToolTipBackground());
 
210
 
 
211
                // Force JEditorPane to use a certain font even in HTML.
 
212
                // All standard LookAndFeels, even Nimbus (!), define Label.font.
 
213
                Font font = UIManager.getFont("Label.font");
 
214
                if (font == null) { // Try to make a sensible default
 
215
                        font = new Font("SansSerif", Font.PLAIN, 12);
 
216
                }
 
217
                HTMLDocument doc = (HTMLDocument) textArea.getDocument();
 
218
                doc.getStyleSheet().addRule(
 
219
                                "body { font-family: " + font.getFamily() +
 
220
                                                "; font-size: " + font.getSize() + "pt" +
 
221
                                                "; color: " + getHexString(fg) + "; }");
 
222
 
 
223
        }
 
224
 
 
225
 
 
226
}
 
 
b'\\ No newline at end of file'