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 BundleGroupEditDialog extends JDialog {
202e5b6d6dSopenharmony_ci	BundleGroup group;
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	JLabel nameLabel = new JLabel(Resources.getTranslation("dialog_group"));
362e5b6d6dSopenharmony_ci	JLabel commentLabel = new JLabel(Resources.getTranslation("dialog_group_comment"));
372e5b6d6dSopenharmony_ci	JTextField nameField = new JTextField("");
382e5b6d6dSopenharmony_ci	JTextField commentField = new JTextField("");
392e5b6d6dSopenharmony_ci	JButton editButton = new JButton(Resources.getTranslation("button_edit"));
402e5b6d6dSopenharmony_ci	JButton cancelButton = new JButton(Resources.getTranslation("button_cancel"));
412e5b6d6dSopenharmony_ci
422e5b6d6dSopenharmony_ci
432e5b6d6dSopenharmony_ci	public BundleGroupEditDialog(BundleGroup group, JFrame frame, String title, boolean modal) {
442e5b6d6dSopenharmony_ci		super(frame, title, modal);
452e5b6d6dSopenharmony_ci		this.group = group;
462e5b6d6dSopenharmony_ci		initComponents();
472e5b6d6dSopenharmony_ci		enableEvents(AWTEvent.KEY_EVENT_MASK);
482e5b6d6dSopenharmony_ci	}
492e5b6d6dSopenharmony_ci
502e5b6d6dSopenharmony_ci	protected void processKeyEvent(KeyEvent ev) {
512e5b6d6dSopenharmony_ci		if (ev.getKeyCode() == KeyEvent.VK_ENTER) {
522e5b6d6dSopenharmony_ci			boolean success = editGroup();
532e5b6d6dSopenharmony_ci			if (!success) {
542e5b6d6dSopenharmony_ci				String alert = Resources.getTranslation("error_modify_group");
552e5b6d6dSopenharmony_ci				JOptionPane.showMessageDialog(this, alert, Resources.getTranslation("error_internal"),
562e5b6d6dSopenharmony_ci											  JOptionPane.ERROR_MESSAGE);
572e5b6d6dSopenharmony_ci			} else {
582e5b6d6dSopenharmony_ci				setVisible(false);
592e5b6d6dSopenharmony_ci				dispose();
602e5b6d6dSopenharmony_ci			}
612e5b6d6dSopenharmony_ci		} else if (ev.getKeyCode() == KeyEvent.VK_CANCEL) {
622e5b6d6dSopenharmony_ci			closeWindow();
632e5b6d6dSopenharmony_ci		}
642e5b6d6dSopenharmony_ci	}
652e5b6d6dSopenharmony_ci
662e5b6d6dSopenharmony_ci	boolean editGroup() {
672e5b6d6dSopenharmony_ci		if (group == null) return false;
682e5b6d6dSopenharmony_ci		group.setName(nameField.getText().trim());
692e5b6d6dSopenharmony_ci		group.setComment(commentField.getText().trim());
702e5b6d6dSopenharmony_ci		return true;
712e5b6d6dSopenharmony_ci	}
722e5b6d6dSopenharmony_ci
732e5b6d6dSopenharmony_ci	private void initComponents(){
742e5b6d6dSopenharmony_ci		// Error check
752e5b6d6dSopenharmony_ci		if (group == null) {
762e5b6d6dSopenharmony_ci			String alert = Resources.getTranslation("error_modify_group");
772e5b6d6dSopenharmony_ci			JOptionPane.showMessageDialog(this, alert, Resources.getTranslation("error_internal"), JOptionPane.ERROR_MESSAGE);
782e5b6d6dSopenharmony_ci			closeWindow();
792e5b6d6dSopenharmony_ci			return;
802e5b6d6dSopenharmony_ci		}
812e5b6d6dSopenharmony_ci
822e5b6d6dSopenharmony_ci		// Initialize values
832e5b6d6dSopenharmony_ci
842e5b6d6dSopenharmony_ci		// Set up the components
852e5b6d6dSopenharmony_ci		nameLabel.setPreferredSize(leftDim);
862e5b6d6dSopenharmony_ci		nameField.setColumns(30);
872e5b6d6dSopenharmony_ci		commentLabel.setPreferredSize(leftDim);
882e5b6d6dSopenharmony_ci		commentField.setColumns(30);
892e5b6d6dSopenharmony_ci
902e5b6d6dSopenharmony_ci		nameField.setText(group.getName());
912e5b6d6dSopenharmony_ci		commentField.setText(group.getComment());
922e5b6d6dSopenharmony_ci		getRootPane().setDefaultButton(editButton);
932e5b6d6dSopenharmony_ci
942e5b6d6dSopenharmony_ci		box1.add(nameLabel); box1.add(nameField);
952e5b6d6dSopenharmony_ci		box2.add(commentLabel); box2.add(commentField);
962e5b6d6dSopenharmony_ci		box3.add(editButton);
972e5b6d6dSopenharmony_ci		box3.add(Box.createHorizontalStrut(5));
982e5b6d6dSopenharmony_ci		box3.add(cancelButton);
992e5b6d6dSopenharmony_ci
1002e5b6d6dSopenharmony_ci		// Add the appropriate listeners
1012e5b6d6dSopenharmony_ci		cancelButton.addActionListener(new ActionListener() {
1022e5b6d6dSopenharmony_ci			public void actionPerformed(ActionEvent ev) {
1032e5b6d6dSopenharmony_ci				JDialog dialog = (JDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent();
1042e5b6d6dSopenharmony_ci				dialog.setVisible(false);
1052e5b6d6dSopenharmony_ci				dialog.dispose();
1062e5b6d6dSopenharmony_ci			}
1072e5b6d6dSopenharmony_ci		});
1082e5b6d6dSopenharmony_ci
1092e5b6d6dSopenharmony_ci		editButton.addActionListener(new ActionListener() {
1102e5b6d6dSopenharmony_ci			public void actionPerformed(ActionEvent ev) {
1112e5b6d6dSopenharmony_ci				BundleGroupEditDialog dialog =
1122e5b6d6dSopenharmony_ci					(BundleGroupEditDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent();
1132e5b6d6dSopenharmony_ci				boolean success = dialog.editGroup();
1142e5b6d6dSopenharmony_ci				if (!success) {
1152e5b6d6dSopenharmony_ci					String alert = Resources.getTranslation("error_modify_group");
1162e5b6d6dSopenharmony_ci					JOptionPane.showMessageDialog(dialog, alert, Resources.getTranslation("error_internal"),
1172e5b6d6dSopenharmony_ci												  JOptionPane.ERROR_MESSAGE);
1182e5b6d6dSopenharmony_ci				} else {
1192e5b6d6dSopenharmony_ci					dialog.setVisible(false);
1202e5b6d6dSopenharmony_ci					dialog.dispose();
1212e5b6d6dSopenharmony_ci				}
1222e5b6d6dSopenharmony_ci			}
1232e5b6d6dSopenharmony_ci		});
1242e5b6d6dSopenharmony_ci
1252e5b6d6dSopenharmony_ci		// Complete the initialization of the frame
1262e5b6d6dSopenharmony_ci		setLocation(new java.awt.Point(50, 50));
1272e5b6d6dSopenharmony_ci		mainBox.removeAll();
1282e5b6d6dSopenharmony_ci		mainBox.add(box1);
1292e5b6d6dSopenharmony_ci		mainBox.add(Box.createVerticalStrut(5));
1302e5b6d6dSopenharmony_ci		mainBox.add(box2);
1312e5b6d6dSopenharmony_ci		getContentPane().setLayout(new BorderLayout());
1322e5b6d6dSopenharmony_ci		getContentPane().removeAll();
1332e5b6d6dSopenharmony_ci		getContentPane().add(mainBox, BorderLayout.CENTER);
1342e5b6d6dSopenharmony_ci		getContentPane().add(box3, BorderLayout.SOUTH);
1352e5b6d6dSopenharmony_ci		validateTree();
1362e5b6d6dSopenharmony_ci		pack();
1372e5b6d6dSopenharmony_ci		setVisible(true);
1382e5b6d6dSopenharmony_ci		//setResizable(false);
1392e5b6d6dSopenharmony_ci	}
1402e5b6d6dSopenharmony_ci
1412e5b6d6dSopenharmony_ci	void closeWindow() {
1422e5b6d6dSopenharmony_ci		setVisible(false);
1432e5b6d6dSopenharmony_ci		dispose();
1442e5b6d6dSopenharmony_ci	}
1452e5b6d6dSopenharmony_ci}
1462e5b6d6dSopenharmony_ci
147