Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 764714e6cac9318ace72ee5efbe516c41c1150c6 (plain) (blame)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*******************************************************************************
 * Copyright (c) 2008-2010 Sonatype, 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:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.core.ui.dialogs;

// import org.eclipse.debug.ui.StringVariableSelectionDialog;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import org.eclipse.m2e.core.core.Messages;


public class MavenPropertyDialog extends Dialog {

  private final String title;
  
  private final String initialName;
  
  private final String initialValue;
  
  private final VerifyListener verifyListener;
  
  protected Text nameText;

  protected Text valueText;

  private String name;
  
  private String value;

  public MavenPropertyDialog(Shell shell, String title, String initialName, String initialValue, VerifyListener verifyListener) {
    super(shell);
    this.title = title;
    this.initialName = initialName;
    this.initialValue = initialValue;
    this.verifyListener = verifyListener;
  }

  /* (non-Javadoc)
   * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
   */
  protected Control createDialogArea(Composite parent) {
    Composite comp = new Composite(parent, SWT.NONE);
    GridLayout gridLayout = new GridLayout(2, false);
    gridLayout.marginTop = 7;
    gridLayout.marginWidth = 12;
    comp.setLayout(gridLayout);

    Label nameLabel = new Label(comp, SWT.NONE);
    nameLabel.setText(Messages.getString("launch.propertyDialog.name")); //$NON-NLS-1$;
    nameLabel.setFont(comp.getFont());

    nameText = new Text(comp, SWT.BORDER | SWT.SINGLE);
    GridData gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.widthHint = 300;
    nameText.setLayoutData(gd);
    nameText.setFont(comp.getFont());
    nameText.setText(initialName==null ? "" : initialName); //$NON-NLS-1$
    nameText.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        updateButtons();
      }
    });

    Label valueLabel = new Label(comp, SWT.NONE);
    valueLabel.setText(Messages.getString("launch.propertyDialog.value")); //$NON-NLS-1$;
    valueLabel.setFont(comp.getFont());

    valueText = new Text(comp, SWT.BORDER | SWT.SINGLE);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.widthHint = 300;
    valueText.setLayoutData(gd);
    valueText.setFont(comp.getFont());
    valueText.setText(initialValue==null ? "" : initialValue); //$NON-NLS-1$
    valueText.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        updateButtons();
      }
    });

//    if(variables) {
//      Button variablesButton = new Button(comp, SWT.PUSH);
//      variablesButton.setText(Messages.getString("launch.propertyDialog.browseVariables")); //$NON-NLS-1$;
//      gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
//      gd.horizontalSpan = 2;
//      int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
//      gd.widthHint = Math.max(widthHint, variablesButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
//      variablesButton.setLayoutData(gd);
//      variablesButton.setFont(comp.getFont());
//  
//      variablesButton.addSelectionListener(new SelectionAdapter() {
//        public void widgetSelected(SelectionEvent se) {
//          StringVariableSelectionDialog variablesDialog = new StringVariableSelectionDialog(getShell());
//          if(variablesDialog.open() == IDialogConstants.OK_ID) {
//            String variable = variablesDialog.getVariableExpression();
//            if(variable != null) {
//              valueText.insert(variable.trim());
//            }
//          }
//        }
//      });
//    }
    
    return comp;
  }

  public String getName() {
    return this.name;
  }
  
  public String getValue() {
    return this.value;
  }

  /* (non-Javadoc)
   * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
   */
  protected void buttonPressed(int buttonId) {
    if(buttonId == IDialogConstants.OK_ID) {
      name = nameText.getText();
      value = valueText.getText();
    } else {
      name = null;
      value = null;
    }
    super.buttonPressed(buttonId);
  }

  /* (non-Javadoc)
   * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
   */
  protected void configureShell(Shell shell) {
    super.configureShell(shell);
    if(title != null) {
      shell.setText(title);
    }
//    if (fInitialValues[0].length() == 0) {
//      PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, IAntUIHelpContextIds.ADD_PROPERTY_DIALOG);
//    } else {
//      PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, IAntUIHelpContextIds.EDIT_PROPERTY_DIALOG);
//    }
  }

  /**
   * Enable the OK button if valid input
   */
  protected void updateButtons() {
    String name = nameText.getText().trim();
    String value = valueText.getText().trim();
    // verify name
    Event e = new Event();
    e.widget = nameText;
    VerifyEvent ev = new VerifyEvent(e);
    ev.doit = true;
    if (verifyListener != null) {
      ev.text = name;
      verifyListener.verifyText(ev);
    }
    getButton(IDialogConstants.OK_ID).setEnabled((name.length() > 0) && (value.length() > 0) && ev.doit);
  }

  /**
   * Enable the buttons on creation.
   * 
   * @see org.eclipse.jface.window.Window#create()
   */
  public void create() {
    super.create();
    updateButtons();
  }
}

Back to the top