/plugins/BufferList/tags/bufferlist_0_6_2/bufferlist/table/PersistentTableColumnModel.java

# · Java · 117 lines · 66 code · 23 blank · 28 comment · 11 complexity · fa19f4ec799c31c9fd2cd76f001c53a0 MD5 · raw file

  1. /*
  2. * PersistentTableColumnModel.java
  3. * Copyright (c) 2001 Dirk Moebius
  4. *
  5. * :tabSize=4:indentSize=4:noTabs=false:maxLineLen=0:
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package bufferlist.table;
  22. import java.util.Enumeration;
  23. import javax.swing.table.DefaultTableColumnModel;
  24. import javax.swing.table.TableCellRenderer;
  25. import javax.swing.table.TableColumn;
  26. import org.gjt.sp.jedit.jEdit;
  27. /**
  28. * A table column model for both tables, that stores order and sizes of the
  29. * displayed columns.
  30. */
  31. public class PersistentTableColumnModel extends DefaultTableColumnModel
  32. {
  33. private TableCellRenderer iconRenderer = new IconCellRenderer();
  34. private TableCellRenderer nameRenderer = new NameCellRenderer();
  35. private TableCellRenderer toolTipRenderer = new ToolTipCellRenderer();
  36. public PersistentTableColumnModel(int tableNum, int modelColumnCount) {
  37. super();
  38. this.modelColumnCount = modelColumnCount;
  39. this.tableNum = tableNum;
  40. for (int i = 0; i < modelColumnCount; i++) {
  41. String prefix = "bufferlist.table"+ tableNum + ".column" + i;
  42. // get model index:
  43. int modelIndex = i;
  44. String sModelIndex = jEdit.getProperty(prefix + ".modelIndex", Integer.toString(i));
  45. try {
  46. modelIndex = Integer.parseInt(sModelIndex);
  47. }
  48. catch (NumberFormatException ex) {}
  49. if (jEdit.getBooleanProperty("bufferlist.showColumn" + modelIndex, true)) {
  50. // get column width:
  51. int width = 75;
  52. String sWidth = jEdit.getProperty(prefix + ".width", "75");
  53. try {
  54. width = Integer.parseInt(sWidth);
  55. }
  56. catch (NumberFormatException ex) {}
  57. // get cell renderer
  58. TableCellRenderer tcr =
  59. (modelIndex == 0) ? iconRenderer :
  60. (modelIndex == 1) ? nameRenderer :
  61. toolTipRenderer;
  62. // add table column
  63. TableColumn tc = new TableColumn(modelIndex, width, tcr, null);
  64. tc.setHeaderValue(jEdit.getProperty("bufferlist.table.column" + modelIndex));
  65. addColumn(tc);
  66. }
  67. }
  68. }
  69. public void save() {
  70. int maxCol = getColumnCount();
  71. for (int modelIndex = 0; modelIndex < modelColumnCount; modelIndex++) {
  72. int colIndex = findColumnForModelIndex(modelIndex);
  73. if (colIndex != -1) {
  74. TableColumn tc = getColumn(colIndex);
  75. String prefix = "bufferlist.table" + tableNum + ".column" + colIndex;
  76. jEdit.setProperty(prefix + ".width", Integer.toString(tc.getWidth()));
  77. jEdit.setProperty(prefix + ".modelIndex", Integer.toString(modelIndex));
  78. } else {
  79. String prefix = "bufferlist.table" + tableNum + ".column" + maxCol;
  80. jEdit.setProperty(prefix + ".modelIndex", Integer.toString(modelIndex));
  81. maxCol++;
  82. }
  83. }
  84. }
  85. private int findColumnForModelIndex(int modelIndex) {
  86. Enumeration e = tableColumns.elements();
  87. for (int i = 0; e.hasMoreElements(); i++)
  88. if (((TableColumn)e.nextElement()).getModelIndex() == modelIndex)
  89. return i;
  90. return -1;
  91. }
  92. private int modelColumnCount;
  93. private int tableNum;
  94. }