Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / java / java / SampleViewer3DPanel.java
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 SampleViewer3DJni.*;
17 import util.*;
18
19 public class SampleViewer3DPanel extends SamplePanel
20                                  implements ActionListener,
21                                             MouseListener,
22                                             MouseMotionListener
23 {
24   //-----------------------------------------------------------//
25   // Resources
26   //-----------------------------------------------------------//
27   static protected ResourceBundle resGui =
28          ResourceBundle.getBundle("SampleViewer3D");
29
30   static private ImageIcon imgBox = new ImageIcon(resGui.getString("Icon-Box"));
31   static private ImageIcon imgCylinder = new ImageIcon(resGui.getString("Icon-Cylinder"));
32   static private ImageIcon imgSphere = new ImageIcon(resGui.getString("Icon-Sphere"));
33   static private ImageIcon imgEraseAll = new ImageIcon(resGui.getString("Icon-EraseAll"));
34   static private ImageIcon imgSpotLight = new ImageIcon(resGui.getString("Icon-SpotLight"));
35   static private ImageIcon imgPositionalLight = new ImageIcon(resGui.getString("Icon-PositionalLight"));
36   static private ImageIcon imgDirectionalLight = new ImageIcon(resGui.getString("Icon-DirectionalLight"));
37   static private ImageIcon imgAmbientLight = new ImageIcon(resGui.getString("Icon-AmbientLight"));
38   static private ImageIcon imgClearLights = new ImageIcon(resGui.getString("Icon-ClearLights"));
39   static private ImageIcon imgZClipping = new ImageIcon(resGui.getString("Icon-ZClipping"));
40   static private ImageIcon imgZCueing = new ImageIcon(resGui.getString("Icon-ZCueing"));
41   static private ImageIcon imgShadingModel = new ImageIcon(resGui.getString("Icon-ShadingModel"));
42   static private ImageIcon imgAntialiasing = new ImageIcon(resGui.getString("Icon-Antialiasing"));
43   static private ImageIcon imgModelClipping = new ImageIcon(resGui.getString("Icon-ModelClipping"));
44
45
46   //-----------------------------------------------------------//
47   // Components
48   //-----------------------------------------------------------//
49   private ViewPanel myView3d;
50
51   private static Graphic3d_GraphicDriver myGraphicDriver = null;
52   private static V3d_Viewer myViewer3d = null;
53   private static AIS_InteractiveContext myAISContext = null;
54
55   private int startX = 0, startY = 0;
56   private boolean Dragged = false;
57
58   
59   //-----------------------------------------------------------//
60   // Actions
61   //-----------------------------------------------------------//
62   private static final int CurAction3d_Nothing = 0;
63   private static final int CurAction3d_BeginSpotLight = 1;
64   private static final int CurAction3d_TargetSpotLight = 2;
65   private static final int CurAction3d_EndSpotLight = 3;
66   private static final int CurAction3d_BeginPositionalLight = 4;
67   private static final int CurAction3d_BeginDirectionalLight = 5;
68   private static final int CurAction3d_EndDirectionalLight = 6;
69
70   private int myCurrentMode = CurAction3d_Nothing;
71   private V3d_View myView = null;
72   private int NbActiveLights = 2;  // There are 2 default active lights
73
74
75   //-----------------------------------------------------------//
76   // External access
77   //-----------------------------------------------------------//
78   public static Graphic3d_GraphicDriver getGraphicDriver()
79   {
80     return myGraphicDriver;
81   }
82
83   public static V3d_Viewer getViewer3d()
84   {
85     return myViewer3d;
86   }
87
88   public static AIS_InteractiveContext getAISContext()
89   {
90     return myAISContext;
91   }
92
93
94 //=======================================================================//
95 //                             Constructor                               //
96 //=======================================================================//
97   public SampleViewer3DPanel()
98   {
99     // 3D Initialization
100     //------------------------------------------
101     myViewer3d.SetDefaultLights();
102     myViewer3d.SetLightOn();
103
104     if (myAISContext == null)
105       myAISContext = new AIS_InteractiveContext(myViewer3d);
106   }
107
108 //-----------------------------------------------------------------------//
109   public JPanel createViewPanel()
110   {
111     JPanel mainPanel = new JPanel();
112     mainPanel.setLayout(new GridBagLayout());
113     mainPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 0));
114
115     // Viewer 3D
116     //------------------------------------------
117     myGraphicDriver = SampleViewer3DPackage.CreateGraphicDriver();
118     myViewer3d = SampleViewer3DPackage.CreateViewer("Viewer3D");
119
120     myView3d = new ViewPanel() {
121         public ViewCanvas createViewPort()
122         {
123           return new CASCADEView3d(SampleViewer3DPanel.getViewer3d()) {
124               public void setWindow3d(V3d_View view, int hiwin, int lowin) {
125                 SampleViewer3DPackage.SetWindow(view, hiwin, lowin);
126               }
127         };
128         }
129     };
130
131     myView3d.addMouseListener(this);
132     myView3d.addMouseMotionListener(this);
133
134
135     // Layout
136     //------------------------------------------
137     mainPanel.add(myView3d, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
138             GridBagConstraints.CENTER, GridBagConstraints.BOTH,
139             new Insets(0, 0, 0, 0), 0, 0));
140
141     return mainPanel;
142   }
143
144 //-----------------------------------------------------------------------//
145   public Component createToolbar()
146   {
147     JToolBar tools = (JToolBar) super.createToolbar();
148
149     ButtonGroup group = new ButtonGroup();
150     Insets margin = new Insets(1, 1, 1, 1);
151     JButton button;
152
153     button = new HeavyButton(imgBox);
154     button.setToolTipText(resGui.getString("Help-Box"));
155     button.setActionCommand("Box");
156     button.addActionListener(this);
157     button.setMargin(margin);
158     tools.add(button);
159
160     button = new HeavyButton(imgCylinder);
161     button.setToolTipText(resGui.getString("Help-Cylinder"));
162     button.setActionCommand("Cylinder");
163     button.addActionListener(this);
164     button.setMargin(margin);
165     tools.add(button);
166
167     button = new HeavyButton(imgSphere);
168     button.setToolTipText(resGui.getString("Help-Sphere"));
169     button.setActionCommand("Sphere");
170     button.addActionListener(this);
171     button.setMargin(margin);
172     tools.add(button);
173
174     tools.addSeparator();;
175
176     button = new HeavyButton(imgEraseAll);
177     button.setToolTipText(resGui.getString("Help-EraseAll"));
178     button.setActionCommand("EraseAll");
179     button.addActionListener(this);
180     button.setMargin(margin);
181     tools.add(button);
182
183     tools.addSeparator();;
184
185     button = new HeavyButton(imgSpotLight);
186     button.setToolTipText(resGui.getString("Help-SpotLight"));
187     button.setActionCommand("SpotLight");
188     button.addActionListener(this);
189     button.setMargin(margin);
190     tools.add(button);
191
192     button = new HeavyButton(imgPositionalLight);
193     button.setToolTipText(resGui.getString("Help-PositionalLight"));
194     button.setActionCommand("PositionalLight");
195     button.addActionListener(this);
196     button.setMargin(margin);
197     tools.add(button);
198
199     button = new HeavyButton(imgDirectionalLight);
200     button.setToolTipText(resGui.getString("Help-DirectionalLight"));
201     button.setActionCommand("DirectionalLight");
202     button.addActionListener(this);
203     button.setMargin(margin);
204     tools.add(button);
205
206     button = new HeavyButton(imgAmbientLight);
207     button.setToolTipText(resGui.getString("Help-AmbientLight"));
208     button.setActionCommand("AmbientLight");
209     button.addActionListener(this);
210     button.setMargin(margin);
211     tools.add(button);
212
213     button = new HeavyButton(imgClearLights);
214     button.setToolTipText(resGui.getString("Help-ClearLights"));
215     button.setActionCommand("ClearLights");
216     button.addActionListener(this);
217     button.setMargin(margin);
218     tools.add(button);
219
220     tools.addSeparator();;
221
222     button = new HeavyButton(imgZClipping);
223     button.setToolTipText(resGui.getString("Help-ZClipping"));
224     button.setActionCommand("ZClipping");
225     button.addActionListener(this);
226     button.setMargin(margin);
227     tools.add(button);
228
229     button = new HeavyButton(imgZCueing);
230     button.setToolTipText(resGui.getString("Help-ZCueing"));
231     button.setActionCommand("ZCueing");
232     button.addActionListener(this);
233     button.setMargin(margin);
234     tools.add(button);
235
236     tools.addSeparator();;
237
238     button = new HeavyButton(imgShadingModel);
239     button.setToolTipText(resGui.getString("Help-ShadingModel"));
240     button.setActionCommand("ShadingModel");
241     button.addActionListener(this);
242     button.setMargin(margin);
243     tools.add(button);
244
245     button = new HeavyButton(imgAntialiasing);
246     button.setToolTipText(resGui.getString("Help-Antialiasing"));
247     button.setActionCommand("Antialiasing");
248     button.addActionListener(this);
249     button.setMargin(margin);
250     tools.add(button);
251
252     tools.addSeparator();;
253
254     button = new HeavyButton(imgModelClipping);
255     button.setToolTipText(resGui.getString("Help-ModelClipping"));
256     button.setActionCommand("ModelClipping");
257     button.addActionListener(this);
258     button.setMargin(margin);
259     tools.add(button);
260
261     return tools;
262   }
263
264
265 //=======================================================================//
266 // Actions
267 //=======================================================================//
268   public void onBox()
269   {
270     SampleViewer3DPackage.DisplayBox(myAISContext);
271   }
272
273   public void onCylinder()
274   {
275     SampleViewer3DPackage.DisplayCylinder(myAISContext);
276   }
277
278   public void onSphere()
279   {
280     SampleViewer3DPackage.DisplaySphere(myAISContext);
281   }
282
283   public void onEraseAll()
284   {
285     SampleViewer3DPackage.EraseAll(myAISContext);
286   }
287
288 //=======================================================================//
289   public void onSpotLight()
290   {
291     if (NbActiveLights >= myGraphicDriver.InquireLightLimit())
292     {
293       String tmp = new String("You have reach the limit number of active lights ");
294       tmp += myGraphicDriver.InquireLightLimit();
295       tmp += ".\n";
296       tmp += "Clear lights to create new ones.";
297       JOptionPane.showMessageDialog(this, tmp);
298       return;
299     }
300
301     myView = ((CASCADEView3d) myView3d.getViewPort()).getView();
302
303     TCollection_AsciiString message = new TCollection_AsciiString();
304     SampleViewer3DPackage.CreateSpotLight(myAISContext, message);
305
306     NbActiveLights++;
307
308     myCurrentMode = CurAction3d_BeginSpotLight;
309     SamplesStarter.put_info("Pick the light position");
310
311     traceMessage(message.ToCString().GetValue(), "V3d_SpotLight");
312   }
313
314 //=======================================================================//
315   public void onPositionalLight()
316   {
317     if (NbActiveLights >= myGraphicDriver.InquireLightLimit())
318     {
319       String tmp = new String("You have reach the limit number of active lights ");
320       tmp += myGraphicDriver.InquireLightLimit();
321       tmp += ".\n";
322       tmp += "Clear lights to create new ones.";
323       JOptionPane.showMessageDialog(this, tmp);
324       return;
325     }
326
327     myView = ((CASCADEView3d) myView3d.getViewPort()).getView();
328
329     TCollection_AsciiString message = new TCollection_AsciiString();
330     SampleViewer3DPackage.CreatePositionalLight(myAISContext, myView, message);
331
332     NbActiveLights++;
333
334     myCurrentMode = CurAction3d_BeginPositionalLight;
335     SamplesStarter.put_info("Pick the light position");
336
337     traceMessage(message.ToCString().GetValue(), "V3d_PositionalLight");
338   }
339
340 //=======================================================================//
341   public void onDirectionalLight()
342   {
343     if (NbActiveLights >= myGraphicDriver.InquireLightLimit())
344     {
345       String tmp = new String("You have reach the limit number of active lights ");
346       tmp += myGraphicDriver.InquireLightLimit();
347       tmp += ".\n";
348       tmp += "Clear lights to create new ones.";
349       JOptionPane.showMessageDialog(this, tmp);
350       return;
351     }
352
353     myView = ((CASCADEView3d) myView3d.getViewPort()).getView();
354
355     TCollection_AsciiString message = new TCollection_AsciiString();
356     SampleViewer3DPackage.CreateDirectionalLight(myAISContext, message);
357
358     NbActiveLights++;
359
360     myCurrentMode = CurAction3d_BeginDirectionalLight;
361     SamplesStarter.put_info("Pick a first point");
362
363     traceMessage(message.ToCString().GetValue(), "V3d_DirectionalLight");
364   }
365
366 //=======================================================================//
367   public void onAmbientLight()
368   {
369     if (NbActiveLights >= myGraphicDriver.InquireLightLimit())
370     {
371       String tmp = new String("You have reach the limit number of active lights ");
372       tmp += myGraphicDriver.InquireLightLimit();
373       tmp += ".\n";
374       tmp += "Clear lights to create new ones.";
375       JOptionPane.showMessageDialog(this, tmp);
376       return;
377     }
378
379     myView = ((CASCADEView3d) myView3d.getViewPort()).getView();
380
381     TCollection_AsciiString message = new TCollection_AsciiString();
382     SampleViewer3DPackage.CreateAmbientLight(myAISContext, myView, message);
383
384     NbActiveLights++;
385
386     traceMessage(message.ToCString().GetValue(), "V3d_AmbientLight");
387   }
388
389 //=======================================================================//
390   public void onClearLights()
391   {
392     myView = ((CASCADEView3d) myView3d.getViewPort()).getView();
393
394     TCollection_AsciiString message = new TCollection_AsciiString();
395     SampleViewer3DPackage.ClearLights(myView, message);
396
397     NbActiveLights = 2; // There are 2 default active lights
398
399     traceMessage(message.ToCString().GetValue(), "SetLightOff");
400   }
401
402 //=======================================================================//
403   public void onZClipping()
404   {
405     myView = ((CASCADEView3d) myView3d.getViewPort()).getView();
406
407     ZClippingDlg theDlg = new ZClippingDlg(SamplesStarter.getFrame(), this, myView);
408     Position.centerWindow(theDlg);
409     theDlg.show();
410   }
411
412 //=======================================================================//
413   public void onZCueing()
414   {
415     myView = ((CASCADEView3d) myView3d.getViewPort()).getView();
416
417     ZCueingDlg theDlg = new ZCueingDlg(SamplesStarter.getFrame(), this, myView);
418     Position.centerWindow(theDlg);
419     theDlg.show();
420   }
421
422 //=======================================================================//
423   public void onShadingModel()
424   {
425     myView = ((CASCADEView3d) myView3d.getViewPort()).getView();
426
427     String message = new String("\n");
428     message += "myView->SetShadingModel(Model);\n";
429     message += "myView->Update();\n";
430     message += "\n";
431
432     traceMessage(message, "SetShadingModel");
433
434     ShadingModelDlg theDlg = new ShadingModelDlg(SamplesStarter.getFrame(), myView);
435     Position.centerWindow(theDlg);
436     theDlg.show();
437   }
438
439 //=======================================================================//
440   public void onAntialiasing()
441   {
442     myView = ((CASCADEView3d) myView3d.getViewPort()).getView();
443
444     TCollection_AsciiString message = new TCollection_AsciiString();
445     SampleViewer3DPackage.ChangeAntialiasing(myView, message);
446
447     traceMessage(message.ToCString().GetValue(), "SetAntialiasingOn/SetAntialiasingOff");
448   }
449
450 //=======================================================================//
451   public void onModelClipping()
452   {
453     myView = ((CASCADEView3d) myView3d.getViewPort()).getView();
454
455     SampleViewer3DPackage.CreateClippingPlane(myViewer3d);
456
457     ModelClippingDlg theDlg = new ModelClippingDlg(SamplesStarter.getFrame(), this, myView);
458     Position.centerWindow(theDlg);
459     theDlg.show();
460   }
461
462
463 //=======================================================================//
464 // ActionListener interface
465 //=======================================================================//
466   public void actionPerformed(ActionEvent e)
467   {
468     String command = e.getActionCommand();
469
470     if (command.equals("Box")) onBox();
471     else if (command.equals("Cylinder")) onCylinder();
472     else if (command.equals("Sphere")) onSphere();
473     else if (command.equals("EraseAll")) onEraseAll();
474
475     else if (command.equals("SpotLight")) onSpotLight();
476     else if (command.equals("PositionalLight")) onPositionalLight();
477     else if (command.equals("DirectionalLight")) onDirectionalLight();
478     else if (command.equals("AmbientLight")) onAmbientLight();
479     else if (command.equals("ClearLights")) onClearLights();
480
481     else if (command.equals("ZClipping")) onZClipping();
482     else if (command.equals("ZCueing")) onZCueing();
483
484     else if (command.equals("ShadingModel")) onShadingModel();
485     else if (command.equals("Antialiasing")) onAntialiasing();
486
487     else if (command.equals("ModelClipping")) onModelClipping();
488   }
489
490 //=======================================================================//
491 // MouseListener interface
492 //=======================================================================//
493   public void mouseClicked(MouseEvent e)
494   {
495   }
496
497   public void mousePressed(MouseEvent e)
498   {
499     if (SwingUtilities.isLeftMouseButton(e))
500     {
501       TCollection_AsciiString message = new TCollection_AsciiString();
502       switch (myCurrentMode)
503       {
504         case CurAction3d_BeginPositionalLight:
505           SampleViewer3DPackage.DirectPositionalLight(myAISContext, myView,
506                                                       e.getX(), e.getY(), message);
507           
508                 myCurrentMode = CurAction3d_Nothing;
509           SamplesStarter.put_info("Ready");
510
511           traceMessage(message.ToCString().GetValue(), "SetPosition");
512           break;
513
514         case CurAction3d_BeginSpotLight:
515                 SampleViewer3DPackage.SetSpotLight(myAISContext, myView,
516                                              e.getX(), e.getY(), message);
517
518           myCurrentMode = CurAction3d_TargetSpotLight;
519           SamplesStarter.put_info("Pick the target point");
520
521           traceMessage(message.ToCString().GetValue(), "SetDirection");
522           break;
523
524         case CurAction3d_TargetSpotLight:
525                 SampleViewer3DPackage.DirectSpotLight(myView, e.getX(), e.getY(), message);
526
527           myCurrentMode = CurAction3d_EndSpotLight;
528           SamplesStarter.put_info("Pick a third point (to define the angle)");
529
530           traceMessage(message.ToCString().GetValue(), "SetAngle");
531           break;
532
533         case CurAction3d_EndSpotLight:
534                 SampleViewer3DPackage.ExpandSpotLight(myAISContext);
535
536                 myCurrentMode = CurAction3d_Nothing;
537           SamplesStarter.put_info("Ready");
538           break;
539
540         case CurAction3d_BeginDirectionalLight:
541                 SampleViewer3DPackage.SetDirectionalLight(myAISContext, myView,
542                                                     e.getX(), e.getY(), message);
543
544           myCurrentMode = CurAction3d_EndDirectionalLight;
545           SamplesStarter.put_info("Pick the target point");
546
547           traceMessage(message.ToCString().GetValue(), "SetDirection");
548           break;
549
550         case CurAction3d_EndDirectionalLight:
551                 SampleViewer3DPackage.DirectDirectionalLight(myAISContext);
552
553                 myCurrentMode = CurAction3d_Nothing;
554           SamplesStarter.put_info("Ready");
555           break;
556
557         case CurAction3d_Nothing:
558           startX = e.getX();
559           startY = e.getY();
560           Object src = e.getSource();
561
562           if (src == myView3d)
563           {
564             V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView();
565             if (view3d != null)
566               myAISContext.MoveTo(startX, startY, view3d);
567           }
568       }
569     }
570     else if (SwingUtilities.isRightMouseButton(e))
571     {
572       if (e.getSource() == myView3d)
573       {
574         PopupMenu defPopup = myView3d.getDefaultPopup();
575         myView3d.add(defPopup);
576         defPopup.show(myView3d, e.getX(), e.getY() + 30);
577       }
578     }
579   }
580
581   public void mouseReleased(MouseEvent e)
582   {
583     if (SwingUtilities.isLeftMouseButton(e) &&
584        (myCurrentMode == CurAction3d_Nothing))
585     {
586       Object src = e.getSource();
587
588       if (Dragged)
589       {
590         if (src == myView3d)
591         {
592           V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView();
593           if (view3d != null)
594           {
595             if (e.isShiftDown())
596               myAISContext.ShiftSelect(startX, startY, e.getX(), e.getY(), view3d, true);
597             else
598               myAISContext.Select(startX, startY, e.getX(), e.getY(), view3d, true);
599           }
600         }
601       }
602       else
603       {
604         if (src == myView3d)
605         {
606           if (e.isShiftDown())
607             myAISContext.ShiftSelect(true);
608           else
609             myAISContext.Select(true);
610         }
611       }
612       Dragged = false;
613     }
614   }
615
616   public void mouseEntered(MouseEvent e)
617   {
618   }
619
620   public void mouseExited(MouseEvent e)
621   {
622   }
623
624 //=======================================================================//
625 // MouseMotionListener interface
626 //=======================================================================//
627   public void mouseDragged(MouseEvent e)
628   {
629     if (SwingUtilities.isLeftMouseButton(e) &&
630        (myCurrentMode == CurAction3d_Nothing) &&
631        (e.getSource() == myView3d))
632       Dragged = true;
633   }
634
635   public void mouseMoved(MouseEvent e)
636   {
637     switch (myCurrentMode)
638     {
639       case CurAction3d_BeginPositionalLight:
640         SampleViewer3DPackage.DirectingPositionalLight(myView, e.getX(), e.getY());
641         break;
642
643       case CurAction3d_TargetSpotLight:
644         SampleViewer3DPackage.DirectingSpotLight(myAISContext, myView,
645                                                  e.getX(), e.getY());
646         break;
647
648       case CurAction3d_EndSpotLight:
649         SampleViewer3DPackage.ExpandingSpotLight(myAISContext, myView,
650                                                  e.getX(), e.getY());
651         break;
652
653       case CurAction3d_EndDirectionalLight:
654         SampleViewer3DPackage.DirectingDirectionalLight(myAISContext, myView,
655                                                         e.getX(), e.getY());
656         break;
657
658       case CurAction3d_Nothing:
659         if (e.getSource() == myView3d)
660         {
661           V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView();
662           if (myAISContext != null && view3d != null)
663             myAISContext.MoveTo(e.getX(), e.getY(), view3d);
664         }
665     }
666   }
667
668
669 }
670