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