PageRenderTime 225ms CodeModel.GetById 61ms app.highlight 66ms RepoModel.GetById 95ms app.codeStats 0ms

/src/mpv5/utils/tables/TableFormat.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 371 lines | 229 code | 51 blank | 91 comment | 33 complexity | 99db998260f8f68d6c85bf52e714bde2 MD5 | raw file
  1
  2/*
  3 *  This file is part of YaBS.
  4 *
  5 *      YaBS is free software: you can redistribute it and/or modify
  6 *      it under the terms of the GNU General Public License as published by
  7 *      the Free Software Foundation, either version 3 of the License, or
  8 *      (at your option) any later version.
  9 *
 10 *      YaBS is distributed in the hope that it will be useful,
 11 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 *      GNU General Public License for more details.
 14 *
 15 *      You should have received a copy of the GNU General Public License
 16 *      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 17 */
 18package mpv5.utils.tables;
 19
 20//~--- non-JDK imports --------------------------------------------------------
 21import com.l2fprod.common.swing.renderer.ColorCellRenderer;
 22import java.util.Enumeration;
 23import javax.swing.table.TableColumn;
 24import javax.swing.table.TableColumnModel;
 25
 26import mpv5.db.common.DatabaseObject;
 27import mpv5.db.common.DatabaseObject.Entity;
 28
 29import mpv5.globals.LocalSettings;
 30
 31import mpv5.logging.Log;
 32
 33import mpv5.utils.models.MPTableModel;
 34
 35//~--- JDK imports ------------------------------------------------------------
 36
 37import java.awt.Color;
 38import java.awt.Component;
 39import java.awt.Dimension;
 40import java.awt.Font;
 41
 42import javax.swing.BorderFactory;
 43import javax.swing.JComponent;
 44import javax.swing.JLabel;
 45import javax.swing.JTable;
 46import javax.swing.event.ChangeEvent;
 47import javax.swing.event.ListSelectionEvent;
 48import javax.swing.event.TableColumnModelEvent;
 49import javax.swing.event.TableColumnModelListener;
 50import javax.swing.table.DefaultTableCellRenderer;
 51import javax.swing.table.DefaultTableModel;
 52import javax.swing.table.TableCellEditor;
 53import javax.swing.table.TableCellRenderer;
 54
 55/**
 56 *
 57 *
 58 */
 59public class TableFormat {
 60
 61    /**
 62     * Changes table values from text to class values,<br/>
 63     * currently supported classes:<br/>
 64     * - <code>Boolean.class</code>: "1" and "true" will be true, false otherwise
 65     * @param values The original values
 66     * @param aClass The class to be changed
 67     * @param cols The column to change
 68     * @return
 69     */
 70    public static Object[][] changeToClassValue(Object[][] values, Class aClass, int[] cols) {
 71        try {
 72            Object[][] data = new Object[values.length][values[0].length];
 73
 74            for (int idx = 0; idx < values.length; idx++) {
 75                for (int k = 0; k < cols.length; k++) {
 76                    if (aClass.getName().equalsIgnoreCase("java.lang.Boolean")) {
 77                        if (String.valueOf(values[idx][cols[k]]).equalsIgnoreCase("1")
 78                                || String.valueOf(values[idx][cols[k]]).equalsIgnoreCase("true")) {
 79                            data[idx][cols[k]] = true;
 80                        } else {
 81                            data[idx][cols[k]] = false;
 82                        }
 83                    }
 84                }
 85
 86                for (int h = 0; h < values[0].length; h++) {
 87                    if (data[idx][h] == null) {
 88                        data[idx][h] = values[idx][h];
 89                    }
 90                }
 91            }
 92
 93            return data;
 94        } catch (Exception e) {
 95
 96//          Log.Debug(this,e);
 97            return new Object[0][0];
 98        }
 99    }
100
101    public static void makeUneditable(JTable table) {
102        try {
103            ((MPTableModel) table.getModel()).setCanEdits(new boolean[]{
104                        false, false, false, false, false, false, false, false, false, false, false, false, false
105                    });
106        } catch (Exception e) {
107            Log.Debug(TableFormat.class, "Can not change this table to uneditable.");
108        }
109    }
110
111    public static void makeUneditableColumns(JTable table, Integer[] desiredCol) {
112        boolean[] unedits;
113
114        try {
115            unedits = new boolean[desiredCol.length];
116
117            for (int i = 0; i < desiredCol.length; i++) {
118                if (desiredCol[i] != null) {
119                    unedits[i] = false;
120                } else {
121                    unedits[i] = true;
122                }
123            }
124
125            ((MPTableModel) table.getModel()).setCanEdits(unedits);
126        } catch (Exception e) {
127            Log.Debug(TableFormat.class, "Can not change this table to uneditable.");
128        }
129    }
130
131    /**
132     * Stops the tables' cell editor
133     * @param jTable1
134     */
135    public static void stopEditing(JTable jTable1) {
136        TableCellEditor editor = jTable1.getCellEditor();
137
138        if (editor != null) {
139            try {
140                editor.stopCellEditing();
141            } catch (Exception e) {
142            }
143        }
144    }
145
146    /**
147     * Resizes a tables cols
148     * @param table
149     * @param desiredColSizes
150     * @param fixed Should the cols be non-resizable
151     */
152    public static void resizeCols(JTable table, Integer[] desiredColSizes, boolean fixed) {
153        for (int i = 0; i < desiredColSizes.length; i++) {
154            if (desiredColSizes[i] != null) {
155                try {
156                    table.getColumnModel().getColumn(i).setMinWidth(desiredColSizes[i]);
157                    table.getColumnModel().getColumn(i).setPreferredWidth(desiredColSizes[i]);
158
159                    if (fixed) {
160                        table.getColumnModel().getColumn(i).setMaxWidth(desiredColSizes[i]);
161                    } else {
162                        table.getColumnModel().getColumn(i).setMaxWidth(1000);
163                    }
164                } catch (Exception e) {
165                    Log.Debug(TableFormat.class, e.getMessage());
166                }
167            }
168        }
169    }
170
171    /**
172     * Resizes a tables cols
173     * @param table
174     * @param columnIndex 
175     * @param desiredColSize
176     * @param fixed Should the cols be non-resizable
177     */
178    public static void resizeCol(JTable table, int columnIndex, int desiredColSize, boolean fixed) {
179        try {
180            table.getColumnModel().getColumn(columnIndex).setMinWidth(desiredColSize);
181            table.getColumnModel().getColumn(columnIndex).setPreferredWidth(desiredColSize);
182
183            if (fixed) {
184                table.getColumnModel().getColumn(columnIndex).setMaxWidth(desiredColSize);
185            } else {
186                table.getColumnModel().getColumn(columnIndex).setMaxWidth(1000);
187            }
188        } catch (Exception e) {
189            Log.Debug(TableFormat.class, e.getMessage());
190        }
191    }
192
193    public static DefaultTableModel getUneditableTable(String[][] data, String[] header) {
194        return new javax.swing.table.DefaultTableModel(data, header) {
195
196            boolean[] canEdits = new boolean[]{
197                false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
198                false, false, false, false, false, false, false, false, false, false, false, false, false, false, false
199            };
200
201            @Override
202            public boolean isCellEditable(int rowIndex, int columnIndex) {
203                return canEdits[columnIndex];
204            }
205        };
206    }
207
208    /**
209     * Hide a column of a table
210     * @param table
211     * @param columnToHide from model
212     */
213    public static void stripColumn(JTable table, int columnToHide) {
214        try {
215            table.getColumnModel().getColumn(columnToHide).setWidth(0);
216            table.getColumnModel().getColumn(columnToHide).setPreferredWidth(0);
217            table.getColumnModel().getColumn(columnToHide).setMinWidth(0);
218            table.getColumnModel().getColumn(columnToHide).setMaxWidth(0);
219            table.doLayout();
220        } catch (Exception e) {
221        }
222    }
223
224    /**
225     * Hide the columns
226     * @param resulttable
227     * @param i
228     */
229    public static void stripColumns(JTable resulttable, int[] i) {
230        for (int j = 0; j < i.length; j++) {
231            int k = i[j];
232
233            try {
234                stripColumn(resulttable, k);
235            } catch (Exception e) {
236            }
237        }
238    }
239
240    /**
241     * Hide columns with a specific column class
242     * @param jTable1
243     * @param aClass
244     */
245    public static void stripColumn(JTable table, Class<Entity> aClass) {
246        for (int i = 0; i < table.getColumnCount(); i++) {
247            if (table.getColumnClass(i).equals(aClass)) {
248                stripColumn(table, table.convertColumnIndexToModel(i));
249            }
250        }
251    }
252
253    /**
254     * Hide the first column of a table
255     * @param table
256     */
257    public static void stripFirstColumn(JTable table) {
258        stripColumn(table, 0);
259    }
260
261    /**
262     *
263     * @param table
264     * @param column
265     * @param width
266     */
267    public static void format(JTable table, int column, int width) {
268        try {
269            table.getColumnModel().getColumn(column).setWidth(width);
270            table.getColumnModel().getColumn(column).setPreferredWidth(width);
271            table.getColumnModel().getColumn(column).setMinWidth(width);
272            table.getColumnModel().getColumn(column).setMaxWidth(width);
273        } catch (Exception e) {
274        }
275    }
276
277    /**
278     *
279     * @param table
280     * @param desiredColSizes
281     * @param fixedCols
282     */
283    public static void resizeCols(JTable table, Integer[] desiredColSizes, Boolean[] fixedCols) {
284        for (int i = 0; i < desiredColSizes.length; i++) {
285            if (desiredColSizes[i] != null) {
286                table.getColumnModel().getColumn(i).setPreferredWidth(desiredColSizes[i]);
287
288                if ((fixedCols.length > 0) && (fixedCols[i] != null) && fixedCols[i]) {
289                    table.getColumnModel().getColumn(i).setMinWidth(desiredColSizes[i]);
290                    table.getColumnModel().getColumn(i).setMaxWidth(desiredColSizes[i]);
291                } else {
292                    table.getColumnModel().getColumn(i).setMinWidth(0);
293                    table.getColumnModel().getColumn(i).setMaxWidth(1000);
294                }
295            }
296        }
297    }
298
299    /**
300     * Overwrites any other renderer for this column..
301     * @param table
302     * @param column
303     * @param color
304     */
305    public static void changeBackground(final JTable table, final int column, final Color color) {
306        class ColorRenderer extends JLabel implements TableCellRenderer {
307
308            public ColorRenderer() {
309                this.setOpaque(true);
310                this.setHorizontalAlignment(JLabel.CENTER);
311            }
312
313            @Override
314            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
315                    boolean hasFocus, int row, int col) {
316                try {
317                    this.setText(value.toString());
318                } catch (Exception e) {
319                }
320
321                this.setBackground(color);
322
323                return this;
324            }
325        }
326        table.getColumnModel().getColumn(column).setCellRenderer(new ColorRenderer());
327    }
328
329    /**
330     * Hides the header of a table by setting it's header height to 0
331     * @param table
332     */
333    public static void hideHeader(JTable table) {
334        table.getTableHeader().setPreferredSize(new Dimension(table.getColumnModel().getTotalColumnWidth(), 15));
335        table.getTableHeader().setFont(new Font(LocalSettings.getProperty(LocalSettings.DEFAULT_FONT), Font.PLAIN, 1));
336        class ColumnListener implements TableColumnModelListener {
337
338            private final JTable table;
339
340            private ColumnListener(JTable target) {
341                this.table = target;
342            }
343
344            public void columnAdded(TableColumnModelEvent e) {
345                table.getTableHeader().setPreferredSize(new Dimension(table.getColumnModel().getTotalColumnWidth(),
346                        15));
347            }
348
349            public void columnRemoved(TableColumnModelEvent e) {
350                table.getTableHeader().setPreferredSize(new Dimension(table.getColumnModel().getTotalColumnWidth(),
351                        15));
352            }
353
354            public void columnMoved(TableColumnModelEvent e) {
355                table.getTableHeader().setPreferredSize(new Dimension(table.getColumnModel().getTotalColumnWidth(),
356                        15));
357            }
358
359            public void columnMarginChanged(ChangeEvent e) {
360                table.getTableHeader().setPreferredSize(new Dimension(table.getColumnModel().getTotalColumnWidth(),
361                        15));
362            }
363
364            public void columnSelectionChanged(ListSelectionEvent e) {
365            }
366        }
367        table.getColumnModel().addColumnModelListener(new ColumnListener(table));
368    }
369}
370//~ Formatted by Jindent --- http://www.jindent.com
371