Commit | Line | Data |
---|---|---|
12c155f5 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2014 Ericsson |
6256d8ad | 3 | * |
12c155f5 FC |
4 | * All rights reserved. This program and the accompanying materials are |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
6256d8ad | 8 | * |
12c155f5 FC |
9 | * Contributors: |
10 | * Francois Chouinard - Copied and adapted from NewFolderDialog | |
a72a6830 | 11 | * Patrick Tasse - Close editors to release resources |
12c155f5 FC |
12 | *******************************************************************************/ |
13 | ||
14 | package org.eclipse.linuxtools.tmf.ui.project.wizards; | |
15 | ||
12c155f5 FC |
16 | import org.eclipse.core.resources.IContainer; |
17 | import org.eclipse.core.resources.IResource; | |
18 | import org.eclipse.core.resources.IWorkspace; | |
12c155f5 | 19 | import org.eclipse.core.runtime.IStatus; |
12c155f5 FC |
20 | import org.eclipse.core.runtime.Status; |
21 | import org.eclipse.jface.dialogs.IDialogConstants; | |
8fd82db5 | 22 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
12c155f5 | 23 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; |
12c155f5 FC |
24 | import org.eclipse.swt.SWT; |
25 | import org.eclipse.swt.graphics.Font; | |
26 | import org.eclipse.swt.layout.GridData; | |
27 | import org.eclipse.swt.layout.GridLayout; | |
28 | import org.eclipse.swt.widgets.Composite; | |
29 | import org.eclipse.swt.widgets.Control; | |
30 | import org.eclipse.swt.widgets.Event; | |
31 | import org.eclipse.swt.widgets.Label; | |
32 | import org.eclipse.swt.widgets.Listener; | |
33 | import org.eclipse.swt.widgets.Shell; | |
34 | import org.eclipse.swt.widgets.Text; | |
12c155f5 FC |
35 | import org.eclipse.ui.dialogs.SelectionStatusDialog; |
36 | ||
37 | /** | |
b544077e | 38 | * Implementation of a dialog box to rename a trace. |
6256d8ad | 39 | * <p> |
b544077e BH |
40 | * @version 1.0 |
41 | * @author Francois Chouinard | |
12c155f5 FC |
42 | */ |
43 | public class RenameTraceDialog extends SelectionStatusDialog { | |
44 | ||
45 | // ------------------------------------------------------------------------ | |
46 | // Members | |
47 | // ------------------------------------------------------------------------ | |
48 | ||
49 | private final TmfTraceElement fTrace; | |
50 | private Text fNewTraceNameText; | |
12c155f5 FC |
51 | |
52 | // ------------------------------------------------------------------------ | |
53 | // Constructor | |
54 | // ------------------------------------------------------------------------ | |
b544077e BH |
55 | /** |
56 | * Constructor | |
57 | * @param shell The parent shell | |
58 | * @param trace The trace element to rename | |
59 | */ | |
12c155f5 FC |
60 | public RenameTraceDialog(Shell shell, TmfTraceElement trace) { |
61 | super(shell); | |
62 | fTrace = trace; | |
12c155f5 FC |
63 | setTitle(Messages.RenameTraceDialog_DialogTitle); |
64 | setStatusLineAboveButtons(true); | |
65 | } | |
66 | ||
67 | // ------------------------------------------------------------------------ | |
68 | // Dialog | |
69 | // ------------------------------------------------------------------------ | |
11252342 | 70 | |
12c155f5 FC |
71 | @Override |
72 | protected Control createDialogArea(Composite parent) { | |
73 | Composite composite = (Composite) super.createDialogArea(parent); | |
74 | composite.setLayout(new GridLayout()); | |
75 | composite.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
76 | ||
77 | createNewTraceNameGroup(composite); | |
78 | return composite; | |
79 | } | |
80 | ||
81 | private void createNewTraceNameGroup(Composite parent) { | |
82 | Font font = parent.getFont(); | |
83 | Composite folderGroup = new Composite(parent, SWT.NONE); | |
84 | GridLayout layout = new GridLayout(); | |
85 | layout.numColumns = 2; | |
86 | folderGroup.setLayout(layout); | |
87 | folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
88 | ||
89 | // Old trace name label | |
90 | Label oldTraceLabel = new Label(folderGroup, SWT.NONE); | |
91 | oldTraceLabel.setFont(font); | |
92 | oldTraceLabel.setText(Messages.RenameTraceDialog_TraceName); | |
93 | ||
94 | // Old trace name field | |
95 | Text oldTraceName = new Text(folderGroup, SWT.BORDER); | |
96 | GridData data = new GridData(GridData.FILL_HORIZONTAL); | |
97 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; | |
98 | oldTraceName.setLayoutData(data); | |
99 | oldTraceName.setFont(font); | |
100 | oldTraceName.setText(fTrace.getName()); | |
101 | oldTraceName.setEnabled(false); | |
102 | ||
103 | // New trace name label | |
104 | Label newTaceLabel = new Label(folderGroup, SWT.NONE); | |
105 | newTaceLabel.setFont(font); | |
106 | newTaceLabel.setText(Messages.RenameTraceDialog_TraceNewName); | |
107 | ||
108 | // New trace name entry field | |
109 | fNewTraceNameText = new Text(folderGroup, SWT.BORDER); | |
110 | data = new GridData(GridData.FILL_HORIZONTAL); | |
111 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; | |
112 | fNewTraceNameText.setLayoutData(data); | |
113 | fNewTraceNameText.setFont(font); | |
114 | fNewTraceNameText.addListener(SWT.Modify, new Listener() { | |
115 | @Override | |
116 | public void handleEvent(Event event) { | |
117 | validateNewTraceName(); | |
118 | } | |
119 | }); | |
120 | } | |
121 | ||
12c155f5 FC |
122 | private void validateNewTraceName() { |
123 | ||
339d539c PT |
124 | String newTraceName = fNewTraceNameText.getText(); |
125 | IWorkspace workspace = fTrace.getResource().getWorkspace(); | |
126 | IStatus nameStatus = workspace.validateName(newTraceName, IResource.FOLDER); | |
12c155f5 | 127 | |
339d539c | 128 | if ("".equals(newTraceName)) { //$NON-NLS-1$ |
8fd82db5 | 129 | updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, |
12c155f5 FC |
130 | Messages.Dialog_EmptyNameError, null)); |
131 | return; | |
132 | } | |
133 | ||
9fa32496 | 134 | if (!nameStatus.isOK()) { |
12c155f5 FC |
135 | updateStatus(nameStatus); |
136 | return; | |
137 | } | |
138 | ||
339d539c PT |
139 | IContainer parentFolder = fTrace.getResource().getParent(); |
140 | if (parentFolder.findMember(newTraceName) != null) { | |
8fd82db5 | 141 | updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, |
12c155f5 FC |
142 | Messages.Dialog_ExistingNameError, null)); |
143 | return; | |
144 | } | |
145 | ||
8fd82db5 | 146 | updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$ |
12c155f5 FC |
147 | } |
148 | ||
149 | // ------------------------------------------------------------------------ | |
150 | // SelectionStatusDialog | |
151 | // ------------------------------------------------------------------------ | |
11252342 | 152 | |
12c155f5 FC |
153 | @Override |
154 | protected void computeResult() { | |
155 | } | |
11252342 | 156 | |
12c155f5 FC |
157 | @Override |
158 | public void create() { | |
159 | super.create(); | |
160 | getButton(IDialogConstants.OK_ID).setEnabled(false); | |
161 | } | |
11252342 | 162 | |
12c155f5 FC |
163 | @Override |
164 | protected void okPressed() { | |
339d539c | 165 | setSelectionResult(new String[] { fNewTraceNameText.getText() }); |
12c155f5 | 166 | super.okPressed(); |
12c155f5 FC |
167 | } |
168 | ||
12c155f5 | 169 | } |