FTPPrefsPanel.java Sample

This topic inludes the source code for the FTPPrefsPanel.java Sample.

Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/ExtensionDevKit/IdeDevKit/PopupAction/src/ideExtensions/popupAction/

Sample Source Code


001 package ideExtensions.popupAction;
002 
003 import com.bea.ide.core.MessageSvc;
004 import com.bea.ide.core.ResourceSvc;
005 import com.bea.ide.swing.TitledTopBorder;
006 import com.bea.ide.workspace.IProject;
007 import com.bea.ide.workspace.IProjectPropertyPanel;
008 import com.bea.wlw.runtime.core.util.CryptUtil;
009 
010 import java.awt.GridBagConstraints;
011 import java.awt.GridBagLayout;
012 import java.awt.Insets;
013 import java.net.UnknownHostException;
014 import java.util.prefs.Preferences;
015 import javax.swing.JDialog;
016 import javax.swing.JLabel;
017 import javax.swing.JPanel;
018 import javax.swing.JPasswordField;
019 import javax.swing.JTextField;
020 
021 /**
022  * A properties panel through which users of the PopupAction extension
023  * can set properties for the extension's FTP behavior; these properties
024  * include the host name, port, user name, and password that should be
025  * used for FTP operations.
026  *
027  * This class extends JPanel, a Java Swing component that's useful for
028  * containing other components for display in a dialog.
029  */
030 public class FTPPrefsPanel extends JPanel implements IProjectPropertyPanel
031 {
032     // Get the package of resource strings that will be used in the UI.
033     static ResourceSvc.IResourcePkg s_pkg =
034             ResourceSvc.get().getResourcePackage(FTPPrefsPanel.class, "ftp");
035 
036     // Variables for user interface components.
037     private JPanel m_ftpSettingsPanel;
038     private JLabel m_hostNameLabel;
039     private JTextField m_hostName;
040     private JLabel m_portLabel;
041     private JTextField m_port;
042     private JLabel m_userNameLabel;
043     private JTextField m_userName;
044     private JLabel m_passwordLabel;
045     private JPasswordField m_password;
046     private JLabel m_remoteDirectoryLabel;
047     private JTextField m_remoteDirectory;
048     private JPanel m_filler;
049     private JLabel m_remoteDirectoryTip;
050     private JLabel m_portDefaultTip;
051 
052     /**
053      * A variable to represent the project for which the user is currently
054      * setting properties. This value will be set to the value received
055      * from the IDE through the setProject method.
056      */
057     private IProject m_project;
058 
059     /**
060      * Constructs a new instance of this class, calling a method
061      * that builds the user interface from Java Swing components.
062      */
063     public FTPPrefsPanel()
064     {
065         super();
066         initComponents();
067     }
068 
069     /**
070      * Retrieves properties from stored preferences and loads them into
071      * the FTP properties panel for display.
072      */
073     public void loadProperties()
074     {
075         Preferences prefs = m_project.systemNodeForPackage(FTPPrefsPanel.class);
076         m_hostName.setText(prefs.get(FTPSettings.HOSTNAME, ""));
077         m_port.setText(prefs.get(FTPSettings.PORT, ""));
078         m_remoteDirectory.setText(prefs.get(FTPSettings.REMOTE_DIRECTORY, ""));
079         m_userName.setText(prefs.get(FTPSettings.USERNAME, ""));
080         String password = prefs.get(FTPSettings.PASSWORD, null);
081         if (password != null)
082         {
083             try
084             {
085                 password = CryptUtil.get().deobfuscate(password);
086                 m_password.setText(password);
087             catch (Exception e)
088             {
089                 MessageSvc.get().debugLog("Exception while attempting to decrypt proxy password: " + e);
090             }
091         }
092     }
093 
094     /**
095      * Stores property values entered in the FTP properties panel.
096      */
097     public void storeProperties()
098     {
099         // Retrieve the FTP preferences.
100         Preferences prefs = m_project.systemNodeForPackage(FTPPrefsPanel.class);
101 
102         /**
103          * Get the hostname property value. If the value is not equal to the
104          * value already set, then set the new value.
105          */
106         String hostName = m_hostName.getText();
107         if (!hostName.equals(prefs.get(FTPSettings.HOSTNAME, null)))
108         {
109             prefs.put(FTPSettings.HOSTNAME, hostName);
110         }
111 
112         /**
113          * Get the port property value. If the value is not equal to the
114          * value already set, then set the new value.
115          */
116         String port = m_port.getText();
117         if (!port.equals(prefs.get(FTPSettings.PORT, null)))
118         {
119             prefs.put(FTPSettings.PORT, port);
120         }
121 
122         /**
123          * Get the remote directory property value. If the value is not equal to the
124          * value already set, then set the new value.
125          */
126         String remoteDirectory = m_remoteDirectory.getText();
127         if (!remoteDirectory.equals(prefs.get(FTPSettings.REMOTE_DIRECTORY, null)))
128         {
129             prefs.put(FTPSettings.REMOTE_DIRECTORY, remoteDirectory);
130         }
131 
132         /**
133          * Get the username property value. If the value is not equal to the
134          * value already set, then set the new value.
135          */
136         String userName = m_userName.getText();
137         if (!userName.equals(prefs.get(FTPSettings.USERNAME, null)))
138         {
139             prefs.put(FTPSettings.USERNAME, userName);
140         }
141 
142         /**
143          * Retrieve the new password set in the properties panel;
144          * retrieve the old password from preferences.
145          */
146         String password = m_password.getText();
147         String oldPassword = prefs.get(FTPSettings.PASSWORD, null);
148         /**
149          * If the old password isn't null, then attempt to decrypt it for
150          * comparison with the new password.
151          */
152         if (oldPassword != null)
153         {
154             try
155             {
156                 oldPassword = CryptUtil.get().deobfuscate(oldPassword);
157             catch (Exception e)
158             {
159                 MessageSvc.get().debugLog("Exception while attempting to decrypt proxy password: " + e);
160                 oldPassword = null;
161             }
162         }
163         /**
164          * If the new password is not the same as the old password, then
165          * set the new password the preferences.
166          */
167         if (!password.equals(oldPassword))
168         {
169             try
170             {
171                 prefs.put(FTPSettings.PASSWORD, CryptUtil.get().obfuscate(password));
172             catch (Exception e)
173             {
174                 MessageSvc.get().debugLog("Exception while attempting to encrypt proxy password: " + e);
175             }
176         }
177     }
178 
179     /**
180      * Called by the IDE to discover whether property values entered by
181      * the user are valid before the entries are saved and the dialog is
182      * closed. This method will be called after the user clicks OK in the
183      * dialog, and before the IDE calls your storeProperties implementation..
184      * This method provides an opportunity for you to check the new
185      * values and return <code>true</code> if they're valid for use as
186      * property values.
187      *
188      * The IDE will call your implementation of the storeProperties method
189      * if you return <code>true</code> from validateEntries.
190      *
191      @param dialog The dialog that contains this panel.
192      @return <code>true</code> if the entries are valid; <code>false</code>
193      * if they are not.
194      */
195     public boolean validateEntries(JDialog dialog)
196     {
197         // Validate the host name by ensuring it exists.
198         String hostName = m_hostName.getText();
199         try
200         {
201             if (hostName == null || hostName.equals(""))
202             {
203                 throw new UnknownHostException();
204             }
205         catch (UnknownHostException uhe)
206         {
207             MessageSvc.get().displayError(s_pkg.getString("hostnameError"), MessageSvc.LEVEL_ERROR);
208             return false;
209         }
210 
211         // Validate the port by ensuring that it exists as an int.
212         String port = m_port.getText();
213         if (!port.equals(""))
214         {
215             try
216             {
217                 int value = Integer.parseInt(port);
218             catch (NumberFormatException nfe)
219             {
220                 MessageSvc.get().displayError(s_pkg.getString("portError"), MessageSvc.LEVEL_ERROR);
221                 return false;
222             }
223         }
224         return true;
225     }
226 
227     /**
228      * Called by the IDE after the user clicks the Cancel button. If this
229      * panel had acquired resources, this would be a good place to
230      * release them.
231      */
232     public void cancel()
233     {
234         // Not implemented
235     }
236 
237     /**
238      * Called by the IDE to inform this panel as to which project the
239      * user is currently setting properties for. It is necessary to know
240      * this information in order to retrieve and display properties for
241      * the correct project.
242      *
243      @param project The project for which the user is setting
244      * properties.
245      */
246     public void setProject(IProject project)
247     {
248         m_project = project;
249     }
250 
251     /**
252      * Builds the user interface for the properties panel by arranging
253      * Java Swing components into a "gridbag" layout.
254      */
255     public void initComponents()
256     {
257         setLayout(new GridBagLayout());
258 
259         m_ftpSettingsPanel = new JPanel();
260 
261         // The label and text box for the hostname property value.
262         m_hostNameLabel = new JLabel(s_pkg.getString("host"));
263         m_hostName = new JTextField();
264 
265         // The label and text box for the port property value.
266         m_portLabel = new JLabel(s_pkg.getString("port"));
267         m_port = new JTextField();
268 
269         // The label and text box for the username property value.
270         m_userNameLabel = new JLabel(s_pkg.getString("username"));
271         m_userName = new JTextField();
272 
273         // The label and text box for the password property value.
274         m_passwordLabel = new JLabel(s_pkg.getString("password"));
275         m_password = new JPasswordField();
276 
277         // The label and text box for the remote directory property value.
278         m_remoteDirectoryLabel = new JLabel(s_pkg.getString("remoteDirectory"));
279         m_remoteDirectory = new JTextField();
280 
281         /**
282          * The panel that holds the input components. This is nested inside
283          * the panel that the IDE puts into the Properties dialog.
284          */
285         m_ftpSettingsPanel.setBorder(new TitledTopBorder(s_pkg.getString("ftpSettings")));
286         m_ftpSettingsPanel.setLayout(new GridBagLayout());
287 
288         // The descriptive text for the remote directory and port boxes.
289         m_remoteDirectoryTip = new JLabel(s_pkg.getString("remoteDirectoryTip"));
290         m_portDefaultTip = new JLabel(s_pkg.getString("portDefaultTip"));
291         m_filler = new JPanel();
292 
293         /**
294          * Use GridBagConstraints instances to hold the components
295          * that make up the user interface. Each of the following sections
296          * creates an instance, sets layout characteristics for the
297          * instance, then adds a component to the panel that contains it
298          * using the instance as it has been set up. The added component
299          * is positioned in the panel according to the characteristics
300          * set for the constraints instance.
301          */
302         GridBagConstraints gbc = new GridBagConstraints();
303         gbc.gridwidth = 1;
304         gbc.weightx = 0.0;
305         gbc.insets = new Insets(2226);
306         gbc.anchor = GridBagConstraints.WEST;
307         gbc.fill = GridBagConstraints.HORIZONTAL;
308         m_ftpSettingsPanel.add(m_hostNameLabel, gbc);
309         gbc.gridwidth = GridBagConstraints.REMAINDER;
310         gbc.weightx = 1.0;
311         m_ftpSettingsPanel.add(m_hostName, gbc);
312 
313         gbc.gridwidth = 1;
314         gbc.weightx = 0.0;
315         m_ftpSettingsPanel.add(m_portLabel, gbc);
316         gbc.gridwidth = GridBagConstraints.REMAINDER;
317         gbc.weightx = 1.0;
318         m_ftpSettingsPanel.add(m_port, gbc);
319 
320         GridBagConstraints tipGBC = new GridBagConstraints();
321         tipGBC.gridwidth = GridBagConstraints.REMAINDER;
322         tipGBC.weightx = 1.0;
323         tipGBC.insets = new Insets(21026);
324         tipGBC.anchor = GridBagConstraints.WEST;
325         tipGBC.fill = GridBagConstraints.HORIZONTAL;
326         m_ftpSettingsPanel.add(m_portDefaultTip, tipGBC);
327 
328         gbc.gridwidth = 1;
329         gbc.weightx = 0.0;
330         m_ftpSettingsPanel.add(m_remoteDirectoryLabel, gbc);
331         gbc.gridwidth = GridBagConstraints.REMAINDER;
332         gbc.weightx = 1.0;
333         m_ftpSettingsPanel.add(m_remoteDirectory, gbc);
334 
335         tipGBC = new GridBagConstraints();
336         tipGBC.gridwidth = GridBagConstraints.REMAINDER;
337         tipGBC.weightx = 1.0;
338         tipGBC.insets = new Insets(21026);
339         tipGBC.anchor = GridBagConstraints.WEST;
340         tipGBC.fill = GridBagConstraints.HORIZONTAL;
341         m_ftpSettingsPanel.add(m_remoteDirectoryTip, tipGBC);
342 
343         GridBagConstraints fillerGBC = new GridBagConstraints();
344         fillerGBC.gridwidth = GridBagConstraints.REMAINDER;
345         fillerGBC.anchor = GridBagConstraints.WEST;
346         fillerGBC.fill = GridBagConstraints.HORIZONTAL;
347         fillerGBC.insets = new Insets(8080);
348         fillerGBC.weightx = 1.0;
349         fillerGBC.weighty = 1.0;
350         m_ftpSettingsPanel.add(m_filler, fillerGBC);
351 
352         gbc.gridwidth = 1;
353         gbc.weightx = 0.0;
354         m_ftpSettingsPanel.add(m_userNameLabel, gbc);
355         gbc.gridwidth = GridBagConstraints.REMAINDER;
356         gbc.weightx = 1.0;
357         m_ftpSettingsPanel.add(m_userName, gbc);
358 
359         gbc.gridwidth = 1;
360         gbc.weightx = 0.0;
361         m_ftpSettingsPanel.add(m_passwordLabel, gbc);
362         gbc.gridwidth = GridBagConstraints.REMAINDER;
363         gbc.weightx = 1.0;
364         m_ftpSettingsPanel.add(m_password, gbc);
365 
366         GridBagConstraints topGbc = new GridBagConstraints();
367         topGbc.gridwidth = GridBagConstraints.REMAINDER;
368         topGbc.anchor = GridBagConstraints.WEST;
369         topGbc.fill = GridBagConstraints.HORIZONTAL;
370         topGbc.insets = new Insets(4181);
371         topGbc.weightx = 1.0;
372         topGbc.weighty = 1.0;
373         add(m_ftpSettingsPanel, topGbc);
374         add(new JPanel(), topGbc);
375     }
376 }