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.*;
11import java.util.Vector;
12
13import javax.swing.*;
14
15import com.ibm.rbm.*;
16
17/**
18 */
19class RBProjectItemPanel extends JPanel implements ActionListener {
20	RBManagerGUI gui;
21
22	// Visual Components
23	Box mainBox;
24	JTextField itemFields[];
25	JLabel     itemLabels[];
26	JButton    commitButtons[];
27	JButton    commitButton;
28	JLabel     titleLabel;
29	JLabel     keyLabel;
30	JLabel     commentLabel;
31
32	public RBProjectItemPanel(RBManagerGUI gui) {
33		super();
34		this.gui = gui;
35		initComponents();
36	}
37
38	public void actionPerformed(ActionEvent ev) {
39		JButton button = (JButton)ev.getSource();
40		String buttonName = button.getName();
41		if (buttonName == null) {
42			// Save all components
43			RBManager bundle = gui.getSelectedProjectBundle();
44			Vector bundles = bundle.getBundles();
45			for (int i=0; i < itemFields.length; i++) {
46				String encoding = commitButtons[i].getName();
47				String translation = itemFields[i].getText();
48				String key = itemFields[i].getName();
49				for (int j=0; j < bundles.size(); j++) {
50					Bundle rbundle = (Bundle)bundles.elementAt(j);
51					if (rbundle.encoding.equals(encoding)) {
52						BundleItem item = rbundle.getBundleItem(key);
53						if (item != null) item.setTranslation(translation);
54						break;
55					}
56				}
57			}
58			gui.saveResources(bundle);
59		} else {
60			// Save a particular encoding
61			String encoding = buttonName;
62			RBManager bundle = gui.getSelectedProjectBundle();
63			int index = -1;
64			for (int i=0; i < commitButtons.length; i++) {
65				if (commitButtons[i] == button) {
66					index = i;
67					break;
68				}
69			}
70			String translation = itemFields[index].getText();
71			String key = itemFields[index].getName();
72			Vector bundles = bundle.getBundles();
73			for (int i=0; i < bundles.size(); i++) {
74				Bundle rbundle = (Bundle)bundles.elementAt(i);
75				if (rbundle.encoding.equals(encoding)) {
76					BundleItem item = rbundle.getBundleItem(key);
77					if (item != null) {
78						item.setTranslation(translation);
79						RBManagerGUI.debugMsg("Set translation to : " + translation);
80					}
81					else
82					    RBManagerGUI.debugMsg("Item was null");
83					break;
84				}
85				RBManagerGUI.debugMsg("Compared " + rbundle.encoding + " with " + encoding);
86			}
87			gui.saveResources(bundle, encoding);
88		}
89		updateComponents();
90	}
91
92	private void initComponents() {
93		setLayout(new BorderLayout());
94		JPanel topPanel = new JPanel(new GridLayout(2,1));
95		titleLabel = new JLabel(Resources.getTranslation("project_panel_default_title"), SwingConstants.CENTER);
96		titleLabel.setFont(new Font("serif",Font.BOLD,18));
97		JPanel commentPanel = new JPanel(new GridLayout(2,1));
98		JLabel commentLabel2 = new JLabel(Resources.getTranslation("project_panel_comment"), SwingConstants.LEFT);
99		commentLabel = new JLabel(Resources.getTranslation("project_panel_comment_none"), SwingConstants.LEFT);
100		commentPanel.add(commentLabel2);
101		commentPanel.add(commentLabel);
102		topPanel.add(titleLabel);
103		topPanel.add(commentPanel);
104		JPanel centerPanel = new JPanel(new BorderLayout());
105		mainBox = new Box(BoxLayout.Y_AXIS);
106		JScrollPane scrollPane = new JScrollPane(mainBox,
107												ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
108												ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
109		centerPanel.add(scrollPane, BorderLayout.NORTH);
110		centerPanel.setBorder(BorderFactory.createEtchedBorder());
111		JPanel botPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
112		commitButton = new JButton(Resources.getTranslation("project_panel_commit_button_all"));
113		commitButton.addActionListener(this);
114		botPanel.add(commitButton);
115		add(topPanel, BorderLayout.NORTH);
116		add(centerPanel, BorderLayout.CENTER);
117		add(botPanel, BorderLayout.SOUTH);
118
119		updateComponents();
120	}
121
122	public void updateComponents() {
123		BundleItem item = gui.getSelectedProjectBundleItem();
124
125		if (item == null) {
126			commentLabel.setText(Resources.getTranslation("project_panel_comment_none"));
127			titleLabel.setText(Resources.getTranslation("project_panel_default_title"));
128			itemFields = null;
129			itemLabels = null;
130			commitButtons = null;
131			commitButton.setEnabled(false);
132		} else {
133			String comment = item.getComment();
134			String key = item.getKey();
135			commentLabel.setText(comment);
136			titleLabel.setText(Resources.getTranslation("project_panel_title", key));
137
138			RBManager manager = gui.getSelectedProjectBundle();
139			Vector bundles = manager.getBundles();
140			itemFields = new JTextField[bundles.size()];
141			itemLabels = new JLabel[bundles.size()];
142			commitButtons = new JButton[bundles.size()];
143			for (int i=0; i < bundles.size(); i++) {
144				Bundle bundle = (Bundle)bundles.elementAt(i);
145				BundleItem bundleItem = bundle.getBundleItem(key);
146				//boolean translated = bundleItem.isTranslated();
147				JLabel encodingLabel = new JLabel(Resources.getTranslation("project_panel_bundle", bundle.toString()),
148												  SwingConstants.LEFT);
149				if (bundleItem == null || !bundleItem.isTranslated()) {
150				    encodingLabel.setText(Resources.getTranslation("project_panel_bundle_untranslated",
151																	bundle.toString()));
152				}
153				String fieldText = (bundleItem == null ? Resources.getTranslation("project_panel_item_inherits") :
154														 bundleItem.getTranslation());
155				JTextField itemField = new JTextField(fieldText);
156				itemField.setMaximumSize(new Dimension(this.getSize().width-150, 200));
157				itemField.setName(key);
158				JButton commitItemButton = new JButton(Resources.getTranslation("project_panel_commit_button"));
159				commitItemButton.addActionListener(this);
160				commitItemButton.setName(bundle.encoding);
161				itemFields[i] = itemField;
162				itemLabels[i] = encodingLabel;
163				commitButtons[i] = commitItemButton;
164			}
165			commitButton.setEnabled(true);
166		}
167
168		mainBox.removeAll();
169		if (itemFields != null) {
170			for (int i=0; i < itemFields.length; i++) {
171				JPanel bundlePanel = new JPanel(new BorderLayout());
172				bundlePanel.setBorder(BorderFactory.createLineBorder(Color.darkGray));
173				bundlePanel.add(itemLabels[i], BorderLayout.NORTH);
174				bundlePanel.add(itemFields[i], BorderLayout.CENTER);
175				bundlePanel.add(commitButtons[i], BorderLayout.EAST);
176				mainBox.add(bundlePanel);
177			}
178		}
179
180		revalidate();
181	}
182}
183
184