SwingX: JXMonthView support (#8)

This commit is contained in:
Karl Tauber
2019-10-20 10:32:07 +02:00
parent 0a86d00c1e
commit 5a952c187c
10 changed files with 492 additions and 180 deletions

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2019 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formdev.flatlaf.swingx.ui;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Shape;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import com.formdev.flatlaf.icons.FlatAbstractIcon;
import com.formdev.flatlaf.ui.FlatArrowButton;
/**
* "month down" icon for {@link org.jdesktop.swingx.JXMonthView}.
*
* @uiDefault Component.arrowType String triangle (default) or chevron
* @uiDefault JXMonthView.arrowColor Color
* @uiDefault JXMonthView.disabledArrowColor Color
*
* @author Karl Tauber
*/
public class FlatMonthDownIcon
extends FlatAbstractIcon
{
protected final boolean chevron = "chevron".equals( UIManager.getString( "Component.arrowType" ) );
protected final Color arrowColor = UIManager.getColor( "JXMonthView.arrowColor" );
protected final Color disabledArrowColor = UIManager.getColor( "JXMonthView.disabledArrowColor" );
private final int direction;
public FlatMonthDownIcon() {
this( SwingConstants.WEST );
}
protected FlatMonthDownIcon( int direction ) {
super( 20, 20, null );
this.direction = direction;
}
@Override
protected void paintIcon( Component c, Graphics2D g ) {
int h = chevron ? 4 : 5;
int w = chevron ? 8 : 9;
int x = Math.round( (width - h) / 2f );
int y = Math.round( (height - w) / 2f );
g.setColor( c.isEnabled() ? arrowColor : disabledArrowColor );
g.translate( x, y );
Shape arrowShape = FlatArrowButton.createArrowShape( direction, chevron, w, h );
if( chevron ) {
// chevron arrow
g.setStroke( new BasicStroke( 1f ) );
g.draw( arrowShape );
} else {
// triangle arrow
g.fill( arrowShape );
}
g.translate( -x, -y );
}
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2019 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formdev.flatlaf.swingx.ui;
import javax.swing.SwingConstants;
/**
* "month up" icon for {@link org.jdesktop.swingx.JXMonthView}.
*
* @author Karl Tauber
*/
public class FlatMonthUpIcon
extends FlatMonthDownIcon
{
public FlatMonthUpIcon() {
super( SwingConstants.EAST );
}
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright 2019 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formdev.flatlaf.swingx.ui;
import java.awt.Insets;
import java.util.Calendar;
import javax.swing.JComponent;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.ComponentUI;
import org.jdesktop.swingx.JXMonthView;
import org.jdesktop.swingx.plaf.basic.BasicMonthViewUI;
import org.jdesktop.swingx.plaf.basic.CalendarRenderingHandler;
import org.jdesktop.swingx.plaf.basic.CalendarState;
import com.formdev.flatlaf.ui.FlatEmptyBorder;
import com.formdev.flatlaf.ui.FlatLineBorder;
/**
* Provides the Flat LaF UI delegate for {@link org.jdesktop.swingx.JXMonthView}.
*
* @author Karl Tauber
*/
public class FlatMonthViewUI
extends BasicMonthViewUI
{
public static ComponentUI createUI( JComponent c ) {
return new FlatMonthViewUI();
}
@Override
protected CalendarRenderingHandler createRenderingHandler() {
return new FlatRenderingHandler();
}
//---- class FlatRenderingHandler -----------------------------------------
private static class FlatRenderingHandler
extends RenderingHandler
{
@Override
public JComponent prepareRenderingComponent( JXMonthView monthView, Calendar calendar,
CalendarState dayState )
{
JComponent c = super.prepareRenderingComponent( monthView, calendar, dayState );
int px = monthView.getBoxPaddingX();
int py = monthView.getBoxPaddingY();
// scale borders
Border border = null;
if( dayState == CalendarState.TITLE && monthView.isTraversable() ) {
Border b = c.getBorder();
if( b instanceof CompoundBorder && ((CompoundBorder)b).getInsideBorder() instanceof EmptyBorder )
border = new CompoundBorder( ((CompoundBorder)b).getOutsideBorder(), new FlatEmptyBorder( py * 2, 0, py * 2, 0 ) );
} else if( dayState == CalendarState.TODAY )
border = new FlatLineBorder( new Insets( py, px, py, px ), monthView.getTodayBackground() );
if( border == null )
border = new FlatEmptyBorder( py, px, py, px );
c.setBorder( border );
return c;
}
}
}