Home | History | Annotate | Download | only in dialogs
      1 /*******************************************************************************
      2  * Copyright (c) 2011 Google, Inc.
      3  * All rights reserved. This program and the accompanying materials
      4  * are made available under the terms of the Eclipse Public License v1.0
      5  * which accompanies this distribution, and is available at
      6  * http://www.eclipse.org/legal/epl-v10.html
      7  *
      8  * Contributors:
      9  *    Google, Inc. - initial API and implementation
     10  *******************************************************************************/
     11 package org.eclipse.wb.internal.core.utils.ui.dialogs;
     12 
     13 import com.google.common.base.Joiner;
     14 import com.google.common.collect.Lists;
     15 
     16 import org.eclipse.swt.widgets.Shell;
     17 import org.eclipse.ui.plugin.AbstractUIPlugin;
     18 import org.eclipse.wb.internal.core.utils.execution.ExecutionUtils;
     19 import org.eclipse.wb.internal.core.utils.execution.RunnableObjectEx;
     20 
     21 import java.io.BufferedReader;
     22 import java.io.StringReader;
     23 import java.util.List;
     24 
     25 /**
     26  * The dialog for editing array of {@link String}'s.
     27  *
     28  * @author scheglov_ke
     29  * @coverage core.ui
     30  */
     31 public class StringsDialog extends TextDialog {
     32   ////////////////////////////////////////////////////////////////////////////
     33   //
     34   // Constructor
     35   //
     36   ////////////////////////////////////////////////////////////////////////////
     37   public StringsDialog(Shell parentShell,
     38       AbstractUIPlugin plugin,
     39       String titleText,
     40       String headerText,
     41       String footerText) {
     42     super(parentShell, plugin, titleText, headerText, footerText);
     43   }
     44 
     45   ////////////////////////////////////////////////////////////////////////////
     46   //
     47   // Items
     48   //
     49   ////////////////////////////////////////////////////////////////////////////
     50   /**
     51    * Sets the items to edit.
     52    */
     53   public void setItems(String[] items) {
     54     setText(Joiner.on('\n').join(items));
     55   }
     56 
     57   /**
     58    * @return the edited items.
     59    */
     60   public String[] getItems() {
     61     return ExecutionUtils.runObjectLog(new RunnableObjectEx<String[]>() {
     62       @Override
     63     public String[] runObject() throws Exception {
     64         List<String> strings = Lists.newArrayList();
     65         BufferedReader br = new BufferedReader(new StringReader(getText()));
     66         while (true) {
     67           String s = br.readLine();
     68           if (s == null) {
     69             break;
     70           }
     71           strings.add(s);
     72         }
     73         return strings.toArray(new String[strings.size()]);
     74       }
     75     }, new String[0]);
     76   }
     77 }
     78