yetanotheraprsc/yetanotheraprsc-code/.svn/pristine/08/08cc1dd83954b54b23ad786a507...

120 lines
4.7 KiB
Plaintext

package org.ka2ddo.yaac.gui.table;
/*
* Copyright (C) 2011-2018 Andrew Pavlin, KA2DDO
* This file is part of YAAC (Yet Another APRS Client).
*
* YAAC is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* YAAC 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
* and GNU Lesser General Public License along with YAAC. If not,
* see <http://www.gnu.org/licenses/>.
*/
import org.ka2ddo.aprs.Symbols;
import org.ka2ddo.yaac.gui.GuiSymbols;
import org.ka2ddo.yaac.gui.SelectableSymbol;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
/**
* This class implements a renderer for SelectableSymbols that shows both the icon and
* the characters representing the icon in the network packets.
*/
public class SelectableSymbolRenderer extends JLabel implements TableCellRenderer {
private final boolean alwaysShowSymbolChars;
/**
* Create a SelectableSymbolRenderer that only shows the symbol characters when it is an overlay.
*/
public SelectableSymbolRenderer() {
setOpaque(true);
alwaysShowSymbolChars = false;
}
/**
* Create a SelectableSymbolRenderer that selectively decides whether to show the symbol characters.
* @param alwaysShowSymbolChars boolean true to always show symbol characters, else only if overlay
*/
public SelectableSymbolRenderer(boolean alwaysShowSymbolChars) {
setOpaque(true);
this.alwaysShowSymbolChars = alwaysShowSymbolChars;
}
/**
* Returns the component used for drawing the cell. This method is
* used to configure the renderer appropriately before drawing.
*
* @param table the <code>JTable</code> that is asking the
* renderer to draw; can be <code>null</code>
* @param value the value of the cell to be rendered. It is
* up to the specific renderer to interpret
* and draw the value. For example, if
* <code>value</code>
* is the string "true", it could be rendered as a
* string or it could be rendered as a check
* box that is checked. <code>null</code> is a
* valid value
* @param isSelected true if the cell is to be rendered with the
* selection highlighted; otherwise false
* @param hasFocus if true, render cell appropriately. For
* example, put a special border on the cell, if
* the cell can be edited, render in the color used
* to indicate editing
* @param row the row index of the cell being drawn. When
* drawing the header, the value of
* <code>row</code> is -1
* @param column the column index of the cell being drawn
*/
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
SelectableSymbol ss = (SelectableSymbol)value;
Symbols.SymbolAttr symbolAttr = null;
if (ss != null) {
symbolAttr = Symbols.getSymbolAttr(ss.symTableId, ss.symbolCode);
}
if (ss != null && alwaysShowSymbolChars) {
setText(new String(new char[]{ss.symTableId, ss.symbolCode}));
if (symbolAttr != null) {
if (symbolAttr.img != null) {
setIcon(((GuiSymbols.GuiSymbolImage)symbolAttr.img).getIcon());
} else {
setIcon(GuiSymbols.getStationIcon('\\', ss.symbolCode));
}
setToolTipText(symbolAttr.getTypeName());
} else {
setIcon(null);
setToolTipText("");
}
} else if (symbolAttr != null) {
if (ss.symTableId != symbolAttr.getAprsSymTableId()) {
setText(new String(new char[]{ss.symTableId}));
} else {
setText("");
}
setIcon(GuiSymbols.getStationIcon(ss.symTableId, ss.symbolCode));
setToolTipText(symbolAttr.getTypeName());
} else {
setText("");
setIcon(null);
setToolTipText("");
}
if (isSelected) {
setBackground(table.getSelectionBackground());
setForeground(table.getSelectionForeground());
} else {
setBackground(table.getBackground());
setForeground(table.getForeground());
}
return this;
}
}