PageRenderTime 49ms CodeModel.GetById 38ms app.highlight 8ms RepoModel.GetById 0ms app.codeStats 0ms

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

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