aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/configuration/LayoutCreatorDialog.java
blob: 97ff668453897174331fbed76906cca44b3b656d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.adt.internal.editors.layout.configuration;

import com.android.ide.common.resources.configuration.FolderConfiguration;
import com.android.ide.common.resources.configuration.ResourceQualifier;
import com.android.ide.eclipse.adt.internal.editors.IconFactory;
import com.android.ide.eclipse.adt.internal.ui.ConfigurationSelector;
import com.android.ide.eclipse.adt.internal.ui.ConfigurationSelector.ConfigurationState;
import com.android.ide.eclipse.adt.internal.ui.ConfigurationSelector.SelectorMode;
import com.android.resources.ResourceFolderType;
import com.android.sdkuilib.ui.GridDialog;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * Dialog to choose a non existing {@link FolderConfiguration}.
 */
public final class LayoutCreatorDialog extends GridDialog {

    private ConfigurationSelector mSelector;
    private Composite mStatusComposite;
    private Label mStatusLabel;
    private Label mStatusImage;

    private final FolderConfiguration mConfig = new FolderConfiguration();
    private final String mFileName;

    /**
     * Creates a dialog, and init the UI from a {@link FolderConfiguration}.
     * @param parentShell the parent {@link Shell}.
     * @param fileName the filename associated with the configuration
     * @param config The starting configuration.
     */
    public LayoutCreatorDialog(Shell parentShell, String fileName, FolderConfiguration config) {
        super(parentShell, 1, false);

        mFileName = fileName;

        // FIXME: add some data to know what configurations already exist.
        mConfig.set(config);
    }

    @Override
    public void createDialogContent(Composite parent) {
        new Label(parent, SWT.NONE).setText(
                String.format("Configuration for the alternate version of %1$s", mFileName));

        mSelector = new ConfigurationSelector(parent, SelectorMode.CONFIG_ONLY);
        mSelector.setConfiguration(mConfig);

        // because the ConfigSelector is running in CONFIG_ONLY mode, the current config
        // displayed by it is not mConfig anymore, so get the current config.
        mSelector.getConfiguration(mConfig);

        // parent's layout is a GridLayout as specified in the javadoc.
        GridData gd = new GridData();
        gd.widthHint = ConfigurationSelector.WIDTH_HINT;
        gd.heightHint = ConfigurationSelector.HEIGHT_HINT;
        mSelector.setLayoutData(gd);

        // add a listener to check on the validity of the FolderConfiguration as
        // they are built.
        mSelector.setOnChangeListener(new Runnable() {
            @Override
            public void run() {
                ConfigurationState state = mSelector.getState();

                switch (state) {
                    case OK:
                        mSelector.getConfiguration(mConfig);

                        resetStatus();
                        mStatusImage.setImage(null);
                        getButton(IDialogConstants.OK_ID).setEnabled(true);
                        break;
                    case INVALID_CONFIG:
                        ResourceQualifier invalidQualifier = mSelector.getInvalidQualifier();
                        mStatusLabel.setText(String.format(
                                "Invalid Configuration: %1$s has no filter set.",
                                invalidQualifier.getName()));
                        mStatusImage.setImage(IconFactory.getInstance().getIcon("warning")); //$NON-NLS-1$
                        getButton(IDialogConstants.OK_ID).setEnabled(false);
                        break;
                    case REGION_WITHOUT_LANGUAGE:
                        mStatusLabel.setText(
                                "The Region qualifier requires the Language qualifier.");
                        mStatusImage.setImage(IconFactory.getInstance().getIcon("warning")); //$NON-NLS-1$
                        getButton(IDialogConstants.OK_ID).setEnabled(false);
                        break;
                }

                // need to relayout, because of the change in size in mErrorImage.
                mStatusComposite.layout();
            }
        });

        mStatusComposite = new Composite(parent, SWT.NONE);
        mStatusComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        GridLayout gl = new GridLayout(2, false);
        mStatusComposite.setLayout(gl);
        gl.marginHeight = gl.marginWidth = 0;

        mStatusImage = new Label(mStatusComposite, SWT.NONE);
        mStatusLabel = new Label(mStatusComposite, SWT.NONE);
        mStatusLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        resetStatus();
    }

    /**
     * Sets the edited configuration on the given configuration parameter
     *
     * @param config the configuration to apply the current edits to
     */
    public void getConfiguration(FolderConfiguration config) {
        config.set(mConfig);
    }

    /**
     * resets the status label to show the file that will be created.
     */
    private void resetStatus() {
        String displayString = Dialog.shortenText(String.format("New File: res/%1$s/%2$s",
                mConfig.getFolderName(ResourceFolderType.LAYOUT), mFileName),
                mStatusLabel);
        mStatusLabel.setText(displayString);
    }
}