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 BundleGroupCreationDialog extends JDialog {
20	RBManager rbm;
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	JTextArea instructionsArea = new JTextArea("");
36	JLabel nameLabel = new JLabel(Resources.getTranslation("dialog_group"));
37	JLabel commentLabel = new JLabel(Resources.getTranslation("dialog_group_comment"));
38	JTextField nameField = new JTextField("");
39	JTextField commentField = new JTextField("");
40	JButton createButton = new JButton(Resources.getTranslation("button_create"));
41	JButton cancelButton = new JButton(Resources.getTranslation("button_cancel"));
42
43
44	public BundleGroupCreationDialog(RBManager rbm, JFrame frame, String title, boolean modal) {
45		super(frame, title, modal);
46		this.rbm = rbm;
47		initComponents();
48		enableEvents(AWTEvent.KEY_EVENT_MASK);
49	}
50
51	boolean createGroup() {
52		if (rbm == null) return false;
53		return rbm.createGroup(nameField.getText().trim(), commentField.getText().trim());
54	}
55
56	protected void processKeyEvent(KeyEvent ev) {
57		if (ev.getKeyCode() == KeyEvent.VK_ENTER) {
58			boolean success = createGroup();
59			if (!success) {
60				String alert = Resources.getTranslation("error_create_group") + " " +
61							   Resources.getTranslation("error_try_again_group");
62				JOptionPane.showMessageDialog(this, alert, Resources.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
63			} else {
64				setVisible(false);
65				dispose();
66			}
67		} else if (ev.getKeyCode() == KeyEvent.VK_CANCEL) {
68			closeWindow();
69		}
70	}
71
72	private void initComponents(){
73		// Error check
74		if (rbm == null) {
75			String alert = Resources.getTranslation("error_no_bundle_for_group");
76			JOptionPane.showMessageDialog(this, alert, Resources.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
77			closeWindow();
78			return;
79		}
80
81		// Initialize values
82
83		// Set up the components
84		nameLabel.setPreferredSize(leftDim);
85		nameField.setColumns(30);
86		commentLabel.setPreferredSize(leftDim);
87		commentField.setColumns(30);
88		getRootPane().setDefaultButton(createButton);
89
90		box1.add(nameLabel); box1.add(nameField);
91		box2.add(commentLabel); box2.add(commentField);
92		box3.add(createButton);
93		box3.add(Box.createHorizontalStrut(5));
94		box3.add(cancelButton);
95
96		instructionsArea.setBorder(BorderFactory.createEtchedBorder());
97
98		// Add the appropriate listeners
99		cancelButton.addActionListener(new ActionListener() {
100			public void actionPerformed(ActionEvent ev) {
101				JDialog dialog = (JDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent();
102				dialog.setVisible(false);
103				dialog.dispose();
104			}
105		});
106
107		createButton.addActionListener(new ActionListener() {
108			public void actionPerformed(ActionEvent ev) {
109				BundleGroupCreationDialog dialog =
110					(BundleGroupCreationDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent();
111				boolean success = dialog.createGroup();
112				if (!success) {
113					String alert = Resources.getTranslation("error_create_group") + " " +
114								   Resources.getTranslation("error_try_again_group");
115					JOptionPane.showMessageDialog(dialog, alert, Resources.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
116				} else {
117					dialog.setVisible(false);
118					dialog.dispose();
119				}
120			}
121		});
122
123		// Complete the initialization of the frame
124		setLocation(new java.awt.Point(50, 50));
125		mainBox.removeAll();
126		mainBox.add(box1);
127		mainBox.add(Box.createVerticalStrut(5));
128		mainBox.add(box2);
129		getContentPane().setLayout(new BorderLayout());
130		getContentPane().removeAll();
131		//getContentPane().add(instructionsArea, BorderLayout.NORTH);
132		getContentPane().add(mainBox, BorderLayout.CENTER);
133		getContentPane().add(box3, BorderLayout.SOUTH);
134		validateTree();
135		pack();
136		setVisible(true);
137		//setResizable(false);
138	}
139
140	void closeWindow() {
141		setVisible(false);
142		dispose();
143	}
144}
145
146