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:
015: import com.ibm.rbm.*;
016:
017: /**
018: * A dialog which allows the user to create a new Bundle Item
019: */
020: class BundleItemCreationDialog extends JDialog {
021: RBManager rbm;
022: String groupName;
023: BundleItem item;
024: boolean firstInit = true;
025:
026: // Helper data
027: int left_col_width = 125;
028: int right_col_width = 275;
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 box1 = new Box(BoxLayout.X_AXIS);
036: Box box2 = new Box(BoxLayout.X_AXIS);
037: Box box3 = new Box(BoxLayout.X_AXIS);
038: Box box4 = new Box(BoxLayout.X_AXIS);
039: Box box5 = new Box(BoxLayout.X_AXIS);
040: Box box6 = new Box(BoxLayout.X_AXIS);
041:
042: JLabel instructionsLabel = new JLabel("");
043: JLabel groupLabel = new JLabel(Resources
044: .getTranslation("dialog_group"));
045: JLabel nameLabel = new JLabel(Resources
046: .getTranslation("dialog_key"));
047: JLabel transLabel = new JLabel(Resources
048: .getTranslation("dialog_translation"));
049: JLabel commentLabel = new JLabel(Resources
050: .getTranslation("dialog_comment"));
051: JLabel lookupLabel = new JLabel(Resources
052: .getTranslation("dialog_lookups"));
053:
054: JComboBox groupComboBox = new JComboBox();
055: JTextField nameField = new JTextField("");
056: JTextField transField = new JTextField("");
057: JTextField commentField = new JTextField("");
058: JTextField lookupFields[] = null;
059: JLabel noLookupLabel = null;
060: Box lookupBox = null;
061: Box lookupBoxes[] = null;
062: JLabel lookupLabels[] = null;
063:
064: JButton createButton = new JButton(Resources
065: .getTranslation("button_create"));
066: JButton createMoreButton = new JButton(Resources
067: .getTranslation("button_create_more"));
068: JButton cancelButton = new JButton(Resources
069: .getTranslation("button_cancel"));
070:
071: Hashtable lookups = new Hashtable();
072:
073: public BundleItemCreationDialog(RBManager rbm, JFrame frame,
074: String title, boolean modal) {
075: super (frame, title, modal);
076: this .rbm = rbm;
077: groupName = null;
078: item = null;
079: initComponents();
080: }
081:
082: public BundleItemCreationDialog(String groupName, RBManager rbm,
083: JFrame frame, String title, boolean modal) {
084: super (frame, title, modal);
085: this .rbm = rbm;
086: this .groupName = groupName;
087: item = null;
088: initComponents();
089: }
090:
091: public BundleItemCreationDialog(BundleItem item, RBManager rbm,
092: JFrame frame, String title, boolean modal) {
093: super (frame, title, modal);
094: this .item = item;
095: this .rbm = rbm;
096: groupName = item.getParentGroup().getName();
097: initComponents();
098: }
099:
100: boolean createItem() {
101: if (rbm == null)
102: return false;
103: Hashtable lookupHash = new Hashtable();
104: if (lookupBoxes != null) {
105: for (int i = 0; i < lookupBoxes.length; i++) {
106: String nameText = lookupLabels[i].getText().trim();
107: String name = nameText.substring(
108: nameText.indexOf("{") + 1, nameText
109: .indexOf("}"));
110: String value = lookupFields[i].getText().trim();
111: lookupHash.put(name, value);
112: }
113: }
114: return rbm.createItem(nameField.getText().trim(), transField
115: .getText().trim(), ((BundleGroup) groupComboBox
116: .getSelectedItem()).getName(), commentField.getText()
117: .trim(), lookupHash);
118: }
119:
120: boolean editItem() {
121: if (item == null)
122: return false;
123: Hashtable lookupHash = new Hashtable();
124: if (lookupBoxes != null) {
125: for (int i = 0; i < lookupBoxes.length; i++) {
126: String nameText = lookupLabels[i].getText().trim();
127: String name = nameText.substring(
128: nameText.indexOf("{") + 1, nameText
129: .indexOf("}"));
130: String value = lookupFields[i].getText().trim();
131: lookupHash.put(name, value);
132: }
133: }
134: return rbm.editItem(item, nameField.getText().trim(),
135: transField.getText().trim(),
136: ((BundleGroup) groupComboBox.getSelectedItem())
137: .getName(), commentField.getText().trim(),
138: lookupHash);
139: }
140:
141: private void clearComponents() {
142: nameField.setText("");
143: transField.setText("");
144: commentField.setText("");
145: initComponents();
146: }
147:
148: protected void processKeyEvent(KeyEvent ev) {
149: if (ev.getKeyCode() == KeyEvent.VK_ENTER
150: && ev.getID() == KeyEvent.KEY_RELEASED) {
151: if (transField.hasFocus()) {
152: // If we are in the translation field, then enter should create a new line character, not exit the dialog
153: int caretPos = transField.getCaretPosition();
154: String oldText = transField.getText();
155: transField
156: .setText(oldText.substring(0, caretPos)
157: + "\n"
158: + oldText.substring(caretPos, oldText
159: .length()));
160: transField.setCaretPosition(caretPos + 1);
161: validate();
162: setSize(getPreferredSize());
163: return;
164: }
165:
166: BundleItemCreationDialog dialog = this ;
167: boolean success = false;
168: if (dialog.item == null)
169: success = dialog.createItem();
170: else
171: success = dialog.editItem();
172: if (!success) {
173: String alert = (item == null ? Resources
174: .getTranslation("error_create_item")
175: : Resources.getTranslation("error_modify_item"));
176: alert += " "
177: + Resources
178: .getTranslation("error_try_again_item");
179: JOptionPane.showMessageDialog(dialog, alert, Resources
180: .getTranslation("error"),
181: JOptionPane.ERROR_MESSAGE);
182: } else {
183: ((RBManagerGUI) dialog.getParent())
184: .updateDisplayPanels();
185: ((RBManagerGUI) dialog.getParent()).invalidate();
186: //((RBManagerGUI)dialog.getParent()).validateMyTree();
187: dialog.setVisible(false);
188: dialog.dispose();
189: }
190: } else if (ev.getKeyCode() == KeyEvent.VK_ESCAPE) {
191: closeWindow();
192: }
193: }
194:
195: private void initComponents(){
196: enableEvents(AWTEvent.KEY_EVENT_MASK);
197: // Error check
198: if (rbm == null || rbm.getBundles() == null) {
199: String alert = Resources.getTranslation("error_no_bundle_for_item");
200: JOptionPane.showMessageDialog(this , alert, Resources.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
201: closeWindow();
202: return;
203: }
204:
205: // Initialize values
206: Bundle mainBundle = (Bundle)rbm.getBundles().firstElement();
207: if (firstInit) {
208: groupComboBox = new JComboBox(mainBundle.getGroupsAsVector());
209: if (groupName != null) {
210: for (int i = 0; i < groupComboBox.getItemCount(); i++) {
211: BundleGroup bg = (BundleGroup)groupComboBox.getItemAt(i);
212: if (bg.getName().equals(groupName)) {
213: groupComboBox.setSelectedIndex(i);
214: break;
215: }
216: }
217: }
218: }
219:
220: if (firstInit && item != null) {
221: // We are editing, not creating an item
222: createButton.setText(Resources.getTranslation("button_edit"));
223: createMoreButton.setText(Resources.getTranslation("button_edit_more"));
224: if (item.getKey() != null) nameField.setText(item.getKey());
225: if (item.getComment() != null) commentField.setText(item.getComment());
226: if (item.getTranslation() != null) transField.setText(item.getTranslation());
227: if (item.getLookups() != null) lookups = item.getLookups();
228: }
229:
230: String currentTrans = transField.getText();
231: // ** LOOKUPS **
232: // Update the lookups if necessary
233: if (lookupBoxes != null) {
234: for (int i=0; i < lookupBoxes.length; i++) {
235: String nameText = lookupLabels[i].getText().trim();
236: String name = nameText.substring(nameText.indexOf("{")+1,nameText.indexOf("}"));
237: String value = lookupFields[i].getText().trim();
238: lookups.put(name,value);
239: }
240: }
241: // Remove old lookups if necessary
242: Enumeration enum = lookups.keys();
243: while (enum.hasMoreElements()) {
244: String name = (String)enum.nextElement();
245: if (currentTrans.indexOf("{" + name + "}") < 0) {
246: lookups.remove(name);
247: }
248: }
249: // Add new lookups if neccesary
250: if (currentTrans != null && currentTrans.indexOf("{") >= 0) {
251: while (currentTrans.indexOf("{") >= 0) {
252: currentTrans = currentTrans.substring(currentTrans.indexOf("{")+1,currentTrans.length());
253: String name = currentTrans.substring(0,currentTrans.indexOf("}"));
254: if (!lookups.containsKey(name)) {
255: lookups.put(name,"");
256: }
257: }
258: }
259: // Remove components
260: box5.removeAll();
261:
262: // Now create the visual components for the lookups
263: if (lookups.size() > 0) {
264: noLookupLabel = null;
265: lookupBox = new Box(BoxLayout.Y_AXIS);
266: lookupBoxes = new Box[lookups.size()];
267: lookupFields = new JTextField[lookups.size()];
268: lookupLabels = new JLabel[lookups.size()];
269: int count = 0;
270: enum = lookups.keys();
271: while (enum.hasMoreElements()) {
272: String name = (String)enum.nextElement();
273: String value = (String)lookups.get(name);
274: RBManagerGUI.debugMsg("Lookup: " + name + " -> " + value);
275: RBManagerGUI.debugMsg(lookups.toString());
276: lookupBoxes[count] = new Box(BoxLayout.X_AXIS);
277: lookupFields[count] = new JTextField((value == null ? "" : value));
278: lookupLabels[count] = new JLabel("{" + name + "}");
279: lookupBoxes[count].add(Box.createHorizontalGlue());
280: lookupBoxes[count].add(lookupLabels[count]);
281: lookupBoxes[count].add(Box.createHorizontalStrut(5));
282: lookupBoxes[count].add(lookupFields[count]);
283: lookupBox.add(lookupBoxes[count]);
284: count++;
285: }
286: } else {
287: lookupBox = null;
288: lookupBoxes = null;
289: lookupFields = null;
290: lookupLabels = null;
291: noLookupLabel = new JLabel(Resources.getTranslation("none"));
292: }
293:
294: // Set up the components
295: if (firstInit) {
296: groupLabel.setPreferredSize(leftDim);
297: groupComboBox.setPreferredSize(rightDim);
298: nameLabel.setPreferredSize(leftDim);
299: nameField.setColumns(30);
300: commentLabel.setPreferredSize(leftDim);
301: commentField.setColumns(30);
302: transLabel.setPreferredSize(leftDim);
303: transField.setColumns(30);
304: lookupLabel.setPreferredSize(leftDim);
305:
306: box1.add(groupLabel); box1.add(groupComboBox);
307: box2.add(nameLabel); box2.add(nameField);
308: box4.add(commentLabel); box4.add(commentField);
309: box3.add(transLabel); box3.add(transField);
310:
311: createButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_create_trigger")));
312: createMoreButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_create_more_trigger")));
313: getRootPane().setDefaultButton(createButton);
314: }
315: box5.add(Box.createHorizontalGlue()); box5.add(lookupLabel); box5.add(Box.createHorizontalStrut(5));
316: if (noLookupLabel != null) {
317: noLookupLabel.setPreferredSize(rightDim);
318: box5.add(noLookupLabel);
319: }
320: else
321: box5.add(lookupBox);
322: if (firstInit) {
323: box6.add(createButton);
324: box6.add(Box.createHorizontalStrut(5));
325: if (item == null)
326: box6.add(createMoreButton);
327: box6.add(Box.createHorizontalStrut(5));
328: box6.add(cancelButton);
329: }
330:
331: instructionsLabel.setBorder(BorderFactory.createEtchedBorder());
332:
333: // Add the appropriate listeners
334: if (firstInit) {
335: cancelButton.addActionListener(new ActionListener() {
336: public void actionPerformed(ActionEvent ev) {
337: JDialog dialog = (JDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent().getParent();
338: dialog.setVisible(false);
339: dialog.dispose();
340: }
341: });
342:
343: createButton.addActionListener(new ActionListener() {
344: public void actionPerformed(ActionEvent ev) {
345: BundleItemCreationDialog dialog =
346: (BundleItemCreationDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent().getParent();
347: boolean success = false;
348: if (dialog.item == null) success = dialog.createItem();
349: else success = dialog.editItem();
350: if (!success) {
351: String alert = (item == null ? Resources.getTranslation("error_create_item") :
352: Resources.getTranslation("error_modify_item"));
353: alert += " " + Resources.getTranslation("error_try_again_item");
354: JOptionPane.showMessageDialog(dialog, alert, Resources.getTranslation("error"),
355: JOptionPane.ERROR_MESSAGE);
356: } else {
357: ((RBManagerGUI)dialog.getParent()).updateDisplayPanels();
358: ((RBManagerGUI)dialog.getParent()).invalidate();
359: //((RBManagerGUI)dialog.getParent()).validateMyTree();
360: dialog.setVisible(false);
361: dialog.dispose();
362: }
363: }
364: });
365:
366: createMoreButton.addActionListener(new ActionListener() {
367: public void actionPerformed(ActionEvent ev) {
368: BundleItemCreationDialog dialog =
369: (BundleItemCreationDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent().getParent();
370: boolean success = false;
371: if (dialog.item == null) success = createItem();
372: else success = dialog.editItem();
373: if (!success) {
374: String alert = (item == null ? Resources.getTranslation("error_create_item") :
375: Resources.getTranslation("error_modify_item"));
376: alert += " " + Resources.getTranslation("error_try_again_item");
377: JOptionPane.showMessageDialog(dialog, alert, Resources.getTranslation("error"),
378: JOptionPane.ERROR_MESSAGE);
379: } else {
380: ((RBManagerGUI)dialog.getParent()).updateDisplayPanels();
381: ((RBManagerGUI)dialog.getParent()).invalidate();
382: //((RBManagerGUI)dialog.getParent()).validateMyTree();
383: dialog.clearComponents();
384: }
385: }
386: });
387:
388: transField.addFocusListener(new FocusListener() {
389: public void focusGained(FocusEvent ev) {}
390: public void focusLost(FocusEvent ev) {
391: BundleItemCreationDialog dialog =
392: (BundleItemCreationDialog)((JTextField)ev.getSource()).getParent().getParent().getParent().getParent().getParent().getParent();
393: firstInit = false;
394: dialog.initComponents();
395: }
396: });
397: }
398:
399: // Complete the initialization of the frame
400: if (firstInit)
401: setLocation(new java.awt.Point(50, 50));
402: mainBox.removeAll();
403: //mainBox.add(instructionsLabel);
404: mainBox.add(Box.createVerticalStrut(5));
405: mainBox.add(box1);
406: mainBox.add(Box.createVerticalStrut(5));
407: mainBox.add(box2);
408: mainBox.add(Box.createVerticalStrut(5));
409: mainBox.add(box3);
410: mainBox.add(Box.createVerticalStrut(5));
411: mainBox.add(box4);
412: mainBox.add(Box.createVerticalStrut(5));
413: if (noLookupLabel == null) {
414: mainBox.add(box5);
415: mainBox.add(Box.createVerticalStrut(5));
416: }
417: mainBox.add(box6);
418: getContentPane().add(mainBox, BorderLayout.CENTER);
419: validateTree();
420: pack();
421: setVisible(true);
422: //setResizable(false);
423: firstInit = false;
424: }
425:
426: void closeWindow() {
427: setVisible(false);
428: dispose();
429: }
430: }
|