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.ResourceBundle; |
14 | import jcas.Standard_CString; |
15 | import jcas.Standard_Integer; |
16 | import jcas.Standard_Real; |
17 | import CASCADESamplesJni.*; |
18 | import SampleDisplayAnimationJni.*; |
19 | import util.*; |
20 | |
21 | public class SampleDisplayAnimationPanel extends SamplePanel |
22 | implements ActionListener, |
23 | MouseListener, |
24 | MouseMotionListener |
25 | { |
26 | //-----------------------------------------------------------// |
27 | // Resources |
28 | //-----------------------------------------------------------// |
29 | static protected ResourceBundle resGui = |
30 | ResourceBundle.getBundle("SampleDisplayAnimation"); |
31 | |
32 | static private ImageIcon imgRestart = new ImageIcon(resGui.getString("Icon-Restart")); |
33 | static private ImageIcon imgStop = new ImageIcon(resGui.getString("Icon-Stop")); |
34 | static private ImageIcon imgThread = new ImageIcon(resGui.getString("Icon-Thread")); |
35 | static private ImageIcon imgDeviation = new ImageIcon(resGui.getString("Icon-Deviation")); |
36 | |
37 | |
38 | //-----------------------------------------------------------// |
39 | // Components |
40 | //-----------------------------------------------------------// |
41 | private ViewPanel myView3d; |
42 | |
43 | private static V3d_Viewer myViewer3d = null; |
44 | |
45 | private static AIS_InteractiveContext myAISContext = null; |
46 | private static boolean isDataLoaded = false; |
47 | private Timer myTimer; |
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 | public static void setDataLoaded(boolean b) |
67 | { |
68 | isDataLoaded = b; |
69 | } |
70 | |
71 | |
72 | |
73 | //=======================================================================// |
74 | // Constructor // |
75 | //=======================================================================// |
76 | public SampleDisplayAnimationPanel() |
77 | { |
78 | super(false); |
79 | |
80 | // 3D Initialization |
81 | //------------------------------------------ |
82 | myViewer3d.SetDefaultLights(); |
83 | myViewer3d.SetLightOn(); |
84 | |
85 | // Moved to the createViewPanel() function |
86 | |
87 | if (myAISContext == null) |
88 | myAISContext = new AIS_InteractiveContext(myViewer3d); |
89 | |
90 | String path = System.getProperty("user.dir") + |
91 | System.getProperty("file.separator") + |
92 | "data" + System.getProperty("file.separator"); |
93 | isDataLoaded = SampleDisplayAnimationPackage.LoadData(myAISContext, |
94 | new Standard_CString(path)); |
95 | |
96 | |
97 | myTimer = new Timer(1, this); |
98 | myTimer.setRepeats(true); |
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 = SampleDisplayAnimationPackage.CreateViewer("DisplayAnimation"); |
112 | myAISContext = new AIS_InteractiveContext(myViewer3d); |
113 | |
114 | myView3d = new ViewPanel() { |
115 | public ViewCanvas createViewPort() { |
116 | return new CASCADEView3d(SampleDisplayAnimationPanel.getViewer3d()) { |
117 | public void setWindow3d(V3d_View view, int hiwin, int lowin) { |
118 | // set the native window |
119 | SampleDisplayAnimationPackage.SetWindow(view, hiwin, lowin); |
120 | |
121 | // loading BRep data... |
122 | SamplesStarter.getFrame().setCursor( |
123 | Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
124 | String path = System.getProperty("user.dir") + |
125 | System.getProperty("file.separator") + |
126 | "data" + System.getProperty("file.separator"); |
127 | SampleDisplayAnimationPanel.setDataLoaded( |
128 | SampleDisplayAnimationPackage.LoadData( |
129 | SampleDisplayAnimationPanel.getAISContext(), |
130 | new Standard_CString(path))); |
131 | SamplesStarter.getFrame().setCursor( |
132 | Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
133 | |
134 | } |
135 | }; |
136 | } |
137 | }; |
138 | |
139 | myView3d.addMouseListener(this); |
140 | myView3d.addMouseMotionListener(this); |
141 | |
142 | |
143 | // Layout |
144 | //------------------------------------------ |
145 | mainPanel.add(myView3d, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, |
146 | GridBagConstraints.CENTER, GridBagConstraints.BOTH, |
147 | new Insets(0, 0, 0, 0), 0, 0)); |
148 | |
149 | return mainPanel; |
150 | } |
151 | |
152 | //-----------------------------------------------------------------------// |
153 | public Component createToolbar() |
154 | { |
155 | JToolBar tools = (JToolBar) super.createToolbar(); |
156 | |
157 | Insets margin = new Insets(1, 1, 1, 1); |
158 | JButton button; |
159 | |
160 | button = new HeavyButton(imgRestart); |
161 | button.setToolTipText(resGui.getString("Help-Restart")); |
162 | button.setActionCommand("Restart"); |
163 | button.addActionListener(this); |
164 | button.setMargin(margin); |
165 | tools.add(button); |
166 | |
167 | button = new HeavyButton(imgStop); |
168 | button.setToolTipText(resGui.getString("Help-Stop")); |
169 | button.setActionCommand("Stop"); |
170 | button.addActionListener(this); |
171 | button.setMargin(margin); |
172 | tools.add(button); |
173 | |
174 | button = new HeavyButton(imgThread); |
175 | button.setToolTipText(resGui.getString("Help-Thread")); |
176 | button.setActionCommand("Thread"); |
177 | button.addActionListener(this); |
178 | button.setMargin(margin); |
179 | tools.add(button); |
180 | |
181 | button = new HeavyButton(imgDeviation); |
182 | button.setToolTipText(resGui.getString("Help-Deviation")); |
183 | button.setActionCommand("Deviation"); |
184 | button.addActionListener(this); |
185 | button.setMargin(margin); |
186 | tools.add(button); |
187 | |
188 | return tools; |
189 | } |
190 | |
191 | |
192 | //=======================================================================// |
193 | // Actions // |
194 | //=======================================================================// |
195 | public void onRestart() |
196 | { |
197 | if (!isDataLoaded) |
198 | { |
199 | JOptionPane.showMessageDialog(this, |
200 | "Shape(s) not found.\n" + |
201 | "Check the data directory path!", |
202 | "Warning!!!", JOptionPane.WARNING_MESSAGE); |
203 | return; |
204 | } |
205 | myTimer.start(); |
206 | } |
207 | |
208 | //=======================================================================// |
209 | public void onStop() |
210 | { |
211 | myTimer.stop(); |
212 | } |
213 | |
214 | //=======================================================================// |
215 | public void onThread() |
216 | { |
217 | onStop(); |
218 | ThreadDlg aDlg = new ThreadDlg(SamplesStarter.getFrame(), |
219 | SampleDisplayAnimationPackage.GetAngleIncrement()); |
220 | Position.centerWindow(aDlg); |
221 | aDlg.show(); |
222 | |
223 | if (aDlg.isOK()) |
224 | SampleDisplayAnimationPackage.SetAngleIncrement(aDlg.getValue()); |
225 | |
226 | onRestart(); |
227 | } |
228 | |
229 | //=======================================================================// |
230 | public void onDeviation() |
231 | { |
232 | onStop(); |
233 | |
234 | double value = SampleDisplayAnimationPackage.GetDeviationCoefficient(); |
235 | |
236 | DeviationDlg aDlg = new DeviationDlg(SamplesStarter.getFrame(), |
237 | (int) ((value-0.00003)/0.00003)); |
238 | |
239 | Position.centerWindow(aDlg); |
240 | aDlg.show(); |
241 | |
242 | if (aDlg.isOK() && isDataLoaded) |
243 | { |
244 | int newValue = aDlg.getValue(); |
245 | SampleDisplayAnimationPackage.SetDeviationCoefficient(myAISContext, |
246 | (newValue*0.00003)+0.00003); |
247 | } |
248 | |
249 | onRestart(); |
250 | } |
251 | |
252 | //=======================================================================// |
253 | public void onTimer() |
254 | { |
255 | if (isDataLoaded) |
256 | SampleDisplayAnimationPackage.ChangePosition(myAISContext); |
257 | } |
258 | |
259 | |
260 | //=======================================================================// |
261 | public void setVisible(boolean b) |
262 | { |
263 | if (!b) onStop(); |
264 | super.setVisible(b); |
265 | } |
266 | |
267 | |
268 | //=======================================================================// |
269 | // ActionListener interface |
270 | //=======================================================================// |
271 | public void actionPerformed(ActionEvent e) |
272 | { |
273 | if (e.getSource().equals(myTimer)) |
274 | onTimer(); |
275 | else |
276 | { |
277 | String command = e.getActionCommand(); |
278 | |
279 | if (command.equals("Restart")) onRestart(); |
280 | else if (command.equals("Stop")) onStop(); |
281 | else if (command.equals("Thread")) onThread(); |
282 | else if (command.equals("Deviation")) onDeviation(); |
283 | } |
284 | } |
285 | |
286 | |
287 | //=======================================================================// |
288 | // MouseListener interface |
289 | //=======================================================================// |
290 | public void mouseClicked(MouseEvent e) |
291 | { |
292 | } |
293 | |
294 | //-----------------------------------------------------------------------// |
295 | public void mousePressed(MouseEvent e) |
296 | { |
297 | if (SwingUtilities.isLeftMouseButton(e)) |
298 | { |
299 | startX = e.getX(); |
300 | startY = e.getY(); |
301 | Object src = e.getSource(); |
302 | |
303 | if (src == myView3d) |
304 | { |
305 | V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView(); |
306 | if (view3d != null) |
307 | myAISContext.MoveTo(startX, startY, view3d); |
308 | } |
309 | } |
310 | else if (SwingUtilities.isRightMouseButton(e)) |
311 | { |
312 | if (e.getSource() == myView3d) |
313 | { |
314 | PopupMenu defPopup = myView3d.getDefaultPopup(); |
315 | myView3d.add(defPopup); |
316 | defPopup.show(myView3d, e.getX(), e.getY() + 30); |
317 | } |
318 | } |
319 | } |
320 | |
321 | //-----------------------------------------------------------------------// |
322 | public void mouseReleased(MouseEvent e) |
323 | { |
324 | if (SwingUtilities.isLeftMouseButton(e)) |
325 | { |
326 | Object src = e.getSource(); |
327 | |
328 | if (Dragged) |
329 | { |
330 | if (src == myView3d) |
331 | { |
332 | V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView(); |
333 | if (view3d != null) |
334 | { |
335 | if (e.isShiftDown()) |
336 | myAISContext.ShiftSelect(startX, startY, e.getX(), e.getY(), view3d, true); |
337 | else |
338 | myAISContext.Select(startX, startY, e.getX(), e.getY(), view3d, true); |
339 | } |
340 | } |
341 | } |
342 | else |
343 | { |
344 | if (src == myView3d) |
345 | { |
346 | if (e.isShiftDown()) |
347 | myAISContext.ShiftSelect(true); |
348 | else |
349 | myAISContext.Select(true); |
350 | } |
351 | } |
352 | Dragged = false; |
353 | } |
354 | } |
355 | |
356 | //-----------------------------------------------------------------------// |
357 | public void mouseEntered(MouseEvent e) |
358 | { |
359 | } |
360 | |
361 | //-----------------------------------------------------------------------// |
362 | public void mouseExited(MouseEvent e) |
363 | { |
364 | } |
365 | |
366 | //=======================================================================// |
367 | // MouseMotionListener interface |
368 | //=======================================================================// |
369 | public void mouseDragged(MouseEvent e) |
370 | { |
371 | if (SwingUtilities.isLeftMouseButton(e) && |
372 | e.getSource() == myView3d) |
373 | Dragged = true; |
374 | } |
375 | |
376 | //-----------------------------------------------------------------------// |
377 | public void mouseMoved(MouseEvent e) |
378 | { |
379 | Object src = e.getSource(); |
380 | |
381 | if (src == myView3d) |
382 | { |
383 | V3d_View view3d = ((CASCADEView3d) myView3d.getViewPort()).getView(); |
384 | if (myAISContext != null && view3d != null) |
385 | myAISContext.MoveTo(e.getX(), e.getY(), view3d); |
386 | } |
387 | } |
388 | |
389 | |
390 | } |
391 | |