org.chromium.debug.ui/src/org/chromium/debug/ui/source/SourceNameMapperContainerDialog.java
changeset 355 8726e95bcbba
equal deleted inserted replaced
354:0bceeb415e7f 355:8726e95bcbba
       
     1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
       
     2 // Use of this source code is governed by a BSD-style license that can be
       
     3 // found in the LICENSE file.
       
     4 
       
     5 package org.chromium.debug.ui.source;
       
     6 
       
     7 import java.util.ArrayList;
       
     8 import java.util.Collections;
       
     9 import java.util.Comparator;
       
    10 import java.util.List;
       
    11 
       
    12 import org.chromium.debug.ui.DialogUtils.ComboWrapper;
       
    13 import org.chromium.debug.ui.DialogUtils.DialogElements;
       
    14 import org.eclipse.debug.core.DebugPlugin;
       
    15 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
       
    16 import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
       
    17 import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
       
    18 import org.eclipse.debug.ui.DebugUITools;
       
    19 import org.eclipse.debug.ui.sourcelookup.ISourceContainerBrowser;
       
    20 import org.eclipse.jface.dialogs.Dialog;
       
    21 import org.eclipse.jface.dialogs.IDialogConstants;
       
    22 import org.eclipse.jface.dialogs.TitleAreaDialog;
       
    23 import org.eclipse.swt.SWT;
       
    24 import org.eclipse.swt.layout.GridData;
       
    25 import org.eclipse.swt.layout.GridLayout;
       
    26 import org.eclipse.swt.widgets.Button;
       
    27 import org.eclipse.swt.widgets.Combo;
       
    28 import org.eclipse.swt.widgets.Composite;
       
    29 import org.eclipse.swt.widgets.Control;
       
    30 import org.eclipse.swt.widgets.Group;
       
    31 import org.eclipse.swt.widgets.Label;
       
    32 import org.eclipse.swt.widgets.Shell;
       
    33 import org.eclipse.swt.widgets.Text;
       
    34 
       
    35 /**
       
    36  * A dialog for adding and editing JavaScript source name mapper containers.
       
    37  */
       
    38 public class SourceNameMapperContainerDialog extends TitleAreaDialog {
       
    39   private final ISourceLookupDirector director;
       
    40   private final PresetFieldValues initialParams;
       
    41 
       
    42   private Result result = null;
       
    43   private SourceNameMapperContainerDialogLogic logic = null;
       
    44 
       
    45   /**
       
    46    * An optional set of preset dialog field values. Useful in "edit" (not "add") mode of dialog.
       
    47    */
       
    48   public interface PresetFieldValues {
       
    49     String getPrefix();
       
    50     ISourceContainer getContainer();
       
    51   }
       
    52 
       
    53   public interface Result {
       
    54     String getResultPrefix();
       
    55     ISourceContainer getResultContainer();
       
    56   }
       
    57 
       
    58   public Result getResult() {
       
    59     return result;
       
    60   }
       
    61 
       
    62   public SourceNameMapperContainerDialog(Shell shell, ISourceLookupDirector director,
       
    63     PresetFieldValues initialParams) {
       
    64     super(shell);
       
    65     setShellStyle(getShellStyle() | SWT.RESIZE);
       
    66     this.director = director;
       
    67     this.initialParams = initialParams;
       
    68   }
       
    69 
       
    70   @Override
       
    71   protected Control createDialogArea(Composite ancestor) {
       
    72     getShell().setText(Messages.SourceNameMapperContainerDialog_DIALOG_TITLE);
       
    73     setTitle(Messages.SourceNameMapperContainerDialog_DIALOG_SUBTITLE);
       
    74 
       
    75     Composite parent = new Composite(ancestor, SWT.NULL);
       
    76     {
       
    77       GridLayout topLayout = new GridLayout();
       
    78       topLayout.numColumns = 1;
       
    79       parent.setLayout(topLayout);
       
    80       parent.setLayoutData(new GridData(GridData.FILL_BOTH));
       
    81     }
       
    82 
       
    83     Label explanationOne = new Label(parent, 0);
       
    84     explanationOne.setText(
       
    85         Messages.SourceNameMapperContainerDialog_EXPLANATION_1);
       
    86 
       
    87     Group prefixGroup = new Group(parent, SWT.NONE);
       
    88     prefixGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
    89     prefixGroup.setText(Messages.SourceNameMapperContainerDialog_PREFIX_GROUP);
       
    90     prefixGroup.setLayout(new GridLayout(1, false));
       
    91     final Text prefixEditor = new Text(prefixGroup, SWT.SINGLE | SWT.BORDER);
       
    92     prefixEditor.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
    93     final Label prefixExampleLine1Label = new Label(prefixGroup, 0);
       
    94     prefixExampleLine1Label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
    95     final Label prefixExampleLine2Label = new Label(prefixGroup, 0);
       
    96     prefixExampleLine2Label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
    97 
       
    98     Label explanationTwo = new Label(parent, 0);
       
    99     explanationTwo.setText(Messages.SourceNameMapperContainerDialog_EXPLANATION_2);
       
   100 
       
   101     Group containerGroup = new Group(parent, SWT.NONE);
       
   102     containerGroup.setLayout(new GridLayout(1, false));
       
   103     containerGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
   104     containerGroup.setText(Messages.SourceNameMapperContainerDialog_CONTAINER_GROUP);
       
   105 
       
   106     Composite typeBlock = new Composite(containerGroup, SWT.NULL);
       
   107     typeBlock.setLayout(new GridLayout(3, false));
       
   108     typeBlock.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
   109 
       
   110     final List<ISourceContainerType> types =
       
   111         filterTypes(DebugPlugin.getDefault().getLaunchManager().getSourceContainerTypes());
       
   112 
       
   113     Collections.sort(types, TYPE_COMPARATOR_BY_NAME);
       
   114 
       
   115     String[] typeNameArray = new String[types.size()];
       
   116     for (int i = 0; i < typeNameArray.length; i++) {
       
   117       typeNameArray[i] = types.get(i).getName();
       
   118     }
       
   119 
       
   120     Label comboLabel = new Label(typeBlock, 0);
       
   121     comboLabel.setText(Messages.SourceNameMapperContainerDialog_TYPE_LABEL);
       
   122 
       
   123     Combo typesCombo = new Combo(typeBlock, SWT.READ_ONLY);
       
   124     typesCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
   125     typesCombo.setFont(parent.getFont());
       
   126     typesCombo.setItems(typeNameArray);
       
   127     if (typeNameArray.length > 0) {
       
   128       typesCombo.select(0);
       
   129     }
       
   130     final Button configureButton = new Button(typeBlock, SWT.PUSH);
       
   131     configureButton.setText(Messages.SourceNameMapperContainerDialog_CONFIGURE_BUTTON);
       
   132 
       
   133     final Composite statusBox = new Composite(containerGroup, SWT.NULL);
       
   134     statusBox.setLayout(new GridLayout(3, false));
       
   135     statusBox.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
   136 
       
   137     final Label statusLabel = new Label(statusBox, 0);
       
   138     final Label containerTypeIconLabel = new Label(statusBox, 0);
       
   139     final Label containerNameLabel = new Label(statusBox, 0);
       
   140 
       
   141     Dialog.applyDialogFont(parent);
       
   142 
       
   143     // Implementing Elements interface
       
   144     final ComboWrapper<ISourceContainerType> comboWrapper =
       
   145         new ComboWrapper<ISourceContainerType>(typesCombo) {
       
   146       @Override
       
   147       public ISourceContainerType getSelected() {
       
   148         return types.get(getCombo().getSelectionIndex());
       
   149       }
       
   150       @Override
       
   151       public void setSelected(ISourceContainerType element) {
       
   152         int index = types.indexOf(element);
       
   153         if (index != -1) {
       
   154           getCombo().select(index);
       
   155         }
       
   156       }
       
   157     };
       
   158 
       
   159     final ContainerStatusGroup containerStatusGroup = new ContainerStatusGroup() {
       
   160       public Label getStatusLabel() {
       
   161         return statusLabel;
       
   162       }
       
   163       public Label getTypeImageLabel() {
       
   164         return containerTypeIconLabel;
       
   165       }
       
   166       public Label getContainerNameLabel() {
       
   167         return containerNameLabel;
       
   168       }
       
   169       public void layout() {
       
   170         statusBox.layout();
       
   171       }
       
   172       public void setEnabled(boolean enabled) {
       
   173         statusLabel.setEnabled(enabled);
       
   174         containerTypeIconLabel.setEnabled(enabled);
       
   175         containerNameLabel.setEnabled(enabled);
       
   176       }
       
   177     };
       
   178 
       
   179     Elements elements = new Elements() {
       
   180       public Text getPrefixField() {
       
   181         return prefixEditor;
       
   182       }
       
   183       public Label getPrefixExampleLine1Label() {
       
   184         return prefixExampleLine1Label;
       
   185       }
       
   186       public Label getPrefixExampleLine2Label() {
       
   187         return prefixExampleLine2Label;
       
   188       }
       
   189       public Button getConfigureButton() {
       
   190         return configureButton;
       
   191       }
       
   192       public ComboWrapper<ISourceContainerType> getContainerTypeCombo() {
       
   193         return comboWrapper;
       
   194       }
       
   195       public Shell getShell() {
       
   196         return SourceNameMapperContainerDialog.this.getShell();
       
   197       }
       
   198       public ContainerStatusGroup getContainerStatusGroup() {
       
   199         return containerStatusGroup;
       
   200       }
       
   201       public Button getOkButton() {
       
   202         return SourceNameMapperContainerDialog.this.getButton(IDialogConstants.OK_ID);
       
   203       }
       
   204       public void setMessage(String message, int type) {
       
   205         SourceNameMapperContainerDialog.this.setMessage(message, type);
       
   206       }
       
   207     };
       
   208 
       
   209     logic = SourceNameMapperContainerDialogLogic.create(elements, director, initialParams);
       
   210 
       
   211     return parent;
       
   212   }
       
   213 
       
   214   @Override
       
   215   public void create() {
       
   216     super.create();
       
   217     logic.updateAll();
       
   218   }
       
   219 
       
   220   @Override
       
   221   protected void okPressed() {
       
   222     result = logic.getResult();
       
   223     super.okPressed();
       
   224   }
       
   225 
       
   226   /**
       
   227    * A main interface to dialog elements, that are used from logic engine.
       
   228    */
       
   229   interface Elements extends DialogElements {
       
   230     Text getPrefixField();
       
   231     Label getPrefixExampleLine1Label();
       
   232     Label getPrefixExampleLine2Label();
       
   233     ComboWrapper<ISourceContainerType> getContainerTypeCombo();
       
   234     Button getConfigureButton();
       
   235     ContainerStatusGroup getContainerStatusGroup();
       
   236   }
       
   237 
       
   238   interface ContainerStatusGroup {
       
   239     Label getStatusLabel();
       
   240     Label getTypeImageLabel();
       
   241     Label getContainerNameLabel();
       
   242     void layout();
       
   243     void setEnabled(boolean enabled);
       
   244   }
       
   245 
       
   246   interface ConfigureButtonAction {
       
   247     ISourceContainer run(Shell shell);
       
   248   }
       
   249 
       
   250   // Creates action implementation for a configure button or return null.
       
   251   static ConfigureButtonAction prepareConfigureAction(ISourceContainerType type,
       
   252       ISourceContainer alreadyCreatedContainer,
       
   253       final ISourceLookupDirector director) {
       
   254     if (type == null) {
       
   255       return null;
       
   256     }
       
   257     final ISourceContainerBrowser browser = DebugUITools.getSourceContainerBrowser(type.getId());
       
   258     if (browser == null) {
       
   259       return null;
       
   260     }
       
   261     abstract class ActionBase implements ConfigureButtonAction {
       
   262       public ISourceContainer run(Shell shell) {
       
   263         ISourceContainer[] containers = runImpl(shell);
       
   264         if (containers.length != 1) {
       
   265           return null;
       
   266         }
       
   267         return containers[0];
       
   268       }
       
   269       abstract ISourceContainer[] runImpl(Shell shell);
       
   270     }
       
   271     ISourceContainer[] containers;
       
   272     if (alreadyCreatedContainer != null && alreadyCreatedContainer.getType().equals(type)) {
       
   273       // Edit existing.
       
   274       final ISourceContainer[] alreadyCreatedContainerArray = { alreadyCreatedContainer };
       
   275       if (browser.canEditSourceContainers(director, alreadyCreatedContainerArray)) {
       
   276         return new ActionBase() {
       
   277           @Override ISourceContainer[] runImpl(Shell shell) {
       
   278             return browser.editSourceContainers(shell, director, alreadyCreatedContainerArray);
       
   279           }
       
   280         };
       
   281       }
       
   282     }
       
   283     // Add new.
       
   284     if (browser.canAddSourceContainers(director)) {
       
   285       return new ActionBase() {
       
   286         @Override ISourceContainer[] runImpl(Shell shell) {
       
   287           return browser.addSourceContainers(shell, director);
       
   288         }
       
   289       };
       
   290     }
       
   291     return null;
       
   292   }
       
   293 
       
   294   private static final Comparator<ISourceContainerType> TYPE_COMPARATOR_BY_NAME =
       
   295       new Comparator<ISourceContainerType>() {
       
   296     public int compare(ISourceContainerType o1, ISourceContainerType o2) {
       
   297       return o1.getName().compareTo(o2.getName());
       
   298     }
       
   299   };
       
   300 
       
   301   private List<ISourceContainerType> filterTypes(ISourceContainerType[] types){
       
   302     ArrayList<ISourceContainerType> result = new ArrayList<ISourceContainerType>();
       
   303     for (int i = 0; i< types.length; i++) {
       
   304       ISourceContainerType type = types[i];
       
   305       if (director.supportsSourceContainerType(type)) {
       
   306         ISourceContainerBrowser sourceContainerBrowser =
       
   307             DebugUITools.getSourceContainerBrowser(type.getId());
       
   308         if(sourceContainerBrowser != null &&
       
   309             sourceContainerBrowser.canAddSourceContainers(director)) {
       
   310           result.add(type);
       
   311         }
       
   312       }
       
   313     }
       
   314     return result;
       
   315   }
       
   316 }