components/visual-panels/apache/src/java/vpanels/app/apache/com/oracle/solaris/vp/panels/apache/client/swing/CreateMimeTypeAction.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 com.oracle.solaris.vp.panel.common.action.*;
       
    33 import com.oracle.solaris.vp.panel.common.control.*;
       
    34 import com.oracle.solaris.vp.panel.swing.action.AddManagedObjectAction;
       
    35 import com.oracle.solaris.vp.util.misc.ObjectUtil;
       
    36 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    37 import com.oracle.solaris.vp.util.misc.property.*;
       
    38 import com.oracle.solaris.vp.util.swing.*;
       
    39 import com.oracle.solaris.vp.util.swing.layout.*;
       
    40 import com.oracle.solaris.vp.util.swing.property.
       
    41     TextComponentPropertySynchronizer;
       
    42 
       
    43 @SuppressWarnings({"serial"})
       
    44 public class CreateMimeTypeAction extends AddManagedObjectAction
       
    45     <MimeType, CreateMimeTypeAction.Data, MimeType> {
       
    46 
       
    47     //
       
    48     // Inner classes
       
    49     //
       
    50 
       
    51     protected class Data {
       
    52 	//
       
    53 	// Instance data
       
    54 	//
       
    55 
       
    56 	public StringProperty typeProperty = new StringProperty();
       
    57 	public StringProperty subtypeProperty = new StringProperty();
       
    58 	public StringArrayProperty extProperty = new StringArrayProperty();
       
    59 	public JOptionPane pane;
       
    60 	public JDialog dialog;
       
    61 
       
    62 	//
       
    63 	// Constructors
       
    64 	//
       
    65 
       
    66 	public Data() {
       
    67 	    ActionListener listener =
       
    68 		new ActionListener() {
       
    69 		    @Override
       
    70 		    public void actionPerformed(ActionEvent e) {
       
    71 			pane.setValue(JOptionPane.OK_OPTION);
       
    72 		    }
       
    73 		};
       
    74 
       
    75 	    JLabel typeLabel = new JLabel(
       
    76 		Finder.getString("mimetype.edit.type"));
       
    77 	    JTextField typeField = GUIUtil.createTextField();
       
    78 	    typeField.addActionListener(listener);
       
    79 	    new TextComponentPropertySynchronizer<String, JTextField>(
       
    80 		typeProperty, typeField, false);
       
    81 
       
    82 	    JLabel subtypeLabel = new JLabel(
       
    83 		Finder.getString("mimetype.edit.subtype"));
       
    84 	    JTextField subtypeField = GUIUtil.createTextField();
       
    85 	    subtypeField.addActionListener(listener);
       
    86 	    new TextComponentPropertySynchronizer<String, JTextField>(
       
    87 		subtypeProperty, subtypeField, false);
       
    88 
       
    89 	    JLabel extLabel = new JLabel(
       
    90 		Finder.getString("mimetype.edit.extensions"));
       
    91 	    JTextField extField = GUIUtil.createTextField();
       
    92 	    extField.addActionListener(listener);
       
    93 	    new TextComponentPropertySynchronizer<String[], JTextField>(
       
    94 		extProperty, extField, 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(typeLabel, a);
       
   111 	    form.add(typeField, a);
       
   112 
       
   113 	    form.add(subtypeLabel, a);
       
   114 	    form.add(subtypeField, a);
       
   115 
       
   116 	    form.add(extLabel, a);
       
   117 	    form.add(extField, a);
       
   118 
       
   119 	    pane = new JOptionPane(formPanel, JOptionPane.PLAIN_MESSAGE,
       
   120 		JOptionPane.OK_CANCEL_OPTION);
       
   121 
       
   122 	    dialog = pane.createDialog(getHasComponent().getComponent(),
       
   123 		Finder.getString("mimetypes.action.create.title"));
       
   124 	}
       
   125     }
       
   126 
       
   127     //
       
   128     // Static data
       
   129     //
       
   130 
       
   131     private static final String TEXT = Finder.getString(
       
   132 	"mimetypes.action.create.button");
       
   133 
       
   134     //
       
   135     // Instance data
       
   136     //
       
   137 
       
   138     private MimeTypesControl control;
       
   139 
       
   140     //
       
   141     // Constructors
       
   142     //
       
   143 
       
   144     public CreateMimeTypeAction(MimeTypesControl control) {
       
   145 	super(TEXT, null, control);
       
   146 	this.control = control;
       
   147 	setLoops(true);
       
   148     }
       
   149 
       
   150     //
       
   151     // StructuredAction methods
       
   152     //
       
   153 
       
   154     @Override
       
   155     public Data getRuntimeInput(List<MimeType> selection, Data data)
       
   156 	throws ActionAbortedException {
       
   157 
       
   158 	// Since this action navigates to the new object once created, we must
       
   159 	// first navigate to control so that any outstanding changes in the
       
   160 	// current Control are handled.
       
   161 	try {
       
   162 	    control.getNavigator().goToAsyncAndWait(false, control);
       
   163 	} catch (NavigationException e) {
       
   164 	    throw new ActionAbortedException(e);
       
   165 	}
       
   166 
       
   167 	if (data == null) {
       
   168 	    data = new Data();
       
   169 	}
       
   170 
       
   171 	// Blocks until dismissed
       
   172 	data.dialog.setVisible(true);
       
   173 
       
   174 	if (!ObjectUtil.equals(data.pane.getValue(), JOptionPane.OK_OPTION)) {
       
   175 	    throw new ActionAbortedException();
       
   176 	}
       
   177 
       
   178 	return data;
       
   179     }
       
   180 
       
   181     //
       
   182     // DefaultStructuredAction methods
       
   183     //
       
   184 
       
   185     @Override
       
   186     public MimeType workBusy(List<MimeType> selection, Data data)
       
   187 	throws ActionAbortedException, ActionFailedException,
       
   188 	ActionUnauthorizedException {
       
   189 
       
   190 	String type = data.typeProperty.getValue();
       
   191 	String subtype = data.subtypeProperty.getValue();
       
   192 	String[] extensions = data.extProperty.getValue();
       
   193 
       
   194 	// Throws ActionFailedException
       
   195 	MimeTypeControl.validate(type, subtype);
       
   196 
       
   197 	MimeTypes mimeTypes = control.getHasMimeTypes().getMimeTypes();
       
   198 
       
   199 	if (mimeTypes.getMimeType(type, subtype) != null) {
       
   200 	    throw new ActionFailedException(Finder.getString(
       
   201 		"mimetype.error.duplicate", type, subtype));
       
   202 	}
       
   203 
       
   204 	MimeType mimeType = new MimeType(mimeTypes);
       
   205 	mimeType.getTypeProperty().setValue(type);
       
   206 	mimeType.getSubtypeProperty().setValue(subtype);
       
   207 	mimeType.getExtensionsProperty().setValue(extensions);
       
   208 
       
   209 	mimeTypes.addChildren(mimeType);
       
   210 
       
   211 	// Navigate to the new MIME type
       
   212 	Navigable navigable = new SimpleNavigable(
       
   213 	    MimeTypeControl.ID, null,
       
   214 	    MimeTypeControl.PARAM_MIMETYPE, mimeType.getId());
       
   215 
       
   216 	control.getNavigator().goToAsync(false, control, navigable);
       
   217 
       
   218 	return mimeType;
       
   219     }
       
   220 }