Skip to content

Commit

Permalink
extract tableHeader from PreventDraggingJTableHeader.java
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasgeiger committed Mar 2, 2016
1 parent a824074 commit 39f60a5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Copyright (C) 2016 JabRef contributors.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package net.sf.jabref.gui.maintable;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;

public class MainTableHeaderRenderer implements TableCellRenderer {

private final TableCellRenderer delegate;

public MainTableHeaderRenderer(TableCellRenderer delegate) {
this.delegate = delegate;
}

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
// delegate to previously used TableCellRenderer which styles the component
Component resultFromDelegate = delegate.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

// Changing style is only possible if both value and resultFromDelegate are JLabels
if ((value instanceof JLabel) && (resultFromDelegate instanceof JLabel)) {
String text = ((JLabel) value).getText();
Icon icon = ((JLabel) value).getIcon();
if (icon == null) {
((JLabel) resultFromDelegate).setText(text);
} else {
((JLabel) resultFromDelegate).setIcon(icon);
((JLabel) resultFromDelegate).setText(null);
}
}

return resultFromDelegate;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2003-2015 JabRef contributors.
/* Copyright (C) 2003-2016 JabRef contributors.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
Expand All @@ -17,9 +17,7 @@

import javax.swing.*;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.Collections;
import java.util.Enumeration;
Expand All @@ -36,25 +34,21 @@
* @author Fabian Bieker
* @since 12/2008
*/
class PreventDraggingJTableHeader extends JTableHeader implements TableCellRenderer {
class PreventDraggingJTableHeader extends JTableHeader {

private final MainTableFormat tableFormat;

private final TableCellRenderer delegate;

public PreventDraggingJTableHeader(JTable table, MainTableFormat tableFormat) {
super(table.getColumnModel());
this.setTable(table);
this.tableFormat = tableFormat;
this.delegate = table.getTableHeader().getDefaultRenderer();
setupTableHeaderIcons();
}

private void setupTableHeaderIcons() {

Enumeration<TableColumn> columns = columnModel.getColumns();
for (TableColumn column : Collections.list(columns)) {
column.setHeaderRenderer(this);
MainTableColumn mainTableColumn = tableFormat.getTableColumn(column.getModelIndex());
column.setHeaderValue(mainTableColumn.getHeaderLabel());
}
Expand All @@ -76,16 +70,15 @@ public String getToolTipText(MouseEvent event) {
public void setDraggedColumn(TableColumn column) {

if ((column != null) && (column.getModelIndex() == 0)) {
return;
return;
}
super.setDraggedColumn(column);
}

/**
* Overridden to prevent dragging of an other column before the first column ("#").
*/
@Override
public TableColumn getDraggedColumn() {
@Override public TableColumn getDraggedColumn() {
TableColumn column = super.getDraggedColumn();
if (column != null) {
PreventDraggingJTableHeader.preventDragBeforeNumberColumn(this.getTable(), column.getModelIndex());
Expand All @@ -94,28 +87,6 @@ public TableColumn getDraggedColumn() {
return column;
}

@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {

// delegate to previously used TableCellRenderer which styles the component
Component resultFromDelegate = delegate.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

// Changing style is only possible if both value and resultFromDelegate are JLabels
if ((value instanceof JLabel) && (resultFromDelegate instanceof JLabel)) {
String text = ((JLabel) value).getText();
Icon icon = ((JLabel) value).getIcon();
if (icon == null) {
((JLabel) resultFromDelegate).setText(text);
} else {
((JLabel) resultFromDelegate).setIcon(icon);
((JLabel) resultFromDelegate).setText(null);
}
}

return resultFromDelegate;
}

/**
* Transform model index <code>modelIndex</code> to a view based index and
* prevent dragging before model index <code>toIndex</code> (inclusive).
Expand Down

0 comments on commit 39f60a5

Please sign in to comment.