Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / java / java / SampleAISDisplayModePanel.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 jcas.Standard_Integer;
16 import CASCADESamplesJni.*;
17 import SampleAISDisplayModeJni.*;
18 import util.*;
19
20 public class SampleAISDisplayModePanel extends SamplePanel
21                                  implements ActionListener,
22                                             MouseListener,
23                                             MouseMotionListener
24 {
25   //-----------------------------------------------------------//
26   // Resources
27   //-----------------------------------------------------------//
28   static protected ResourceBundle resGui =
29          ResourceBundle.getBundle("SampleAISDisplayMode");
30
31   static private ImageIcon imgBox = new ImageIcon(resGui.getString("Icon-Box"));
32   static private ImageIcon imgSphere = new ImageIcon(resGui.getString("Icon-Sphere"));
33   static private ImageIcon imgCylinder = new ImageIcon(resGui.getString("Icon-Cylinder"));
34   static private ImageIcon imgWireframe = new ImageIcon(resGui.getString("Icon-Wireframe"));
35   static private ImageIcon imgShading = new ImageIcon(resGui.getString("Icon-Shading"));
36   static private ImageIcon imgIsos = new ImageIcon(resGui.getString("Icon-Isos"));
37   static private ImageIcon imgEraseAll = new ImageIcon(resGui.getString("Icon-EraseAll"));
38   static private ImageIcon imgLight = new ImageIcon(resGui.getString("Icon-Light"));
39
40   private HTMLFrame myHtmlFrame = null;
41
42   //-----------------------------------------------------------//
43   // Components
44   //-----------------------------------------------------------//
45   private ViewPanel myView3d;
46
47   private static V3d_Viewer myViewer3d = null;
48
49   private static AIS_InteractiveContext myAISContext = null;
50
51   private int startX = 0, startY = 0;
52   private boolean Dragged = false;
53
54   
55   //-----------------------------------------------------------//
56   // Light Creation
57   //-----------------------------------------------------------//
58   private static final int CurAction3d_Nothing = 0;
59   private static final int CurAction3d_FirstPointLight = 1;
60   private static final int CurAction3d_SecondPointLight = 2;
61
62   private int myCurrentMode = CurAction3d_Nothing;
63   private V3d_View myView = null;
64
65
66   //-----------------------------------------------------------//
67   // External access
68   //-----------------------------------------------------------//
69   public static V3d_Viewer getViewer3d()
70   {
71     return myViewer3d;
72   }
73
74   public static AIS_InteractiveContext getAISContext()
75   {
76     return myAISContext;
77   }
78
79
80 //=======================================================================//
81 //                             Constructor                               //
82 //=======================================================================//
83   public SampleAISDisplayModePanel()
84   {
85     // 3D Initialization
86     //------------------------------------------
87     myViewer3d.SetDefaultLights();
88     myViewer3d.SetLightOn();
89
90     SampleAISDisplayModePackage.InitContext(myAISContext);
91   }
92
93 //-----------------------------------------------------------------------//
94   public JPanel createViewPanel()
95   {
96     JPanel mainPanel = new JPanel();
97     mainPanel.setLayout(new GridBagLayout());
98     mainPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 0));
99
100
101     // Viewer 3D
102     //------------------------------------------
103     myViewer3d = SampleAISDisplayModePackage.CreateViewer("AISDisplayMode");
104
105     myView3d = new ViewPanel() {
106         public ViewCanvas createViewPort()
107         {
108           return new CASCADEView3d(SampleAISDisplayModePanel.getViewer3d()) {
109               public void setWindow3d(V3d_View view, int hiwin, int lowin) {
110                 SampleAISDisplayModePackage.SetWindow(view, hiwin, lowin);
111               }
112           };
113         }
114     };
115    
116     myView3d.addMouseListener(this);
117     myView3d.addMouseMotionListener(this);
118
119
120     // Attributes
121     //------------------------------------------
122     myAISContext = new AIS_InteractiveContext(myViewer3d);
123       
124     AttributesPanel attrib = new AttributesPanel(this, myAISContext);
125
126
127     // Layout
128     //------------------------------------------
129     mainPanel.add(attrib, new GridBagConstraints(0, 0, 1, 1, 0.0, 1.0,
130             GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
131             new Insets(0, 0, 0, 0), 0, 0));
132
133     mainPanel.add(myView3d, new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0,
134             GridBagConstraints.CENTER, GridBagConstraints.BOTH,
135             new Insets(0, 0, 0, 0), 0, 0));
136
137     return mainPanel;
138   }
139
140 //-----------------------------------------------------------------------//
141   public Component createToolbar()
142   {
143     JToolBar tools = (JToolBar) super.createToolbar();
144
145     Insets margin = new Insets(1, 1, 1, 1);
146     JButton button;
147
148     button = new HeavyButton(imgBox);
149     button.setToolTipText(resGui.getString("Help-Box"));
150     button.setActionCommand("Box");
151     button.addActionListener(this);
152     button.setMargin(margin);
153     tools.add(button);
154
155     button = new HeavyButton(imgSphere);
156     button.setToolTipText(resGui.getString("Help-Sphere"));
157     button.setActionCommand("Sphere");
158     button.addActionListener(this);
159     button.setMargin(margin);
160     tools.add(button);
161
162     button = new HeavyButton(imgCylinder);
163     button.setToolTipText(resGui.getString("Help-Cylinder"));
164     button.setActionCommand("Cylinder");
165     button.addActionListener(this);
166     button.setMargin(margin);
167     tools.add(button);
168
169     tools.addSeparator();;
170
171     button = new HeavyButton(imgWireframe);
172     button.setToolTipText(resGui.getString("Help-Wireframe"));
173     button.setActionCommand("Wireframe");
174     button.addActionListener(this);
175     button.setMargin(margin);
176     tools.add(button);
177
178     button = new HeavyButton(imgShading);
179     button.setToolTipText(resGui.getString("Help-Shading"));
180     button.setActionCommand("Shading");
181     button.addActionListener(this);
182     button.setMargin(margin);
183     tools.add(button);
184
185     button = new HeavyButton(imgIsos);
186     button.setToolTipText(resGui.getString("Help-Isos"));
187     button.setActionCommand("Isos");
188     button.addActionListener(this);
189     button.setMargin(margin);
190     tools.add(button);
191
192     button = new HeavyButton(imgEraseAll);
193     button.setToolTipText(resGui.getString("Help-EraseAll"));
194     button.setActionCommand("EraseAll");
195     button.addActionListener(this);
196     button.setMargin(margin);
197     tools.add(button);
198
199     tools.addSeparator();;
200
201     button = new HeavyButton(imgLight);
202     button.setToolTipText(resGui.getString("Help-Light"));
203     button.setActionCommand("Light");
204     button.addActionListener(this);
205     button.setMargin(margin);
206     tools.add(button);
207
208     return tools;
209   }
210
211
212 //=======================================================================//
213 //                              Actions                                  //
214 //=======================================================================//
215   public void onBox()
216   {
217     TCollection_AsciiString message = new TCollection_AsciiString();
218     SampleAISDisplayModePackage.DisplayBox(myAISContext, message);
219
220     traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-Box"));
221   }
222
223 //=======================================================================//
224   public void onSphere()
225   {
226     TCollection_AsciiString message = new TCollection_AsciiString();
227     SampleAISDisplayModePackage.DisplaySphere(myAISContext, message);
228
229     traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-Sphere"));
230   }
231
232 //=======================================================================//
233   public void onCylinder()
234   {
235     TCollection_AsciiString message = new TCollection_AsciiString();
236     SampleAISDisplayModePackage.DisplayCylinder(myAISContext, message);
237
238     traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-Cylinder"));
239   }
240
241 //=======================================================================//
242   public void onEraseAll()
243   {
244     TCollection_AsciiString message = new TCollection_AsciiString();
245     SampleAISDisplayModePackage.EraseAll(myAISContext, message);
246
247     traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-EraseAll"));
248   }
249
250 //=======================================================================//
251   public void onWireframe()
252   {
253     TCollection_AsciiString message = new TCollection_AsciiString();
254     SampleAISDisplayModePackage.SetDisplayMode(myAISContext, (short)0, message);
255
256     traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-Wireframe"));
257   }
258
259 //=======================================================================//
260   public void onShading()
261   {
262     TCollection_AsciiString message = new TCollection_AsciiString();
263     SampleAISDisplayModePackage.SetDisplayMode(myAISContext, (short)1, message);
264
265     traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-Shading"));
266   }
267
268 //=======================================================================//
269   public void onIsos()
270   {
271     Standard_Integer u = new Standard_Integer();
272     Standard_Integer v = new Standard_Integer();
273     SampleAISDisplayModePackage.GetIsosNumber(myAISContext,u,v);
274
275     IsosDlg aDlg = new IsosDlg(SamplesStarter.getFrame(), u.GetValue(), v.GetValue());
276     Position.centerWindow(aDlg);
277     aDlg.show();
278
279     if (aDlg.isOK())
280     {
281       TCollection_AsciiString message = new TCollection_AsciiString();
282       SampleAISDisplayModePackage.SetIsosNumber(myAISContext, 
283                           aDlg.getUValue(), aDlg.getVValue(), message);
284
285       traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-Shading"));
286     }
287   }
288
289 //=======================================================================//
290   public void onLight()
291   {
292     myView = ((CASCADEView3d) myView3d.getViewPort()).getView();
293
294     SampleAISDisplayModePackage.CreateLight(myAISContext);
295
296     myCurrentMode = CurAction3d_FirstPointLight;
297     SamplesStarter.put_info("Pick a first point");
298   }
299
300 //=======================================================================//
301 // ActionListener interface
302 //=======================================================================//
303   public void actionPerformed(ActionEvent e)
304   {
305     String command = e.getActionCommand();
306
307     if (command.equals("Box")) onBox();
308     else if (command.equals("Sphere")) onSphere();
309     else if (command.equals("Cylinder")) onCylinder();
310     else if (command.equals("Wireframe")) onWireframe();
311     else if (command.equals("Shading")) onShading();
312     else if (command.equals("Isos")) onIsos();
313     else if (command.equals("EraseAll")) onEraseAll();
314     else if (command.equals("Light")) onLight();
315   }
316
317 //=======================================================================//
318 // MouseListener interface
319 //=======================================================================//
320   public void mouseClicked(MouseEvent e)
321   {
322   }
323
324   public void mousePressed(MouseEvent e)
325   {
326     if (SwingUtilities.isLeftMouseButton(e))
327     {
328       startX = e.getX();
329       startY = e.getY();
330       Object src = e.getSource();
331
332       switch (myCurrentMode)
333       {
334         case CurAction3d_FirstPointLight:
335           SampleAISDisplayModePackage.SetFirstPointOfLight(myAISContext, myView,
336                                                            e.getX(), e.getY());
337           myCurrentMode = CurAction3d_SecondPointLight;
338           SamplesStarter.put_info("Pick the second point");
339           break;
340
341         case CurAction3d_SecondPointLight:
342           SampleAISDisplayModePackage.SetSecondPointOfLight(myAISContext);
343           myCurrentMode = CurAction3d_Nothing;
344           SamplesStarter.put_info("Ready");
345           break;
346
347         case CurAction3d_Nothing:
348           if (src == myView3d)
349           {
350             V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView();
351             if (view3d != null)
352               myAISContext.MoveTo(startX, startY, view3d);
353           }
354           break;
355       }
356     }
357     else if (SwingUtilities.isRightMouseButton(e))
358     {
359       if (e.getSource() == myView3d)
360       {
361         PopupMenu defPopup = myView3d.getDefaultPopup();
362         myView3d.add(defPopup);
363         defPopup.show(myView3d, e.getX(), e.getY() + 30);
364       }
365     }
366   }
367
368   public void mouseReleased(MouseEvent e)
369   {
370     if (SwingUtilities.isLeftMouseButton(e) &&
371        (myCurrentMode == CurAction3d_Nothing))
372     {
373       Object src = e.getSource();
374
375       if (Dragged)
376       {
377         if (src == myView3d)
378         {
379           V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView();
380           if (view3d != null)
381           {
382             if (e.isShiftDown())
383               myAISContext.ShiftSelect(startX, startY, e.getX(), e.getY(), view3d, true);
384             else
385               myAISContext.Select(startX, startY, e.getX(), e.getY(), view3d, true);
386           }
387         }
388       }
389       else
390       {
391         if (src == myView3d)
392         {
393           if (e.isShiftDown())
394             myAISContext.ShiftSelect(true);
395           else
396             myAISContext.Select(true);
397         }
398       }
399       Dragged = false;
400     }
401   }
402
403   public void mouseEntered(MouseEvent e)
404   {
405   }
406
407   public void mouseExited(MouseEvent e)
408   {
409   }
410
411 //=======================================================================//
412 // MouseMotionListener interface
413 //=======================================================================//
414   public void mouseDragged(MouseEvent e)
415   {
416     if (SwingUtilities.isLeftMouseButton(e) &&
417        (myCurrentMode == CurAction3d_Nothing) &&
418        (e.getSource() == myView3d))
419       Dragged = true;
420   }
421
422   public void mouseMoved(MouseEvent e)
423   {
424     switch (myCurrentMode)
425     {
426       case CurAction3d_SecondPointLight:
427           SampleAISDisplayModePackage.MoveSecondPointOfLight(myAISContext, myView,
428                                                              e.getX(), e.getY());
429           break;
430
431       case CurAction3d_Nothing:
432         if (e.getSource() == myView3d)
433         {
434           V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView();
435           if (myAISContext != null && view3d != null)
436             myAISContext.MoveTo(e.getX(), e.getY(), view3d);
437         }
438     }
439   }
440
441
442 }
443