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
)
61 linkList
= new ArrayList
<LinkBox
>();
62 box_list
= new Box(BoxLayout
.PAGE_AXIS
);
65 for (Link link
: project
.getMuseumEnvironment().linkArray()) {
66 lb
= new LinkBox(link
);
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
),
82 public void addLink (Link l
)
84 LinkBox lb
= new LinkBox(l
);
87 ((JScrollPane
) getComponent(1)).updateUI();
88 if (linkList
.size() == 1) { project
.getNavigationFrame().pack(); }
91 public void removeLink (String id
)
94 for (LinkBox lbb
: linkList
) {
95 if (lbb
.getLinkId().equals(id
)) {
99 ((JScrollPane
) getComponent(1)).updateUI();
105 public HallGraph
[] linkHalls (String id
)
107 for (LinkBox lb
: linkList
) {
108 if (lb
.getLinkId().equals(id
)) {
109 HallGraph
[] ret
= new HallGraph
[2];
111 project
.getHallView().getHallGraph(lb
.hallGraph1Id());
113 project
.getHallView().getHallGraph(lb
.hallGraph2Id());
120 public void actionPerformed (ActionEvent e
)
122 ProjectDialogue
.fireDialogue(new CreateLinkDialogue(project
));
125 public class LinkBox
extends JPanel
126 implements ActionListener
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
)
143 for (Hall h
: project
.getMuseumEnvironment().hallArray()) {
144 if (hl1
== null && h
.getDoor(l
.d1
) != null) {
146 } else if (hl2
== null && h
.getDoor(l
.d2
) != null) {
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);
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
;
187 add(new JLabel(String
.format("%s:", BnacEditor
.gs("DIMENSIONS")),
188 JLabel
.TRAILING
), c
);
191 c
.gridx
= GridBagConstraints
.RELATIVE
;
195 add(new JLabel("x", JLabel
.CENTER
), c
);
203 c
.weightx
= c
.weighty
= 1.0;
204 c
.fill
= GridBagConstraints
.BOTH
;
211 c
.gridwidth
= c
.gridheight
= 1;
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
240 protected Color hlColor
;
243 public DoorPlotCanvas (Hall hall
, Door door
,
244 int mx
, int my
, int bx
, int by
)
246 super(hall
, mx
, my
, bx
, by
);
251 wall
= hall
.getWall(hall
.getParentWall(door
.getId()));
254 public void paint (Graphics g
)
258 float width
= door
.getDimensions().x
;
259 float offset
= door
.getPosition().x
;
261 Vector2f v
= new Vector2f();
262 Point2f tmp
= new Point2f();
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
,
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
);
280 g2
.setColor(fgColor
);
284 public class RemoveLinkCommand
extends ProjectCommand
288 protected HallGraph hg1
;
290 protected DoorGraph dg1
;
292 protected HallGraph hg2
;
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");
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());
322 hg1
.addDoorGraph(widx1
, dg1
);
323 hg2
.addDoorGraph(widx2
, dg2
);
324 project
.getMuseumEnvironment().addLink(link
);
325 project
.getNavigationView().addLink(link
);