12e5b6d6dSopenharmony_ci/*
22e5b6d6dSopenharmony_ci *****************************************************************************
32e5b6d6dSopenharmony_ci * Copyright (C) 2000-2004, International Business Machines Corporation and  *
42e5b6d6dSopenharmony_ci * others. All Rights Reserved.                                              *
52e5b6d6dSopenharmony_ci *****************************************************************************
62e5b6d6dSopenharmony_ci */
72e5b6d6dSopenharmony_cipackage com.ibm.rbm.gui;
82e5b6d6dSopenharmony_ci
92e5b6d6dSopenharmony_ciimport java.awt.*;
102e5b6d6dSopenharmony_ciimport java.awt.event.*;
112e5b6d6dSopenharmony_ci
122e5b6d6dSopenharmony_ciimport javax.swing.*;
132e5b6d6dSopenharmony_ci
142e5b6d6dSopenharmony_ciimport com.ibm.rbm.*;
152e5b6d6dSopenharmony_ci
162e5b6d6dSopenharmony_ci/**
172e5b6d6dSopenharmony_ci * A dialog which allows the user to create a new Bundle Group
182e5b6d6dSopenharmony_ci */
192e5b6d6dSopenharmony_ciclass BundleGroupCreationDialog extends JDialog {
202e5b6d6dSopenharmony_ci	RBManager rbm;
212e5b6d6dSopenharmony_ci
222e5b6d6dSopenharmony_ci	// Helper data
232e5b6d6dSopenharmony_ci	int left_col_width = 125;
242e5b6d6dSopenharmony_ci	int right_col_width = 275;
252e5b6d6dSopenharmony_ci	int row_height = 25;
262e5b6d6dSopenharmony_ci	Dimension leftDim = new Dimension(left_col_width, row_height);
272e5b6d6dSopenharmony_ci	Dimension rightDim = new Dimension(right_col_width, row_height);
282e5b6d6dSopenharmony_ci
292e5b6d6dSopenharmony_ci	// Components
302e5b6d6dSopenharmony_ci	Box mainBox = new Box(BoxLayout.Y_AXIS);
312e5b6d6dSopenharmony_ci	Box box1 = new Box(BoxLayout.X_AXIS);
322e5b6d6dSopenharmony_ci	Box box2 = new Box(BoxLayout.X_AXIS);
332e5b6d6dSopenharmony_ci	Box box3 = new Box(BoxLayout.X_AXIS);
342e5b6d6dSopenharmony_ci
352e5b6d6dSopenharmony_ci	JTextArea instructionsArea = new JTextArea("");
362e5b6d6dSopenharmony_ci	JLabel nameLabel = new JLabel(Resources.getTranslation("dialog_group"));
372e5b6d6dSopenharmony_ci	JLabel commentLabel = new JLabel(Resources.getTranslation("dialog_group_comment"));
382e5b6d6dSopenharmony_ci	JTextField nameField = new JTextField("");
392e5b6d6dSopenharmony_ci	JTextField commentField = new JTextField("");
402e5b6d6dSopenharmony_ci	JButton createButton = new JButton(Resources.getTranslation("button_create"));
412e5b6d6dSopenharmony_ci	JButton cancelButton = new JButton(Resources.getTranslation("button_cancel"));
422e5b6d6dSopenharmony_ci
432e5b6d6dSopenharmony_ci
442e5b6d6dSopenharmony_ci	public BundleGroupCreationDialog(RBManager rbm, JFrame frame, String title, boolean modal) {
452e5b6d6dSopenharmony_ci		super(frame, title, modal);
462e5b6d6dSopenharmony_ci		this.rbm = rbm;
472e5b6d6dSopenharmony_ci		initComponents();
482e5b6d6dSopenharmony_ci		enableEvents(AWTEvent.KEY_EVENT_MASK);
492e5b6d6dSopenharmony_ci	}
502e5b6d6dSopenharmony_ci
512e5b6d6dSopenharmony_ci	boolean createGroup() {
522e5b6d6dSopenharmony_ci		if (rbm == null) return false;
532e5b6d6dSopenharmony_ci		return rbm.createGroup(nameField.getText().trim(), commentField.getText().trim());
542e5b6d6dSopenharmony_ci	}
552e5b6d6dSopenharmony_ci
562e5b6d6dSopenharmony_ci	protected void processKeyEvent(KeyEvent ev) {
572e5b6d6dSopenharmony_ci		if (ev.getKeyCode() == KeyEvent.VK_ENTER) {
582e5b6d6dSopenharmony_ci			boolean success = createGroup();
592e5b6d6dSopenharmony_ci			if (!success) {
602e5b6d6dSopenharmony_ci				String alert = Resources.getTranslation("error_create_group") + " " +
612e5b6d6dSopenharmony_ci							   Resources.getTranslation("error_try_again_group");
622e5b6d6dSopenharmony_ci				JOptionPane.showMessageDialog(this, alert, Resources.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
632e5b6d6dSopenharmony_ci			} else {
642e5b6d6dSopenharmony_ci				setVisible(false);
652e5b6d6dSopenharmony_ci				dispose();
662e5b6d6dSopenharmony_ci			}
672e5b6d6dSopenharmony_ci		} else if (ev.getKeyCode() == KeyEvent.VK_CANCEL) {
682e5b6d6dSopenharmony_ci			closeWindow();
692e5b6d6dSopenharmony_ci		}
702e5b6d6dSopenharmony_ci	}
712e5b6d6dSopenharmony_ci
722e5b6d6dSopenharmony_ci	private void initComponents(){
732e5b6d6dSopenharmony_ci		// Error check
742e5b6d6dSopenharmony_ci		if (rbm == null) {
752e5b6d6dSopenharmony_ci			String alert = Resources.getTranslation("error_no_bundle_for_group");
762e5b6d6dSopenharmony_ci			JOptionPane.showMessageDialog(this, alert, Resources.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
772e5b6d6dSopenharmony_ci			closeWindow();
782e5b6d6dSopenharmony_ci			return;
792e5b6d6dSopenharmony_ci		}
802e5b6d6dSopenharmony_ci
812e5b6d6dSopenharmony_ci		// Initialize values
822e5b6d6dSopenharmony_ci
832e5b6d6dSopenharmony_ci		// Set up the components
842e5b6d6dSopenharmony_ci		nameLabel.setPreferredSize(leftDim);
852e5b6d6dSopenharmony_ci		nameField.setColumns(30);
862e5b6d6dSopenharmony_ci		commentLabel.setPreferredSize(leftDim);
872e5b6d6dSopenharmony_ci		commentField.setColumns(30);
882e5b6d6dSopenharmony_ci		getRootPane().setDefaultButton(createButton);
892e5b6d6dSopenharmony_ci
902e5b6d6dSopenharmony_ci		box1.add(nameLabel); box1.add(nameField);
912e5b6d6dSopenharmony_ci		box2.add(commentLabel); box2.add(commentField);
922e5b6d6dSopenharmony_ci		box3.add(createButton);
932e5b6d6dSopenharmony_ci		box3.add(Box.createHorizontalStrut(5));
942e5b6d6dSopenharmony_ci		box3.add(cancelButton);
952e5b6d6dSopenharmony_ci
962e5b6d6dSopenharmony_ci		instructionsArea.setBorder(BorderFactory.createEtchedBorder());
972e5b6d6dSopenharmony_ci
982e5b6d6dSopenharmony_ci		// Add the appropriate listeners
992e5b6d6dSopenharmony_ci		cancelButton.addActionListener(new ActionListener() {
1002e5b6d6dSopenharmony_ci			public void actionPerformed(ActionEvent ev) {
1012e5b6d6dSopenharmony_ci				JDialog dialog = (JDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent();
1022e5b6d6dSopenharmony_ci				dialog.setVisible(false);
1032e5b6d6dSopenharmony_ci				dialog.dispose();
1042e5b6d6dSopenharmony_ci			}
1052e5b6d6dSopenharmony_ci		});
1062e5b6d6dSopenharmony_ci
1072e5b6d6dSopenharmony_ci		createButton.addActionListener(new ActionListener() {
1082e5b6d6dSopenharmony_ci			public void actionPerformed(ActionEvent ev) {
1092e5b6d6dSopenharmony_ci				BundleGroupCreationDialog dialog =
1102e5b6d6dSopenharmony_ci					(BundleGroupCreationDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent();
1112e5b6d6dSopenharmony_ci				boolean success = dialog.createGroup();
1122e5b6d6dSopenharmony_ci				if (!success) {
1132e5b6d6dSopenharmony_ci					String alert = Resources.getTranslation("error_create_group") + " " +
1142e5b6d6dSopenharmony_ci								   Resources.getTranslation("error_try_again_group");
1152e5b6d6dSopenharmony_ci					JOptionPane.showMessageDialog(dialog, alert, Resources.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
1162e5b6d6dSopenharmony_ci				} else {
1172e5b6d6dSopenharmony_ci					dialog.setVisible(false);
1182e5b6d6dSopenharmony_ci					dialog.dispose();
1192e5b6d6dSopenharmony_ci				}
1202e5b6d6dSopenharmony_ci			}
1212e5b6d6dSopenharmony_ci		});
1222e5b6d6dSopenharmony_ci
1232e5b6d6dSopenharmony_ci		// Complete the initialization of the frame
1242e5b6d6dSopenharmony_ci		setLocation(new java.awt.Point(50, 50));
1252e5b6d6dSopenharmony_ci		mainBox.removeAll();
1262e5b6d6dSopenharmony_ci		mainBox.add(box1);
1272e5b6d6dSopenharmony_ci		mainBox.add(Box.createVerticalStrut(5));
1282e5b6d6dSopenharmony_ci		mainBox.add(box2);
1292e5b6d6dSopenharmony_ci		getContentPane().setLayout(new BorderLayout());
1302e5b6d6dSopenharmony_ci		getContentPane().removeAll();
1312e5b6d6dSopenharmony_ci		//getContentPane().add(instructionsArea, BorderLayout.NORTH);
1322e5b6d6dSopenharmony_ci		getContentPane().add(mainBox, BorderLayout.CENTER);
1332e5b6d6dSopenharmony_ci		getContentPane().add(box3, BorderLayout.SOUTH);
1342e5b6d6dSopenharmony_ci		validateTree();
1352e5b6d6dSopenharmony_ci		pack();
1362e5b6d6dSopenharmony_ci		setVisible(true);
1372e5b6d6dSopenharmony_ci		//setResizable(false);
1382e5b6d6dSopenharmony_ci	}
1392e5b6d6dSopenharmony_ci
1402e5b6d6dSopenharmony_ci	void closeWindow() {
1412e5b6d6dSopenharmony_ci		setVisible(false);
1422e5b6d6dSopenharmony_ci		dispose();
1432e5b6d6dSopenharmony_ci	}
1442e5b6d6dSopenharmony_ci}
1452e5b6d6dSopenharmony_ci
146