PageRenderTime 52ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/gui/PasteFromListDialog.java

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