PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/SelectLineRange.java

#
Java | 184 lines | 124 code | 28 blank | 32 comment | 9 complexity | 676977a1c3aedfd560f8e16091f2b43f 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. * SelectLineRange.java - Selects a range of lines
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999, 2000 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 java.awt.*;
  27. import java.awt.event.*;
  28. import org.gjt.sp.jedit.textarea.*;
  29. import org.gjt.sp.jedit.*;
  30. //}}}
  31. public class SelectLineRange extends EnhancedDialog implements ActionListener
  32. {
  33. //{{{ SelectLineRange constructor
  34. public SelectLineRange(View view)
  35. {
  36. super(view,jEdit.getProperty("selectlinerange.title"),true);
  37. this.view = view;
  38. JPanel content = new JPanel(new BorderLayout());
  39. content.setBorder(new EmptyBorder(12,12,12,0));
  40. setContentPane(content);
  41. JLabel label = new JLabel(jEdit.getProperty(
  42. "selectlinerange.caption"));
  43. label.setBorder(new EmptyBorder(0,0,6,12));
  44. content.add(BorderLayout.NORTH,label);
  45. JPanel panel = createFieldPanel();
  46. content.add(BorderLayout.CENTER,panel);
  47. panel = new JPanel();
  48. panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
  49. panel.setBorder(new EmptyBorder(6,0,0,12));
  50. panel.add(Box.createGlue());
  51. panel.add(Box.createGlue());
  52. ok = new JButton(jEdit.getProperty("common.ok"));
  53. ok.addActionListener(this);
  54. getRootPane().setDefaultButton(ok);
  55. panel.add(ok);
  56. panel.add(Box.createHorizontalStrut(6));
  57. cancel = new JButton(jEdit.getProperty("common.cancel"));
  58. cancel.addActionListener(this);
  59. panel.add(cancel);
  60. panel.add(Box.createGlue());
  61. content.add(panel,BorderLayout.SOUTH);
  62. GUIUtilities.requestFocus(this,startField);
  63. pack();
  64. setLocationRelativeTo(view);
  65. setVisible(true);
  66. } //}}}
  67. //{{{ ok() method
  68. public void ok()
  69. {
  70. int startLine;
  71. int endLine;
  72. try
  73. {
  74. startLine = Integer.parseInt(startField.getText()) - 1;
  75. endLine = Integer.parseInt(endField.getText()) - 1;
  76. }
  77. catch(NumberFormatException nf)
  78. {
  79. getToolkit().beep();
  80. return;
  81. }
  82. Buffer buffer = view.getBuffer();
  83. if(startLine < 0 || endLine >= buffer.getLineCount()
  84. || startLine > endLine)
  85. {
  86. getToolkit().beep();
  87. return;
  88. }
  89. JEditTextArea textArea = view.getTextArea();
  90. Selection s = new Selection.Range(
  91. buffer.getLineStartOffset(startLine),
  92. buffer.getLineEndOffset(endLine) - 1);
  93. if(textArea.isMultipleSelectionEnabled())
  94. textArea.addToSelection(s);
  95. else
  96. textArea.setSelection(s);
  97. textArea.moveCaretPosition(buffer.getLineEndOffset(endLine) - 1);
  98. dispose();
  99. } //}}}
  100. //{{{ cancel() method
  101. public void cancel()
  102. {
  103. dispose();
  104. } //}}}
  105. //{{{ actionPerformed() method
  106. public void actionPerformed(ActionEvent evt)
  107. {
  108. Object source = evt.getSource();
  109. if(source == ok)
  110. ok();
  111. else if(source == cancel)
  112. cancel();
  113. } //}}}
  114. //{{{ Private members
  115. //{{{ Instance variables
  116. private View view;
  117. private JTextField startField;
  118. private JTextField endField;
  119. private JButton ok;
  120. private JButton cancel;
  121. //}}}
  122. //{{{ createFieldPanel() method
  123. private JPanel createFieldPanel()
  124. {
  125. GridBagLayout layout = new GridBagLayout();
  126. JPanel panel = new JPanel(layout);
  127. GridBagConstraints cons = new GridBagConstraints();
  128. cons.insets = new Insets(0,0,6,12);
  129. cons.gridwidth = cons.gridheight = 1;
  130. cons.gridx = cons.gridy = 0;
  131. cons.fill = GridBagConstraints.BOTH;
  132. JLabel label = new JLabel(jEdit.getProperty("selectlinerange.start"),
  133. SwingConstants.RIGHT);
  134. layout.setConstraints(label,cons);
  135. panel.add(label);
  136. startField = new JTextField(10);
  137. cons.gridx = 1;
  138. cons.weightx = 1.0f;
  139. layout.setConstraints(startField,cons);
  140. panel.add(startField);
  141. label = new JLabel(jEdit.getProperty("selectlinerange.end"),
  142. SwingConstants.RIGHT);
  143. cons.gridx = 0;
  144. cons.weightx = 0.0f;
  145. cons.gridy = 1;
  146. layout.setConstraints(label,cons);
  147. panel.add(label);
  148. endField = new JTextField(10);
  149. cons.gridx = 1;
  150. cons.weightx = 1.0f;
  151. layout.setConstraints(endField,cons);
  152. panel.add(endField);
  153. return panel;
  154. } //}}}
  155. //}}}
  156. }