001package jmri.jmrix.can.cbus.swing;
002
003import java.awt.Color;
004import java.text.DateFormat;
005import java.util.Date;
006import java.util.regex.Pattern;
007import javax.swing.BorderFactory;
008import javax.swing.JComponent;
009import javax.swing.JTable;
010import javax.swing.JTextField;
011import javax.swing.text.BadLocationException;
012import jmri.jmrix.can.cbus.CbusEventDataElements.EvState;
013import jmri.jmrix.can.cbus.node.CbusNodeConstants.BackupType;
014
015/**
016 * Common CBUS swing functions.
017 * @author Steve Young Copyright (C) 2020
018 */
019public class CbusCommonSwing {
020    
021    public static final Color VERY_LIGHT_RED = new Color(255,176,173);
022    public static final Color VERY_LIGHT_GREEN = new Color(165,255,164);
023    public static final Color WHITE_GREEN = new Color(0xf5,0xf5,0xf5);
024    public static final Color GOLD = new Color(255,204,51);
025    
026    /**
027     * Set cell background with alternating rows.
028     * @param isSelected true if selected.
029     * @param f cell component.
030     * @param table cell table.
031     * @param row cell row.
032     */
033    public static void setCellBackground( boolean isSelected, JComponent f, JTable table, int row){
034        f.setBackground(isSelected ? table.getSelectionBackground() : 
035            (( row % 2 == 0 ) ? table.getBackground() : WHITE_GREEN ) );
036
037        f.setForeground(isSelected ? table.getSelectionForeground(): table.getForeground());
038
039    }
040    
041    public static void setCellFocus(boolean hasFocus, JComponent f, JTable table) {
042        f.setBorder(hasFocus ? BorderFactory.createMatteBorder(1, 1, 1, 1, Color.blue) : 
043            BorderFactory.createEmptyBorder(1, 1, 1, 1) );
044    }
045    
046    public static void hideNumbersLessThan(int min, String string, JTextField f){
047        try {
048            if (Integer.parseInt(string)<min) {
049                f.setText("");
050            }
051        } catch (NumberFormatException ex) {}
052    }
053    
054    public static void setCellFromCbusEventEnum(Object object, JTextField f){
055        if ( object instanceof EvState ) {
056            EvState state = (EvState) object;
057            switch (state) {
058                case ON:
059                    setTextBackGround(f,Bundle.getMessage("CbusEventOn"),VERY_LIGHT_GREEN);
060                    break;
061                case OFF:
062                    setTextBackGround(f,Bundle.getMessage("CbusEventOff"),VERY_LIGHT_RED);
063                    break;
064                case REQUEST:
065                    setTextBackGround(f,Bundle.getMessage("CbusEventRequest"),GOLD);
066                    break;
067                case UNKNOWN:
068                    f.setText("");
069                    break;
070                default:
071                    break;
072            }
073        }
074    }
075    
076    public static void setCellFromBackupEnum(Object object, JTextField f){
077        if ( object instanceof BackupType ) {
078            BackupType type = (BackupType) object;
079            switch (type) {
080                case INCOMPLETE:
081                    setTextBackGround(f,Bundle.getMessage("BackupIncomplete"),VERY_LIGHT_RED);
082                    break;
083                case COMPLETE:
084                    setTextBackGround(f,Bundle.getMessage("BackupComplete"),VERY_LIGHT_GREEN);
085                    break;
086                case COMPLETEDWITHERROR:
087                    setTextBackGround(f,Bundle.getMessage("BackupCompleteError"),VERY_LIGHT_RED);
088                    break;
089                case NOTONNETWORK:
090                    setTextBackGround(f,Bundle.getMessage("BackupNotOnNetwork"),VERY_LIGHT_RED);
091                    break;
092                case OUTSTANDING:
093                    setTextBackGround(f,Bundle.getMessage("BackupOutstanding"),GOLD);
094                    break;
095                case SLIM:
096                    setTextBackGround(f,Bundle.getMessage("NodeInSlim"),GOLD);
097                    break;
098                default:
099                    break;
100            }
101        }
102    }
103    
104    private static void setTextBackGround(JTextField f, String text, Color bg) {
105        f.setBackground( bg );
106        f.setText(text);
107    }
108    
109    public static void setCellFromDate(Object object, JTextField f, DateFormat dformat){
110        if (object instanceof Date) {
111            f.setText(dformat.format((Date) object));
112        } 
113    }
114    
115    public static void setCellTextHighlighter(String textForSearch, String string, JTextField f){
116        if(Pattern.compile(Pattern.quote(textForSearch), Pattern.CASE_INSENSITIVE).matcher(string).find()){
117            string = string.toLowerCase();
118            textForSearch = textForSearch.toLowerCase();
119            int indexOf = string.indexOf(textForSearch);
120            try {
121                f.getHighlighter().addHighlight(
122                    indexOf,
123                    indexOf+textForSearch.length(),
124                    new javax.swing.text.DefaultHighlighter.DefaultHighlightPainter(Color.CYAN)
125                );
126            } catch (BadLocationException e) {}
127        }
128    }
129    
130    /**
131     * Configure a table to have our standard rows and columns.
132     * <p>
133     * This is optional, in that other table formats can use this table model.
134     * But we put it here to help keep it consistent.
135     * @param table table to configure
136     */
137    public static void configureTable(JTable table) {
138        // allow reordering of the columns
139        table.getTableHeader().setReorderingAllowed(true);
140
141        table.createDefaultColumnsFromModel();
142        
143        // prevent the TableColumnModel from being recreated and loosing custom cell renderers
144        table.setAutoCreateColumnsFromModel(false); 
145        
146        // shut off autoResizeMode to get horizontal scroll to work (JavaSwing p 541)
147        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
148        
149        table.setRowSelectionAllowed(true);
150        table.setColumnSelectionAllowed(false);
151        table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
152        
153        table.setRowHeight(new JTextField("Y").getPreferredSize().height+6);
154        
155        // give table a name for JmriJTablePersistenceManager
156        table.setName(table.getModel().getClass().getName());
157        
158        // resize columns to initial size
159        for (int i = 0; i < table.getColumnCount(); i++) {
160            int width = Math.min(260,new JTextField(table.getColumnName(i)).getPreferredSize().width*2);
161            table.getColumnModel().getColumn(i).setPreferredWidth(width);
162        }
163        table.sizeColumnsToFit(-1);
164        
165    }
166    
167}