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.IViewableText; 21 import com.jgoodies.forms.layout.CellConstraints; 22 import com.jgoodies.forms.layout.FormLayout; 23 24 @SuppressWarnings ("serial") 25 public class FindDialog extends JDialog implements ActionListener { 26 private static final String RESOURCE_PATH = "org.columba.core.i18n.dialog"; 27 28 private LabelWithMnemonic findLabel; 29 30 private ButtonWithMnemonic findButton; 31 private ButtonWithMnemonic findNextButton; 32 private ButtonWithMnemonic closeButton; 33 34 private CheckBoxWithMnemonic wholeWordCheckBox; 35 private CheckBoxWithMnemonic caseSensCheckBox; 36 private CheckBoxWithMnemonic findBackCheckBox; 37 38 private JTextField findTextField; 39 40 private IViewableText text; 42 43 private FindReplace finder = new FindReplace(); 44 45 46 public FindDialog(IViewableText text) { 47 setTitle(GlobalResourceLoader.getString(RESOURCE_PATH,"find","find.title")); 48 createComponents(); 49 initComponents(); 50 setResizable(false); 51 pack(); 52 this.text = text; 53 setVisible(true); 54 } 55 56 private void createComponents(){ 57 58 findLabel = new LabelWithMnemonic(GlobalResourceLoader.getString(RESOURCE_PATH,"find","findlabel")); 59 60 findTextField = new JTextField (16); 61 62 wholeWordCheckBox = new CheckBoxWithMnemonic(GlobalResourceLoader.getString(RESOURCE_PATH,"find","matchword")); 63 caseSensCheckBox = new CheckBoxWithMnemonic(GlobalResourceLoader.getString(RESOURCE_PATH,"find","matchcase")); 64 findBackCheckBox = new CheckBoxWithMnemonic(GlobalResourceLoader.getString(RESOURCE_PATH,"find","findbackwards")); 65 66 findButton = new ButtonWithMnemonic(GlobalResourceLoader.getString(RESOURCE_PATH,"find","find")); 67 findNextButton = new ButtonWithMnemonic(GlobalResourceLoader.getString(RESOURCE_PATH,"find","findnext")); 68 closeButton = new ButtonWithMnemonic(GlobalResourceLoader.getString(RESOURCE_PATH,"find","close")); 69 70 } 71 private void initComponents(){ 72 73 FormLayout layout = new FormLayout("pref, 6dlu, pref", "pref, 3dlu, pref"); 76 CellConstraints cc = new CellConstraints(); 77 78 JPanel panelMain = new JPanel (layout); 79 panelMain.setBorder(BorderFactory.createEmptyBorder(12,12,12,12)); 80 81 82 panelMain.add(createInputPanel(),cc.xy(1,1)); 83 panelMain.add(createButtonPanel(),cc.xywh(3,1,1,3)); 84 panelMain.add(createOptionPanel(),cc.xy(1,3)); 85 86 getContentPane().add(panelMain,BorderLayout.SOUTH); 87 getContentPane().add( 88 new DialogHeaderPanel(GlobalResourceLoader.getString(RESOURCE_PATH, 89 "find", "header_title"), GlobalResourceLoader 90 .getString(RESOURCE_PATH, "find", "header_description"), 91 ImageLoader.getIcon(IconKeys.SEARCH)), 92 BorderLayout.NORTH); 93 } 94 95 private JPanel createInputPanel(){ 96 FormLayout layout = new FormLayout("left:pref, 6dlu, pref", 97 "pref"); 98 CellConstraints cc = new CellConstraints(); 99 JPanel inputPanel = new JPanel (layout); 100 101 inputPanel.add(findLabel,cc.xy(1,1)); 102 findLabel.setLabelFor(findTextField); 103 inputPanel.add(findTextField,cc.xy(3,1)); 104 105 return inputPanel; 106 } 107 108 private JPanel createOptionPanel(){ 109 FormLayout layout = new FormLayout("3dlu, left:pref, 30dlu, right:pref, 3dlu", "pref, 3dlu, pref"); 113 CellConstraints cc = new CellConstraints(); 114 115 JPanel optionPanel = new JPanel (layout); 116 optionPanel.setBorder(BorderFactory.createTitledBorder(GlobalResourceLoader.getString(RESOURCE_PATH,"findreplace","options"))); 117 118 optionPanel.add(caseSensCheckBox,cc.xy(2,1)); 119 optionPanel.add(wholeWordCheckBox,cc.xywh(2,3,3,1)); 120 optionPanel.add(findBackCheckBox,cc.xy(4,1)); 121 122 return optionPanel; 123 } 124 125 private JPanel createButtonPanel(){ 126 FormLayout layout = new FormLayout("fill:pref", "pref, 3dlu, pref, 3dlu, pref"); CellConstraints cc = new CellConstraints(); 130 JPanel buttonPanel = new JPanel (layout); 131 132 133 findButton.addActionListener(this); 134 findButton.setActionCommand("FIND"); 135 136 findNextButton.addActionListener(this); 137 findNextButton.setActionCommand("FINDNEXT"); 138 this.getRootPane().setDefaultButton(findNextButton); 139 140 closeButton.addActionListener(this); 141 closeButton.setActionCommand("CLOSE"); 142 143 buttonPanel.add(findButton,cc.xy(1,1)); 144 buttonPanel.add(findNextButton,cc.xy(1,3)); 145 buttonPanel.add(closeButton,cc.xy(1,5)); 146 147 return buttonPanel; 148 } 149 150 151 public void actionPerformed(ActionEvent e) { 152 153 String action = e.getActionCommand(); 155 String source = new String (""); 156 try { 157 source = text.getText(); 158 } catch (NullPointerException e1) 159 { 160 if (!action.equals("CLOSE")) 161 return; 162 } 163 164 finder.setPattern(findTextField.getText()); 166 finder.setSource(source); 167 finder.setCaseSensitive(caseSensCheckBox.isSelected()); 168 finder.setMatchWholeWord(wholeWordCheckBox.isSelected()); 169 finder.setBackwards(findBackCheckBox.isSelected()); 170 171 if (action.equals("FIND")) { 172 173 int position = finder.find(); 175 176 if(position>=0) { 177 mark(position,position + findTextField.getText().length()); 180 } else 181 { 183 mark(0, 0); 184 JOptionPane.showMessageDialog(this, "String was not found!"); 185 } 186 187 188 } 189 else if (action.equals("FINDNEXT")){ 190 int position = finder.findnext(); 192 193 if(position>=0) { 194 mark(position,position + findTextField.getText().length()); 197 } else 198 { 200 mark(0, 0); 201 JOptionPane.showMessageDialog(this, "String was not found!"); 202 } 203 } 204 else if (action.equals("CLOSE")){ 205 this.dispose(); 206 } 207 } 208 209 private void mark(int start, int end) { 210 text.setCaretPosition(start); 211 text.moveCaretPosition(end); 212 } 213 } 214 | Popular Tags |