001: /*
002: * RecentFilesDialog.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: RecentFilesDialog.java,v 1.1.1.1 2002/09/24 16:09:14 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.Dimension;
025: import java.awt.Point;
026: import java.awt.event.InputEvent;
027: import java.awt.event.MouseEvent;
028: import java.awt.event.MouseListener;
029: import javax.swing.JScrollPane;
030: import javax.swing.JTable;
031: import javax.swing.ListSelectionModel;
032: import javax.swing.table.JTableHeader;
033: import javax.swing.table.TableColumn;
034: import javax.swing.table.TableColumnModel;
035:
036: public final class RecentFilesDialog extends AbstractDialog implements
037: MouseListener {
038: private final JTable table;
039: private final RecentFilesTableModel model;
040: private final Editor editor;
041:
042: public RecentFilesDialog(Editor editor) {
043: super (editor, "Recent Files", true);
044: this .editor = editor;
045: model = new RecentFilesTableModel();
046: table = new JTable(model);
047: table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
048: table.addRowSelectionInterval(0, 0);
049: int width = editor.getSize().width - 50;
050: int height = table.getPreferredScrollableViewportSize().height;
051: table.setPreferredScrollableViewportSize(new Dimension(width,
052: height));
053: JScrollPane scrollPane = new JScrollPane(table);
054: mainPanel.add(scrollPane);
055: addVerticalStrut();
056: addOKCancel();
057: table.addKeyListener(this );
058: table.addMouseListener(this );
059: JTableHeader th = table.getTableHeader();
060: th.addMouseListener(this );
061: TableColumnModel columnModel = th.getColumnModel();
062: int count = columnModel.getColumnCount();
063: for (int i = 0; i < count; i++) {
064: String key = getColumnWidthKey(i);
065: int columnWidth = Editor.getSessionProperties()
066: .getIntegerProperty(key, 0);
067: if (columnWidth == 0)
068: break;
069: TableColumn column = columnModel.getColumn(i);
070: column.setPreferredWidth(columnWidth);
071: }
072: pack();
073: table.requestFocus();
074: }
075:
076: protected void ok() {
077: openSelectedFile();
078: }
079:
080: private void openSelectedFile() {
081: int row = table.getSelectedRow();
082: if (row >= 0)
083: openFileAtRow(row);
084: }
085:
086: private void openFileAtPoint(Point point) {
087: int row = table.rowAtPoint(point);
088: if (row >= 0)
089: openFileAtRow(row);
090: }
091:
092: private void openFileAtRow(int row) {
093: dispose();
094: editor.repaintNow();
095: RecentFilesEntry entry = model.getEntryAtRow(row);
096: File parent = File.getInstance(entry.location);
097: File file = File.getInstance(parent, entry.name);
098: Buffer buf = editor.getBuffer(file);
099: if (buf == null)
100: editor.status("File not found");
101: else if (buf != editor.getBuffer()) {
102: editor.makeNext(buf);
103: editor.activate(buf);
104: if (buf instanceof RemoteBuffer)
105: ((RemoteBuffer) buf).setInitialDotPos(entry.lineNumber,
106: entry.offs);
107: else {
108: Line line = buf.getLine(entry.lineNumber);
109: if (line != null) {
110: int offs = entry.offs;
111: if (offs > line.length())
112: offs = line.length();
113: editor.moveDotTo(line, offs);
114: } else
115: editor.moveDotTo(buf.getFirstLine(), 0);
116: editor.updateDisplay();
117: }
118: }
119: }
120:
121: public void dispose() {
122: JTableHeader th = table.getTableHeader();
123: TableColumnModel columnModel = th.getColumnModel();
124: int count = columnModel.getColumnCount();
125: for (int i = 0; i < count; i++) {
126: TableColumn column = columnModel.getColumn(i);
127: String key = getColumnWidthKey(i);
128: Editor.getSessionProperties().setIntegerProperty(key,
129: column.getWidth());
130: }
131: super .dispose();
132: }
133:
134: private String getColumnWidthKey(int i) {
135: return "RecentFilesDialog.columnWidth." + i;
136: }
137:
138: public void mouseClicked(MouseEvent e) {
139: if (e.getClickCount() == 2)
140: openSelectedFile();
141: else if (e.getModifiers() == InputEvent.BUTTON2_MASK)
142: openFileAtPoint(e.getPoint());
143: else if (e.getComponent() == table.getTableHeader()) {
144: TableColumnModel columnModel = table.getColumnModel();
145: int viewColumn = columnModel.getColumnIndexAtX(e.getX());
146: int column = table.convertColumnIndexToModel(viewColumn);
147: if (e.getClickCount() == 1 && column >= 0) {
148: int row = table.getSelectedRow();
149: RecentFilesEntry entry = null;
150: if (row >= 0)
151: entry = model.getEntryAtRow(row);
152: model.sortByColumn(column);
153: if (entry != null) {
154: row = model.getRowForEntry(entry);
155: if (row >= 0)
156: table.addRowSelectionInterval(row, row);
157: }
158: }
159: }
160: }
161:
162: public void mousePressed(MouseEvent e) {
163: }
164:
165: public void mouseReleased(MouseEvent e) {
166: }
167:
168: public void mouseEntered(MouseEvent e) {
169: }
170:
171: public void mouseExited(MouseEvent e) {
172: }
173:
174: public static void recentFiles() {
175: final Editor editor = Editor.currentEditor();
176: RecentFilesDialog d = new RecentFilesDialog(editor);
177: editor.centerDialog(d);
178: d.show();
179: }
180: }
|