Commit | Line | Data |
---|---|---|
339d539c PT |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 Ericsson | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Patrick Tasse - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ui.project.wizards; | |
14 | ||
15 | import org.eclipse.core.resources.IContainer; | |
16 | import org.eclipse.core.resources.IResource; | |
17 | import org.eclipse.core.resources.IWorkspace; | |
18 | import org.eclipse.core.runtime.IStatus; | |
19 | import org.eclipse.core.runtime.Status; | |
20 | import org.eclipse.jface.dialogs.IDialogConstants; | |
21 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; | |
22 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; | |
23 | import org.eclipse.swt.SWT; | |
24 | import org.eclipse.swt.graphics.Font; | |
25 | import org.eclipse.swt.layout.GridData; | |
26 | import org.eclipse.swt.layout.GridLayout; | |
27 | import org.eclipse.swt.widgets.Composite; | |
28 | import org.eclipse.swt.widgets.Control; | |
29 | import org.eclipse.swt.widgets.Event; | |
30 | import org.eclipse.swt.widgets.Label; | |
31 | import org.eclipse.swt.widgets.Listener; | |
32 | import org.eclipse.swt.widgets.Shell; | |
33 | import org.eclipse.swt.widgets.Text; | |
34 | import org.eclipse.ui.dialogs.SelectionStatusDialog; | |
35 | ||
36 | /** | |
37 | * Implementation of a dialog box to rename a folder. | |
38 | * @since 3.0 | |
39 | */ | |
40 | public class RenameFolderDialog extends SelectionStatusDialog { | |
41 | ||
42 | // ------------------------------------------------------------------------ | |
43 | // Members | |
44 | // ------------------------------------------------------------------------ | |
45 | ||
46 | private final TmfTraceFolder fFolder; | |
47 | private Text fNewFolderNameText; | |
48 | ||
49 | // ------------------------------------------------------------------------ | |
50 | // Constructor | |
51 | // ------------------------------------------------------------------------ | |
52 | ||
53 | /** | |
54 | * Constructor | |
55 | * @param shell The parent shell | |
56 | * @param folder The trace element to rename | |
57 | */ | |
58 | public RenameFolderDialog(Shell shell, TmfTraceFolder folder) { | |
59 | super(shell); | |
60 | fFolder = folder; | |
61 | setTitle(Messages.RenameFolderDialog_DialogTitle); | |
62 | setStatusLineAboveButtons(true); | |
63 | } | |
64 | ||
65 | // ------------------------------------------------------------------------ | |
66 | // Dialog | |
67 | // ------------------------------------------------------------------------ | |
68 | ||
69 | @Override | |
70 | protected Control createDialogArea(Composite parent) { | |
71 | Composite composite = (Composite) super.createDialogArea(parent); | |
72 | composite.setLayout(new GridLayout()); | |
73 | composite.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
74 | ||
75 | createNewTraceNameGroup(composite); | |
76 | return composite; | |
77 | } | |
78 | ||
79 | private void createNewTraceNameGroup(Composite parent) { | |
80 | Font font = parent.getFont(); | |
81 | Composite folderGroup = new Composite(parent, SWT.NONE); | |
82 | GridLayout layout = new GridLayout(); | |
83 | layout.numColumns = 2; | |
84 | folderGroup.setLayout(layout); | |
85 | folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
86 | ||
87 | // Old trace name label | |
88 | Label oldTraceLabel = new Label(folderGroup, SWT.NONE); | |
89 | oldTraceLabel.setFont(font); | |
90 | oldTraceLabel.setText(Messages.RenameFolderDialog_FolderName); | |
91 | ||
92 | // Old trace name field | |
93 | Text oldTraceName = new Text(folderGroup, SWT.BORDER); | |
94 | GridData data = new GridData(GridData.FILL_HORIZONTAL); | |
95 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; | |
96 | oldTraceName.setLayoutData(data); | |
97 | oldTraceName.setFont(font); | |
98 | oldTraceName.setText(fFolder.getName()); | |
99 | oldTraceName.setEnabled(false); | |
100 | ||
101 | // New trace name label | |
102 | Label newTaceLabel = new Label(folderGroup, SWT.NONE); | |
103 | newTaceLabel.setFont(font); | |
104 | newTaceLabel.setText(Messages.RenameFolderDialog_FolderNewName); | |
105 | ||
106 | // New trace name entry field | |
107 | fNewFolderNameText = new Text(folderGroup, SWT.BORDER); | |
108 | data = new GridData(GridData.FILL_HORIZONTAL); | |
109 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; | |
110 | fNewFolderNameText.setLayoutData(data); | |
111 | fNewFolderNameText.setFont(font); | |
112 | fNewFolderNameText.addListener(SWT.Modify, new Listener() { | |
113 | @Override | |
114 | public void handleEvent(Event event) { | |
115 | validateNewFolderName(); | |
116 | } | |
117 | }); | |
118 | } | |
119 | ||
120 | private void validateNewFolderName() { | |
121 | ||
122 | String newFolderName = fNewFolderNameText.getText(); | |
123 | IWorkspace workspace = fFolder.getResource().getWorkspace(); | |
124 | IStatus nameStatus = workspace.validateName(newFolderName, IResource.FOLDER); | |
125 | ||
126 | if ("".equals(newFolderName)) { //$NON-NLS-1$ | |
127 | updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, | |
128 | Messages.Dialog_EmptyNameError, null)); | |
129 | return; | |
130 | } | |
131 | ||
132 | if (!nameStatus.isOK()) { | |
133 | updateStatus(nameStatus); | |
134 | return; | |
135 | } | |
136 | ||
137 | IContainer parentFolder = fFolder.getResource().getParent(); | |
138 | if (parentFolder.findMember(newFolderName) != null) { | |
139 | updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, | |
140 | Messages.Dialog_ExistingNameError, null)); | |
141 | return; | |
142 | } | |
143 | ||
144 | updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$ | |
145 | } | |
146 | ||
147 | // ------------------------------------------------------------------------ | |
148 | // SelectionStatusDialog | |
149 | // ------------------------------------------------------------------------ | |
150 | ||
151 | @Override | |
152 | protected void computeResult() { | |
153 | } | |
154 | ||
155 | @Override | |
156 | public void create() { | |
157 | super.create(); | |
158 | getButton(IDialogConstants.OK_ID).setEnabled(false); | |
159 | } | |
160 | ||
161 | @Override | |
162 | protected void okPressed() { | |
163 | setSelectionResult(new String[] { fNewFolderNameText.getText() }); | |
164 | super.okPressed(); | |
165 | } | |
166 | ||
167 | } |