components/visual-panels/apache/src/java/vpanels/app/apache/com/oracle/solaris/vp/panels/apache/client/swing/MimeTypeControl.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.util.Map;
       
    29 import com.oracle.solaris.vp.panel.common.action.*;
       
    30 import com.oracle.solaris.vp.panel.common.control.*;
       
    31 import com.oracle.solaris.vp.panel.swing.control.SettingsControl;
       
    32 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    33 
       
    34 public class MimeTypeControl
       
    35     extends SettingsControl<ApachePanelDescriptor, MimeTypePanel> {
       
    36 
       
    37     //
       
    38     // Static data
       
    39     //
       
    40 
       
    41     public static final String ID = "mimetype";
       
    42     public static final String PARAM_MIMETYPE = "mimetype";
       
    43 
       
    44     //
       
    45     // Instance data
       
    46     //
       
    47 
       
    48     private MimeTypesControl parent;
       
    49     private MimeType mimeType;
       
    50 
       
    51     //
       
    52     // Constructors
       
    53     //
       
    54 
       
    55     public MimeTypeControl(ApachePanelDescriptor descriptor,
       
    56 	MimeTypesControl parent) {
       
    57 
       
    58 	super(ID, null, descriptor);
       
    59 
       
    60 	this.parent = parent;
       
    61     }
       
    62 
       
    63     //
       
    64     // Control methods
       
    65     //
       
    66 
       
    67     @Override
       
    68     protected UnsavedChangesAction getUnsavedChangesAction() {
       
    69 	// Automatically save changes to the MimeType -- let MainControl
       
    70 	// prompt the user to save them to the repo
       
    71 	return UnsavedChangesAction.SAVE;
       
    72     }
       
    73 
       
    74     @Override
       
    75     public boolean isBrowsable() {
       
    76 	// This Control requires init parameters
       
    77 	return false;
       
    78     }
       
    79 
       
    80     @Override
       
    81     protected boolean isChanged() {
       
    82 	// Save unconditionally, since a property that reports itself as
       
    83 	// unchanged in the panel may still be changed from the corresponding
       
    84 	// property in the MimeType.  Alternatively, we could compare each panel
       
    85 	// property to its MimeType counterpart and save only if there are
       
    86 	// differences, but this is easier and save() has low overhead.
       
    87 	return true;
       
    88     }
       
    89 
       
    90     @Override
       
    91     protected void save() throws ActionAbortedException, ActionFailedException,
       
    92 	ActionUnauthorizedException {
       
    93 
       
    94 	setPropertyChangeIgnore(true);
       
    95 
       
    96 	try {
       
    97 	    MimeTypePanel panel = getComponent();
       
    98 
       
    99 	    String type = panel.getTypeProperty().getValue();
       
   100 	    String subtype = panel.getSubtypeProperty().getValue();
       
   101 
       
   102 	    validate(type, subtype);
       
   103 
       
   104 	    MimeType child = mimeType.getParent().getMimeType(type, subtype);
       
   105 	    if (child != null && child != mimeType) {
       
   106 		throw new ActionFailedException(Finder.getString(
       
   107 		    "mimetype.error.duplicate", type, subtype));
       
   108 	    }
       
   109 
       
   110 	    mimeType.getTypeProperty().setValue(type);
       
   111 	    mimeType.getSubtypeProperty().setValue(subtype);
       
   112 	    mimeType.getExtensionsProperty().setValue(
       
   113 		panel.getExtensionsProperty().getValue());
       
   114 	} finally {
       
   115 	    setPropertyChangeIgnore(false);
       
   116 	}
       
   117     }
       
   118 
       
   119     @Override
       
   120     public void start(Navigator navigator, Map<String, String> parameters)
       
   121 	throws NavigationAbortedException, InvalidParameterException,
       
   122 	NavigationFailedException {
       
   123 
       
   124 	String param = getParameter(parameters, PARAM_MIMETYPE);
       
   125 
       
   126 	MimeType mimeType = parent.getHasMimeTypes().getMimeTypes().
       
   127 	    getMimeType(param);
       
   128 	if (mimeType == null) {
       
   129 	    throw new InvalidParameterException(getId(), PARAM_MIMETYPE, param);
       
   130 	}
       
   131 
       
   132 	this.mimeType = mimeType;
       
   133 	setName(mimeType.getName());
       
   134 
       
   135 	setPropertyChangeSource(mimeType);
       
   136 	super.start(navigator, parameters);
       
   137     }
       
   138 
       
   139     @Override
       
   140     public void stop(boolean isCancel) throws NavigationAbortedException {
       
   141 	super.stop(isCancel);
       
   142 
       
   143 	// Remove reference so it can be garbage collected if deleted
       
   144 	mimeType = null;
       
   145     }
       
   146 
       
   147     //
       
   148     // SwingControl methods
       
   149     //
       
   150 
       
   151     @Override
       
   152     protected MimeTypePanel createComponent() {
       
   153 	return new MimeTypePanel();
       
   154     }
       
   155 
       
   156     @Override
       
   157     protected void initComponent() {
       
   158 	MimeTypePanel panel = getComponent();
       
   159 	panel.getChangeableAggregator().reset();
       
   160 	panel.init(mimeType);
       
   161     }
       
   162 
       
   163     //
       
   164     // MimeTypeControl methods
       
   165     //
       
   166 
       
   167     public MimeType getMimeType() {
       
   168 	return mimeType;
       
   169     }
       
   170 
       
   171     //
       
   172     // Static methods
       
   173     //
       
   174 
       
   175     /**
       
   176      * Validates the entered data in the UI.
       
   177      *
       
   178      * @exception   ActionFailedException
       
   179      *		    if the entered data is invalid (the thrown exception will
       
   180      *		    contain a localized error message)
       
   181      */
       
   182     public static void validate(String type, String subtype)
       
   183 	throws ActionFailedException {
       
   184 
       
   185 	if (type.isEmpty()) {
       
   186 	    throw new ActionFailedException(Finder.getString(
       
   187 		"mimetype.error.type.empty", type));
       
   188 	}
       
   189 
       
   190 	if (!MimeType.isValidToken(type)) {
       
   191 	    throw new ActionFailedException(Finder.getString(
       
   192 		"mimetype.error.type.invalid", type));
       
   193 	}
       
   194 
       
   195 	if (subtype.isEmpty()) {
       
   196 	    throw new ActionFailedException(Finder.getString(
       
   197 		"mimetype.error.subtype.empty", subtype));
       
   198 	}
       
   199 
       
   200 	if (!MimeType.isValidToken(subtype)) {
       
   201 	    throw new ActionFailedException(Finder.getString(
       
   202 		"mimetype.error.subtype.invalid", subtype));
       
   203 	}
       
   204     }
       
   205 }