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.text.DateFormat; 12 import java.util.*; 13 14 import javax.swing.*; 15 16 import com.ibm.rbm.*; 17 18 //A dialog which displays the properties of a Bundle Item in an editable way 19 20 class BundleItemDialog extends JDialog implements ActionListener { 21 RBManager rbm; 22 BundleItem item; 23 String user; 24 boolean firstInit = true; 25 26 // Helper data 27 int left_col_width = 125; 28 int right_col_width = 375; 29 int row_height = 25; 30 Dimension leftDim = new Dimension(left_col_width, row_height); 31 Dimension rightDim = new Dimension(right_col_width, row_height); 32 33 // Components 34 Box mainBox = new Box(BoxLayout.Y_AXIS); 35 Box box0 = new Box(BoxLayout.X_AXIS); 36 Box box1 = new Box(BoxLayout.X_AXIS); 37 Box box2 = new Box(BoxLayout.X_AXIS); 38 Box box3 = new Box(BoxLayout.X_AXIS); 39 Box box4 = new Box(BoxLayout.X_AXIS); 40 Box box5 = new Box(BoxLayout.X_AXIS); 41 Box box6 = new Box(BoxLayout.X_AXIS); 42 Box box7 = new Box(BoxLayout.X_AXIS); 43 Box box8 = new Box(BoxLayout.X_AXIS); 44 45 JLabel groupLabel = new JLabel(Resources.getTranslation("dialog_group")); 46 JLabel keyLabel = new JLabel(Resources.getTranslation("dialog_key")); 47 JLabel defTransLabel = new JLabel(Resources.getTranslation("dialog_default_translation")); 48 JLabel transLabel = new JLabel(Resources.getTranslation("dialog_translation")); 49 JLabel commentLabel = new JLabel(Resources.getTranslation("dialog_comment")); 50 JLabel lookupLabel = new JLabel(Resources.getTranslation("dialog_lookups")); 51 JLabel createdLabel = new JLabel(Resources.getTranslation("dialog_created")); 52 JLabel modifiedLabel = new JLabel(Resources.getTranslation("dialog_modified")); 53 54 JComboBox groupComboBox; 55 JTextField keyField; 56 JTextField transField; 57 JTextField defTransField; 58 JTextField commentField; 59 JLabel createdLabel2; 60 JLabel modifiedLabel2; 61 JLabel lookupLabel2 = null; 62 JCheckBox transCheckBox; 63 JButton saveButton = new JButton(Resources.getTranslation("button_edit")); 64 JButton cancelButton = new JButton(Resources.getTranslation("button_cancel")); 65 Box lookupBox = null; 66 Box lookups[] = null; 67 JLabel lookupLabels[] = null; 68 JTextField lookupFields[] = null; 69 BundleItemDialog(RBManager rbm, BundleItem item, String user, JFrame frame, String title, boolean modal)70 public BundleItemDialog(RBManager rbm, BundleItem item, String user, JFrame frame, String title, boolean modal) { 71 super(frame, title, modal); 72 this.rbm = rbm; 73 this.user = user; 74 this.item = item; 75 initComponents(); 76 enableEvents(AWTEvent.KEY_EVENT_MASK); 77 } 78 processKeyEvent(KeyEvent ev)79 protected void processKeyEvent(KeyEvent ev) { 80 if (ev.getKeyCode() == KeyEvent.VK_ENTER && ev.getID() == KeyEvent.KEY_RELEASED) { 81 actionPerformed(null); 82 } else if (ev.getKeyCode() == KeyEvent.VK_CANCEL) { 83 closeWindow(); 84 } 85 } 86 initComponents()87 private void initComponents(){ 88 // Error check 89 if (item == null) closeWindow(); 90 if (!firstInit) closeWindow(); 91 92 // Initialize values 93 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); 94 Bundle bundle = item.getParentGroup().getParentBundle(); 95 96 // Lookup the default translation 97 String defTrans = new String(); 98 Object o = ((Bundle)rbm.getBundles().firstElement()).allItems.get(item.getKey()); 99 if (o != null) 100 defTrans = ((BundleItem)o).getTranslation(); 101 102 keyField = new JTextField(item.getKey()); 103 keyField.setEnabled(false); 104 defTransField = new JTextField(defTrans); 105 defTransField.setEnabled(false); 106 transField = new JTextField(item.getTranslation()); 107 commentField = new JTextField(item.getComment()); 108 String created[] = {df.format(item.getCreatedDate()), item.getCreator()}; 109 String modified[] = {df.format(item.getModifiedDate()), item.getModifier()}; 110 String createdString = Resources.getTranslation("dialog_date_person", created); 111 String modifiedString = Resources.getTranslation("dialog_date_person", modified); 112 createdLabel2 = new JLabel(item.getCreator() == null ? df.format(item.getCreatedDate()) : createdString); 113 modifiedLabel2 = new JLabel(item.getModifier() == null ? df.format(item.getModifiedDate()) : modifiedString); 114 transCheckBox = new JCheckBox(Resources.getTranslation("dialog_checkbox_translated"),item.isTranslated()); 115 116 groupComboBox = new JComboBox(bundle.getGroupsAsVector()); 117 for (int i=0; i < groupComboBox.getItemCount(); i++) { 118 BundleGroup bg = (BundleGroup)groupComboBox.getItemAt(i); 119 if (bg.getName().equals(item.getParentGroup().getName())) { 120 groupComboBox.setSelectedIndex(i); 121 break; 122 } 123 } 124 groupComboBox.setEnabled(false); 125 126 // Set up the components 127 groupLabel.setPreferredSize(leftDim); 128 groupComboBox.setPreferredSize(rightDim); 129 keyLabel.setPreferredSize(leftDim); 130 //keyField.setPreferredSize(rightDim); 131 keyField.setColumns(30); 132 defTransLabel.setPreferredSize(leftDim); 133 //defTransField.setPreferredSize(rightDim); 134 defTransField.setColumns(30); 135 transLabel.setPreferredSize(leftDim); 136 //transField.setPreferredSize(rightDim); 137 transField.setColumns(30); 138 commentLabel.setPreferredSize(leftDim); 139 //commentField.setPreferredSize(rightDim); 140 commentField.setColumns(30); 141 lookupLabel.setPreferredSize(leftDim); 142 createdLabel.setPreferredSize(leftDim); 143 createdLabel2.setPreferredSize(rightDim); 144 modifiedLabel.setPreferredSize(leftDim); 145 modifiedLabel2.setPreferredSize(rightDim); 146 // Special setup for the lookup items if they exist 147 if (item.getLookups().size() < 1) { 148 lookupLabel2 = new JLabel(Resources.getTranslation("none")); 149 lookupLabel2.setPreferredSize(rightDim); 150 } else { 151 lookupBox = new Box(BoxLayout.Y_AXIS); 152 lookups = new Box[item.getLookups().size()]; 153 lookupLabels = new JLabel[item.getLookups().size()]; 154 lookupFields = new JTextField[item.getLookups().size()]; 155 Enumeration keys = item.getLookups().keys(); 156 for (int i = 0; i < item.getLookups().size(); i++) { 157 String name = (String)keys.nextElement(); 158 String value = (String)item.getLookups().get(name); 159 RBManagerGUI.debugMsg("X - Lookup: " + name + " -> " + value); 160 lookups[i] = new Box(BoxLayout.X_AXIS); 161 lookupLabels[i] = new JLabel("{" + name + "}"); 162 lookupLabels[i].setPreferredSize(new Dimension(30,row_height)); 163 lookupFields[i] = new JTextField(value); 164 lookupFields[i].setPreferredSize(new Dimension(right_col_width-35,row_height)); 165 lookups[i].add(Box.createHorizontalGlue()); 166 lookups[i].add(lookupLabels[i]); 167 lookups[i].add(Box.createHorizontalStrut(5)); 168 lookups[i].add(lookupFields[i]); 169 lookupBox.add(lookups[i]); 170 } 171 } 172 173 // Add the appropriate listeners 174 if (firstInit) { 175 cancelButton.addActionListener(new ActionListener() { 176 public void actionPerformed(ActionEvent ev) { 177 JDialog dialog = (JDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent().getParent(); 178 dialog.setVisible(false); 179 dialog.dispose(); 180 } 181 }); 182 183 saveButton.addActionListener(this); 184 getRootPane().setDefaultButton(saveButton); 185 186 transField.addFocusListener(new TranslationFocusListener(item.getTranslation(),transCheckBox)); 187 } 188 189 box0.add(groupLabel); box0.add(groupComboBox); 190 box1.add(keyLabel); box1.add(keyField); 191 box8.add(defTransLabel); box8.add(defTransField); 192 box2.add(transLabel); box2.add(transField); 193 box3.add(commentLabel); box3.add(commentField); 194 box4.add(Box.createHorizontalGlue()); box4.add(lookupLabel); 195 if (lookupLabel2 != null) { 196 box4.add(Box.createHorizontalStrut(5)); 197 box4.add(lookupLabel2); 198 } else if (lookupBox != null) { 199 box4.add(Box.createHorizontalStrut(5)); 200 box4.add(lookupBox); 201 } 202 box5.add(Box.createHorizontalGlue()); box5.add(createdLabel); 203 box5.add(Box.createHorizontalStrut(5)); box5.add(createdLabel2); 204 box6.add(Box.createHorizontalGlue()); box6.add(modifiedLabel); 205 box6.add(Box.createHorizontalStrut(5)); box6.add(modifiedLabel2); 206 box7.add(transCheckBox); box7.add(saveButton); box7.add(cancelButton); 207 208 // Complete the initialization of the frame 209 setLocation(new java.awt.Point(50, 50)); 210 mainBox.removeAll(); 211 mainBox.add(box0); 212 mainBox.add(box1); 213 mainBox.add(box8); 214 mainBox.add(box2); 215 mainBox.add(box3); 216 mainBox.add(Box.createVerticalStrut(5)); 217 mainBox.add(box4); 218 mainBox.add(Box.createVerticalStrut(5)); 219 mainBox.add(box5); 220 mainBox.add(box6); 221 mainBox.add(Box.createVerticalStrut(5)); 222 mainBox.add(box7); 223 getContentPane().add(mainBox, BorderLayout.CENTER); 224 validateTree(); 225 pack(); 226 setVisible(true); 227 //setResizable(false); 228 229 firstInit = false; 230 } 231 closeWindow()232 void closeWindow() { 233 setVisible(false); 234 dispose(); 235 } 236 actionPerformed(ActionEvent ev)237 public void actionPerformed(ActionEvent ev) { 238 if (ev == null && transField.hasFocus()) { 239 // If we are in the translation field, then enter should create a new line character, not exit the dialog 240 int caretPos = transField.getCaretPosition(); 241 String oldText = transField.getText(); 242 transField.setText(oldText.substring(0,caretPos) + "\n" + oldText.substring(caretPos,oldText.length())); 243 transField.setCaretPosition(caretPos+1); 244 validate(); 245 setSize(getPreferredSize()); 246 return; 247 } 248 249 // This action is called when the 'Edit' button is pressed 250 item.setTranslation(transField.getText().trim()); 251 if (!item.getKey().equals(keyField.getText())) item.setKey(keyField.getText().trim()); 252 item.setComment(commentField.getText()); 253 item.setModifiedDate(new Date()); 254 item.setModifier(user); 255 item.setTranslated(transCheckBox.isSelected()); 256 if (transCheckBox.isSelected()) { 257 // Remove this item from the untranslated items, if it is there 258 item.getParentGroup().getParentBundle().removeUntranslatedItem(item.getKey()); 259 } else { 260 item.getParentGroup().getParentBundle().addUntranslatedItem(item); 261 } 262 if (lookups != null) { 263 item.setLookups(new Hashtable()); 264 for (int i=0; i < lookups.length; i++) { 265 String name = lookupLabels[i].getText().trim(); 266 if (name.indexOf("{") >= 0) name = name.substring(name.indexOf("{")+1,name.length()); 267 if (name.indexOf("}") >= 0) name = name.substring(0, name.indexOf("}")); 268 String value = lookupFields[i].getText().trim(); 269 item.getLookups().put(name,value); 270 } 271 } 272 closeWindow(); 273 } 274 } 275 276 /** 277 * 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 278 */ 279 class TranslationFocusListener implements FocusListener { 280 String original; 281 JCheckBox cbox; 282 boolean selected; 283 TranslationFocusListener(String original, JCheckBox cbox)284 public TranslationFocusListener(String original, JCheckBox cbox) { 285 this.original = original; 286 this.cbox = cbox; 287 selected = cbox.isSelected(); 288 } 289 focusGained(FocusEvent ev)290 public void focusGained(FocusEvent ev) {} 291 focusLost(FocusEvent ev)292 public void focusLost(FocusEvent ev) { 293 JTextField field = (JTextField)ev.getSource(); 294 if (field.getText().equals(original)) { 295 cbox.setSelected(selected); 296 return; 297 } 298 cbox.setSelected(true); 299 } 300 } 301 302