1 22 23 package org.gjt.sp.jedit.gui; 24 25 import javax.swing.*; 27 import javax.swing.border.*; 28 import java.awt.*; 29 import java.awt.event.*; 30 import org.gjt.sp.jedit.textarea.*; 31 import org.gjt.sp.jedit.*; 32 34 public class SelectLineRange extends EnhancedDialog implements ActionListener 35 { 36 public SelectLineRange(View view) 38 { 39 super(view,jEdit.getProperty("selectlinerange.title"),true); 40 this.view = view; 41 42 JPanel content = new JPanel(new BorderLayout()); 43 content.setBorder(new EmptyBorder(12,12,12,0)); 44 setContentPane(content); 45 46 JLabel label = new JLabel(jEdit.getProperty( 47 "selectlinerange.caption")); 48 label.setBorder(new EmptyBorder(0,0,6,12)); 49 content.add(BorderLayout.NORTH,label); 50 51 JPanel panel = createFieldPanel(); 52 53 content.add(BorderLayout.CENTER,panel); 54 55 panel = new JPanel(); 56 panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS)); 57 panel.setBorder(new EmptyBorder(6,0,0,12)); 58 panel.add(Box.createGlue()); 59 panel.add(Box.createGlue()); 60 ok = new JButton(jEdit.getProperty("common.ok")); 61 ok.addActionListener(this); 62 getRootPane().setDefaultButton(ok); 63 panel.add(ok); 64 panel.add(Box.createHorizontalStrut(6)); 65 cancel = new JButton(jEdit.getProperty("common.cancel")); 66 cancel.addActionListener(this); 67 panel.add(cancel); 68 panel.add(Box.createGlue()); 69 70 content.add(panel,BorderLayout.SOUTH); 71 72 GUIUtilities.requestFocus(this,startField); 73 74 pack(); 75 setLocationRelativeTo(view); 76 setVisible(true); 77 } 79 public void ok() 81 { 82 int startLine; 83 int endLine; 84 85 try 86 { 87 startLine = Integer.parseInt(startField.getText()) - 1; 88 endLine = Integer.parseInt(endField.getText()) - 1; 89 } 90 catch(NumberFormatException nf) 91 { 92 getToolkit().beep(); 93 return; 94 } 95 96 Buffer buffer = view.getBuffer(); 97 98 if(startLine < 0 || endLine >= buffer.getLineCount() 99 || startLine > endLine) 100 { 101 getToolkit().beep(); 102 return; 103 } 104 105 JEditTextArea textArea = view.getTextArea(); 106 Selection s = new Selection.Range( 107 buffer.getLineStartOffset(startLine), 108 buffer.getLineEndOffset(endLine) - 1); 109 if(textArea.isMultipleSelectionEnabled()) 110 textArea.addToSelection(s); 111 else 112 textArea.setSelection(s); 113 textArea.moveCaretPosition(buffer.getLineEndOffset(endLine) - 1); 114 115 dispose(); 116 } 118 public void cancel() 120 { 121 dispose(); 122 } 124 public void actionPerformed(ActionEvent evt) 126 { 127 Object source = evt.getSource(); 128 if(source == ok) 129 ok(); 130 else if(source == cancel) 131 cancel(); 132 } 134 136 private View view; 138 private JTextField startField; 139 private JTextField endField; 140 private JButton ok; 141 private JButton cancel; 142 144 private JPanel createFieldPanel() 146 { 147 GridBagLayout layout = new GridBagLayout(); 148 JPanel panel = new JPanel(layout); 149 150 GridBagConstraints cons = new GridBagConstraints(); 151 cons.insets = new Insets(0,0,6,12); 152 cons.gridwidth = cons.gridheight = 1; 153 cons.gridx = cons.gridy = 0; 154 cons.fill = GridBagConstraints.BOTH; 155 JLabel label = new JLabel(jEdit.getProperty("selectlinerange.start"), 156 SwingConstants.RIGHT); 157 layout.setConstraints(label,cons); 158 panel.add(label); 159 160 startField = new JTextField(10); 161 cons.gridx = 1; 162 cons.weightx = 1.0f; 163 layout.setConstraints(startField,cons); 164 panel.add(startField); 165 166 label = new JLabel(jEdit.getProperty("selectlinerange.end"), 167 SwingConstants.RIGHT); 168 cons.gridx = 0; 169 cons.weightx = 0.0f; 170 cons.gridy = 1; 171 layout.setConstraints(label,cons); 172 panel.add(label); 173 174 endField = new JTextField(10); 175 cons.gridx = 1; 176 cons.weightx = 1.0f; 177 layout.setConstraints(endField,cons); 178 panel.add(endField); 179 180 return panel; 181 } 183 } 185 | Popular Tags |