Debian Codegraph @

substance-swingx/src/main/java/org/pushingpixels/substance/swingx/SubstanceDatePickerUI.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.Component;
22 import java.awt.Container;
23 import java.awt.Dimension;
24 import java.awt.Graphics;
25 import java.awt.Graphics2D;
26 import java.awt.Insets;
27 import java.awt.LayoutManager;
28 import java.awt.Shape;
29 import java.beans.PropertyChangeEvent;
30 import java.beans.PropertyChangeListener;
31
32 import javax.swing.JButton;
33 import javax.swing.JComponent;
34 import javax.swing.JFormattedTextField;
35 import javax.swing.SwingConstants;
36 import javax.swing.border.Border;
37 import javax.swing.border.EmptyBorder;
38 import javax.swing.plaf.ComponentUI;
39 import javax.swing.plaf.UIResource;
40 import javax.swing.text.JTextComponent;
41
42 import org.jdesktop.swingx.JXDatePicker;
43 import org.jdesktop.swingx.JXMonthView;
44 import org.jdesktop.swingx.plaf.MonthViewUI;
45 import org.jdesktop.swingx.plaf.basic.BasicDatePickerUI;
46 import org.pushingpixels.substance.api.SubstanceLookAndFeel;
47 import org.pushingpixels.substance.api.SubstanceConstants.Side;
48 import org.pushingpixels.substance.internal.utils.SubstanceCoreUtilities;
49 import org.pushingpixels.substance.internal.utils.SubstanceDropDownButton;
50 import org.pushingpixels.substance.internal.utils.SubstanceOutlineUtilities;
51 import org.pushingpixels.substance.internal.utils.SubstanceSizeUtils;
52 import org.pushingpixels.substance.internal.utils.SubstanceTextUtilities;
53 import org.pushingpixels.substance.internal.utils.SubstanceCoreUtilities.TextComponentAware;
54 import org.pushingpixels.substance.internal.utils.border.SubstanceTextComponentBorder;
55
56 /**
57 * Substance-consistent UI delegate for {@link JXDatePicker}.
58 *
59 * @author Kirill Grouchnikov
60 */
61 public class SubstanceDatePickerUI extends BasicDatePickerUI {
62 protected PropertyChangeListener substancePropertyChangeListener;
63
64 protected JButton substancePopupButton;
65
66 private Insets layoutInsets;
67
68 public static ComponentUI createUI(JComponent comp) {
69 SubstanceCoreUtilities.testComponentCreationThreadingViolation(comp);
70 return new SubstanceDatePickerUI();
71 }
72
73 @Override
74 public void installUI(JComponent c) {
75 super.installUI(c);
76
77 c.putClientProperty(SubstanceCoreUtilities.TEXT_COMPONENT_AWARE,
78 new TextComponentAware<JXDatePicker>() {
79 @Override
80 public JTextComponent getTextComponent(JXDatePicker t) {
81 return t.getEditor();
82 }
83 });
84 }
85
86 @Override
87 public void uninstallUI(JComponent c) {
88 c.putClientProperty(SubstanceCoreUtilities.TEXT_COMPONENT_AWARE, null);
89
90 super.uninstallUI(c);
91 }
92
93 @Override
94 protected void installListeners() {
95 super.installListeners();
96
97 this.substancePropertyChangeListener = new PropertyChangeListener() {
98 @Override
99 public void propertyChange(PropertyChangeEvent evt) {
100 // issue 7 - update the popup button on orientation change
101 if ("componentOrientation".equals(evt.getPropertyName())) {
102 configurePopupButton();
103 }
104 }
105 };
106 this.datePicker
107 .addPropertyChangeListener(this.substancePropertyChangeListener);
108 }
109
110 @Override
111 protected void uninstallListeners() {
112 this.datePicker
113 .removePropertyChangeListener(this.substancePropertyChangeListener);
114 this.substancePropertyChangeListener = null;
115 super.uninstallListeners();
116 }
117
118 @Override
119 protected void installDefaults() {
120 super.installDefaults();
121
122 Border b = this.datePicker.getBorder();
123 if (b == null || b instanceof UIResource) {
124 int fontSize = SubstanceSizeUtils
125 .getComponentFontSize(this.datePicker);
126 Insets borderInsets = SubstanceSizeUtils
127 .getComboBorderInsets(fontSize);
128 this.datePicker.setBorder(new SubstanceTextComponentBorder(
129 borderInsets));
130 this.layoutInsets = SubstanceSizeUtils
131 .getComboLayoutInsets(fontSize);
132 }
133
134 this.datePicker.setOpaque(false);
135 }
136
137 /*
138 * (non-Javadoc)
139 *
140 * @see org.jdesktop.swingx.plaf.basic.BasicDatePickerUI#createPopupButton()
141 */
142 @Override
143 protected JButton createPopupButton() {
144 this.substancePopupButton = new SubstanceDropDownButton(this.datePicker);
145 this.substancePopupButton.setFocusPainted(false);
146 configurePopupButton();
147
148 this.substancePopupButton.setIcon(SubstanceCoreUtilities.getArrowIcon(
149 this.substancePopupButton, SwingConstants.SOUTH));
150
151 return this.substancePopupButton;
152 }
153
154 private void configurePopupButton() {
155 Side side = this.datePicker.getComponentOrientation().isLeftToRight() ? Side.LEFT
156 : Side.RIGHT;
157 this.substancePopupButton.putClientProperty(
158 SubstanceLookAndFeel.BUTTON_SIDE_PROPERTY, side);
159 // this.substancePopupButton.putClientProperty(
160 // SubstanceLookAndFeel.BUTTON_OPEN_SIDE_PROPERTY, side);
161
162 // this.substancePopupButton.putClientProperty(
163 // SubstanceLookAndFeel.FLAT_PROPERTY, Boolean.TRUE);
164
165 this.substancePopupButton.setRolloverEnabled(true);
166 }
167
168 /*
169 * (non-Javadoc)
170 *
171 * @see org.jdesktop.swingx.plaf.basic.BasicDatePickerUI#hidePopup()
172 */
173 @Override
174 public void hidePopup() {
175 super.hidePopup();
176
177 // fix for issue 6 - clear rollover indices on the popup
178 // month view component
179 JXMonthView popupMonthView = this.datePicker.getMonthView();
180 MonthViewUI ui = popupMonthView.getUI();
181 if (ui instanceof SubstanceMonthViewUI) {
182 ((SubstanceMonthViewUI) ui).resetRolloverIndex();
183 }
184 }
185
186 @Override
187 protected JFormattedTextField createEditor() {
188 JFormattedTextField result = super.createEditor();
189 Insets ins = SubstanceSizeUtils
190 .getComboTextBorderInsets(SubstanceSizeUtils
191 .getComponentFontSize(result));
192 result.setBorder(new EmptyBorder(ins.top, ins.left, ins.bottom,
193 ins.right));
194 result.setBackground(this.datePicker.getBackground());
195 result.setOpaque(false);
196 return result;
197 }
198
199 @Override
200 public void paint(Graphics g, JComponent c) {
201 Graphics2D graphics = (Graphics2D) g.create();
202
203 int componentFontSize = SubstanceSizeUtils
204 .getComponentFontSize(this.datePicker);
205 int borderDelta = (int) Math.floor(SubstanceSizeUtils
206 .getBorderStrokeWidth(componentFontSize));
207 Shape contour = SubstanceOutlineUtilities.getBaseOutline(datePicker
208 .getWidth(), datePicker.getHeight(), Math.max(0, 2.0f
209 * SubstanceSizeUtils
210 .getClassicButtonCornerRadius(componentFontSize)
211 - borderDelta), null, borderDelta);
212
213 graphics.setColor(SubstanceTextUtilities
214 .getTextBackgroundFillColor(this.datePicker));
215 graphics.fill(contour);
216
217 super.paint(g, c);
218 }
219
220 @Override
221 protected LayoutManager createLayoutManager() {
222 return new DatePickerLayoutManager();
223 }
224
225 private class DatePickerLayoutManager implements LayoutManager {
226 public void addLayoutComponent(String name, Component comp) {
227 }
228
229 public void removeLayoutComponent(Component comp) {
230 }
231
232 public Dimension preferredLayoutSize(Container parent) {
233 return parent.getPreferredSize();
234 }
235
236 public Dimension minimumLayoutSize(Container parent) {
237 return parent.getMinimumSize();
238 }
239
240 public void layoutContainer(Container parent) {
241 int popupButtonWidth = substancePopupButton != null ? substancePopupButton
242 .getPreferredSize().width
243 : 0;
244
245 boolean ltr = datePicker.getComponentOrientation().isLeftToRight();
246
247 datePicker.getEditor().setBounds(
248 ltr ? layoutInsets.left : layoutInsets.left
249 + popupButtonWidth,
250 layoutInsets.top,
251 datePicker.getWidth() - popupButtonWidth
252 - layoutInsets.left - layoutInsets.right,
253 datePicker.getHeight() - layoutInsets.top
254 - layoutInsets.bottom);
255
256 if (substancePopupButton != null) {
257 if (ltr) {
258 substancePopupButton.setBounds(datePicker.getWidth()
259 - popupButtonWidth, 0, popupButtonWidth, datePicker
260 .getHeight());
261 } else {
262 substancePopupButton.setBounds(0, 0, popupButtonWidth,
263 datePicker.getHeight());
264 }
265 }
266 }
267 }
268 }