Home | History | Annotate | Download | only in string
      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.model.property.editor.string;
     12 
     13 import org.eclipse.jface.dialogs.Dialog;
     14 import org.eclipse.swt.SWT;
     15 import org.eclipse.swt.events.KeyAdapter;
     16 import org.eclipse.swt.events.KeyEvent;
     17 import org.eclipse.swt.widgets.Composite;
     18 import org.eclipse.swt.widgets.Control;
     19 import org.eclipse.swt.widgets.Shell;
     20 import org.eclipse.swt.widgets.Text;
     21 import org.eclipse.wb.internal.core.DesignerPlugin;
     22 import org.eclipse.wb.internal.core.model.ModelMessages;
     23 import org.eclipse.wb.internal.core.model.property.Property;
     24 import org.eclipse.wb.internal.core.utils.execution.ExecutionUtils;
     25 import org.eclipse.wb.internal.core.utils.execution.RunnableEx;
     26 import org.eclipse.wb.internal.core.utils.ui.GridDataFactory;
     27 import org.eclipse.wb.internal.core.utils.ui.dialogs.ResizableDialog;
     28 
     29 /**
     30  * {@link Dialog} for editing value in {@link StringPropertyEditor}.
     31  *
     32  * @author scheglov_ke
     33  * @coverage core.model.property.editor
     34  */
     35 public class StringPropertyDialog extends ResizableDialog {
     36     // NOTE: In WindowBuilder this class had a lot of support for
     37     // editing Java strings, dealing with automatic localization
     38     // etc. This will need to be done differently in ADT (and had hooks
     39     // into a bunch of other parts of WindowBuilder we're not including)
     40     // so this was all stripped down to a plain String editor.
     41 
     42   ////////////////////////////////////////////////////////////////////////////
     43   //
     44   // Final fields
     45   //
     46   ////////////////////////////////////////////////////////////////////////////
     47   private final Property m_property;
     48 
     49   ////////////////////////////////////////////////////////////////////////////
     50   //
     51   // Constructor
     52   //
     53   ////////////////////////////////////////////////////////////////////////////
     54   public StringPropertyDialog(Shell parentShell, Property property) throws Exception {
     55     super(parentShell, DesignerPlugin.getDefault());
     56     m_property = property;
     57   }
     58 
     59   ////////////////////////////////////////////////////////////////////////////
     60   //
     61   // GUI
     62   //
     63   ////////////////////////////////////////////////////////////////////////////
     64   private Text m_valueText;
     65 
     66   @Override
     67   public void create() {
     68     super.create();
     69     m_valueText.selectAll();
     70   }
     71 
     72   @Override
     73   protected Control createDialogArea(Composite parent) {
     74     Composite area = (Composite) super.createDialogArea(parent);
     75     // value
     76     {
     77       // BEGIN ADT MODIFICATIONS
     78       if (isMultiLine()) {
     79       // END ADT MODIFICATIONS
     80         m_valueText = new Text(area, SWT.BORDER | SWT.MULTI | SWT.WRAP);
     81         GridDataFactory.create(m_valueText).grab().hintC(80, 8).fill();
     82       // BEGIN ADT MODIFICATIONS
     83       } else {
     84         m_valueText = new Text(area, SWT.BORDER | SWT.SINGLE);
     85         GridDataFactory.create(m_valueText).grab().hintC(50, 1).fill();
     86       }
     87       // END ADT MODIFICATIONS
     88       // initial value
     89       ExecutionUtils.runLog(new RunnableEx() {
     90         @Override
     91         public void run() throws Exception {
     92           Object value = m_property.getValue();
     93           if (value instanceof String) {
     94             m_valueText.setText((String) value);
     95           }
     96         }
     97       });
     98       // handle Ctrl+Enter as OK
     99       m_valueText.addKeyListener(new KeyAdapter() {
    100         @Override
    101         public void keyPressed(KeyEvent e) {
    102           if (e.stateMask == SWT.CTRL && e.keyCode == SWT.CR) {
    103             okPressed();
    104           }
    105         }
    106       });
    107     }
    108 
    109     return area;
    110   }
    111 
    112   // BEGIN ADT MODIFICATIONS
    113   protected boolean isMultiLine() {
    114       return true;
    115   }
    116   // END ADT MODIFICATIONS
    117 
    118   ////////////////////////////////////////////////////////////////////////////
    119   //
    120   // Shell
    121   //
    122   ////////////////////////////////////////////////////////////////////////////
    123   @Override
    124   protected void configureShell(Shell newShell) {
    125     super.configureShell(newShell);
    126     newShell.setText(ModelMessages.StringPropertyDialog_title);
    127   }
    128 
    129   @Override
    130   protected void okPressed() {
    131     final String value = m_valueText.getText();
    132     ExecutionUtils.runLog(new RunnableEx() {
    133         @Override
    134         public void run() throws Exception {
    135           m_property.setValue(value);
    136         }
    137     });
    138     // close dialog
    139     super.okPressed();
    140   }
    141 }
    142