7fd59977 |
1 | |
2 | //Title: OpenCASCADE Samples |
3 | //Version: |
4 | //Copyright: Copyright (c) 1999 |
5 | //Author: User Interface Group (Nizhny Novgorod) |
6 | //Company: Matra Datavision |
7 | //Description: |
8 | |
9 | |
10 | import java.awt.*; |
11 | import java.awt.event.*; |
12 | import javax.swing.*; |
13 | import java.util.*; |
14 | import jcas.Standard_CString; |
15 | import CASCADESamplesJni.*; |
16 | import SamplesTopologyJni.*; |
17 | import util.*; |
18 | |
19 | public class SampleTopologyPrimitivesPanel extends SamplePanel |
20 | implements ActionListener, |
21 | MouseListener, |
22 | MouseMotionListener |
23 | { |
24 | //-----------------------------------------------------------// |
25 | // Resources |
26 | //-----------------------------------------------------------// |
27 | static protected ResourceBundle resGui = |
28 | ResourceBundle.getBundle("SampleTopologyPrimitives"); |
29 | |
30 | static protected ImageIcon imgBox = new ImageIcon(resGui.getString("Icon-Box")); |
31 | static protected ImageIcon imgCylinder = new ImageIcon(resGui.getString("Icon-Cylinder")); |
32 | static protected ImageIcon imgCone = new ImageIcon(resGui.getString("Icon-Cone")); |
33 | static protected ImageIcon imgSphere = new ImageIcon(resGui.getString("Icon-Sphere")); |
34 | static protected ImageIcon imgTorus = new ImageIcon(resGui.getString("Icon-Torus")); |
35 | static protected ImageIcon imgWedge = new ImageIcon(resGui.getString("Icon-Wedge")); |
36 | static protected ImageIcon imgPrism = new ImageIcon(resGui.getString("Icon-Prism")); |
37 | static protected ImageIcon imgRevol = new ImageIcon(resGui.getString("Icon-Revol")); |
38 | static protected ImageIcon imgPipe = new ImageIcon(resGui.getString("Icon-Pipe")); |
39 | static protected ImageIcon imgThru = new ImageIcon(resGui.getString("Icon-Thru")); |
40 | static protected ImageIcon imgEvolved = new ImageIcon(resGui.getString("Icon-Evolved")); |
41 | static protected ImageIcon imgDraft = new ImageIcon(resGui.getString("Icon-Draft")); |
42 | |
43 | |
44 | //-----------------------------------------------------------// |
45 | // Components |
46 | //-----------------------------------------------------------// |
47 | private ViewPanel myView3d; |
48 | |
49 | private static V3d_Viewer myViewer3d = null; |
50 | |
51 | private static AIS_InteractiveContext myAISContext = null; |
52 | |
53 | private int startX = 0, startY = 0; |
54 | private boolean Dragged = false; |
55 | |
56 | |
57 | //-----------------------------------------------------------// |
58 | // External access |
59 | //-----------------------------------------------------------// |
60 | public static V3d_Viewer getViewer3d() |
61 | { |
62 | return myViewer3d; |
63 | } |
64 | |
65 | public static AIS_InteractiveContext getAISContext() |
66 | { |
67 | return myAISContext; |
68 | } |
69 | |
70 | |
71 | //=======================================================================// |
72 | // Constructor // |
73 | //=======================================================================// |
74 | public SampleTopologyPrimitivesPanel() |
75 | { |
76 | // 3D Initialization |
77 | //------------------------------------------ |
78 | myViewer3d.SetDefaultLights(); |
79 | myViewer3d.SetLightOn(); |
80 | |
81 | if (myAISContext == null) |
82 | { |
83 | myAISContext = new AIS_InteractiveContext(myViewer3d); |
84 | myAISContext.SetDisplayMode(AIS_DisplayMode.AIS_Shaded, false); |
85 | } |
86 | } |
87 | |
88 | //-----------------------------------------------------------------------// |
89 | public JPanel createViewPanel() |
90 | { |
91 | JPanel mainPanel = new JPanel(); |
92 | mainPanel.setLayout(new GridBagLayout()); |
93 | mainPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 0)); |
94 | |
95 | // Viewer 3D |
96 | //------------------------------------------ |
97 | myViewer3d = SamplesTopologyPackage.CreateViewer("TopologyPrimitives"); |
98 | |
99 | myView3d = new ViewPanel() { |
100 | public ViewCanvas createViewPort() |
101 | { |
102 | return new CASCADEView3d(SampleTopologyPrimitivesPanel.getViewer3d()) { |
103 | public void setWindow3d(V3d_View view, int hiwin, int lowin) { |
104 | SamplesTopologyPackage.SetWindow(view, hiwin, lowin); |
105 | } |
106 | }; |
107 | } |
108 | }; |
109 | |
110 | myView3d.addMouseListener(this); |
111 | myView3d.addMouseMotionListener(this); |
112 | |
113 | |
114 | // Layout |
115 | //------------------------------------------ |
116 | mainPanel.add(myView3d, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, |
117 | GridBagConstraints.CENTER, GridBagConstraints.BOTH, |
118 | new Insets(0, 0, 0, 0), 0, 0)); |
119 | |
120 | return mainPanel; |
121 | } |
122 | |
123 | //-----------------------------------------------------------------------// |
124 | public Component createToolbar() |
125 | { |
126 | JToolBar tools = (JToolBar) super.createToolbar(); |
127 | |
128 | ButtonGroup group = new ButtonGroup(); |
129 | Insets margin = new Insets(1, 1, 1, 1); |
130 | JToggleButton button; |
131 | |
132 | button = new HeavyToggleButton(imgBox, false); |
133 | button.setToolTipText(resGui.getString("TT-Box")); |
134 | button.setActionCommand("Box"); |
135 | button.addActionListener(this); |
136 | button.setMargin(margin); |
137 | group.add(button); |
138 | tools.add(button); |
139 | |
140 | button = new HeavyToggleButton(imgCylinder, false); |
141 | button.setToolTipText(resGui.getString("TT-Cylinder")); |
142 | button.setActionCommand("Cylinder"); |
143 | button.addActionListener(this); |
144 | button.setMargin(margin); |
145 | group.add(button); |
146 | tools.add(button); |
147 | |
148 | button = new HeavyToggleButton(imgCone, false); |
149 | button.setToolTipText(resGui.getString("TT-Cone")); |
150 | button.setActionCommand("Cone"); |
151 | button.addActionListener(this); |
152 | button.setMargin(margin); |
153 | group.add(button); |
154 | tools.add(button); |
155 | |
156 | button = new HeavyToggleButton(imgSphere, false); |
157 | button.setToolTipText(resGui.getString("TT-Sphere")); |
158 | button.setActionCommand("Sphere"); |
159 | button.addActionListener(this); |
160 | button.setMargin(margin); |
161 | group.add(button); |
162 | tools.add(button); |
163 | |
164 | button = new HeavyToggleButton(imgTorus, false); |
165 | button.setToolTipText(resGui.getString("TT-Torus")); |
166 | button.setActionCommand("Torus"); |
167 | button.addActionListener(this); |
168 | button.setMargin(margin); |
169 | group.add(button); |
170 | tools.add(button); |
171 | |
172 | button = new HeavyToggleButton(imgWedge, false); |
173 | button.setToolTipText(resGui.getString("TT-Wedge")); |
174 | button.setActionCommand("Wedge"); |
175 | button.addActionListener(this); |
176 | button.setMargin(margin); |
177 | group.add(button); |
178 | tools.add(button); |
179 | |
180 | tools.addSeparator();; |
181 | |
182 | button = new HeavyToggleButton(imgPrism, false); |
183 | button.setToolTipText(resGui.getString("TT-Prism")); |
184 | button.setActionCommand("Prism"); |
185 | button.addActionListener(this); |
186 | button.setMargin(margin); |
187 | group.add(button); |
188 | tools.add(button); |
189 | |
190 | button = new HeavyToggleButton(imgRevol, false); |
191 | button.setToolTipText(resGui.getString("TT-Revol")); |
192 | button.setActionCommand("Revol"); |
193 | button.addActionListener(this); |
194 | button.setMargin(margin); |
195 | group.add(button); |
196 | tools.add(button); |
197 | |
198 | button = new HeavyToggleButton(imgPipe, false); |
199 | button.setToolTipText(resGui.getString("TT-Pipe")); |
200 | button.setActionCommand("Pipe"); |
201 | button.addActionListener(this); |
202 | button.setMargin(margin); |
203 | group.add(button); |
204 | tools.add(button); |
205 | |
206 | button = new HeavyToggleButton(imgThru, false); |
207 | button.setToolTipText(resGui.getString("TT-Thru")); |
208 | button.setActionCommand("Thru"); |
209 | button.addActionListener(this); |
210 | button.setMargin(margin); |
211 | group.add(button); |
212 | tools.add(button); |
213 | |
214 | button = new HeavyToggleButton(imgEvolved, false); |
215 | button.setToolTipText(resGui.getString("TT-Evolved")); |
216 | button.setActionCommand("Evolved"); |
217 | button.addActionListener(this); |
218 | button.setMargin(margin); |
219 | group.add(button); |
220 | tools.add(button); |
221 | |
222 | tools.addSeparator();; |
223 | |
224 | button = new HeavyToggleButton(imgDraft, false); |
225 | button.setToolTipText(resGui.getString("TT-Draft")); |
226 | button.setActionCommand("Draft"); |
227 | button.addActionListener(this); |
228 | button.setMargin(margin); |
229 | group.add(button); |
230 | tools.add(button); |
231 | |
232 | return tools; |
233 | } |
234 | |
235 | |
236 | //=======================================================================// |
237 | // Actions // |
238 | //=======================================================================// |
239 | public void postProcess(TCollection_AsciiString message, String title) |
240 | { |
241 | myView3d.getViewPort().FitAll(); |
242 | |
243 | String text = message.ToCString().GetValue(); |
244 | text += "\n-------------------- END ----------------------\n"; |
245 | |
246 | traceMessage(text, title); |
247 | } |
248 | |
249 | //=======================================================================// |
250 | // Tests |
251 | //=======================================================================// |
252 | public void onBox() |
253 | { |
254 | TCollection_AsciiString message = new TCollection_AsciiString(); |
255 | SamplesTopologyPackage.MakeBox(myAISContext, message); |
256 | |
257 | postProcess(message, resGui.getString("Dlg-Box")); |
258 | } |
259 | |
260 | |
261 | //=======================================================================// |
262 | public void onCylinder() |
263 | { |
264 | TCollection_AsciiString message = new TCollection_AsciiString(); |
265 | SamplesTopologyPackage.MakeCylinder(myAISContext, message); |
266 | |
267 | postProcess(message, resGui.getString("Dlg-Cylinder")); |
268 | } |
269 | |
270 | |
271 | //=======================================================================// |
272 | public void onCone() |
273 | { |
274 | TCollection_AsciiString message = new TCollection_AsciiString(); |
275 | SamplesTopologyPackage.MakeCone(myAISContext, message); |
276 | |
277 | postProcess(message, resGui.getString("Dlg-Cone")); |
278 | } |
279 | |
280 | |
281 | //=======================================================================// |
282 | public void onSphere() |
283 | { |
284 | TCollection_AsciiString message = new TCollection_AsciiString(); |
285 | SamplesTopologyPackage.MakeSphere(myAISContext, message); |
286 | |
287 | postProcess(message, resGui.getString("Dlg-Sphere")); |
288 | } |
289 | |
290 | |
291 | //=======================================================================// |
292 | public void onTorus() |
293 | { |
294 | TCollection_AsciiString message = new TCollection_AsciiString(); |
295 | SamplesTopologyPackage.MakeTorus(myAISContext, message); |
296 | |
297 | postProcess(message, resGui.getString("Dlg-Torus")); |
298 | } |
299 | |
300 | |
301 | //=======================================================================// |
302 | public void onWedge() |
303 | { |
304 | TCollection_AsciiString message = new TCollection_AsciiString(); |
305 | SamplesTopologyPackage.MakeWedge(myAISContext, message); |
306 | |
307 | postProcess(message, resGui.getString("Dlg-Wedge")); |
308 | } |
309 | |
310 | |
311 | //=======================================================================// |
312 | public void onPrism() |
313 | { |
314 | TCollection_AsciiString message = new TCollection_AsciiString(); |
315 | SamplesTopologyPackage.MakePrism(myAISContext, message); |
316 | |
317 | postProcess(message, resGui.getString("Dlg-Prism")); |
318 | } |
319 | |
320 | |
321 | //=======================================================================// |
322 | public void onRevol() |
323 | { |
324 | TCollection_AsciiString message = new TCollection_AsciiString(); |
325 | SamplesTopologyPackage.MakeRevol(myAISContext, message); |
326 | |
327 | postProcess(message, resGui.getString("Dlg-Revol")); |
328 | } |
329 | |
330 | |
331 | //=======================================================================// |
332 | public void onPipe() |
333 | { |
334 | TCollection_AsciiString message = new TCollection_AsciiString(); |
335 | SamplesTopologyPackage.MakePipe(myAISContext, message); |
336 | |
337 | postProcess(message, resGui.getString("Dlg-Pipe")); |
338 | } |
339 | |
340 | |
341 | //=======================================================================// |
342 | public void onThru() |
343 | { |
344 | TCollection_AsciiString message = new TCollection_AsciiString(); |
345 | SamplesTopologyPackage.MakeThru(myAISContext, message); |
346 | |
347 | postProcess(message, resGui.getString("Dlg-Thru")); |
348 | } |
349 | |
350 | |
351 | //=======================================================================// |
352 | public void onEvolved() |
353 | { |
354 | TCollection_AsciiString message = new TCollection_AsciiString(); |
355 | SamplesTopologyPackage.MakeEvolved(myAISContext, message); |
356 | |
357 | postProcess(message, resGui.getString("Dlg-Evolved")); |
358 | } |
359 | |
360 | |
361 | //=======================================================================// |
362 | public void onDraft() |
363 | { |
364 | TCollection_AsciiString message = new TCollection_AsciiString(); |
365 | SamplesTopologyPackage.MakeDraft(myAISContext, message); |
366 | |
367 | postProcess(message, resGui.getString("Dlg-Draft")); |
368 | } |
369 | |
370 | |
371 | //=======================================================================// |
372 | // ActionListener interface |
373 | //=======================================================================// |
374 | public void actionPerformed(ActionEvent e) |
375 | { |
376 | String command = e.getActionCommand(); |
377 | |
378 | if (command.equals("Box")) onBox(); |
379 | else if (command.equals("Cylinder")) onCylinder(); |
380 | else if (command.equals("Cone")) onCone(); |
381 | else if (command.equals("Sphere")) onSphere(); |
382 | else if (command.equals("Torus")) onTorus(); |
383 | else if (command.equals("Wedge")) onWedge(); |
384 | else if (command.equals("Prism")) onPrism(); |
385 | else if (command.equals("Revol")) onRevol(); |
386 | else if (command.equals("Pipe")) onPipe(); |
387 | else if (command.equals("Thru")) onThru(); |
388 | else if (command.equals("Evolved")) onEvolved(); |
389 | else if (command.equals("Draft")) onDraft(); |
390 | } |
391 | |
392 | //=======================================================================// |
393 | // MouseListener interface |
394 | //=======================================================================// |
395 | public void mouseClicked(MouseEvent e) |
396 | { |
397 | } |
398 | |
399 | public void mousePressed(MouseEvent e) |
400 | { |
401 | if (SwingUtilities.isLeftMouseButton(e)) |
402 | { |
403 | startX = e.getX(); |
404 | startY = e.getY(); |
405 | Object src = e.getSource(); |
406 | |
407 | if (src == myView3d) |
408 | { |
409 | V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView(); |
410 | if (view3d != null) |
411 | myAISContext.MoveTo(startX, startY, view3d); |
412 | } |
413 | } |
414 | else if (SwingUtilities.isRightMouseButton(e)) |
415 | { |
416 | if (e.getSource() == myView3d) |
417 | { |
418 | PopupMenu defPopup = myView3d.getDefaultPopup(); |
419 | myView3d.add(defPopup); |
420 | defPopup.show(myView3d, e.getX(), e.getY() + 30); |
421 | } |
422 | } |
423 | } |
424 | |
425 | public void mouseReleased(MouseEvent e) |
426 | { |
427 | if (SwingUtilities.isLeftMouseButton(e)) |
428 | { |
429 | Object src = e.getSource(); |
430 | |
431 | if (Dragged) |
432 | { |
433 | if (src == myView3d) |
434 | { |
435 | V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView(); |
436 | if (view3d != null) |
437 | { |
438 | if (e.isShiftDown()) |
439 | myAISContext.ShiftSelect(startX, startY, e.getX(), e.getY(), view3d, true); |
440 | else |
441 | myAISContext.Select(startX, startY, e.getX(), e.getY(), view3d, true); |
442 | } |
443 | } |
444 | } |
445 | else |
446 | { |
447 | if (src == myView3d) |
448 | { |
449 | if (e.isShiftDown()) |
450 | myAISContext.ShiftSelect(true); |
451 | else |
452 | myAISContext.Select(true); |
453 | } |
454 | } |
455 | Dragged = false; |
456 | } |
457 | } |
458 | |
459 | public void mouseEntered(MouseEvent e) |
460 | { |
461 | } |
462 | |
463 | public void mouseExited(MouseEvent e) |
464 | { |
465 | } |
466 | |
467 | //=======================================================================// |
468 | // MouseMotionListener interface |
469 | //=======================================================================// |
470 | public void mouseDragged(MouseEvent e) |
471 | { |
472 | if (SwingUtilities.isLeftMouseButton(e) && |
473 | e.getSource() == myView3d) |
474 | Dragged = true; |
475 | } |
476 | |
477 | public void mouseMoved(MouseEvent e) |
478 | { |
479 | Object src = e.getSource(); |
480 | |
481 | if (src == myView3d) |
482 | { |
483 | V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView(); |
484 | if (myAISContext != null && view3d != null) |
485 | myAISContext.MoveTo(e.getX(), e.getY(), view3d); |
486 | } |
487 | } |
488 | |
489 | |
490 | } |
491 | |