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.text.DateFormat;
012: import java.util.*;
013:
014: import javax.swing.*;
015:
016: import com.ibm.rbm.*;
017:
018: //A dialog which displays the properties of a Bundle Item in an editable way
019:
020: class BundleItemDialog extends JDialog implements ActionListener {
021: RBManager rbm;
022: BundleItem item;
023: String user;
024: boolean firstInit = true;
025:
026: // Helper data
027: int left_col_width = 125;
028: int right_col_width = 375;
029: int row_height = 25;
030: Dimension leftDim = new Dimension(left_col_width, row_height);
031: Dimension rightDim = new Dimension(right_col_width, row_height);
032:
033: // Components
034: Box mainBox = new Box(BoxLayout.Y_AXIS);
035: Box box0 = new Box(BoxLayout.X_AXIS);
036: Box box1 = new Box(BoxLayout.X_AXIS);
037: Box box2 = new Box(BoxLayout.X_AXIS);
038: Box box3 = new Box(BoxLayout.X_AXIS);
039: Box box4 = new Box(BoxLayout.X_AXIS);
040: Box box5 = new Box(BoxLayout.X_AXIS);
041: Box box6 = new Box(BoxLayout.X_AXIS);
042: Box box7 = new Box(BoxLayout.X_AXIS);
043: Box box8 = new Box(BoxLayout.X_AXIS);
044:
045: JLabel groupLabel = new JLabel(Resources
046: .getTranslation("dialog_group"));
047: JLabel keyLabel = new JLabel(Resources.getTranslation("dialog_key"));
048: JLabel defTransLabel = new JLabel(Resources
049: .getTranslation("dialog_default_translation"));
050: JLabel transLabel = new JLabel(Resources
051: .getTranslation("dialog_translation"));
052: JLabel commentLabel = new JLabel(Resources
053: .getTranslation("dialog_comment"));
054: JLabel lookupLabel = new JLabel(Resources
055: .getTranslation("dialog_lookups"));
056: JLabel createdLabel = new JLabel(Resources
057: .getTranslation("dialog_created"));
058: JLabel modifiedLabel = new JLabel(Resources
059: .getTranslation("dialog_modified"));
060:
061: JComboBox groupComboBox;
062: JTextField keyField;
063: JTextField transField;
064: JTextField defTransField;
065: JTextField commentField;
066: JLabel createdLabel2;
067: JLabel modifiedLabel2;
068: JLabel lookupLabel2 = null;
069: JCheckBox transCheckBox;
070: JButton saveButton = new JButton(Resources
071: .getTranslation("button_edit"));
072: JButton cancelButton = new JButton(Resources
073: .getTranslation("button_cancel"));
074: Box lookupBox = null;
075: Box lookups[] = null;
076: JLabel lookupLabels[] = null;
077: JTextField lookupFields[] = null;
078:
079: public BundleItemDialog(RBManager rbm, BundleItem item,
080: String user, JFrame frame, String title, boolean modal) {
081: super (frame, title, modal);
082: this .rbm = rbm;
083: this .user = user;
084: this .item = item;
085: initComponents();
086: enableEvents(AWTEvent.KEY_EVENT_MASK);
087: }
088:
089: protected void processKeyEvent(KeyEvent ev) {
090: if (ev.getKeyCode() == KeyEvent.VK_ENTER
091: && ev.getID() == KeyEvent.KEY_RELEASED) {
092: actionPerformed(null);
093: } else if (ev.getKeyCode() == KeyEvent.VK_CANCEL) {
094: closeWindow();
095: }
096: }
097:
098: private void initComponents(){
099: // Error check
100: if (item == null) closeWindow();
101: if (!firstInit) closeWindow();
102:
103: // Initialize values
104: DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
105: Bundle bundle = item.getParentGroup().getParentBundle();
106:
107: // Lookup the default translation
108: String defTrans = new String();
109: Object o = ((Bundle)rbm.getBundles().firstElement()).allItems.get(item.getKey());
110: if (o != null)
111: defTrans = ((BundleItem)o).getTranslation();
112:
113: keyField = new JTextField(item.getKey());
114: keyField.setEnabled(false);
115: defTransField = new JTextField(defTrans);
116: defTransField.setEnabled(false);
117: transField = new JTextField(item.getTranslation());
118: commentField = new JTextField(item.getComment());
119: String created[] = {df.format(item.getCreatedDate()), item.getCreator()};
120: String modified[] = {df.format(item.getModifiedDate()), item.getModifier()};
121: String createdString = Resources.getTranslation("dialog_date_person", created);
122: String modifiedString = Resources.getTranslation("dialog_date_person", modified);
123: createdLabel2 = new JLabel(item.getCreator() == null ? df.format(item.getCreatedDate()) : createdString);
124: modifiedLabel2 = new JLabel(item.getModifier() == null ? df.format(item.getModifiedDate()) : modifiedString);
125: transCheckBox = new JCheckBox(Resources.getTranslation("dialog_checkbox_translated"),item.isTranslated());
126:
127: groupComboBox = new JComboBox(bundle.getGroupsAsVector());
128: for (int i=0; i < groupComboBox.getItemCount(); i++) {
129: BundleGroup bg = (BundleGroup)groupComboBox.getItemAt(i);
130: if (bg.getName().equals(item.getParentGroup().getName())) {
131: groupComboBox.setSelectedIndex(i);
132: break;
133: }
134: }
135: groupComboBox.setEnabled(false);
136:
137: // Set up the components
138: groupLabel.setPreferredSize(leftDim);
139: groupComboBox.setPreferredSize(rightDim);
140: keyLabel.setPreferredSize(leftDim);
141: //keyField.setPreferredSize(rightDim);
142: keyField.setColumns(30);
143: defTransLabel.setPreferredSize(leftDim);
144: //defTransField.setPreferredSize(rightDim);
145: defTransField.setColumns(30);
146: transLabel.setPreferredSize(leftDim);
147: //transField.setPreferredSize(rightDim);
148: transField.setColumns(30);
149: commentLabel.setPreferredSize(leftDim);
150: //commentField.setPreferredSize(rightDim);
151: commentField.setColumns(30);
152: lookupLabel.setPreferredSize(leftDim);
153: createdLabel.setPreferredSize(leftDim);
154: createdLabel2.setPreferredSize(rightDim);
155: modifiedLabel.setPreferredSize(leftDim);
156: modifiedLabel2.setPreferredSize(rightDim);
157: // Special setup for the lookup items if they exist
158: if (item.getLookups().size() < 1) {
159: lookupLabel2 = new JLabel(Resources.getTranslation("none"));
160: lookupLabel2.setPreferredSize(rightDim);
161: } else {
162: lookupBox = new Box(BoxLayout.Y_AXIS);
163: lookups = new Box[item.getLookups().size()];
164: lookupLabels = new JLabel[item.getLookups().size()];
165: lookupFields = new JTextField[item.getLookups().size()];
166: Enumeration enum = item.getLookups().keys();
167: for (int i = 0; i < item.getLookups().size(); i++) {
168: String name = (String)enum.nextElement();
169: String value = (String)item.getLookups().get(name);
170: RBManagerGUI.debugMsg("X - Lookup: " + name + " -> " + value);
171: lookups[i] = new Box(BoxLayout.X_AXIS);
172: lookupLabels[i] = new JLabel("{" + name + "}");
173: lookupLabels[i].setPreferredSize(new Dimension(30,row_height));
174: lookupFields[i] = new JTextField(value);
175: lookupFields[i].setPreferredSize(new Dimension(right_col_width-35,row_height));
176: lookups[i].add(Box.createHorizontalGlue());
177: lookups[i].add(lookupLabels[i]);
178: lookups[i].add(Box.createHorizontalStrut(5));
179: lookups[i].add(lookupFields[i]);
180: lookupBox.add(lookups[i]);
181: }
182: }
183:
184: // Add the appropriate listeners
185: if (firstInit) {
186: cancelButton.addActionListener(new ActionListener() {
187: public void actionPerformed(ActionEvent ev) {
188: JDialog dialog = (JDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent().getParent();
189: dialog.setVisible(false);
190: dialog.dispose();
191: }
192: });
193:
194: saveButton.addActionListener(this );
195: getRootPane().setDefaultButton(saveButton);
196:
197: transField.addFocusListener(new TranslationFocusListener(item.getTranslation(),transCheckBox));
198: }
199:
200: box0.add(groupLabel); box0.add(groupComboBox);
201: box1.add(keyLabel); box1.add(keyField);
202: box8.add(defTransLabel); box8.add(defTransField);
203: box2.add(transLabel); box2.add(transField);
204: box3.add(commentLabel); box3.add(commentField);
205: box4.add(Box.createHorizontalGlue()); box4.add(lookupLabel);
206: if (lookupLabel2 != null) {
207: box4.add(Box.createHorizontalStrut(5));
208: box4.add(lookupLabel2);
209: } else if (lookupBox != null) {
210: box4.add(Box.createHorizontalStrut(5));
211: box4.add(lookupBox);
212: }
213: box5.add(Box.createHorizontalGlue()); box5.add(createdLabel);
214: box5.add(Box.createHorizontalStrut(5)); box5.add(createdLabel2);
215: box6.add(Box.createHorizontalGlue()); box6.add(modifiedLabel);
216: box6.add(Box.createHorizontalStrut(5)); box6.add(modifiedLabel2);
217: box7.add(transCheckBox); box7.add(saveButton); box7.add(cancelButton);
218:
219: // Complete the initialization of the frame
220: setLocation(new java.awt.Point(50, 50));
221: mainBox.removeAll();
222: mainBox.add(box0);
223: mainBox.add(box1);
224: mainBox.add(box8);
225: mainBox.add(box2);
226: mainBox.add(box3);
227: mainBox.add(Box.createVerticalStrut(5));
228: mainBox.add(box4);
229: mainBox.add(Box.createVerticalStrut(5));
230: mainBox.add(box5);
231: mainBox.add(box6);
232: mainBox.add(Box.createVerticalStrut(5));
233: mainBox.add(box7);
234: getContentPane().add(mainBox, BorderLayout.CENTER);
235: validateTree();
236: pack();
237: setVisible(true);
238: //setResizable(false);
239:
240: firstInit = false;
241: }
242:
243: void closeWindow() {
244: setVisible(false);
245: dispose();
246: }
247:
248: public void actionPerformed(ActionEvent ev) {
249: if (ev == null && transField.hasFocus()) {
250: // If we are in the translation field, then enter should create a new line character, not exit the dialog
251: int caretPos = transField.getCaretPosition();
252: String oldText = transField.getText();
253: transField.setText(oldText.substring(0, caretPos) + "\n"
254: + oldText.substring(caretPos, oldText.length()));
255: transField.setCaretPosition(caretPos + 1);
256: validate();
257: setSize(getPreferredSize());
258: return;
259: }
260:
261: // This action is called when the 'Edit' button is pressed
262: item.setTranslation(transField.getText().trim());
263: if (!item.getKey().equals(keyField.getText()))
264: item.setKey(keyField.getText().trim());
265: item.setComment(commentField.getText());
266: item.setModifiedDate(new Date());
267: item.setModifier(user);
268: item.setTranslated(transCheckBox.isSelected());
269: if (transCheckBox.isSelected()) {
270: // Remove this item from the untranslated items, if it is there
271: item.getParentGroup().getParentBundle()
272: .removeUntranslatedItem(item.getKey());
273: } else {
274: item.getParentGroup().getParentBundle()
275: .addUntranslatedItem(item);
276: }
277: if (lookups != null) {
278: item.setLookups(new Hashtable());
279: for (int i = 0; i < lookups.length; i++) {
280: String name = lookupLabels[i].getText().trim();
281: if (name.indexOf("{") >= 0)
282: name = name.substring(name.indexOf("{") + 1, name
283: .length());
284: if (name.indexOf("}") >= 0)
285: name = name.substring(0, name.indexOf("}"));
286: String value = lookupFields[i].getText().trim();
287: item.getLookups().put(name, value);
288: }
289: }
290: closeWindow();
291: }
292: }
293:
294: /**
295: * 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
296: */
297: class TranslationFocusListener implements FocusListener {
298: String original;
299: JCheckBox cbox;
300: boolean selected;
301:
302: public TranslationFocusListener(String original, JCheckBox cbox) {
303: this .original = original;
304: this .cbox = cbox;
305: selected = cbox.isSelected();
306: }
307:
308: public void focusGained(FocusEvent ev) {
309: }
310:
311: public void focusLost(FocusEvent ev) {
312: JTextField field = (JTextField) ev.getSource();
313: if (field.getText().equals(original)) {
314: cbox.setSelected(selected);
315: return;
316: }
317: cbox.setSelected(true);
318: }
319: }
|