0023097: MFC Samples do not compile after redesigning the TKOpenGl driver
[occt.git] / samples / java / java / SampleAISBasicPanel.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 SampleAISBasicJni.*;
17 import util.*;
18
19 public class SampleAISBasicPanel extends SamplePanel
20                                  implements ActionListener,
21                                             MouseListener,
22                                             MouseMotionListener
23 {
24   //-----------------------------------------------------------//
25   // Resources
26   //-----------------------------------------------------------//
27   static protected ResourceBundle resGui =
28          ResourceBundle.getBundle("SampleAISBasic");
29
30   static private ImageIcon imgCircle = new ImageIcon(resGui.getString("Icon-Circle"));
31   static private ImageIcon imgLine = new ImageIcon(resGui.getString("Icon-Line"));
32   static private ImageIcon imgSphere = new ImageIcon(resGui.getString("Icon-Sphere"));
33   static private ImageIcon imgCylinder = new ImageIcon(resGui.getString("Icon-Cylinder"));
34
35   private HTMLFrame myHtmlFrame = null;
36
37   //-----------------------------------------------------------//
38   // Components
39   //-----------------------------------------------------------//
40   private ViewPanel myView3d;
41
42   private static V3d_Viewer myViewer3d = null;
43
44   private static AIS_InteractiveContext myAISContext = null;
45
46   private int startX = 0, startY = 0;
47   private boolean Dragged = false;
48
49
50   //-----------------------------------------------------------//
51   // External access
52   //-----------------------------------------------------------//
53   public static V3d_Viewer getViewer3d()
54   {
55     return myViewer3d;
56   }
57
58   public static AIS_InteractiveContext getAISContext()
59   {
60     return myAISContext;
61   }
62
63
64 //=======================================================================//
65 //                             Constructor                               //
66 //=======================================================================//
67   public SampleAISBasicPanel()
68   {
69     // 3D Initialization
70     //------------------------------------------
71     myViewer3d.SetDefaultLights();
72     myViewer3d.SetLightOn();
73
74     if (myAISContext == null)
75       myAISContext = new AIS_InteractiveContext(myViewer3d);
76
77     SampleAISBasicPackage.DisplayTrihedron(myAISContext);
78   }
79
80 //-----------------------------------------------------------------------//
81   public JPanel createViewPanel()
82   {
83     JPanel mainPanel = new JPanel();
84     mainPanel.setLayout(new GridBagLayout());
85     mainPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 0));
86
87     // Set the local system units
88     //------------------------------------------
89     UnitsAPI.SetLocalSystem(UnitsAPI_SystemUnits.UnitsAPI_MDTV);
90     
91
92     // Viewer 3D
93     //------------------------------------------
94     myViewer3d = SampleAISBasicPackage.CreateViewer("AISBasic");
95
96     myView3d = new ViewPanel() {
97         public ViewCanvas createViewPort() {
98           return new CASCADEView3d(SampleAISBasicPanel.getViewer3d()) {
99               public void setWindow3d(V3d_View view, int hiwin, int lowin) {
100                 SampleAISBasicPackage.SetWindow(view, hiwin, lowin);
101               }
102           };
103         }
104     };
105    
106     myView3d.addMouseListener(this);
107     myView3d.addMouseMotionListener(this);
108
109
110     // Layout
111     //------------------------------------------
112     mainPanel.add(myView3d, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
113             GridBagConstraints.CENTER, GridBagConstraints.BOTH,
114             new Insets(0, 0, 0, 0), 0, 0));
115
116     return mainPanel;
117   }
118
119 //-----------------------------------------------------------------------//
120   public Component createToolbar()
121   {
122     JToolBar tools = (JToolBar) super.createToolbar();
123
124     Insets margin = new Insets(1, 1, 1, 1);
125     JButton button;
126
127     button = new HeavyButton(imgCircle);
128     button.setToolTipText(resGui.getString("Help-Circle"));
129     button.setActionCommand("Circle");
130     button.addActionListener(this);
131     button.setMargin(margin);
132     tools.add(button);
133
134     button = new HeavyButton(imgLine);
135     button.setToolTipText(resGui.getString("Help-Line"));
136     button.setActionCommand("Line");
137     button.addActionListener(this);
138     button.setMargin(margin);
139     tools.add(button);
140
141     button = new HeavyButton(imgSphere);
142     button.setToolTipText(resGui.getString("Help-Sphere"));
143     button.setActionCommand("Sphere");
144     button.addActionListener(this);
145     button.setMargin(margin);
146     tools.add(button);
147
148     button = new HeavyButton(imgCylinder);
149     button.setToolTipText(resGui.getString("Help-Cylinder"));
150     button.setActionCommand("Cylinder");
151     button.addActionListener(this);
152     button.setMargin(margin);
153     tools.add(button);
154
155     return tools;
156   }
157
158
159 //=======================================================================//
160 //                              Actions                                  //
161 //=======================================================================//
162   public void onCircle()
163   {
164     TCollection_AsciiString message = new TCollection_AsciiString();
165     SampleAISBasicPackage.DisplayCircle(myAISContext, message);
166
167     traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-Circle"));
168   }
169
170 //=======================================================================//
171   public void onLine()
172   {
173     TCollection_AsciiString message = new TCollection_AsciiString();
174     SampleAISBasicPackage.DisplayLine(myAISContext, message);
175
176     traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-Line"));
177   }
178
179 //=======================================================================//
180   public void onSphere()
181   {
182     TCollection_AsciiString message = new TCollection_AsciiString();
183     SampleAISBasicPackage.DisplaySphere(myAISContext, message);
184
185     traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-Sphere"));
186   }
187
188 //=======================================================================//
189   public void onCylinder()
190   {
191     TCollection_AsciiString message = new TCollection_AsciiString();
192     SampleAISBasicPackage.DisplayCylinder(myAISContext, message);
193
194     traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-Cylinder"));
195   }
196
197 //=======================================================================//
198 // ActionListener interface
199 //=======================================================================//
200   public void actionPerformed(ActionEvent e)
201   {
202     String command = e.getActionCommand();
203
204     if (command.equals("Circle")) onCircle();
205     else if (command.equals("Line")) onLine();
206     else if (command.equals("Sphere")) onSphere();
207     else if (command.equals("Cylinder")) onCylinder();
208   }
209
210 //=======================================================================//
211 // MouseListener interface
212 //=======================================================================//
213   public void mouseClicked(MouseEvent e)
214   {
215   }
216
217   public void mousePressed(MouseEvent e)
218   {
219     if (SwingUtilities.isLeftMouseButton(e))
220     {
221       startX = e.getX();
222       startY = e.getY();
223       Object src = e.getSource();
224
225       if (src == myView3d)
226       {
227         V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView();
228         if (view3d != null)
229           myAISContext.MoveTo(startX, startY, view3d);
230       }
231     }
232     else if (SwingUtilities.isRightMouseButton(e))
233     {
234       if (e.getSource() == myView3d)
235       {
236         PopupMenu defPopup = myView3d.getDefaultPopup();
237         myView3d.add(defPopup);
238         defPopup.show(myView3d, e.getX(), e.getY() + 30);
239       }
240     }
241   }
242
243   public void mouseReleased(MouseEvent e)
244   {
245     if (SwingUtilities.isLeftMouseButton(e))
246     {
247       Object src = e.getSource();
248
249       if (Dragged)
250       {
251         if (src == myView3d)
252         {
253           V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView();
254           if (view3d != null)
255           {
256             if (e.isShiftDown())
257               myAISContext.ShiftSelect(startX, startY, e.getX(), e.getY(), view3d, true);
258             else
259               myAISContext.Select(startX, startY, e.getX(), e.getY(), view3d, true);
260           }
261         }
262       }
263       else
264       {
265         if (src == myView3d)
266         {
267           if (e.isShiftDown())
268             myAISContext.ShiftSelect(true);
269           else
270             myAISContext.Select(true);
271         }
272       }
273       Dragged = false;
274     }
275   }
276
277   public void mouseEntered(MouseEvent e)
278   {
279   }
280
281   public void mouseExited(MouseEvent e)
282   {
283   }
284
285 //=======================================================================//
286 // MouseMotionListener interface
287 //=======================================================================//
288   public void mouseDragged(MouseEvent e)
289   {
290     if (SwingUtilities.isLeftMouseButton(e) &&
291         e.getSource() == myView3d)
292       Dragged = true;
293   }
294
295   public void mouseMoved(MouseEvent e)
296   {
297     Object src = e.getSource();
298
299     if (src == myView3d)
300     {
301       V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView();
302       if (myAISContext != null && view3d != null)
303         myAISContext.MoveTo(e.getX(), e.getY(), view3d);
304     }
305   }
306
307
308 }
309