• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *****************************************************************************
3  * Copyright (C) 2000-2007, International Business Machines Corporation and  *
4  * others. All Rights Reserved.                                              *
5  *****************************************************************************
6  */
7 package com.ibm.rbm.gui;
8 
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.util.*;
12 
13 import javax.swing.*;
14 import javax.swing.table.*;
15 
16 import com.ibm.rbm.*;
17 
18 /**
19  * The class used to display untranslated items
20  */
21 class RBSearchPanel extends JPanel {
22 	RBManager rbm;
23 	Bundle bundle;
24 	RBManagerGUI listener;
25 
26 	// Components
27 	JLabel titleLabel       = new JLabel();
28 	JLabel findLabel        = new JLabel(Resources.getTranslation("search_find"));
29 	JLabel replaceLabel     = new JLabel(Resources.getTranslation("search_replace"));
30 
31 	JTextField findField    = new JTextField();
32 	JTextField replaceField = new JTextField();
33 
34 	JCheckBox keysCheck     = new JCheckBox(Resources.getTranslation("search_keys"), false);
35 	JCheckBox transCheck    = new JCheckBox(Resources.getTranslation("search_values"), true);
36 	JCheckBox commentsCheck = new JCheckBox(Resources.getTranslation("search_comments"), false);
37 	JCheckBox caseCheck     = new JCheckBox(Resources.getTranslation("search_case_sensitive"), false);
38 
39 	JButton findButton      = new JButton(Resources.getTranslation("button_search_find_all"));
40 	JButton replaceButton   = new JButton(Resources.getTranslation("button_search_replace_all"));
41 
42 	SearchItemsTableModel     model;
43 	JTable                    table;
44 	JScrollPane               tableScroll;
45 
RBSearchPanel(RBManagerGUI gui)46 	public RBSearchPanel(RBManagerGUI gui) {
47 		super();
48 		listener = gui;
49 	}
50 
setBundle(Bundle b)51 	public void setBundle(Bundle b) {
52 		rbm = null;
53 		if (bundle == null) {
54 			bundle = b;
55 			initComponents();
56 		} else if (bundle != b) {
57 			bundle = b;
58 			updateComponents();
59 		}
60 	}
61 
setManager(RBManager m)62 	public void setManager(RBManager m) {
63 		bundle = null;
64 		if (rbm == null) {
65 			rbm = m;
66 			initComponents();
67 		} else if (rbm != m) {
68 			rbm = m;
69 			updateComponents();
70 		}
71 	}
72 
removeElements()73 	public void removeElements() {
74 		if (rbm != null || bundle != null) {
75 			rbm = null;
76 			bundle = null;
77 			initComponents();
78 		}
79 	}
80 
performSearch()81 	protected void performSearch() {
82 		String search_term = findField.getText().trim();
83 		if (search_term.length() < 1) return;
84 		if (bundle != null) {
85 			performSearch(search_term, bundle, caseCheck.isSelected());
86 		} else if (rbm != null) {
87 			performSearch(search_term, (Bundle)rbm.getBundles().elementAt(0), caseCheck.isSelected());
88 		}
89 	}
90 
performSearch(String term, Bundle bundle, boolean case_sensitive)91 	private void performSearch(String term, Bundle bundle, boolean case_sensitive) {
92 		Vector ret_v = new Vector();
93 		Enumeration keys = bundle.allItems.keys();
94 		while (keys.hasMoreElements()) {
95 			String key = (String)keys.nextElement();
96 			BundleItem item = (BundleItem)bundle.allItems.get(key);
97 			if (case_sensitive) {
98 				if (keysCheck.isSelected() && key.indexOf(term) >= 0) {
99 					ret_v.addElement(item);
100 					continue;
101 				} // end if - keys
102 				if (transCheck.isSelected() && item.getTranslation().indexOf(term) >= 0) {
103 					ret_v.addElement(item);
104 					continue;
105 				} // end if - translations
106 				if (commentsCheck.isSelected()) {
107 					if (item.getComment().indexOf(term) >= 0) {
108 						ret_v.addElement(item);
109 						continue;
110 					}
111 					Hashtable lookups = item.getLookups();
112 					Enumeration enum2 = lookups.keys();
113 					while (enum2.hasMoreElements()) {
114 						String lookup_key = (String)enum2.nextElement();
115 						String lookup_value = (String)lookups.get(lookup_key);
116 						if (lookup_value.indexOf(term) >= 0) {
117 							ret_v.addElement(item);
118 							continue;
119 						}
120 					} // end while
121 				} // end if - comments
122 			} else {
123 				// Not case sensitive
124 				if (keysCheck.isSelected() && key.toUpperCase().indexOf(term.toUpperCase()) >= 0) {
125 					ret_v.addElement(item);
126 					continue;
127 				} // end if - keys
128 				if (transCheck.isSelected() && item.getTranslation().toUpperCase().indexOf(term.toUpperCase()) >= 0) {
129 					ret_v.addElement(item);
130 					continue;
131 				} // end if - translations
132 				if (commentsCheck.isSelected()) {
133 					if (item.getComment().toUpperCase().indexOf(term.toUpperCase()) >= 0) {
134 						ret_v.addElement(item);
135 						continue;
136 					}
137 					Hashtable lookups = item.getLookups();
138 					Enumeration enum2 = lookups.keys();
139 					while (enum2.hasMoreElements()) {
140 						String lookup_key = (String)enum2.nextElement();
141 						String lookup_value = (String)lookups.get(lookup_key);
142 						if (lookup_value.toUpperCase().indexOf(term.toUpperCase()) >= 0) {
143 							ret_v.addElement(item);
144 							continue;
145 						}
146 					} // end while
147 				} // end if - comments
148 			}
149 		} // end while
150 		model.setItems(ret_v);
151 		model.update();
152 	}
153 
performReplace()154 	protected void performReplace() {
155 		String search_term = findField.getText().trim();
156 		String replace_term = replaceField.getText().trim();
157 		performSearch();
158 		if (search_term.length() < 1 || replace_term.length() < 1) return;
159 		if (keysCheck.isSelected()) {
160 			JOptionPane.showMessageDialog(this,
161 				Resources.getTranslation("error_no_key_replace"),
162 				Resources.getTranslation("warning"), JOptionPane.WARNING_MESSAGE);
163 		}
164 		Vector items = model.getBundleItems();
165 		for (int i=0; i < items.size(); i++) {
166 			BundleItem item = (BundleItem)items.elementAt(i);
167 			if (transCheck.isSelected()) {
168 				item.setTranslation(replace(item.getTranslation(), search_term, replace_term));
169 			}
170 			if (commentsCheck.isSelected()) {
171 				item.setComment(replace(item.getComment(), search_term, replace_term));
172 			}
173 		}
174 		model.update();
175 	}
176 
177 	// Replaces all instances of match in original with replace
178 
replace(String original, String match, String replace)179 	private String replace(String original, String match, String replace) {
180 		int current_index = -1;
181 		while (original.indexOf(match,++current_index) >= 0) {
182 			current_index = original.indexOf(match, current_index);
183 			original = original.substring(0,current_index) + replace +
184 					   original.substring(current_index+match.length(), original.length());
185 		}
186 		return original;
187 	}
188 
initComponents()189 	public void initComponents() {
190 		// Initialize components
191 		if (bundle != null) {
192 			titleLabel.setText(bundle.name);
193 		}
194 		else if (rbm != null) {
195 			titleLabel.setText(rbm.getBaseClass() + " - " + Resources.getTranslation("search"));
196 		}
197 		model = new SearchItemsTableModel(new Vector());
198 
199 		titleLabel.setFont(new Font("SansSerif",Font.PLAIN,18));
200 
201 		removeAll();
202 		setLayout(new BorderLayout());
203 		table = new JTable(model);
204 		tableScroll = new JScrollPane(table);
205 
206 		table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
207 		table.addMouseListener(listener);
208 
209 		Dimension dim = new Dimension(75,15);
210 
211 		findField.setColumns(20);
212 		replaceField.setColumns(20);
213 		findLabel.setPreferredSize(dim);
214 		replaceLabel.setPreferredSize(dim);
215 
216 		JPanel innerPanel = new JPanel(new BorderLayout());
217 		JPanel southPanel = new JPanel();
218 		JPanel westPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
219 		JPanel westPanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
220 		Box rightBox = new Box(BoxLayout.Y_AXIS);
221 		Box leftBox = new Box(BoxLayout.Y_AXIS);
222 
223 		// Add action listeners
224 		findButton.addActionListener(new ActionListener(){
225 			public void actionPerformed(ActionEvent ev) {
226 				performSearch();
227 			}
228 		});
229 
230 		replaceButton.addActionListener(new ActionListener(){
231 			public void actionPerformed(ActionEvent ev) {
232 				performReplace();
233 			}
234 		});
235 
236 		findButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_search_find_all_trigger")));
237 		replaceButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_search_replace_all_trigger")));
238 
239 		// Place components
240 		westPanel1.add(findLabel);
241 		westPanel1.add(Box.createHorizontalStrut(5));
242 		westPanel1.add(findField);
243 
244 		westPanel2.add(replaceLabel);
245 		westPanel2.add(Box.createHorizontalStrut(5));
246 		westPanel2.add(replaceField);
247 
248 		leftBox.add(Box.createVerticalGlue());
249 		leftBox.add(westPanel1);
250 		leftBox.add(westPanel2);
251 		//leftBox.add(caseCheck);
252 
253 		rightBox.add(keysCheck);
254 		rightBox.add(transCheck);
255 		rightBox.add(commentsCheck);
256 
257 		southPanel.add(findButton);
258 		southPanel.add(Box.createHorizontalStrut(5));
259 		southPanel.add(replaceButton);
260 		southPanel.add(Box.createHorizontalStrut(10));
261 		southPanel.add(caseCheck);
262 
263 		innerPanel.add(titleLabel, BorderLayout.NORTH);
264 		innerPanel.add(leftBox, BorderLayout.CENTER);
265 		innerPanel.add(rightBox, BorderLayout.EAST);
266 		innerPanel.add(southPanel, BorderLayout.SOUTH);
267 
268 		add(innerPanel, BorderLayout.NORTH);
269 		add(tableScroll, BorderLayout.CENTER);
270 
271 		if (rbm == null && bundle == null) {
272 			removeAll();
273 		}
274 	}
275 
updateComponents()276 	public void updateComponents() {
277 
278 	}
279 }
280 
281 // The table model for searched Items
282 
283 class SearchItemsTableModel extends AbstractTableModel {
284 	Vector items;
285 
SearchItemsTableModel(Vector items)286 	public SearchItemsTableModel(Vector items) {
287 		this.items = items;
288 	}
289 
setItems(Vector items)290 	public void setItems(Vector items) {
291 		this.items = items;
292 	}
293 
getColumnCount()294 	public int getColumnCount() { return 3; }
295 
getRowCount()296 	public int getRowCount() {
297 		return items.size();
298 	}
299 
getValueAt(int row, int col)300 	public Object getValueAt(int row, int col) {
301 		BundleItem item = (BundleItem)items.elementAt(row);
302 		String retStr = null;
303 
304 		switch(col) {
305 		case 0:
306 			retStr = item.getKey();
307 			break;
308 		case 1:
309 			retStr = item.getTranslation();
310 			break;
311 		case 2:
312 			retStr = (item.getParentGroup() == null ? "" : item.getParentGroup().getName());
313 			break;
314 		default:
315 			retStr = Resources.getTranslation("table_cell_error");
316 		}
317 
318 		return retStr;
319 	}
320 
getColumnName(int col)321 	public String getColumnName(int col) {
322 		if (col == 0) return Resources.getTranslation("languageuntrans_column_key");
323 		else if (col == 1) return Resources.getTranslation("languageuntrans_column_translation");
324 		else if (col == 2) return Resources.getTranslation("languageuntrans_column_group");
325 		else return Resources.getTranslation("table_column_error");
326 	}
327 
getBundleItem(int row)328 	public BundleItem getBundleItem(int row) {
329 		return (BundleItem)items.elementAt(row);
330 	}
331 
getBundleItems()332 	public Vector getBundleItems() {
333 		return items;
334 	}
335 
update()336 	public void update() {
337 		fireTableDataChanged();
338 	}
339 }
340