PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/gui/PasteFromListDialog.java

#
Java | 262 lines | 179 code | 36 blank | 47 comment | 27 complexity | e32588e089b06dcf378bb21f5bcaf21e MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package org.gjt.sp.jedit.gui;
  23. //{{{ Imports
  24. import javax.swing.*;
  25. import javax.swing.border.*;
  26. import javax.swing.event.*;
  27. import java.awt.*;
  28. import java.awt.event.*;
  29. import org.gjt.sp.jedit.*;
  30. //}}}
  31. public class PasteFromListDialog extends EnhancedDialog
  32. {
  33. //{{{ PasteFromListDialog constructor
  34. public PasteFromListDialog(String name, View view, MutableListModel model)
  35. {
  36. super(view,jEdit.getProperty(name + ".title"),true);
  37. this.view = view;
  38. this.listModel = model;
  39. JPanel content = new JPanel(new BorderLayout());
  40. content.setBorder(new EmptyBorder(12,12,12,12));
  41. setContentPane(content);
  42. JPanel center = new JPanel(new GridLayout(2,1,2,12));
  43. clips = new JList(model);
  44. clips.setCellRenderer(new Renderer());
  45. clips.setVisibleRowCount(12);
  46. clips.addMouseListener(new MouseHandler());
  47. clips.addListSelectionListener(new ListHandler());
  48. insert = new JButton(jEdit.getProperty("common.insert"));
  49. cancel = new JButton(jEdit.getProperty("common.cancel"));
  50. JLabel label = new JLabel(jEdit.getProperty(name + ".caption"));
  51. label.setBorder(new EmptyBorder(0,0,6,0));
  52. content.add(BorderLayout.NORTH,label);
  53. JScrollPane scroller = new JScrollPane(clips);
  54. scroller.setPreferredSize(new Dimension(500,150));
  55. center.add(scroller);
  56. clipText = new JTextArea();
  57. clipText.setEditable(false);
  58. scroller = new JScrollPane(clipText);
  59. scroller.setPreferredSize(new Dimension(500,150));
  60. center.add(scroller);
  61. content.add(center, BorderLayout.CENTER);
  62. JPanel panel = new JPanel();
  63. panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
  64. panel.setBorder(new EmptyBorder(12,0,0,0));
  65. panel.add(Box.createGlue());
  66. panel.add(insert);
  67. panel.add(Box.createHorizontalStrut(6));
  68. panel.add(cancel);
  69. panel.add(Box.createGlue());
  70. content.add(panel, BorderLayout.SOUTH);
  71. if(model.getSize() >= 1)
  72. clips.setSelectedIndex(0);
  73. updateButtons();
  74. getRootPane().setDefaultButton(insert);
  75. insert.addActionListener(new ActionHandler());
  76. cancel.addActionListener(new ActionHandler());
  77. GUIUtilities.requestFocus(this,clips);
  78. pack();
  79. setLocationRelativeTo(view);
  80. setVisible(true);
  81. } //}}}
  82. //{{{ ok() method
  83. public void ok()
  84. {
  85. Object[] selected = clips.getSelectedValues();
  86. if(selected == null || selected.length == 0)
  87. {
  88. getToolkit().beep();
  89. return;
  90. }
  91. String text = getSelectedClipText();
  92. /**
  93. * For each selected clip, we remove it, then add it back
  94. * to the model. This has the effect of moving it to the
  95. * top of the list.
  96. */
  97. for(int i = 0; i < selected.length; i++)
  98. {
  99. listModel.removeElement(selected[i]);
  100. listModel.insertElementAt(selected[i],0);
  101. }
  102. view.getTextArea().setSelectedText(text);
  103. dispose();
  104. } //}}}
  105. //{{{ cancel() method
  106. public void cancel()
  107. {
  108. dispose();
  109. } //}}}
  110. //{{{ Private members
  111. //{{{ Instance variables
  112. private View view;
  113. private MutableListModel listModel;
  114. private JList clips;
  115. private JTextArea clipText;
  116. private JButton insert;
  117. private JButton cancel;
  118. //}}}
  119. //{{{ getSelectedClipText()
  120. private String getSelectedClipText()
  121. {
  122. Object[] selected = clips.getSelectedValues();
  123. StringBuffer clip = new StringBuffer();
  124. for(int i = 0; i < selected.length; i++)
  125. {
  126. if(i != 0)
  127. clip.append('\n');
  128. clip.append(selected[i]);
  129. }
  130. return clip.toString();
  131. }
  132. //}}}
  133. //{{{ updateButtons() method
  134. private void updateButtons()
  135. {
  136. int selected = clips.getSelectedIndex();
  137. insert.setEnabled(selected != -1);
  138. } //}}}
  139. //{{{ showClipText() method
  140. private void showClipText()
  141. {
  142. Object[] selected = clips.getSelectedValues();
  143. if(selected == null || selected.length == 0)
  144. clipText.setText("");
  145. else
  146. clipText.setText(getSelectedClipText());
  147. clipText.setCaretPosition(0);
  148. }
  149. //}}}
  150. //}}}
  151. //{{{ Renderer class
  152. class Renderer extends DefaultListCellRenderer
  153. {
  154. String shorten(String item)
  155. {
  156. StringBuffer buf = new StringBuffer();
  157. // workaround for Swing rendering labels starting
  158. // with <html> using the HTML engine
  159. if(item.toLowerCase().startsWith("<html>"))
  160. buf.append(' ');
  161. boolean ws = true;
  162. for(int i = 0; i < item.length(); i++)
  163. {
  164. char ch = item.charAt(i);
  165. if(Character.isWhitespace(ch))
  166. {
  167. if(ws)
  168. /* do nothing */;
  169. else
  170. {
  171. buf.append(' ');
  172. ws = true;
  173. }
  174. }
  175. else
  176. {
  177. ws = false;
  178. buf.append(ch);
  179. }
  180. }
  181. if(buf.length() == 0)
  182. return jEdit.getProperty("paste-from-list.whitespace");
  183. return buf.toString();
  184. }
  185. public Component getListCellRendererComponent(
  186. JList list, Object value, int index,
  187. boolean isSelected, boolean cellHasFocus)
  188. {
  189. super.getListCellRendererComponent(list,value,index,
  190. isSelected,cellHasFocus);
  191. setText(shorten(value.toString()));
  192. return this;
  193. }
  194. } //}}}
  195. //{{{ ActionHandler class
  196. class ActionHandler implements ActionListener
  197. {
  198. public void actionPerformed(ActionEvent evt)
  199. {
  200. Object source = evt.getSource();
  201. if(source == insert)
  202. ok();
  203. else if(source == cancel)
  204. cancel();
  205. }
  206. } //}}}
  207. //{{{ ListHandler class
  208. class ListHandler implements ListSelectionListener
  209. {
  210. //{{{ valueChanged() method
  211. public void valueChanged(ListSelectionEvent evt)
  212. {
  213. showClipText();
  214. updateButtons();
  215. } //}}}
  216. } //}}}
  217. //{{{ MouseHandler class
  218. class MouseHandler extends MouseAdapter
  219. {
  220. public void mouseClicked(MouseEvent evt)
  221. {
  222. if(evt.getClickCount() == 2)
  223. ok();
  224. }
  225. } //}}}
  226. }