Adjusting testing cases for current state of OCCT
[occt.git] / samples / java / java / SampleLocalOperationsPanel.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 SamplesTopologyJni.*;
17 import util.*;
18
19 public class SampleLocalOperationsPanel extends SamplePanel
20                                  implements ActionListener,
21                                             MouseListener,
22                                             MouseMotionListener
23 {
24   //-----------------------------------------------------------//
25   // Resources
26   //-----------------------------------------------------------//
27   static protected ResourceBundle resGui =
28          ResourceBundle.getBundle("SampleLocalOperations");
29
30   static protected ImageIcon imgPrism = new ImageIcon(resGui.getString("Icon-Prism"));
31   static protected ImageIcon imgDPrism = new ImageIcon(resGui.getString("Icon-DPrism"));
32   static protected ImageIcon imgRevol = new ImageIcon(resGui.getString("Icon-Revol"));
33   static protected ImageIcon imgPipe = new ImageIcon(resGui.getString("Icon-Pipe"));
34   static protected ImageIcon imgRib = new ImageIcon(resGui.getString("Icon-Rib"));
35   static protected ImageIcon imgGlue = new ImageIcon(resGui.getString("Icon-Glue"));
36   static protected ImageIcon imgSplit = new ImageIcon(resGui.getString("Icon-Split"));
37   static protected ImageIcon imgThick = new ImageIcon(resGui.getString("Icon-Thick"));
38   static protected ImageIcon imgOffset = new ImageIcon(resGui.getString("Icon-Offset"));
39
40
41   //-----------------------------------------------------------//
42   // Components
43   //-----------------------------------------------------------//
44   private ViewPanel myView3d;
45
46   private static V3d_Viewer myViewer3d = null;
47
48   private static AIS_InteractiveContext myAISContext = null;
49
50   private int startX = 0, startY = 0;
51   private boolean Dragged = false;
52
53
54   //-----------------------------------------------------------//
55   // External access
56   //-----------------------------------------------------------//
57   public static V3d_Viewer getViewer3d()
58   {
59     return myViewer3d;
60   }
61
62   public static AIS_InteractiveContext getAISContext()
63   {
64     return myAISContext;
65   }
66
67
68 //=======================================================================//
69 //                             Constructor                               //
70 //=======================================================================//
71   public SampleLocalOperationsPanel()
72   {
73     // 3D Initialization
74     //------------------------------------------
75     myViewer3d.SetDefaultLights();
76     myViewer3d.SetLightOn();
77
78     if (myAISContext == null)
79     {
80       myAISContext = new AIS_InteractiveContext(myViewer3d);
81       myAISContext.SetDisplayMode(AIS_DisplayMode.AIS_Shaded, false);
82     }
83   }
84
85 //-----------------------------------------------------------------------//
86   public JPanel createViewPanel()
87   {
88     JPanel mainPanel = new JPanel();
89     mainPanel.setLayout(new GridBagLayout());
90     mainPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 0));
91
92     // Viewer 3D
93     //------------------------------------------
94     myViewer3d = SamplesTopologyPackage.CreateViewer("LocalOperations");
95
96     myView3d = new ViewPanel() {
97         public ViewCanvas createViewPort()
98         {
99           return new CASCADEView3d(SampleLocalOperationsPanel.getViewer3d()) {
100               public void setWindow3d(V3d_View view, int hiwin, int lowin) {
101                 SamplesTopologyPackage.SetWindow(view, hiwin, lowin);
102               }
103           };
104         }
105     };
106    
107     myView3d.addMouseListener(this);
108     myView3d.addMouseMotionListener(this);
109
110
111     // Layout
112     //------------------------------------------
113     mainPanel.add(myView3d, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
114             GridBagConstraints.CENTER, GridBagConstraints.BOTH,
115             new Insets(0, 0, 0, 0), 0, 0));
116
117     return mainPanel;
118   }
119
120 //-----------------------------------------------------------------------//
121   public Component createToolbar()
122   {
123     JToolBar tools = (JToolBar) super.createToolbar();
124
125     ButtonGroup group = new ButtonGroup();
126     Insets margin = new Insets(1, 1, 1, 1);
127     JToggleButton button;
128
129     button = new HeavyToggleButton(imgPrism, false);
130     button.setToolTipText(resGui.getString("TT-Prism"));
131     button.setActionCommand("Prism");
132     button.addActionListener(this);
133     button.setMargin(margin);
134     group.add(button);
135     tools.add(button);
136
137     button = new HeavyToggleButton(imgDPrism, false);
138     button.setToolTipText(resGui.getString("TT-DPrism"));
139     button.setActionCommand("DPrism");
140     button.addActionListener(this);
141     button.setMargin(margin);
142     group.add(button);
143     tools.add(button);
144
145     button = new HeavyToggleButton(imgRevol, false);
146     button.setToolTipText(resGui.getString("TT-Revol"));
147     button.setActionCommand("Revol");
148     button.addActionListener(this);
149     button.setMargin(margin);
150     group.add(button);
151     tools.add(button);
152
153     button = new HeavyToggleButton(imgPipe, false);
154     button.setToolTipText(resGui.getString("TT-Pipe"));
155     button.setActionCommand("Pipe");
156     button.addActionListener(this);
157     button.setMargin(margin);
158     group.add(button);
159     tools.add(button);
160
161     button = new HeavyToggleButton(imgRib, false);
162     button.setToolTipText(resGui.getString("TT-Rib"));
163     button.setActionCommand("Rib");
164     button.addActionListener(this);
165     button.setMargin(margin);
166     group.add(button);
167     tools.add(button);
168
169     button = new HeavyToggleButton(imgGlue, false);
170     button.setToolTipText(resGui.getString("TT-Glue"));
171     button.setActionCommand("Glue");
172     button.addActionListener(this);
173     button.setMargin(margin);
174     group.add(button);
175     tools.add(button);
176
177     button = new HeavyToggleButton(imgSplit, false);
178     button.setToolTipText(resGui.getString("TT-Split"));
179     button.setActionCommand("Split");
180     button.addActionListener(this);
181     button.setMargin(margin);
182     group.add(button);
183     tools.add(button);
184
185     button = new HeavyToggleButton(imgThick, false);
186     button.setToolTipText(resGui.getString("TT-Thick"));
187     button.setActionCommand("Thick");
188     button.addActionListener(this);
189     button.setMargin(margin);
190     group.add(button);
191     tools.add(button);
192
193     button = new HeavyToggleButton(imgOffset, false);
194     button.setToolTipText(resGui.getString("TT-Offset"));
195     button.setActionCommand("Offset");
196     button.addActionListener(this);
197     button.setMargin(margin);
198     group.add(button);
199     tools.add(button);
200
201     return tools;
202   }
203
204
205 //=======================================================================//
206 //                              Actions                                  //
207 //=======================================================================//
208   public void postProcess(TCollection_AsciiString message, String title)
209   {
210     myView3d.getViewPort().FitAll();
211
212     String text = message.ToCString().GetValue();
213     text += "\n-------------------- END ----------------------\n";
214
215     traceMessage(text, title);
216   }
217
218 //=======================================================================//
219 // Tests
220 //=======================================================================//
221   public void onPrism()
222   {
223     TCollection_AsciiString message = new TCollection_AsciiString();
224     SamplesTopologyPackage.LocalPrism(myAISContext, message);
225
226     postProcess(message, resGui.getString("Dlg-Prism"));
227   }
228
229
230 //=======================================================================//
231   public void onDPrism()
232   {
233     TCollection_AsciiString message = new TCollection_AsciiString();
234     SamplesTopologyPackage.LocalDPrism(myAISContext, message);
235
236     postProcess(message, resGui.getString("Dlg-DPrism"));
237   }
238
239
240 //=======================================================================//
241   public void onRevol()
242   {
243     TCollection_AsciiString message = new TCollection_AsciiString();
244     SamplesTopologyPackage.LocalRevol(myAISContext, message);
245
246     postProcess(message, resGui.getString("Dlg-Revol"));
247   }
248
249
250 //=======================================================================//
251   public void onPipe()
252   {
253     TCollection_AsciiString message = new TCollection_AsciiString();
254     SamplesTopologyPackage.LocalPipe(myAISContext, message);
255
256     postProcess(message, resGui.getString("Dlg-Pipe"));
257   }
258
259
260 //=======================================================================//
261   public void onRib()
262   {
263     TCollection_AsciiString message = new TCollection_AsciiString();
264     SamplesTopologyPackage.Rib(myAISContext, message);
265
266     postProcess(message, resGui.getString("Dlg-Rib"));
267   }
268
269
270 //=======================================================================//
271   public void onGlue()
272   {
273     TCollection_AsciiString message = new TCollection_AsciiString();
274     SamplesTopologyPackage.Glue(myAISContext, message);
275
276     postProcess(message, resGui.getString("Dlg-Glue"));
277   }
278
279
280 //=======================================================================//
281   public void onSplit()
282   {
283     TCollection_AsciiString message = new TCollection_AsciiString();
284     SamplesTopologyPackage.Split(myAISContext, message);
285
286     postProcess(message, resGui.getString("Dlg-Split"));
287   }
288
289
290 //=======================================================================//
291   public void onThick()
292   {
293     TCollection_AsciiString message = new TCollection_AsciiString();
294     SamplesTopologyPackage.Thick(myAISContext, message);
295
296     postProcess(message, resGui.getString("Dlg-Thick"));
297   }
298
299
300 //=======================================================================//
301   public void onOffset()
302   {
303     TCollection_AsciiString message = new TCollection_AsciiString();
304     SamplesTopologyPackage.Offset(myAISContext, message);
305
306     postProcess(message, resGui.getString("Dlg-Offset"));
307   }
308
309
310 //=======================================================================//
311 // ActionListener interface
312 //=======================================================================//
313   public void actionPerformed(ActionEvent e)
314   {
315     String command = e.getActionCommand();
316
317     if (command.equals("Prism")) onPrism();
318     else if (command.equals("DPrism")) onDPrism();
319     else if (command.equals("Revol")) onRevol();
320     else if (command.equals("Pipe")) onPipe();
321     else if (command.equals("Rib")) onRib();
322     else if (command.equals("Glue")) onGlue();
323     else if (command.equals("Split")) onSplit();
324     else if (command.equals("Thick")) onThick();
325     else if (command.equals("Offset")) onOffset();
326   }
327
328 //=======================================================================//
329 // MouseListener interface
330 //=======================================================================//
331   public void mouseClicked(MouseEvent e)
332   {
333   }
334
335   public void mousePressed(MouseEvent e)
336   {
337     if (SwingUtilities.isLeftMouseButton(e))
338     {
339       startX = e.getX();
340       startY = e.getY();
341       Object src = e.getSource();
342
343       if (src == myView3d)
344       {
345         V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView();
346         if (view3d != null)
347           myAISContext.MoveTo(startX, startY, view3d);
348       }
349     }
350     else if (SwingUtilities.isRightMouseButton(e))
351     {
352       if (e.getSource() == myView3d)
353       {
354         PopupMenu defPopup = myView3d.getDefaultPopup();
355         myView3d.add(defPopup);
356         defPopup.show(myView3d, e.getX(), e.getY() + 30);
357       }
358     }
359   }
360
361   public void mouseReleased(MouseEvent e)
362   {
363     if (SwingUtilities.isLeftMouseButton(e))
364     {
365       Object src = e.getSource();
366
367       if (Dragged)
368       {
369         if (src == myView3d)
370         {
371           V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView();
372           if (view3d != null)
373           {
374             if (e.isShiftDown())
375               myAISContext.ShiftSelect(startX, startY, e.getX(), e.getY(), view3d, true);
376             else
377               myAISContext.Select(startX, startY, e.getX(), e.getY(), view3d, true);
378           }
379         }
380       }
381       else
382       {
383         if (src == myView3d)
384         {
385           if (e.isShiftDown())
386             myAISContext.ShiftSelect(true);
387           else
388             myAISContext.Select(true);
389         }
390       }
391       Dragged = false;
392     }
393   }
394
395   public void mouseEntered(MouseEvent e)
396   {
397   }
398
399   public void mouseExited(MouseEvent e)
400   {
401   }
402
403 //=======================================================================//
404 // MouseMotionListener interface
405 //=======================================================================//
406   public void mouseDragged(MouseEvent e)
407   {
408     if (SwingUtilities.isLeftMouseButton(e) &&
409         e.getSource() == myView3d)
410       Dragged = true;
411   }
412
413   public void mouseMoved(MouseEvent e)
414   {
415     Object src = e.getSource();
416
417     if (src == myView3d)
418     {
419       V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView();
420       if (myAISContext != null && view3d != null)
421         myAISContext.MoveTo(e.getX(), e.getY(), view3d);
422     }
423   }
424
425
426 }
427