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:
012: import javax.swing.*;
013:
014: import com.ibm.rbm.*;
015:
016: /**
017: * A dialog which allows the user to create a new Bundle Group
018: */
019: class BundleGroupEditDialog extends JDialog {
020: BundleGroup group;
021:
022: // Helper data
023: int left_col_width = 125;
024: int right_col_width = 275;
025: int row_height = 25;
026: Dimension leftDim = new Dimension(left_col_width, row_height);
027: Dimension rightDim = new Dimension(right_col_width, row_height);
028:
029: // Components
030: Box mainBox = new Box(BoxLayout.Y_AXIS);
031: Box box1 = new Box(BoxLayout.X_AXIS);
032: Box box2 = new Box(BoxLayout.X_AXIS);
033: Box box3 = new Box(BoxLayout.X_AXIS);
034:
035: JLabel nameLabel = new JLabel(Resources
036: .getTranslation("dialog_group"));
037: JLabel commentLabel = new JLabel(Resources
038: .getTranslation("dialog_group_comment"));
039: JTextField nameField = new JTextField("");
040: JTextField commentField = new JTextField("");
041: JButton editButton = new JButton(Resources
042: .getTranslation("button_edit"));
043: JButton cancelButton = new JButton(Resources
044: .getTranslation("button_cancel"));
045:
046: public BundleGroupEditDialog(BundleGroup group, JFrame frame,
047: String title, boolean modal) {
048: super (frame, title, modal);
049: this .group = group;
050: initComponents();
051: enableEvents(AWTEvent.KEY_EVENT_MASK);
052: }
053:
054: protected void processKeyEvent(KeyEvent ev) {
055: if (ev.getKeyCode() == KeyEvent.VK_ENTER) {
056: boolean success = editGroup();
057: if (!success) {
058: String alert = Resources
059: .getTranslation("error_modify_group");
060: JOptionPane.showMessageDialog(this , alert, Resources
061: .getTranslation("error_internal"),
062: JOptionPane.ERROR_MESSAGE);
063: } else {
064: setVisible(false);
065: dispose();
066: }
067: } else if (ev.getKeyCode() == KeyEvent.VK_CANCEL) {
068: closeWindow();
069: }
070: }
071:
072: boolean editGroup() {
073: if (group == null)
074: return false;
075: group.setName(nameField.getText().trim());
076: group.setComment(commentField.getText().trim());
077: return true;
078: }
079:
080: private void initComponents() {
081: // Error check
082: if (group == null) {
083: String alert = Resources
084: .getTranslation("error_modify_group");
085: JOptionPane.showMessageDialog(this , alert, Resources
086: .getTranslation("error_internal"),
087: JOptionPane.ERROR_MESSAGE);
088: closeWindow();
089: return;
090: }
091:
092: // Initialize values
093:
094: // Set up the components
095: nameLabel.setPreferredSize(leftDim);
096: nameField.setColumns(30);
097: commentLabel.setPreferredSize(leftDim);
098: commentField.setColumns(30);
099:
100: nameField.setText(group.getName());
101: commentField.setText(group.getComment());
102: getRootPane().setDefaultButton(editButton);
103:
104: box1.add(nameLabel);
105: box1.add(nameField);
106: box2.add(commentLabel);
107: box2.add(commentField);
108: box3.add(editButton);
109: box3.add(Box.createHorizontalStrut(5));
110: box3.add(cancelButton);
111:
112: // Add the appropriate listeners
113: cancelButton.addActionListener(new ActionListener() {
114: public void actionPerformed(ActionEvent ev) {
115: JDialog dialog = (JDialog) ((JButton) ev.getSource())
116: .getParent().getParent().getParent()
117: .getParent().getParent();
118: dialog.setVisible(false);
119: dialog.dispose();
120: }
121: });
122:
123: editButton.addActionListener(new ActionListener() {
124: public void actionPerformed(ActionEvent ev) {
125: BundleGroupEditDialog dialog = (BundleGroupEditDialog) ((JButton) ev
126: .getSource()).getParent().getParent()
127: .getParent().getParent().getParent();
128: boolean success = dialog.editGroup();
129: if (!success) {
130: String alert = Resources
131: .getTranslation("error_modify_group");
132: JOptionPane.showMessageDialog(dialog, alert,
133: Resources.getTranslation("error_internal"),
134: JOptionPane.ERROR_MESSAGE);
135: } else {
136: dialog.setVisible(false);
137: dialog.dispose();
138: }
139: }
140: });
141:
142: // Complete the initialization of the frame
143: setLocation(new java.awt.Point(50, 50));
144: mainBox.removeAll();
145: mainBox.add(box1);
146: mainBox.add(Box.createVerticalStrut(5));
147: mainBox.add(box2);
148: getContentPane().setLayout(new BorderLayout());
149: getContentPane().removeAll();
150: getContentPane().add(mainBox, BorderLayout.CENTER);
151: getContentPane().add(box3, BorderLayout.SOUTH);
152: validateTree();
153: pack();
154: setVisible(true);
155: //setResizable(false);
156: }
157:
158: void closeWindow() {
159: setVisible(false);
160: dispose();
161: }
162: }
|