1 package fr.ove.openmath.mathematica;
2
3 import javax.swing.*;
4 import javax.swing.border.*;
5 import javax.swing.event.*;
6 import java.awt.*;
7 import java.awt.event.*;
8 import java.util.*;
9 import fr.ove.utils.ConnectionConfigurable;
10 /***
11 * A connection parameter setter.
12 */
13 public class ConnectionConfigDialog extends JDialog {
14 /***
15 * A connection configurable object.
16 */
17 private ConnectionConfigurable configurable;
18
19 /***
20 * The textfield for entering the host name.
21 */
22 private JTextField entryHost;
23
24 /***
25 * The textfield for entering the port number.
26 */
27 private JTextField entryPort;
28
29
30 /***
31 * The Constructor.
32 * @param configurableObj a connection configurable object.
33 */
34 public ConnectionConfigDialog(JFrame frame, ConnectionConfigurable configurableObj) {
35 super(frame, "Connection Configuration", true);
36 configurable = configurableObj;
37
38 JPanel contentPane = new JPanel(new GridBagLayout());
39
40 TitledBorder border = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "");
41
42 contentPane.setBorder(border);
43
44 GridBagConstraints constraints = new GridBagConstraints();
45 constraints.weightx = 1.0;
46 constraints.weighty = 1.0;
47 constraints.fill = GridBagConstraints.HORIZONTAL;
48
49 JLabel label = new JLabel("Host Name : ", SwingConstants.RIGHT);
50 constraints.anchor = GridBagConstraints.WEST;
51 contentPane.add(label, constraints);
52
53 label = new JLabel("Port Number : ", SwingConstants.RIGHT);
54 constraints.gridx = 0;
55 constraints.gridy = 1;
56 constraints.anchor = GridBagConstraints.WEST;
57 contentPane.add(label, constraints);
58
59 entryHost = new JTextField(10);
60 constraints.gridx = 1;
61 constraints.gridy = 0;
62 constraints.fill = GridBagConstraints.NONE;
63 constraints.insets = new Insets(0, 10, 0, 0);
64 constraints.anchor = GridBagConstraints.EAST;
65 contentPane.add(entryHost, constraints);
66
67 entryPort = new JTextField(4);
68 constraints.gridy = 1;
69 constraints.anchor = GridBagConstraints.WEST;
70 contentPane.add(entryPort, constraints);
71
72 Box panelButtons = new Box(BoxLayout.X_AXIS);
73 JButton button = new JButton("OK");
74 button.setToolTipText("Confirm data values");
75 button.addActionListener(
76 new ActionListener() {
77 public void actionPerformed(ActionEvent ae) {
78 configurable.setHostName(entryHost.getText());
79 try {
80 int port = Integer.parseInt(entryPort.getText());
81 configurable.setPortNumber(port);
82 hide();
83 }
84 catch (NumberFormatException nfe) {
85 JOptionPane jop = new JOptionPane("String invalid port number : " + entryPort.getText(),
86 JOptionPane.ERROR_MESSAGE,
87 JOptionPane.DEFAULT_OPTION);
88
89 JDialog dialog = jop.createDialog(ConnectionConfigDialog.this, "Invalid port Number");
90 dialog.show();
91 }
92 }
93 }
94 );
95 panelButtons.add(button);
96
97 panelButtons.add(Box.createHorizontalGlue());
98
99 button = new JButton("Cancel");
100 button.setToolTipText("Ignore data values");
101 button.addActionListener(
102 new ActionListener() {
103 public void actionPerformed(ActionEvent ae) {
104 hide();
105 }
106 }
107 );
108 panelButtons.add(button);
109
110 constraints.gridx = 0;
111 constraints.gridy = 2;
112 constraints.fill = GridBagConstraints.HORIZONTAL;
113 constraints.anchor = GridBagConstraints.CENTER;
114 constraints.gridwidth = 2;
115 constraints.insets = new Insets(15, 0, 0, 0);
116
117 contentPane.add(panelButtons, constraints);
118
119 setContentPane(contentPane);
120
121 pack();
122 }
123
124 /***
125 * Returns the connection configurable object.
126 */
127 public ConnectionConfigurable getConnectionConfigurable() {
128 return configurable;
129 }
130
131 /***
132 * Sets the connection configurable object
133 * @param configurable the connection configurable object.
134 */
135 public void setConnectionConfigurable(ConnectionConfigurable configurable) {
136 this.configurable = configurable;
137 }
138
139 /***
140 * Shows the instance.
141 */
142 public void show() {
143 entryHost.setText(configurable.getHostName());
144 entryPort.setText("" + configurable.getPortNumber());
145 super.show();
146 }
147 }