PageRenderTime 105ms CodeModel.GetById 61ms app.highlight 38ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/RegisterViewer.java

#
Java | 406 lines | 310 code | 49 blank | 47 comment | 57 complexity | 0a04ef36e5cd8df19c04a4bfb5a7642b MD5 | raw file
  1/*
  2 * RegisterViewer.java - Dockable view of register contents
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2004, 2005 Nicholas O'Leary
  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 java.awt.*;
 27import java.awt.event.*;
 28import javax.swing.*;
 29import javax.swing.border.EmptyBorder;
 30import javax.swing.event.*;
 31import org.gjt.sp.jedit.*;
 32import org.gjt.sp.jedit.EditBus.EBHandler;
 33import org.gjt.sp.jedit.Registers.Register;
 34import org.gjt.sp.jedit.msg.RegisterChanged;
 35import org.gjt.sp.jedit.msg.PropertiesChanged;
 36//}}}
 37
 38public class RegisterViewer extends JPanel implements ActionListener,
 39	DockableWindow
 40{
 41	//{{{ RegisterViewer constructor
 42	public RegisterViewer(View view, String position)
 43	{
 44		super(new BorderLayout());
 45		this.view = view;
 46		Box toolBar = new Box(BoxLayout.X_AXIS);
 47		JLabel label = new JLabel(
 48			jEdit.getProperty("view-registers.title"));
 49		label.setBorder(new EmptyBorder(0,0,3,0));
 50		toolBar.add(label);
 51		
 52		toolBar.add(Box.createGlue());
 53
 54		RolloverButton pasteRegister = new RolloverButton(
 55			GUIUtilities.loadIcon("Paste.png"));
 56		pasteRegister.setToolTipText(GUIUtilities.prettifyMenuLabel(
 57			jEdit.getProperty("paste-string-register.label")));
 58		pasteRegister.addActionListener(this);
 59		pasteRegister.setActionCommand("paste-string-register");
 60		toolBar.add(pasteRegister);
 61
 62		RolloverButton clearRegister = new RolloverButton(
 63			GUIUtilities.loadIcon("Clear.png"));
 64		clearRegister.setToolTipText(GUIUtilities.prettifyMenuLabel(
 65			jEdit.getProperty("clear-string-register.label")));
 66		clearRegister.addActionListener(this);
 67		clearRegister.setActionCommand("clear-string-register");
 68		toolBar.add(clearRegister);
 69
 70		
 71		add(BorderLayout.NORTH,toolBar);
 72
 73		DefaultListModel registerModel = new DefaultListModel();
 74		registerList = new JList(registerModel);
 75		registerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 76		registerList.setCellRenderer(new Renderer());
 77		registerList.addListSelectionListener(new ListHandler());
 78		registerList.addMouseListener(new MouseHandler());
 79
 80		contentTextArea = new JTextArea(10,20);
 81		contentTextArea.setEditable(true);
 82		documentHandler = new DocumentHandler();
 83		//contentTextArea.getDocument().addDocumentListener(documentHandler);
 84		contentTextArea.addFocusListener(new FocusHandler());
 85
 86		int orientation = JSplitPane.HORIZONTAL_SPLIT;
 87		if (position.equals(DockableWindowManager.LEFT) ||
 88			position.equals(DockableWindowManager.RIGHT))
 89			orientation = JSplitPane.VERTICAL_SPLIT;
 90
 91		add(BorderLayout.CENTER,splitPane = new JSplitPane(orientation,
 92			jEdit.getBooleanProperty("appearance.continuousLayout"),
 93			new JScrollPane(registerList),
 94			new JScrollPane(contentTextArea)));
 95
 96		refreshList();
 97	} //}}}
 98	
 99	//{{{ actionPerformed() method
100	public void actionPerformed(ActionEvent evt)
101	{
102		String cmd = evt.getActionCommand();
103		if (cmd.equals("paste-string-register"))
104			insertRegister();
105		else if (cmd.equals("clear-string-register"))
106			clearSelectedIndex();
107	} //}}}
108
109	//{{{ handleRegisterChanged() method
110	@EBHandler
111	public void handleRegisterChanged(RegisterChanged msg)
112	{
113		if (msg.getRegisterName() != '%')
114			refreshList();
115	} //}}}
116
117	//{{{ handlePropertiesChanged
118	@EBHandler
119	public void handlePropertiesChanged(PropertiesChanged msg)
120	{
121		GUIUtilities.initContinuousLayout(splitPane);
122	} //}}}
123
124	//{{{ addNotify() method
125	@Override
126	public void addNotify()
127	{
128		super.addNotify();
129		EditBus.addToBus(this);
130	} //}}}
131
132	//{{{ removeNotify() method
133	@Override
134	public void removeNotify()
135	{
136		super.removeNotify();
137		EditBus.removeFromBus(this);
138	} //}}}
139
140	//{{{ move() method
141	public void move(String newPosition)
142	{
143		int orientation = JSplitPane.HORIZONTAL_SPLIT;
144		if (newPosition.equals(DockableWindowManager.LEFT) ||
145			newPosition.equals(DockableWindowManager.RIGHT))
146			orientation = JSplitPane.VERTICAL_SPLIT;
147		splitPane.setOrientation(orientation);
148		revalidate();
149	} //}}}
150
151	//{{{ Private members
152
153	//{{{ clearSelectedIndex() method
154	private void clearSelectedIndex()
155	{
156		Object o = registerList.getSelectedValue();
157		if (o != null && o instanceof Character)
158		{
159			Registers.clearRegister(((Character)o).charValue());
160			refreshList();
161		}
162	} //}}}
163
164	//{{{ Instance variables
165	private JList registerList;
166	private JTextArea contentTextArea;
167	private DocumentHandler documentHandler;
168	private View view;
169	private boolean editing;
170	private JSplitPane splitPane;
171	private JPopupMenu popup;
172	//}}}
173
174	//{{{ refreshList
175	private void refreshList()
176	{
177		DefaultListModel registerModel = (DefaultListModel)registerList.getModel();
178		Object o = registerList.getSelectedValue();
179		int selected = -1;
180		if (o != null && o instanceof Character)
181			selected = ((Character)o).charValue();
182
183		registerModel.removeAllElements();
184		Registers.Register[] registers = Registers.getRegisters();
185
186		int index = 0;
187		for(int i = 0; i < registers.length; i++)
188		{
189			Registers.Register reg = registers[i];
190			if(reg == null)
191				continue;
192			if (i == '%')
193				continue;
194
195			String value = reg.toString();
196			if(value == null) // || value.length() == 0)
197				continue;
198			if (i == selected)
199				index = registerModel.size();
200			registerModel.addElement(Character.valueOf((char)i));
201		}
202
203		if(registerModel.getSize() == 0)
204		{
205			registerModel.addElement(jEdit.getProperty("view-registers.none"));
206			registerList.setEnabled(false);
207		}
208		else
209			registerList.setEnabled(true);
210		registerList.setSelectedIndex(index);
211	} //}}}
212
213	//{{{ insertRegister
214	private void insertRegister()
215	{
216		Object o = registerList.getSelectedValue();
217		if (o == null || !(o instanceof Character))
218			return;
219		Registers.Register reg = Registers.getRegister(((Character)o).charValue());
220		view.getTextArea().setSelectedText(reg.toString());
221		view.getTextArea().requestFocus();
222	} //}}}
223
224	//}}}
225
226	//{{{ Inner classes
227
228	//{{{ Renderer Class
229	static class Renderer extends DefaultListCellRenderer
230	{
231		@Override
232		public Component getListCellRendererComponent(
233			JList list, Object value, int index,
234			boolean isSelected, boolean cellHasFocus)
235		{
236			super.getListCellRendererComponent(list,value,
237			index,isSelected,cellHasFocus);
238
239			if(value instanceof Character)
240			{
241				char name = ((Character)value).charValue();
242
243				String label;
244
245				if(name == '\n')
246					label = "\n";
247				else if(name == '\t')
248					label = "\t";
249				else if(name == '$')
250					label = jEdit.getProperty("view-registers.clipboard");
251				else if(name == '%')
252					label = jEdit.getProperty("view-registers.selection");
253				else
254					label = String.valueOf(name);
255				Register register = Registers.getRegister(name);
256				String registerValue;
257				if (register == null)
258				{
259					// The register is not defined anymore, it has been removed before
260					// the painting event
261					registerValue = jEdit.getProperty("view-registers.undefined");
262				}
263				else
264				{
265					registerValue = register.toString();
266					if (registerValue.length() > 100)
267						registerValue = registerValue.substring(0,100)+"...";
268					registerValue = registerValue.replaceAll("\n"," ");
269					registerValue = registerValue.replaceAll("\t"," ");
270				}
271				setText(label + " : " + registerValue);
272			}
273
274			return this;
275
276		}
277	} //}}}
278
279	//{{{ ListHandler Class
280	class ListHandler implements ListSelectionListener
281	{
282		public void valueChanged(ListSelectionEvent evt)
283		{			
284			Object value = registerList.getSelectedValue();
285			if(!(value instanceof Character))
286			{
287				if (!editing)
288				{
289					contentTextArea.setText("");
290					contentTextArea.setEditable(false);
291				}
292				return;
293			}
294
295			char name = ((Character)value).charValue();
296
297			Registers.Register reg = Registers.getRegister(name);
298			if(reg == null)
299			{
300				if (!editing)
301				{
302					contentTextArea.setText("");
303					contentTextArea.setEditable(false);
304				}	
305				return;
306			}
307			
308			
309			if (!editing)
310			{
311				contentTextArea.setText(reg.toString());
312				contentTextArea.setEditable(true);
313				contentTextArea.setCaretPosition(0);
314			}
315		}
316	} //}}}
317
318	//{{{ MouseHandler Class
319	class MouseHandler extends MouseAdapter
320	{
321		@Override
322		public void mouseClicked(MouseEvent evt)
323		{
324			int i = registerList.locationToIndex(evt.getPoint());
325			if (i != -1)
326				registerList.setSelectedIndex(i);
327			if (GUIUtilities.isPopupTrigger(evt))
328			{
329				if (popup == null)
330				{
331					popup = new JPopupMenu();
332					JMenuItem item = GUIUtilities.loadMenuItem("paste");
333					popup.add(item);
334					item = new JMenuItem(jEdit.getProperty("clear-string-register.label"));
335					item.addActionListener(new ActionListener()
336					{
337						public void actionPerformed(ActionEvent e)
338						{
339							clearSelectedIndex();
340						}
341					});
342					popup.add(item);
343				}
344				GUIUtilities.showPopupMenu(popup, registerList, evt.getX(), evt.getY(), false);
345			}
346			else if (evt.getClickCount() % 2 == 0)
347				insertRegister();
348		}
349	} //}}}
350
351	//{{{ DocumentHandler Class
352	class DocumentHandler implements DocumentListener
353	{
354		public void changedUpdate(DocumentEvent e)
355		{
356			updateRegisterSafely();
357		}
358
359		public void insertUpdate(DocumentEvent e)
360		{
361			updateRegisterSafely();
362		}
363
364		public void removeUpdate(DocumentEvent e)
365		{
366			updateRegisterSafely();
367		}
368
369		private void updateRegisterSafely()
370		{
371			try
372			{
373				editing = true;
374				updateRegister();
375			}
376			finally
377			{
378				editing = false;
379			}
380		}
381		
382		private void updateRegister()
383		{
384			Object value = registerList.getSelectedValue();
385			if(!(value instanceof Character))
386				return;
387			char name = ((Character)value).charValue();
388			Registers.setRegister(name,contentTextArea.getText());
389		}
390	} //}}}
391
392	//{{{ FocusHandler Class
393	class FocusHandler implements FocusListener
394	{
395		public void focusGained(FocusEvent e)
396		{
397			contentTextArea.getDocument().addDocumentListener(documentHandler);
398		}
399		public void focusLost(FocusEvent e)
400		{
401			contentTextArea.getDocument().removeDocumentListener(documentHandler);
402		}
403	}//}}}
404
405	//}}}
406}