001: /*
002: * RegisterViewer.java - Dockable view of register contents
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2004, 2005 Nicholas O'Leary
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.gui;
024:
025: //{{{ Imports
026: import java.awt.*;
027: import java.awt.event.*;
028: import javax.swing.*;
029: import javax.swing.border.EmptyBorder;
030: import javax.swing.event.*;
031: import org.gjt.sp.jedit.*;
032: import org.gjt.sp.jedit.msg.RegisterChanged;
033: import org.gjt.sp.jedit.msg.PropertiesChanged;
034:
035: //}}}
036:
037: public class RegisterViewer extends JPanel implements EBComponent,
038: ActionListener, DockableWindow {
039: //{{{ RegisterViewer constructor
040: public RegisterViewer(View view, String position) {
041: super (new BorderLayout());
042: this .view = view;
043: Box toolBar = new Box(BoxLayout.X_AXIS);
044: JLabel label = new JLabel(jEdit
045: .getProperty("view-registers.title"));
046: label.setBorder(new EmptyBorder(0, 0, 3, 0));
047: toolBar.add(label);
048:
049: toolBar.add(Box.createGlue());
050:
051: RolloverButton pasteRegister = new RolloverButton(GUIUtilities
052: .loadIcon("Paste.png"));
053: pasteRegister.setToolTipText(GUIUtilities
054: .prettifyMenuLabel(jEdit
055: .getProperty("paste-string-register.label")));
056: pasteRegister.addActionListener(this );
057: pasteRegister.setActionCommand("paste-string-register");
058: toolBar.add(pasteRegister);
059:
060: RolloverButton clearRegister = new RolloverButton(GUIUtilities
061: .loadIcon("Clear.png"));
062: clearRegister.setToolTipText(GUIUtilities
063: .prettifyMenuLabel(jEdit
064: .getProperty("clear-string-register.label")));
065: clearRegister.addActionListener(this );
066: clearRegister.setActionCommand("clear-string-register");
067: toolBar.add(clearRegister);
068:
069: add(BorderLayout.NORTH, toolBar);
070:
071: DefaultListModel registerModel = new DefaultListModel();
072: registerList = new JList(registerModel);
073: registerList
074: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
075: registerList.setCellRenderer(new Renderer());
076: registerList.addListSelectionListener(new ListHandler());
077: registerList.addMouseListener(new MouseHandler());
078:
079: contentTextArea = new JTextArea(10, 20);
080: contentTextArea.setEditable(true);
081: documentHandler = new DocumentHandler();
082: //contentTextArea.getDocument().addDocumentListener(documentHandler);
083: contentTextArea.addFocusListener(new FocusHandler());
084:
085: int orientation = JSplitPane.HORIZONTAL_SPLIT;
086: if (position.equals(DockableWindowManager.LEFT)
087: || position.equals(DockableWindowManager.RIGHT))
088: orientation = JSplitPane.VERTICAL_SPLIT;
089:
090: add(
091: BorderLayout.CENTER,
092: splitPane = new JSplitPane(
093: orientation,
094: jEdit
095: .getBooleanProperty("appearance.continuousLayout"),
096: new JScrollPane(registerList), new JScrollPane(
097: contentTextArea)));
098:
099: refreshList();
100: } //}}}
101:
102: //{{{ actionPerformed() method
103: public void actionPerformed(ActionEvent evt) {
104: String cmd = evt.getActionCommand();
105: if (cmd.equals("paste-string-register"))
106: insertRegister();
107: else if (cmd.equals("clear-string-register"))
108: clearSelectedIndex();
109: } //}}}
110:
111: //{{{ handleMessage
112: public void handleMessage(EBMessage msg) {
113: if (msg instanceof RegisterChanged) {
114: if (((RegisterChanged) msg).getRegisterName() != '%')
115: refreshList();
116: } else if (msg instanceof PropertiesChanged) {
117: GUIUtilities.initContinuousLayout(splitPane);
118: }
119:
120: }//}}}
121:
122: //{{{ addNotify() method
123: public void addNotify() {
124: super .addNotify();
125: EditBus.addToBus(this );
126: } //}}}
127:
128: //{{{ removeNotify() method
129: public void removeNotify() {
130: super .removeNotify();
131: EditBus.removeFromBus(this );
132: } //}}}
133:
134: //{{{ move() method
135: public void move(String newPosition) {
136: int orientation = JSplitPane.HORIZONTAL_SPLIT;
137: if (newPosition.equals(DockableWindowManager.LEFT)
138: || newPosition.equals(DockableWindowManager.RIGHT))
139: orientation = JSplitPane.VERTICAL_SPLIT;
140: splitPane.setOrientation(orientation);
141: revalidate();
142: } //}}}
143:
144: //{{{ Private members
145:
146: //{{{ clearSelectedIndex() method
147: private void clearSelectedIndex() {
148: Object o = registerList.getSelectedValue();
149: if (o != null && o instanceof Character) {
150: Registers.clearRegister(((Character) o).charValue());
151: refreshList();
152: }
153: } //}}}
154:
155: //{{{ Instance variables
156: private JList registerList;
157: private JTextArea contentTextArea;
158: private DocumentHandler documentHandler;
159: private View view;
160: private boolean editing;
161: private JSplitPane splitPane;
162: private JPopupMenu popup;
163:
164: //}}}
165:
166: //{{{ refreshList
167: private void refreshList() {
168: DefaultListModel registerModel = (DefaultListModel) registerList
169: .getModel();
170: Object o = registerList.getSelectedValue();
171: int selected = -1;
172: if (o != null && o instanceof Character)
173: selected = ((Character) o).charValue();
174:
175: registerModel.removeAllElements();
176: Registers.Register[] registers = Registers.getRegisters();
177:
178: int index = 0;
179: for (int i = 0; i < registers.length; i++) {
180: Registers.Register reg = registers[i];
181: if (reg == null)
182: continue;
183: if (i == '%')
184: continue;
185:
186: String value = reg.toString();
187: if (value == null) // || value.length() == 0)
188: continue;
189: if (i == selected)
190: index = registerModel.size();
191: registerModel.addElement(new Character((char) i));
192: }
193:
194: if (registerModel.getSize() == 0) {
195: registerModel.addElement(jEdit
196: .getProperty("view-registers.none"));
197: registerList.setEnabled(false);
198: } else
199: registerList.setEnabled(true);
200: registerList.setSelectedIndex(index);
201: } //}}}
202:
203: //{{{ updateSelected
204: private void updateSelected() {
205: Object o = registerList.getSelectedValue();
206: if (o == null)
207: return;
208: else if (o instanceof Character) {
209: Registers.Register reg = Registers
210: .getRegister(((Character) o).charValue());
211: if (!editing) {
212: contentTextArea.setText(reg.toString());
213: contentTextArea.setEditable(true);
214: }
215: } else {
216: if (!editing) {
217: contentTextArea.setText("");
218: contentTextArea.setEditable(false);
219: }
220: }
221: if (!editing)
222: contentTextArea.setCaretPosition(0);
223: }//}}}
224:
225: //{{{ insertRegister
226: private void insertRegister() {
227: Object o = registerList.getSelectedValue();
228: if (o == null || !(o instanceof Character))
229: return;
230: Registers.Register reg = Registers.getRegister(((Character) o)
231: .charValue());
232: view.getTextArea().setSelectedText(reg.toString());
233: view.getTextArea().requestFocus();
234: } //}}}
235:
236: //}}}
237:
238: //{{{ Inner classes
239:
240: //{{{ Renderer Class
241: static class Renderer extends DefaultListCellRenderer {
242: public Component getListCellRendererComponent(JList list,
243: Object value, int index, boolean isSelected,
244: boolean cellHasFocus) {
245: super .getListCellRendererComponent(list, value, index,
246: isSelected, cellHasFocus);
247:
248: if (value instanceof Character) {
249: char name = ((Character) value).charValue();
250:
251: String label;
252:
253: if (name == '\n')
254: label = "\n";
255: else if (name == '\t')
256: label = "\t";
257: else if (name == '$')
258: label = jEdit
259: .getProperty("view-registers.clipboard");
260: else if (name == '%')
261: label = jEdit
262: .getProperty("view-registers.selection");
263: else
264: label = String.valueOf(name);
265: String registerValue = Registers.getRegister(name)
266: .toString();
267: if (registerValue.length() > 100)
268: registerValue = registerValue.substring(0, 100)
269: + "...";
270: registerValue = registerValue.replaceAll("\n", " ");
271: registerValue = registerValue.replaceAll("\t", " ");
272: setText(label + " : " + registerValue);
273: }
274:
275: return this ;
276:
277: }
278: } //}}}
279:
280: //{{{ ListHandler Class
281: class ListHandler implements ListSelectionListener {
282: public void valueChanged(ListSelectionEvent evt) {
283: Object value = registerList.getSelectedValue();
284: if (!(value instanceof Character))
285: return;
286:
287: char name = ((Character) value).charValue();
288:
289: Registers.Register reg = Registers.getRegister(name);
290: if (reg == null)
291: return;
292: updateSelected();
293: }
294: } //}}}
295:
296: //{{{ MouseHandler Class
297: class MouseHandler extends MouseAdapter {
298: public void mouseClicked(MouseEvent evt) {
299: int i = registerList.locationToIndex(evt.getPoint());
300: if (i != -1)
301: registerList.setSelectedIndex(i);
302: if (GUIUtilities.isPopupTrigger(evt)) {
303: if (popup == null) {
304: popup = new JPopupMenu();
305: JMenuItem item = GUIUtilities.loadMenuItem("paste");
306: popup.add(item);
307: item = new JMenuItem(jEdit
308: .getProperty("clear-string-register.label"));
309: item.addActionListener(new ActionListener() {
310: public void actionPerformed(ActionEvent e) {
311: clearSelectedIndex();
312: }
313: });
314: popup.add(item);
315: }
316: GUIUtilities.showPopupMenu(popup, registerList, evt
317: .getX(), evt.getY(), false);
318: } else if (evt.getClickCount() % 2 == 0)
319: insertRegister();
320: }
321: } //}}}
322:
323: //{{{ DocumentHandler Class
324: class DocumentHandler implements DocumentListener {
325: public void changedUpdate(DocumentEvent e) {
326: updateRegisterSafely();
327: }
328:
329: public void insertUpdate(DocumentEvent e) {
330: updateRegisterSafely();
331: }
332:
333: public void removeUpdate(DocumentEvent e) {
334: updateRegisterSafely();
335: }
336:
337: private void updateRegisterSafely() {
338: try {
339: editing = true;
340: updateRegister();
341: } finally {
342: editing = false;
343: }
344: }
345:
346: private void updateRegister() {
347: Object value = registerList.getSelectedValue();
348: if (!(value instanceof Character))
349: return;
350: char name = ((Character) value).charValue();
351: Registers.setRegister(name, contentTextArea.getText());
352: }
353: } //}}}
354:
355: //{{{ FocusHandler Class
356: class FocusHandler implements FocusListener {
357: public void focusGained(FocusEvent e) {
358: contentTextArea.getDocument().addDocumentListener(
359: documentHandler);
360: }
361:
362: public void focusLost(FocusEvent e) {
363: contentTextArea.getDocument().removeDocumentListener(
364: documentHandler);
365: }
366: }//}}}
367:
368: //}}}
369: }
|