PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/gui/PastePrevious.java

#
Java | 202 lines | 133 code | 31 blank | 38 comment | 15 complexity | 2d20ca803ea1b37f4235e53b7915b151 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. * PastePrevious.java - Paste previous dialog
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1998, 1999, 2001 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 java.util.Vector;
  30. import org.gjt.sp.jedit.*;
  31. //}}}
  32. public class PastePrevious extends EnhancedDialog
  33. implements ActionListener, ListSelectionListener, MouseListener
  34. {
  35. //{{{ PastePrevious constructor
  36. public PastePrevious(View view)
  37. {
  38. super(view,jEdit.getProperty("pasteprev.title"),true);
  39. this.view = view;
  40. JPanel content = new JPanel(new BorderLayout());
  41. content.setBorder(new EmptyBorder(12,12,12,12));
  42. setContentPane(content);
  43. clipHistory = HistoryModel.getModel("clipboard");
  44. clips = new JList(new AbstractListModel() {
  45. public int getSize()
  46. {
  47. return clipHistory.getSize();
  48. }
  49. public Object getElementAt(int index)
  50. {
  51. StringBuffer buf = new StringBuffer();
  52. String item = clipHistory.getItem(index);
  53. // workaround for Swing rendering labels starting
  54. // with <html> using the HTML engine
  55. if(item.toLowerCase().startsWith("<html>"))
  56. buf.append(' ');
  57. boolean ws = true;
  58. for(int i = 0; i < item.length(); i++)
  59. {
  60. char ch = item.charAt(i);
  61. if(Character.isWhitespace(ch))
  62. {
  63. if(ws)
  64. /* do nothing */;
  65. else
  66. {
  67. buf.append(' ');
  68. ws = true;
  69. }
  70. }
  71. else
  72. {
  73. ws = false;
  74. buf.append(ch);
  75. }
  76. }
  77. return buf.toString();
  78. }
  79. });
  80. clips.setVisibleRowCount(16);
  81. clips.addMouseListener(this);
  82. clips.addListSelectionListener(this);
  83. insert = new JButton(jEdit.getProperty("pasteprev.insert"));
  84. cancel = new JButton(jEdit.getProperty("common.cancel"));
  85. JLabel label = new JLabel(jEdit.getProperty("pasteprev.caption"));
  86. label.setBorder(new EmptyBorder(0,0,6,0));
  87. content.add(BorderLayout.NORTH,label);
  88. JScrollPane scroller = new JScrollPane(clips);
  89. Dimension dim = scroller.getPreferredSize();
  90. scroller.setPreferredSize(new Dimension(640,dim.height));
  91. content.add(scroller, BorderLayout.CENTER);
  92. JPanel panel = new JPanel();
  93. panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
  94. panel.setBorder(new EmptyBorder(12,0,0,0));
  95. panel.add(Box.createGlue());
  96. panel.add(insert);
  97. panel.add(Box.createHorizontalStrut(6));
  98. panel.add(cancel);
  99. panel.add(Box.createGlue());
  100. content.add(panel, BorderLayout.SOUTH);
  101. if(clipHistory.getSize() >= 1)
  102. clips.setSelectedIndex(0);
  103. updateButtons();
  104. getRootPane().setDefaultButton(insert);
  105. insert.addActionListener(this);
  106. cancel.addActionListener(this);
  107. GUIUtilities.requestFocus(this,clips);
  108. pack();
  109. setLocationRelativeTo(view);
  110. show();
  111. } //}}}
  112. //{{{ ok() method
  113. public void ok()
  114. {
  115. int selected = clips.getSelectedIndex();
  116. if(selected == -1)
  117. {
  118. view.getToolkit().beep();
  119. return;
  120. }
  121. String clip = clipHistory.getItem(selected);
  122. view.getTextArea().setSelectedText(clip);
  123. dispose();
  124. } //}}}
  125. //{{{ cancel() method
  126. public void cancel()
  127. {
  128. dispose();
  129. } //}}}
  130. //{{{ actionPerformed() method
  131. public void actionPerformed(ActionEvent evt)
  132. {
  133. Object source = evt.getSource();
  134. if(source == insert)
  135. ok();
  136. else if(source == cancel)
  137. cancel();
  138. } //}}}
  139. //{{{ mouseClicked() method
  140. public void mouseClicked(MouseEvent evt)
  141. {
  142. if(evt.getClickCount() == 2)
  143. ok();
  144. } //}}}
  145. //{{{ Crap
  146. public void mouseEntered(MouseEvent evt) {}
  147. public void mouseExited(MouseEvent evt) {}
  148. public void mousePressed(MouseEvent evt) {}
  149. public void mouseReleased(MouseEvent evt) {}
  150. //}}}
  151. //{{{ valueChanged() method
  152. public void valueChanged(ListSelectionEvent evt)
  153. {
  154. updateButtons();
  155. } //}}}
  156. //{{{ Private members
  157. //{{{ Instance variables
  158. private View view;
  159. private JList clips;
  160. private HistoryModel clipHistory;
  161. private JButton insert;
  162. private JButton cancel;
  163. //}}}
  164. //{{{ updateButtons() method
  165. private void updateButtons()
  166. {
  167. int selected = clips.getSelectedIndex();
  168. insert.setEnabled(selected != -1);
  169. } //}}}
  170. //}}}
  171. }