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