Home | History | Annotate | Download | only in configuration
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.eclipse.org/org/documents/epl-v10.php
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.ide.eclipse.adt.internal.editors.layout.configuration;
     18 
     19 import com.android.ide.common.resources.configuration.FolderConfiguration;
     20 import com.android.ide.common.resources.configuration.ResourceQualifier;
     21 import com.android.ide.eclipse.adt.internal.editors.IconFactory;
     22 import com.android.ide.eclipse.adt.internal.ui.ConfigurationSelector;
     23 import com.android.ide.eclipse.adt.internal.ui.ConfigurationSelector.ConfigurationState;
     24 import com.android.ide.eclipse.adt.internal.ui.ConfigurationSelector.SelectorMode;
     25 import com.android.resources.ResourceFolderType;
     26 import com.android.sdkuilib.ui.GridDialog;
     27 
     28 import org.eclipse.jface.dialogs.Dialog;
     29 import org.eclipse.jface.dialogs.IDialogConstants;
     30 import org.eclipse.swt.SWT;
     31 import org.eclipse.swt.layout.GridData;
     32 import org.eclipse.swt.layout.GridLayout;
     33 import org.eclipse.swt.widgets.Composite;
     34 import org.eclipse.swt.widgets.Label;
     35 import org.eclipse.swt.widgets.Shell;
     36 
     37 /**
     38  * Dialog to choose a non existing {@link FolderConfiguration}.
     39  */
     40 public final class LayoutCreatorDialog extends GridDialog {
     41 
     42     private ConfigurationSelector mSelector;
     43     private Composite mStatusComposite;
     44     private Label mStatusLabel;
     45     private Label mStatusImage;
     46 
     47     private final FolderConfiguration mConfig = new FolderConfiguration();
     48     private final String mFileName;
     49 
     50     /**
     51      * Creates a dialog, and init the UI from a {@link FolderConfiguration}.
     52      * @param parentShell the parent {@link Shell}.
     53      * @param fileName the filename associated with the configuration
     54      * @param config The starting configuration.
     55      */
     56     public LayoutCreatorDialog(Shell parentShell, String fileName, FolderConfiguration config) {
     57         super(parentShell, 1, false);
     58 
     59         mFileName = fileName;
     60 
     61         // FIXME: add some data to know what configurations already exist.
     62         mConfig.set(config);
     63     }
     64 
     65     @Override
     66     public void createDialogContent(Composite parent) {
     67         new Label(parent, SWT.NONE).setText(
     68                 String.format("Configuration for the alternate version of %1$s", mFileName));
     69 
     70         mSelector = new ConfigurationSelector(parent, SelectorMode.CONFIG_ONLY);
     71         mSelector.setConfiguration(mConfig);
     72 
     73         // because the ConfigSelector is running in CONFIG_ONLY mode, the current config
     74         // displayed by it is not mConfig anymore, so get the current config.
     75         mSelector.getConfiguration(mConfig);
     76 
     77         // parent's layout is a GridLayout as specified in the javadoc.
     78         GridData gd = new GridData();
     79         gd.widthHint = ConfigurationSelector.WIDTH_HINT;
     80         gd.heightHint = ConfigurationSelector.HEIGHT_HINT;
     81         mSelector.setLayoutData(gd);
     82 
     83         // add a listener to check on the validity of the FolderConfiguration as
     84         // they are built.
     85         mSelector.setOnChangeListener(new Runnable() {
     86             @Override
     87             public void run() {
     88                 ConfigurationState state = mSelector.getState();
     89 
     90                 switch (state) {
     91                     case OK:
     92                         mSelector.getConfiguration(mConfig);
     93 
     94                         resetStatus();
     95                         mStatusImage.setImage(null);
     96                         getButton(IDialogConstants.OK_ID).setEnabled(true);
     97                         break;
     98                     case INVALID_CONFIG:
     99                         ResourceQualifier invalidQualifier = mSelector.getInvalidQualifier();
    100                         mStatusLabel.setText(String.format(
    101                                 "Invalid Configuration: %1$s has no filter set.",
    102                                 invalidQualifier.getName()));
    103                         mStatusImage.setImage(IconFactory.getInstance().getIcon("warning")); //$NON-NLS-1$
    104                         getButton(IDialogConstants.OK_ID).setEnabled(false);
    105                         break;
    106                     case REGION_WITHOUT_LANGUAGE:
    107                         mStatusLabel.setText(
    108                                 "The Region qualifier requires the Language qualifier.");
    109                         mStatusImage.setImage(IconFactory.getInstance().getIcon("warning")); //$NON-NLS-1$
    110                         getButton(IDialogConstants.OK_ID).setEnabled(false);
    111                         break;
    112                 }
    113 
    114                 // need to relayout, because of the change in size in mErrorImage.
    115                 mStatusComposite.layout();
    116             }
    117         });
    118 
    119         mStatusComposite = new Composite(parent, SWT.NONE);
    120         mStatusComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    121         GridLayout gl = new GridLayout(2, false);
    122         mStatusComposite.setLayout(gl);
    123         gl.marginHeight = gl.marginWidth = 0;
    124 
    125         mStatusImage = new Label(mStatusComposite, SWT.NONE);
    126         mStatusLabel = new Label(mStatusComposite, SWT.NONE);
    127         mStatusLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    128         resetStatus();
    129     }
    130 
    131     /**
    132      * Sets the edited configuration on the given configuration parameter
    133      *
    134      * @param config the configuration to apply the current edits to
    135      */
    136     public void getConfiguration(FolderConfiguration config) {
    137         config.set(mConfig);
    138     }
    139 
    140     /**
    141      * resets the status label to show the file that will be created.
    142      */
    143     private void resetStatus() {
    144         String displayString = Dialog.shortenText(String.format("New File: res/%1$s/%2$s",
    145                 mConfig.getFolderName(ResourceFolderType.LAYOUT), mFileName),
    146                 mStatusLabel);
    147         mStatusLabel.setText(displayString);
    148     }
    149 }
    150