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 for creating new Resource Files
182e5b6d6dSopenharmony_ci */
192e5b6d6dSopenharmony_ciclass ResourceCreationDialog extends JDialog {
202e5b6d6dSopenharmony_ci	RBManager rbm;
212e5b6d6dSopenharmony_ci	RBManagerGUI gui;
222e5b6d6dSopenharmony_ci
232e5b6d6dSopenharmony_ci	// Components
242e5b6d6dSopenharmony_ci	Box mainBox = new Box(BoxLayout.Y_AXIS);
252e5b6d6dSopenharmony_ci	Box infoBox = new Box(BoxLayout.Y_AXIS);
262e5b6d6dSopenharmony_ci	JPanel infoPanel = new JPanel();
272e5b6d6dSopenharmony_ci	JPanel infoTitlePanel = new JPanel();
282e5b6d6dSopenharmony_ci	JPanel infoCommentPanel = new JPanel();
292e5b6d6dSopenharmony_ci	JPanel infoManagerPanel = new JPanel();
302e5b6d6dSopenharmony_ci	JPanel langPanel = new JPanel();
312e5b6d6dSopenharmony_ci	JPanel counPanel = new JPanel();
322e5b6d6dSopenharmony_ci	JPanel variPanel = new JPanel();
332e5b6d6dSopenharmony_ci	JPanel buttPanel = new JPanel();
342e5b6d6dSopenharmony_ci
352e5b6d6dSopenharmony_ci	JLabel instructionsLabel = new JLabel("");
362e5b6d6dSopenharmony_ci	JLabel titleLabel = new JLabel(Resources.getTranslation("dialog_file_title"));
372e5b6d6dSopenharmony_ci	JLabel commentLabel = new JLabel(Resources.getTranslation("dialog_file_comment"));
382e5b6d6dSopenharmony_ci	JLabel managerLabel = new JLabel(Resources.getTranslation("dialog_file_manager"));
392e5b6d6dSopenharmony_ci	JLabel enc1Label = new JLabel(Resources.getTranslation("dialog_encoding"));
402e5b6d6dSopenharmony_ci	JLabel enc2Label = new JLabel(Resources.getTranslation("dialog_encoding"));
412e5b6d6dSopenharmony_ci	JLabel enc3Label = new JLabel(Resources.getTranslation("dialog_encoding"));
422e5b6d6dSopenharmony_ci	JLabel nam1Label = new JLabel(Resources.getTranslation("dialog_name"));
432e5b6d6dSopenharmony_ci	JLabel nam2Label = new JLabel(Resources.getTranslation("dialog_name"));
442e5b6d6dSopenharmony_ci	JLabel nam3Label = new JLabel(Resources.getTranslation("dialog_name"));
452e5b6d6dSopenharmony_ci
462e5b6d6dSopenharmony_ci	JTextField titleField = new JTextField("");
472e5b6d6dSopenharmony_ci	JTextField commentField = new JTextField("");
482e5b6d6dSopenharmony_ci	JTextField managerField = new JTextField("");
492e5b6d6dSopenharmony_ci	JTextField enc1Field = new JTextField("");
502e5b6d6dSopenharmony_ci	JTextField enc2Field = new JTextField("");
512e5b6d6dSopenharmony_ci	JTextField enc3Field = new JTextField("");
522e5b6d6dSopenharmony_ci	JTextField nam1Field = new JTextField("");
532e5b6d6dSopenharmony_ci	JTextField nam2Field = new JTextField("");
542e5b6d6dSopenharmony_ci	JTextField nam3Field = new JTextField("");
552e5b6d6dSopenharmony_ci
562e5b6d6dSopenharmony_ci	JCheckBox copyCheckBox = new JCheckBox(Resources.getTranslation("dialog_checkbox_copy_elements"), true);
572e5b6d6dSopenharmony_ci
582e5b6d6dSopenharmony_ci	JButton createButton = new JButton(Resources.getTranslation("button_create"));
592e5b6d6dSopenharmony_ci	JButton cancelButton = new JButton(Resources.getTranslation("button_cancel"));
602e5b6d6dSopenharmony_ci
612e5b6d6dSopenharmony_ci	public ResourceCreationDialog(RBManager rbm, JFrame frame, String title, boolean modal) {
622e5b6d6dSopenharmony_ci		super(frame, title, modal);
632e5b6d6dSopenharmony_ci		this.gui = (RBManagerGUI)frame;
642e5b6d6dSopenharmony_ci		this.rbm = rbm;
652e5b6d6dSopenharmony_ci		initComponents();
662e5b6d6dSopenharmony_ci		enableEvents(AWTEvent.KEY_EVENT_MASK);
672e5b6d6dSopenharmony_ci	}
682e5b6d6dSopenharmony_ci
692e5b6d6dSopenharmony_ci	protected void processKeyEvent(KeyEvent ev) {
702e5b6d6dSopenharmony_ci		if (ev.getKeyCode() == KeyEvent.VK_ENTER) {
712e5b6d6dSopenharmony_ci			boolean success = createResource();
722e5b6d6dSopenharmony_ci			if (!success) {
732e5b6d6dSopenharmony_ci				String alert = Resources.getTranslation("error_create_file") + " " +
742e5b6d6dSopenharmony_ci							   Resources.getTranslation("error_try_again_file");
752e5b6d6dSopenharmony_ci				JOptionPane.showMessageDialog(this, alert, Resources.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
762e5b6d6dSopenharmony_ci			} else {
772e5b6d6dSopenharmony_ci				setVisible(false);
782e5b6d6dSopenharmony_ci				dispose();
792e5b6d6dSopenharmony_ci			}
802e5b6d6dSopenharmony_ci		} else if (ev.getKeyCode() == KeyEvent.VK_CANCEL) {
812e5b6d6dSopenharmony_ci			closeWindow();
822e5b6d6dSopenharmony_ci		}
832e5b6d6dSopenharmony_ci	}
842e5b6d6dSopenharmony_ci
852e5b6d6dSopenharmony_ci	boolean createResource() {
862e5b6d6dSopenharmony_ci		if (rbm == null) return false;
872e5b6d6dSopenharmony_ci		String encoding = enc1Field.getText().trim();
882e5b6d6dSopenharmony_ci		String enc2 = enc2Field.getText().trim();
892e5b6d6dSopenharmony_ci		if (enc2 != null && !enc2.equals("")) encoding += "_" + enc2;
902e5b6d6dSopenharmony_ci		String enc3 = enc3Field.getText().trim();
912e5b6d6dSopenharmony_ci		if (enc3 != null && !enc3.equals("")) encoding += "_" + enc3;
922e5b6d6dSopenharmony_ci		boolean ret = rbm.createResource(titleField.getText().trim(), commentField.getText().trim(), managerField.getText().trim(),
932e5b6d6dSopenharmony_ci							encoding, nam1Field.getText().trim(), nam2Field.getText().trim(),
942e5b6d6dSopenharmony_ci							nam3Field.getText().trim(), copyCheckBox.isSelected());
952e5b6d6dSopenharmony_ci		if (ret) {
962e5b6d6dSopenharmony_ci			gui.updateDisplayTree();
972e5b6d6dSopenharmony_ci			gui.updateProjectTree();
982e5b6d6dSopenharmony_ci			gui.updateProjectPanels();
992e5b6d6dSopenharmony_ci		}
1002e5b6d6dSopenharmony_ci		return ret;
1012e5b6d6dSopenharmony_ci	}
1022e5b6d6dSopenharmony_ci
1032e5b6d6dSopenharmony_ci	public void initComponents(){
1042e5b6d6dSopenharmony_ci		// Error check
1052e5b6d6dSopenharmony_ci		if (rbm == null) {
1062e5b6d6dSopenharmony_ci			String alert = Resources.getTranslation("error_no_bundle_for_file");
1072e5b6d6dSopenharmony_ci			JOptionPane.showMessageDialog(this, alert, Resources.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
1082e5b6d6dSopenharmony_ci			closeWindow();
1092e5b6d6dSopenharmony_ci			return;
1102e5b6d6dSopenharmony_ci		}
1112e5b6d6dSopenharmony_ci
1122e5b6d6dSopenharmony_ci		// Initialize values
1132e5b6d6dSopenharmony_ci		int tempWidth = 175;
1142e5b6d6dSopenharmony_ci		Dimension labelDim = new Dimension(tempWidth, 30);
1152e5b6d6dSopenharmony_ci		titleLabel.setPreferredSize(labelDim);
1162e5b6d6dSopenharmony_ci		commentLabel.setPreferredSize(labelDim);
1172e5b6d6dSopenharmony_ci		managerLabel.setPreferredSize(labelDim);
1182e5b6d6dSopenharmony_ci
1192e5b6d6dSopenharmony_ci		infoPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
1202e5b6d6dSopenharmony_ci															 Resources.getTranslation("dialog_file_info")));
1212e5b6d6dSopenharmony_ci		langPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
1222e5b6d6dSopenharmony_ci															 Resources.getTranslation("dialog_language")));
1232e5b6d6dSopenharmony_ci		counPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
1242e5b6d6dSopenharmony_ci															 Resources.getTranslation("dialog_country")));
1252e5b6d6dSopenharmony_ci		variPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
1262e5b6d6dSopenharmony_ci															 Resources.getTranslation("dialog_variant")));
1272e5b6d6dSopenharmony_ci
1282e5b6d6dSopenharmony_ci		titleField.setColumns(30);
1292e5b6d6dSopenharmony_ci		commentField.setColumns(30);
1302e5b6d6dSopenharmony_ci		managerField.setColumns(30);
1312e5b6d6dSopenharmony_ci
1322e5b6d6dSopenharmony_ci		enc1Field.setColumns(3);
1332e5b6d6dSopenharmony_ci		enc2Field.setColumns(3);
1342e5b6d6dSopenharmony_ci		enc3Field.setColumns(3);
1352e5b6d6dSopenharmony_ci		nam1Field.setColumns(20);
1362e5b6d6dSopenharmony_ci		nam2Field.setColumns(20);
1372e5b6d6dSopenharmony_ci		nam3Field.setColumns(20);
1382e5b6d6dSopenharmony_ci
1392e5b6d6dSopenharmony_ci		// Set up the components
1402e5b6d6dSopenharmony_ci		infoTitlePanel.add(titleLabel); infoTitlePanel.add(titleField);
1412e5b6d6dSopenharmony_ci		infoCommentPanel.add(commentLabel); infoCommentPanel.add(commentField);
1422e5b6d6dSopenharmony_ci		infoManagerPanel.add(managerLabel); infoManagerPanel.add(managerField);
1432e5b6d6dSopenharmony_ci		infoBox.add(infoTitlePanel);
1442e5b6d6dSopenharmony_ci		infoBox.add(infoCommentPanel);
1452e5b6d6dSopenharmony_ci		infoBox.add(infoManagerPanel);
1462e5b6d6dSopenharmony_ci		infoPanel.add(infoBox);
1472e5b6d6dSopenharmony_ci
1482e5b6d6dSopenharmony_ci		langPanel.add(enc1Label); langPanel.add(enc1Field); langPanel.add(nam1Label); langPanel.add(nam1Field);
1492e5b6d6dSopenharmony_ci		counPanel.add(enc2Label); counPanel.add(enc2Field); counPanel.add(nam2Label); counPanel.add(nam2Field);
1502e5b6d6dSopenharmony_ci		variPanel.add(enc3Label); variPanel.add(enc3Field); variPanel.add(nam3Label); variPanel.add(nam3Field);
1512e5b6d6dSopenharmony_ci
1522e5b6d6dSopenharmony_ci		buttPanel.add(createButton); buttPanel.add(cancelButton);
1532e5b6d6dSopenharmony_ci
1542e5b6d6dSopenharmony_ci		// Add the appropriate listeners
1552e5b6d6dSopenharmony_ci		cancelButton.addActionListener(new ActionListener() {
1562e5b6d6dSopenharmony_ci			public void actionPerformed(ActionEvent ev) {
1572e5b6d6dSopenharmony_ci				JDialog dialog = (JDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent().getParent();
1582e5b6d6dSopenharmony_ci				dialog.setVisible(false);
1592e5b6d6dSopenharmony_ci				dialog.dispose();
1602e5b6d6dSopenharmony_ci			}
1612e5b6d6dSopenharmony_ci		});
1622e5b6d6dSopenharmony_ci
1632e5b6d6dSopenharmony_ci		createButton.addActionListener(new ActionListener() {
1642e5b6d6dSopenharmony_ci			public void actionPerformed(ActionEvent ev) {
1652e5b6d6dSopenharmony_ci				ResourceCreationDialog dialog =
1662e5b6d6dSopenharmony_ci					(ResourceCreationDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent().getParent();
1672e5b6d6dSopenharmony_ci				boolean success = dialog.createResource();
1682e5b6d6dSopenharmony_ci				if (!success) {
1692e5b6d6dSopenharmony_ci					String alert = Resources.getTranslation("error_create_file") + " " +
1702e5b6d6dSopenharmony_ci								   Resources.getTranslation("error_try_again_file");
1712e5b6d6dSopenharmony_ci					JOptionPane.showMessageDialog(dialog, alert, Resources.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
1722e5b6d6dSopenharmony_ci				} else {
1732e5b6d6dSopenharmony_ci					dialog.setVisible(false);
1742e5b6d6dSopenharmony_ci					dialog.dispose();
1752e5b6d6dSopenharmony_ci				}
1762e5b6d6dSopenharmony_ci			}
1772e5b6d6dSopenharmony_ci		});
1782e5b6d6dSopenharmony_ci		getRootPane().setDefaultButton(createButton);
1792e5b6d6dSopenharmony_ci
1802e5b6d6dSopenharmony_ci		// Complete the component layout
1812e5b6d6dSopenharmony_ci		mainBox.removeAll();
1822e5b6d6dSopenharmony_ci		//mainBox.add(instructionsLabel);
1832e5b6d6dSopenharmony_ci		mainBox.add(infoPanel);
1842e5b6d6dSopenharmony_ci		mainBox.add(langPanel);
1852e5b6d6dSopenharmony_ci		mainBox.add(counPanel);
1862e5b6d6dSopenharmony_ci		mainBox.add(variPanel);
1872e5b6d6dSopenharmony_ci		mainBox.add(copyCheckBox);
1882e5b6d6dSopenharmony_ci		mainBox.add(buttPanel);
1892e5b6d6dSopenharmony_ci
1902e5b6d6dSopenharmony_ci		setLocation(new java.awt.Point(50, 50));
1912e5b6d6dSopenharmony_ci		getContentPane().add(mainBox, BorderLayout.CENTER);
1922e5b6d6dSopenharmony_ci		validateTree();
1932e5b6d6dSopenharmony_ci		pack();
1942e5b6d6dSopenharmony_ci		setVisible(true);
1952e5b6d6dSopenharmony_ci		//setResizable(false);
1962e5b6d6dSopenharmony_ci	}
1972e5b6d6dSopenharmony_ci
1982e5b6d6dSopenharmony_ci	void closeWindow() {
1992e5b6d6dSopenharmony_ci		setVisible(false);
2002e5b6d6dSopenharmony_ci		dispose();
2012e5b6d6dSopenharmony_ci	}
2022e5b6d6dSopenharmony_ci}
2032e5b6d6dSopenharmony_ci
204