PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/gui/SelectLineRange.java

#
Java | 180 lines | 120 code | 28 blank | 32 comment | 8 complexity | 7aa11e198a8de0b38818e9310e5e6f26 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. show();
  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. textArea.setSelection(new Selection.Range(
  91. buffer.getLineStartOffset(startLine),
  92. buffer.getLineEndOffset(endLine) - 1));
  93. textArea.moveCaretPosition(buffer.getLineEndOffset(endLine) - 1);
  94. dispose();
  95. } //}}}
  96. //{{{ cancel() method
  97. public void cancel()
  98. {
  99. dispose();
  100. } //}}}
  101. //{{{ actionPerformed() method
  102. public void actionPerformed(ActionEvent evt)
  103. {
  104. Object source = evt.getSource();
  105. if(source == ok)
  106. ok();
  107. else if(source == cancel)
  108. cancel();
  109. } //}}}
  110. //{{{ Private members
  111. //{{{ Instance variables
  112. private View view;
  113. private JTextField startField;
  114. private JTextField endField;
  115. private JButton ok;
  116. private JButton cancel;
  117. //}}}
  118. //{{{ createFieldPanel() method
  119. private JPanel createFieldPanel()
  120. {
  121. GridBagLayout layout = new GridBagLayout();
  122. JPanel panel = new JPanel(layout);
  123. GridBagConstraints cons = new GridBagConstraints();
  124. cons.insets = new Insets(0,0,6,12);
  125. cons.gridwidth = cons.gridheight = 1;
  126. cons.gridx = cons.gridy = 0;
  127. cons.fill = GridBagConstraints.BOTH;
  128. JLabel label = new JLabel(jEdit.getProperty("selectlinerange.start"),
  129. SwingConstants.RIGHT);
  130. layout.setConstraints(label,cons);
  131. panel.add(label);
  132. startField = new JTextField(10);
  133. cons.gridx = 1;
  134. cons.weightx = 1.0f;
  135. layout.setConstraints(startField,cons);
  136. panel.add(startField);
  137. label = new JLabel(jEdit.getProperty("selectlinerange.end"),
  138. SwingConstants.RIGHT);
  139. cons.gridx = 0;
  140. cons.weightx = 0.0f;
  141. cons.gridy = 1;
  142. layout.setConstraints(label,cons);
  143. panel.add(label);
  144. endField = new JTextField(10);
  145. cons.gridx = 1;
  146. cons.weightx = 1.0f;
  147. layout.setConstraints(endField,cons);
  148. panel.add(endField);
  149. return panel;
  150. } //}}}
  151. //}}}
  152. }