Skip to content

Commit

Permalink
extracted constant for days in a week to prevent magic numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
javatlacati committed Sep 6, 2019
1 parent 492a05f commit 5db6f4c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 47 deletions.
17 changes: 9 additions & 8 deletions jcalendar/src/main/java/com/toedter/calendar/JDateChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,13 @@ public void actionPerformed(ActionEvent e)
popup = new JPopupMenu() {
private static final long serialVersionUID = -6078272560337577761L;

public void setVisible(boolean b) {
@Override
public void setVisible(boolean visible) {
Boolean isCanceled = (Boolean) getClientProperty("JPopupMenu.firePopupMenuCanceled");
if (b
|| (!b && dateSelected)
|| ((isCanceled != null) && !b && isCanceled
.booleanValue())) {
super.setVisible(b);
if (visible
|| (!visible && dateSelected)
|| ((isCanceled != null) && !visible && isCanceled)) {
super.setVisible(visible);
}
}
};
Expand Down Expand Up @@ -690,8 +690,8 @@ private boolean checkNewDate(Date oldValue, Date newValue) {
return true;

// No verifier then ok
DateVerifier v;
if ((v = getDateVerifier()) == null)
DateVerifier v = getDateVerifier();
if (v == null)
return true;

// If verifies then ok
Expand Down Expand Up @@ -771,6 +771,7 @@ public void stateChanged(ChangeEvent e) {
*/
public static void main(String[] s) {
JFrame frame = new JFrame("JDateChooser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JDateChooser dateChooser = new JDateChooser();
// JDateChooser dateChooser = new JDateChooser(null, new Date(), null,
// null);
Expand Down
63 changes: 24 additions & 39 deletions jcalendar/src/main/java/com/toedter/calendar/JDayChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
public class JDayChooser extends JPanel implements ActionListener, KeyListener,
FocusListener {
private static final long serialVersionUID = 5876398337018781820L;
public static final int DAYS_IN_WEEK = 7;

protected JButton[] days;

Expand Down Expand Up @@ -158,6 +159,7 @@ public JDayChooser(boolean weekOfYearVisible) {
days[index] = new JButton("x") {
private static final long serialVersionUID = -7433645992591669725L;

@Override
public void paint(Graphics g) {
if ("Windows".equals(UIManager.getLookAndFeel()
.getID())) {
Expand Down Expand Up @@ -246,12 +248,10 @@ private void drawDayNames() {

int day = firstDayOfWeek;

for (int i = 0; i < 7; i++) {
if (maxDayCharacters > 0 && maxDayCharacters < 5) {
if (dayNames[day].length() >= maxDayCharacters) {
dayNames[day] = dayNames[day]
.substring(0, maxDayCharacters);
}
for (int i = 0; i < DAYS_IN_WEEK; i++) {
if (maxDayCharacters > 0 && maxDayCharacters < 5 && dayNames[day].length() >= maxDayCharacters) {
dayNames[day] = dayNames[day]
.substring(0, maxDayCharacters);
}

days[i].setText(dayNames[day]);
Expand Down Expand Up @@ -292,7 +292,7 @@ private void assignSelectedDay(JButton selectedDay, JButton other) {
* Initializes both day names and weeks of the year.
*/
protected void initDecorations() {
for (int x = 0; x < 7; x++) {
for (int x = 0; x < DAYS_IN_WEEK; x++) {
days[x].setContentAreaFilled(decorationBackgroundVisible);
days[x].setBorderPainted(decorationBordersVisible);
days[x].invalidate();
Expand All @@ -310,8 +310,8 @@ protected void initDecorations() {
protected void drawWeeks() {
Calendar tmpCalendar = (Calendar) calendar.clone();

for (int i = 1; i < 7; i++) {
tmpCalendar.set(Calendar.DAY_OF_MONTH, (i * 7) - 6);
for (int i = 1; i < DAYS_IN_WEEK; i++) {
tmpCalendar.set(Calendar.DAY_OF_MONTH, (i * DAYS_IN_WEEK) - 6);

int week = tmpCalendar.get(Calendar.WEEK_OF_YEAR);
String buttonText = Integer.toString(week);
Expand All @@ -323,7 +323,7 @@ protected void drawWeeks() {
weeks[i].setText(buttonText);

if ((i == 5) || (i == 6)) {
weeks[i].setVisible(days[i * 7].isVisible());
weeks[i].setVisible(days[i * DAYS_IN_WEEK].isVisible());
}
}
}
Expand Down Expand Up @@ -697,16 +697,16 @@ public void setEnabled(boolean enabled) {
if (enabled)
drawDays();
else {
for (short i = 0; i < days.length; i++) {
if (days[i] != null) {
days[i].setEnabled(false);
}
}
for (JButton jButton : days) {
if (jButton != null) {
jButton.setEnabled(false);
}
}
}

for (short i = 0; i < weeks.length; i++) {
if (weeks[i] != null) {
weeks[i].setEnabled(enabled);
for (JButton week : weeks) {
if (week != null) {
week.setEnabled(enabled);
}
}
}
Expand Down Expand Up @@ -770,7 +770,7 @@ public void setDecorationBackgroundColor(Color decorationBackgroundColor) {
this.decorationBackgroundColor = decorationBackgroundColor;

if (days != null) {
for (int i = 0; i < 7; i++) {
for (int i = 0; i < DAYS_IN_WEEK; i++) {
days[i].setBackground(decorationBackgroundColor);
}
}
Expand Down Expand Up @@ -931,16 +931,8 @@ public void updateUI() {
* set to 01\01\9999)
*/
public void setSelectableDateRange(Date min, Date max) {
if (min == null) {
minSelectableDate = defaultMinSelectableDate;
} else {
minSelectableDate = min;
}
if (max == null) {
maxSelectableDate = defaultMaxSelectableDate;
} else {
maxSelectableDate = max;
}
minSelectableDate = min == null ? defaultMinSelectableDate : min;
maxSelectableDate = max == null ? defaultMaxSelectableDate : max;
if (maxSelectableDate.before(minSelectableDate)) {
minSelectableDate = defaultMinSelectableDate;
maxSelectableDate = defaultMaxSelectableDate;
Expand All @@ -958,11 +950,7 @@ public void setSelectableDateRange(Date min, Date max) {
* @return the maximum selectable date
*/
public Date setMaxSelectableDate(Date max) {
if (max == null) {
maxSelectableDate = defaultMaxSelectableDate;
} else {
maxSelectableDate = max;
}
maxSelectableDate = max == null ? defaultMaxSelectableDate : max;
drawDays();
return maxSelectableDate;
}
Expand Down Expand Up @@ -1058,11 +1046,7 @@ public void setMaxDayCharacters(int maxDayCharacters) {
return;
}

if (maxDayCharacters < 0 || maxDayCharacters > 4) {
this.maxDayCharacters = 0;
} else {
this.maxDayCharacters = maxDayCharacters;
}
this.maxDayCharacters = maxDayCharacters < 0 || maxDayCharacters > 4 ? 0 : maxDayCharacters;
drawDayNames();
drawDays();
invalidate();
Expand All @@ -1076,6 +1060,7 @@ public void setMaxDayCharacters(int maxDayCharacters) {
*/
public static void main(String[] s) {
JFrame frame = new JFrame("JDayChooser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JDayChooser());
frame.pack();
frame.setVisible(true);
Expand Down

0 comments on commit 5db6f4c

Please sign in to comment.