usr/src/java/vpanels/app/firewall/org/opensolaris/os/vp/panels/firewall/client/swing/AddServiceAction.java
changeset 733 31e9ec33c7d2
parent 720 58815d7de6df
equal deleted inserted replaced
732:8bcc11c4f6e6 733:31e9ec33c7d2
    18  *
    18  *
    19  * CDDL HEADER END
    19  * CDDL HEADER END
    20  */
    20  */
    21 
    21 
    22 /*
    22 /*
    23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
    23  * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
    24  */
    24  */
    25 
    25 
    26 package org.opensolaris.os.vp.panels.firewall.client.swing;
    26 package org.opensolaris.os.vp.panels.firewall.client.swing;
    27 
    27 
    28 import java.awt.Component;
    28 import java.awt.*;
    29 import java.awt.event.*;
    29 import java.awt.event.*;
    30 import java.util.*;
    30 import java.util.*;
       
    31 import java.util.List;
    31 import javax.swing.*;
    32 import javax.swing.*;
    32 import javax.swing.event.*;
    33 import javax.swing.event.*;
    33 import org.opensolaris.os.vp.panel.common.action.*;
    34 import org.opensolaris.os.vp.panel.common.action.*;
    34 import org.opensolaris.os.vp.panel.common.control.*;
    35 import org.opensolaris.os.vp.panel.common.control.*;
    35 import org.opensolaris.os.vp.panel.common.model.ManagedObject;
    36 import org.opensolaris.os.vp.panel.common.model.ManagedObject;
    36 import org.opensolaris.os.vp.panel.swing.action.AddManagedObjectAction;
    37 import org.opensolaris.os.vp.panel.swing.action.AddManagedObjectAction;
    37 import org.opensolaris.os.vp.util.misc.finder.Finder;
    38 import org.opensolaris.os.vp.util.misc.finder.Finder;
    38 import org.opensolaris.os.vp.util.swing.*;
    39 import org.opensolaris.os.vp.util.swing.*;
    39 import org.opensolaris.os.vp.util.swing.layout.*;
       
    40 
    40 
    41 @SuppressWarnings({"serial"})
    41 @SuppressWarnings({"serial"})
    42 public class AddServiceAction
    42 public class AddServiceAction extends AddManagedObjectAction<ManagedObject,
    43     extends AddManagedObjectAction<ManagedObject,
       
    44     ServiceManagedObject, ServiceManagedObject> {
    43     ServiceManagedObject, ServiceManagedObject> {
    45 
    44 
    46     //
    45     //
    47     // Static data
    46     // Static data
    48     //
    47     //
   117     //
   116     //
   118     // A custom inputDialog method as the static JOptionPane.showInputDialog()
   117     // A custom inputDialog method as the static JOptionPane.showInputDialog()
   119     // treats input value as valid even when user selected the cancel option.
   118     // treats input value as valid even when user selected the cancel option.
   120     //
   119     //
   121     private Object getInput(List<ManagedObject> selection) {
   120     private Object getInput(List<ManagedObject> selection) {
   122         JLabel label = new JLabel(
   121 	SettingsPanel panel = new SettingsPanel();
       
   122 	panel.setBorder(GUIUtil.getEmptyBorder());
       
   123 	panel.getHelpField().setText(
   123             Finder.getString("service.action.add.message"));
   124             Finder.getString("service.action.add.message"));
       
   125 
       
   126         SettingsButtonBar bar = panel.getButtonBar();
       
   127 	final JButton okayButton = bar.getOkayButton();
       
   128 
   124         final JList list = new JList(selection.toArray());
   129         final JList list = new JList(selection.toArray());
   125         JScrollPane scrollPane = new JScrollPane(list);
       
   126 
       
   127         final JButton okButton = new JButton("OK");
       
   128         final JButton cancelButton = new JButton("Cancel");
       
   129         okButton.setEnabled(false);
       
   130 
       
   131         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
   130         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
   132         list.setVisibleRowCount(10);
   131         list.setVisibleRowCount(10);
   133         list.addListSelectionListener(
   132         list.addListSelectionListener(
   134             new ListSelectionListener() {
   133             new ListSelectionListener() {
   135                 public void valueChanged(ListSelectionEvent e) {
   134                 public void valueChanged(ListSelectionEvent e) {
   136 		    okButton.setEnabled(!list.isSelectionEmpty());
   135 		    okayButton.setEnabled(!list.isSelectionEmpty());
   137                 }
   136                 }
   138 	});
   137 	    });
   139 
   138 
   140         int gap = GUIUtil.getHalfGap();
   139         JScrollPane scroll = new JScrollPane(list);
   141         ColumnLayoutConstraint c = new ColumnLayoutConstraint(
   140 	panel.setContent(scroll, false, false);
   142             HorizontalAnchor.FILL, gap);
       
   143         RowLayoutConstraint r = new RowLayoutConstraint(
       
   144             VerticalAnchor.CENTER, gap);
       
   145 
   141 
   146         JPanel formPanel = new JPanel();
   142 	Window pWin = SwingUtilities.getWindowAncestor(
   147         formPanel.setOpaque(false);
   143 	    getHasComponent().getComponent());
   148         Form form = new Form(formPanel, VerticalAnchor.TOP);
   144         final JDialog dialog = new JDialog(pWin,
       
   145 	    Dialog.ModalityType.DOCUMENT_MODAL);
   149 
   146 
   150         form.addRow(HorizontalAnchor.LEFT, c);
   147         okayButton.addActionListener(
   151         form.add(label);
   148 	    new ActionListener() {
   152 
       
   153         form.addRow(HorizontalAnchor.LEFT, c);
       
   154         form.add(scrollPane);
       
   155 
       
   156         final JOptionPane pane = new JOptionPane(formPanel,
       
   157             JOptionPane.PLAIN_MESSAGE);
       
   158         Component pComponent = getHasComponent().getComponent();
       
   159         pane.setComponentOrientation(pComponent.getComponentOrientation());
       
   160         final JDialog dialog = pane.createDialog(pComponent,
       
   161             Finder.getString("service.action.add.title"));
       
   162 
       
   163         okButton.addActionListener(
       
   164                 new ActionListener() {
       
   165                 public void actionPerformed(ActionEvent e) {
   149                 public void actionPerformed(ActionEvent e) {
   166                     pane.setInputValue(list.getSelectedValue());
       
   167                     pane.setValue(JOptionPane.OK_OPTION);
       
   168                     dialog.setVisible(false);
   150                     dialog.setVisible(false);
   169                 }
   151                 }
   170         });
   152 	    });
   171 
   153 
   172         cancelButton.addActionListener(
   154         bar.getCancelButton().addActionListener(
   173                 new ActionListener() {
   155 	    new ActionListener() {
   174 		public void actionPerformed(ActionEvent e) {
   156 		public void actionPerformed(ActionEvent e) {
   175 		    pane.setValue(JOptionPane.CANCEL_OPTION);
   157                     list.clearSelection();
   176 		    dialog.setVisible(false);
   158 		    dialog.setVisible(false);
   177 		}
   159 		}
   178 	});
   160 	    });
   179 	pane.setOptions(new Object[] {okButton, cancelButton});
       
   180 
   161 
   181         //
       
   182         // Allow double-clicking to add a service
   162         // Allow double-clicking to add a service
   183         //
       
   184         list.addMouseListener(
   163         list.addMouseListener(
   185             new MouseAdapter() {
   164             new MouseAdapter() {
   186             @Override
   165 		@Override
   187             public void mouseClicked(MouseEvent e) {
   166 		public void mouseClicked(MouseEvent e) {
   188                 if (e.getClickCount() >= 2) {
   167 		    if (e.getClickCount() >= 2) {
   189                     pane.setInputValue(list.getSelectedValue());
   168 			dialog.setVisible(false);
   190                     pane.setValue(JOptionPane.OK_OPTION);
   169 		    }
   191                     dialog.setVisible(false);
   170 		}
   192                 }
   171 	    });
   193             }
       
   194         });
       
   195 
   172 
       
   173 	Container cont = dialog.getContentPane();
       
   174 	cont.setLayout(new BorderLayout());
       
   175 	cont.add(panel, BorderLayout.CENTER);
       
   176 
       
   177         dialog.pack();
       
   178 	dialog.setLocationRelativeTo(pWin);
   196         dialog.setVisible(true);
   179         dialog.setVisible(true);
   197         dialog.dispose();
   180         dialog.dispose();
   198 
   181 
   199         Object response = pane.getInputValue();
   182 	return list.getSelectedValue();
   200         if (response == JOptionPane.UNINITIALIZED_VALUE)
       
   201             return null;
       
   202 
       
   203         Object option = pane.getValue();
       
   204         if (option == null || option.equals(JOptionPane.CANCEL_OPTION))
       
   205             return null;
       
   206 
       
   207         return response;
       
   208     }
   183     }
   209 }
   184 }