001: /*
002: *****************************************************************************
003: * Copyright (C) 2000-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *****************************************************************************
006: */
007: package com.ibm.rbm.gui;
008:
009: import java.awt.*;
010: import java.awt.event.*;
011: import java.util.*;
012:
013: import javax.swing.*;
014: import javax.swing.table.*;
015:
016: import com.ibm.rbm.*;
017:
018: /**
019: * The class used to display untranslated items
020: */
021: class RBSearchPanel extends JPanel {
022: RBManager rbm;
023: Bundle bundle;
024: RBManagerGUI listener;
025:
026: // Components
027: JLabel titleLabel = new JLabel();
028: JLabel findLabel = new JLabel(Resources
029: .getTranslation("search_find"));
030: JLabel replaceLabel = new JLabel(Resources
031: .getTranslation("search_replace"));
032:
033: JTextField findField = new JTextField();
034: JTextField replaceField = new JTextField();
035:
036: JCheckBox keysCheck = new JCheckBox(Resources
037: .getTranslation("search_keys"), false);
038: JCheckBox transCheck = new JCheckBox(Resources
039: .getTranslation("search_values"), true);
040: JCheckBox commentsCheck = new JCheckBox(Resources
041: .getTranslation("search_comments"), false);
042: JCheckBox caseCheck = new JCheckBox(Resources
043: .getTranslation("search_case_sensitive"), false);
044:
045: JButton findButton = new JButton(Resources
046: .getTranslation("button_search_find_all"));
047: JButton replaceButton = new JButton(Resources
048: .getTranslation("button_search_replace_all"));
049:
050: SearchItemsTableModel model;
051: JTable table;
052: JScrollPane tableScroll;
053:
054: public RBSearchPanel(RBManagerGUI gui) {
055: super ();
056: listener = gui;
057: }
058:
059: public void setBundle(Bundle b) {
060: rbm = null;
061: if (bundle == null) {
062: bundle = b;
063: initComponents();
064: } else if (bundle != b) {
065: bundle = b;
066: updateComponents();
067: }
068: }
069:
070: public void setManager(RBManager m) {
071: bundle = null;
072: if (rbm == null) {
073: rbm = m;
074: initComponents();
075: } else if (rbm != m) {
076: rbm = m;
077: updateComponents();
078: }
079: }
080:
081: public void removeElements() {
082: if (rbm != null || bundle != null) {
083: rbm = null;
084: bundle = null;
085: initComponents();
086: }
087: }
088:
089: protected void performSearch() {
090: String search_term = findField.getText().trim();
091: if (search_term.length() < 1)
092: return;
093: if (bundle != null) {
094: performSearch(search_term, bundle, caseCheck.isSelected());
095: } else if (rbm != null) {
096: performSearch(search_term, (Bundle) rbm.getBundles()
097: .elementAt(0), caseCheck.isSelected());
098: }
099: }
100:
101: private void performSearch(String term, Bundle bundle, boolean case_sensitive) {
102: Vector ret_v = new Vector();
103: Enumeration enum = bundle.allItems.keys();
104: while (enum.hasMoreElements()) {
105: String key = (String)enum.nextElement();
106: BundleItem item = (BundleItem)bundle.allItems.get(key);
107: if (case_sensitive) {
108: if (keysCheck.isSelected() && key.indexOf(term) >= 0) {
109: ret_v.addElement(item);
110: continue;
111: } // end if - keys
112: if (transCheck.isSelected() && item.getTranslation().indexOf(term) >= 0) {
113: ret_v.addElement(item);
114: continue;
115: } // end if - translations
116: if (commentsCheck.isSelected()) {
117: if (item.getComment().indexOf(term) >= 0) {
118: ret_v.addElement(item);
119: continue;
120: }
121: Hashtable lookups = item.getLookups();
122: Enumeration enum2 = lookups.keys();
123: while (enum2.hasMoreElements()) {
124: String lookup_key = (String)enum2.nextElement();
125: String lookup_value = (String)lookups.get(lookup_key);
126: if (lookup_value.indexOf(term) >= 0) {
127: ret_v.addElement(item);
128: continue;
129: }
130: } // end while
131: } // end if - comments
132: } else {
133: // Not case sensitive
134: if (keysCheck.isSelected() && key.toUpperCase().indexOf(term.toUpperCase()) >= 0) {
135: ret_v.addElement(item);
136: continue;
137: } // end if - keys
138: if (transCheck.isSelected() && item.getTranslation().toUpperCase().indexOf(term.toUpperCase()) >= 0) {
139: ret_v.addElement(item);
140: continue;
141: } // end if - translations
142: if (commentsCheck.isSelected()) {
143: if (item.getComment().toUpperCase().indexOf(term.toUpperCase()) >= 0) {
144: ret_v.addElement(item);
145: continue;
146: }
147: Hashtable lookups = item.getLookups();
148: Enumeration enum2 = lookups.keys();
149: while (enum2.hasMoreElements()) {
150: String lookup_key = (String)enum2.nextElement();
151: String lookup_value = (String)lookups.get(lookup_key);
152: if (lookup_value.toUpperCase().indexOf(term.toUpperCase()) >= 0) {
153: ret_v.addElement(item);
154: continue;
155: }
156: } // end while
157: } // end if - comments
158: }
159: } // end while
160: model.setItems(ret_v);
161: model.update();
162: }
163:
164: protected void performReplace() {
165: String search_term = findField.getText().trim();
166: String replace_term = replaceField.getText().trim();
167: performSearch();
168: if (search_term.length() < 1 || replace_term.length() < 1)
169: return;
170: if (keysCheck.isSelected()) {
171: JOptionPane.showMessageDialog(this , Resources
172: .getTranslation("error_no_key_replace"), Resources
173: .getTranslation("warning"),
174: JOptionPane.WARNING_MESSAGE);
175: }
176: Vector items = model.getBundleItems();
177: for (int i = 0; i < items.size(); i++) {
178: BundleItem item = (BundleItem) items.elementAt(i);
179: if (transCheck.isSelected()) {
180: item.setTranslation(replace(item.getTranslation(),
181: search_term, replace_term));
182: }
183: if (commentsCheck.isSelected()) {
184: item.setComment(replace(item.getComment(), search_term,
185: replace_term));
186: }
187: }
188: model.update();
189: }
190:
191: // Replaces all instances of match in original with replace
192:
193: private String replace(String original, String match, String replace) {
194: int current_index = -1;
195: while (original.indexOf(match, ++current_index) >= 0) {
196: current_index = original.indexOf(match, current_index);
197: original = original.substring(0, current_index)
198: + replace
199: + original.substring(
200: current_index + match.length(), original
201: .length());
202: }
203: return original;
204: }
205:
206: public void initComponents() {
207: // Initialize components
208: if (bundle != null) {
209: titleLabel.setText(bundle.name);
210: } else if (rbm != null) {
211: titleLabel.setText(rbm.getBaseClass() + " - "
212: + Resources.getTranslation("search"));
213: }
214: model = new SearchItemsTableModel(new Vector());
215:
216: titleLabel.setFont(new Font("SansSerif", Font.PLAIN, 18));
217:
218: removeAll();
219: setLayout(new BorderLayout());
220: table = new JTable(model);
221: tableScroll = new JScrollPane(table);
222:
223: table
224: .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
225: table.addMouseListener(listener);
226:
227: Dimension dim = new Dimension(75, 15);
228:
229: findField.setColumns(20);
230: replaceField.setColumns(20);
231: findLabel.setPreferredSize(dim);
232: replaceLabel.setPreferredSize(dim);
233:
234: JPanel innerPanel = new JPanel(new BorderLayout());
235: JPanel southPanel = new JPanel();
236: JPanel westPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
237: JPanel westPanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
238: Box rightBox = new Box(BoxLayout.Y_AXIS);
239: Box leftBox = new Box(BoxLayout.Y_AXIS);
240:
241: // Add action listeners
242: findButton.addActionListener(new ActionListener() {
243: public void actionPerformed(ActionEvent ev) {
244: performSearch();
245: }
246: });
247:
248: replaceButton.addActionListener(new ActionListener() {
249: public void actionPerformed(ActionEvent ev) {
250: performReplace();
251: }
252: });
253:
254: findButton
255: .setMnemonic(RBManagerMenuBar
256: .getKeyEventKey(Resources
257: .getTranslation("button_search_find_all_trigger")));
258: replaceButton
259: .setMnemonic(RBManagerMenuBar
260: .getKeyEventKey(Resources
261: .getTranslation("button_search_replace_all_trigger")));
262:
263: // Place components
264: westPanel1.add(findLabel);
265: westPanel1.add(Box.createHorizontalStrut(5));
266: westPanel1.add(findField);
267:
268: westPanel2.add(replaceLabel);
269: westPanel2.add(Box.createHorizontalStrut(5));
270: westPanel2.add(replaceField);
271:
272: leftBox.add(Box.createVerticalGlue());
273: leftBox.add(westPanel1);
274: leftBox.add(westPanel2);
275: //leftBox.add(caseCheck);
276:
277: rightBox.add(keysCheck);
278: rightBox.add(transCheck);
279: rightBox.add(commentsCheck);
280:
281: southPanel.add(findButton);
282: southPanel.add(Box.createHorizontalStrut(5));
283: southPanel.add(replaceButton);
284: southPanel.add(Box.createHorizontalStrut(10));
285: southPanel.add(caseCheck);
286:
287: innerPanel.add(titleLabel, BorderLayout.NORTH);
288: innerPanel.add(leftBox, BorderLayout.CENTER);
289: innerPanel.add(rightBox, BorderLayout.EAST);
290: innerPanel.add(southPanel, BorderLayout.SOUTH);
291:
292: add(innerPanel, BorderLayout.NORTH);
293: add(tableScroll, BorderLayout.CENTER);
294:
295: if (rbm == null && bundle == null) {
296: removeAll();
297: }
298: }
299:
300: public void updateComponents() {
301:
302: }
303: }
304:
305: // The table model for searched Items
306:
307: class SearchItemsTableModel extends AbstractTableModel {
308: Vector items;
309:
310: public SearchItemsTableModel(Vector items) {
311: this .items = items;
312: }
313:
314: public void setItems(Vector items) {
315: this .items = items;
316: }
317:
318: public int getColumnCount() {
319: return 3;
320: }
321:
322: public int getRowCount() {
323: return items.size();
324: }
325:
326: public Object getValueAt(int row, int col) {
327: BundleItem item = (BundleItem) items.elementAt(row);
328: String retStr = null;
329:
330: switch (col) {
331: case 0:
332: retStr = item.getKey();
333: break;
334: case 1:
335: retStr = item.getTranslation();
336: break;
337: case 2:
338: retStr = (item.getParentGroup() == null ? "" : item
339: .getParentGroup().getName());
340: break;
341: default:
342: retStr = Resources.getTranslation("table_cell_error");
343: }
344:
345: return retStr;
346: }
347:
348: public String getColumnName(int col) {
349: if (col == 0)
350: return Resources
351: .getTranslation("languageuntrans_column_key");
352: else if (col == 1)
353: return Resources
354: .getTranslation("languageuntrans_column_translation");
355: else if (col == 2)
356: return Resources
357: .getTranslation("languageuntrans_column_group");
358: else
359: return Resources.getTranslation("table_column_error");
360: }
361:
362: public BundleItem getBundleItem(int row) {
363: return (BundleItem) items.elementAt(row);
364: }
365:
366: public Vector getBundleItems() {
367: return items;
368: }
369:
370: public void update() {
371: fireTableDataChanged();
372: }
373: }
|