PageRenderTime 165ms CodeModel.GetById 152ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/gui/PasteFromListDialog.java

#
Java | 247 lines | 171 code | 34 blank | 42 comment | 26 complexity | e26e9f60c3f7fee4e7f4333523d9948d MD5 | raw file
  1/*
  2 * PasteFromListDialog.java - Paste previous/paste deleted dialog
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2003 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 org.gjt.sp.jedit.*;
 32//}}}
 33
 34public class PasteFromListDialog extends EnhancedDialog
 35{
 36	//{{{ PasteFromListDialog constructor
 37	public PasteFromListDialog(String name, View view, ListModel model)
 38	{
 39		super(view,jEdit.getProperty(name + ".title"),true);
 40		this.view = view;
 41
 42		JPanel content = new JPanel(new BorderLayout());
 43		content.setBorder(new EmptyBorder(12,12,12,12));
 44		setContentPane(content);
 45		JPanel center = new JPanel(new GridLayout(2,1,2,12));
 46
 47		clips = new JList(model);
 48		clips.setCellRenderer(new Renderer());
 49		clips.setVisibleRowCount(12);
 50
 51		clips.addMouseListener(new MouseHandler());
 52		clips.addListSelectionListener(new ListHandler());
 53
 54		insert = new JButton(jEdit.getProperty("common.insert"));
 55		cancel = new JButton(jEdit.getProperty("common.cancel"));
 56
 57		JLabel label = new JLabel(jEdit.getProperty(name + ".caption"));
 58		label.setBorder(new EmptyBorder(0,0,6,0));
 59		content.add(BorderLayout.NORTH,label);
 60
 61		JScrollPane scroller = new JScrollPane(clips);
 62		scroller.setPreferredSize(new Dimension(500,150));
 63		center.add(scroller);
 64
 65		clipText = new JTextArea();
 66		clipText.setEditable(false);
 67		scroller = new JScrollPane(clipText);
 68		scroller.setPreferredSize(new Dimension(500,150));
 69		center.add(scroller);
 70
 71		content.add(center, BorderLayout.CENTER);
 72
 73		JPanel panel = new JPanel();
 74		panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
 75		panel.setBorder(new EmptyBorder(12,0,0,0));
 76		panel.add(Box.createGlue());
 77		panel.add(insert);
 78		panel.add(Box.createHorizontalStrut(6));
 79		panel.add(cancel);
 80		panel.add(Box.createGlue());
 81		content.add(panel, BorderLayout.SOUTH);
 82
 83		if(model.getSize() >= 1)
 84			clips.setSelectedIndex(0);
 85		updateButtons();
 86
 87		getRootPane().setDefaultButton(insert);
 88		insert.addActionListener(new ActionHandler());
 89		cancel.addActionListener(new ActionHandler());
 90
 91		GUIUtilities.requestFocus(this,clips);
 92
 93		pack();
 94		setLocationRelativeTo(view);
 95		show();
 96	} //}}}
 97
 98	//{{{ ok() method
 99	public void ok()
100	{
101		Object[] selected = clips.getSelectedValues();
102		if(selected == null || selected.length == 0)
103		{
104			getToolkit().beep();
105			return;
106		}
107
108		view.getTextArea().setSelectedText(getSelectedClipText());
109
110		dispose();
111	} //}}}
112
113	//{{{ cancel() method
114	public void cancel()
115	{
116		dispose();
117	} //}}}
118
119	//{{{ Private members
120
121	//{{{ Instance variables
122	private View view;
123	private JList clips;
124	private JTextArea clipText;
125	private JButton insert;
126	private JButton cancel;
127	//}}}
128
129	//{{{ getSelectedClipText()
130	private String getSelectedClipText()
131	{
132		Object[] selected = clips.getSelectedValues();
133		StringBuffer clip = new StringBuffer();
134		for(int i = 0; i < selected.length; i++)
135		{
136			if(i != 0)
137				clip.append('\n');
138			clip.append(selected[i]);
139		}
140		return clip.toString();
141	}
142	//}}}
143
144	//{{{ updateButtons() method
145	private void updateButtons()
146	{
147		int selected = clips.getSelectedIndex();
148		insert.setEnabled(selected != -1);
149	} //}}}
150
151	//{{{ showClipText() method
152	private void showClipText()
153	{
154		Object[] selected = clips.getSelectedValues();
155		if(selected == null || selected.length == 0)
156			clipText.setText("");
157		else
158			clipText.setText(getSelectedClipText());
159		clipText.setCaretPosition(0);
160	}
161	//}}}
162
163	//}}}
164
165	//{{{ Renderer class
166	class Renderer extends DefaultListCellRenderer
167	{
168		String shorten(String item)
169		{
170			StringBuffer buf = new StringBuffer();
171			// workaround for Swing rendering labels starting
172			// with <html> using the HTML engine
173			if(item.toLowerCase().startsWith("<html>"))
174				buf.append(' ');
175			boolean ws = true;
176			for(int i = 0; i < item.length(); i++)
177			{
178				char ch = item.charAt(i);
179				if(Character.isWhitespace(ch))
180				{
181					if(ws)
182						/* do nothing */;
183					else
184					{
185						buf.append(' ');
186						ws = true;
187					}
188				}
189				else
190				{
191					ws = false;
192					buf.append(ch);
193				}
194			}
195
196			if(buf.length() == 0)
197				return jEdit.getProperty("paste-from-list.whitespace");
198			return buf.toString();
199		}
200
201		public Component getListCellRendererComponent(
202			JList list, Object value, int index,
203			boolean isSelected, boolean cellHasFocus)
204		{
205			super.getListCellRendererComponent(list,value,index,
206				isSelected,cellHasFocus);
207
208			setText(shorten((String)value));
209
210			return this;
211		}
212	} //}}}
213
214	//{{{ ActionHandler class
215	class ActionHandler implements ActionListener
216	{
217		public void actionPerformed(ActionEvent evt)
218		{
219			Object source = evt.getSource();
220			if(source == insert)
221				ok();
222			else if(source == cancel)
223				cancel();
224		}
225	} //}}}
226
227	//{{{ ListHandler class
228	class ListHandler implements ListSelectionListener
229	{
230		//{{{ valueChanged() method
231		public void valueChanged(ListSelectionEvent evt)
232		{
233			showClipText();
234			updateButtons();
235		} //}}}
236	} //}}}
237
238	//{{{ MouseHandler class
239	class MouseHandler extends MouseAdapter
240	{
241		public void mouseClicked(MouseEvent evt)
242		{
243			if(evt.getClickCount() == 2)
244				ok();
245		}
246	} //}}}
247}