summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/string/StringPropertyDialog.java
blob: 2b0d47e4db2a6540245467725bcd6fc4b47f868e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.wb.internal.core.model.property.editor.string;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.internal.core.DesignerPlugin;
import org.eclipse.wb.internal.core.model.ModelMessages;
import org.eclipse.wb.internal.core.model.property.Property;
import org.eclipse.wb.internal.core.utils.execution.ExecutionUtils;
import org.eclipse.wb.internal.core.utils.execution.RunnableEx;
import org.eclipse.wb.internal.core.utils.ui.GridDataFactory;
import org.eclipse.wb.internal.core.utils.ui.dialogs.ResizableDialog;

/**
 * {@link Dialog} for editing value in {@link StringPropertyEditor}.
 *
 * @author scheglov_ke
 * @coverage core.model.property.editor
 */
public class StringPropertyDialog extends ResizableDialog {
    // NOTE: In WindowBuilder this class had a lot of support for
    // editing Java strings, dealing with automatic localization
    // etc. This will need to be done differently in ADT (and had hooks
    // into a bunch of other parts of WindowBuilder we're not including)
    // so this was all stripped down to a plain String editor.

  ////////////////////////////////////////////////////////////////////////////
  //
  // Final fields
  //
  ////////////////////////////////////////////////////////////////////////////
  private final Property m_property;

  ////////////////////////////////////////////////////////////////////////////
  //
  // Constructor
  //
  ////////////////////////////////////////////////////////////////////////////
  public StringPropertyDialog(Shell parentShell, Property property) throws Exception {
    super(parentShell, DesignerPlugin.getDefault());
    m_property = property;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // GUI
  //
  ////////////////////////////////////////////////////////////////////////////
  private Text m_valueText;

  @Override
  public void create() {
    super.create();
    m_valueText.selectAll();
  }

  @Override
  protected Control createDialogArea(Composite parent) {
    Composite area = (Composite) super.createDialogArea(parent);
    // value
    {
      // BEGIN ADT MODIFICATIONS
      if (isMultiLine()) {
      // END ADT MODIFICATIONS
        m_valueText = new Text(area, SWT.BORDER | SWT.MULTI | SWT.WRAP);
        GridDataFactory.create(m_valueText).grab().hintC(80, 8).fill();
      // BEGIN ADT MODIFICATIONS
      } else {
        m_valueText = new Text(area, SWT.BORDER | SWT.SINGLE);
        GridDataFactory.create(m_valueText).grab().hintC(50, 1).fill();
      }
      // END ADT MODIFICATIONS
      // initial value
      ExecutionUtils.runLog(new RunnableEx() {
        @Override
        public void run() throws Exception {
          Object value = m_property.getValue();
          if (value instanceof String) {
            m_valueText.setText((String) value);
          }
        }
      });
      // handle Ctrl+Enter as OK
      m_valueText.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
          if (e.stateMask == SWT.CTRL && e.keyCode == SWT.CR) {
            okPressed();
          }
        }
      });
    }

    return area;
  }

  // BEGIN ADT MODIFICATIONS
  protected boolean isMultiLine() {
      return true;
  }
  // END ADT MODIFICATIONS

  ////////////////////////////////////////////////////////////////////////////
  //
  // Shell
  //
  ////////////////////////////////////////////////////////////////////////////
  @Override
  protected void configureShell(Shell newShell) {
    super.configureShell(newShell);
    newShell.setText(ModelMessages.StringPropertyDialog_title);
  }

  @Override
  protected void okPressed() {
    final String value = m_valueText.getText();
    ExecutionUtils.runLog(new RunnableEx() {
        @Override
        public void run() throws Exception {
          m_property.setValue(value);
        }
    });
    // close dialog
    super.okPressed();
  }
}