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.*;
13import javax.swing.text.JTextComponent;
14
15import com.ibm.rbm.*;
16
17/**
18 * The class used to display statistics
19 */
20class RBStatisticsPanel extends JPanel {
21	RBManager rbm;
22	Bundle bundle;
23
24	// Components - Bundle
25	JLabel     jLabelStatsTitle;
26
27	JLabel     jLabelStatsName;
28	JLabel     jLabelStatsComment;
29	JLabel     jLabelStatsManager;
30	JLabel     jLabelStatsLanguage;
31	JLabel     jLabelStatsCountry;
32	JLabel     jLabelStatsVariant;
33	JLabel     jLabelStatsNumTrans;
34	JLabel     jLabelStatsNumUntrans;
35
36	JTextField jTextFieldStatsName;
37	JTextField jTextFieldStatsComment;
38	JTextField jTextFieldStatsManager;
39	JTextField jTextFieldStatsLanguage;
40	JTextField jTextFieldStatsCountry;
41	JTextField jTextFieldStatsVariant;
42
43	JButton    updateButton;
44
45	Box        boxStatsLeftRight1;
46	Box        boxStatsLeftRight2;
47
48	// Components - bundle manager
49	JLabel      titleLabel;
50	JLabel      numFileLabel;
51	JLabel      numDupLabel;
52	JLabel      numGroupLabel;
53	JLabel      numItemLabel;
54
55	JList       groupList;
56	JList       fileList;
57	JList       dupList;
58
59	JScrollPane groupScroll;
60	JScrollPane dupScroll;
61	JScrollPane fileScroll;
62
63	JPanel      filePanel;
64	JPanel      itemPanel;
65	JPanel      groupPanel;
66
67	JButton     fileButton;
68	JButton     groupButton;
69	JButton     itemButton;
70
71	Box         mainBox;
72	Box         dupBox;
73
74
75	public void setBundle(Bundle b) {
76		rbm = null;
77		if (bundle == null) {
78			bundle = b;
79			initComponents();
80		} else if (bundle != b) {
81			bundle = b;
82			updateComponents();
83		}
84	}
85
86	public void setManager(RBManager m) {
87		bundle = null;
88		if (rbm == null) {
89			rbm = m;
90			initComponents();
91		} else if (rbm != m) {
92			rbm = m;
93			updateComponents();
94		}
95	}
96
97	public void removeElements() {
98		if (rbm != null || bundle != null) {
99			rbm = null;
100			bundle = null;
101			initComponents();
102		}
103	}
104
105	public void initComponents() {
106		// Initialize components
107		if (bundle != null) {
108			RBManagerGUI.debugMsg("Initializing components for Resource File");
109			int untranslated = bundle.getUntranslatedItemsSize();
110
111			jLabelStatsTitle          = new JLabel(bundle.name);
112
113			jLabelStatsName           = new JLabel(Resources.getTranslation("languagestats_title"));
114			jLabelStatsComment        = new JLabel(Resources.getTranslation("languagestats_comment"));
115			jLabelStatsManager        = new JLabel(Resources.getTranslation("languagestats_manager"));
116			jLabelStatsLanguage       = new JLabel(Resources.getTranslation("languagestats_language"));
117			jLabelStatsCountry        = new JLabel(Resources.getTranslation("languagestats_country"));
118			jLabelStatsVariant        = new JLabel(Resources.getTranslation("languagestats_variant"));
119			jLabelStatsNumTrans       = new JLabel(Resources.getTranslation("languagestats_item_count") + " " +
120												   String.valueOf(bundle.allItems.size()));
121			jLabelStatsNumUntrans     = new JLabel(Resources.getTranslation("languagestats_translation_count") +
122												   String.valueOf(untranslated));
123
124			jTextFieldStatsName       = new JTextField((bundle.name == null ? Resources.getTranslation("untitled") : bundle.name));
125			jTextFieldStatsComment    = new JTextField((bundle.comment == null ? "" : bundle.comment));
126			jTextFieldStatsManager    = new JTextField((bundle.manager == null ? "" : bundle.manager));
127			jTextFieldStatsLanguage   = new JTextField((bundle.language == null ? "" : bundle.language),25);
128			jTextFieldStatsCountry    = new JTextField((bundle.country == null ? "" : bundle.country),25);
129			jTextFieldStatsVariant    = new JTextField((bundle.variant == null ? "" : bundle.variant),25);
130
131			boxStatsLeftRight1        = new Box(BoxLayout.X_AXIS);
132			boxStatsLeftRight2        = new Box(BoxLayout.X_AXIS);
133
134			updateButton              = new JButton(Resources.getTranslation("button_update"));
135			updateButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_update_trigger")));
136
137			// Set up the components
138			jLabelStatsTitle.setFont(new Font("SansSerif",Font.PLAIN,18));
139
140			ButtonEnablerFocusListener befl = new ButtonEnablerFocusListener(updateButton);
141
142			// Add listeners
143			updateButton.addActionListener(new ActionListener(){
144				public void actionPerformed(ActionEvent ev) {
145					updateBundleInfo();
146				}
147			});
148
149			jTextFieldStatsName.addFocusListener(befl);
150			jTextFieldStatsComment.addFocusListener(befl);
151			jTextFieldStatsManager.addFocusListener(befl);
152			jTextFieldStatsLanguage.addFocusListener(befl);
153			jTextFieldStatsCountry.addFocusListener(befl);
154			jTextFieldStatsVariant.addFocusListener(befl);
155
156			jTextFieldStatsName.setColumns(35);
157			jTextFieldStatsComment.setColumns(35);
158			jTextFieldStatsManager.setColumns(35);
159			jTextFieldStatsLanguage.setColumns(25);
160			jTextFieldStatsCountry.setColumns(25);
161			jTextFieldStatsVariant.setColumns(25);
162
163			//updateButton.setEnabled(false);
164
165			// Update the display
166			if (mainBox != null){
167				mainBox.removeAll();
168			} else {
169				mainBox = new Box(BoxLayout.Y_AXIS);
170			}
171			if (dupBox != null)
172				dupBox.removeAll();
173			removeAll();
174			mainBox.add(jLabelStatsTitle);
175			mainBox.add(Box.createVerticalStrut(10));
176			mainBox.add(jLabelStatsName);
177			mainBox.add(jTextFieldStatsName);
178			mainBox.add(Box.createVerticalStrut(5));
179			mainBox.add(jLabelStatsComment);
180			mainBox.add(jTextFieldStatsComment);
181			mainBox.add(Box.createVerticalStrut(5));
182			mainBox.add(jLabelStatsManager);
183			mainBox.add(jTextFieldStatsManager);
184			mainBox.add(Box.createVerticalStrut(5));
185			mainBox.add(jLabelStatsLanguage);
186			mainBox.add(jTextFieldStatsLanguage);
187			mainBox.add(Box.createVerticalStrut(5));
188			mainBox.add(jLabelStatsCountry);
189			mainBox.add(jTextFieldStatsCountry);
190			mainBox.add(Box.createVerticalStrut(5));
191			mainBox.add(jLabelStatsVariant);
192			mainBox.add(jTextFieldStatsVariant);
193			mainBox.add(Box.createVerticalStrut(5));
194			mainBox.add(jLabelStatsNumTrans);
195			mainBox.add(Box.createVerticalStrut(5));
196			mainBox.add(jLabelStatsNumUntrans);
197			mainBox.add(Box.createVerticalStrut(10));
198			mainBox.add(updateButton);
199			mainBox.add(Box.createHorizontalGlue());
200			if (!(getLayout() instanceof FlowLayout)) {
201				setLayout(new FlowLayout());
202			}
203			add(mainBox);
204		} else if (rbm != null) {
205			RBManagerGUI.debugMsg("Initializing components for Resource Bundle");
206			titleLabel          = new JLabel(rbm.getBaseClass() + " - " + Resources.getTranslation("baseclass"));
207
208			numFileLabel        = new JLabel(Resources.getTranslation("basestats_file_count") + " " + rbm.getNumberLanguages());
209			numGroupLabel       = new JLabel(Resources.getTranslation("basestats_group_count") + " " + rbm.getNumberGroups());
210			numItemLabel        = new JLabel(Resources.getTranslation("basestats_item_count") + " " + rbm.getNumberTotalTranslations());
211			numDupLabel         = new JLabel(Resources.getTranslation("basestats_duplicates_count") + " " + rbm.getNumberDuplicates());
212
213			fileList            = new JList(rbm.getLanguageListingVector());
214			groupList           = new JList(rbm.getGroupListingVector());
215			dupList             = new JList(rbm.getDuplicatesListingVector());
216
217			fileButton          = new JButton(Resources.getTranslation("button_add_file"));
218			groupButton         = new JButton(Resources.getTranslation("button_add_group"));
219			itemButton          = new JButton(Resources.getTranslation("button_add_resource"));
220
221			filePanel           = new JPanel();
222			groupPanel          = new JPanel();
223			itemPanel           = new JPanel();
224
225			fileScroll          = new JScrollPane(fileList);
226			groupScroll         = new JScrollPane(groupList);
227			dupScroll           = new JScrollPane(dupList);
228
229			if (mainBox == null) {
230				mainBox         = new Box(BoxLayout.Y_AXIS);
231			} else {
232				mainBox.removeAll();
233			}
234			dupBox              = new Box(BoxLayout.Y_AXIS);
235
236			// Set up the components
237			filePanel.setLayout(new BorderLayout());
238			groupPanel.setLayout(new BorderLayout());
239			itemPanel.setLayout(new BorderLayout());
240
241			filePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
242																 Resources.getTranslation("basestats_file_group")));
243			groupPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
244																 Resources.getTranslation("basestats_group_group")));
245			itemPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
246																 Resources.getTranslation("basestats_item_group")));
247
248			titleLabel.setFont(new Font("SansSerif",Font.PLAIN,18));
249
250			fileButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_add_file_trigger")));
251			groupButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_add_group_trigger")));
252			itemButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_add_resource_trigger")));
253
254			// Add listeners
255			fileButton.addActionListener(new ActionListener(){
256				public void actionPerformed(ActionEvent ev) {
257					Container c = ((JButton)ev.getSource()).getParent();
258					RBManagerGUI gui = null;
259					while (!(c.getParent() instanceof RBManagerGUI)) c = c.getParent();
260					gui = (RBManagerGUI)c.getParent();
261					gui.createResourceFile();
262				}
263			});
264
265			groupButton.addActionListener(new ActionListener(){
266				public void actionPerformed(ActionEvent ev) {
267					Container c = ((JButton)ev.getSource()).getParent();
268					RBManagerGUI gui = null;
269					while (!(c.getParent() instanceof RBManagerGUI)) c = c.getParent();
270					gui = (RBManagerGUI)c.getParent();
271					gui.createBundleGroup();
272				}
273			});
274
275			itemButton.addActionListener(new ActionListener(){
276				public void actionPerformed(ActionEvent ev) {
277					Container c = ((JButton)ev.getSource()).getParent();
278					RBManagerGUI gui = null;
279					while (!(c.getParent() instanceof RBManagerGUI)) c = c.getParent();
280					gui = (RBManagerGUI)c.getParent();
281					gui.createBundleItem();
282				}
283			});
284
285			// Update the display
286			filePanel.removeAll();
287			filePanel.add(numFileLabel, BorderLayout.NORTH);
288			filePanel.add(fileScroll, BorderLayout.CENTER);
289			filePanel.add(fileButton, BorderLayout.SOUTH);
290
291			groupPanel.removeAll();
292			groupPanel.add(numGroupLabel, BorderLayout.NORTH);
293			groupPanel.add(groupScroll, BorderLayout.CENTER);
294			groupPanel.add(groupButton, BorderLayout.SOUTH);
295
296			dupBox.removeAll();
297			dupBox.add(numDupLabel);
298			dupBox.add(dupScroll);
299
300			itemPanel.removeAll();
301			itemPanel.add(numItemLabel, BorderLayout.NORTH);
302			itemPanel.add(dupBox, BorderLayout.CENTER);
303			itemPanel.add(itemButton, BorderLayout.SOUTH);
304
305			mainBox.removeAll();
306			mainBox.add(titleLabel);
307			mainBox.add(Box.createVerticalStrut(10));
308			mainBox.add(filePanel);
309			mainBox.add(Box.createVerticalStrut(10));
310			mainBox.add(groupPanel);
311			mainBox.add(Box.createVerticalStrut(10));
312			mainBox.add(itemPanel);
313
314			removeAll();
315			if (!(getLayout() instanceof BorderLayout))
316				setLayout(new BorderLayout());
317			add(mainBox, BorderLayout.CENTER);
318			updateComponents();
319		} else {
320			removeAll();
321		}
322		repaint();
323	}
324
325	public void updateComponents() {
326		if (bundle != null) {
327			int untranslated = bundle.getUntranslatedItemsSize();
328
329			jLabelStatsTitle.setText(bundle.name);
330
331			jTextFieldStatsName.setText(bundle.name == null ? Resources.getTranslation("untitled") : bundle.name);
332			jTextFieldStatsComment.setText(bundle.comment == null ? "" : bundle.comment);
333			jTextFieldStatsManager.setText(bundle.manager == null ? "" : bundle.manager);
334			jTextFieldStatsLanguage.setText(bundle.language == null ? "" : bundle.language);
335			jTextFieldStatsCountry.setText(bundle.country == null ? "" : bundle.country);
336			jTextFieldStatsVariant.setText(bundle.variant == null ? "" : bundle.variant);
337			jLabelStatsNumTrans.setText(Resources.getTranslation("languagestats_item_count") + " " +
338					   String.valueOf(bundle.allItems.size()));
339			jLabelStatsNumUntrans.setText(Resources.getTranslation("languagestats_translation_count") +
340					   String.valueOf(untranslated));
341		} else if (rbm == null) {
342			removeAll();
343		}
344
345	}
346
347	void updateBundleInfo() {
348		bundle.name     = jTextFieldStatsName.getText().trim();
349		bundle.comment  = jTextFieldStatsComment.getText().trim();
350		bundle.manager  = jTextFieldStatsManager.getText().trim();
351		bundle.language = jTextFieldStatsLanguage.getText().trim();
352		bundle.country  = jTextFieldStatsCountry.getText().trim();
353		bundle.variant  = jTextFieldStatsVariant.getText().trim();
354		updateButton.setEnabled(false);
355	}
356
357	public RBStatisticsPanel() {
358		super();
359		bundle = null;
360		rbm = null;
361	}
362
363}
364
365class ButtonEnablerFocusListener implements FocusListener {
366	JButton button;
367	String beforeText = null;
368
369	public ButtonEnablerFocusListener(JButton button) {
370		super();
371		this.button = button;
372	}
373
374	public void focusGained(FocusEvent ev) {
375		Object o = ev.getSource();
376		if (o instanceof JTextComponent) {
377			JTextComponent jtc = (JTextComponent)o;
378			beforeText = jtc.getText();
379		}
380	}
381
382	public void focusLost(FocusEvent ev) {
383		Object o = ev.getSource();
384		if (o instanceof JTextComponent) {
385			JTextComponent jtc = (JTextComponent)o;
386			String afterText = jtc.getText();
387			if (!afterText.equals(beforeText)) button.setEnabled(true);
388		} else button.setEnabled(true);
389	}
390}
391
392