KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > RecentFilesDialog


1 /*
2  * RecentFilesDialog.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: RecentFilesDialog.java,v 1.1.1.1 2002/09/24 16:09:14 piso Exp $
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 (at your option) 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
22 package org.armedbear.j;
23
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Point JavaDoc;
26 import java.awt.event.InputEvent JavaDoc;
27 import java.awt.event.MouseEvent JavaDoc;
28 import java.awt.event.MouseListener JavaDoc;
29 import javax.swing.JScrollPane JavaDoc;
30 import javax.swing.JTable JavaDoc;
31 import javax.swing.ListSelectionModel JavaDoc;
32 import javax.swing.table.JTableHeader JavaDoc;
33 import javax.swing.table.TableColumn JavaDoc;
34 import javax.swing.table.TableColumnModel JavaDoc;
35
36 public final class RecentFilesDialog extends AbstractDialog implements MouseListener JavaDoc
37 {
38     private final JTable JavaDoc table;
39     private final RecentFilesTableModel model;
40     private final Editor editor;
41
42     public RecentFilesDialog(Editor editor)
43     {
44         super(editor, "Recent Files", true);
45         this.editor = editor;
46         model = new RecentFilesTableModel();
47         table = new JTable JavaDoc(model);
48         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
49         table.addRowSelectionInterval(0, 0);
50         int width = editor.getSize().width - 50;
51         int height = table.getPreferredScrollableViewportSize().height;
52         table.setPreferredScrollableViewportSize(new Dimension JavaDoc(width, height));
53         JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc(table);
54         mainPanel.add(scrollPane);
55         addVerticalStrut();
56         addOKCancel();
57         table.addKeyListener(this);
58         table.addMouseListener(this);
59         JTableHeader JavaDoc th = table.getTableHeader();
60         th.addMouseListener(this);
61         TableColumnModel JavaDoc columnModel = th.getColumnModel();
62         int count = columnModel.getColumnCount();
63         for (int i = 0; i < count; i++) {
64             String JavaDoc key = getColumnWidthKey(i);
65             int columnWidth = Editor.getSessionProperties().getIntegerProperty(key, 0);
66             if (columnWidth == 0)
67                 break;
68             TableColumn JavaDoc column = columnModel.getColumn(i);
69             column.setPreferredWidth(columnWidth);
70         }
71         pack();
72         table.requestFocus();
73     }
74
75     protected void ok()
76     {
77         openSelectedFile();
78     }
79
80     private void openSelectedFile()
81     {
82         int row = table.getSelectedRow();
83         if (row >= 0)
84             openFileAtRow(row);
85     }
86
87     private void openFileAtPoint(Point JavaDoc point)
88     {
89         int row = table.rowAtPoint(point);
90         if (row >= 0)
91             openFileAtRow(row);
92     }
93
94     private void openFileAtRow(int row)
95     {
96         dispose();
97         editor.repaintNow();
98         RecentFilesEntry entry = model.getEntryAtRow(row);
99         File parent = File.getInstance(entry.location);
100         File file = File.getInstance(parent, entry.name);
101         Buffer buf = editor.getBuffer(file);
102         if (buf == null)
103             editor.status("File not found");
104         else if (buf != editor.getBuffer()) {
105             editor.makeNext(buf);
106             editor.activate(buf);
107             if (buf instanceof RemoteBuffer)
108                 ((RemoteBuffer) buf).setInitialDotPos(entry.lineNumber, entry.offs);
109             else {
110                 Line line = buf.getLine(entry.lineNumber);
111                 if (line != null) {
112                     int offs = entry.offs;
113                     if (offs > line.length())
114                         offs = line.length();
115                     editor.moveDotTo(line, offs);
116                 } else
117                     editor.moveDotTo(buf.getFirstLine(), 0);
118                 editor.updateDisplay();
119             }
120         }
121     }
122
123     public void dispose()
124     {
125         JTableHeader JavaDoc th = table.getTableHeader();
126         TableColumnModel JavaDoc columnModel = th.getColumnModel();
127         int count = columnModel.getColumnCount();
128         for (int i = 0; i < count; i++) {
129             TableColumn JavaDoc column = columnModel.getColumn(i);
130             String JavaDoc key = getColumnWidthKey(i);
131             Editor.getSessionProperties().setIntegerProperty(key, column.getWidth());
132         }
133         super.dispose();
134     }
135
136     private String JavaDoc getColumnWidthKey(int i)
137     {
138         return "RecentFilesDialog.columnWidth." + i;
139     }
140
141     public void mouseClicked(MouseEvent JavaDoc e)
142     {
143         if (e.getClickCount() == 2)
144             openSelectedFile();
145         else if (e.getModifiers() == InputEvent.BUTTON2_MASK)
146             openFileAtPoint(e.getPoint());
147         else if (e.getComponent() == table.getTableHeader()) {
148             TableColumnModel JavaDoc columnModel = table.getColumnModel();
149             int viewColumn = columnModel.getColumnIndexAtX(e.getX());
150             int column = table.convertColumnIndexToModel(viewColumn);
151             if (e.getClickCount() == 1 && column >= 0) {
152                 int row = table.getSelectedRow();
153                 RecentFilesEntry entry = null;
154                 if (row >= 0)
155                     entry = model.getEntryAtRow(row);
156                 model.sortByColumn(column);
157                 if (entry != null) {
158                     row = model.getRowForEntry(entry);
159                     if (row >= 0)
160                         table.addRowSelectionInterval(row, row);
161                 }
162             }
163         }
164     }
165
166     public void mousePressed(MouseEvent JavaDoc e) {}
167     public void mouseReleased(MouseEvent JavaDoc e) {}
168     public void mouseEntered(MouseEvent JavaDoc e) {}
169     public void mouseExited(MouseEvent JavaDoc e) {}
170
171     public static void recentFiles()
172     {
173         final Editor editor = Editor.currentEditor();
174         RecentFilesDialog d = new RecentFilesDialog(editor);
175         editor.centerDialog(d);
176         d.show();
177     }
178 }
179
Popular Tags