components/visual-panels/firewall/src/java/vpanels/app/firewall/com/oracle/solaris/vp/panels/firewall/client/swing/AddServiceAction.java
changeset 3553 f1d133b09a8c
parent 3552 077ebe3d0d24
child 3554 ef58713bafc4
equal deleted inserted replaced
3552:077ebe3d0d24 3553:f1d133b09a8c
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.panels.firewall.client.swing;
       
    27 
       
    28 import java.awt.*;
       
    29 import java.awt.event.*;
       
    30 import java.util.*;
       
    31 import java.util.List;
       
    32 import javax.swing.*;
       
    33 import javax.swing.event.*;
       
    34 import com.oracle.solaris.vp.panel.common.action.*;
       
    35 import com.oracle.solaris.vp.panel.common.control.*;
       
    36 import com.oracle.solaris.vp.panel.common.model.ManagedObject;
       
    37 import com.oracle.solaris.vp.panel.swing.action.AddManagedObjectAction;
       
    38 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    39 import com.oracle.solaris.vp.util.swing.*;
       
    40 
       
    41 @SuppressWarnings({"serial"})
       
    42 public class AddServiceAction extends AddManagedObjectAction<ManagedObject,
       
    43     ServiceManagedObject, ServiceManagedObject> {
       
    44 
       
    45     //
       
    46     // Static data
       
    47     //
       
    48 
       
    49     private static final String BUTTON_TEXT = Finder.getString(
       
    50 	"service.action.add.button");
       
    51 
       
    52     //
       
    53     // Instance data
       
    54     //
       
    55 
       
    56     private MainControl control;
       
    57 
       
    58     //
       
    59     // Constructors
       
    60     //
       
    61 
       
    62     public AddServiceAction(MainControl control) {
       
    63 	super(BUTTON_TEXT, null, control);
       
    64 	this.control = control;
       
    65 	setLoops(true);
       
    66     }
       
    67 
       
    68     //
       
    69     // StructuredAction methods
       
    70     //
       
    71 
       
    72     @Override
       
    73     public ServiceManagedObject invoke(ServiceManagedObject rtInput)
       
    74 	throws ActionAbortedException, ActionFailedException {
       
    75 	List<ManagedObject> svcList = new ArrayList<ManagedObject>(
       
    76 	    control.getPanelDescriptor().getAllServices());
       
    77 
       
    78 	setPresetInput(svcList);
       
    79 	return super.invoke(rtInput);
       
    80     }
       
    81 
       
    82     @Override
       
    83     public ServiceManagedObject getRuntimeInput(
       
    84 	List<ManagedObject> selection, ServiceManagedObject rtInput)
       
    85 	throws ActionAbortedException {
       
    86 
       
    87         Object response = getInput(selection);
       
    88         if (response == null)
       
    89             throw new ActionAbortedException();
       
    90 
       
    91         return (ServiceManagedObject) response;
       
    92     }
       
    93 
       
    94     //
       
    95     // DefaultStructuredAction methods
       
    96     //
       
    97 
       
    98     @Override
       
    99     public ServiceManagedObject workBusy(List<ManagedObject> selection,
       
   100 	ServiceManagedObject svc) throws ActionAbortedException,
       
   101 	ActionFailedException, ActionUnauthorizedException {
       
   102 
       
   103 	FirewallPanelDescriptor descriptor = control.getPanelDescriptor();
       
   104 	descriptor.addChildren(svc);
       
   105 
       
   106 	// Navigate to the new service
       
   107 	Navigable navigable = new SimpleNavigable(
       
   108 	    ServiceEditControl.ID, null,
       
   109 	    ServiceEditControl.PARAM_SVC, svc.getId());
       
   110 
       
   111 	control.getNavigator().goToAsync(false, control, navigable);
       
   112 
       
   113 	return svc;
       
   114     }
       
   115 
       
   116     //
       
   117     // A custom inputDialog method as the static JOptionPane.showInputDialog()
       
   118     // treats input value as valid even when user selected the cancel option.
       
   119     //
       
   120     private Object getInput(List<ManagedObject> selection) {
       
   121 	SettingsPanel panel = new SettingsPanel();
       
   122 	panel.setBorder(GUIUtil.getEmptyBorder());
       
   123 	panel.getHelpField().setText(
       
   124             Finder.getString("service.action.add.message"));
       
   125 
       
   126         SettingsButtonBar bar = panel.getButtonBar();
       
   127 	final JButton okayButton = bar.getOkayButton();
       
   128 
       
   129         final JList list = new JList(selection.toArray());
       
   130         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
       
   131         list.setVisibleRowCount(10);
       
   132         list.addListSelectionListener(
       
   133             new ListSelectionListener() {
       
   134                 public void valueChanged(ListSelectionEvent e) {
       
   135 		    okayButton.setEnabled(!list.isSelectionEmpty());
       
   136                 }
       
   137 	    });
       
   138 
       
   139         JScrollPane scroll = new JScrollPane(list);
       
   140 	panel.setContent(scroll, false, false);
       
   141 
       
   142 	Window pWin = SwingUtilities.getWindowAncestor(
       
   143 	    getHasComponent().getComponent());
       
   144         final JDialog dialog = new JDialog(pWin,
       
   145 	    Dialog.ModalityType.DOCUMENT_MODAL);
       
   146 
       
   147         okayButton.addActionListener(
       
   148 	    new ActionListener() {
       
   149                 public void actionPerformed(ActionEvent e) {
       
   150                     dialog.setVisible(false);
       
   151                 }
       
   152 	    });
       
   153 
       
   154         bar.getCancelButton().addActionListener(
       
   155 	    new ActionListener() {
       
   156 		public void actionPerformed(ActionEvent e) {
       
   157                     list.clearSelection();
       
   158 		    dialog.setVisible(false);
       
   159 		}
       
   160 	    });
       
   161 
       
   162         // Allow double-clicking to add a service
       
   163         list.addMouseListener(
       
   164             new MouseAdapter() {
       
   165 		@Override
       
   166 		public void mouseClicked(MouseEvent e) {
       
   167 		    if (e.getClickCount() >= 2) {
       
   168 			dialog.setVisible(false);
       
   169 		    }
       
   170 		}
       
   171 	    });
       
   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);
       
   179         dialog.setVisible(true);
       
   180         dialog.dispose();
       
   181 
       
   182 	return list.getSelectedValue();
       
   183     }
       
   184 }