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.text.DateFormat; 122e5b6d6dSopenharmony_ciimport java.util.*; 132e5b6d6dSopenharmony_ci 142e5b6d6dSopenharmony_ciimport javax.swing.*; 152e5b6d6dSopenharmony_ci 162e5b6d6dSopenharmony_ciimport com.ibm.rbm.*; 172e5b6d6dSopenharmony_ci 182e5b6d6dSopenharmony_ci//A dialog which displays the properties of a Bundle Item in an editable way 192e5b6d6dSopenharmony_ci 202e5b6d6dSopenharmony_ciclass BundleItemDialog extends JDialog implements ActionListener { 212e5b6d6dSopenharmony_ci RBManager rbm; 222e5b6d6dSopenharmony_ci BundleItem item; 232e5b6d6dSopenharmony_ci String user; 242e5b6d6dSopenharmony_ci boolean firstInit = true; 252e5b6d6dSopenharmony_ci 262e5b6d6dSopenharmony_ci // Helper data 272e5b6d6dSopenharmony_ci int left_col_width = 125; 282e5b6d6dSopenharmony_ci int right_col_width = 375; 292e5b6d6dSopenharmony_ci int row_height = 25; 302e5b6d6dSopenharmony_ci Dimension leftDim = new Dimension(left_col_width, row_height); 312e5b6d6dSopenharmony_ci Dimension rightDim = new Dimension(right_col_width, row_height); 322e5b6d6dSopenharmony_ci 332e5b6d6dSopenharmony_ci // Components 342e5b6d6dSopenharmony_ci Box mainBox = new Box(BoxLayout.Y_AXIS); 352e5b6d6dSopenharmony_ci Box box0 = new Box(BoxLayout.X_AXIS); 362e5b6d6dSopenharmony_ci Box box1 = new Box(BoxLayout.X_AXIS); 372e5b6d6dSopenharmony_ci Box box2 = new Box(BoxLayout.X_AXIS); 382e5b6d6dSopenharmony_ci Box box3 = new Box(BoxLayout.X_AXIS); 392e5b6d6dSopenharmony_ci Box box4 = new Box(BoxLayout.X_AXIS); 402e5b6d6dSopenharmony_ci Box box5 = new Box(BoxLayout.X_AXIS); 412e5b6d6dSopenharmony_ci Box box6 = new Box(BoxLayout.X_AXIS); 422e5b6d6dSopenharmony_ci Box box7 = new Box(BoxLayout.X_AXIS); 432e5b6d6dSopenharmony_ci Box box8 = new Box(BoxLayout.X_AXIS); 442e5b6d6dSopenharmony_ci 452e5b6d6dSopenharmony_ci JLabel groupLabel = new JLabel(Resources.getTranslation("dialog_group")); 462e5b6d6dSopenharmony_ci JLabel keyLabel = new JLabel(Resources.getTranslation("dialog_key")); 472e5b6d6dSopenharmony_ci JLabel defTransLabel = new JLabel(Resources.getTranslation("dialog_default_translation")); 482e5b6d6dSopenharmony_ci JLabel transLabel = new JLabel(Resources.getTranslation("dialog_translation")); 492e5b6d6dSopenharmony_ci JLabel commentLabel = new JLabel(Resources.getTranslation("dialog_comment")); 502e5b6d6dSopenharmony_ci JLabel lookupLabel = new JLabel(Resources.getTranslation("dialog_lookups")); 512e5b6d6dSopenharmony_ci JLabel createdLabel = new JLabel(Resources.getTranslation("dialog_created")); 522e5b6d6dSopenharmony_ci JLabel modifiedLabel = new JLabel(Resources.getTranslation("dialog_modified")); 532e5b6d6dSopenharmony_ci 542e5b6d6dSopenharmony_ci JComboBox groupComboBox; 552e5b6d6dSopenharmony_ci JTextField keyField; 562e5b6d6dSopenharmony_ci JTextField transField; 572e5b6d6dSopenharmony_ci JTextField defTransField; 582e5b6d6dSopenharmony_ci JTextField commentField; 592e5b6d6dSopenharmony_ci JLabel createdLabel2; 602e5b6d6dSopenharmony_ci JLabel modifiedLabel2; 612e5b6d6dSopenharmony_ci JLabel lookupLabel2 = null; 622e5b6d6dSopenharmony_ci JCheckBox transCheckBox; 632e5b6d6dSopenharmony_ci JButton saveButton = new JButton(Resources.getTranslation("button_edit")); 642e5b6d6dSopenharmony_ci JButton cancelButton = new JButton(Resources.getTranslation("button_cancel")); 652e5b6d6dSopenharmony_ci Box lookupBox = null; 662e5b6d6dSopenharmony_ci Box lookups[] = null; 672e5b6d6dSopenharmony_ci JLabel lookupLabels[] = null; 682e5b6d6dSopenharmony_ci JTextField lookupFields[] = null; 692e5b6d6dSopenharmony_ci 702e5b6d6dSopenharmony_ci public BundleItemDialog(RBManager rbm, BundleItem item, String user, JFrame frame, String title, boolean modal) { 712e5b6d6dSopenharmony_ci super(frame, title, modal); 722e5b6d6dSopenharmony_ci this.rbm = rbm; 732e5b6d6dSopenharmony_ci this.user = user; 742e5b6d6dSopenharmony_ci this.item = item; 752e5b6d6dSopenharmony_ci initComponents(); 762e5b6d6dSopenharmony_ci enableEvents(AWTEvent.KEY_EVENT_MASK); 772e5b6d6dSopenharmony_ci } 782e5b6d6dSopenharmony_ci 792e5b6d6dSopenharmony_ci protected void processKeyEvent(KeyEvent ev) { 802e5b6d6dSopenharmony_ci if (ev.getKeyCode() == KeyEvent.VK_ENTER && ev.getID() == KeyEvent.KEY_RELEASED) { 812e5b6d6dSopenharmony_ci actionPerformed(null); 822e5b6d6dSopenharmony_ci } else if (ev.getKeyCode() == KeyEvent.VK_CANCEL) { 832e5b6d6dSopenharmony_ci closeWindow(); 842e5b6d6dSopenharmony_ci } 852e5b6d6dSopenharmony_ci } 862e5b6d6dSopenharmony_ci 872e5b6d6dSopenharmony_ci private void initComponents(){ 882e5b6d6dSopenharmony_ci // Error check 892e5b6d6dSopenharmony_ci if (item == null) closeWindow(); 902e5b6d6dSopenharmony_ci if (!firstInit) closeWindow(); 912e5b6d6dSopenharmony_ci 922e5b6d6dSopenharmony_ci // Initialize values 932e5b6d6dSopenharmony_ci DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); 942e5b6d6dSopenharmony_ci Bundle bundle = item.getParentGroup().getParentBundle(); 952e5b6d6dSopenharmony_ci 962e5b6d6dSopenharmony_ci // Lookup the default translation 972e5b6d6dSopenharmony_ci String defTrans = new String(); 982e5b6d6dSopenharmony_ci Object o = ((Bundle)rbm.getBundles().firstElement()).allItems.get(item.getKey()); 992e5b6d6dSopenharmony_ci if (o != null) 1002e5b6d6dSopenharmony_ci defTrans = ((BundleItem)o).getTranslation(); 1012e5b6d6dSopenharmony_ci 1022e5b6d6dSopenharmony_ci keyField = new JTextField(item.getKey()); 1032e5b6d6dSopenharmony_ci keyField.setEnabled(false); 1042e5b6d6dSopenharmony_ci defTransField = new JTextField(defTrans); 1052e5b6d6dSopenharmony_ci defTransField.setEnabled(false); 1062e5b6d6dSopenharmony_ci transField = new JTextField(item.getTranslation()); 1072e5b6d6dSopenharmony_ci commentField = new JTextField(item.getComment()); 1082e5b6d6dSopenharmony_ci String created[] = {df.format(item.getCreatedDate()), item.getCreator()}; 1092e5b6d6dSopenharmony_ci String modified[] = {df.format(item.getModifiedDate()), item.getModifier()}; 1102e5b6d6dSopenharmony_ci String createdString = Resources.getTranslation("dialog_date_person", created); 1112e5b6d6dSopenharmony_ci String modifiedString = Resources.getTranslation("dialog_date_person", modified); 1122e5b6d6dSopenharmony_ci createdLabel2 = new JLabel(item.getCreator() == null ? df.format(item.getCreatedDate()) : createdString); 1132e5b6d6dSopenharmony_ci modifiedLabel2 = new JLabel(item.getModifier() == null ? df.format(item.getModifiedDate()) : modifiedString); 1142e5b6d6dSopenharmony_ci transCheckBox = new JCheckBox(Resources.getTranslation("dialog_checkbox_translated"),item.isTranslated()); 1152e5b6d6dSopenharmony_ci 1162e5b6d6dSopenharmony_ci groupComboBox = new JComboBox(bundle.getGroupsAsVector()); 1172e5b6d6dSopenharmony_ci for (int i=0; i < groupComboBox.getItemCount(); i++) { 1182e5b6d6dSopenharmony_ci BundleGroup bg = (BundleGroup)groupComboBox.getItemAt(i); 1192e5b6d6dSopenharmony_ci if (bg.getName().equals(item.getParentGroup().getName())) { 1202e5b6d6dSopenharmony_ci groupComboBox.setSelectedIndex(i); 1212e5b6d6dSopenharmony_ci break; 1222e5b6d6dSopenharmony_ci } 1232e5b6d6dSopenharmony_ci } 1242e5b6d6dSopenharmony_ci groupComboBox.setEnabled(false); 1252e5b6d6dSopenharmony_ci 1262e5b6d6dSopenharmony_ci // Set up the components 1272e5b6d6dSopenharmony_ci groupLabel.setPreferredSize(leftDim); 1282e5b6d6dSopenharmony_ci groupComboBox.setPreferredSize(rightDim); 1292e5b6d6dSopenharmony_ci keyLabel.setPreferredSize(leftDim); 1302e5b6d6dSopenharmony_ci //keyField.setPreferredSize(rightDim); 1312e5b6d6dSopenharmony_ci keyField.setColumns(30); 1322e5b6d6dSopenharmony_ci defTransLabel.setPreferredSize(leftDim); 1332e5b6d6dSopenharmony_ci //defTransField.setPreferredSize(rightDim); 1342e5b6d6dSopenharmony_ci defTransField.setColumns(30); 1352e5b6d6dSopenharmony_ci transLabel.setPreferredSize(leftDim); 1362e5b6d6dSopenharmony_ci //transField.setPreferredSize(rightDim); 1372e5b6d6dSopenharmony_ci transField.setColumns(30); 1382e5b6d6dSopenharmony_ci commentLabel.setPreferredSize(leftDim); 1392e5b6d6dSopenharmony_ci //commentField.setPreferredSize(rightDim); 1402e5b6d6dSopenharmony_ci commentField.setColumns(30); 1412e5b6d6dSopenharmony_ci lookupLabel.setPreferredSize(leftDim); 1422e5b6d6dSopenharmony_ci createdLabel.setPreferredSize(leftDim); 1432e5b6d6dSopenharmony_ci createdLabel2.setPreferredSize(rightDim); 1442e5b6d6dSopenharmony_ci modifiedLabel.setPreferredSize(leftDim); 1452e5b6d6dSopenharmony_ci modifiedLabel2.setPreferredSize(rightDim); 1462e5b6d6dSopenharmony_ci // Special setup for the lookup items if they exist 1472e5b6d6dSopenharmony_ci if (item.getLookups().size() < 1) { 1482e5b6d6dSopenharmony_ci lookupLabel2 = new JLabel(Resources.getTranslation("none")); 1492e5b6d6dSopenharmony_ci lookupLabel2.setPreferredSize(rightDim); 1502e5b6d6dSopenharmony_ci } else { 1512e5b6d6dSopenharmony_ci lookupBox = new Box(BoxLayout.Y_AXIS); 1522e5b6d6dSopenharmony_ci lookups = new Box[item.getLookups().size()]; 1532e5b6d6dSopenharmony_ci lookupLabels = new JLabel[item.getLookups().size()]; 1542e5b6d6dSopenharmony_ci lookupFields = new JTextField[item.getLookups().size()]; 1552e5b6d6dSopenharmony_ci Enumeration keys = item.getLookups().keys(); 1562e5b6d6dSopenharmony_ci for (int i = 0; i < item.getLookups().size(); i++) { 1572e5b6d6dSopenharmony_ci String name = (String)keys.nextElement(); 1582e5b6d6dSopenharmony_ci String value = (String)item.getLookups().get(name); 1592e5b6d6dSopenharmony_ci RBManagerGUI.debugMsg("X - Lookup: " + name + " -> " + value); 1602e5b6d6dSopenharmony_ci lookups[i] = new Box(BoxLayout.X_AXIS); 1612e5b6d6dSopenharmony_ci lookupLabels[i] = new JLabel("{" + name + "}"); 1622e5b6d6dSopenharmony_ci lookupLabels[i].setPreferredSize(new Dimension(30,row_height)); 1632e5b6d6dSopenharmony_ci lookupFields[i] = new JTextField(value); 1642e5b6d6dSopenharmony_ci lookupFields[i].setPreferredSize(new Dimension(right_col_width-35,row_height)); 1652e5b6d6dSopenharmony_ci lookups[i].add(Box.createHorizontalGlue()); 1662e5b6d6dSopenharmony_ci lookups[i].add(lookupLabels[i]); 1672e5b6d6dSopenharmony_ci lookups[i].add(Box.createHorizontalStrut(5)); 1682e5b6d6dSopenharmony_ci lookups[i].add(lookupFields[i]); 1692e5b6d6dSopenharmony_ci lookupBox.add(lookups[i]); 1702e5b6d6dSopenharmony_ci } 1712e5b6d6dSopenharmony_ci } 1722e5b6d6dSopenharmony_ci 1732e5b6d6dSopenharmony_ci // Add the appropriate listeners 1742e5b6d6dSopenharmony_ci if (firstInit) { 1752e5b6d6dSopenharmony_ci cancelButton.addActionListener(new ActionListener() { 1762e5b6d6dSopenharmony_ci public void actionPerformed(ActionEvent ev) { 1772e5b6d6dSopenharmony_ci JDialog dialog = (JDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent().getParent(); 1782e5b6d6dSopenharmony_ci dialog.setVisible(false); 1792e5b6d6dSopenharmony_ci dialog.dispose(); 1802e5b6d6dSopenharmony_ci } 1812e5b6d6dSopenharmony_ci }); 1822e5b6d6dSopenharmony_ci 1832e5b6d6dSopenharmony_ci saveButton.addActionListener(this); 1842e5b6d6dSopenharmony_ci getRootPane().setDefaultButton(saveButton); 1852e5b6d6dSopenharmony_ci 1862e5b6d6dSopenharmony_ci transField.addFocusListener(new TranslationFocusListener(item.getTranslation(),transCheckBox)); 1872e5b6d6dSopenharmony_ci } 1882e5b6d6dSopenharmony_ci 1892e5b6d6dSopenharmony_ci box0.add(groupLabel); box0.add(groupComboBox); 1902e5b6d6dSopenharmony_ci box1.add(keyLabel); box1.add(keyField); 1912e5b6d6dSopenharmony_ci box8.add(defTransLabel); box8.add(defTransField); 1922e5b6d6dSopenharmony_ci box2.add(transLabel); box2.add(transField); 1932e5b6d6dSopenharmony_ci box3.add(commentLabel); box3.add(commentField); 1942e5b6d6dSopenharmony_ci box4.add(Box.createHorizontalGlue()); box4.add(lookupLabel); 1952e5b6d6dSopenharmony_ci if (lookupLabel2 != null) { 1962e5b6d6dSopenharmony_ci box4.add(Box.createHorizontalStrut(5)); 1972e5b6d6dSopenharmony_ci box4.add(lookupLabel2); 1982e5b6d6dSopenharmony_ci } else if (lookupBox != null) { 1992e5b6d6dSopenharmony_ci box4.add(Box.createHorizontalStrut(5)); 2002e5b6d6dSopenharmony_ci box4.add(lookupBox); 2012e5b6d6dSopenharmony_ci } 2022e5b6d6dSopenharmony_ci box5.add(Box.createHorizontalGlue()); box5.add(createdLabel); 2032e5b6d6dSopenharmony_ci box5.add(Box.createHorizontalStrut(5)); box5.add(createdLabel2); 2042e5b6d6dSopenharmony_ci box6.add(Box.createHorizontalGlue()); box6.add(modifiedLabel); 2052e5b6d6dSopenharmony_ci box6.add(Box.createHorizontalStrut(5)); box6.add(modifiedLabel2); 2062e5b6d6dSopenharmony_ci box7.add(transCheckBox); box7.add(saveButton); box7.add(cancelButton); 2072e5b6d6dSopenharmony_ci 2082e5b6d6dSopenharmony_ci // Complete the initialization of the frame 2092e5b6d6dSopenharmony_ci setLocation(new java.awt.Point(50, 50)); 2102e5b6d6dSopenharmony_ci mainBox.removeAll(); 2112e5b6d6dSopenharmony_ci mainBox.add(box0); 2122e5b6d6dSopenharmony_ci mainBox.add(box1); 2132e5b6d6dSopenharmony_ci mainBox.add(box8); 2142e5b6d6dSopenharmony_ci mainBox.add(box2); 2152e5b6d6dSopenharmony_ci mainBox.add(box3); 2162e5b6d6dSopenharmony_ci mainBox.add(Box.createVerticalStrut(5)); 2172e5b6d6dSopenharmony_ci mainBox.add(box4); 2182e5b6d6dSopenharmony_ci mainBox.add(Box.createVerticalStrut(5)); 2192e5b6d6dSopenharmony_ci mainBox.add(box5); 2202e5b6d6dSopenharmony_ci mainBox.add(box6); 2212e5b6d6dSopenharmony_ci mainBox.add(Box.createVerticalStrut(5)); 2222e5b6d6dSopenharmony_ci mainBox.add(box7); 2232e5b6d6dSopenharmony_ci getContentPane().add(mainBox, BorderLayout.CENTER); 2242e5b6d6dSopenharmony_ci validateTree(); 2252e5b6d6dSopenharmony_ci pack(); 2262e5b6d6dSopenharmony_ci setVisible(true); 2272e5b6d6dSopenharmony_ci //setResizable(false); 2282e5b6d6dSopenharmony_ci 2292e5b6d6dSopenharmony_ci firstInit = false; 2302e5b6d6dSopenharmony_ci } 2312e5b6d6dSopenharmony_ci 2322e5b6d6dSopenharmony_ci void closeWindow() { 2332e5b6d6dSopenharmony_ci setVisible(false); 2342e5b6d6dSopenharmony_ci dispose(); 2352e5b6d6dSopenharmony_ci } 2362e5b6d6dSopenharmony_ci 2372e5b6d6dSopenharmony_ci public void actionPerformed(ActionEvent ev) { 2382e5b6d6dSopenharmony_ci if (ev == null && transField.hasFocus()) { 2392e5b6d6dSopenharmony_ci // If we are in the translation field, then enter should create a new line character, not exit the dialog 2402e5b6d6dSopenharmony_ci int caretPos = transField.getCaretPosition(); 2412e5b6d6dSopenharmony_ci String oldText = transField.getText(); 2422e5b6d6dSopenharmony_ci transField.setText(oldText.substring(0,caretPos) + "\n" + oldText.substring(caretPos,oldText.length())); 2432e5b6d6dSopenharmony_ci transField.setCaretPosition(caretPos+1); 2442e5b6d6dSopenharmony_ci validate(); 2452e5b6d6dSopenharmony_ci setSize(getPreferredSize()); 2462e5b6d6dSopenharmony_ci return; 2472e5b6d6dSopenharmony_ci } 2482e5b6d6dSopenharmony_ci 2492e5b6d6dSopenharmony_ci // This action is called when the 'Edit' button is pressed 2502e5b6d6dSopenharmony_ci item.setTranslation(transField.getText().trim()); 2512e5b6d6dSopenharmony_ci if (!item.getKey().equals(keyField.getText())) item.setKey(keyField.getText().trim()); 2522e5b6d6dSopenharmony_ci item.setComment(commentField.getText()); 2532e5b6d6dSopenharmony_ci item.setModifiedDate(new Date()); 2542e5b6d6dSopenharmony_ci item.setModifier(user); 2552e5b6d6dSopenharmony_ci item.setTranslated(transCheckBox.isSelected()); 2562e5b6d6dSopenharmony_ci if (transCheckBox.isSelected()) { 2572e5b6d6dSopenharmony_ci // Remove this item from the untranslated items, if it is there 2582e5b6d6dSopenharmony_ci item.getParentGroup().getParentBundle().removeUntranslatedItem(item.getKey()); 2592e5b6d6dSopenharmony_ci } else { 2602e5b6d6dSopenharmony_ci item.getParentGroup().getParentBundle().addUntranslatedItem(item); 2612e5b6d6dSopenharmony_ci } 2622e5b6d6dSopenharmony_ci if (lookups != null) { 2632e5b6d6dSopenharmony_ci item.setLookups(new Hashtable()); 2642e5b6d6dSopenharmony_ci for (int i=0; i < lookups.length; i++) { 2652e5b6d6dSopenharmony_ci String name = lookupLabels[i].getText().trim(); 2662e5b6d6dSopenharmony_ci if (name.indexOf("{") >= 0) name = name.substring(name.indexOf("{")+1,name.length()); 2672e5b6d6dSopenharmony_ci if (name.indexOf("}") >= 0) name = name.substring(0, name.indexOf("}")); 2682e5b6d6dSopenharmony_ci String value = lookupFields[i].getText().trim(); 2692e5b6d6dSopenharmony_ci item.getLookups().put(name,value); 2702e5b6d6dSopenharmony_ci } 2712e5b6d6dSopenharmony_ci } 2722e5b6d6dSopenharmony_ci closeWindow(); 2732e5b6d6dSopenharmony_ci } 2742e5b6d6dSopenharmony_ci} 2752e5b6d6dSopenharmony_ci 2762e5b6d6dSopenharmony_ci/** 2772e5b6d6dSopenharmony_ci * A listener which checks a translation box to see if it changes, if it does, it marks the word as translated in a check box 2782e5b6d6dSopenharmony_ci */ 2792e5b6d6dSopenharmony_ciclass TranslationFocusListener implements FocusListener { 2802e5b6d6dSopenharmony_ci String original; 2812e5b6d6dSopenharmony_ci JCheckBox cbox; 2822e5b6d6dSopenharmony_ci boolean selected; 2832e5b6d6dSopenharmony_ci 2842e5b6d6dSopenharmony_ci public TranslationFocusListener(String original, JCheckBox cbox) { 2852e5b6d6dSopenharmony_ci this.original = original; 2862e5b6d6dSopenharmony_ci this.cbox = cbox; 2872e5b6d6dSopenharmony_ci selected = cbox.isSelected(); 2882e5b6d6dSopenharmony_ci } 2892e5b6d6dSopenharmony_ci 2902e5b6d6dSopenharmony_ci public void focusGained(FocusEvent ev) {} 2912e5b6d6dSopenharmony_ci 2922e5b6d6dSopenharmony_ci public void focusLost(FocusEvent ev) { 2932e5b6d6dSopenharmony_ci JTextField field = (JTextField)ev.getSource(); 2942e5b6d6dSopenharmony_ci if (field.getText().equals(original)) { 2952e5b6d6dSopenharmony_ci cbox.setSelected(selected); 2962e5b6d6dSopenharmony_ci return; 2972e5b6d6dSopenharmony_ci } 2982e5b6d6dSopenharmony_ci cbox.setSelected(true); 2992e5b6d6dSopenharmony_ci } 3002e5b6d6dSopenharmony_ci} 3012e5b6d6dSopenharmony_ci 302