Debian Codegraph @

substance-swingx/src/main/java/org/pushingpixels/substance/swingx/SubstanceLoginPaneUI.java

raw symbols SCIP
1 /*
2 * Copyright 2005-2010 Kirill Grouchnikov, based on work by
3 * Sun Microsystems, Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 package org.pushingpixels.substance.swingx;
20
21 import java.awt.*;
22 import java.awt.event.HierarchyEvent;
23 import java.awt.event.HierarchyListener;
24 import java.awt.image.BufferedImage;
25
26 import javax.swing.*;
27 import javax.swing.plaf.ComponentUI;
28
29 import org.jdesktop.swingx.JXLoginPane;
30 import org.jdesktop.swingx.plaf.basic.BasicLoginPaneUI;
31 import org.pushingpixels.lafwidget.utils.RenderingUtils;
32 import org.pushingpixels.substance.api.DecorationAreaType;
33 import org.pushingpixels.substance.api.SubstanceLookAndFeel;
34 import org.pushingpixels.substance.internal.painter.BackgroundPaintingUtils;
35 import org.pushingpixels.substance.internal.painter.DecorationPainterUtils;
36 import org.pushingpixels.substance.internal.utils.SubstanceColorUtilities;
37 import org.pushingpixels.substance.internal.utils.SubstanceCoreUtilities;
38
39 /**
40 * Substance-consistent UI delegate for {@link JXLoginPane}.
41 *
42 * @author Kirill Grouchnikov
43 */
44 public class SubstanceLoginPaneUI extends BasicLoginPaneUI {
45 protected JXLoginPane loginPanel;
46
47 protected HierarchyListener substanceHierarchyListener;
48
49 // protected ComponentListener substanceComponentListener;
50
51 public static ComponentUI createUI(JComponent comp) {
52 SubstanceCoreUtilities.testComponentCreationThreadingViolation(comp);
53 return new SubstanceLoginPaneUI((JXLoginPane) comp);
54 }
55
56 /**
57 * Creates a new UI component.
58 *
59 * @param dlg
60 * Login panel component.
61 */
62 public SubstanceLoginPaneUI(JXLoginPane dlg) {
63 super(dlg);
64 this.loginPanel = dlg;
65 }
66
67 /*
68 * (non-Javadoc)
69 *
70 * @see javax.swing.plaf.ComponentUI#installUI(javax.swing.JComponent)
71 */
72 @Override
73 public void installUI(JComponent c) {
74 super.installUI(c);
75 this.substanceHierarchyListener = new HierarchyListener() {
76 public void hierarchyChanged(HierarchyEvent e) {
77 SwingUtilities.invokeLater(new Runnable() {
78 public void run() {
79 Window window = SwingUtilities
80 .getWindowAncestor(loginPanel);
81 if (window != null) {
82 JComponent titlePane = SubstanceLookAndFeel
83 .getTitlePaneComponent(window);
84 if (titlePane != null) {
85 titlePane.putClientProperty(
86 SubstanceLookAndFeel.WATERMARK_VISIBLE,
87 Boolean.FALSE);
88 }
89 }
90
91 loginPanel.setBanner(getBanner());
92 }
93 });
94 }
95 };
96 this.loginPanel.addHierarchyListener(this.substanceHierarchyListener);
97 // this.substanceComponentListener = new ComponentAdapter() {
98 // @Override
99 // public void componentShown(ComponentEvent e) {
100 // SwingUtilities.invokeLater(new Runnable() {
101 // public void run() {
102 // System.out.println("Recomputed");
103 // loginPanel.setImage(getBanner());
104 // }
105 // });
106 // }
107 // };
108 // this.loginPanel.addComponentListener(this.substanceComponentListener)
109 // ;
110 }
111
112 /*
113 * (non-Javadoc)
114 *
115 * @see javax.swing.plaf.ComponentUI#uninstallUI(javax.swing.JComponent)
116 */
117 @Override
118 public void uninstallUI(JComponent c) {
119 this.loginPanel
120 .removeHierarchyListener(this.substanceHierarchyListener);
121 this.substanceHierarchyListener = null;
122 // this.loginPanel.removeComponentListener(this.
123 // substanceComponentListener);
124 // this.substanceComponentListener = null;
125 super.uninstallUI(c);
126 }
127
128 /*
129 * (non-Javadoc)
130 *
131 * @see org.jdesktop.swingx.plaf.basic.BasicLoginPanelUI#getBanner()
132 */
133 @Override
134 public Image getBanner() {
135 Image superResult = super.getBanner();
136 if (this.loginPanel.getParent() == null)
137 return superResult;
138
139 int width = superResult.getWidth(null);
140 int height = superResult.getHeight(null);
141 BufferedImage result = SubstanceCoreUtilities.getBlankImage(width,
142 height);
143 Graphics2D graphics = (Graphics2D) result.getGraphics();
144
145 Icon origIcon = SubstanceCoreUtilities
146 .getIcon("resource/32/login-new32.png");
147 origIcon = SubstanceCoreUtilities.getThemedIcon(this.loginPanel,
148 origIcon);
149
150 SubstanceLookAndFeel.setDecorationType(this.loginPanel,
151 DecorationAreaType.HEADER);
152 BackgroundPaintingUtils.update(graphics, this.loginPanel, true);
153 DecorationPainterUtils.clearDecorationType(this.loginPanel);
154
155 float loginStringX = origIcon.getIconWidth() + 8 + width * .05f;
156 float loginStringY = height * .75f;
157
158 Font font = UIManager.getFont("JXLoginPane.bannerFont");
159 graphics.setFont(font);
160 Graphics2D originalGraphics = graphics;
161 if (!loginPanel.getComponentOrientation().isLeftToRight()) {
162 originalGraphics = (Graphics2D) graphics.create();
163 graphics.scale(-1, 1);
164 graphics.translate(-width, 0);
165 loginStringX = width
166 - origIcon.getIconWidth()
167 - 8
168 - (((float) font.getStringBounds(
169 loginPanel.getBannerText(),
170 originalGraphics.getFontRenderContext()).getWidth()) + width * .05f);
171 }
172
173 RenderingUtils.installDesktopHints(graphics, this.loginPanel);
174
175 originalGraphics.setColor(SubstanceColorUtilities
176 .getForegroundColor(SubstanceCoreUtilities.getSkin(
177 this.loginPanel).getEnabledColorScheme(
178 DecorationAreaType.HEADER)));
179 originalGraphics.drawString(loginPanel.getBannerText(), loginStringX,
180 loginStringY);
181
182 int iconY = (height - origIcon.getIconHeight()) / 2;
183 if (!loginPanel.getComponentOrientation().isLeftToRight()) {
184 origIcon.paintIcon(loginPanel, graphics, width
185 - origIcon.getIconWidth() - 8, iconY);
186 } else {
187 origIcon.paintIcon(loginPanel, graphics, 8, iconY);
188 }
189 return result;
190 }
191 }