001: /*
002: * PasteFromListDialog.java - Paste previous/paste deleted dialog
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2003, 2005 Slava Pestov
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 javax.swing.*;
027: import javax.swing.border.*;
028: import javax.swing.event.*;
029: import java.awt.*;
030: import java.awt.event.*;
031: import org.gjt.sp.jedit.*;
032:
033: //}}}
034:
035: public class PasteFromListDialog extends EnhancedDialog {
036: //{{{ PasteFromListDialog constructor
037: public PasteFromListDialog(String name, View view,
038: MutableListModel model) {
039: super (view, jEdit.getProperty(name + ".title"), true);
040: this .view = view;
041: this .listModel = model;
042:
043: JPanel content = new JPanel(new BorderLayout());
044: content.setBorder(new EmptyBorder(12, 12, 12, 12));
045: setContentPane(content);
046: JPanel center = new JPanel(new GridLayout(2, 1, 2, 12));
047:
048: clips = new JList(model);
049: clips.setCellRenderer(new Renderer());
050: clips.setVisibleRowCount(12);
051:
052: clips.addMouseListener(new MouseHandler());
053: clips.addListSelectionListener(new ListHandler());
054:
055: insert = new JButton(jEdit.getProperty("common.insert"));
056: cancel = new JButton(jEdit.getProperty("common.cancel"));
057:
058: JLabel label = new JLabel(jEdit.getProperty(name + ".caption"));
059: label.setBorder(new EmptyBorder(0, 0, 6, 0));
060: content.add(BorderLayout.NORTH, label);
061:
062: JScrollPane scroller = new JScrollPane(clips);
063: scroller.setPreferredSize(new Dimension(500, 150));
064: center.add(scroller);
065:
066: clipText = new JTextArea();
067: clipText.setEditable(false);
068: scroller = new JScrollPane(clipText);
069: scroller.setPreferredSize(new Dimension(500, 150));
070: center.add(scroller);
071:
072: content.add(center, BorderLayout.CENTER);
073:
074: JPanel panel = new JPanel();
075: panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
076: panel.setBorder(new EmptyBorder(12, 0, 0, 0));
077: panel.add(Box.createGlue());
078: panel.add(insert);
079: panel.add(Box.createHorizontalStrut(6));
080: panel.add(cancel);
081: panel.add(Box.createGlue());
082: content.add(panel, BorderLayout.SOUTH);
083:
084: if (model.getSize() >= 1)
085: clips.setSelectedIndex(0);
086: updateButtons();
087:
088: getRootPane().setDefaultButton(insert);
089: insert.addActionListener(new ActionHandler());
090: cancel.addActionListener(new ActionHandler());
091:
092: GUIUtilities.requestFocus(this , clips);
093:
094: pack();
095: setLocationRelativeTo(view);
096: setVisible(true);
097: } //}}}
098:
099: //{{{ ok() method
100: public void ok() {
101: Object[] selected = clips.getSelectedValues();
102: if (selected == null || selected.length == 0) {
103: getToolkit().beep();
104: return;
105: }
106:
107: String text = getSelectedClipText();
108:
109: /**
110: * For each selected clip, we remove it, then add it back
111: * to the model. This has the effect of moving it to the
112: * top of the list.
113: */
114: for (int i = 0; i < selected.length; i++) {
115: listModel.removeElement(selected[i]);
116: listModel.insertElementAt(selected[i], 0);
117: }
118:
119: view.getTextArea().setSelectedText(text);
120:
121: dispose();
122: } //}}}
123:
124: //{{{ cancel() method
125: public void cancel() {
126: dispose();
127: } //}}}
128:
129: //{{{ Private members
130:
131: //{{{ Instance variables
132: private View view;
133: private MutableListModel listModel;
134: private JList clips;
135: private JTextArea clipText;
136: private JButton insert;
137: private JButton cancel;
138:
139: //}}}
140:
141: //{{{ getSelectedClipText()
142: private String getSelectedClipText() {
143: Object[] selected = clips.getSelectedValues();
144: StringBuffer clip = new StringBuffer();
145: for (int i = 0; i < selected.length; i++) {
146: if (i != 0)
147: clip.append('\n');
148: clip.append(selected[i]);
149: }
150: return clip.toString();
151: }
152:
153: //}}}
154:
155: //{{{ updateButtons() method
156: private void updateButtons() {
157: int selected = clips.getSelectedIndex();
158: insert.setEnabled(selected != -1);
159: } //}}}
160:
161: //{{{ showClipText() method
162: private void showClipText() {
163: Object[] selected = clips.getSelectedValues();
164: if (selected == null || selected.length == 0)
165: clipText.setText("");
166: else
167: clipText.setText(getSelectedClipText());
168: clipText.setCaretPosition(0);
169: }
170:
171: //}}}
172:
173: //}}}
174:
175: //{{{ Renderer class
176: class Renderer extends DefaultListCellRenderer {
177: String shorten(String item) {
178: StringBuffer buf = new StringBuffer();
179: // workaround for Swing rendering labels starting
180: // with <html> using the HTML engine
181: if (item.toLowerCase().startsWith("<html>"))
182: buf.append(' ');
183: boolean ws = true;
184: for (int i = 0; i < item.length(); i++) {
185: char ch = item.charAt(i);
186: if (Character.isWhitespace(ch)) {
187: if (ws)
188: /* do nothing */;
189: else {
190: buf.append(' ');
191: ws = true;
192: }
193: } else {
194: ws = false;
195: buf.append(ch);
196: }
197: }
198:
199: if (buf.length() == 0)
200: return jEdit.getProperty("paste-from-list.whitespace");
201: return buf.toString();
202: }
203:
204: public Component getListCellRendererComponent(JList list,
205: Object value, int index, boolean isSelected,
206: boolean cellHasFocus) {
207: super .getListCellRendererComponent(list, value, index,
208: isSelected, cellHasFocus);
209:
210: setText(shorten(value.toString()));
211:
212: return this ;
213: }
214: } //}}}
215:
216: //{{{ ActionHandler class
217: class ActionHandler implements ActionListener {
218: public void actionPerformed(ActionEvent evt) {
219: Object source = evt.getSource();
220: if (source == insert)
221: ok();
222: else if (source == cancel)
223: cancel();
224: }
225: } //}}}
226:
227: //{{{ ListHandler class
228: class ListHandler implements ListSelectionListener {
229: //{{{ valueChanged() method
230: public void valueChanged(ListSelectionEvent evt) {
231: showClipText();
232: updateButtons();
233: } //}}}
234: } //}}}
235:
236: //{{{ MouseHandler class
237: class MouseHandler extends MouseAdapter {
238: public void mouseClicked(MouseEvent evt) {
239: if (evt.getClickCount() == 2)
240: ok();
241: }
242: } //}}}
243: }
|