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 BundleGroupCreationDialog extends JDialog {
020: RBManager rbm;
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: JTextArea instructionsArea = new JTextArea("");
036: JLabel nameLabel = new JLabel(Resources
037: .getTranslation("dialog_group"));
038: JLabel commentLabel = new JLabel(Resources
039: .getTranslation("dialog_group_comment"));
040: JTextField nameField = new JTextField("");
041: JTextField commentField = new JTextField("");
042: JButton createButton = new JButton(Resources
043: .getTranslation("button_create"));
044: JButton cancelButton = new JButton(Resources
045: .getTranslation("button_cancel"));
046:
047: public BundleGroupCreationDialog(RBManager rbm, JFrame frame,
048: String title, boolean modal) {
049: super (frame, title, modal);
050: this .rbm = rbm;
051: initComponents();
052: enableEvents(AWTEvent.KEY_EVENT_MASK);
053: }
054:
055: boolean createGroup() {
056: if (rbm == null)
057: return false;
058: return rbm.createGroup(nameField.getText().trim(), commentField
059: .getText().trim());
060: }
061:
062: protected void processKeyEvent(KeyEvent ev) {
063: if (ev.getKeyCode() == KeyEvent.VK_ENTER) {
064: boolean success = createGroup();
065: if (!success) {
066: String alert = Resources
067: .getTranslation("error_create_group")
068: + " "
069: + Resources
070: .getTranslation("error_try_again_group");
071: JOptionPane.showMessageDialog(this , alert, Resources
072: .getTranslation("error"),
073: JOptionPane.ERROR_MESSAGE);
074: } else {
075: setVisible(false);
076: dispose();
077: }
078: } else if (ev.getKeyCode() == KeyEvent.VK_CANCEL) {
079: closeWindow();
080: }
081: }
082:
083: private void initComponents() {
084: // Error check
085: if (rbm == null) {
086: String alert = Resources
087: .getTranslation("error_no_bundle_for_group");
088: JOptionPane
089: .showMessageDialog(this , alert, Resources
090: .getTranslation("error"),
091: JOptionPane.ERROR_MESSAGE);
092: closeWindow();
093: return;
094: }
095:
096: // Initialize values
097:
098: // Set up the components
099: nameLabel.setPreferredSize(leftDim);
100: nameField.setColumns(30);
101: commentLabel.setPreferredSize(leftDim);
102: commentField.setColumns(30);
103: getRootPane().setDefaultButton(createButton);
104:
105: box1.add(nameLabel);
106: box1.add(nameField);
107: box2.add(commentLabel);
108: box2.add(commentField);
109: box3.add(createButton);
110: box3.add(Box.createHorizontalStrut(5));
111: box3.add(cancelButton);
112:
113: instructionsArea.setBorder(BorderFactory.createEtchedBorder());
114:
115: // Add the appropriate listeners
116: cancelButton.addActionListener(new ActionListener() {
117: public void actionPerformed(ActionEvent ev) {
118: JDialog dialog = (JDialog) ((JButton) ev.getSource())
119: .getParent().getParent().getParent()
120: .getParent().getParent();
121: dialog.setVisible(false);
122: dialog.dispose();
123: }
124: });
125:
126: createButton.addActionListener(new ActionListener() {
127: public void actionPerformed(ActionEvent ev) {
128: BundleGroupCreationDialog dialog = (BundleGroupCreationDialog) ((JButton) ev
129: .getSource()).getParent().getParent()
130: .getParent().getParent().getParent();
131: boolean success = dialog.createGroup();
132: if (!success) {
133: String alert = Resources
134: .getTranslation("error_create_group")
135: + " "
136: + Resources
137: .getTranslation("error_try_again_group");
138: JOptionPane.showMessageDialog(dialog, alert,
139: Resources.getTranslation("error"),
140: JOptionPane.ERROR_MESSAGE);
141: } else {
142: dialog.setVisible(false);
143: dialog.dispose();
144: }
145: }
146: });
147:
148: // Complete the initialization of the frame
149: setLocation(new java.awt.Point(50, 50));
150: mainBox.removeAll();
151: mainBox.add(box1);
152: mainBox.add(Box.createVerticalStrut(5));
153: mainBox.add(box2);
154: getContentPane().setLayout(new BorderLayout());
155: getContentPane().removeAll();
156: //getContentPane().add(instructionsArea, BorderLayout.NORTH);
157: getContentPane().add(mainBox, BorderLayout.CENTER);
158: getContentPane().add(box3, BorderLayout.SOUTH);
159: validateTree();
160: pack();
161: setVisible(true);
162: //setResizable(false);
163: }
164:
165: void closeWindow() {
166: setVisible(false);
167: dispose();
168: }
169: }
|