12e5b6d6dSopenharmony_ci/* 22e5b6d6dSopenharmony_ci ***************************************************************************** 32e5b6d6dSopenharmony_ci * Copyright (C) 2000-2007, International Business Machines Corporation and * 42e5b6d6dSopenharmony_ci * others. All Rights Reserved. * 52e5b6d6dSopenharmony_ci ***************************************************************************** 62e5b6d6dSopenharmony_ci */ 72e5b6d6dSopenharmony_cipackage com.ibm.rbm.gui; 82e5b6d6dSopenharmony_ci 92e5b6d6dSopenharmony_ciimport java.awt.*; 102e5b6d6dSopenharmony_ciimport java.awt.event.*; 112e5b6d6dSopenharmony_ciimport java.util.*; 122e5b6d6dSopenharmony_ci 132e5b6d6dSopenharmony_ciimport javax.swing.*; 142e5b6d6dSopenharmony_ciimport javax.swing.table.*; 152e5b6d6dSopenharmony_ci 162e5b6d6dSopenharmony_ciimport com.ibm.rbm.*; 172e5b6d6dSopenharmony_ci 182e5b6d6dSopenharmony_ci/** 192e5b6d6dSopenharmony_ci * The class used to display untranslated items 202e5b6d6dSopenharmony_ci */ 212e5b6d6dSopenharmony_ciclass RBSearchPanel extends JPanel { 222e5b6d6dSopenharmony_ci RBManager rbm; 232e5b6d6dSopenharmony_ci Bundle bundle; 242e5b6d6dSopenharmony_ci RBManagerGUI listener; 252e5b6d6dSopenharmony_ci 262e5b6d6dSopenharmony_ci // Components 272e5b6d6dSopenharmony_ci JLabel titleLabel = new JLabel(); 282e5b6d6dSopenharmony_ci JLabel findLabel = new JLabel(Resources.getTranslation("search_find")); 292e5b6d6dSopenharmony_ci JLabel replaceLabel = new JLabel(Resources.getTranslation("search_replace")); 302e5b6d6dSopenharmony_ci 312e5b6d6dSopenharmony_ci JTextField findField = new JTextField(); 322e5b6d6dSopenharmony_ci JTextField replaceField = new JTextField(); 332e5b6d6dSopenharmony_ci 342e5b6d6dSopenharmony_ci JCheckBox keysCheck = new JCheckBox(Resources.getTranslation("search_keys"), false); 352e5b6d6dSopenharmony_ci JCheckBox transCheck = new JCheckBox(Resources.getTranslation("search_values"), true); 362e5b6d6dSopenharmony_ci JCheckBox commentsCheck = new JCheckBox(Resources.getTranslation("search_comments"), false); 372e5b6d6dSopenharmony_ci JCheckBox caseCheck = new JCheckBox(Resources.getTranslation("search_case_sensitive"), false); 382e5b6d6dSopenharmony_ci 392e5b6d6dSopenharmony_ci JButton findButton = new JButton(Resources.getTranslation("button_search_find_all")); 402e5b6d6dSopenharmony_ci JButton replaceButton = new JButton(Resources.getTranslation("button_search_replace_all")); 412e5b6d6dSopenharmony_ci 422e5b6d6dSopenharmony_ci SearchItemsTableModel model; 432e5b6d6dSopenharmony_ci JTable table; 442e5b6d6dSopenharmony_ci JScrollPane tableScroll; 452e5b6d6dSopenharmony_ci 462e5b6d6dSopenharmony_ci public RBSearchPanel(RBManagerGUI gui) { 472e5b6d6dSopenharmony_ci super(); 482e5b6d6dSopenharmony_ci listener = gui; 492e5b6d6dSopenharmony_ci } 502e5b6d6dSopenharmony_ci 512e5b6d6dSopenharmony_ci public void setBundle(Bundle b) { 522e5b6d6dSopenharmony_ci rbm = null; 532e5b6d6dSopenharmony_ci if (bundle == null) { 542e5b6d6dSopenharmony_ci bundle = b; 552e5b6d6dSopenharmony_ci initComponents(); 562e5b6d6dSopenharmony_ci } else if (bundle != b) { 572e5b6d6dSopenharmony_ci bundle = b; 582e5b6d6dSopenharmony_ci updateComponents(); 592e5b6d6dSopenharmony_ci } 602e5b6d6dSopenharmony_ci } 612e5b6d6dSopenharmony_ci 622e5b6d6dSopenharmony_ci public void setManager(RBManager m) { 632e5b6d6dSopenharmony_ci bundle = null; 642e5b6d6dSopenharmony_ci if (rbm == null) { 652e5b6d6dSopenharmony_ci rbm = m; 662e5b6d6dSopenharmony_ci initComponents(); 672e5b6d6dSopenharmony_ci } else if (rbm != m) { 682e5b6d6dSopenharmony_ci rbm = m; 692e5b6d6dSopenharmony_ci updateComponents(); 702e5b6d6dSopenharmony_ci } 712e5b6d6dSopenharmony_ci } 722e5b6d6dSopenharmony_ci 732e5b6d6dSopenharmony_ci public void removeElements() { 742e5b6d6dSopenharmony_ci if (rbm != null || bundle != null) { 752e5b6d6dSopenharmony_ci rbm = null; 762e5b6d6dSopenharmony_ci bundle = null; 772e5b6d6dSopenharmony_ci initComponents(); 782e5b6d6dSopenharmony_ci } 792e5b6d6dSopenharmony_ci } 802e5b6d6dSopenharmony_ci 812e5b6d6dSopenharmony_ci protected void performSearch() { 822e5b6d6dSopenharmony_ci String search_term = findField.getText().trim(); 832e5b6d6dSopenharmony_ci if (search_term.length() < 1) return; 842e5b6d6dSopenharmony_ci if (bundle != null) { 852e5b6d6dSopenharmony_ci performSearch(search_term, bundle, caseCheck.isSelected()); 862e5b6d6dSopenharmony_ci } else if (rbm != null) { 872e5b6d6dSopenharmony_ci performSearch(search_term, (Bundle)rbm.getBundles().elementAt(0), caseCheck.isSelected()); 882e5b6d6dSopenharmony_ci } 892e5b6d6dSopenharmony_ci } 902e5b6d6dSopenharmony_ci 912e5b6d6dSopenharmony_ci private void performSearch(String term, Bundle bundle, boolean case_sensitive) { 922e5b6d6dSopenharmony_ci Vector ret_v = new Vector(); 932e5b6d6dSopenharmony_ci Enumeration keys = bundle.allItems.keys(); 942e5b6d6dSopenharmony_ci while (keys.hasMoreElements()) { 952e5b6d6dSopenharmony_ci String key = (String)keys.nextElement(); 962e5b6d6dSopenharmony_ci BundleItem item = (BundleItem)bundle.allItems.get(key); 972e5b6d6dSopenharmony_ci if (case_sensitive) { 982e5b6d6dSopenharmony_ci if (keysCheck.isSelected() && key.indexOf(term) >= 0) { 992e5b6d6dSopenharmony_ci ret_v.addElement(item); 1002e5b6d6dSopenharmony_ci continue; 1012e5b6d6dSopenharmony_ci } // end if - keys 1022e5b6d6dSopenharmony_ci if (transCheck.isSelected() && item.getTranslation().indexOf(term) >= 0) { 1032e5b6d6dSopenharmony_ci ret_v.addElement(item); 1042e5b6d6dSopenharmony_ci continue; 1052e5b6d6dSopenharmony_ci } // end if - translations 1062e5b6d6dSopenharmony_ci if (commentsCheck.isSelected()) { 1072e5b6d6dSopenharmony_ci if (item.getComment().indexOf(term) >= 0) { 1082e5b6d6dSopenharmony_ci ret_v.addElement(item); 1092e5b6d6dSopenharmony_ci continue; 1102e5b6d6dSopenharmony_ci } 1112e5b6d6dSopenharmony_ci Hashtable lookups = item.getLookups(); 1122e5b6d6dSopenharmony_ci Enumeration enum2 = lookups.keys(); 1132e5b6d6dSopenharmony_ci while (enum2.hasMoreElements()) { 1142e5b6d6dSopenharmony_ci String lookup_key = (String)enum2.nextElement(); 1152e5b6d6dSopenharmony_ci String lookup_value = (String)lookups.get(lookup_key); 1162e5b6d6dSopenharmony_ci if (lookup_value.indexOf(term) >= 0) { 1172e5b6d6dSopenharmony_ci ret_v.addElement(item); 1182e5b6d6dSopenharmony_ci continue; 1192e5b6d6dSopenharmony_ci } 1202e5b6d6dSopenharmony_ci } // end while 1212e5b6d6dSopenharmony_ci } // end if - comments 1222e5b6d6dSopenharmony_ci } else { 1232e5b6d6dSopenharmony_ci // Not case sensitive 1242e5b6d6dSopenharmony_ci if (keysCheck.isSelected() && key.toUpperCase().indexOf(term.toUpperCase()) >= 0) { 1252e5b6d6dSopenharmony_ci ret_v.addElement(item); 1262e5b6d6dSopenharmony_ci continue; 1272e5b6d6dSopenharmony_ci } // end if - keys 1282e5b6d6dSopenharmony_ci if (transCheck.isSelected() && item.getTranslation().toUpperCase().indexOf(term.toUpperCase()) >= 0) { 1292e5b6d6dSopenharmony_ci ret_v.addElement(item); 1302e5b6d6dSopenharmony_ci continue; 1312e5b6d6dSopenharmony_ci } // end if - translations 1322e5b6d6dSopenharmony_ci if (commentsCheck.isSelected()) { 1332e5b6d6dSopenharmony_ci if (item.getComment().toUpperCase().indexOf(term.toUpperCase()) >= 0) { 1342e5b6d6dSopenharmony_ci ret_v.addElement(item); 1352e5b6d6dSopenharmony_ci continue; 1362e5b6d6dSopenharmony_ci } 1372e5b6d6dSopenharmony_ci Hashtable lookups = item.getLookups(); 1382e5b6d6dSopenharmony_ci Enumeration enum2 = lookups.keys(); 1392e5b6d6dSopenharmony_ci while (enum2.hasMoreElements()) { 1402e5b6d6dSopenharmony_ci String lookup_key = (String)enum2.nextElement(); 1412e5b6d6dSopenharmony_ci String lookup_value = (String)lookups.get(lookup_key); 1422e5b6d6dSopenharmony_ci if (lookup_value.toUpperCase().indexOf(term.toUpperCase()) >= 0) { 1432e5b6d6dSopenharmony_ci ret_v.addElement(item); 1442e5b6d6dSopenharmony_ci continue; 1452e5b6d6dSopenharmony_ci } 1462e5b6d6dSopenharmony_ci } // end while 1472e5b6d6dSopenharmony_ci } // end if - comments 1482e5b6d6dSopenharmony_ci } 1492e5b6d6dSopenharmony_ci } // end while 1502e5b6d6dSopenharmony_ci model.setItems(ret_v); 1512e5b6d6dSopenharmony_ci model.update(); 1522e5b6d6dSopenharmony_ci } 1532e5b6d6dSopenharmony_ci 1542e5b6d6dSopenharmony_ci protected void performReplace() { 1552e5b6d6dSopenharmony_ci String search_term = findField.getText().trim(); 1562e5b6d6dSopenharmony_ci String replace_term = replaceField.getText().trim(); 1572e5b6d6dSopenharmony_ci performSearch(); 1582e5b6d6dSopenharmony_ci if (search_term.length() < 1 || replace_term.length() < 1) return; 1592e5b6d6dSopenharmony_ci if (keysCheck.isSelected()) { 1602e5b6d6dSopenharmony_ci JOptionPane.showMessageDialog(this, 1612e5b6d6dSopenharmony_ci Resources.getTranslation("error_no_key_replace"), 1622e5b6d6dSopenharmony_ci Resources.getTranslation("warning"), JOptionPane.WARNING_MESSAGE); 1632e5b6d6dSopenharmony_ci } 1642e5b6d6dSopenharmony_ci Vector items = model.getBundleItems(); 1652e5b6d6dSopenharmony_ci for (int i=0; i < items.size(); i++) { 1662e5b6d6dSopenharmony_ci BundleItem item = (BundleItem)items.elementAt(i); 1672e5b6d6dSopenharmony_ci if (transCheck.isSelected()) { 1682e5b6d6dSopenharmony_ci item.setTranslation(replace(item.getTranslation(), search_term, replace_term)); 1692e5b6d6dSopenharmony_ci } 1702e5b6d6dSopenharmony_ci if (commentsCheck.isSelected()) { 1712e5b6d6dSopenharmony_ci item.setComment(replace(item.getComment(), search_term, replace_term)); 1722e5b6d6dSopenharmony_ci } 1732e5b6d6dSopenharmony_ci } 1742e5b6d6dSopenharmony_ci model.update(); 1752e5b6d6dSopenharmony_ci } 1762e5b6d6dSopenharmony_ci 1772e5b6d6dSopenharmony_ci // Replaces all instances of match in original with replace 1782e5b6d6dSopenharmony_ci 1792e5b6d6dSopenharmony_ci private String replace(String original, String match, String replace) { 1802e5b6d6dSopenharmony_ci int current_index = -1; 1812e5b6d6dSopenharmony_ci while (original.indexOf(match,++current_index) >= 0) { 1822e5b6d6dSopenharmony_ci current_index = original.indexOf(match, current_index); 1832e5b6d6dSopenharmony_ci original = original.substring(0,current_index) + replace + 1842e5b6d6dSopenharmony_ci original.substring(current_index+match.length(), original.length()); 1852e5b6d6dSopenharmony_ci } 1862e5b6d6dSopenharmony_ci return original; 1872e5b6d6dSopenharmony_ci } 1882e5b6d6dSopenharmony_ci 1892e5b6d6dSopenharmony_ci public void initComponents() { 1902e5b6d6dSopenharmony_ci // Initialize components 1912e5b6d6dSopenharmony_ci if (bundle != null) { 1922e5b6d6dSopenharmony_ci titleLabel.setText(bundle.name); 1932e5b6d6dSopenharmony_ci } 1942e5b6d6dSopenharmony_ci else if (rbm != null) { 1952e5b6d6dSopenharmony_ci titleLabel.setText(rbm.getBaseClass() + " - " + Resources.getTranslation("search")); 1962e5b6d6dSopenharmony_ci } 1972e5b6d6dSopenharmony_ci model = new SearchItemsTableModel(new Vector()); 1982e5b6d6dSopenharmony_ci 1992e5b6d6dSopenharmony_ci titleLabel.setFont(new Font("SansSerif",Font.PLAIN,18)); 2002e5b6d6dSopenharmony_ci 2012e5b6d6dSopenharmony_ci removeAll(); 2022e5b6d6dSopenharmony_ci setLayout(new BorderLayout()); 2032e5b6d6dSopenharmony_ci table = new JTable(model); 2042e5b6d6dSopenharmony_ci tableScroll = new JScrollPane(table); 2052e5b6d6dSopenharmony_ci 2062e5b6d6dSopenharmony_ci table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); 2072e5b6d6dSopenharmony_ci table.addMouseListener(listener); 2082e5b6d6dSopenharmony_ci 2092e5b6d6dSopenharmony_ci Dimension dim = new Dimension(75,15); 2102e5b6d6dSopenharmony_ci 2112e5b6d6dSopenharmony_ci findField.setColumns(20); 2122e5b6d6dSopenharmony_ci replaceField.setColumns(20); 2132e5b6d6dSopenharmony_ci findLabel.setPreferredSize(dim); 2142e5b6d6dSopenharmony_ci replaceLabel.setPreferredSize(dim); 2152e5b6d6dSopenharmony_ci 2162e5b6d6dSopenharmony_ci JPanel innerPanel = new JPanel(new BorderLayout()); 2172e5b6d6dSopenharmony_ci JPanel southPanel = new JPanel(); 2182e5b6d6dSopenharmony_ci JPanel westPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT)); 2192e5b6d6dSopenharmony_ci JPanel westPanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT)); 2202e5b6d6dSopenharmony_ci Box rightBox = new Box(BoxLayout.Y_AXIS); 2212e5b6d6dSopenharmony_ci Box leftBox = new Box(BoxLayout.Y_AXIS); 2222e5b6d6dSopenharmony_ci 2232e5b6d6dSopenharmony_ci // Add action listeners 2242e5b6d6dSopenharmony_ci findButton.addActionListener(new ActionListener(){ 2252e5b6d6dSopenharmony_ci public void actionPerformed(ActionEvent ev) { 2262e5b6d6dSopenharmony_ci performSearch(); 2272e5b6d6dSopenharmony_ci } 2282e5b6d6dSopenharmony_ci }); 2292e5b6d6dSopenharmony_ci 2302e5b6d6dSopenharmony_ci replaceButton.addActionListener(new ActionListener(){ 2312e5b6d6dSopenharmony_ci public void actionPerformed(ActionEvent ev) { 2322e5b6d6dSopenharmony_ci performReplace(); 2332e5b6d6dSopenharmony_ci } 2342e5b6d6dSopenharmony_ci }); 2352e5b6d6dSopenharmony_ci 2362e5b6d6dSopenharmony_ci findButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_search_find_all_trigger"))); 2372e5b6d6dSopenharmony_ci replaceButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_search_replace_all_trigger"))); 2382e5b6d6dSopenharmony_ci 2392e5b6d6dSopenharmony_ci // Place components 2402e5b6d6dSopenharmony_ci westPanel1.add(findLabel); 2412e5b6d6dSopenharmony_ci westPanel1.add(Box.createHorizontalStrut(5)); 2422e5b6d6dSopenharmony_ci westPanel1.add(findField); 2432e5b6d6dSopenharmony_ci 2442e5b6d6dSopenharmony_ci westPanel2.add(replaceLabel); 2452e5b6d6dSopenharmony_ci westPanel2.add(Box.createHorizontalStrut(5)); 2462e5b6d6dSopenharmony_ci westPanel2.add(replaceField); 2472e5b6d6dSopenharmony_ci 2482e5b6d6dSopenharmony_ci leftBox.add(Box.createVerticalGlue()); 2492e5b6d6dSopenharmony_ci leftBox.add(westPanel1); 2502e5b6d6dSopenharmony_ci leftBox.add(westPanel2); 2512e5b6d6dSopenharmony_ci //leftBox.add(caseCheck); 2522e5b6d6dSopenharmony_ci 2532e5b6d6dSopenharmony_ci rightBox.add(keysCheck); 2542e5b6d6dSopenharmony_ci rightBox.add(transCheck); 2552e5b6d6dSopenharmony_ci rightBox.add(commentsCheck); 2562e5b6d6dSopenharmony_ci 2572e5b6d6dSopenharmony_ci southPanel.add(findButton); 2582e5b6d6dSopenharmony_ci southPanel.add(Box.createHorizontalStrut(5)); 2592e5b6d6dSopenharmony_ci southPanel.add(replaceButton); 2602e5b6d6dSopenharmony_ci southPanel.add(Box.createHorizontalStrut(10)); 2612e5b6d6dSopenharmony_ci southPanel.add(caseCheck); 2622e5b6d6dSopenharmony_ci 2632e5b6d6dSopenharmony_ci innerPanel.add(titleLabel, BorderLayout.NORTH); 2642e5b6d6dSopenharmony_ci innerPanel.add(leftBox, BorderLayout.CENTER); 2652e5b6d6dSopenharmony_ci innerPanel.add(rightBox, BorderLayout.EAST); 2662e5b6d6dSopenharmony_ci innerPanel.add(southPanel, BorderLayout.SOUTH); 2672e5b6d6dSopenharmony_ci 2682e5b6d6dSopenharmony_ci add(innerPanel, BorderLayout.NORTH); 2692e5b6d6dSopenharmony_ci add(tableScroll, BorderLayout.CENTER); 2702e5b6d6dSopenharmony_ci 2712e5b6d6dSopenharmony_ci if (rbm == null && bundle == null) { 2722e5b6d6dSopenharmony_ci removeAll(); 2732e5b6d6dSopenharmony_ci } 2742e5b6d6dSopenharmony_ci } 2752e5b6d6dSopenharmony_ci 2762e5b6d6dSopenharmony_ci public void updateComponents() { 2772e5b6d6dSopenharmony_ci 2782e5b6d6dSopenharmony_ci } 2792e5b6d6dSopenharmony_ci} 2802e5b6d6dSopenharmony_ci 2812e5b6d6dSopenharmony_ci// The table model for searched Items 2822e5b6d6dSopenharmony_ci 2832e5b6d6dSopenharmony_ciclass SearchItemsTableModel extends AbstractTableModel { 2842e5b6d6dSopenharmony_ci Vector items; 2852e5b6d6dSopenharmony_ci 2862e5b6d6dSopenharmony_ci public SearchItemsTableModel(Vector items) { 2872e5b6d6dSopenharmony_ci this.items = items; 2882e5b6d6dSopenharmony_ci } 2892e5b6d6dSopenharmony_ci 2902e5b6d6dSopenharmony_ci public void setItems(Vector items) { 2912e5b6d6dSopenharmony_ci this.items = items; 2922e5b6d6dSopenharmony_ci } 2932e5b6d6dSopenharmony_ci 2942e5b6d6dSopenharmony_ci public int getColumnCount() { return 3; } 2952e5b6d6dSopenharmony_ci 2962e5b6d6dSopenharmony_ci public int getRowCount() { 2972e5b6d6dSopenharmony_ci return items.size(); 2982e5b6d6dSopenharmony_ci } 2992e5b6d6dSopenharmony_ci 3002e5b6d6dSopenharmony_ci public Object getValueAt(int row, int col) { 3012e5b6d6dSopenharmony_ci BundleItem item = (BundleItem)items.elementAt(row); 3022e5b6d6dSopenharmony_ci String retStr = null; 3032e5b6d6dSopenharmony_ci 3042e5b6d6dSopenharmony_ci switch(col) { 3052e5b6d6dSopenharmony_ci case 0: 3062e5b6d6dSopenharmony_ci retStr = item.getKey(); 3072e5b6d6dSopenharmony_ci break; 3082e5b6d6dSopenharmony_ci case 1: 3092e5b6d6dSopenharmony_ci retStr = item.getTranslation(); 3102e5b6d6dSopenharmony_ci break; 3112e5b6d6dSopenharmony_ci case 2: 3122e5b6d6dSopenharmony_ci retStr = (item.getParentGroup() == null ? "" : item.getParentGroup().getName()); 3132e5b6d6dSopenharmony_ci break; 3142e5b6d6dSopenharmony_ci default: 3152e5b6d6dSopenharmony_ci retStr = Resources.getTranslation("table_cell_error"); 3162e5b6d6dSopenharmony_ci } 3172e5b6d6dSopenharmony_ci 3182e5b6d6dSopenharmony_ci return retStr; 3192e5b6d6dSopenharmony_ci } 3202e5b6d6dSopenharmony_ci 3212e5b6d6dSopenharmony_ci public String getColumnName(int col) { 3222e5b6d6dSopenharmony_ci if (col == 0) return Resources.getTranslation("languageuntrans_column_key"); 3232e5b6d6dSopenharmony_ci else if (col == 1) return Resources.getTranslation("languageuntrans_column_translation"); 3242e5b6d6dSopenharmony_ci else if (col == 2) return Resources.getTranslation("languageuntrans_column_group"); 3252e5b6d6dSopenharmony_ci else return Resources.getTranslation("table_column_error"); 3262e5b6d6dSopenharmony_ci } 3272e5b6d6dSopenharmony_ci 3282e5b6d6dSopenharmony_ci public BundleItem getBundleItem(int row) { 3292e5b6d6dSopenharmony_ci return (BundleItem)items.elementAt(row); 3302e5b6d6dSopenharmony_ci } 3312e5b6d6dSopenharmony_ci 3322e5b6d6dSopenharmony_ci public Vector getBundleItems() { 3332e5b6d6dSopenharmony_ci return items; 3342e5b6d6dSopenharmony_ci } 3352e5b6d6dSopenharmony_ci 3362e5b6d6dSopenharmony_ci public void update() { 3372e5b6d6dSopenharmony_ci fireTableDataChanged(); 3382e5b6d6dSopenharmony_ci } 3392e5b6d6dSopenharmony_ci} 340