PageRenderTime 27ms CodeModel.GetById 15ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/gui/PastePrevious.java

#
Java | 202 lines | 133 code | 31 blank | 38 comment | 15 complexity | 2d20ca803ea1b37f4235e53b7915b151 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1/*
  2 * PastePrevious.java - Paste previous dialog
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1998, 1999, 2001 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.gui;
 24
 25//{{{ Imports
 26import javax.swing.*;
 27import javax.swing.border.*;
 28import javax.swing.event.*;
 29import java.awt.*;
 30import java.awt.event.*;
 31import java.util.Vector;
 32import org.gjt.sp.jedit.*;
 33//}}}
 34
 35public class PastePrevious extends EnhancedDialog
 36implements ActionListener, ListSelectionListener, MouseListener
 37{
 38	//{{{ PastePrevious constructor
 39	public PastePrevious(View view)
 40	{
 41		super(view,jEdit.getProperty("pasteprev.title"),true);
 42		this.view = view;
 43
 44		JPanel content = new JPanel(new BorderLayout());
 45		content.setBorder(new EmptyBorder(12,12,12,12));
 46		setContentPane(content);
 47
 48		clipHistory = HistoryModel.getModel("clipboard");
 49
 50		clips = new JList(new AbstractListModel() {
 51			public int getSize()
 52			{
 53				return clipHistory.getSize();
 54			}
 55
 56			public Object getElementAt(int index)
 57			{
 58				StringBuffer buf = new StringBuffer();
 59				String item = clipHistory.getItem(index);
 60				// workaround for Swing rendering labels starting
 61				// with <html> using the HTML engine
 62				if(item.toLowerCase().startsWith("<html>"))
 63					buf.append(' ');
 64				boolean ws = true;
 65				for(int i = 0; i < item.length(); i++)
 66				{
 67					char ch = item.charAt(i);
 68					if(Character.isWhitespace(ch))
 69					{
 70						if(ws)
 71							/* do nothing */;
 72						else
 73						{
 74							buf.append(' ');
 75							ws = true;
 76						}
 77					}
 78					else
 79					{
 80						ws = false;
 81						buf.append(ch);
 82					}
 83				}
 84				return buf.toString();
 85			}
 86		});
 87
 88		clips.setVisibleRowCount(16);
 89
 90		clips.addMouseListener(this);
 91		clips.addListSelectionListener(this);
 92
 93		insert = new JButton(jEdit.getProperty("pasteprev.insert"));
 94		cancel = new JButton(jEdit.getProperty("common.cancel"));
 95
 96		JLabel label = new JLabel(jEdit.getProperty("pasteprev.caption"));
 97		label.setBorder(new EmptyBorder(0,0,6,0));
 98		content.add(BorderLayout.NORTH,label);
 99
100		JScrollPane scroller = new JScrollPane(clips);
101		Dimension dim = scroller.getPreferredSize();
102		scroller.setPreferredSize(new Dimension(640,dim.height));
103
104		content.add(scroller, BorderLayout.CENTER);
105
106		JPanel panel = new JPanel();
107		panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
108		panel.setBorder(new EmptyBorder(12,0,0,0));
109		panel.add(Box.createGlue());
110		panel.add(insert);
111		panel.add(Box.createHorizontalStrut(6));
112		panel.add(cancel);
113		panel.add(Box.createGlue());
114		content.add(panel, BorderLayout.SOUTH);
115
116		if(clipHistory.getSize() >= 1)
117			clips.setSelectedIndex(0);
118		updateButtons();
119
120		getRootPane().setDefaultButton(insert);
121		insert.addActionListener(this);
122		cancel.addActionListener(this);
123
124		GUIUtilities.requestFocus(this,clips);
125
126		pack();
127		setLocationRelativeTo(view);
128		show();
129	} //}}}
130
131	//{{{ ok() method
132	public void ok()
133	{
134		int selected = clips.getSelectedIndex();
135
136		if(selected == -1)
137		{
138			view.getToolkit().beep();
139			return;
140		}
141
142		String clip = clipHistory.getItem(selected);
143		view.getTextArea().setSelectedText(clip);
144
145		dispose();
146	} //}}}
147
148	//{{{ cancel() method
149	public void cancel()
150	{
151		dispose();
152	} //}}}
153
154	//{{{ actionPerformed() method
155	public void actionPerformed(ActionEvent evt)
156	{
157		Object source = evt.getSource();
158		if(source == insert)
159			ok();
160		else if(source == cancel)
161			cancel();
162	} //}}}
163
164	//{{{ mouseClicked() method
165	public void mouseClicked(MouseEvent evt)
166	{
167		if(evt.getClickCount() == 2)
168			ok();
169	} //}}}
170
171	//{{{ Crap
172	public void mouseEntered(MouseEvent evt) {}
173	public void mouseExited(MouseEvent evt) {}
174	public void mousePressed(MouseEvent evt) {}
175	public void mouseReleased(MouseEvent evt) {}
176	//}}}
177
178	//{{{ valueChanged() method
179	public void valueChanged(ListSelectionEvent evt)
180	{
181		updateButtons();
182	} //}}}
183
184	//{{{ Private members
185
186	//{{{ Instance variables
187	private View view;
188	private JList clips;
189	private HistoryModel clipHistory;
190	private JButton insert;
191	private JButton cancel;
192	//}}}
193
194	//{{{ updateButtons() method
195	private void updateButtons()
196	{
197		int selected = clips.getSelectedIndex();
198		insert.setEnabled(selected != -1);
199	} //}}}
200
201	//}}}
202}