components/visual-panels/apache/src/java/vpanels/app/apache/com/oracle/solaris/vp/panels/apache/client/swing/CreateVirtualHostAction.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.apache.client.swing;
       
    27 
       
    28 import java.awt.Component;
       
    29 import java.awt.event.*;
       
    30 import java.util.List;
       
    31 import javax.swing.*;
       
    32 import javax.swing.text.JTextComponent;
       
    33 import com.oracle.solaris.vp.panel.common.action.*;
       
    34 import com.oracle.solaris.vp.panel.common.control.*;
       
    35 import com.oracle.solaris.vp.panel.common.model.ManagedObject;
       
    36 import com.oracle.solaris.vp.panel.swing.action.AddManagedObjectAction;
       
    37 import com.oracle.solaris.vp.util.misc.ObjectUtil;
       
    38 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    39 import com.oracle.solaris.vp.util.misc.property.StringProperty;
       
    40 import com.oracle.solaris.vp.util.swing.*;
       
    41 import com.oracle.solaris.vp.util.swing.layout.*;
       
    42 import com.oracle.solaris.vp.util.swing.property.
       
    43     TextComponentPropertySynchronizer;
       
    44 
       
    45 @SuppressWarnings({"serial"})
       
    46 public class CreateVirtualHostAction extends AddManagedObjectAction
       
    47     <ManagedObject, CreateVirtualHostAction.Data, VirtualHost> {
       
    48 
       
    49     //
       
    50     // Inner classes
       
    51     //
       
    52 
       
    53     protected class Data {
       
    54 	//
       
    55 	// Instance data
       
    56 	//
       
    57 
       
    58 	public StringProperty domainProperty = new StringProperty();
       
    59 	public StringProperty docRootProperty = new StringProperty();
       
    60 	public JOptionPane pane;
       
    61 	public JDialog dialog;
       
    62 
       
    63 	//
       
    64 	// Constructors
       
    65 	//
       
    66 
       
    67 	public Data() {
       
    68 	    ActionListener listener =
       
    69 		new ActionListener() {
       
    70 		    @Override
       
    71 		    public void actionPerformed(ActionEvent e) {
       
    72 			pane.setValue(JOptionPane.OK_OPTION);
       
    73 		    }
       
    74 		};
       
    75 
       
    76 	    JLabel domainLabel = new JLabel(
       
    77 		Finder.getString("vhost.general.edit.domain"));
       
    78 	    JTextField domainField = GUIUtil.createTextField();
       
    79 	    domainField.addActionListener(listener);
       
    80 	    new TextComponentPropertySynchronizer<String, JTextField>(
       
    81 		domainProperty, domainField, false);
       
    82 
       
    83 	    JLabel docRootLabel = new JLabel(
       
    84 		Finder.getString("vhost.general.edit.docroot"));
       
    85 
       
    86 	    BrowsableFilePanel docRootField = new BrowsableFilePanel();
       
    87 	    docRootField.setOpaque(false);
       
    88 	    JFileChooser chooser = docRootField.getFileChooser();
       
    89 	    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
       
    90 	    chooser.setFileSystemView(
       
    91 		control.getPanelDescriptor().getFileSystemView());
       
    92 
       
    93 	    new TextComponentPropertySynchronizer<String, JTextComponent>(
       
    94 		docRootProperty, docRootField.getField(), false);
       
    95 
       
    96 	    JPanel formPanel = new JPanel();
       
    97 	    formPanel.setOpaque(false);
       
    98 	    Form form = new Form(formPanel, VerticalAnchor.TOP);
       
    99 
       
   100 	    int gap = GUIUtil.getHalfGap();
       
   101 
       
   102 	    ColumnLayoutConstraint c = new ColumnLayoutConstraint(
       
   103 		HorizontalAnchor.FILL, gap);
       
   104 
       
   105 	    HasAnchors a = new SimpleHasAnchors(
       
   106 		HorizontalAnchor.LEFT, VerticalAnchor.CENTER);
       
   107 
       
   108 	    form.addTable(2, gap, gap, HorizontalAnchor.LEFT, c);
       
   109 
       
   110 	    form.add(domainLabel, a);
       
   111 	    form.add(domainField, a);
       
   112 
       
   113 	    form.add(docRootLabel, a);
       
   114 	    form.add(docRootField, a);
       
   115 
       
   116 	    pane = new JOptionPane(formPanel, JOptionPane.PLAIN_MESSAGE,
       
   117 		JOptionPane.OK_CANCEL_OPTION);
       
   118 
       
   119 	    dialog = pane.createDialog(getHasComponent().getComponent(),
       
   120 		Finder.getString("vhosts.action.create.title"));
       
   121 	}
       
   122     }
       
   123 
       
   124     //
       
   125     // Static data
       
   126     //
       
   127 
       
   128     private static final String TEXT = Finder.getString(
       
   129 	"vhosts.action.create.button");
       
   130 
       
   131     //
       
   132     // Instance data
       
   133     //
       
   134 
       
   135     private MainControl control;
       
   136 
       
   137     //
       
   138     // Constructors
       
   139     //
       
   140 
       
   141     public CreateVirtualHostAction(MainControl control) {
       
   142 	super(TEXT, null, control);
       
   143 	this.control = control;
       
   144 	setLoops(true);
       
   145     }
       
   146 
       
   147     //
       
   148     // StructuredAction methods
       
   149     //
       
   150 
       
   151     @Override
       
   152     public Data getRuntimeInput(List<ManagedObject> selection, Data data)
       
   153 	throws ActionAbortedException {
       
   154 
       
   155 	// Since this action navigates to the new object once created, we must
       
   156 	// first navigate to control so that any outstanding changes in the
       
   157 	// current Control are handled.
       
   158 	try {
       
   159 	    control.getNavigator().goToAsyncAndWait(false, control);
       
   160 	} catch (NavigationException e) {
       
   161 	    throw new ActionAbortedException(e);
       
   162 	}
       
   163 
       
   164 	if (data == null) {
       
   165 	    data = new Data();
       
   166 	}
       
   167 
       
   168 	// Blocks until dismissed
       
   169 	data.dialog.setVisible(true);
       
   170 
       
   171 	if (!ObjectUtil.equals(data.pane.getValue(), JOptionPane.OK_OPTION)) {
       
   172 	    throw new ActionAbortedException();
       
   173 	}
       
   174 
       
   175 	return data;
       
   176     }
       
   177 
       
   178     //
       
   179     // DefaultStructuredAction methods
       
   180     //
       
   181 
       
   182     @Override
       
   183     public VirtualHost workBusy(List<ManagedObject> selection, Data data)
       
   184 	throws ActionAbortedException, ActionFailedException,
       
   185 	ActionUnauthorizedException {
       
   186 
       
   187 	String domain = data.domainProperty.getValue();
       
   188 	VirtualHost.validateDomainName(domain, null);
       
   189 
       
   190 	ApachePanelDescriptor descriptor = control.getPanelDescriptor();
       
   191 
       
   192 	VirtualHost vHost = new VirtualHost(descriptor);
       
   193 	vHost.getDomainProperty().setFirstValue(domain);
       
   194 
       
   195 	vHost.getDocRootProperty().setFirstValue(
       
   196 	    data.docRootProperty.getValue());
       
   197 
       
   198 	descriptor.addChildren(vHost);
       
   199 
       
   200 	// Navigate to the new virtual host
       
   201 	Navigable navigable = new SimpleNavigable(
       
   202 	    VirtualHostControl.ID, null,
       
   203 	    VirtualHostControl.PARAM_ID, vHost.getId());
       
   204 
       
   205 	control.getNavigator().goToAsync(false, control, navigable);
       
   206 
       
   207 	return vHost;
       
   208     }
       
   209 }