7fd59977 |
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 SampleAISSelectJni.*; |
17 | import util.*; |
18 | |
19 | public class SampleAISSelectPanel extends SamplePanel |
20 | implements ActionListener, |
21 | MouseListener, |
22 | MouseMotionListener |
23 | { |
24 | //-----------------------------------------------------------// |
25 | // Resources |
26 | //-----------------------------------------------------------// |
27 | static protected ResourceBundle resGui = |
28 | ResourceBundle.getBundle("SampleAISSelect"); |
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 imgVertices = new ImageIcon(resGui.getString("Icon-Vertices")); |
33 | static private ImageIcon imgEdges = new ImageIcon(resGui.getString("Icon-Edges")); |
34 | static private ImageIcon imgFaces = new ImageIcon(resGui.getString("Icon-Faces")); |
35 | static private ImageIcon imgNeutral = new ImageIcon(resGui.getString("Icon-Neutral")); |
36 | static private ImageIcon imgFillet = new ImageIcon(resGui.getString("Icon-Fillet")); |
37 | |
38 | private HTMLFrame myHtmlFrame = null; |
39 | private PopupMenu myPopup; |
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 | // Changing face color |
56 | //-----------------------------------------------------------// |
57 | private static final int CurAction3d_Default = 0; |
58 | private static final int CurAction3d_SelectFace = 1; |
59 | |
60 | private int myCurrentMode = CurAction3d_Default; |
61 | |
62 | |
63 | //-----------------------------------------------------------// |
64 | // External access |
65 | //-----------------------------------------------------------// |
66 | public static V3d_Viewer getViewer3d() |
67 | { |
68 | return myViewer3d; |
69 | } |
70 | |
71 | public static AIS_InteractiveContext getAISContext() |
72 | { |
73 | return myAISContext; |
74 | } |
75 | |
76 | |
77 | //=======================================================================// |
78 | // Constructor // |
79 | //=======================================================================// |
80 | public SampleAISSelectPanel() |
81 | { |
82 | // 3D Initialization |
83 | //------------------------------------------ |
84 | myViewer3d.SetDefaultLights(); |
85 | myViewer3d.SetLightOn(); |
86 | |
87 | if (myAISContext == null) |
88 | myAISContext = new AIS_InteractiveContext(myViewer3d); |
89 | |
90 | |
91 | // Creation popup |
92 | //------------------------------------------ |
93 | myPopup = new PopupMenu("User cylinder"); |
94 | |
95 | MenuItem menuItem = new MenuItem("Change face color..."); |
96 | menuItem.setActionCommand("ChangeFaceColor"); |
97 | menuItem.addActionListener(this); |
98 | myPopup.add(menuItem); |
99 | } |
100 | |
101 | //-----------------------------------------------------------------------// |
102 | public JPanel createViewPanel() |
103 | { |
104 | JPanel mainPanel = new JPanel(); |
105 | mainPanel.setLayout(new GridBagLayout()); |
106 | mainPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 0)); |
107 | |
108 | |
109 | // Viewer 3D |
110 | //------------------------------------------ |
111 | myViewer3d = SampleAISSelectPackage.CreateViewer("AISSelect"); |
112 | |
113 | myView3d = new ViewPanel() { |
114 | public ViewCanvas createViewPort() |
115 | { |
116 | return new CASCADEView3d(SampleAISSelectPanel.getViewer3d()) { |
117 | public void setWindow3d(V3d_View view, int hiwin, int lowin) { |
118 | SampleAISSelectPackage.SetWindow(view, hiwin, lowin); |
119 | } |
120 | }; |
121 | } |
122 | }; |
123 | |
124 | myView3d.addMouseListener(this); |
125 | myView3d.addMouseMotionListener(this); |
126 | |
127 | |
128 | // Layout |
129 | //------------------------------------------ |
130 | mainPanel.add(myView3d, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, |
131 | GridBagConstraints.CENTER, GridBagConstraints.BOTH, |
132 | new Insets(0, 0, 0, 0), 0, 0)); |
133 | |
134 | return mainPanel; |
135 | } |
136 | |
137 | //-----------------------------------------------------------------------// |
138 | public Component createToolbar() |
139 | { |
140 | JToolBar tools = (JToolBar) super.createToolbar(); |
141 | |
142 | Insets margin = new Insets(1, 1, 1, 1); |
143 | JButton button; |
144 | |
145 | button = new HeavyButton(imgBox); |
146 | button.setToolTipText(resGui.getString("Help-Box")); |
147 | button.setActionCommand("Box"); |
148 | button.addActionListener(this); |
149 | button.setMargin(margin); |
150 | tools.add(button); |
151 | |
152 | button = new HeavyButton(imgCylinder); |
153 | button.setToolTipText(resGui.getString("Help-Cylinder")); |
154 | button.setActionCommand("Cylinder"); |
155 | button.addActionListener(this); |
156 | button.setMargin(margin); |
157 | tools.add(button); |
158 | |
159 | tools.addSeparator();; |
160 | |
161 | button = new HeavyButton(imgVertices); |
162 | button.setToolTipText(resGui.getString("Help-Vertices")); |
163 | button.setActionCommand("Vertices"); |
164 | button.addActionListener(this); |
165 | button.setMargin(margin); |
166 | tools.add(button); |
167 | |
168 | button = new HeavyButton(imgEdges); |
169 | button.setToolTipText(resGui.getString("Help-Edges")); |
170 | button.setActionCommand("Edges"); |
171 | button.addActionListener(this); |
172 | button.setMargin(margin); |
173 | tools.add(button); |
174 | |
175 | button = new HeavyButton(imgFaces); |
176 | button.setToolTipText(resGui.getString("Help-Faces")); |
177 | button.setActionCommand("Faces"); |
178 | button.addActionListener(this); |
179 | button.setMargin(margin); |
180 | tools.add(button); |
181 | |
182 | button = new HeavyButton(imgNeutral); |
183 | button.setToolTipText(resGui.getString("Help-Neutral")); |
184 | button.setActionCommand("Neutral"); |
185 | button.addActionListener(this); |
186 | button.setMargin(margin); |
187 | tools.add(button); |
188 | |
189 | tools.addSeparator();; |
190 | |
191 | button = new HeavyButton(imgFillet); |
192 | button.setToolTipText(resGui.getString("Help-Fillet")); |
193 | button.setActionCommand("Fillet"); |
194 | button.addActionListener(this); |
195 | button.setMargin(margin); |
196 | tools.add(button); |
197 | |
198 | return tools; |
199 | } |
200 | |
201 | |
202 | //=======================================================================// |
203 | // Actions // |
204 | //=======================================================================// |
205 | public void onBox() |
206 | { |
207 | TCollection_AsciiString message = new TCollection_AsciiString(); |
208 | SampleAISSelectPackage.DisplayBox(myAISContext, message); |
209 | |
210 | traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-Box")); |
211 | } |
212 | |
213 | //=======================================================================// |
214 | public void onCylinder() |
215 | { |
216 | TCollection_AsciiString message = new TCollection_AsciiString(); |
217 | SampleAISSelectPackage.DisplayCylinder(myAISContext, message); |
218 | |
219 | traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-Cylinder")); |
220 | } |
221 | |
222 | //=======================================================================// |
223 | public void onVertices() |
224 | { |
225 | TCollection_AsciiString message = new TCollection_AsciiString(); |
226 | SampleAISSelectPackage.SelectVertices(myAISContext, message); |
227 | |
228 | traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-Vertices")); |
229 | } |
230 | |
231 | //=======================================================================// |
232 | public void onEdges() |
233 | { |
234 | TCollection_AsciiString message = new TCollection_AsciiString(); |
235 | SampleAISSelectPackage.SelectEdges(myAISContext, message); |
236 | |
237 | traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-Edges")); |
238 | } |
239 | |
240 | //=======================================================================// |
241 | public void onFaces() |
242 | { |
243 | TCollection_AsciiString message = new TCollection_AsciiString(); |
244 | SampleAISSelectPackage.SelectFaces(myAISContext, message); |
245 | |
246 | traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-Faces")); |
247 | } |
248 | |
249 | //=======================================================================// |
250 | public void onNeutral() |
251 | { |
252 | TCollection_AsciiString message = new TCollection_AsciiString(); |
253 | SampleAISSelectPackage.SelectNeutral(myAISContext, message); |
254 | |
255 | traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-Neutral")); |
256 | } |
257 | |
258 | //=======================================================================// |
259 | public void onFillet() |
260 | { |
261 | if (!myAISContext.HasOpenedContext()) |
262 | { |
263 | JOptionPane.showMessageDialog(this, |
264 | "It is necessary to activate the edges selection mode\n" + |
265 | "and select edges on an object before\n" + |
266 | "running this function", |
267 | "Warning!!!", JOptionPane.WARNING_MESSAGE); |
268 | return; |
269 | } |
270 | |
271 | myAISContext.InitSelected(); |
272 | if (myAISContext.MoreSelected()) |
273 | { |
274 | RadiusDlg aDlg = new RadiusDlg(SamplesStarter.getFrame(), 10.); |
275 | Position.centerWindow(aDlg); |
276 | aDlg.show(); |
277 | |
278 | if (aDlg.isOK()) |
279 | { |
280 | TCollection_AsciiString message = new TCollection_AsciiString(); |
281 | int result = SampleAISSelectPackage.MakeFillet(myAISContext, aDlg.getValue(), |
282 | message); |
283 | if (result == 2) |
284 | JOptionPane.showMessageDialog(this, |
285 | "It is necessary to activate the edges selection mode\n" + |
286 | "and select edges on an object before\n" + |
287 | "running this function", |
288 | "Warning!!!", JOptionPane.WARNING_MESSAGE); |
289 | else if (result == 1) |
290 | JOptionPane.showMessageDialog(this, "Error during fillet computation", |
291 | "Warning!!!", JOptionPane.WARNING_MESSAGE); |
292 | else if (result == 0) |
293 | traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-Fillet")); |
294 | } |
295 | } |
296 | else |
297 | JOptionPane.showMessageDialog(this, |
298 | "It is necessary to activate the edges selection mode\n" + |
299 | "and select edges on an object before\n" + |
300 | "running this function", |
301 | "Warning!!!", JOptionPane.WARNING_MESSAGE); |
302 | } |
303 | |
304 | //=======================================================================// |
305 | public void onChangeFaceColor() |
306 | { |
307 | SampleAISSelectPackage.StartSelectFace(myAISContext); |
308 | myCurrentMode = CurAction3d_SelectFace; |
309 | SamplesStarter.put_info("Select the face"); |
310 | } |
311 | |
312 | //=======================================================================// |
313 | // ActionListener interface |
314 | //=======================================================================// |
315 | public void actionPerformed(ActionEvent e) |
316 | { |
317 | String command = e.getActionCommand(); |
318 | |
319 | if (command.equals("Box")) onBox(); |
320 | else if (command.equals("Cylinder")) onCylinder(); |
321 | else if (command.equals("Vertices")) onVertices(); |
322 | else if (command.equals("Edges")) onEdges(); |
323 | else if (command.equals("Faces")) onFaces(); |
324 | else if (command.equals("Neutral")) onNeutral(); |
325 | else if (command.equals("Fillet")) onFillet(); |
326 | else if (command.equals("ChangeFaceColor")) onChangeFaceColor(); |
327 | } |
328 | |
329 | //=======================================================================// |
330 | // MouseListener interface |
331 | //=======================================================================// |
332 | public void mouseClicked(MouseEvent e) |
333 | { |
334 | } |
335 | |
336 | public void mousePressed(MouseEvent e) |
337 | { |
338 | if (SwingUtilities.isLeftMouseButton(e)) |
339 | { |
340 | startX = e.getX(); |
341 | startY = e.getY(); |
342 | Object src = e.getSource(); |
343 | |
344 | if (src == myView3d) |
345 | { |
346 | V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView(); |
347 | if (view3d != null) |
348 | myAISContext.MoveTo(startX, startY, view3d); |
349 | } |
350 | } |
351 | else if (SwingUtilities.isRightMouseButton(e)) |
352 | { |
353 | Object src = e.getSource(); |
354 | if (src == myView3d) |
355 | { |
356 | if (SampleAISSelectPackage.IsCylinderSelected(myAISContext)) |
357 | { |
358 | myView3d.add(myPopup); |
359 | myPopup.show(myView3d, e.getX(), e.getY() + 30); |
360 | } |
361 | else |
362 | { |
363 | PopupMenu defPopup = myView3d.getDefaultPopup(); |
364 | myView3d.add(defPopup); |
365 | defPopup.show(myView3d, e.getX(), e.getY() + 30); |
366 | } |
367 | } |
368 | } |
369 | } |
370 | |
371 | public void mouseReleased(MouseEvent e) |
372 | { |
373 | if (SwingUtilities.isLeftMouseButton(e)) |
374 | { |
375 | Object src = e.getSource(); |
376 | |
377 | if (Dragged) |
378 | { |
379 | if (src == myView3d) |
380 | { |
381 | V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView(); |
382 | if (view3d != null) |
383 | { |
384 | if (e.isShiftDown()) |
385 | myAISContext.ShiftSelect(startX, startY, e.getX(), e.getY(), view3d, true); |
386 | else |
387 | myAISContext.Select(startX, startY, e.getX(), e.getY(), view3d, true); |
388 | } |
389 | } |
390 | } |
391 | else |
392 | { |
393 | if (src == myView3d) |
394 | { |
395 | if (e.isShiftDown()) |
396 | myAISContext.ShiftSelect(true); |
397 | else |
398 | myAISContext.Select(true); |
399 | } |
400 | } |
401 | Dragged = false; |
402 | |
403 | // Change the face color |
404 | if (myCurrentMode == CurAction3d_SelectFace) |
405 | { |
406 | myAISContext.InitSelected(); |
407 | if (myAISContext.MoreSelected()) |
408 | { |
409 | Quantity_Color aColor = SampleAISSelectPackage.GetFaceColor(myAISContext); |
410 | int red = (int) (aColor.Red()*255); |
411 | int green = (int) (aColor.Green()*255); |
412 | int blue = (int) (aColor.Blue()*255); |
413 | Color theColor = new Color(red, green, blue); |
414 | |
415 | Color theNewColor = JColorChooser.showDialog(SamplesStarter.getFrame(), |
416 | "Choose the color", theColor); |
417 | |
418 | if (theNewColor != null) |
419 | { |
420 | Quantity_Color aNewColor = new Quantity_Color(theNewColor.getRed()/255., |
421 | theNewColor.getGreen()/255., |
422 | theNewColor.getBlue()/255., |
423 | Quantity_TypeOfColor.Quantity_TOC_RGB); |
424 | TCollection_AsciiString message = new TCollection_AsciiString(); |
425 | SampleAISSelectPackage.SetFaceColor(myAISContext, aNewColor, message); |
426 | |
427 | traceMessage(message.ToCString().GetValue(), resGui.getString("Dlg-Color")); |
428 | } |
429 | |
430 | SampleAISSelectPackage.EndSelectFace(myAISContext); |
431 | myCurrentMode = CurAction3d_Default; |
432 | SamplesStarter.put_info(""); |
433 | } |
434 | } |
435 | } |
436 | } |
437 | |
438 | public void mouseEntered(MouseEvent e) |
439 | { |
440 | } |
441 | |
442 | public void mouseExited(MouseEvent e) |
443 | { |
444 | } |
445 | |
446 | //=======================================================================// |
447 | // MouseMotionListener interface |
448 | //=======================================================================// |
449 | public void mouseDragged(MouseEvent e) |
450 | { |
451 | if (SwingUtilities.isLeftMouseButton(e) && |
452 | e.getSource() == myView3d) |
453 | Dragged = true; |
454 | } |
455 | |
456 | public void mouseMoved(MouseEvent e) |
457 | { |
458 | Object src = e.getSource(); |
459 | |
460 | if (src == myView3d) |
461 | { |
462 | V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView(); |
463 | if (myAISContext != null && view3d != null) |
464 | myAISContext.MoveTo(e.getX(), e.getY(), view3d); |
465 | } |
466 | } |
467 | |
468 | |
469 | } |
470 | |