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 config The starting configuration.
     54      */
     55     public LayoutCreatorDialog(Shell parentShell, String fileName, FolderConfiguration config) {
     56         super(parentShell, 1, false);
     57 
     58         mFileName = fileName;
     59 
     60         // FIXME: add some data to know what configurations already exist.
     61         mConfig.set(config);
     62     }
     63 
     64     @Override
     65     public void createDialogContent(Composite parent) {
     66         new Label(parent, SWT.NONE).setText(
     67                 String.format("Configuration for the alternate version of %1$s", mFileName));
     68 
     69         mSelector = new ConfigurationSelector(parent, SelectorMode.CONFIG_ONLY);
     70         mSelector.setConfiguration(mConfig);
     71 
     72         // because the ConfigSelector is running in CONFIG_ONLY mode, the current config
     73         // displayed by it is not mConfig anymore, so get the current config.
     74         mSelector.getConfiguration(mConfig);
     75 
     76         // parent's layout is a GridLayout as specified in the javadoc.
     77         GridData gd = new GridData();
     78         gd.widthHint = ConfigurationSelector.WIDTH_HINT;
     79         gd.heightHint = ConfigurationSelector.HEIGHT_HINT;
     80         mSelector.setLayoutData(gd);
     81 
     82         // add a listener to check on the validity of the FolderConfiguration as
     83         // they are built.
     84         mSelector.setOnChangeListener(new Runnable() {
     85             @Override
     86             public void run() {
     87                 ConfigurationState state = mSelector.getState();
     88 
     89                 switch (state) {
     90                     case OK:
     91                         mSelector.getConfiguration(mConfig);
     92 
     93                         resetStatus();
     94                         mStatusImage.setImage(null);
     95                         getButton(IDialogConstants.OK_ID).setEnabled(true);
     96                         break;
     97                     case INVALID_CONFIG:
     98                         ResourceQualifier invalidQualifier = mSelector.getInvalidQualifier();
     99                         mStatusLabel.setText(String.format(
    100                                 "Invalid Configuration: %1$s has no filter set.",
    101                                 invalidQualifier.getName()));
    102                         mStatusImage.setImage(IconFactory.getInstance().getIcon("warning")); //$NON-NLS-1$
    103                         getButton(IDialogConstants.OK_ID).setEnabled(false);
    104                         break;
    105                     case REGION_WITHOUT_LANGUAGE:
    106                         mStatusLabel.setText(
    107                                 "The Region qualifier requires the Language qualifier.");
    108                         mStatusImage.setImage(IconFactory.getInstance().getIcon("warning")); //$NON-NLS-1$
    109                         getButton(IDialogConstants.OK_ID).setEnabled(false);
    110                         break;
    111                 }
    112 
    113                 // need to relayout, because of the change in size in mErrorImage.
    114                 mStatusComposite.layout();
    115             }
    116         });
    117 
    118         mStatusComposite = new Composite(parent, SWT.NONE);
    119         mStatusComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    120         GridLayout gl = new GridLayout(2, false);
    121         mStatusComposite.setLayout(gl);
    122         gl.marginHeight = gl.marginWidth = 0;
    123 
    124         mStatusImage = new Label(mStatusComposite, SWT.NONE);
    125         mStatusLabel = new Label(mStatusComposite, SWT.NONE);
    126         mStatusLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    127         resetStatus();
    128     }
    129 
    130     public void getConfiguration(FolderConfiguration config) {
    131         config.set(mConfig);
    132     }
    133 
    134     /**
    135      * resets the status label to show the file that will be created.
    136      */
    137     private void resetStatus() {
    138         String displayString = Dialog.shortenText(String.format("New File: res/%1$s/%2$s",
    139                 mConfig.getFolderName(ResourceFolderType.LAYOUT), mFileName),
    140                 mStatusLabel);
    141         mStatusLabel.setText(displayString);
    142     }
    143 }
    144