~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to laf-widget/src/main/java/org/pushingpixels/lafwidget/LafWidgetSupport.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2005-2010 Laf-Widget Kirill Grouchnikov. All Rights Reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without 
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 * 
 
7
 *  o Redistributions of source code must retain the above copyright notice, 
 
8
 *    this list of conditions and the following disclaimer. 
 
9
 *     
 
10
 *  o Redistributions in binary form must reproduce the above copyright notice, 
 
11
 *    this list of conditions and the following disclaimer in the documentation 
 
12
 *    and/or other materials provided with the distribution. 
 
13
 *     
 
14
 *  o Neither the name of Laf-Widget Kirill Grouchnikov nor the names of 
 
15
 *    its contributors may be used to endorse or promote products derived 
 
16
 *    from this software without specific prior written permission. 
 
17
 *     
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 
20
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 
21
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 
22
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 
23
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 
24
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 
25
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 
26
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 
27
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 
28
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 
29
 */
 
30
package org.pushingpixels.lafwidget;
 
31
 
 
32
import java.awt.*;
 
33
import java.lang.reflect.Field;
 
34
import java.lang.reflect.Method;
 
35
 
 
36
import javax.swing.*;
 
37
import javax.swing.JInternalFrame.JDesktopIcon;
 
38
import javax.swing.plaf.TabbedPaneUI;
 
39
import javax.swing.plaf.basic.BasicTabbedPaneUI;
 
40
import javax.swing.text.JTextComponent;
 
41
 
 
42
import org.pushingpixels.lafwidget.desktop.DesktopIconHoverPreviewWidget;
 
43
import org.pushingpixels.lafwidget.menu.MenuSearchWidget;
 
44
import org.pushingpixels.lafwidget.tabbed.TabHoverPreviewWidget;
 
45
import org.pushingpixels.lafwidget.tabbed.TabOverviewDialogWidget;
 
46
import org.pushingpixels.lafwidget.text.LockBorderWidget;
 
47
import org.pushingpixels.lafwidget.text.PasswordStrengthCheckerWidget;
 
48
import org.pushingpixels.lafwidget.utils.LafConstants;
 
49
import org.pushingpixels.lafwidget.utils.LafConstants.PasswordStrength;
 
50
 
 
51
/**
 
52
 * LAF-specific support for widgets. Each LAF should override relevant functions
 
53
 * based on the internal implementation. Note that if
 
54
 * {@link LafWidgetRepository#setLafSupport(LafWidgetSupport)} is called with a
 
55
 * custom implementation, that implementation should not throw exceptions in any
 
56
 * function.
 
57
 * 
 
58
 * @author Kirill Grouchnikov
 
59
 */
 
60
public class LafWidgetSupport {
 
61
        /**
 
62
         * Returns the component for desktop icon hover (internal frame preview)
 
63
         * functionality. Is used in the {@link DesktopIconHoverPreviewWidget}
 
64
         * widget.
 
65
         * 
 
66
         * @param desktopIcon
 
67
         *            Desktop icon.
 
68
         * @return The component for desktop icon hover (internal frame preview)
 
69
         *         functionality.
 
70
         */
 
71
        public JComponent getComponentForHover(JDesktopIcon desktopIcon) {
 
72
                return desktopIcon;
 
73
        }
 
74
 
 
75
        /**
 
76
         * Returns indication whether the menu search functionality should be
 
77
         * installed on the specified menu bar. Is used in the
 
78
         * {@link MenuSearchWidget} widget.
 
79
         * 
 
80
         * @param menuBar
 
81
         *            Menu bar.
 
82
         * @return <code>true</code> if the menu search functionality should be
 
83
         *         installed on the specified menu bar, <code>false</code>
 
84
         *         otherwise.
 
85
         */
 
86
        public boolean toInstallMenuSearch(JMenuBar menuBar) {
 
87
                return (MenuSearchWidget.getMenuItemCount(menuBar) > 40);
 
88
        }
 
89
 
 
90
        /**
 
91
         * Returns indication whether additional functionality should be installed
 
92
         * on the specified component.
 
93
         * 
 
94
         * @param comp
 
95
         *            Component.
 
96
         * @return <code>true</code> if additional functionality should be installed
 
97
         *         on the specified component, <code>false</code> otherwise.
 
98
         */
 
99
        public boolean toInstallExtraElements(Component comp) {
 
100
                return true;
 
101
        }
 
102
 
 
103
        /**
 
104
         * Returns the search icon that matches the specified parameters. Is used in
 
105
         * the {@link MenuSearchWidget} widget.
 
106
         * 
 
107
         * @param dimension
 
108
         *            Search icon dimension.
 
109
         * @param componentOrientation
 
110
         *            The orientation for the search icon. Should be considered in
 
111
         *            the implementation code for proper RTL support.
 
112
         * @return The search icon that matches the specified parameters.
 
113
         */
 
114
        public Icon getSearchIcon(int dimension,
 
115
                        ComponentOrientation componentOrientation) {
 
116
                return LafWidgetUtilities.getSearchIcon(dimension, componentOrientation
 
117
                                .isLeftToRight());
 
118
        }
 
119
 
 
120
        /**
 
121
         * Returns the icon that matches the specified number. This function is used
 
122
         * in {@link MenuSearchWidget} to set icons on menu search results. See
 
123
         * default implementation in {@link LafWidgetUtilities#getHexaMarker(int)}
 
124
         * that returns binary-based icons for numbers from 0 to 15. Is used in the
 
125
         * {@link MenuSearchWidget} widget.
 
126
         * 
 
127
         * @param number
 
128
         *            Number.
 
129
         * @return The icon that matches the specified number.
 
130
         */
 
131
        public Icon getNumberIcon(int number) {
 
132
                return LafWidgetUtilities.getHexaMarker(number);
 
133
        }
 
134
 
 
135
        /**
 
136
         * Marks the specified button as <code>flat</code>. A flat button doesn't
 
137
         * show its background unless selected, armed, pressed or (possibly) hovered
 
138
         * over. Some LAFs have flat buttons on toolbars. Is used in
 
139
         * {@link MenuSearchWidget} and {@link TabOverviewDialogWidget} widgets.
 
140
         * 
 
141
         * @param button
 
142
         *            Button to mark as flat.
 
143
         */
 
144
        public void markButtonAsFlat(AbstractButton button) {
 
145
        }
 
146
 
 
147
        /**
 
148
         * Returns the index of the rollover tab in the specified tabbed pane. Is
 
149
         * used in the {@link TabHoverPreviewWidget} widget.
 
150
         * 
 
151
         * @param tabbedPane
 
152
         *            Tabbed pane.
 
153
         * @return The index of the rollover tab in the specified tabbed pane.
 
154
         * @throws UnsupportedOperationException
 
155
         *             In the base implementation.
 
156
         */
 
157
        public int getRolloverTabIndex(JTabbedPane tabbedPane) {
 
158
                TabbedPaneUI ui = tabbedPane.getUI();
 
159
                if (ui instanceof BasicTabbedPaneUI) {
 
160
                        try {
 
161
                                Class<?> clazz = ui.getClass();
 
162
                                while (clazz != null) {
 
163
                                        try {
 
164
                                                Method mtd = clazz.getDeclaredMethod("getRolloverTab",
 
165
                                                                new Class[0]);
 
166
                                                if (mtd != null) {
 
167
                                                        mtd.setAccessible(true);
 
168
                                                        int result = (Integer) mtd
 
169
                                                                        .invoke(ui, new Object[0]);
 
170
                                                        return result;
 
171
                                                }
 
172
                                        } catch (NoSuchMethodException nsme) {
 
173
                                        }
 
174
                                        clazz = clazz.getSuperclass();
 
175
                                }
 
176
                        } catch (Throwable t) {
 
177
                                // ignore all fall through
 
178
                        }
 
179
                }
 
180
                throw new UnsupportedOperationException();
 
181
        }
 
182
 
 
183
        /**
 
184
         * Sets the tab area insets for the specified tabbed pane. Is used in the
 
185
         * {@link TabOverviewDialogWidget} widget.
 
186
         * 
 
187
         * @param tabbedPane
 
188
         *            Tabbed pane.
 
189
         * @param tabAreaInsets
 
190
         *            Tab area insets.
 
191
         * @throws UnsupportedOperationException
 
192
         *             In the base implementation.
 
193
         */
 
194
        public void setTabAreaInsets(JTabbedPane tabbedPane, Insets tabAreaInsets) {
 
195
                Insets old = this.getTabAreaInsets(tabbedPane);
 
196
                TabbedPaneUI ui = tabbedPane.getUI();
 
197
                if (ui instanceof BasicTabbedPaneUI) {
 
198
                        try {
 
199
                                Class<?> clazz = ui.getClass();
 
200
                                while (clazz != null) {
 
201
                                        try {
 
202
                                                Field fld = clazz.getDeclaredField("tabAreaInsets");
 
203
                                                if (fld != null) {
 
204
                                                        fld.setAccessible(true);
 
205
                                                        fld.set(ui, tabAreaInsets);
 
206
                                                        // Fire a property change event so that the tabbed
 
207
                                                        // pane can revalidate itself
 
208
                                                        LafWidgetUtilities.firePropertyChangeEvent(
 
209
                                                                        tabbedPane, "tabAreaInsets", old,
 
210
                                                                        tabAreaInsets);
 
211
                                                        return;
 
212
                                                }
 
213
                                        } catch (NoSuchFieldException nsfe) {
 
214
                                        }
 
215
                                        clazz = clazz.getSuperclass();
 
216
                                }
 
217
                        } catch (Throwable t) {
 
218
                                // ignore all fall through
 
219
                        }
 
220
                }
 
221
                throw new UnsupportedOperationException();
 
222
        }
 
223
 
 
224
        /**
 
225
         * Returns the tab area insets for the specified tabbed pane.Is used in the
 
226
         * {@link TabOverviewDialogWidget} widget.
 
227
         * 
 
228
         * @param tabbedPane
 
229
         *            Tabbed pane.
 
230
         * @return The tab area insets for the specified tabbed pane.
 
231
         */
 
232
        public Insets getTabAreaInsets(JTabbedPane tabbedPane) {
 
233
                TabbedPaneUI ui = tabbedPane.getUI();
 
234
                if (ui instanceof BasicTabbedPaneUI) {
 
235
                        try {
 
236
                                Class<?> clazz = ui.getClass();
 
237
                                while (clazz != null) {
 
238
                                        try {
 
239
                                                Field fld = clazz.getDeclaredField("tabAreaInsets");
 
240
                                                if (fld != null) {
 
241
                                                        fld.setAccessible(true);
 
242
                                                        Insets result = (Insets) fld.get(ui);
 
243
                                                        return result;
 
244
                                                }
 
245
                                        } catch (NoSuchFieldException nsfe) {
 
246
                                        }
 
247
                                        clazz = clazz.getSuperclass();
 
248
                                }
 
249
                        } catch (Throwable t) {
 
250
                                // ignore all fall through
 
251
                        }
 
252
                }
 
253
                Insets result = UIManager.getInsets("TabbedPane.tabAreaInsets");
 
254
                if (result == null)
 
255
                        result = new Insets(0, 0, 0, 0);
 
256
                return result;
 
257
        }
 
258
 
 
259
        /**
 
260
         * Returns the tab rectangle for the specified tab in a tabbed pane.Is used
 
261
         * in the {@link TabHoverPreviewWidget} widget.
 
262
         * 
 
263
         * @param tabbedPane
 
264
         *            Tabbed pane.
 
265
         * @param tabIndex
 
266
         *            Index of a tab.
 
267
         * @return The tab rectangle for the specified parameters.
 
268
         * @throws UnsupportedOperationException
 
269
         *             In the base implementation.
 
270
         */
 
271
        public Rectangle getTabRectangle(JTabbedPane tabbedPane, int tabIndex) {
 
272
                TabbedPaneUI ui = tabbedPane.getUI();
 
273
                if (ui instanceof BasicTabbedPaneUI) {
 
274
                        try {
 
275
                                Class<?> clazz = ui.getClass();
 
276
                                while (clazz != null) {
 
277
                                        try {
 
278
                                                Field fld = clazz.getDeclaredField("rects");
 
279
                                                if (fld != null) {
 
280
                                                        fld.setAccessible(true);
 
281
                                                        Rectangle[] rects = (Rectangle[]) fld.get(ui);
 
282
                                                        return rects[tabIndex];
 
283
                                                }
 
284
                                        } catch (NoSuchFieldException nsfe) {
 
285
                                        }
 
286
                                        clazz = clazz.getSuperclass();
 
287
                                }
 
288
                        } catch (Throwable t) {
 
289
                                // ignore all fall through
 
290
                        }
 
291
                }
 
292
                throw new UnsupportedOperationException();
 
293
        }
 
294
 
 
295
        /**
 
296
         * Paints password strength marker. Is used in the
 
297
         * {@link PasswordStrengthCheckerWidget} widget. The default implementation
 
298
         * uses orange color for {@link org.pushingpixels.lafwidget.utils.LafConstants.PasswordStrength#WEAK}
 
299
         * passwords, yellow color for {@link org.pushingpixels.lafwidget.utils.LafConstants.PasswordStrength#MEDIUM}
 
300
         * passwords and green color for
 
301
         * {@link org.pushingpixels.lafwidget.utils.LafConstants.PasswordStrength#STRONG} passwords.
 
302
         * 
 
303
         * @param g
 
304
         *            Graphics context.
 
305
         * @param x
 
306
         *            X coordinate for the marker.
 
307
         * @param y
 
308
         *            Y coordinate for the marker.
 
309
         * @param width
 
310
         *            Marker width.
 
311
         * @param height
 
312
         *            Marker height.
 
313
         * @param pStrength
 
314
         *            Password strength.
 
315
         */
 
316
        public void paintPasswordStrengthMarker(Graphics g, int x, int y,
 
317
                        int width, int height, PasswordStrength pStrength) {
 
318
                Graphics2D g2 = (Graphics2D) g.create();
 
319
                if (pStrength == PasswordStrength.WEAK)
 
320
                        g2.setColor(Color.orange);
 
321
                if (pStrength == PasswordStrength.MEDIUM)
 
322
                        g2.setColor(Color.yellow);
 
323
                if (pStrength == PasswordStrength.STRONG)
 
324
                        g2.setColor(Color.green);
 
325
                g2.fillRect(x, y, width, height);
 
326
                g2.dispose();
 
327
        }
 
328
 
 
329
        /**
 
330
         * Checks whether the specified component should show a lock icon. Is used
 
331
         * in the {@link LockBorderWidget} widget.
 
332
         * 
 
333
         * @param comp
 
334
         *            Component.
 
335
         * @return <code>true</code> if the specified component should show a lock
 
336
         *         icon, <code>false</code> otherwise.
 
337
         */
 
338
        public boolean hasLockIcon(Component comp) {
 
339
                // check the HAS_LOCK_ICON property
 
340
                boolean isEditableTextComponent = (comp instanceof JTextComponent) ? ((JTextComponent) comp)
 
341
                                .isEditable()
 
342
                                : false;
 
343
                if (comp instanceof JComponent) {
 
344
                        if (!isEditableTextComponent
 
345
                                        && Boolean.TRUE.equals(((JComponent) comp)
 
346
                                                        .getClientProperty(LafWidget.HAS_LOCK_ICON)))
 
347
                                return true;
 
348
                        if (Boolean.FALSE.equals(((JComponent) comp)
 
349
                                        .getClientProperty(LafWidget.HAS_LOCK_ICON)))
 
350
                                return false;
 
351
                }
 
352
                if (!isEditableTextComponent
 
353
                                && Boolean.TRUE.equals(UIManager.get(LafWidget.HAS_LOCK_ICON)))
 
354
                        return true;
 
355
 
 
356
                return false;
 
357
        }
 
358
 
 
359
        /**
 
360
         * Returns the lock icon. Is used in {@link LockBorderWidget} widget.
 
361
         * 
 
362
         * @return Lock icon. Should be sufficiently small (preferrably not more
 
363
         *         than 5-6 pixels wide).
 
364
         */
 
365
        public Icon getLockIcon(Component c) {
 
366
                return LafWidgetUtilities.getSmallLockIcon();
 
367
        }
 
368
 
 
369
        /**
 
370
         * Returns the arrow icon (the icon used in combo box drop button, scroll
 
371
         * bar buttons etc.).
 
372
         * 
 
373
         * @param orientation
 
374
         *            One of {@link SwingConstants#NORTH} or
 
375
         *            {@link SwingConstants#SOUTH}.
 
376
         * @return Arrow icon.
 
377
         */
 
378
        public Icon getArrowIcon(int orientation) {
 
379
                return null;
 
380
        }
 
381
 
 
382
        /**
 
383
         * Returns the size of the lookup icon. Override to handle high DPI mode.
 
384
         * 
 
385
         * @return The size of the lookup icon.
 
386
         */
 
387
        public int getLookupIconSize() {
 
388
                return 14;
 
389
        }
 
390
 
 
391
        /**
 
392
         * Returns the size of the lookup button. Override to handle high DPI mode.
 
393
         * 
 
394
         * @return The size of the lookup button.
 
395
         */
 
396
        public int getLookupButtonSize() {
 
397
                return 16;
 
398
        }
 
399
}