TableHeader implementation

This commit is contained in:
Karl Tauber
2019-09-02 17:03:49 +02:00
parent 03f4cda907
commit 8754ffe1cb
7 changed files with 649 additions and 0 deletions

View File

@@ -0,0 +1,197 @@
/*
* 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.ui;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTableHeaderUI;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import com.formdev.flatlaf.util.UIScale;
/**
* Provides the Flat LaF UI delegate for {@link javax.swing.table.JTableHeader}.
*
* TODO document used UI defaults of superclass
*
* @uiDefault TableHeader.separatorColor Color
* @uiDefault TableHeader.bottomSeparatorColor Color
* @uiDefault TableHeader.height int
*
* @author Karl Tauber
*/
public class FlatTableHeaderUI
extends BasicTableHeaderUI
{
protected Color separatorColor;
protected Color bottomSeparatorColor;
protected int height;
public static ComponentUI createUI( JComponent c ) {
return new FlatTableHeaderUI();
}
@Override
protected void installDefaults() {
super.installDefaults();
separatorColor = UIManager.getColor( "TableHeader.separatorColor" );
bottomSeparatorColor = UIManager.getColor( "TableHeader.bottomSeparatorColor" );
height = UIManager.getInt( "TableHeader.height" );
}
@Override
protected void uninstallDefaults() {
super.uninstallDefaults();
separatorColor = null;
bottomSeparatorColor = null;
}
@Override
public void paint( Graphics g, JComponent c ) {
// do not paint borders if JTableHeader.setDefaultRenderer() was used
boolean paintBorders = header.getDefaultRenderer().getClass().getName().equals(
"sun.swing.table.DefaultTableCellHeaderRenderer" );
if( paintBorders )
paintColumnBorders( g, c );
super.paint( g, c );
if( paintBorders )
paintDraggedColumnBorders( g, c );
}
private void paintColumnBorders( Graphics g, JComponent c ) {
int width = c.getWidth();
int height = c.getHeight();
float lineWidth = UIScale.scale( 1f );
float topLineIndent = lineWidth;
float bottomLineIndent = lineWidth * 3;
TableColumnModel columnModel = header.getColumnModel();
int columnCount = columnModel.getColumnCount();
Graphics2D g2 = (Graphics2D) g.create();
try {
FlatUIUtils.setRenderingHints( g2 );
// paint bottom line
g2.setColor( bottomSeparatorColor );
g2.fill( new Rectangle2D.Float( 0, height - lineWidth, width, lineWidth ) );
// paint column separator lines
g2.setColor( separatorColor );
int sepCount = columnCount;
if( header.getTable().getAutoResizeMode() != JTable.AUTO_RESIZE_OFF && !isVerticalScrollBarVisible() )
sepCount--;
if( header.getComponentOrientation().isLeftToRight() ) {
int x = 0;
for( int i = 0; i < sepCount; i++ ) {
x += columnModel.getColumn( i ).getWidth();
g2.fill( new Rectangle2D.Float( x - lineWidth, topLineIndent, lineWidth, height - bottomLineIndent ) );
}
} else {
int x = width;
for( int i = 0; i < sepCount; i++ ) {
x -= columnModel.getColumn( i ).getWidth();
g2.fill( new Rectangle2D.Float( x - lineWidth, topLineIndent, lineWidth, height - bottomLineIndent ) );
}
}
} finally {
g2.dispose();
}
}
private void paintDraggedColumnBorders( Graphics g, JComponent c ) {
TableColumn draggedColumn = header.getDraggedColumn();
if( draggedColumn == null )
return;
// find index of dragged column
TableColumnModel columnModel = header.getColumnModel();
int columnCount = columnModel.getColumnCount();
int draggedColumnIndex = -1;
for( int i = 0; i < columnCount; i++ ) {
if( columnModel.getColumn( i ) == draggedColumn ) {
draggedColumnIndex = i;
break;
}
}
if( draggedColumnIndex < 0 )
return;
float lineWidth = UIScale.scale( 1f );
float topLineIndent = lineWidth;
float bottomLineIndent = lineWidth * 3;
Rectangle r = header.getHeaderRect( draggedColumnIndex );
r.x += header.getDraggedDistance();
Graphics2D g2 = (Graphics2D) g.create();
try {
FlatUIUtils.setRenderingHints( g2 );
// paint dragged bottom line
g2.setColor( bottomSeparatorColor );
g2.fill( new Rectangle2D.Float( r.x, r.y + r.height - lineWidth, r.width, lineWidth ) );
// paint dragged column separator lines
g2.setColor( separatorColor );
g2.fill( new Rectangle2D.Float( r.x, topLineIndent, lineWidth, r.height - bottomLineIndent ) );
g2.fill( new Rectangle2D.Float( r.x + r.width - lineWidth, r.y + topLineIndent, lineWidth, r.height - bottomLineIndent ) );
} finally {
g2.dispose();
}
}
@Override
public Dimension getPreferredSize( JComponent c ) {
Dimension size = super.getPreferredSize( c );
if( size.height > 0 )
size.height = Math.max( size.height, UIScale.scale( height ) );
return size;
}
private boolean isVerticalScrollBarVisible() {
JScrollPane scrollPane = getScrollPane();
return (scrollPane != null && scrollPane.getVerticalScrollBar() != null)
? scrollPane.getVerticalScrollBar().isVisible()
: false;
}
private JScrollPane getScrollPane() {
Container parent = header.getParent();
if( parent == null )
return null;
parent = parent.getParent();
return (parent instanceof JScrollPane) ? (JScrollPane) parent : null;
}
}

View File

@@ -169,5 +169,12 @@ TabbedPane.focusColor=3d4b5c
TabbedPane.contentAreaColor=323232
#---- TableHeader ----
TableHeader.background=45494A
TableHeader.separatorColor=585858
TableHeader.bottomSeparatorColor=585858
#---- Tree ----

View File

@@ -39,6 +39,7 @@ SliderUI=com.formdev.flatlaf.ui.FlatSliderUI
SpinnerUI=com.formdev.flatlaf.ui.FlatSpinnerUI
SplitPaneUI=com.formdev.flatlaf.ui.FlatSplitPaneUI
TabbedPaneUI=com.formdev.flatlaf.ui.FlatTabbedPaneUI
TableHeaderUI=com.formdev.flatlaf.ui.FlatTableHeaderUI
TextAreaUI=com.formdev.flatlaf.ui.FlatTextAreaUI
TextFieldUI=com.formdev.flatlaf.ui.FlatTextFieldUI
TextPaneUI=com.formdev.flatlaf.ui.FlatTextPaneUI
@@ -215,6 +216,12 @@ TabbedPane.tabsOverlapBorder=true
TabbedPane.shadow=@@ComboBox.buttonArrowColor
#---- TableHeader ----
TableHeader.height=25
TableHeader.cellBorder=2,2,2,2
#---- TextArea ----
TextArea.border=com.formdev.flatlaf.ui.FlatMarginBorder

View File

@@ -169,5 +169,12 @@ TabbedPane.focusColor=dae4ed
TabbedPane.contentAreaColor=bfbfbf
#---- TableHeader ----
TableHeader.background=ffffff
TableHeader.separatorColor=e5e5e5
TableHeader.bottomSeparatorColor=e5e5e5
#---- Tree ----

View File

@@ -0,0 +1,246 @@
/*
* 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;
import javax.swing.*;
import javax.swing.table.*;
import net.miginfocom.swing.*;
/**
* @author Karl Tauber
*/
public class FlatComponents2Test
extends JPanel
{
public static void main( String[] args ) {
FlatTestFrame frame = FlatTestFrame.create( args, "FlatComponents2Test" );
frame.showFrame( new FlatComponents2Test() );
}
FlatComponents2Test() {
initComponents();
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
JLabel labelLabel = new JLabel();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel textFieldLabel = new JLabel();
JTextField textField1 = new JTextField();
JTextField textField2 = new JTextField();
JLabel listLabel = new JLabel();
JScrollPane scrollPane1 = new JScrollPane();
JList<String> list1 = new JList<>();
JScrollPane scrollPane2 = new JScrollPane();
JList<String> list2 = new JList<>();
JLabel treeLabel = new JLabel();
JScrollPane scrollPane3 = new JScrollPane();
JTree tree1 = new JTree();
JScrollPane scrollPane4 = new JScrollPane();
JTree tree2 = new JTree();
JLabel tableLabel = new JLabel();
JScrollPane scrollPane5 = new JScrollPane();
JTable table1 = new JTable();
//======== this ========
setLayout(new MigLayout(
"insets 0,hidemode 3,gap 5 5,ltr",
// columns
"[]" +
"[200]" +
"[200]",
// rows
"[]" +
"[]" +
"[]" +
"[::200]" +
"[::150]"));
//---- labelLabel ----
labelLabel.setText("JLabel:");
add(labelLabel, "cell 0 0");
//---- label1 ----
label1.setText("enabled");
label1.setDisplayedMnemonic('E');
add(label1, "cell 1 0");
//---- label2 ----
label2.setText("disabled");
label2.setDisplayedMnemonic('D');
label2.setEnabled(false);
add(label2, "cell 2 0");
//---- textFieldLabel ----
textFieldLabel.setText("JTextField:");
add(textFieldLabel, "cell 0 1");
//---- textField1 ----
textField1.setText("editable");
add(textField1, "cell 1 1,growx");
//---- textField2 ----
textField2.setText("disabled");
textField2.setEnabled(false);
add(textField2, "cell 2 1,growx");
//---- listLabel ----
listLabel.setText("JList:");
add(listLabel, "cell 0 2");
//======== scrollPane1 ========
{
scrollPane1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
scrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//---- list1 ----
list1.setModel(new AbstractListModel<String>() {
String[] values = {
"abc",
"de",
"f"
};
@Override
public int getSize() { return values.length; }
@Override
public String getElementAt(int i) { return values[i]; }
});
scrollPane1.setViewportView(list1);
}
add(scrollPane1, "cell 1 2,growx");
//======== scrollPane2 ========
{
scrollPane2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
scrollPane2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//---- list2 ----
list2.setModel(new AbstractListModel<String>() {
String[] values = {
"abc",
"de",
"f"
};
@Override
public int getSize() { return values.length; }
@Override
public String getElementAt(int i) { return values[i]; }
});
list2.setEnabled(false);
scrollPane2.setViewportView(list2);
}
add(scrollPane2, "cell 2 2,growx");
//---- treeLabel ----
treeLabel.setText("JTree:");
add(treeLabel, "cell 0 3");
//======== scrollPane3 ========
{
//---- tree1 ----
tree1.setShowsRootHandles(true);
scrollPane3.setViewportView(tree1);
}
add(scrollPane3, "cell 1 3,growx");
//======== scrollPane4 ========
{
//---- tree2 ----
tree2.setEnabled(false);
scrollPane4.setViewportView(tree2);
}
add(scrollPane4, "cell 2 3,growx");
//---- tableLabel ----
tableLabel.setText("JTable:");
add(tableLabel, "cell 0 4");
//======== scrollPane5 ========
{
//---- table1 ----
table1.setModel(new DefaultTableModel(
new Object[][] {
{"Item 1a", "Item 2a", "January", "July", null},
{"Item 1b", "Item 2b", "February", "August", true},
},
new String[] {
"Not editable", "Text", "Combo", "Combo Editable", "Boolean"
}
) {
Class<?>[] columnTypes = new Class<?>[] {
Object.class, Object.class, String.class, String.class, Boolean.class
};
boolean[] columnEditable = new boolean[] {
false, true, true, true, true
};
@Override
public Class<?> getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnEditable[columnIndex];
}
});
{
TableColumnModel cm = table1.getColumnModel();
cm.getColumn(2).setCellEditor(new DefaultCellEditor(
new JComboBox(new DefaultComboBoxModel(new String[] {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
}))));
cm.getColumn(3).setCellEditor(new DefaultCellEditor(
new JComboBox(new DefaultComboBoxModel(new String[] {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
}))));
}
scrollPane5.setViewportView(table1);
}
add(scrollPane5, "cell 1 4 2 1,growx,width 300");
// JFormDesigner - End of component initialization //GEN-END:initComponents
((JComboBox)((DefaultCellEditor)table1.getColumnModel().getColumn( 3 ).getCellEditor()).getComponent()).setEditable( true );
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
// JFormDesigner - End of variables declaration //GEN-END:variables
}

View File

@@ -0,0 +1,177 @@
JFDML JFormDesigner: "7.0.0.0.194" Java: "11.0.2" encoding: "UTF-8"
new FormModel {
contentType: "form/swing"
root: new FormRoot {
auxiliary() {
"JavaCodeGenerator.defaultVariableLocal": true
}
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "insets 0,hidemode 3,gap 5 5,ltr"
"$columnConstraints": "[][200][200]"
"$rowConstraints": "[][][][::200][::150]"
} ) {
name: "this"
add( new FormComponent( "javax.swing.JLabel" ) {
name: "labelLabel"
"text": "JLabel:"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 0"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label1"
"text": "enabled"
"displayedMnemonic": 69
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 0"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label2"
"text": "disabled"
"displayedMnemonic": 68
"enabled": false
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 0"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "textFieldLabel"
"text": "JTextField:"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 1"
} )
add( new FormComponent( "javax.swing.JTextField" ) {
name: "textField1"
"text": "editable"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 1,growx"
} )
add( new FormComponent( "javax.swing.JTextField" ) {
name: "textField2"
"text": "disabled"
"enabled": false
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 1,growx"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "listLabel"
"text": "JList:"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 2"
} )
add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) {
name: "scrollPane1"
"verticalScrollBarPolicy": 21
"horizontalScrollBarPolicy": 31
add( new FormComponent( "javax.swing.JList" ) {
name: "list1"
"model": &DefaultListModel0 new javax.swing.DefaultListModel {
addElement( "abc" )
addElement( "de" )
addElement( "f" )
}
auxiliary() {
"JavaCodeGenerator.typeParameters": "String"
}
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2,growx"
} )
add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) {
name: "scrollPane2"
"verticalScrollBarPolicy": 21
"horizontalScrollBarPolicy": 31
add( new FormComponent( "javax.swing.JList" ) {
name: "list2"
"model": #DefaultListModel0
"enabled": false
auxiliary() {
"JavaCodeGenerator.typeParameters": "String"
}
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 2,growx"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "treeLabel"
"text": "JTree:"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 3"
} )
add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) {
name: "scrollPane3"
add( new FormComponent( "javax.swing.JTree" ) {
name: "tree1"
"showsRootHandles": true
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 3,growx"
} )
add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) {
name: "scrollPane4"
add( new FormComponent( "javax.swing.JTree" ) {
name: "tree2"
"enabled": false
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 3,growx"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "tableLabel"
"text": "JTable:"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 4"
} )
add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) {
name: "scrollPane5"
add( new FormComponent( "javax.swing.JTable" ) {
name: "table1"
"model": new com.jformdesigner.model.SwingTableModel( new java.util.Vector {
add( new java.util.Vector {
add( "Item 1a" )
add( "Item 2a" )
add( "January" )
add( "July" )
add( null )
} )
add( new java.util.Vector {
add( "Item 1b" )
add( "Item 2b" )
add( "February" )
add( "August" )
add( true )
} )
}, new java.util.Vector {
add( "Not editable" )
add( "Text" )
add( "Combo" )
add( "Combo Editable" )
add( "Boolean" )
}, new java.util.Vector {
add( null )
add( null )
add( class java.lang.String )
add( class java.lang.String )
add( class java.lang.Boolean )
}, new java.util.Vector {
add( false )
add( null )
add( null )
add( null )
add( null )
}, new java.util.Vector {
add( null )
add( null )
add( new com.jformdesigner.model.SwingTableColumn( new java.lang.Object[ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ], 0, 0, 0, true ) )
add( new com.jformdesigner.model.SwingTableColumn( new java.lang.Object[ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ], 0, 0, 0, true ) )
add( null )
} )
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 4 2 1,growx,width 300"
} )
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 )
"size": new java.awt.Dimension( 790, 715 )
} )
}
}

View File

@@ -179,5 +179,13 @@ TabbedPane.focusColor=dddddd
TabbedPane.contentAreaColor=bbbbbb
#---- TableHeader ----
TableHeader.background=4444ff
TableHeader.foreground=ffffff
TableHeader.separatorColor=00ff00
TableHeader.bottomSeparatorColor=00ff00
#---- Tree ----