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             public void run() {
     86                 ConfigurationState state = mSelector.getState();
     87 
     88                 switch (state) {
     89                     case OK:
     90                         mSelector.getConfiguration(mConfig);
     91 
     92                         resetStatus();
     93                         mStatusImage.setImage(null);
     94                         getButton(IDialogConstants.OK_ID).setEnabled(true);
     95                         break;
     96                     case INVALID_CONFIG:
     97                         ResourceQualifier invalidQualifier = mSelector.getInvalidQualifier();
     98                         mStatusLabel.setText(String.format(
     99                                 "Invalid Configuration: %1$s has no filter set.",
    100                                 invalidQualifier.getName()));
    101                         mStatusImage.setImage(IconFactory.getInstance().getIcon("warning")); //$NON-NLS-1$
    102                         getButton(IDialogConstants.OK_ID).setEnabled(false);
    103                         break;
    104                     case REGION_WITHOUT_LANGUAGE:
    105                         mStatusLabel.setText(
    106                                 "The Region qualifier requires the Language qualifier.");
    107                         mStatusImage.setImage(IconFactory.getInstance().getIcon("warning")); //$NON-NLS-1$
    108                         getButton(IDialogConstants.OK_ID).setEnabled(false);
    109                         break;
    110                 }
    111 
    112                 // need to relayout, because of the change in size in mErrorImage.
    113                 mStatusComposite.layout();
    114             }
    115         });
    116 
    117         mStatusComposite = new Composite(parent, SWT.NONE);
    118         mStatusComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    119         GridLayout gl = new GridLayout(2, false);
    120         mStatusComposite.setLayout(gl);
    121         gl.marginHeight = gl.marginWidth = 0;
    122 
    123         mStatusImage = new Label(mStatusComposite, SWT.NONE);
    124         mStatusLabel = new Label(mStatusComposite, SWT.NONE);
    125         mStatusLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    126         resetStatus();
    127     }
    128 
    129     public void getConfiguration(FolderConfiguration config) {
    130         config.set(mConfig);
    131     }
    132 
    133     /**
    134      * resets the status label to show the file that will be created.
    135      */
    136     private void resetStatus() {
    137         String displayString = Dialog.shortenText(String.format("New File: res/%1$s/%2$s",
    138                 mConfig.getFolderName(ResourceFolderType.LAYOUT), mFileName),
    139                 mStatusLabel);
    140         mStatusLabel.setText(displayString);
    141     }
    142 }
    143