1/*
2 *****************************************************************************
3 * Copyright (C) 2000-2004, International Business Machines Corporation and  *
4 * others. All Rights Reserved.                                              *
5 *****************************************************************************
6 */
7package com.ibm.rbm.gui;
8
9import java.awt.*;
10import java.awt.event.*;
11
12import javax.swing.*;
13
14import com.ibm.rbm.*;
15
16/**
17 * A dialog which allows the user to create a new Bundle Group
18 */
19class BundleGroupEditDialog extends JDialog {
20	BundleGroup group;
21
22	// Helper data
23	int left_col_width = 125;
24	int right_col_width = 275;
25	int row_height = 25;
26	Dimension leftDim = new Dimension(left_col_width, row_height);
27	Dimension rightDim = new Dimension(right_col_width, row_height);
28
29	// Components
30	Box mainBox = new Box(BoxLayout.Y_AXIS);
31	Box box1 = new Box(BoxLayout.X_AXIS);
32	Box box2 = new Box(BoxLayout.X_AXIS);
33	Box box3 = new Box(BoxLayout.X_AXIS);
34
35	JLabel nameLabel = new JLabel(Resources.getTranslation("dialog_group"));
36	JLabel commentLabel = new JLabel(Resources.getTranslation("dialog_group_comment"));
37	JTextField nameField = new JTextField("");
38	JTextField commentField = new JTextField("");
39	JButton editButton = new JButton(Resources.getTranslation("button_edit"));
40	JButton cancelButton = new JButton(Resources.getTranslation("button_cancel"));
41
42
43	public BundleGroupEditDialog(BundleGroup group, JFrame frame, String title, boolean modal) {
44		super(frame, title, modal);
45		this.group = group;
46		initComponents();
47		enableEvents(AWTEvent.KEY_EVENT_MASK);
48	}
49
50	protected void processKeyEvent(KeyEvent ev) {
51		if (ev.getKeyCode() == KeyEvent.VK_ENTER) {
52			boolean success = editGroup();
53			if (!success) {
54				String alert = Resources.getTranslation("error_modify_group");
55				JOptionPane.showMessageDialog(this, alert, Resources.getTranslation("error_internal"),
56											  JOptionPane.ERROR_MESSAGE);
57			} else {
58				setVisible(false);
59				dispose();
60			}
61		} else if (ev.getKeyCode() == KeyEvent.VK_CANCEL) {
62			closeWindow();
63		}
64	}
65
66	boolean editGroup() {
67		if (group == null) return false;
68		group.setName(nameField.getText().trim());
69		group.setComment(commentField.getText().trim());
70		return true;
71	}
72
73	private void initComponents(){
74		// Error check
75		if (group == null) {
76			String alert = Resources.getTranslation("error_modify_group");
77			JOptionPane.showMessageDialog(this, alert, Resources.getTranslation("error_internal"), JOptionPane.ERROR_MESSAGE);
78			closeWindow();
79			return;
80		}
81
82		// Initialize values
83
84		// Set up the components
85		nameLabel.setPreferredSize(leftDim);
86		nameField.setColumns(30);
87		commentLabel.setPreferredSize(leftDim);
88		commentField.setColumns(30);
89
90		nameField.setText(group.getName());
91		commentField.setText(group.getComment());
92		getRootPane().setDefaultButton(editButton);
93
94		box1.add(nameLabel); box1.add(nameField);
95		box2.add(commentLabel); box2.add(commentField);
96		box3.add(editButton);
97		box3.add(Box.createHorizontalStrut(5));
98		box3.add(cancelButton);
99
100		// Add the appropriate listeners
101		cancelButton.addActionListener(new ActionListener() {
102			public void actionPerformed(ActionEvent ev) {
103				JDialog dialog = (JDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent();
104				dialog.setVisible(false);
105				dialog.dispose();
106			}
107		});
108
109		editButton.addActionListener(new ActionListener() {
110			public void actionPerformed(ActionEvent ev) {
111				BundleGroupEditDialog dialog =
112					(BundleGroupEditDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent();
113				boolean success = dialog.editGroup();
114				if (!success) {
115					String alert = Resources.getTranslation("error_modify_group");
116					JOptionPane.showMessageDialog(dialog, alert, Resources.getTranslation("error_internal"),
117												  JOptionPane.ERROR_MESSAGE);
118				} else {
119					dialog.setVisible(false);
120					dialog.dispose();
121				}
122			}
123		});
124
125		// Complete the initialization of the frame
126		setLocation(new java.awt.Point(50, 50));
127		mainBox.removeAll();
128		mainBox.add(box1);
129		mainBox.add(Box.createVerticalStrut(5));
130		mainBox.add(box2);
131		getContentPane().setLayout(new BorderLayout());
132		getContentPane().removeAll();
133		getContentPane().add(mainBox, BorderLayout.CENTER);
134		getContentPane().add(box3, BorderLayout.SOUTH);
135		validateTree();
136		pack();
137		setVisible(true);
138		//setResizable(false);
139	}
140
141	void closeWindow() {
142		setVisible(false);
143		dispose();
144	}
145}
146
147