Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / java / java / ZClippingDlg.java
1
2 //Title:        Viewer3D Sample
3 //Version:      
4 //Copyright:    Copyright (c) 1999
5 //Author:       User Interface group
6 //Company:      Matra Datavision
7 //Description:  
8
9
10 import java.awt.*;
11 import java.awt.event.*;
12 import javax.swing.*;
13 import javax.swing.event.*;
14 import CASCADESamplesJni.*;
15 import SampleViewer3DJni.*;
16 import jcas.Standard_Real;
17
18
19 public class ZClippingDlg extends JDialog
20                           implements ActionListener,
21                                      InputMethodListener,
22                                      ChangeListener,
23                                      KeyListener
24 {
25   private SamplePanel myDocument;
26   private V3d_View myView;
27
28   //-----------------------------------------------------------//
29   // GUI components
30   //-----------------------------------------------------------//
31   JComboBox cmbType;
32   JSlider sldDepth;
33   JSlider sldWidth;
34   JTextField txtDepth;
35   JTextField txtWidth;
36
37   private boolean consume = false;
38   private String strDepth = new String("");
39   private String strWidth = new String("");
40   private boolean userDepthChanged = false;
41   private boolean userWidthChanged = false;
42   
43
44 //=======================================================================//
45 // Construction
46 //=======================================================================//
47   public ZClippingDlg(Frame frame, SamplePanel aDoc, V3d_View aView)
48   {
49     super(frame, "ZClipping", false);
50     myDocument = aDoc;
51     myView = aView;
52
53     try
54     {
55       jbInit();
56       initValues();
57       pack();
58     }
59     catch(Exception ex)
60     {
61       ex.printStackTrace();
62     }
63   }
64
65   void jbInit() throws Exception
66   {
67     getContentPane().setLayout(new BorderLayout());
68
69     JPanel mainPanel = new JPanel(new GridBagLayout());
70     mainPanel.setBorder(BorderFactory.createRaisedBevelBorder());
71
72     JLabel lblDepth = new JLabel("Depth");
73     mainPanel.add(lblDepth, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
74             GridBagConstraints.WEST, GridBagConstraints.NONE,
75             new Insets(10, 10, 5, 5), 0, 0));
76
77     sldDepth = new JSlider(-1500, 1500, 0);
78     sldDepth.addChangeListener(this);
79     mainPanel.add(sldDepth, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
80             GridBagConstraints.CENTER, GridBagConstraints.NONE,
81             new Insets(10, 5, 5, 5), 0, 0));
82
83     txtDepth = new JTextField(6);
84     txtDepth.addInputMethodListener(this);
85     txtDepth.addKeyListener(this);
86     mainPanel.add(txtDepth, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
87             GridBagConstraints.CENTER, GridBagConstraints.NONE,
88             new Insets(10, 5, 5, 10), 0, 0));
89
90     JLabel lblWidth = new JLabel("Width");
91     mainPanel.add(lblWidth, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
92             GridBagConstraints.WEST, GridBagConstraints.NONE,
93             new Insets(5, 10, 10, 5), 0, 0));
94
95     sldWidth = new JSlider(0, 1500, 0);
96     sldWidth.addChangeListener(this);
97     mainPanel.add(sldWidth, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
98             GridBagConstraints.CENTER, GridBagConstraints.NONE,
99             new Insets(5, 5, 10, 5), 0, 0));
100
101     txtWidth = new JTextField(6);
102     txtWidth.addInputMethodListener(this);
103     txtWidth.addKeyListener(this);
104     mainPanel.add(txtWidth, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
105             GridBagConstraints.CENTER, GridBagConstraints.NONE,
106             new Insets(5, 5, 10, 10), 0, 0));
107
108     JLabel lblType = new JLabel("Type");
109     mainPanel.add(lblType, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
110             GridBagConstraints.WEST, GridBagConstraints.NONE,
111             new Insets(10, 10, 10, 5), 0, 0));
112
113     String[] items = {"OFF", "BACK", "FRONT", "SLICE"};
114     cmbType = new JComboBox(items);
115     cmbType.setSelectedIndex(0);
116     cmbType.setEditable(false);
117     cmbType.addActionListener(this);
118     cmbType.setActionCommand("Type");
119     mainPanel.add(cmbType, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
120             GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
121             new Insets(10, 10, 10, 10), 0, 0));
122     getContentPane().add(mainPanel, BorderLayout.CENTER);
123
124     JPanel controlPanel = new JPanel();
125
126     JButton btnOK = new JButton("OK");
127     btnOK.addActionListener(this);
128     btnOK.setActionCommand("OK");
129     controlPanel.add(btnOK);
130
131     JButton btnCancel = new JButton("Cancel");
132     btnCancel.addActionListener(this);
133     btnCancel.setActionCommand("Cancel");
134     controlPanel.add(btnCancel);
135     getContentPane().add(controlPanel, BorderLayout.SOUTH);
136   }
137
138   private void initValues()
139   {
140     Standard_Real depth = new Standard_Real();
141     Standard_Real width = new Standard_Real();
142     int type = myView.ZClipping(depth, width);
143
144 /*
145     if (type == V3d_TypeOfZclipping.V3d_OFF)
146       cmbType.setSelectedIndex(0);
147     else if (type == V3d_TypeOfZclipping.V3d_BACK)
148       cmbType.setSelectedIndex(1);
149     else if (type == V3d_TypeOfZclipping.V3d_FRONT)
150       cmbType.setSelectedIndex(2);
151     else if (type == V3d_TypeOfZclipping.V3d_SLICE)
152       cmbType.setSelectedIndex(3);
153 */
154     cmbType.setSelectedIndex(type);
155
156     sldDepth.setValue((int) Math.round(depth.GetValue()));
157     txtDepth.setText(String.valueOf(depth.GetValue()));
158
159     sldWidth.setValue((int) Math.round(width.GetValue()));
160     txtWidth.setText(String.valueOf(width.GetValue()));
161   }
162
163 //=======================================================================//
164 // Commands
165 //=======================================================================//
166   private void onDepthChanged()
167   {
168     String newValue = txtDepth.getText();
169     Double value = new Double((newValue.equals("") || newValue.equals("-"))?
170                               "0." : newValue);
171     if (value.doubleValue() > 1500. || value.doubleValue() < -1500.)
172       return;
173
174     TCollection_AsciiString message = new TCollection_AsciiString();
175     SampleViewer3DPackage.ChangeZClippingDepth(myView, value.doubleValue(), message);
176
177     myDocument.traceMessage(message.ToCString().GetValue(), "SetZClippingDepth");
178   }
179
180 //=======================================================================//
181   private void onWidthChanged()
182   {
183     String newValue = txtWidth.getText();
184     Double value = new Double((newValue.equals("") || newValue.equals("-"))?
185                               "0." : newValue);
186     if (value.doubleValue() > 1500. || value.doubleValue() <= 0.)
187       return;
188
189     TCollection_AsciiString message = new TCollection_AsciiString();
190     SampleViewer3DPackage.ChangeZClippingWidth(myView, value.doubleValue(), message);
191
192     myDocument.traceMessage(message.ToCString().GetValue(), "SetZClippingWidth");
193   }
194
195 //=======================================================================//
196   private void onTypeChanged()
197   {
198     TCollection_AsciiString message = new TCollection_AsciiString();
199     SampleViewer3DPackage.ChangeZClippingType(myView, (short)cmbType.getSelectedIndex(), message);
200
201     myDocument.traceMessage(message.ToCString().GetValue(), "SetZClippingType");
202   }
203
204 //=======================================================================//
205   private void close()
206   {
207     dispose();
208   }
209
210
211 //=======================================================================//
212 // Action listener interface
213 //=======================================================================//
214   public void actionPerformed(ActionEvent event)
215   {
216     String nameAction = event.getActionCommand();
217     if (nameAction.equals("Type"))
218       onTypeChanged();
219     else if (nameAction.equals("OK"))
220       close();
221     else if (nameAction.equals("Cancel"))
222       close();
223   }
224
225
226 //=======================================================================//
227 // Change listener interface
228 //=======================================================================//
229   public void stateChanged(ChangeEvent event)
230   {
231     JSlider slider = (JSlider) event.getSource();
232     if (slider.equals(sldDepth))
233     {
234       if (userDepthChanged)
235       {
236         txtDepth.setText(String.valueOf(slider.getValue()));
237         onDepthChanged();
238       }
239       else
240         userDepthChanged = true;
241     }
242     else if (slider.equals(sldWidth))
243     {
244       if (userWidthChanged)
245       {
246         txtWidth.setText(String.valueOf(slider.getValue()));
247         onWidthChanged();
248       }
249       else
250         userWidthChanged = true;
251     }
252   }
253
254 //=======================================================================//
255 // Key listener interface
256 //=======================================================================//
257   public void keyTyped(KeyEvent event)
258   {
259   }
260
261 //=======================================================================//
262   public void keyPressed(KeyEvent event)
263   {
264     JTextField field = (JTextField) event.getSource();
265
266     int aKod = event.getKeyCode();
267     if (aKod == event.VK_MINUS)
268     {
269       String aStr = field.getText();
270       int aPos = aStr.indexOf("-");
271
272       if (aPos == -1) // Minus not present
273       {
274         if ((field.getCaretPosition()) != 0)
275           consume = true;
276       }
277       else
278         consume = true;
279     }
280     else if ((aKod == event.VK_DECIMAL) || (aKod == event.VK_PERIOD))
281     {
282       String aStr = field.getText();
283       int aPos = aStr.indexOf(".");
284       if (aPos != -1) // the point is present in the string
285         consume = true;
286     }
287     else if (!event.isActionKey() && aKod != event.VK_BACK_SPACE &&
288                                      aKod != event.VK_DELETE)
289     {
290       if (!Character.isDigit(event.getKeyChar()))
291         consume = true;
292     }
293   }
294
295 //=======================================================================//
296   public void keyReleased(KeyEvent event)
297   {
298     JTextField field = (JTextField) event.getSource();
299     String newValue = field.getText();
300     Double value = new Double((newValue.equals("") || newValue.equals("-"))?
301                                "0." : newValue);
302
303     if (field.equals(txtDepth))
304     {
305       if (!newValue.equals(strDepth))
306       {
307         strDepth = newValue;
308         if (value.doubleValue() < -1500. || value.doubleValue() > 1500.)
309         {
310           txtDepth.selectAll();
311           JOptionPane.showMessageDialog(this, "Please enter a value between -1500 and 1500",
312                                         "Warning!!!", JOptionPane.WARNING_MESSAGE);
313           requestFocus();
314         }
315         else
316         {
317           userDepthChanged = false;
318           sldDepth.setValue((int) Math.round(value.doubleValue()));
319           onDepthChanged();
320         }
321       }
322     }
323     else if (field.equals(txtWidth))
324     {
325       if (!newValue.equals(strWidth))
326       {
327         strWidth = newValue;
328         if (value.doubleValue() <= 0. || value.doubleValue() > 1500.)
329         {
330           txtWidth.selectAll();
331           JOptionPane.showMessageDialog(this, "Please enter a value between 0 and 1500",
332                                         "Warning!!!", JOptionPane.WARNING_MESSAGE);
333           requestFocus();
334         }
335         else
336         {
337           userWidthChanged = false;
338           sldWidth.setValue((int) Math.round(value.doubleValue()));
339           onWidthChanged();
340         }
341       }
342     }
343   }
344
345 //=======================================================================//
346 // InputMethod listener interface
347 //=======================================================================//
348   public void inputMethodTextChanged(InputMethodEvent event)
349   {
350     if (consume)
351     {
352       event.consume();
353       consume = false;
354     }
355   }
356
357   public void caretPositionChanged(InputMethodEvent event)
358   {
359   }
360 }