/plugins/InfoViewer/tags/first/infoviewer/Bookmarks.java

#
Java | 166 lines | 113 code | 33 blank | 20 comment | 25 complexity | fbe42ec4781f3d0f8131306798b7e1d5 MD5 | raw file

✨ Summary
  1. /*
  2. * Bookmarks.java - Model for Bookmarks
  3. * Copyright (C) 1999 Slava Pestov
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program 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 this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package infoviewer;
  20. import infoviewer.actions.InfoViewerAction;
  21. import java.awt.event.ActionListener;
  22. import java.awt.event.ActionEvent;
  23. import java.util.Vector;
  24. import javax.swing.table.*;
  25. import org.gjt.sp.jedit.jEdit;
  26. public class Bookmarks extends AbstractTableModel {
  27. private Vector entries = new Vector();
  28. public Bookmarks() {
  29. for (int i=0; ; i++) {
  30. String title = jEdit.getProperty("infoviewer.bookmarks.title."+i);
  31. if (title == null) break;
  32. String url = jEdit.getProperty("infoviewer.bookmarks.url."+i);
  33. add(title, url);
  34. }
  35. }
  36. public void add(String title, String url) {
  37. entries.addElement(new BookmarksEntry(title, url));
  38. fireTableRowsInserted(entries.size()-1, entries.size()-1);
  39. }
  40. public int getSize() {
  41. return entries.size();
  42. }
  43. public String getTitle(int index) {
  44. if (index < 0 || index > entries.size()) return null;
  45. BookmarksEntry e = (BookmarksEntry) entries.elementAt(index);
  46. return e.title;
  47. }
  48. public String getURL(int index) {
  49. if (index < 0 || index > entries.size()) return null;
  50. BookmarksEntry e = (BookmarksEntry) entries.elementAt(index);
  51. return e.url;
  52. }
  53. public BookmarksEntry getEntry(int index) {
  54. if (index < 0 || index > entries.size()) return null;
  55. BookmarksEntry e = (BookmarksEntry) entries.elementAt(index);
  56. return e;
  57. }
  58. public void delete(int row) {
  59. entries.removeElementAt(row);
  60. fireTableRowsDeleted(row, row);
  61. }
  62. public void moveup(int row) {
  63. if (row == 0) return;
  64. BookmarksEntry b = getEntry(row);
  65. entries.removeElementAt(row);
  66. entries.insertElementAt(b, row-1);
  67. fireTableRowsUpdated(row-1, row);
  68. }
  69. public void movedown(int row) {
  70. if (row == entries.size()-1) return;
  71. BookmarksEntry b = getEntry(row);
  72. entries.removeElementAt(row);
  73. entries.insertElementAt(b, row+1);
  74. fireTableRowsUpdated(row, row+1);
  75. }
  76. // begin AbstractTableModel implementation
  77. public int getColumnCount() {
  78. return 2;
  79. }
  80. public int getRowCount() {
  81. return entries.size();
  82. }
  83. public Object getValueAt(int row, int col) {
  84. Object obj = null;
  85. if (row < entries.size()) {
  86. switch (col) {
  87. case 0: obj = getEntry(row).title; break;
  88. case 1: obj = getEntry(row).url; break;
  89. }
  90. }
  91. return obj;
  92. }
  93. public boolean isCellEditable(int row, int col) {
  94. return true;
  95. }
  96. public void setValueAt(Object value, int row, int col) {
  97. BookmarksEntry e = getEntry(row);
  98. switch (col) {
  99. case 0: e.title = value.toString(); break;
  100. case 1: e.url = value.toString(); break;
  101. default: break;
  102. }
  103. fireTableRowsUpdated(row, row);
  104. }
  105. public String getColumnName(int index) {
  106. String ret = "";
  107. switch (index) {
  108. case 0: ret = jEdit.getProperty("infoviewer.bdialog.col0"); break;
  109. case 1: ret = jEdit.getProperty("infoviewer.bdialog.col1"); break;
  110. default: break;
  111. }
  112. return ret;
  113. }
  114. // end AbstractTableModel implementation
  115. public void save() {
  116. int i = 0;
  117. int count = 0;
  118. while (i < entries.size()) {
  119. BookmarksEntry b = getEntry(i);
  120. if (b == null) continue;
  121. if (b.title == null || b.title.length() == 0) {
  122. delete(i);
  123. } else {
  124. jEdit.setProperty("infoviewer.bookmarks.title." + count, b.title);
  125. jEdit.setProperty("infoviewer.bookmarks.url." + count, b.url);
  126. i++;
  127. count++;
  128. }
  129. }
  130. jEdit.unsetProperty("infoviewer.bookmarks.title." + count);
  131. jEdit.unsetProperty("infoviewer.bookmarks.url." + count);
  132. }
  133. }