PageRenderTime 32ms CodeModel.GetById 18ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/gui/ViewRegisters.java

#
Java | 184 lines | 128 code | 35 blank | 21 comment | 26 complexity | 48d69c5ddbe6b1fb592e2585b0c39f05 MD5 | raw file
  1/*
  2 * ViewRegisters.java - View registers dialog
  3 * Copyright (C) 1999, 2000, 2001 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
 20package org.gjt.sp.jedit.gui;
 21
 22import javax.swing.*;
 23import javax.swing.border.*;
 24import javax.swing.event.*;
 25import java.awt.*;
 26import java.awt.event.*;
 27import java.util.Vector;
 28import org.gjt.sp.jedit.*;
 29
 30public class ViewRegisters extends EnhancedDialog
 31{
 32	public ViewRegisters(View view)
 33	{
 34		super(view,jEdit.getProperty("view-registers.title"),true);
 35
 36		JPanel content = new JPanel(new BorderLayout());
 37		content.setBorder(new EmptyBorder(12,12,12,12));
 38		setContentPane(content);
 39
 40		JPanel panel = new JPanel(new BorderLayout());
 41		panel.setBorder(new EmptyBorder(0,12,0,0));
 42
 43		JLabel label = new JLabel(jEdit.getProperty("view-registers.register"));
 44		label.setBorder(new EmptyBorder(0,0,3,0));
 45		panel.add(BorderLayout.NORTH,label);
 46
 47		DefaultListModel registerModel = new DefaultListModel();
 48		registerList = new JList(registerModel);
 49		registerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 50		registerList.setCellRenderer(new Renderer());
 51
 52		Registers.Register[] registers = Registers.getRegisters();
 53
 54		int index = 0;
 55		for(int i = 0; i < registers.length; i++)
 56		{
 57			Registers.Register reg = registers[i];
 58			if(reg == null)
 59				continue;
 60
 61			String value = reg.toString();
 62			if(value == null || value.length() == 0)
 63				continue;
 64
 65			registerModel.addElement(new Character((char)i));
 66		}
 67
 68		if(registerModel.getSize() == 0)
 69			registerModel.addElement(jEdit.getProperty("view-registers.none"));
 70
 71		panel.add(BorderLayout.CENTER,new JScrollPane(registerList));
 72
 73		content.add(BorderLayout.WEST,panel);
 74
 75		panel = new JPanel(new BorderLayout());
 76		panel.setBorder(new EmptyBorder(0,12,0,0));
 77
 78		label = new JLabel(jEdit.getProperty("view-registers.contents"));
 79		label.setBorder(new EmptyBorder(0,0,3,0));
 80		panel.add(BorderLayout.NORTH,label);
 81
 82		contentTextArea = new JTextArea(10,80);
 83		contentTextArea.setEditable(false);
 84		panel.add(BorderLayout.CENTER,new JScrollPane(contentTextArea));
 85		content.add(BorderLayout.CENTER,panel);
 86
 87		panel = new JPanel();
 88		panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
 89		panel.setBorder(new EmptyBorder(12,0,0,0));
 90		close = new JButton(jEdit.getProperty("common.close"));
 91		close.addActionListener(new ActionHandler());
 92		panel.add(Box.createGlue());
 93		panel.add(close);
 94		panel.add(Box.createGlue());
 95		getRootPane().setDefaultButton(close);
 96		content.add(BorderLayout.SOUTH,panel);
 97
 98		registerList.addListSelectionListener(new ListHandler());
 99		registerList.setSelectedIndex(index);
100
101		pack();
102		setLocationRelativeTo(view);
103		show();
104	}
105
106	// EnhancedDialog implementation
107	public void ok()
108	{
109		dispose();
110	}
111
112	public void cancel()
113	{
114		dispose();
115	}
116	// end EnhancedDialog implementation
117
118	// private members
119	private JList registerList;
120	private JTextArea contentTextArea;
121	private JButton close;
122
123	class Renderer extends DefaultListCellRenderer
124	{
125		public Component getListCellRendererComponent(
126			JList list, Object value, int index,
127			boolean isSelected, boolean cellHasFocus)
128		{
129			super.getListCellRendererComponent(list,value,
130				index,isSelected,cellHasFocus);
131
132			if(value instanceof Character)
133			{
134				char name = ((Character)value).charValue();
135
136				String label;
137
138				if(name == '\n')
139					label = "\n";
140				else if(name == '\t')
141					label = "\t";
142				else if(name == '$')
143					label = jEdit.getProperty("view-registers.clipboard");
144				else if(name == '%')
145					label = jEdit.getProperty("view-registers.selection");
146				else
147					label = String.valueOf((char)name);
148
149				setText(label);
150			}
151
152			return this;
153		}
154	}
155
156	class ActionHandler implements ActionListener
157	{
158		public void actionPerformed(ActionEvent evt)
159		{
160			if(evt.getSource() == close)
161				cancel();
162		}
163	}
164
165	class ListHandler implements ListSelectionListener
166	{
167		public void valueChanged(ListSelectionEvent evt)
168		{
169			Object value = registerList.getSelectedValue();
170			if(!(value instanceof Character))
171				return;
172
173			char name = ((Character)value).charValue();
174
175			Registers.Register reg = Registers.getRegister(name);
176
177			if(reg == null)
178				return;
179
180			contentTextArea.setText(reg.toString());
181			contentTextArea.setCaretPosition(0);
182		}
183	}
184}