Initial commit, a stable version.
[bnac-editor.git] / src / cl.uchile.dcc.bnac.editor / NavigationView.java
blob20dce525181cf17dae1639cd0890ef01838fc960
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.Door;
22 import cl.uchile.dcc.bnac.Hall;
23 import cl.uchile.dcc.bnac.Link;
24 import cl.uchile.dcc.bnac.Wall;
26 import java.awt.BasicStroke;
27 import java.awt.BorderLayout;
28 import java.awt.Color;
29 import java.awt.Graphics2D;
30 import java.awt.Graphics;
31 import java.awt.GridBagConstraints;
32 import java.awt.GridBagLayout;
33 import java.awt.Insets;
34 import java.awt.Stroke;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.awt.image.BufferedImage;
38 import java.util.ArrayList;
39 import javax.swing.BorderFactory;
40 import javax.swing.Box;
41 import javax.swing.BoxLayout;
42 import javax.swing.ImageIcon;
43 import javax.swing.JButton;
44 import javax.swing.JLabel;
45 import javax.swing.JPanel;
46 import javax.swing.JScrollPane;
47 import javax.vecmath.Point2f;
48 import javax.vecmath.Point2i;
49 import javax.vecmath.Vector2f;
51 public class NavigationView extends ProjectView
52 implements ActionListener
54 protected ArrayList<LinkBox> linkList;
56 protected Box box_list;
58 public NavigationView (Project project)
60 super(project);
61 linkList = new ArrayList<LinkBox>();
62 box_list = new Box(BoxLayout.PAGE_AXIS);
64 LinkBox lb;
65 for (Link link: project.getMuseumEnvironment().linkArray()) {
66 lb = new LinkBox(link);
67 linkList.add(lb);
68 box_list.add(lb);
71 JButton bn = new JButton(BnacEditor.gs("CREATE_LINK"));
72 bn.addActionListener(this);
74 setLayout(new BorderLayout(0, 2));
75 add(bn, BorderLayout.NORTH);
76 add(new JScrollPane(box_list,
77 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
78 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),
79 BorderLayout.CENTER);
82 public void addLink (Link l)
84 LinkBox lb = new LinkBox(l);
85 linkList.add(lb);
86 box_list.add(lb);
87 ((JScrollPane) getComponent(1)).updateUI();
88 if (linkList.size() == 1) { project.getNavigationFrame().pack(); }
91 public void removeLink (String id)
93 LinkBox lb = null;
94 for (LinkBox lbb: linkList) {
95 if (lbb.getLinkId().equals(id)) {
96 lb = lbb;
97 box_list.remove(lb);
98 linkList.remove(lb);
99 ((JScrollPane) getComponent(1)).updateUI();
100 return;
105 public HallGraph[] linkHalls (String id)
107 for (LinkBox lb: linkList) {
108 if (lb.getLinkId().equals(id)) {
109 HallGraph[] ret = new HallGraph[2];
110 ret[0] =
111 project.getHallView().getHallGraph(lb.hallGraph1Id());
112 ret[1] =
113 project.getHallView().getHallGraph(lb.hallGraph2Id());
114 return ret;
117 return null;
120 public void actionPerformed (ActionEvent e)
122 ProjectDialogue.fireDialogue(new CreateLinkDialogue(project));
125 public class LinkBox extends JPanel
126 implements ActionListener
128 protected Link l;
129 protected JLabel lb_wid;
130 protected JLabel lb_hei;
131 protected JLabel lb_hl1;
132 protected JLabel lb_hl2;
133 protected JButton bn_edt;
134 protected JButton bn_del;
136 protected String hl1;
137 protected String hl2;
139 public LinkBox (Link l)
141 this.l = l;
143 for (Hall h: project.getMuseumEnvironment().hallArray()) {
144 if (hl1 == null && h.getDoor(l.d1) != null) {
145 hl1 = h.getId();
146 } else if (hl2 == null && h.getDoor(l.d2) != null) {
147 hl2 = h.getId();
150 if (hl1 != null && hl2 != null) { break; }
153 Hall h1 = project.getMuseumEnvironment().getHall(hl1);
154 Hall h2 = project.getMuseumEnvironment().getHall(hl2);
156 Point2f d = h1.getDoor(l.d1).getDimensions();
158 lb_wid = new JLabel(String.format("%.02f", d.x), JLabel.CENTER);
159 lb_hei = new JLabel(String.format("%.02f", d.y), JLabel.CENTER);
160 lb_hl1 = new JLabel(new ImageIcon(new DoorPlotCanvas(
161 h1, h1.getDoor(l.d1), 96, 96, 2, 2).makeImage()));
162 lb_hl2 = new JLabel(new ImageIcon(new DoorPlotCanvas(
163 h2, h2.getDoor(l.d2), 96, 96, 2, 2).makeImage()));
164 bn_edt = new JButton(BnacEditor.gs("EDIT"));
165 bn_del = new JButton(BnacEditor.gs("DELETE"));
167 bn_edt.addActionListener(this);
168 bn_del.addActionListener(this);
170 buildForm();
173 public String hallGraph1Id () { return hl1; }
174 public String hallGraph2Id () { return hl2; }
176 protected void buildForm ()
178 setLayout(new GridBagLayout());
179 GridBagConstraints c = new GridBagConstraints();
181 c.insets = new Insets(2, 5, 2, 5);
182 c.gridx = c.gridy = 0;
183 c.fill = GridBagConstraints.HORIZONTAL;
184 c.weightx = 0.0;
185 c.gridwidth = 3;
186 c.weighty = 1.0;
187 add(new JLabel(String.format("%s:", BnacEditor.gs("DIMENSIONS")),
188 JLabel.TRAILING), c);
190 c.weightx = 1.0;
191 c.gridx = GridBagConstraints.RELATIVE;
192 c.gridwidth = 1;
193 add(lb_wid, c);
194 c.weightx = 0.0;
195 add(new JLabel("x", JLabel.CENTER), c);
196 c.weightx = 1.0;
197 add(lb_hei, c);
199 c.gridx = 0;
200 c.gridy = 1;
201 c.gridwidth = 3;
202 c.gridheight = 2;
203 c.weightx = c.weighty = 1.0;
204 c.fill = GridBagConstraints.BOTH;
205 add(lb_hl1, c);
206 c.gridx = 3;
207 add(lb_hl2, c);
209 c.gridx = 6;
210 c.gridy = 1;
211 c.gridwidth = c.gridheight = 1;
212 add(bn_edt, c);
213 c.gridy = 2;
214 add(bn_del, c);
216 setBorder(BorderFactory.createEtchedBorder());
219 public void actionPerformed (ActionEvent e)
221 if (e.getSource() == bn_del) {
222 HallGraph hg1 = project.getHallView().getHallGraph(hl1);
223 int widx1 = hg1.getHall().getParentWall(l.d1);
224 DoorGraph dg1 = hg1.getDoorGraph(l.d1);
226 HallGraph hg2 = project.getHallView().getHallGraph(hl2);
227 int widx2 = hg2.getHall().getParentWall(l.d2);
228 DoorGraph dg2 = hg2.getDoorGraph(l.d2);
230 project.pushCommand(new RemoveLinkCommand(project, l,
231 hg1, widx1, dg1, hg2, widx2, dg2));
235 public String getLinkId () { return l.getId(); }
237 public class DoorPlotCanvas extends PlotCanvas
239 protected Door door;
240 protected Color hlColor;
241 protected Wall wall;
243 public DoorPlotCanvas (Hall hall, Door door,
244 int mx, int my, int bx, int by)
246 super(hall, mx, my, bx, by);
247 this.door = door;
249 hlColor = Color.RED;
251 wall = hall.getWall(hall.getParentWall(door.getId()));
254 public void paint (Graphics g)
256 super.paint(g);
258 float width = door.getDimensions().x;
259 float offset = door.getPosition().x;
261 Vector2f v = new Vector2f();
262 Point2f tmp = new Point2f();
263 v.sub(wall.end2f(),
264 wall.origin2f());
265 float d = v.length();
267 tmp.scaleAdd(offset/d, v, wall.origin2f());
268 Point2i l = coordsToPix(tmp.x, tmp.y);
269 tmp.scaleAdd((offset+width)/d, v,
270 wall.origin2f());
271 Point2i r = coordsToPix(tmp.x, tmp.y);
273 Graphics2D g2 = (Graphics2D) g;
274 Stroke os = g2.getStroke();
275 g2.setColor(hlColor);
276 g2.setStroke(new BasicStroke(4.0f, BasicStroke.CAP_SQUARE,
277 BasicStroke.JOIN_BEVEL));
278 g2.drawLine(l.x, l.y, r.x, r.y);
279 g2.setStroke(os);
280 g2.setColor(fgColor);
284 public class RemoveLinkCommand extends ProjectCommand
286 protected Link link;
288 protected HallGraph hg1;
289 protected int widx1;
290 protected DoorGraph dg1;
292 protected HallGraph hg2;
293 protected int widx2;
294 protected DoorGraph dg2;
296 public RemoveLinkCommand (Project project, Link link,
297 HallGraph hg1, int widx1, DoorGraph dg1,
298 HallGraph hg2, int widx2, DoorGraph dg2)
300 super(project, "REMOVE_LINK");
301 this.link = link;
303 this.hg1 = hg1;
304 this.widx1 = widx1;
305 this.dg1 = dg1;
307 this.hg2 = hg2;
308 this.widx2 = widx2;
309 this.dg2 = dg2;
312 public void execute ()
314 project.getMuseumEnvironment().removeLink(link.getId());
315 project.getNavigationView().removeLink(link.getId());
316 hg1.removeDoorGraph(dg1.getId());
317 hg2.removeDoorGraph(dg2.getId());
320 public void undo ()
322 hg1.addDoorGraph(widx1, dg1);
323 hg2.addDoorGraph(widx2, dg2);
324 project.getMuseumEnvironment().addLink(link);
325 project.getNavigationView().addLink(link);