d95398b76491dcdda8f79ac57e453be5fbad3c6e
[public/netxms.git] / src / java / netxms-eclipse / ServerConfig / src / org / netxms / ui / eclipse / serverconfig / dialogs / AddAddressListElementDialog.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2011 Victor Kirhenshtein
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 package org.netxms.ui.eclipse.serverconfig.dialogs;
20
21 import java.net.InetAddress;
22 import java.net.UnknownHostException;
23 import org.eclipse.jface.dialogs.Dialog;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.events.SelectionListener;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Shell;
33 import org.netxms.client.IpAddressListElement;
34 import org.netxms.ui.eclipse.serverconfig.Messages;
35 import org.netxms.ui.eclipse.tools.MessageDialogHelper;
36 import org.netxms.ui.eclipse.tools.WidgetHelper;
37 import org.netxms.ui.eclipse.widgets.LabeledText;
38
39 /**
40 * Dialog for adding address list element
41 */
42 public class AddAddressListElementDialog extends Dialog
43 {
44 private Button radioSubnet;
45 private Button radioRange;
46 private LabeledText textAddr1;
47 private LabeledText textAddr2;
48 private InetAddress address1;
49 private InetAddress address2;
50 private int type;
51
52 /**
53 * @param parentShell
54 */
55 public AddAddressListElementDialog(Shell parentShell)
56 {
57 super(parentShell);
58 }
59
60 /* (non-Javadoc)
61 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
62 */
63 @Override
64 protected void configureShell(Shell newShell)
65 {
66 super.configureShell(newShell);
67 newShell.setText(Messages.get().AddAddressListElementDialog_Title);
68 }
69
70 /* (non-Javadoc)
71 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
72 */
73 @Override
74 protected Control createDialogArea(Composite parent)
75 {
76 Composite dialogArea = (Composite)super.createDialogArea(parent);
77
78 GridLayout layout = new GridLayout();
79 layout.marginHeight = WidgetHelper.DIALOG_HEIGHT_MARGIN;
80 layout.marginWidth = WidgetHelper.DIALOG_WIDTH_MARGIN;
81 layout.verticalSpacing = WidgetHelper.DIALOG_SPACING;
82 dialogArea.setLayout(layout);
83
84 radioSubnet = new Button(dialogArea, SWT.RADIO);
85 radioSubnet.setText(Messages.get().AddAddressListElementDialog_Subnet);
86 radioSubnet.setSelection(true);
87 radioSubnet.addSelectionListener(new SelectionListener() {
88 @Override
89 public void widgetSelected(SelectionEvent e)
90 {
91 textAddr1.setLabel(Messages.get().AddAddressListElementDialog_NetAddr);
92 textAddr2.setLabel(Messages.get().AddAddressListElementDialog_NetMask);
93 }
94
95 @Override
96 public void widgetDefaultSelected(SelectionEvent e)
97 {
98 widgetSelected(e);
99 }
100 });
101
102 radioRange = new Button(dialogArea, SWT.RADIO);
103 radioRange.setText(Messages.get().AddAddressListElementDialog_AddrRange);
104 radioRange.addSelectionListener(new SelectionListener() {
105 @Override
106 public void widgetSelected(SelectionEvent e)
107 {
108 textAddr1.setLabel(Messages.get().AddAddressListElementDialog_StartAddr);
109 textAddr2.setLabel(Messages.get().AddAddressListElementDialog_EndAddr);
110 }
111
112 @Override
113 public void widgetDefaultSelected(SelectionEvent e)
114 {
115 widgetSelected(e);
116 }
117 });
118
119 textAddr1 = new LabeledText(dialogArea, SWT.NONE);
120 textAddr1.setLabel(Messages.get().AddAddressListElementDialog_NetAddr);
121 textAddr1.setText("0.0.0.0"); //$NON-NLS-1$
122 GridData gd = new GridData();
123 gd.horizontalAlignment = SWT.FILL;
124 gd.grabExcessHorizontalSpace = true;
125 gd.widthHint = 300;
126 textAddr1.setLayoutData(gd);
127
128 textAddr2 = new LabeledText(dialogArea, SWT.NONE);
129 textAddr2.setLabel(Messages.get().AddAddressListElementDialog_NetMask);
130 textAddr2.setText("255.255.255.0"); //$NON-NLS-1$
131 gd = new GridData();
132 gd.horizontalAlignment = SWT.FILL;
133 gd.grabExcessHorizontalSpace = true;
134 textAddr2.setLayoutData(gd);
135
136 return dialogArea;
137 }
138
139 /* (non-Javadoc)
140 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
141 */
142 @Override
143 protected void okPressed()
144 {
145 type = radioSubnet.getSelection() ? IpAddressListElement.SUBNET : IpAddressListElement.RANGE;
146 try
147 {
148 address1 = InetAddress.getByName(textAddr1.getText());
149 address2 = InetAddress.getByName(textAddr2.getText());
150 }
151 catch(UnknownHostException e)
152 {
153 MessageDialogHelper.openWarning(getShell(), Messages.get().AddAddressListElementDialog_Warning, Messages.get().AddAddressListElementDialog_EnterValidData);
154 return;
155 }
156 super.okPressed();
157 }
158
159 /**
160 * @return the address1
161 */
162 public InetAddress getAddress1()
163 {
164 return address1;
165 }
166
167 /**
168 * @return the address2
169 */
170 public InetAddress getAddress2()
171 {
172 return address2;
173 }
174
175 /**
176 * @return the type
177 */
178 public int getType()
179 {
180 return type;
181 }
182 }