KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > PasteFromListDialog


1 /*
2  * PasteFromListDialog.java - Paste previous/paste deleted dialog
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2003, 2005 Slava Pestov
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
23 package org.gjt.sp.jedit.gui;
24
25 //{{{ Imports
26
import javax.swing.*;
27 import javax.swing.border.*;
28 import javax.swing.event.*;
29 import java.awt.*;
30 import java.awt.event.*;
31 import org.gjt.sp.jedit.*;
32 //}}}
33

34 public class PasteFromListDialog extends EnhancedDialog
35 {
36     //{{{ PasteFromListDialog constructor
37
public PasteFromListDialog(String JavaDoc name, View view, MutableListModel model)
38     {
39         super(view,jEdit.getProperty(name + ".title"),true);
40         this.view = view;
41         this.listModel = model;
42
43         JPanel content = new JPanel(new BorderLayout());
44         content.setBorder(new EmptyBorder(12,12,12,12));
45         setContentPane(content);
46         JPanel center = new JPanel(new GridLayout(2,1,2,12));
47
48         clips = new JList(model);
49         clips.setCellRenderer(new Renderer());
50         clips.setVisibleRowCount(12);
51
52         clips.addMouseListener(new MouseHandler());
53         clips.addListSelectionListener(new ListHandler());
54
55         insert = new JButton(jEdit.getProperty("common.insert"));
56         cancel = new JButton(jEdit.getProperty("common.cancel"));
57
58         JLabel label = new JLabel(jEdit.getProperty(name + ".caption"));
59         label.setBorder(new EmptyBorder(0,0,6,0));
60         content.add(BorderLayout.NORTH,label);
61
62         JScrollPane scroller = new JScrollPane(clips);
63         scroller.setPreferredSize(new Dimension(500,150));
64         center.add(scroller);
65
66         clipText = new JTextArea();
67         clipText.setEditable(false);
68         scroller = new JScrollPane(clipText);
69         scroller.setPreferredSize(new Dimension(500,150));
70         center.add(scroller);
71
72         content.add(center, BorderLayout.CENTER);
73
74         JPanel panel = new JPanel();
75         panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
76         panel.setBorder(new EmptyBorder(12,0,0,0));
77         panel.add(Box.createGlue());
78         panel.add(insert);
79         panel.add(Box.createHorizontalStrut(6));
80         panel.add(cancel);
81         panel.add(Box.createGlue());
82         content.add(panel, BorderLayout.SOUTH);
83
84         if(model.getSize() >= 1)
85             clips.setSelectedIndex(0);
86         updateButtons();
87
88         getRootPane().setDefaultButton(insert);
89         insert.addActionListener(new ActionHandler());
90         cancel.addActionListener(new ActionHandler());
91
92         GUIUtilities.requestFocus(this,clips);
93
94         pack();
95         setLocationRelativeTo(view);
96         setVisible(true);
97     } //}}}
98

99     //{{{ ok() method
100
public void ok()
101     {
102         Object JavaDoc[] selected = clips.getSelectedValues();
103         if(selected == null || selected.length == 0)
104         {
105             getToolkit().beep();
106             return;
107         }
108
109         String JavaDoc text = getSelectedClipText();
110
111         /**
112          * For each selected clip, we remove it, then add it back
113          * to the model. This has the effect of moving it to the
114          * top of the list.
115          */

116         for(int i = 0; i < selected.length; i++)
117         {
118             listModel.removeElement(selected[i]);
119             listModel.insertElementAt(selected[i],0);
120         }
121
122         view.getTextArea().setSelectedText(text);
123
124         dispose();
125     } //}}}
126

127     //{{{ cancel() method
128
public void cancel()
129     {
130         dispose();
131     } //}}}
132

133     //{{{ Private members
134

135     //{{{ Instance variables
136
private View view;
137     private MutableListModel listModel;
138     private JList clips;
139     private JTextArea clipText;
140     private JButton insert;
141     private JButton cancel;
142     //}}}
143

144     //{{{ getSelectedClipText()
145
private String JavaDoc getSelectedClipText()
146     {
147         Object JavaDoc[] selected = clips.getSelectedValues();
148         StringBuffer JavaDoc clip = new StringBuffer JavaDoc();
149         for(int i = 0; i < selected.length; i++)
150         {
151             if(i != 0)
152                 clip.append('\n');
153             clip.append(selected[i]);
154         }
155         return clip.toString();
156     }
157     //}}}
158

159     //{{{ updateButtons() method
160
private void updateButtons()
161     {
162         int selected = clips.getSelectedIndex();
163         insert.setEnabled(selected != -1);
164     } //}}}
165

166     //{{{ showClipText() method
167
private void showClipText()
168     {
169         Object JavaDoc[] selected = clips.getSelectedValues();
170         if(selected == null || selected.length == 0)
171             clipText.setText("");
172         else
173             clipText.setText(getSelectedClipText());
174         clipText.setCaretPosition(0);
175     }
176     //}}}
177

178     //}}}
179

180     //{{{ Renderer class
181
class Renderer extends DefaultListCellRenderer
182     {
183         String JavaDoc shorten(String JavaDoc item)
184         {
185             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
186             // workaround for Swing rendering labels starting
187
// with <html> using the HTML engine
188
if(item.toLowerCase().startsWith("<html>"))
189                 buf.append(' ');
190             boolean ws = true;
191             for(int i = 0; i < item.length(); i++)
192             {
193                 char ch = item.charAt(i);
194                 if(Character.isWhitespace(ch))
195                 {
196                     if(ws)
197                         /* do nothing */;
198                     else
199                     {
200                         buf.append(' ');
201                         ws = true;
202                     }
203                 }
204                 else
205                 {
206                     ws = false;
207                     buf.append(ch);
208                 }
209             }
210
211             if(buf.length() == 0)
212                 return jEdit.getProperty("paste-from-list.whitespace");
213             return buf.toString();
214         }
215
216         public Component getListCellRendererComponent(
217             JList list, Object JavaDoc value, int index,
218             boolean isSelected, boolean cellHasFocus)
219         {
220             super.getListCellRendererComponent(list,value,index,
221                 isSelected,cellHasFocus);
222
223             setText(shorten(value.toString()));
224
225             return this;
226         }
227     } //}}}
228

229     //{{{ ActionHandler class
230
class ActionHandler implements ActionListener
231     {
232         public void actionPerformed(ActionEvent evt)
233         {
234             Object JavaDoc source = evt.getSource();
235             if(source == insert)
236                 ok();
237             else if(source == cancel)
238                 cancel();
239         }
240     } //}}}
241

242     //{{{ ListHandler class
243
class ListHandler implements ListSelectionListener
244     {
245         //{{{ valueChanged() method
246
public void valueChanged(ListSelectionEvent evt)
247         {
248             showClipText();
249             updateButtons();
250         } //}}}
251
} //}}}
252

253     //{{{ MouseHandler class
254
class MouseHandler extends MouseAdapter
255     {
256         public void mouseClicked(MouseEvent evt)
257         {
258             if(evt.getClickCount() == 2)
259                 ok();
260         }
261     } //}}}
262
}
263
Popular Tags