PageRenderTime 78ms CodeModel.GetById 53ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/gui/RegisterViewer.java

#
Java | 320 lines | 231 code | 39 blank | 50 comment | 47 complexity | a779f61eafc6b85f6d78e86a18a92cba 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.msg.RegisterChanged;
 33import org.gjt.sp.util.Log;
 34//}}}
 35
 36public class RegisterViewer extends JPanel implements EBComponent
 37{
 38	//{{{ RegisterViewer constructor
 39	public RegisterViewer(View view, String position)
 40	{
 41		super(new BorderLayout());
 42		this.view = view;
 43		JLabel label = new JLabel(
 44			jEdit.getProperty("view-registers.title"));
 45		label.setBorder(new EmptyBorder(0,0,3,0));
 46		add(BorderLayout.NORTH,label);
 47
 48		DefaultListModel registerModel = new DefaultListModel();
 49		registerList = new JList(registerModel);
 50		registerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 51		registerList.setCellRenderer(new Renderer());
 52		registerList.addListSelectionListener(new ListHandler());
 53		registerList.addMouseListener(new MouseHandler());
 54
 55		contentTextArea = new JTextArea(10,20);
 56		contentTextArea.setEditable(true);
 57		documentHandler = new DocumentHandler();
 58		//contentTextArea.getDocument().addDocumentListener(documentHandler);
 59		contentTextArea.addFocusListener(new FocusHandler());
 60
 61		int orientation = JSplitPane.HORIZONTAL_SPLIT;
 62		if (position.equals(DockableWindowManager.LEFT) ||
 63			position.equals(DockableWindowManager.RIGHT))
 64			orientation = JSplitPane.VERTICAL_SPLIT;
 65
 66		add(BorderLayout.CENTER,new JSplitPane(orientation,
 67			true,
 68			new JScrollPane(registerList),
 69			new JScrollPane(contentTextArea)
 70			));
 71
 72		refreshList();
 73	} //}}}
 74
 75	//{{{ handleMessage
 76	public void handleMessage(EBMessage msg)
 77	{
 78		if (msg instanceof RegisterChanged)
 79		{
 80			if (((RegisterChanged)msg).getRegisterName() != '%')
 81				refreshList();
 82		}
 83	}//}}}
 84
 85	//{{{ addNotify() method
 86	public void addNotify()
 87	{
 88		super.addNotify();
 89		EditBus.addToBus(this);
 90	} //}}}
 91
 92	//{{{ removeNotify() method
 93	public void removeNotify()
 94	{
 95		super.removeNotify();
 96		EditBus.removeFromBus(this);
 97	} //}}}
 98
 99	//{{{ Private members
100
101	//{{{ Instance variables
102	private JList registerList;
103	private JTextArea contentTextArea;
104	private DocumentHandler documentHandler;
105	private View view;
106	private boolean editing;
107	//}}}
108
109	//{{{ refreshList
110	private void refreshList()
111	{
112		DefaultListModel registerModel = (DefaultListModel)registerList.getModel();
113		Object o = registerList.getSelectedValue();
114		int index = 0;
115		int selected = -1;
116		if (o != null && o instanceof Character)
117			selected = ((Character)o).charValue();
118
119		registerModel.removeAllElements();
120		Registers.Register[] registers = Registers.getRegisters();
121
122		for(int i = 0; i < registers.length; i++)
123		{
124			Registers.Register reg = registers[i];
125			if(reg == null)
126				continue;
127			if (i == '%')
128				continue;
129
130			String value = reg.toString();
131			if(value == null) // || value.length() == 0)
132				continue;
133			if (i == selected)
134				index = registerModel.size();
135			registerModel.addElement(new Character((char)i));
136		}
137
138		if(registerModel.getSize() == 0)
139		{
140			registerModel.addElement(jEdit.getProperty("view-registers.none"));
141			registerList.setEnabled(false);
142		}
143		else
144			registerList.setEnabled(true);
145		registerList.setSelectedIndex(index);
146	} //}}}
147
148	//{{{ updateSelected
149	private void updateSelected()
150	{
151		Object o = registerList.getSelectedValue();
152		if (o == null)
153			return;
154		else if (o instanceof Character)
155		{
156			Registers.Register reg = Registers.getRegister(((Character)o).charValue());
157			if (!editing)
158			{
159				contentTextArea.setText(reg.toString());
160				contentTextArea.setEditable(true);
161			}
162		}
163		else
164		{
165			if (!editing)
166			{
167				contentTextArea.setText("");
168				contentTextArea.setEditable(false);
169			}
170		}
171		if (!editing)
172			contentTextArea.setCaretPosition(0);
173	}//}}}
174
175	//{{{ insertRegister
176	private void insertRegister()
177	{
178		Object o = registerList.getSelectedValue();
179		if (o == null || !(o instanceof Character))
180			return;
181		Registers.Register reg = Registers.getRegister(((Character)o).charValue());
182		view.getTextArea().setSelectedText(reg.toString());
183		view.getTextArea().requestFocus();
184	} //}}}
185
186	//}}}
187
188	//{{{ Inner classes
189
190	//{{{ Renderer Class
191	class Renderer extends DefaultListCellRenderer
192	{
193		public Component getListCellRendererComponent(
194			JList list, Object value, int index,
195			boolean isSelected, boolean cellHasFocus)
196		{
197			super.getListCellRendererComponent(list,value,
198			index,isSelected,cellHasFocus);
199
200			if(value instanceof Character)
201			{
202				char name = ((Character)value).charValue();
203
204				String label;
205
206				if(name == '\n')
207					label = "\n";
208				else if(name == '\t')
209					label = "\t";
210				else if(name == '$')
211					label = jEdit.getProperty("view-registers.clipboard");
212				else if(name == '%')
213					label = jEdit.getProperty("view-registers.selection");
214				else
215					label = String.valueOf(name);
216				String registerValue = Registers.getRegister(name).toString();
217				if (registerValue.length() > 100)
218					registerValue = registerValue.substring(0,100)+"...";
219				registerValue = registerValue.replaceAll("\n"," ");
220				registerValue = registerValue.replaceAll("\t"," ");
221				setText(label+" : "+registerValue);
222			}
223
224			return this;
225		}
226	} //}}}
227
228	//{{{ ListHandler Class
229	class ListHandler implements ListSelectionListener
230	{
231		public void valueChanged(ListSelectionEvent evt)
232		{
233			Object value = registerList.getSelectedValue();
234			if(!(value instanceof Character))
235				return;
236
237			char name = ((Character)value).charValue();
238
239			Registers.Register reg = Registers.getRegister(name);
240			if(reg == null)
241				return;
242			updateSelected();
243		}
244	} //}}}
245
246	//{{{ MouseHandler Class
247	class MouseHandler extends MouseAdapter
248	{
249		/*public void mousePressed(MouseEvent evt)
250		{
251			if(evt.isConsumed())
252				return;
253			int index = registerList.locationToIndex(
254				evt.getPoint());
255			registerList.setSelectedIndex(index);
256		} */
257
258		public void mouseClicked(MouseEvent evt)
259		{
260			if (evt.getClickCount() % 2 == 0)
261				insertRegister();
262		}
263	} //}}}
264
265	//{{{ DocumentHandler Class
266	class DocumentHandler implements DocumentListener
267	{
268		public void changedUpdate(DocumentEvent e)
269		{
270			updateRegisterSafely();
271		}
272
273		public void insertUpdate(DocumentEvent e)
274		{
275			updateRegisterSafely();
276		}
277
278		public void removeUpdate(DocumentEvent e)
279		{
280			updateRegisterSafely();
281		}
282
283		private void updateRegisterSafely()
284		{
285			try
286			{
287				editing = true;
288				updateRegister();
289			}
290			finally
291			{
292				editing = false;
293			}
294		}
295		
296		private void updateRegister()
297		{
298			Object value = registerList.getSelectedValue();
299			if(!(value instanceof Character))
300				return;
301			char name = ((Character)value).charValue();
302			Registers.setRegister(name,contentTextArea.getText());
303		}
304	} //}}}
305
306	//{{{ FocusHandler Class
307	class FocusHandler implements FocusListener
308	{
309		public void focusGained(FocusEvent e)
310		{
311			contentTextArea.getDocument().addDocumentListener(documentHandler);
312		}
313		public void focusLost(FocusEvent e)
314		{
315			contentTextArea.getDocument().removeDocumentListener(documentHandler);
316		}
317	}//}}}
318
319	//}}}
320}