PageRenderTime 13ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/data/MPList.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 128 lines | 73 code | 12 blank | 43 comment | 9 complexity | 2dd5d54a36e5831ab55bcdf016a7c2c0 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.data;
  18. import com.lowagie.text.ListItem;
  19. import java.awt.Color;
  20. import java.awt.Component;
  21. import java.util.Vector;
  22. import javax.swing.BorderFactory;
  23. import javax.swing.DefaultListModel;
  24. import javax.swing.JLabel;
  25. import javax.swing.JList;
  26. import javax.swing.ListCellRenderer;
  27. import mpv5.db.common.DatabaseObject;
  28. /**
  29. * This is our own implementation of a list holding {@link DatabaseObject}s
  30. * @author andreasw
  31. */
  32. public class MPList extends Vector<DatabaseObject> {
  33. /**
  34. * Invokes the save method on every entry in the list. If the list is empty, returns true
  35. * @return true if every item in the last was saved successfully
  36. */
  37. public boolean saveAll() {
  38. boolean success = false;
  39. for (int i = 0; i < this.size(); i++) {
  40. if (success) {
  41. success = this.get(i).save();
  42. } else {//save state
  43. this.get(i).save();
  44. }
  45. }
  46. return success;
  47. }
  48. /**
  49. * Invokes the delete method on every entry in the list. If the list is empty, returns true
  50. * @return true if every item in the last was deleted successfully
  51. */
  52. public boolean deleteAll() {
  53. boolean success = false;
  54. for (int i = 0; i < this.size(); i++) {
  55. if (success) {
  56. success = this.get(i).delete();
  57. } else {//save state
  58. this.get(i).delete();
  59. }
  60. }
  61. return success;
  62. }
  63. /**
  64. * Creates a new list model from the actual values in the list
  65. * @return
  66. */
  67. public DefaultListModel getListModel() {
  68. DefaultListModel model = new DefaultListModel();
  69. for (int i = 0; i < this.size(); i++) {
  70. DatabaseObject databaseObject = this.get(i);
  71. model.addElement(databaseObject);
  72. }
  73. return model;
  74. }
  75. /**
  76. * Returns a custom cell renderer which displays an icon and text
  77. * @return
  78. */
  79. public static ListCellRenderer getDBORenderer() {
  80. return new MyCellRenderer();
  81. }
  82. static class MyCellRenderer extends JLabel
  83. implements ListCellRenderer {
  84. public MyCellRenderer() {
  85. // Don't paint behind the component
  86. setOpaque(true);
  87. }
  88. // Set the attributes of the
  89. //class and return a reference
  90. public Component getListCellRendererComponent(JList list,
  91. Object value, // value to display
  92. int index, // cell index
  93. boolean iss, // is selected
  94. boolean chf) // cell has focus?
  95. {
  96. // Set the text and
  97. //background color for rendering
  98. setText(((DatabaseObject) value).toString());
  99. setIcon(((DatabaseObject) value).getIcon());
  100. // Set a border if the
  101. //list item is selected
  102. if (iss) {
  103. setBorder(BorderFactory.createLineBorder(
  104. Color.blue, 1));
  105. } else {
  106. setBorder(BorderFactory.createLineBorder(
  107. list.getBackground(), 1));
  108. }
  109. return this;
  110. }
  111. }
  112. @Override
  113. public DatabaseObject[] toArray() {
  114. return super.toArray(new DatabaseObject[0]);
  115. }
  116. }