1 22 23 package org.gjt.sp.jedit.gui; 24 25 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 34 public class PasteFromListDialog extends EnhancedDialog 35 { 36 public PasteFromListDialog(String 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 } 99 public void ok() 101 { 102 Object [] selected = clips.getSelectedValues(); 103 if(selected == null || selected.length == 0) 104 { 105 getToolkit().beep(); 106 return; 107 } 108 109 String text = getSelectedClipText(); 110 111 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 } 127 public void cancel() 129 { 130 dispose(); 131 } 133 135 private View view; 137 private MutableListModel listModel; 138 private JList clips; 139 private JTextArea clipText; 140 private JButton insert; 141 private JButton cancel; 142 144 private String getSelectedClipText() 146 { 147 Object [] selected = clips.getSelectedValues(); 148 StringBuffer clip = new StringBuffer (); 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 159 private void updateButtons() 161 { 162 int selected = clips.getSelectedIndex(); 163 insert.setEnabled(selected != -1); 164 } 166 private void showClipText() 168 { 169 Object [] 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 178 180 class Renderer extends DefaultListCellRenderer 182 { 183 String shorten(String item) 184 { 185 StringBuffer buf = new StringBuffer (); 186 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 ; 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 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 } 229 class ActionHandler implements ActionListener 231 { 232 public void actionPerformed(ActionEvent evt) 233 { 234 Object source = evt.getSource(); 235 if(source == insert) 236 ok(); 237 else if(source == cancel) 238 cancel(); 239 } 240 } 242 class ListHandler implements ListSelectionListener 244 { 245 public void valueChanged(ListSelectionEvent evt) 247 { 248 showClipText(); 249 updateButtons(); 250 } } 253 class MouseHandler extends MouseAdapter 255 { 256 public void mouseClicked(MouseEvent evt) 257 { 258 if(evt.getClickCount() == 2) 259 ok(); 260 } 261 } } 263 | Popular Tags |