package com.onaro.sanscreen.client.view.profile; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.Collection; import javax.swing.JCheckBox; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import org.apache.commons.lang3.StringUtils; import com.onaro.client.leekui.jface.dialogs.ValidatableDialogPage; import com.onaro.commons.swing.FixedSizeDocumentFilter; import com.onaro.sanscreen.client.SANscreenClientPlugin; import com.onaro.sanscreen.server.interfaces.data.viewprofile.ViewProfile; public class SaveProfileDialogPage extends ValidatableDialogPage { /** * In case the User has entered a profile name that already exist, then * this member variable will hold the existing profile. */ private ViewProfile existingProfile; private String profileName; private boolean global; private Collection existingProfiles; private JPanel contentsPanel = null; // @jve:decl-index=0:visual-constraint="43,28" private JLabel nameLabel = null; private JTextField nameField = null; private JCheckBox globalCheckBox = null; public SaveProfileDialogPage(String defaultName, boolean defaultGlobal, Collection existingProfiles) { setTitle(Messages.INSTANCE.getSaveProfileDialogPageTitle()); setDescription(Messages.INSTANCE.getSaveProfileDialogPageDescription()); profileName = defaultName; global = defaultGlobal; this.existingProfiles = existingProfiles; } public boolean isGlobal() { return global; } /** * If the User has entered the name of a profile that already exist * then this method will return TRUE otherwise FALSE. */ public boolean isOverwrite() { return existingProfile != null; } /** * When the User enters the name of a profile to be saved, this name is check * against the already existing profiles. If there is a match and the User * agree to overwrite the existing profile then this method will return * the profile in question. */ public ViewProfile getOverwritenProfile(){ return existingProfile; } public String getProfileName() { return profileName; } @Override protected JComponent createComponent() { return getContentsPanel(); } @Override public boolean performOk() { ViewProfile profile = findMatchingProfile(profileName); if (profile != null) { int result = JOptionPane.showConfirmDialog(getContentsPanel(), Messages.INSTANCE.getOverwriteProfileConfirmMessage(), Messages.INSTANCE.getOverwriteProfileConfirmTitle(), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (result != JOptionPane.YES_OPTION) { return false; } existingProfile = profile; } return super.performOk(); } public boolean isPageValid() { if (StringUtils.isBlank(profileName)) { return false; } // Check to see if the name is used by another profile ViewProfile existingProfile = findMatchingProfile(profileName); if (existingProfile != null && existingProfile.isSystemDefined()) { setErrorMessage(Messages.INSTANCE.getExistingSystemProfileError()); return false; } setErrorMessage(null); return true; } private ViewProfile findMatchingProfile(String profileName) { for (ViewProfile profile : existingProfiles) { if (profile.getProfileName().equalsIgnoreCase(profileName)) { return profile; } } return null; } private void revalidate() { if (getContainer() != null) { getContainer().updateButtons(); getContainer().updateMessage(); } } /** * This method initializes contentsPanel * * @return javax.swing.JPanel */ private JPanel getContentsPanel() { if (contentsPanel == null) { GridBagConstraints gridBagConstraints2 = new GridBagConstraints(); gridBagConstraints2.gridx = 0; gridBagConstraints2.gridwidth = 2; gridBagConstraints2.anchor = GridBagConstraints.WEST; gridBagConstraints2.insets = new Insets(5, 5, 5, 5); gridBagConstraints2.gridy = 1; GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL; gridBagConstraints1.gridy = 0; gridBagConstraints1.weightx = 1.0; gridBagConstraints1.insets = new Insets(5, 5, 0, 5); gridBagConstraints1.gridx = 1; GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.insets = new Insets(5, 5, 0, 0); gridBagConstraints.gridy = 0; nameLabel = new JLabel(); nameLabel.setText(Messages.INSTANCE.getProfileNameFieldLabel()); contentsPanel = new JPanel(); contentsPanel.setLayout(new GridBagLayout()); contentsPanel.setSize(new Dimension(393, 146)); contentsPanel.add(nameLabel, gridBagConstraints); contentsPanel.add(getNameField(), gridBagConstraints1); contentsPanel.add(getGlobalCheckBox(), gridBagConstraints2); } return contentsPanel; } /** * This method initializes nameField * * @return javax.swing.JTextField */ private JTextField getNameField() { if (nameField == null) { nameField = new JTextField(); nameField.setName("nameField"); //$NON-NLS-1$ nameField.setText(profileName); FixedSizeDocumentFilter.installFilter(255, nameField); nameField.getDocument().addDocumentListener(new DocumentListener() { private void update() { profileName = getNameField().getText(); revalidate(); } public void changedUpdate(DocumentEvent e) { update(); } public void insertUpdate(DocumentEvent e) { update(); } public void removeUpdate(DocumentEvent e) { update(); } }); } return nameField; } /** * This method initializes globalCheckBox * * @return javax.swing.JCheckBox */ private JCheckBox getGlobalCheckBox() { if (globalCheckBox == null) { globalCheckBox = new JCheckBox(); globalCheckBox.setName("globalCheckBox"); //$NON-NLS-1$ globalCheckBox.setText(Messages.INSTANCE.getGlobalProfileCheckboxLabel(SANscreenClientPlugin.getDefault().getBranding().getProductName())); globalCheckBox.setSelected(global); globalCheckBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { global = getGlobalCheckBox().isSelected(); } }); } return globalCheckBox; } }