KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > BufferSwitcher


1 /*
2  * BufferSwitcher.java - Status bar
3  * Copyright (C) 2000, 2004 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
20 package org.gjt.sp.jedit.gui;
21
22 import javax.swing.event.*;
23 import javax.swing.*;
24 import java.awt.event.*;
25 import java.awt.*;
26 import org.gjt.sp.jedit.*;
27
28 public class BufferSwitcher extends JComboBox
29 {
30     public BufferSwitcher(final EditPane editPane)
31     {
32         this.editPane = editPane;
33
34         //setFont(new Font("Dialog",Font.BOLD,10));
35
setRenderer(new BufferCellRenderer());
36         setMaximumRowCount(jEdit.getIntegerProperty("bufferSwitcher.maxRowCount",10));
37         addActionListener(new ActionHandler());
38         addPopupMenuListener(new PopupMenuListener()
39         {
40             public void popupMenuWillBecomeVisible(
41                 PopupMenuEvent e) {}
42
43             public void popupMenuWillBecomeInvisible(
44                 PopupMenuEvent e)
45             {
46                 editPane.getTextArea().requestFocus();
47             }
48
49             public void popupMenuCanceled(PopupMenuEvent e)
50             {
51                 editPane.getTextArea().requestFocus();
52             }
53         });
54     }
55
56     public void updateBufferList()
57     {
58         // if the buffer count becomes 0, then it is guaranteed to
59
// become 1 very soon, so don't do anything in that case.
60
if(jEdit.getBufferCount() == 0)
61             return;
62
63         updating = true;
64         setMaximumRowCount(jEdit.getIntegerProperty("bufferSwitcher.maxRowCount",10));
65         setModel(new DefaultComboBoxModel(jEdit.getBuffers()));
66         setSelectedItem(editPane.getBuffer());
67         setToolTipText(editPane.getBuffer().getPath());
68         updating = false;
69     }
70
71     // private members
72
private EditPane editPane;
73     private boolean updating;
74
75     class ActionHandler implements ActionListener
76     {
77         public void actionPerformed(ActionEvent evt)
78         {
79             if(!updating)
80             {
81                 Buffer buffer = (Buffer)getSelectedItem();
82                 if(buffer != null)
83                     editPane.setBuffer(buffer);
84             }
85         }
86     }
87
88     class BufferCellRenderer extends DefaultListCellRenderer
89     {
90         public Component getListCellRendererComponent(
91             JList list, Object JavaDoc value, int index,
92             boolean isSelected, boolean cellHasFocus)
93         {
94             super.getListCellRendererComponent(list,value,index,
95                 isSelected,cellHasFocus);
96             Buffer buffer = (Buffer)value;
97             
98             if(buffer == null)
99                 setIcon(null);
100             else {
101                 setIcon(buffer.getIcon());
102                 setToolTipText(buffer.getPath());
103             }
104             return this;
105         }
106     }
107 }
108
Popular Tags