1 package org.columba.core.gui.util; 2 3 import java.awt.BorderLayout ; 4 import java.awt.event.ActionEvent ; 5 import java.awt.event.ActionListener ; 6 7 import javax.swing.BorderFactory ; 8 import javax.swing.JDialog ; 9 import javax.swing.JOptionPane ; 10 import javax.swing.JPanel ; 11 import javax.swing.JTextField ; 12 13 import org.columba.core.gui.base.ButtonWithMnemonic; 14 import org.columba.core.gui.base.CheckBoxWithMnemonic; 15 import org.columba.core.gui.base.LabelWithMnemonic; 16 import org.columba.core.resourceloader.GlobalResourceLoader; 17 import org.columba.core.resourceloader.IconKeys; 18 import org.columba.core.resourceloader.ImageLoader; 19 import org.columba.core.util.FindReplace; 20 import org.columba.core.util.IEditableText; 21 22 import com.jgoodies.forms.layout.CellConstraints; 23 import com.jgoodies.forms.layout.FormLayout; 24 25 @SuppressWarnings ("serial") 26 public class FindReplaceDialog extends JDialog implements ActionListener { 27 private static final String RESOURCE_PATH = "org.columba.core.i18n.dialog"; 28 29 private LabelWithMnemonic findLabel; 30 private LabelWithMnemonic replaceLabel; 31 32 private ButtonWithMnemonic findButton; 33 private ButtonWithMnemonic replaceButton; 34 private ButtonWithMnemonic replaceAllButton; 35 private ButtonWithMnemonic closeButton; 36 37 private CheckBoxWithMnemonic wholeWordCheckBox; 38 private CheckBoxWithMnemonic caseSensCheckBox; 39 private CheckBoxWithMnemonic findBackCheckBox; 40 41 private JTextField findTextField; 42 private JTextField replaceWithTextField; 43 44 private IEditableText text; 45 private FindReplace finder = new FindReplace(); 46 47 public FindReplaceDialog(IEditableText text) { 48 setTitle(GlobalResourceLoader.getString(RESOURCE_PATH,"findreplace","findreplace.title")); 49 this.text = text; 50 initComponents(); 51 setResizable(false); 52 pack(); 53 setVisible(true); 54 } 55 56 private void createComponents(){ 57 58 findLabel = new LabelWithMnemonic(GlobalResourceLoader.getString(RESOURCE_PATH,"findreplace","findlabel")); 59 replaceLabel = new LabelWithMnemonic(GlobalResourceLoader.getString(RESOURCE_PATH,"findreplace","replacewithlabel")); 60 61 findTextField = new JTextField (16); 62 replaceWithTextField = new JTextField (16); 63 64 wholeWordCheckBox = new CheckBoxWithMnemonic(GlobalResourceLoader.getString(RESOURCE_PATH,"findreplace","matchword")); 65 caseSensCheckBox = new CheckBoxWithMnemonic(GlobalResourceLoader.getString(RESOURCE_PATH,"findreplace","matchcase")); 66 findBackCheckBox = new CheckBoxWithMnemonic(GlobalResourceLoader.getString(RESOURCE_PATH,"findreplace","findbackwards")); 67 68 findButton = new ButtonWithMnemonic(GlobalResourceLoader.getString(RESOURCE_PATH,"findreplace","find")); 69 replaceButton = new ButtonWithMnemonic(GlobalResourceLoader.getString(RESOURCE_PATH,"findreplace","replace")); 70 replaceAllButton = new ButtonWithMnemonic(GlobalResourceLoader.getString(RESOURCE_PATH,"findreplace","replaceall")); 71 closeButton = new ButtonWithMnemonic(GlobalResourceLoader.getString(RESOURCE_PATH,"findreplace","close")); 72 73 } 74 private void initComponents(){ 75 76 FormLayout layout = new FormLayout("pref, 6dlu, pref", "pref, 3dlu, pref"); 79 CellConstraints cc = new CellConstraints(); 80 81 createComponents(); 82 83 JPanel panelMain = new JPanel (layout); 84 panelMain.setBorder(BorderFactory.createEmptyBorder(12,12,12,12)); 85 86 panelMain.add(createInputPanel(), cc.xy(1,1)); 87 panelMain.add(createButtonPanel(),cc.xywh(3,1,1,3)); 88 panelMain.add(createOptionPanel(),cc.xy(1,3)); 89 90 getContentPane().add(panelMain,BorderLayout.SOUTH); 91 getContentPane().add( 92 new DialogHeaderPanel(GlobalResourceLoader.getString(RESOURCE_PATH, 93 "findreplace", "header_title"), GlobalResourceLoader 94 .getString(RESOURCE_PATH, "findreplace", "header_description"), 95 ImageLoader.getIcon(IconKeys.SEARCH)), 96 BorderLayout.NORTH); 97 } 98 99 private JPanel createInputPanel(){ 100 FormLayout layout = new FormLayout("left:pref, 6dlu, left:pref", 101 "pref, 3dlu, pref"); 102 CellConstraints cc = new CellConstraints(); 103 JPanel inputPanel = new JPanel (layout); 104 105 inputPanel.add(findLabel,cc.xy(1,1)); 106 findLabel.setLabelFor(findTextField); 107 inputPanel.add(findTextField,cc.xy(3,1)); 108 109 inputPanel.add(replaceLabel,cc.xy(1,3)); 110 replaceLabel.setLabelFor(replaceWithTextField); 111 inputPanel.add(replaceWithTextField,cc.xy(3,3)); 112 113 return inputPanel; 114 } 115 116 private JPanel createOptionPanel(){ 117 FormLayout layout = new FormLayout("3dlu, left:pref, 30dlu, right:pref, 3dlu", "pref, 3dlu, pref"); 121 CellConstraints cc = new CellConstraints(); 122 123 JPanel optionPanel = new JPanel (layout); 124 optionPanel.setBorder(BorderFactory.createTitledBorder(GlobalResourceLoader.getString(RESOURCE_PATH,"findreplace","options"))); 125 126 optionPanel.add(caseSensCheckBox,cc.xy(2,1)); 127 optionPanel.add(wholeWordCheckBox,cc.xywh(2,3,3,1)); 128 optionPanel.add(findBackCheckBox,cc.xy(4,1)); 129 130 return optionPanel; 131 } 132 133 134 private JPanel createButtonPanel(){ 135 FormLayout layout = new FormLayout("fill:pref", "pref, 3dlu, pref, 3dlu, pref, 3dlu, pref"); CellConstraints cc = new CellConstraints(); 139 JPanel buttonPanel = new JPanel (layout); 140 141 142 findButton.addActionListener(this); 143 findButton.setActionCommand("FIND"); 144 this.getRootPane().setDefaultButton(findButton); 145 146 replaceButton.addActionListener(this); 147 replaceButton.setActionCommand("REPLACE"); 148 149 replaceAllButton.addActionListener(this); 150 replaceAllButton.setActionCommand("REPLACEALL"); 151 152 closeButton.addActionListener(this); 153 closeButton.setActionCommand("CLOSE"); 154 155 buttonPanel.add(findButton,cc.xy(1,1)); 156 buttonPanel.add(replaceButton,cc.xy(1,3)); 157 buttonPanel.add(replaceAllButton,cc.xy(1,5)); 158 buttonPanel.add(closeButton,cc.xy(1,7)); 159 160 return buttonPanel; 161 } 162 163 164 public void actionPerformed(ActionEvent e) { 165 String action = e.getActionCommand(); 167 168 String source = text.getText(); 169 170 finder.setPattern(findTextField.getText()); 172 finder.setSource(source); 173 finder.setCaseSensitive(caseSensCheckBox.isSelected()); 174 finder.setMatchWholeWord(wholeWordCheckBox.isSelected()); 175 finder.setBackwards(findBackCheckBox.isSelected()); 176 finder.setReplaceWith(replaceWithTextField.getText()); 177 178 if (action.equals("FIND")) { 180 int position = finder.findnext(); 181 if(position>=0) { 182 mark(position,position + findTextField.getText().length()); 184 } else 186 { 187 mark(0, 0); 189 JOptionPane.showMessageDialog(this, GlobalResourceLoader.getString(RESOURCE_PATH,"findreplace","string_not_found")); 190 } 191 } 192 else if (action.equals("REPLACE")){ 194 int position = finder.replace(); 195 if (position>=0) { 196 text.setText(finder.getSource()); 198 mark(position,position + replaceWithTextField.getText().length()); 199 } 200 else 201 { 202 mark(0, 0); 203 JOptionPane.showMessageDialog(this, GlobalResourceLoader.getString(RESOURCE_PATH,"findreplace","string_not_found")); 204 } 205 } 206 else if (action.equals("REPLACEALL")){ 208 finder.replaceAll(); 209 if (finder.getSource().equals(text.getText())) { 210 text.setText(finder.getSource()); 212 JOptionPane.showMessageDialog(this, GlobalResourceLoader.getString(RESOURCE_PATH,"findreplace","string_not_found")); 213 } 214 else 215 text.setText(finder.getSource()); 216 } 217 else if (action.equals("CLOSE")){ 218 this.dispose(); 219 } 220 } 221 222 private void mark(int start, int end) { 224 text.setCaretPosition(start); 225 text.moveCaretPosition(end); 226 } 227 } 228 | Popular Tags |