1 31 32 33 package org.antlr.works.find; 34 35 import com.jgoodies.forms.factories.FormFactory; 36 import com.jgoodies.forms.layout.*; 37 import org.antlr.works.IDE; 38 import org.antlr.xjlib.appkit.frame.XJPanel; 39 import org.antlr.xjlib.appkit.utils.XJAlert; 40 41 import javax.swing.*; 42 import java.awt.*; 43 import java.awt.event.ActionEvent ; 44 import java.awt.event.ActionListener ; 45 import java.awt.event.KeyEvent ; 46 47 public class FindAndReplaceDialog extends XJPanel { 48 49 private FindAndReplace delegate; 50 51 public FindAndReplaceDialog(FindAndReplace delegate) { 52 this.delegate = delegate; 53 54 initComponents(); 55 56 setSize(550, 180); 57 setTitle("Find"); 58 awake(); 59 center(); 60 61 getRootPane().setDefaultButton(nextButton); 62 addEscapeHandling(); 63 64 createActions(); 65 66 ignoreCaseButton.setSelected(true); 68 delegate.setIgnoreCase(true); 69 } 70 71 public void setFindText(String text) { 72 if(text == null || text.length() == 0) return; 73 findField.setText(text); 74 } 75 76 public void addEscapeHandling() { 77 KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true); 78 79 ActionListener cancelAction = new AbstractAction() { 80 public void actionPerformed(ActionEvent ae) { 81 setVisible(false); 82 } 83 }; 84 85 getRootPane().registerKeyboardAction(cancelAction, "CancelAction", ks, 86 JComponent.WHEN_IN_FOCUSED_WINDOW); 87 } 88 89 public void createActions() { 90 nextButton.addActionListener(new ActionListener () { 91 public void actionPerformed(ActionEvent event) { 92 delegate.setFindString(findField.getText()); 93 alertEndOfDocument(this, delegate.next()); 94 } 95 }); 96 97 previousButton.addActionListener(new ActionListener () { 98 public void actionPerformed(ActionEvent event) { 99 delegate.setFindString(findField.getText()); 100 alertBeginningOfDocument(this, delegate.prev()); 101 } 102 }); 103 104 replaceButton.addActionListener(new ActionListener () { 105 public void actionPerformed(ActionEvent event) { 106 delegate.setReplaceString(replaceField.getText()); 107 delegate.replace(); 108 } 109 }); 110 111 replaceAndFindButton.addActionListener(new ActionListener () { 112 public void actionPerformed(ActionEvent event) { 113 delegate.setFindString(findField.getText()); 114 delegate.setReplaceString(replaceField.getText()); 115 delegate.replace(); 116 alertEndOfDocument(this, delegate.next()); 117 } 118 }); 119 120 replaceAllButton.addActionListener(new ActionListener () { 121 public void actionPerformed(ActionEvent event) { 122 delegate.setFindString(findField.getText()); 123 delegate.setReplaceString(replaceField.getText()); 124 delegate.replaceAll(); 125 } 126 }); 127 128 ignoreCaseButton.addActionListener(new ActionListener () { 129 public void actionPerformed(ActionEvent event) { 130 delegate.setIgnoreCase(ignoreCaseButton.isSelected()); 131 } 132 }); 133 134 regexButton.addActionListener(new ActionListener () { 135 public void actionPerformed(ActionEvent event) { 136 delegate.setRegex(regexButton.isSelected()); 137 } 138 }); 139 140 optionsCombo.addActionListener(new ActionListener () { 141 public void actionPerformed(ActionEvent event) { 142 delegate.setOptions(optionsCombo.getSelectedIndex()); 143 } 144 }); 145 } 146 147 private void alertEndOfDocument(ActionListener actionListener, boolean result) { 148 if(result) return; 149 150 if(XJAlert.displayAlert(getJavaContainer(), "End of Document", "The end of the document has been reached.", 151 "Continue", "OK", 0) == 0) 152 { 153 delegate.setPositionToTop(); 154 actionListener.actionPerformed(null); 155 } 156 } 157 158 private void alertBeginningOfDocument(ActionListener actionListener, boolean result) { 159 if(result) return; 160 161 if(XJAlert.displayAlert(getJavaContainer(), "Beginning of Document", "The beginning of the document has been reached.", 162 "Continue", "OK", 0) == 0) 163 { 164 delegate.setPositionToBottom(); 165 actionListener.actionPerformed(null); 166 } 167 } 168 169 public boolean shouldDisplayMainMenuBar() { 170 return super.shouldDisplayMainMenuBar() && !IDE.isPlugin(); 171 } 172 173 private void initComponents() { 174 label1 = new JLabel(); 177 findField = new JTextField(); 178 label2 = new JLabel(); 179 replaceField = new JTextField(); 180 ignoreCaseButton = new JCheckBox(); 181 regexButton = new JCheckBox(); 182 optionsCombo = new JComboBox(); 183 replaceAllButton = new JButton(); 184 replaceButton = new JButton(); 185 replaceAndFindButton = new JButton(); 186 previousButton = new JButton(); 187 nextButton = new JButton(); 188 CellConstraints cc = new CellConstraints(); 189 190 Container contentPane = getContentPane(); 192 contentPane.setLayout(new FormLayout( 193 new ColumnSpec[] { 194 new ColumnSpec(Sizes.DLUX5), 195 FormFactory.LABEL_COMPONENT_GAP_COLSPEC, 196 new ColumnSpec(ColumnSpec.RIGHT, Sizes.DEFAULT, FormSpec.NO_GROW), 197 FormFactory.LABEL_COMPONENT_GAP_COLSPEC, 198 FormFactory.DEFAULT_COLSPEC, 199 FormFactory.LABEL_COMPONENT_GAP_COLSPEC, 200 FormFactory.DEFAULT_COLSPEC, 201 FormFactory.LABEL_COMPONENT_GAP_COLSPEC, 202 new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW), 203 FormFactory.LABEL_COMPONENT_GAP_COLSPEC, 204 FormFactory.DEFAULT_COLSPEC, 205 FormFactory.LABEL_COMPONENT_GAP_COLSPEC, 206 FormFactory.DEFAULT_COLSPEC, 207 FormFactory.LABEL_COMPONENT_GAP_COLSPEC, 208 new ColumnSpec(Sizes.DLUX5) 209 }, 210 new RowSpec[] { 211 new RowSpec(Sizes.DLUY5), 212 FormFactory.LINE_GAP_ROWSPEC, 213 FormFactory.DEFAULT_ROWSPEC, 214 FormFactory.LINE_GAP_ROWSPEC, 215 FormFactory.DEFAULT_ROWSPEC, 216 FormFactory.LINE_GAP_ROWSPEC, 217 FormFactory.DEFAULT_ROWSPEC, 218 FormFactory.LINE_GAP_ROWSPEC, 219 new RowSpec(RowSpec.CENTER, Sizes.DEFAULT, FormSpec.DEFAULT_GROW), 220 FormFactory.LINE_GAP_ROWSPEC, 221 FormFactory.DEFAULT_ROWSPEC, 222 FormFactory.LINE_GAP_ROWSPEC, 223 new RowSpec(Sizes.DLUY5) 224 })); 225 226 label1.setText("Find:"); 228 contentPane.add(label1, cc.xy(3, 3)); 229 contentPane.add(findField, cc.xywh(5, 3, 9, 1)); 230 231 label2.setText("Replace by:"); 233 contentPane.add(label2, cc.xy(3, 5)); 234 contentPane.add(replaceField, cc.xywh(5, 5, 9, 1)); 235 236 ignoreCaseButton.setText("Ignore case"); 238 contentPane.add(ignoreCaseButton, cc.xy(5, 7)); 239 240 regexButton.setText("Regular expression"); 242 contentPane.add(regexButton, cc.xy(7, 7)); 243 244 optionsCombo.setModel(new DefaultComboBoxModel(new String [] { 246 "Contains", 247 "Starts with", 248 "Whole words", 249 "Ends with" 250 })); 251 contentPane.add(optionsCombo, cc.xywh(11, 7, 3, 1)); 252 253 replaceAllButton.setText("Replace All"); 255 contentPane.add(replaceAllButton, cc.xy(3, 11)); 256 257 replaceButton.setText("Replace"); 259 contentPane.add(replaceButton, cc.xy(5, 11)); 260 261 replaceAndFindButton.setText("Replace & Find"); 263 contentPane.add(replaceAndFindButton, cc.xy(7, 11)); 264 265 previousButton.setText("Previous"); 267 contentPane.add(previousButton, cc.xy(11, 11)); 268 269 nextButton.setText("Next"); 271 contentPane.add(nextButton, cc.xy(13, 11)); 272 pack(); 273 } 275 276 private JLabel label1; 279 private JTextField findField; 280 private JLabel label2; 281 private JTextField replaceField; 282 private JCheckBox ignoreCaseButton; 283 private JCheckBox regexButton; 284 private JComboBox optionsCombo; 285 private JButton replaceAllButton; 286 private JButton replaceButton; 287 private JButton replaceAndFindButton; 288 private JButton previousButton; 289 private JButton nextButton; 290 292 } 293 | Popular Tags |