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
;
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
;
42 implements WindowListener
, KeyListener
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
)
71 cmdUndo
= new Stack
<ProjectCommand
>();
72 cmdRedo
= new Stack
<ProjectCommand
>();
76 if (me
.hallArray().length
> 0) {
77 setCurrentHall(me
.hallArray()[0].getId());
81 protected void initViews ()
84 "SIMULATIONV", "PLOTV", "CAPTUREV", "HALLV", "NAVIGATIONV",
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());
100 plotv
= new PlotView(this);
105 capv
= new CaptureView(this);
110 hallv
= new HallView(this);
115 navv
= new NavigationView(this);
120 rotv
= new RouteView(this);
126 protected Project (String path
)
127 throws IOException
, SAXException
, ParserConfigurationException
130 MuseumEnvironment
.fromFile(
131 new File(path
+ System
.getProperty("file.separator") +
133 File infofile
= new File(path
, "info.xml");
134 if (infofile
.exists()) {
135 info
= InfoSet
.fromFile(infofile
);
137 info
= new InfoSet();
141 protected Project (MuseumEnvironment mm
)
146 public void pushCommand (ProjectCommand pc
)
148 BnacEditor
.debug("push %s", pc
.getName());
157 if (cmdUndo
.size() > 0) {
158 BnacEditor
.debug("undo %s", cmdUndo
.peek().getName());
159 cmdUndo
.peek().undo();
160 cmdRedo
.push(cmdUndo
.pop());
167 if (cmdRedo
.size() > 0) {
168 BnacEditor
.debug("redo %s", cmdRedo
.peek().getName());
169 cmdRedo
.peek().execute();
170 cmdUndo
.push(cmdRedo
.pop());
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
)
193 public void addCapture (Capture 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());
215 setCurrentHall(null);
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
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(
277 String.format("newcap-%d",
278 (int) System.currentTimeMillis()),
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"));
294 undo
.setEnabled(true);
295 undo
.putValue(Action
.SHORT_DESCRIPTION
, String
.format("%s (%s)",
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"));
304 redo
.setEnabled(true);
305 redo
.putValue(Action
.SHORT_DESCRIPTION
, String
.format("%s (%s)",
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
) {
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
; }