Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / java / java / CASCADEView2d.java
CommitLineData
7fd59977 1
2//Title: OpenCASCADE Samples
3//Version:
4//Copyright: Copyright (c) 1999
5//Author: User Interface group
6//Company: Matra Datavision
7//Description:
8
9import java.awt.*;
10import java.awt.event.*;
11import javax.swing.*;
12import jcas.*;
13import CASCADESamplesJni.*;
14import util.*;
15
16
17public class CASCADEView2d extends ViewCanvas
18 implements FocusListener
19{
20 V2d_Viewer myViewer = null;
21 V2d_View myView = null;
22 short myBackColor = Quantity_NameOfColor.Quantity_NOC_AQUAMARINE4;
23
24 private double currScale = 1.0;
25
26 //-----------------------------------------------------------//
27 // 2d Grid management
28 //-----------------------------------------------------------//
29 private int gridState = 0; // 0 - no active grid
30 // 1 - active rectangular grid
31 // 2 - active circular grid
32
33 // Rectangular grid
34 private RealSpin ctlXOrigin = new RealSpin();
35 private RealSpin ctlYOrigin = new RealSpin();
36 private RealSpin ctlXStep = new RealSpin();
37 private RealSpin ctlYStep = new RealSpin();
38 private RealSpin ctlAngle = new RealSpin();
39 private JPanel RectGridPane = new JPanel(new GridLayout(5, 2, 0, 4));
40
41 // Circular grid
42 private RealSpin ctlXOrigC = new RealSpin();
43 private RealSpin ctlYOrigC = new RealSpin();
44 private RealSpin ctlRadStep = new RealSpin();
45 private IntegerSpin ctlDivision = new IntegerSpin();
46 private RealSpin ctlAngleRot = new RealSpin();
47 private JPanel CircGridPane = new JPanel(new GridLayout(5, 2, 0, 4));
48
49 // Buttons
50 private JButton btnClose = new JButton(resGui.getString("BT_CLOSE"));
51 private JButton btnHelp = new JButton(resGui.getString("BT_HELP"));
52 private JPanel ButtonsPane = new JPanel();
53 private JDialog RectGridDlg= null;
54 private JDialog CircGridDlg = null;
55
56
57//=======================================================================//
58// Constructor //
59//=======================================================================//
60 public CASCADEView2d(V2d_Viewer viewer)
61 {
62 myViewer = viewer;
63 createDialogs();
64 }
65
66 private void createDialogs()
67 {
68 // Rectangular
69 RectGridPane.setBorder(BorderFactory.createEmptyBorder(2, 8, 2, 8));
70 RectGridPane.add(new JLabel(resGui.getString("TXT_XORIGIN")));
71 ctlXOrigin.addFocusListener(this);
72 RectGridPane.add(ctlXOrigin);
73
74 RectGridPane.add(new JLabel(resGui.getString("TXT_YORIGIN")));
75 ctlYOrigin.addFocusListener(this);
76 RectGridPane.add(ctlYOrigin);
77
78 RectGridPane.add(new JLabel(resGui.getString("TXT_XSTEP")));
79 ctlXStep.addFocusListener(this);
80 RectGridPane.add(ctlXStep);
81
82 RectGridPane.add(new JLabel(resGui.getString("TXT_YSTEP")));
83 ctlYStep.addFocusListener(this);
84 RectGridPane.add(ctlYStep);
85
86 RectGridPane.add(new JLabel(resGui.getString("TXT_ANGLE")));
87 ctlAngle.addFocusListener(this);
88 RectGridPane.add(ctlAngle);
89
90 // Circular
91 CircGridPane.setBorder(BorderFactory.createEmptyBorder(2, 8, 2, 8));
92 CircGridPane.add(new JLabel(resGui.getString("TXT_XORIGIN")));
93 ctlXOrigC.addFocusListener(this);
94 CircGridPane.add(ctlXOrigC);
95
96 CircGridPane.add(new JLabel(resGui.getString("TXT_YORIGIN")));
97 ctlYOrigC.addFocusListener(this);
98 CircGridPane.add(ctlYOrigC);
99
100 CircGridPane.add(new JLabel(resGui.getString("TXT_RADSTEP")));
101 ctlRadStep.addFocusListener(this);
102 CircGridPane.add(ctlRadStep);
103
104 CircGridPane.add(new JLabel(resGui.getString("TXT_DIVISION")));
105 ctlDivision.addFocusListener(this);
106 CircGridPane.add(ctlDivision);
107
108 CircGridPane.add(new JLabel(resGui.getString("TXT_ANGLE")));
109 ctlAngleRot.addFocusListener(this);
110 CircGridPane.add(ctlAngleRot);
111
112 // Buttons
113 ButtonsPane.setBorder(BorderFactory.createCompoundBorder(
114 BorderFactory.createMatteBorder(1,0,0,0,SystemColor.controlShadow),
115 BorderFactory.createMatteBorder(1,0,0,0,SystemColor.controlLtHighlight)));
116 btnClose.setActionCommand("CloseGridDlg");
117 btnClose.addActionListener(this);
118 ButtonsPane.add(btnClose);
119 btnHelp.setActionCommand("HelpGridDlg");
120 btnHelp.addActionListener(this);
121 ButtonsPane.add(btnHelp);
122 }
123
124
125//=======================================================================//
126// Redefined operations
127//=======================================================================//
128 /**
129 * Creates a new CASCADE V2d_View object and
130 * sets a handle of native window to CASCADE view.
131 */
132 public V2d_View createView2d(V2d_Viewer viewer, int hiwin, int lowin)
133 {
134 return null;
135 }
136
137//=======================================================================//
138// public void SetWindow()
139// {
140// int windowHandle = getWinData();
141// short hi, lo;
142// lo = (short) windowHandle;
143// hi = (short) (windowHandle >> 16);
144
145// myView = createView2d(myViewer, hi, lo);
146// if (myView != null)
147// myView.Fitall();
148// }
149
150//=======================================================================//
151 public void FitAll()
152 {
153 if (myView != null)
154 myView.Fitall();
155 }
156
157//=======================================================================//
158 public void WindowFit(Rectangle rect)
159 {
160 if (myView != null)
161 myView.WindowFit(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
162 }
163
164//=======================================================================//
165 public void Zoom(int x0, int y0, int x, int y)
166 {
167 if (myView != null)
168 myView.Zoom(x0, y0, x, y, 0.005);
169 }
170
171//=======================================================================//
172 public void ActivateGlobalPanning()
173 {
174 super.ActivateGlobalPanning();
175 if (myView != null)
176 {
177 currScale = myView.Zoom();
178 myView.Fitall();
179 }
180 }
181
182//=======================================================================//
183 public void Pan(int dx, int dy)
184 {
185 if (myView != null)
186 myView.Pan(dx, dy);
187 }
188
189//=======================================================================//
190 public void SetCenter(int x, int y)
191 {
192 if (myView != null)
193 myView.Place(x, y, currScale);
194 }
195
196//=======================================================================//
197 public void ResetView()
198 {
199 if (myView != null)
200 myView.Reset();
201 }
202
203//=======================================================================//
204 public void EditGrid()
205 {
206 if (RectGridDlg != null) return;
207 if (CircGridDlg != null) return;
208
209 if (gridState == 1)
210 {
211 Standard_Real XOrigin = new Standard_Real(),
212 YOrigin = new Standard_Real(),
213 XStep = new Standard_Real(),
214 YStep = new Standard_Real(),
215 aAngle = new Standard_Real();
216 myViewer.RectangularGridValues(XOrigin, YOrigin, XStep, YStep, aAngle);
217
218 ctlXOrigin.setValue(XOrigin.GetValue());
219 ctlYOrigin.setValue(YOrigin.GetValue());
220 ctlXStep.setValue(XStep.GetValue());
221 ctlYStep.setValue(YStep.GetValue());
222 ctlAngle.setValue(aAngle.GetValue());
223
224 Frame mainFrame = SamplesStarter.getFrame();
225 RectGridDlg = new JDialog(mainFrame, resGui.getString("DLG_RECTEDIT"), true);
226 RectGridDlg.setSize(180, 200);
227 RectGridDlg.getContentPane().setLayout(new BorderLayout());
228 RectGridDlg.getContentPane().add(RectGridPane, BorderLayout.CENTER);
229 RectGridDlg.getContentPane().add(ButtonsPane, BorderLayout.SOUTH);
230
231 Point aLoc = mainFrame.getLocation();
232 RectGridDlg.setLocation(aLoc.x + mainFrame.getWidth()/2 - RectGridDlg.getWidth()/2,
233 aLoc.y + mainFrame.getHeight()/2 - RectGridDlg.getHeight()/2);
234 RectGridDlg.show();
235 }
236 else if (gridState == 2)
237 {
238 Standard_Real XOrigin = new Standard_Real(),
239 YOrigin = new Standard_Real(),
240 RadStep = new Standard_Real(),
241 aAngle = new Standard_Real();
242 Standard_Integer DivN = new Standard_Integer();
243 myViewer.CircularGridValues(XOrigin, YOrigin, RadStep, DivN, aAngle);
244
245 ctlXOrigC.setValue(XOrigin.GetValue());
246 ctlYOrigC.setValue(YOrigin.GetValue());
247 ctlRadStep.setValue(RadStep.GetValue());
248 ctlDivision.setValue(DivN.GetValue());
249 ctlAngleRot.setValue(aAngle.GetValue());
250
251 Frame mainFrame = SamplesStarter.getFrame();
252 CircGridDlg = new JDialog(mainFrame, resGui.getString("DLG_CIRCEDIT"), true);
253 CircGridDlg.setSize(180, 200);
254 CircGridDlg.getContentPane().setLayout(new BorderLayout());
255 CircGridDlg.getContentPane().add(CircGridPane, BorderLayout.CENTER);
256 CircGridDlg.getContentPane().add(ButtonsPane, BorderLayout.SOUTH);
257
258 Point aLoc = mainFrame.getLocation();
259 CircGridDlg.setLocation(aLoc.x + mainFrame.getWidth()/2 - CircGridDlg.getWidth()/2,
260 aLoc.y + mainFrame.getHeight()/2 - CircGridDlg.getHeight()/2);
261 CircGridDlg.show();
262 }
263 }
264
265//=======================================================================//
266 public void EraseGrid()
267 {
268 myViewer.DeactivateGrid();
269 gridState = 0;
270 }
271
272//=======================================================================//
273 public void ActivateGrid(String type)
274 {
275 if (type.equals("RectLine"))
276 {
277 myViewer.ActivateGrid(Aspect_GridType.Aspect_GT_Rectangular,
278 Aspect_GridDrawMode.Aspect_GDM_Lines);
279 gridState = 1;
280 }
281 else if (type.equals("RectPoint"))
282 {
283 myViewer.ActivateGrid(Aspect_GridType.Aspect_GT_Rectangular,
284 Aspect_GridDrawMode.Aspect_GDM_Points);
285 gridState = 1;
286 }
287 else if (type.equals("CircLine"))
288 {
289 myViewer.ActivateGrid(Aspect_GridType.Aspect_GT_Circular,
290 Aspect_GridDrawMode.Aspect_GDM_Lines);
291 gridState = 2;
292 }
293 else if (type.equals("CircPoint"))
294 {
295 myViewer.ActivateGrid(Aspect_GridType.Aspect_GT_Circular,
296 Aspect_GridDrawMode.Aspect_GDM_Points);
297 gridState = 2;
298 }
299 }
300
301//=======================================================================//
302// public void paint(Graphics g)
303// {
304// if (myView != null)
305// {
306// if (needResize)
307// {
308// myView.MustBeResized(V2d_TypeOfWindowResizingEffect.V2d_TOWRE_ENLARGE_OBJECTS);
309// needResize = false;
310// }
311// myView.Update();
312// }
313// super.paint(g);
314// }
315 public native void paint(Graphics g);
316
317//=======================================================================//
318 public V2d_View getView()
319 {
320 return myView;
321 }
322
323
324//=======================================================================//
325// ActionListener //
326//=======================================================================//
327 public void actionPerformed(ActionEvent e)
328 {
329 String nameAction = e.getActionCommand();
330 if (nameAction.equals("CloseGridDlg"))
331 {
332 if (gridState == 1)
333 {
334 RectGridDlg.dispose();
335 RectGridDlg = null;
336 }
337 else if (gridState == 2)
338 {
339 CircGridDlg.dispose();
340 CircGridDlg = null;
341 }
342 }
343 else
344 super.actionPerformed(e);
345 }
346
347//=======================================================================//
348// FocusListener //
349//=======================================================================//
350 public void focusGained(FocusEvent e)
351 {
352 }
353
354 public void focusLost(FocusEvent e)
355 {
356 if (RectGridDlg != null)
357 myViewer.SetRectangularGridValues(ctlXOrigin.getValue(),
358 ctlYOrigin.getValue(),
359 ctlXStep.getValue(),
360 ctlYStep.getValue(),
361 ctlAngle.getValue());
362 if (CircGridDlg != null)
363 myViewer.SetCircularGridValues(ctlXOrigC.getValue(),
364 ctlYOrigC.getValue(),
365 ctlRadStep.getValue(),
366 ctlDivision.getValue(),
367 ctlAngleRot.getValue());
368 }
369
370
371}