Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ceb56c8192271f287839fb08eb2bee981c09bcd6 (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
/*
 * Copyright (c) 2015 Eike Stepper (Berlin, Germany) and others.
 * 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.explorer.ui;

import org.eclipse.emf.cdo.common.util.CDORenameContext;
import org.eclipse.emf.cdo.explorer.ui.bundle.OM;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * @author Eike Stepper
 */
public class RenameDialog extends TitleAreaDialog
{
  private final CDORenameContext renameContext;

  private String name;

  private Text nameText;

  public RenameDialog(Shell parentShell, CDORenameContext renameContext)
  {
    super(parentShell);
    this.renameContext = renameContext;
    name = renameContext.getName();

    setShellStyle(SWT.CLOSE | SWT.RESIZE | SWT.TITLE | SWT.APPLICATION_MODAL);
  }

  public String getName()
  {
    return name;
  }

  @Override
  protected Control createDialogArea(Composite parent)
  {
    String title = "Rename " + renameContext.getType();
    getShell().setText(title);
    setTitle(title);
    setTitleImage(OM.getImage("icons/wiz/rename.gif"));
    setMessage("Enter the new " + getAttributeName() + " of the " + renameContext.getType().toLowerCase() + ".");

    Composite area = (Composite)super.createDialogArea(parent);
    Composite container = new Composite(area, SWT.NONE);
    container.setLayoutData(new GridData(GridData.FILL_BOTH));
    GridLayout containerGridLayout = new GridLayout(2, false);
    containerGridLayout.marginWidth = 10;
    containerGridLayout.marginHeight = 10;
    container.setLayout(containerGridLayout);

    Label oldLabel = new Label(container, SWT.NONE);
    oldLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
    oldLabel.setText("Current " + getAttributeName() + ":");

    Text oldNameText = new Text(container, SWT.BORDER | SWT.READ_ONLY);
    oldNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    oldNameText.setText(name);

    Label newLabel = new Label(container, SWT.NONE);
    newLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
    newLabel.setText("New " + getAttributeName() + ":");

    nameText = new Text(container, SWT.BORDER);
    nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    nameText.setText(name);
    nameText.setFocus();
    nameText.addModifyListener(new ModifyListener()
    {
      public void modifyText(ModifyEvent e)
      {
        name = nameText.getText();

        String error = renameContext.validateName(name);
        setErrorMessage(error);

        Button button = getButton(IDialogConstants.OK_ID);
        if (button != null)
        {
          button.setEnabled(error == null);
        }
      }
    });

    int lastDot = name.lastIndexOf('.');
    if (lastDot != -1)
    {
      nameText.setSelection(0, lastDot);
    }
    else
    {
      nameText.selectAll();
    }

    return area;
  }

  protected String getAttributeName()
  {
    return "name";
  }

  @Override
  protected Point getInitialSize()
  {
    return new Point(450, 250);
  }
}

Back to the top