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 org.eclipse.swt.SWT;
     14 import org.eclipse.swt.events.KeyAdapter;
     15 import org.eclipse.swt.events.KeyEvent;
     16 import org.eclipse.swt.widgets.Composite;
     17 import org.eclipse.swt.widgets.Control;
     18 import org.eclipse.swt.widgets.Label;
     19 import org.eclipse.swt.widgets.Shell;
     20 import org.eclipse.swt.widgets.Text;
     21 import org.eclipse.ui.plugin.AbstractUIPlugin;
     22 import org.eclipse.wb.internal.core.utils.ui.GridDataFactory;
     23 import org.eclipse.wb.internal.core.utils.ui.GridLayoutFactory;
     24 
     25 /**
     26  * The dialog for editing multiline text.
     27  *
     28  * @author scheglov_ke
     29  * @coverage core.ui
     30  */
     31 public class TextDialog extends ResizableDialog {
     32   private final String m_titleText;
     33   private final String m_headerText;
     34   private final String m_footerText;
     35 
     36   ////////////////////////////////////////////////////////////////////////////
     37   //
     38   // Constructor
     39   //
     40   ////////////////////////////////////////////////////////////////////////////
     41   public TextDialog(Shell parentShell,
     42       AbstractUIPlugin plugin,
     43       String titleText,
     44       String headerText,
     45       String footerText) {
     46     super(parentShell, plugin);
     47     m_titleText = titleText;
     48     m_headerText = headerText;
     49     m_footerText = footerText;
     50   }
     51 
     52   ////////////////////////////////////////////////////////////////////////////
     53   //
     54   // Text
     55   //
     56   ////////////////////////////////////////////////////////////////////////////
     57   private String m_text;
     58 
     59   /**
     60    * Sets the text to edit.
     61    */
     62   public final void setText(String text) {
     63     m_text = text;
     64   }
     65 
     66   /**
     67    * @return the edited text.
     68    */
     69   public final String getText() {
     70     return m_text;
     71   }
     72 
     73   ////////////////////////////////////////////////////////////////////////////
     74   //
     75   // GUI
     76   //
     77   ////////////////////////////////////////////////////////////////////////////
     78   protected Text m_textWidget;
     79 
     80   @Override
     81   protected Control createDialogArea(Composite parent) {
     82     Composite area = (Composite) super.createDialogArea(parent);
     83     GridLayoutFactory.create(area);
     84     // header
     85     new Label(area, SWT.NONE).setText(m_headerText);
     86     // Text widget
     87     {
     88       m_textWidget = new Text(area, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
     89       GridDataFactory.create(m_textWidget).grab().fill().hintVC(10);
     90       m_textWidget.setText(m_text);
     91       // handle Ctrl+Enter as OK
     92       m_textWidget.addKeyListener(new KeyAdapter() {
     93         @Override
     94         public void keyPressed(KeyEvent e) {
     95           if (e.stateMask == SWT.CTRL && e.keyCode == SWT.CR) {
     96             okPressed();
     97           }
     98         }
     99       });
    100     }
    101     // footer
    102     new Label(area, SWT.NONE).setText(m_footerText);
    103     //
    104     return area;
    105   }
    106 
    107   @Override
    108   protected void configureShell(Shell newShell) {
    109     super.configureShell(newShell);
    110     newShell.setText(m_titleText);
    111   }
    112 
    113   @Override
    114   protected void okPressed() {
    115     m_text = m_textWidget.getText();
    116     super.okPressed();
    117   }
    118 }
    119