FileDocCategorySizeDatePackage
ConfigurationSelector.javaAPI DocAndroid 1.5 API50257Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.adt.ui

ConfigurationSelector

public class ConfigurationSelector extends org.eclipse.swt.widgets.Composite
Custom UI widget to let user build a Folder configuration.

To use this, instantiate somewhere in the UI and then:

  • Use {@link #setConfiguration(String)} or {@link #setConfiguration(FolderConfiguration)}.
  • Retrieve the configuration using {@link #getConfiguration(FolderConfiguration)}.

Fields Summary
public static final int
WIDTH_HINT
public static final int
HEIGHT_HINT
private Runnable
mOnChangeListener
private org.eclipse.jface.viewers.TableViewer
mFullTableViewer
private org.eclipse.jface.viewers.TableViewer
mSelectionTableViewer
private org.eclipse.swt.widgets.Button
mAddButton
private org.eclipse.swt.widgets.Button
mRemoveButton
private org.eclipse.swt.custom.StackLayout
mStackLayout
private boolean
mOnRefresh
private final com.android.ide.eclipse.editors.resources.configurations.FolderConfiguration
mBaseConfiguration
private final com.android.ide.eclipse.editors.resources.configurations.FolderConfiguration
mSelectedConfiguration
private final HashMap
mUiMap
private org.eclipse.swt.widgets.Composite
mQualifierEditParent
Constructors Summary
public ConfigurationSelector(org.eclipse.swt.widgets.Composite parent)

        super(parent, SWT.NONE);

        mBaseConfiguration.createDefault();

        GridLayout gl = new GridLayout(4, false);
        gl.marginWidth = gl.marginHeight = 0;
        setLayout(gl);
        
        // first column is the first table
        final Table fullTable = new Table(this, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER);
        fullTable.setLayoutData(new GridData(GridData.FILL_BOTH));
        fullTable.setHeaderVisible(true);
        fullTable.setLinesVisible(true);
        
        // create the column
        final TableColumn fullTableColumn = new TableColumn(fullTable, SWT.LEFT);
        // set the header
        fullTableColumn.setText("Available Qualifiers");
        
        fullTable.addControlListener(new ControlAdapter() {
            @Override
            public void controlResized(ControlEvent e) {
                Rectangle r = fullTable.getClientArea();
                fullTableColumn.setWidth(r.width);
            }
        });

        mFullTableViewer = new TableViewer(fullTable);
        mFullTableViewer.setContentProvider(new QualifierContentProvider());
        mFullTableViewer.setLabelProvider(new QualifierLabelProvider(
                false /* showQualifierValue */));
        mFullTableViewer.setInput(mBaseConfiguration);
        mFullTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
            public void selectionChanged(SelectionChangedEvent event) {
                ISelection selection = event.getSelection();
                if (selection instanceof IStructuredSelection) {
                    IStructuredSelection structSelection = (IStructuredSelection)selection;
                    Object first = structSelection.getFirstElement();
                    
                    if (first instanceof ResourceQualifier) {
                        mAddButton.setEnabled(true);
                        return;
                    }
                }
                
                mAddButton.setEnabled(false);
            }
        });
        
        // 2nd column is the left/right arrow button
        Composite buttonComposite = new Composite(this, SWT.NONE);
        gl = new GridLayout(1, false);
        gl.marginWidth = gl.marginHeight = 0;
        buttonComposite.setLayout(gl);
        buttonComposite.setLayoutData(new GridData(GridData.FILL_VERTICAL));
        
        new Composite(buttonComposite, SWT.NONE);
        mAddButton = new Button(buttonComposite, SWT.BORDER | SWT.PUSH);
        mAddButton.setText("->");
        mAddButton.setEnabled(false);
        mAddButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                IStructuredSelection selection = 
                    (IStructuredSelection)mFullTableViewer.getSelection();
                
                Object first = selection.getFirstElement();
                if (first instanceof ResourceQualifier) {
                    ResourceQualifier qualifier = (ResourceQualifier)first;
                    
                    mBaseConfiguration.removeQualifier(qualifier);
                    mSelectedConfiguration.addQualifier(qualifier);
                    
                    mFullTableViewer.refresh();
                    mSelectionTableViewer.refresh();
                    mSelectionTableViewer.setSelection(new StructuredSelection(qualifier), true);
                    
                    onChange(false /* keepSelection */);
                }
            }
        });

        mRemoveButton = new Button(buttonComposite, SWT.BORDER | SWT.PUSH);
        mRemoveButton.setText("<-");
        mRemoveButton.setEnabled(false);
        mRemoveButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                IStructuredSelection selection = 
                    (IStructuredSelection)mSelectionTableViewer.getSelection();
                
                Object first = selection.getFirstElement();
                if (first instanceof ResourceQualifier) {
                    ResourceQualifier qualifier = (ResourceQualifier)first;
                    
                    mSelectedConfiguration.removeQualifier(qualifier);
                    mBaseConfiguration.addQualifier(qualifier);

                    mFullTableViewer.refresh();
                    mSelectionTableViewer.refresh();
                    
                    onChange(false /* keepSelection */);
                }
            }
        });

        // 3rd column is the selected config table
        final Table selectionTable = new Table(this, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER);
        selectionTable.setLayoutData(new GridData(GridData.FILL_BOTH));
        selectionTable.setHeaderVisible(true);
        selectionTable.setLinesVisible(true);
        
        // create the column
        final TableColumn selectionTableColumn = new TableColumn(selectionTable, SWT.LEFT);
        // set the header
        selectionTableColumn.setText("Chosen Qualifiers");
        
        selectionTable.addControlListener(new ControlAdapter() {
            @Override
            public void controlResized(ControlEvent e) {
                Rectangle r = selectionTable.getClientArea();
                selectionTableColumn.setWidth(r.width);
            }
        });
        mSelectionTableViewer = new TableViewer(selectionTable);
        mSelectionTableViewer.setContentProvider(new QualifierContentProvider());
        mSelectionTableViewer.setLabelProvider(new QualifierLabelProvider(
                true /* showQualifierValue */));
        mSelectionTableViewer.setInput(mSelectedConfiguration);
        mSelectionTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
            public void selectionChanged(SelectionChangedEvent event) {
                // ignore selection changes during resfreshes in some cases.
                if (mOnRefresh) {
                    return;
                }

                ISelection selection = event.getSelection();
                if (selection instanceof IStructuredSelection) {
                    IStructuredSelection structSelection = (IStructuredSelection)selection;
                    
                    if (structSelection.isEmpty() == false) {
                        Object first = structSelection.getFirstElement();
                        
                        if (first instanceof ResourceQualifier) {
                            mRemoveButton.setEnabled(true);
                            
                            QualifierEditBase composite = mUiMap.get(first.getClass());
    
                            if (composite != null) {
                                composite.setQualifier((ResourceQualifier)first);
                            }
    
                            mStackLayout.topControl = composite;
                            mQualifierEditParent.layout();
                            
                            return;
                        }
                    } else {
                        mStackLayout.topControl = null;
                        mQualifierEditParent.layout();
                    }
                }
                
                mRemoveButton.setEnabled(false);
            }
        });
        
        // 4th column is the detail of the selected qualifier 
        mQualifierEditParent = new Composite(this, SWT.NONE);
        mQualifierEditParent.setLayout(mStackLayout = new StackLayout());
        mQualifierEditParent.setLayoutData(new GridData(GridData.FILL_VERTICAL));
        
        // create the UI for all the qualifiers, and associate them to the ResourceQualifer class.
        mUiMap.put(CountryCodeQualifier.class, new MCCEdit(mQualifierEditParent));
        mUiMap.put(NetworkCodeQualifier.class, new MNCEdit(mQualifierEditParent));
        mUiMap.put(LanguageQualifier.class, new LanguageEdit(mQualifierEditParent));
        mUiMap.put(RegionQualifier.class, new RegionEdit(mQualifierEditParent));
        mUiMap.put(ScreenOrientationQualifier.class, new OrientationEdit(mQualifierEditParent));
        mUiMap.put(PixelDensityQualifier.class, new PixelDensityEdit(mQualifierEditParent));
        mUiMap.put(TouchScreenQualifier.class, new TouchEdit(mQualifierEditParent));
        mUiMap.put(KeyboardStateQualifier.class, new KeyboardEdit(mQualifierEditParent));
        mUiMap.put(TextInputMethodQualifier.class, new TextInputEdit(mQualifierEditParent));
        mUiMap.put(NavigationMethodQualifier.class, new NavigationEdit(mQualifierEditParent));
        mUiMap.put(ScreenDimensionQualifier.class, new ScreenDimensionEdit(mQualifierEditParent));
    
Methods Summary
public voidgetConfiguration(com.android.ide.eclipse.editors.resources.configurations.FolderConfiguration config)
Gets the configuration as setup by the widget.

param
config the {@link FolderConfiguration} object to be filled with the information from the UI.

        config.set(mSelectedConfiguration);
    
public com.android.ide.eclipse.editors.resources.configurations.ResourceQualifiergetInvalidQualifier()
Returns the first invalid qualifier of the configuration being edited/created, or null if they are all valid (or if none exists).

If {@link #getState()} return {@link ConfigurationState#INVALID_CONFIG} then this will not return null.

        return mSelectedConfiguration.getInvalidQualifier();
    
public com.android.ide.eclipse.adt.ui.ConfigurationSelector$ConfigurationStategetState()
Returns the state of the configuration being edited/created.

        if (mSelectedConfiguration.getInvalidQualifier() != null) {
            return ConfigurationState.INVALID_CONFIG;
        }
        
        if (mSelectedConfiguration.checkRegion() == false) {
            return ConfigurationState.REGION_WITHOUT_LANGUAGE;
        }
        
        return ConfigurationState.OK;
    
private voidonChange(boolean keepSelection)
Handle changes in the configuration.

param
keepSelection if true attemps to avoid triggering selection change in {@link #mSelectedConfiguration}.

        ISelection selection = null;
        if (keepSelection) {
            mOnRefresh = true;
            selection = mSelectionTableViewer.getSelection();
        }

        mSelectionTableViewer.refresh(true);
        
        if (keepSelection) {
            mSelectionTableViewer.setSelection(selection);
            mOnRefresh = false;
        }

        if (mOnChangeListener != null) {
            mOnChangeListener.run();
        }
    
public voidsetConfiguration(com.android.ide.eclipse.editors.resources.configurations.FolderConfiguration config)
Initialize the UI with a given {@link FolderConfiguration}. This must be called from the UI thread.

param
config The configuration.

        mSelectedConfiguration.set(config);
        mSelectionTableViewer.refresh();
        
        // create the base config, which is the default config minus the qualifiers
        // in SelectedConfiguration
        mBaseConfiguration.substract(mSelectedConfiguration);
        mFullTableViewer.refresh();
    
public booleansetConfiguration(java.lang.String[] folderSegments)
Initialize the UI with the configuration represented by a resource folder name. This must be called from the UI thread.

param
folderSegments the segments of the folder name, split using {@link FolderConfiguration#QUALIFIER_SEP}.
return
true if success, or false if the folder name is not a valid name.

        FolderConfiguration config = ResourceManager.getInstance().getConfig(folderSegments);
        
        if (config == null) {
            return false;
        }

        setConfiguration(config);

        return true;
    
public booleansetConfiguration(java.lang.String folderName)
Initialize the UI with the configuration represented by a resource folder name. This must be called from the UI thread.

param
folderName the name of the folder.
return
true if success, or false if the folder name is not a valid name.

        // split the name of the folder in segments.
        String[] folderSegments = folderName.split(FolderConfiguration.QUALIFIER_SEP);

        return setConfiguration(folderSegments);
    
public voidsetOnChangeListener(java.lang.Runnable listener)
Sets a listener to be notified when the configuration changes.

param
listener A {@link Runnable} whose run() method is called when the configuration is changed. The method is called from the UI thread.

        mOnChangeListener = listener;