001: package tide.editor.bookmarks;
002:
003: import tide.sources.FileItem;
004: import snow.utils.DateUtils;
005: import tide.editor.linemessages.LineMessagesManager;
006: import tide.editor.MainEditorFrame;
007: import tide.project.*;
008: import snow.sortabletable.*;
009: import snow.utils.gui.*;
010:
011: import javax.swing.*;
012: import java.awt.BorderLayout;
013: import java.awt.event.*;
014: import javax.swing.border.*;
015: import java.util.*;
016: import java.io.*;
017: import java.util.concurrent.*;
018:
019: /** Not modal... but not updated ...
020: * TODO: show/remove bookmarks that are no more valid
021: */
022: public class BookmarksManagementDialog extends JDialog {
023: private final BookmarksTM tm = new BookmarksTM();
024: private final SortableTableModel stm = new SortableTableModel(tm,
025: 4, false); // so: first seen is the newest
026: private final JTable table = new JTable(stm);
027:
028: public BookmarksManagementDialog(String filterForBookmarkManagement) {
029: super (MainEditorFrame.instance, "Bookmarks management", false);
030: this .setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
031:
032: add(new JScrollPane(table), BorderLayout.CENTER);
033: stm.installGUI(table);
034:
035: table.addMouseListener(new MouseAdapter() {
036: @Override
037: public void mousePressed(MouseEvent me) {
038: if (me.isPopupTrigger())
039: showPopup(me);
040: else if (me.getClickCount() == 2
041: && table.getSelectedRowCount() == 1) {
042: int ind = stm.getIndexInUnsortedFromTablePos(table
043: .getSelectedRow());
044: SourceBookmark fbm = tm.getBookmarkAt(ind);
045: final FileItem sff = MainEditorFrame.instance
046: .getFileItem(fbm.getJavaName(), "");
047: sff
048: .setCaretPositionToRemember(fbm
049: .getLinePosition(), fbm
050: .getColumnPosition());
051: MainEditorFrame.instance
052: .setSourceOrItemToEditOrView(sff, true);
053: }
054: }
055:
056: @Override
057: public void mouseReleased(MouseEvent me) {
058: if (me.isPopupTrigger())
059: showPopup(me);
060: }
061:
062: private void showPopup(MouseEvent me) {
063: JPopupMenu popup = new JPopupMenu();
064: JMenuItem remove = new JMenuItem("Remove",
065: Icons.sharedCross);
066: popup.add(remove);
067: remove.addActionListener(new ActionListener() {
068: public void actionPerformed(ActionEvent ae) {
069: Vector<Integer> rem = new Vector<Integer>();
070: for (int sel : table.getSelectedRows()) {
071: int ind = stm
072: .getIndexInUnsortedFromTablePos(sel);
073: if (ind >= 0) {
074: rem.add(ind);
075: }
076: }
077: tm.remove(rem);
078: }
079: });
080: /*
081: JMenuItem up = new JMenuItem("Move up");
082: //popup.add(up);
083: // TODO:
084: up.addActionListener(new ActionListener()
085: {
086: public void actionPerformed(ActionEvent ae)
087: {
088: }
089: });
090: */
091:
092: popup.show(table, me.getX(), me.getY());
093: }
094:
095: });
096:
097: AdvancedSearchPanel asp = new AdvancedSearchPanel("", null,
098: stm, false);
099: add(asp, BorderLayout.NORTH);
100: asp.setSearchText(filterForBookmarkManagement, "");
101:
102: // Todo: move up & down buttons
103:
104: add(new CloseControlPanel(this , false, true, "close"),
105: BorderLayout.SOUTH);
106:
107: ((JComponent) getContentPane()).registerKeyboardAction(
108: new ActionListener() {
109: public void actionPerformed(ActionEvent ae) {
110: setVisible(false);
111: return;
112: }
113: }, "Escape", KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,
114: 0, true),
115: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
116:
117: pack();
118: this .setLocationRelativeTo(MainEditorFrame.instance);
119: setVisible(true);
120: } // Constructor
121:
122: class BookmarksTM extends FineGrainTableModel {
123: // Directly the project reference !! not cached !! be careful
124: final List<SourceBookmark> bookmarks;
125:
126: public BookmarksTM() {
127: bookmarks = MainEditorFrame.instance.getActualProject()
128: .getAllBookmarks_REF();
129: }
130:
131: @Override
132: public String getColumnName(int col) {
133: if (col == 0)
134: return "Bookmark";
135: if (col == 1)
136: return "Remark";
137: if (col == 2)
138: return "Line";
139: if (col == 3)
140: return "Column";
141: if (col == 4)
142: return "Age";
143: return "?";
144: }
145:
146: public int getColumnCount() {
147: return 5;
148: } // do not show the columns
149:
150: public int getRowCount() {
151: return bookmarks.size();
152: }
153:
154: public SourceBookmark getBookmarkAt(int row) {
155: return bookmarks.get(row);
156: }
157:
158: public Object getValueAt(int row, int col) {
159: // must be protected because the dialog is not modal and bookmarks may be removed during show.
160: if (row < 0 || row >= bookmarks.size())
161: return "removed";
162: SourceBookmark bm = bookmarks.get(row);
163: if (col == 0)
164: return bm.getJavaName();
165: if (col == 1)
166: return bm.getRemark();
167: if (col == 2)
168: return bm.getLinePosition();
169: if (col == 3)
170: return bm.getColumnPosition();
171: if (col == 4)
172: return DateUtils.formatTimeDifference(System
173: .currentTimeMillis()
174: - bm.getCreated());
175: return "?";
176:
177: }
178:
179: @Override
180: public int compareForColumnSort(int pos1, int pos2, int col) {
181: if (pos1 < 0 || pos1 >= bookmarks.size())
182: return 0;
183: if (pos2 < 0 || pos2 >= bookmarks.size())
184: return 0;
185:
186: if (col == 4) {
187:
188: SourceBookmark m1 = bookmarks.get(pos1);
189: SourceBookmark m2 = bookmarks.get(pos2);
190: return Long.valueOf(m1.getCreated()).compareTo(
191: m2.getCreated());
192: } else {
193: return super .compareForColumnSort(pos1, pos2, col);
194: }
195: }
196:
197: @Override
198: public int getPreferredColumnWidth(int column) {
199: if (column == 0)
200: return 10;
201: if (column == 1)
202: return 10;
203: return 4;
204: }
205:
206: @Override
207: public void setValueAt(Object o, int row, int col) {
208: if (row < 0 || row >= bookmarks.size())
209: return; // just ignore.
210:
211: SourceBookmark bm = bookmarks.get(row);
212: if (col == 1)
213: bm.setRemark("" + o);
214:
215: if (col == 2) {
216: try {
217: bm.setLinePosition(Integer.parseInt("" + o));
218: } catch (Exception e) {
219: e.printStackTrace();
220: }
221: }
222:
223: LineMessagesManager.getInstance().refreshView();
224: }
225:
226: @Override
227: public boolean isCellEditable(int row, int col) {
228: return col == 1 || col == 2;
229: }
230:
231: public void remove(List<Integer> ind) {
232: this .fireTableModelWillChange();
233: Collections.sort(ind); // increasing {0,1,2,...}
234: for (int i = ind.size() - 1; i >= 0; i--) // start with the greatest index !!
235: {
236: bookmarks.remove((int) ind.get(i)); // #### (int) is important here !!!
237: LineMessagesManager.getInstance().refreshView();
238: }
239: this.fireTableDataChanged();
240: this.fireTableModelHasChanged();
241: }
242: }
243:
244: }
|