Initial commit, a stable version.
[bnac-editor.git] / src / cl.uchile.dcc.bnac.editor / TranslateTool.java
blob0dae74294afc1ff0ecba3d9724e8abd29860b0da
1 /*
2 * bnac : virtual museum environment creation/manipulation project
3 * Copyright (C) 2010 Felipe CaƱas Sabat
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package cl.uchile.dcc.bnac.editor;
21 import cl.uchile.dcc.bnac.CObject;
22 import cl.uchile.dcc.bnac.Capture;
23 import cl.uchile.dcc.bnac.MuseumEnvironment;
24 import cl.uchile.dcc.bnac.Route;
26 import java.awt.BorderLayout;
27 import java.awt.FlowLayout;
28 import java.awt.GridBagConstraints;
29 import java.awt.GridBagLayout;
30 import java.awt.event.ActionEvent;
31 import java.awt.event.ActionListener;
32 import java.awt.event.ItemEvent;
33 import java.awt.event.ItemListener;
34 import javax.swing.BorderFactory;
35 import javax.swing.DefaultComboBoxModel;
36 import javax.swing.JButton;
37 import javax.swing.JComboBox;
38 import javax.swing.JLabel;
39 import javax.swing.JPanel;
40 import javax.swing.JTextField;
41 import javax.swing.SwingUtilities;
43 public class TranslateTool extends JPanel
44 implements ActionListener, ItemListener
46 protected Project project;
48 protected JPanel pnMain;
49 protected JComboBox cbType;
50 protected JButton bnClose;
52 public TranslateTool (Project project)
54 super(new BorderLayout());
55 this.project = project;
57 pnMain = new JPanel();
58 cbType = new JComboBox();
59 bnClose = new JButton(BnacEditor.gs("CLOSE"));
61 buildUi();
63 cbType.setModel(new DefaultComboBoxModel(Type.values()));
65 pnMain.add(new CaptureTranslateBox());
67 cbType.addItemListener(this);
68 bnClose.addActionListener(this);
71 protected void buildUi ()
73 setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
74 pnMain.setBorder(BorderFactory.createEtchedBorder());
76 JPanel caca = new JPanel(new FlowLayout(FlowLayout.LEADING, 2, 2));
77 caca.add(new JLabel(String.format("%s:",
78 BnacEditor.gs("TYPE_TO_TRANSLATE"))));
79 caca.add(cbType);
80 add(caca, BorderLayout.NORTH);
81 add(pnMain, BorderLayout.CENTER);
82 add(bnClose, BorderLayout.SOUTH);
85 public void actionPerformed (ActionEvent e)
87 if (e.getSource() == bnClose) {
88 SwingUtilities.getWindowAncestor(this).setVisible(false);
92 public void itemStateChanged (ItemEvent e)
94 if (e.getStateChange() == ItemEvent.SELECTED) {
95 pnMain.removeAll();
96 Type t = (Type) e.getItem();
97 switch (t) {
98 case Capture:
99 pnMain.add(new CaptureTranslateBox());
100 break;
101 case Route:
102 pnMain.add(new RouteTranslateBox());
103 break;
105 SwingUtilities.getWindowAncestor(this).pack();
109 class CaptureTranslateBox extends JPanel
110 implements ActionListener, ItemListener
112 protected Language lang;
114 protected JComboBox cbCaps;
115 protected JComboBox cbLangs;
116 protected JTextField tfAuthor;
117 protected JTextField tfTitle;
118 protected JTextField tfType;
119 protected JButton bnSave;
120 protected JButton bnOrig;
122 public CaptureTranslateBox ()
124 super(new GridBagLayout());
126 lang = Language.values()[0];
128 cbCaps = new JComboBox();
129 cbLangs = new JComboBox();
130 tfAuthor = new JTextField();
131 tfTitle = new JTextField();
132 tfType = new JTextField();
133 bnSave = new JButton(BnacEditor.gs("SAVE"));
134 bnOrig = new JButton(BnacEditor.gs("ORIGINAL_VALUES"));
136 buildUi();
138 String[] caps =
139 new String[project.getMuseumEnvironment().captureNo()];
140 int i=0;
141 for (Capture ca: project.getMuseumEnvironment().captureArray()) {
142 caps[i++] = String.format("%s - %s",
143 ca.getReferencedObject().get("Author"),
144 ca.getReferencedObject().get("Title"));
146 cbCaps.setModel(new DefaultComboBoxModel(caps));
148 cbLangs.setModel(new DefaultComboBoxModel(Language.values()));
150 cbCaps.addItemListener(this);
151 cbLangs.addItemListener(this);
152 bnOrig.addActionListener(this);
153 bnSave.addActionListener(this);
155 fillCaptureInfo();
158 protected void buildUi ()
160 pnMain.removeAll();
162 pnMain.setLayout(new GridBagLayout());
163 GridBagConstraints c = new GridBagConstraints();
165 c.ipadx = 5;
166 c.ipady = 5;
167 c.fill = GridBagConstraints.HORIZONTAL;
169 c.gridx = 0;
170 c.gridy = 0;
171 c.gridwidth = 2;
172 add(cbCaps, c);
173 c.gridx = GridBagConstraints.RELATIVE;
174 add(cbLangs, c);
176 c.anchor = GridBagConstraints.WEST;
177 c.gridwidth = 1;
178 c.gridx = 0;
179 c.gridy = 1;
180 add(new JLabel(BnacEditor.gs("AUTHOR")), c);
181 c.gridy = GridBagConstraints.RELATIVE;
182 add(new JLabel(BnacEditor.gs("TITLE")), c);
183 add(new JLabel(BnacEditor.gs("TYPE")), c);
185 c.gridwidth = 3;
186 c.gridx = 1;
187 c.gridy = 1;
188 add(tfAuthor, c);
189 c.gridy = GridBagConstraints.RELATIVE;
190 add(tfTitle, c);
191 add(tfType, c);
193 c.gridwidth = 1;
194 c.gridx = 2;
195 c.gridy = 4;
196 add(bnOrig, c);
198 c.gridwidth = 1;
199 c.gridx = 3;
200 c.gridy = 4;
201 add(bnSave, c);
203 setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
206 public void actionPerformed (ActionEvent e)
208 Capture c = project.getMuseumEnvironment().
209 captureArray()[cbCaps.getSelectedIndex()];
210 CObject obj = c.getReferencedObject();
211 if (e.getSource() == bnOrig) {
212 tfAuthor.setText(obj.get("Author"));
213 tfTitle.setText(obj.get("Title"));
214 tfType.setText(obj.get("Type"));
215 bnSave.doClick();
216 } else if (e.getSource() == bnSave) {
217 project.getMuseumEnvironment().addString(
218 obj.getId(), lang.getCode(),
219 "Author", tfAuthor.getText());
220 project.getMuseumEnvironment().addString(
221 obj.getId(), lang.getCode(),
222 "Title", tfTitle.getText());
223 project.getMuseumEnvironment().addString(
224 obj.getId(), lang.getCode(),
225 "Type", tfType.getText());
229 public void itemStateChanged (ItemEvent e)
231 if (e.getStateChange() == ItemEvent.SELECTED) {
232 if (e.getSource() == cbCaps) {
233 fillCaptureInfo();
234 } else if (e.getSource() == cbLangs) {
235 lang = (Language) e.getItem();
236 fillCaptureInfo();
241 public void fillCaptureInfo ()
243 Capture c = project.getMuseumEnvironment().
244 captureArray()[cbCaps.getSelectedIndex()];
245 CObject obj = c.getReferencedObject();
246 String id = obj.getId();
247 String l = lang.getCode();
248 MuseumEnvironment me = project.getMuseumEnvironment();
249 if (me.getString(id, l, "Author") != null) {
250 tfAuthor.setText(me.getString(id, l, "Author"));
251 } else {
252 tfAuthor.setText("");
254 if (me.getString(id, l, "Title") != null) {
255 tfTitle.setText(me.getString(id, l, "Title"));
256 } else {
257 tfTitle.setText("");
259 if (me.getString(id, l, "Type") != null) {
260 tfType.setText(me.getString(id, l, "Type"));
261 } else {
262 tfType.setText("");
267 class RouteTranslateBox extends JPanel
268 implements ActionListener, ItemListener
270 protected Language lang;
271 protected JComboBox cbRoutes;
272 protected JComboBox cbLangs;
273 protected JTextField tfName;
274 protected JButton bnSave;
276 public RouteTranslateBox ()
278 super(new GridBagLayout());
280 lang = Language.values()[0];
281 cbLangs = new JComboBox();
282 cbRoutes = new JComboBox();
283 tfName = new JTextField(20);
284 bnSave = new JButton(BnacEditor.gs("SAVE"));
286 buildUi();
288 String[] rts = new
289 String[project.getMuseumEnvironment().routeArray().length];
290 int i=0;
291 for (Route r: project.getMuseumEnvironment().routeArray()) {
292 rts[i++] = r.getId();
294 cbRoutes.setModel(new DefaultComboBoxModel(rts));
295 cbLangs.setModel(new DefaultComboBoxModel(Language.values()));
297 cbRoutes.addItemListener(this);
298 cbLangs.addItemListener(this);
299 bnSave.addActionListener(this);
301 fillRouteInfo();
304 protected void buildUi ()
306 GridBagConstraints c = new GridBagConstraints();
308 c.ipadx = 5;
309 c.ipady = 5;
310 c.fill = GridBagConstraints.HORIZONTAL;
312 c.gridwidth = 2;
313 c.gridx = 0;
314 c.gridy = 0;
315 add(cbRoutes, c);
317 c.gridwidth = 2;
318 c.gridx = 2;
319 c.gridy = 0;
320 add(cbLangs, c);
322 c.gridwidth = 1;
323 c.gridx = 0;
324 c.gridy = 1;
325 add(new JLabel(BnacEditor.gs("NAME")), c);
326 c.gridwidth = 3;
327 c.gridx = 1;
328 add(tfName, c);
330 c.gridwidth = 1;
331 c.gridx = 3;
332 c.gridy = 2;
333 add(bnSave, c);
335 setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
338 public void actionPerformed (ActionEvent e)
340 Route route = project.getMuseumEnvironment().routeArray()[
341 cbRoutes.getSelectedIndex()];
342 if (e.getSource() == bnSave) {
343 project.getMuseumEnvironment().addString(
344 route.getId(), lang.getCode(),
345 "Name", tfName.getText());
349 public void itemStateChanged (ItemEvent e)
351 if (e.getStateChange() == ItemEvent.SELECTED) {
352 if (e.getSource() == cbRoutes) {
353 fillRouteInfo();
354 } else if (e.getSource() == cbLangs) {
355 lang = (Language) e.getItem();
356 fillRouteInfo();
361 public void fillRouteInfo ()
363 Route route = project.getMuseumEnvironment().routeArray()[
364 cbRoutes.getSelectedIndex()];
365 String s = project.getMuseumEnvironment().getString(
366 route.getId(), lang.getCode(), "Name");
367 if (s != null) {
368 tfName.setText(s);
369 } else {
370 tfName.setText("");
375 public enum Type
377 Capture,
378 Route;