Initial commit, a stable version.
[bnac-editor.git] / src / cl.uchile.dcc.bnac.editor / Project.java
blob8e86218f0012e239205b81fb718165f2e00a11a0
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.*;
23 import java.util.Stack;
25 import java.io.File;
26 import java.io.IOException;
28 import java.awt.event.KeyEvent;
29 import java.awt.event.KeyListener;
30 import java.awt.event.WindowEvent;
31 import java.awt.event.WindowListener;
33 import javax.swing.Action;
34 import javax.swing.JFrame;
36 import javax.vecmath.Point2f;
38 import javax.xml.parsers.ParserConfigurationException;
39 import org.xml.sax.SAXException;
41 public class Project
42 implements WindowListener, KeyListener
44 protected File root;
45 protected MuseumEnvironment me;
46 protected InfoSet info;
47 protected HallGraph curhg;
48 protected Stack<ProjectCommand> cmdUndo;
49 protected Stack<ProjectCommand> cmdRedo;
51 protected SimulationView simv;
52 protected PlotView plotv;
53 protected CaptureView capv;
54 protected HallView hallv;
55 protected NavigationView navv;
56 protected RouteView rotv;
58 protected JFrame simf;
59 protected JFrame pltf;
60 protected JFrame capf;
61 protected JFrame half;
62 protected JFrame navf;
63 protected JFrame rotf;
65 protected Project (File root, MuseumEnvironment me)
67 this.root = root;
68 this.me = me;
69 info = new InfoSet();
70 curhg = null;
71 cmdUndo = new Stack<ProjectCommand>();
72 cmdRedo = new Stack<ProjectCommand>();
74 initViews();
76 if (me.hallArray().length > 0) {
77 setCurrentHall(me.hallArray()[0].getId());
81 protected void initViews ()
83 String[] strs = {
84 "SIMULATIONV", "PLOTV", "CAPTUREV", "HALLV", "NAVIGATIONV",
85 "ROUTEV"
87 JFrame[] jfs = new JFrame[strs.length];
89 for (int i=0; i<jfs.length; ++i) {
90 jfs[i] = new JFrame(Util.gs(strs[i]));
91 jfs[i].setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
92 jfs[i].addWindowListener(this);
95 simv = new SimulationView(this, jfs[0].getGraphicsConfiguration());
96 simf = jfs[0];
97 simf.add(simv);
98 simf.pack();
100 plotv = new PlotView(this);
101 pltf = jfs[1];
102 pltf.add(plotv);
103 pltf.pack();
105 capv = new CaptureView(this);
106 capf = jfs[2];
107 capf.add(capv);
108 capf.pack();
110 hallv = new HallView(this);
111 half = jfs[3];
112 half.add(hallv);
113 half.pack();
115 navv = new NavigationView(this);
116 navf = jfs[4];
117 navf.add(navv);
118 navf.pack();
120 rotv = new RouteView(this);
121 rotf = jfs[5];
122 rotf.add(rotv);
123 rotf.pack();
126 protected Project (String path)
127 throws IOException, SAXException, ParserConfigurationException
129 this(new File(path),
130 MuseumEnvironment.fromFile(
131 new File(path + System.getProperty("file.separator") +
132 "project.xml")));
133 File infofile = new File(path, "info.xml");
134 if (infofile.exists()) {
135 info = InfoSet.fromFile(infofile);
136 } else {
137 info = new InfoSet();
141 protected Project (MuseumEnvironment mm)
143 this(null, mm);
146 public void pushCommand (ProjectCommand pc)
148 BnacEditor.debug("push %s", pc.getName());
149 pc.execute();
150 cmdUndo.push(pc);
151 cmdRedo.clear();
152 updateActions();
155 public void undo ()
157 if (cmdUndo.size() > 0) {
158 BnacEditor.debug("undo %s", cmdUndo.peek().getName());
159 cmdUndo.peek().undo();
160 cmdRedo.push(cmdUndo.pop());
161 updateActions();
165 public void redo ()
167 if (cmdRedo.size() > 0) {
168 BnacEditor.debug("redo %s", cmdRedo.peek().getName());
169 cmdRedo.peek().execute();
170 cmdUndo.push(cmdRedo.pop());
171 updateActions();
175 public boolean close () { return true; }
177 public String toString () { return me.get("Title"); }
179 public HallGraph getCurrentHallGraph () { return curhg; }
180 public void setCurrentHall (String id)
182 curhg = (id!=null) ? hallv.getHallGraph(id) : null;
183 simv.setHallGraph(curhg);
184 plotv.setHallGraph(curhg);
185 hallv.setSelectedHallBox(id);
188 public void addObject (CObject obj)
190 me.addObject(obj);
193 public void addCapture (Capture cap)
195 me.addCapture(cap);
196 capv.addCapture(cap);
197 if (me.captureArray().length == 1) { capf.pack(); }
200 public void addHallGraph (HallGraph hg)
202 me.addHall(hg.getHall());
203 hallv.addHallGraph(hg);
204 if (me.hallArray().length == 1) { half.pack(); }
206 public HallGraph removeHallGraph (String id)
208 Hall hall = (id!=null) ? me.removeHall(id) : null;
209 if (hall == null) { return null; }
210 HallGraph hg = hallv.removeHallGraph(id);
211 if (curhg.getId().equals(id)) {
212 if (me.hallArray().length > 0) {
213 setCurrentHall(me.hallArray()[0].getId());
214 } else {
215 setCurrentHall(null);
218 return hg;
221 public String getRootPath ()
223 return (root!=null) ? root.getAbsolutePath() : null;
225 public void setRootPath (String path)
227 root = new File(path);
230 public void save () throws IOException
232 File xml;
234 xml = new File(root, "project.xml");
235 MemlWriter.write(me, xml);
237 xml = new File(root, "info.xml");
238 InfoWriter.write(info, xml);
240 BnacEditor.info("project %s saved to %s", toString(),
241 root.getAbsolutePath());
244 public MuseumEnvironment getMuseumEnvironment () { return me; }
246 static public Project fromPath (String path)
247 throws IOException, SAXException, ParserConfigurationException
249 return new Project(path);
252 public SimulationView getSimulationView () { return simv; }
253 public PlotView getPlotView () { return plotv; }
254 public CaptureView getCaptureView () { return capv; }
255 public HallView getHallView () { return hallv; }
256 public NavigationView getNavigationView () { return navv; }
258 public JFrame getNavigationFrame () { return navf; }
260 public void setSimulationViewVisible (boolean b) { simf.setVisible(b); }
261 public void setPlotViewVisible (boolean b) { pltf.setVisible(b); }
262 public void setCaptureViewVisible (boolean b) { capf.setVisible(b); }
263 public void setHallViewVisible (boolean b) { half.setVisible(b); }
264 public void setNavigationViewVisible (boolean b) { navf.setVisible(b); }
265 public void setRouteViewVisible (boolean b) { rotf.setVisible(b); }
267 static public Project createProject (File root, String title)
269 return new Project(root, new MuseumEnvironment(title));
272 public void addPlacedCapture (Capture cap)
275 curHall.getWall(0).addPlacedCapture(
276 new PlacedCapture(
277 String.format("newcap-%d",
278 (int) System.currentTimeMillis()),
279 cap));
283 public void activate () { updateActions(); }
285 protected void updateActions ()
287 Action undo = BnacEditor.getUndoAction();
288 Action redo = BnacEditor.getRedoAction();
290 if (cmdUndo.size() == 0) {
291 undo.setEnabled(false);
292 undo.putValue(Action.SHORT_DESCRIPTION, Util.gs("UNDO_SD"));
293 } else {
294 undo.setEnabled(true);
295 undo.putValue(Action.SHORT_DESCRIPTION, String.format("%s (%s)",
296 Util.gs("UNDO_SD"),
297 Util.gs(cmdUndo.peek().getName().toUpperCase())));
300 if (cmdRedo.size() == 0) {
301 redo.setEnabled(false);
302 redo.putValue(Action.SHORT_DESCRIPTION, Util.gs("REDO_SD"));
303 } else {
304 redo.setEnabled(true);
305 redo.putValue(Action.SHORT_DESCRIPTION, String.format("%s (%s)",
306 Util.gs("REDO_SD"),
307 Util.gs(cmdRedo.peek().getName().toUpperCase())));
311 public void keyPressed (KeyEvent e)
313 int kc = e.getKeyCode();
314 boolean ctrl = e.isControlDown();
315 boolean shft = e.isShiftDown();
316 boolean alt = e.isAltDown();
318 if (ctrl && !shft && !alt && kc == KeyEvent.VK_Z) { undo(); }
319 if (ctrl && shft && !alt && kc == KeyEvent.VK_Z) { redo(); }
320 if (!ctrl && !shft && alt) {
321 JFrame jf = null;
322 switch (kc) {
323 case KeyEvent.VK_1:
324 jf = simf;
325 break;
326 case KeyEvent.VK_2:
327 jf = capf;
328 break;
329 case KeyEvent.VK_3:
330 jf = half;
331 break;
332 case KeyEvent.VK_4:
333 jf = pltf;
334 break;
335 case KeyEvent.VK_5:
336 jf = navf;
337 break;
338 case KeyEvent.VK_6:
339 jf = rotf;
340 break;
341 default:
342 break;
344 if (jf != null) {
345 jf.setVisible(true);
346 jf.toFront();
351 public void keyReleased (KeyEvent e) { }
352 public void keyTyped (KeyEvent e) { }
354 public void windowActivated (WindowEvent e)
356 if (BnacEditor.autoFocus()) { BnacEditor.selectProject(this); }
359 public void windowClosing (WindowEvent e) { }
360 public void windowClosed (WindowEvent e) { }
361 public void windowDeactivated (WindowEvent e) { }
362 public void windowDeiconified (WindowEvent e) { }
363 public void windowIconified (WindowEvent e) { }
364 public void windowOpened (WindowEvent e) { }
366 public InfoSet getInfoSet () { return info; }