Skip to content

Commit df3d38b

Browse files
committed
Unify Maven configuration preference handling
and show all warnings in case multiple user-setting files cannot be read.
1 parent 3aa12f5 commit df3d38b

File tree

4 files changed

+140
-180
lines changed

4 files changed

+140
-180
lines changed

org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/Messages.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ public class Messages extends NLS {
557557

558558
public static String MavenRepositoryView_update_one;
559559

560-
public static String MavenSettingsPreferencePage_userSettingsBrowseButton_text;
560+
public static String MavenSettingsPreferencePage_settingsBrowseButton_text;
561561

562562
public static String MavenSettingsPreferencePage_btnUpdate;
563563

@@ -1025,8 +1025,6 @@ public class Messages extends NLS {
10251025

10261026
public static String MavenInstallationWizardPage_selectProjectTitle;
10271027

1028-
public static String MavenSettingsPreferencePage_globalSettingsBrowseButton_text;
1029-
10301028
public static String MavenProjectWizardArchetypeParametersPage_runInteractive;
10311029

10321030
static {

org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/messages.properties

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,12 @@ MavenRepositoryView_reload_msg=This will reload the settings.xml and rebuild the
309309
MavenRepositoryView_reload_title=Reload settings.xml
310310
MavenRepositoryView_update_more=Update Indexes
311311
MavenRepositoryView_update_one=Update Index
312-
MavenSettingsPreferencePage_userSettingsBrowseButton_text=Browse...
312+
MavenSettingsPreferencePage_settingsBrowseButton_text=Browse...
313313
MavenSettingsPreferencePage_btnUpdate=Update Settings
314314
MavenSettingsPreferencePage_error_globalSettingsMissing=Global settings file doesn't exist
315315
MavenSettingsPreferencePage_error_userSettingsMissing=User settings file doesn't exist
316316
MavenSettingsPreferencePage_error_globalSettingsParse=Unable to parse global settings file {0}
317317
MavenSettingsPreferencePage_error_userSettingsParse=Unable to parse user settings file {0}
318-
MavenSettingsPreferencePage_globalSettingsBrowseButton_text=Browse...
319318
MavenSettingsPreferencePage_job_indexing=Indexing Local Repository...
320319
MavenSettingsPreferencePage_job_updating=Updating Maven settings
321320
MavenSettingsPreferencePage_lblLocal=Local Repository (From merged user and global settings)\:

org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/preferences/MavenSettingsPreferencePage.java

Lines changed: 79 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
import java.util.ArrayList;
1818
import java.util.List;
1919
import java.util.Objects;
20+
import java.util.Optional;
21+
import java.util.function.Function;
22+
import java.util.function.Supplier;
23+
import java.util.stream.Stream;
2024

2125
import org.slf4j.Logger;
2226
import org.slf4j.LoggerFactory;
@@ -32,7 +36,6 @@
3236
import org.eclipse.jface.preference.PreferencePage;
3337
import org.eclipse.osgi.util.NLS;
3438
import org.eclipse.swt.SWT;
35-
import org.eclipse.swt.events.ModifyListener;
3639
import org.eclipse.swt.events.SelectionListener;
3740
import org.eclipse.swt.layout.GridData;
3841
import org.eclipse.swt.layout.GridLayout;
@@ -118,11 +121,9 @@ protected void updateSettings(boolean updateMavenDependencies) {
118121
String userSettings = getUserSettings();
119122
String globalSettings = getGlobalSettings();
120123

121-
String currentGlobalSettings = mavenConfiguration.getGlobalSettingsFile();
122-
String currentUserSettings = mavenConfiguration.getUserSettingsFile();
123-
124-
if(Objects.equals(globalSettings, currentGlobalSettings) && Objects.equals(currentUserSettings, userSettings)) {
125-
return;
124+
if(Objects.equals(globalSettings, mavenConfiguration.getGlobalSettingsFile())
125+
&& Objects.equals(userSettings, mavenConfiguration.getUserSettingsFile())) {
126+
return; // current preferences not changed
126127
}
127128

128129
Boolean[] updateProjects = new Boolean[1];
@@ -187,50 +188,21 @@ protected Control createContents(Composite parent) {
187188
Composite composite = new Composite(parent, SWT.NONE);
188189
composite.setLayout(new GridLayout(2, false));
189190

190-
globalSettingsLink = new Link(composite, SWT.NONE);
191-
globalSettingsLink.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
192-
globalSettingsLink.setText(Messages.MavenSettingsPreferencePage_globalSettingslink2);
193-
globalSettingsLink.setToolTipText(Messages.MavenSettingsPreferencePage_globalSettingslink_tooltip);
194-
globalSettingsLink.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
195-
String globalSettings = getGlobalSettings();
196-
if(globalSettings != null) {
197-
openEditor(globalSettings);
198-
}
199-
}));
191+
globalSettingsLink = createLink(composite, Messages.MavenSettingsPreferencePage_globalSettingslink2,
192+
Messages.MavenSettingsPreferencePage_globalSettingslink_tooltip, this::getGlobalSettings, null);
193+
globalSettingsText = createFileSelectionWidgets(composite, mavenConfiguration.getGlobalSettingsFile(), null);
200194

201-
globalSettingsText = new Text(composite, SWT.BORDER);
202-
globalSettingsText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
203-
204-
Button globalSettingsBrowseButton = new Button(composite, SWT.NONE);
205-
globalSettingsBrowseButton.setText(Messages.MavenSettingsPreferencePage_globalSettingsBrowseButton_text);
206-
globalSettingsBrowseButton
207-
.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> browseSettingsAction(globalSettingsText)));
208-
209-
userSettingsLink = new Link(composite, SWT.NONE);
210-
userSettingsLink.setText(Messages.MavenSettingsPreferencePage_userSettingslink2);
211-
userSettingsLink.setToolTipText(Messages.MavenSettingsPreferencePage_userSettingslink_tooltip);
212-
userSettingsLink.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
213-
userSettingsLink.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
214-
String userSettings = getUserSettings();
215-
if(userSettings == null) {
216-
userSettings = SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE.getAbsolutePath();
217-
}
218-
openEditor(userSettings);
219-
}));
220-
userSettingsText = new Text(composite, SWT.BORDER);
221-
userSettingsText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
222-
userSettingsText.setMessage(SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE.getAbsolutePath());
223-
224-
Button userSettingsBrowseButton = new Button(composite, SWT.NONE);
225-
userSettingsBrowseButton.setLayoutData(new GridData(SWT.FILL, SWT.RIGHT, false, false, 1, 1));
226-
userSettingsBrowseButton.setText(Messages.MavenSettingsPreferencePage_userSettingsBrowseButton_text);
227-
userSettingsBrowseButton
228-
.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> browseSettingsAction(userSettingsText)));
195+
userSettingsLink = createLink(composite, Messages.MavenSettingsPreferencePage_userSettingslink2,
196+
Messages.MavenSettingsPreferencePage_userSettingslink_tooltip, this::getUserSettings,
197+
SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE);
198+
userSettingsText = createFileSelectionWidgets(composite, mavenConfiguration.getUserSettingsFile(),
199+
SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE);
229200

230201
Button updateSettings = new Button(composite, SWT.NONE);
231202
updateSettings.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
232203
updateSettings.setText(Messages.MavenSettingsPreferencePage_btnUpdate);
233204
updateSettings.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> updateSettings(true)));
205+
234206
Label localRepositoryLabel = new Label(composite, SWT.NONE);
235207
GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
236208
gd.verticalIndent = 25;
@@ -242,48 +214,52 @@ protected Control createContents(Composite parent) {
242214
localRepositoryText.setData("name", "localRepositoryText"); //$NON-NLS-1$ //$NON-NLS-2$
243215
localRepositoryText.setEditable(false);
244216

245-
ModifyListener settingsModifyListener = modifyevent -> {
246-
updateLocalRepository();
247-
checkSettings();
248-
};
249-
userSettingsText.addModifyListener(settingsModifyListener);
250-
globalSettingsText.addModifyListener(settingsModifyListener);
251-
252-
String globalSettings = mavenConfiguration.getGlobalSettingsFile();
253-
if(globalSettings != null) {
254-
globalSettingsText.setText(globalSettings);
255-
}
256-
String userSettings = mavenConfiguration.getUserSettingsFile();
257-
if(userSettings != null) {
258-
userSettingsText.setText(userSettings);
259-
}
260217
checkSettings();
261218
updateLocalRepository();
262219

263220
return composite;
264221
}
265222

266-
private void updateUserSettingsLink(String userSettings) {
267-
File userSettingsFile = SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE;
268-
if(userSettings != null) {
269-
userSettingsFile = new File(userSettings);
270-
}
271-
boolean active = userSettingsFile.canRead();
223+
private Link createLink(Composite composite, String text, String tooltip, Supplier<String> selectedFile,
224+
File defaultFile) {
225+
Link link = new Link(composite, SWT.NONE);
226+
link.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
227+
link.setText(text);
228+
link.setToolTipText(tooltip);
229+
link.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
230+
File file = Optional.ofNullable(selectedFile.get()).map(File::new).orElse(defaultFile);
231+
if(file != null) {
232+
openEditor(file);
233+
}
234+
}));
235+
return link;
236+
}
272237

273-
String text = Messages.MavenSettingsPreferencePage_userSettingslink1;
274-
if(active) {
275-
text = Messages.MavenSettingsPreferencePage_userSettingslink2;
238+
private Text createFileSelectionWidgets(Composite composite, String selectedFile, File defaultFile) {
239+
Text fileText = new Text(composite, SWT.BORDER);
240+
fileText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
241+
if(defaultFile != null) {
242+
fileText.setMessage(defaultFile.getAbsolutePath());
276243
}
277-
userSettingsLink.setText(text);
244+
if(selectedFile != null) {
245+
fileText.setText(selectedFile);
246+
}
247+
fileText.addModifyListener(modifyevent -> {
248+
updateLocalRepository();
249+
checkSettings();
250+
});
251+
252+
Button browseButton = new Button(composite, SWT.NONE);
253+
browseButton.setLayoutData(new GridData(SWT.FILL, SWT.RIGHT, false, false, 1, 1));
254+
browseButton.setText(Messages.MavenSettingsPreferencePage_settingsBrowseButton_text);
255+
browseButton.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> browseSettingsAction(fileText)));
256+
return fileText;
278257
}
279258

280-
private void updateGlobalSettingsLink(String globalSettings) {
281-
boolean active = globalSettings != null && new File(globalSettings).canRead();
282-
String text = Messages.MavenSettingsPreferencePage_globalSettingslink1;
283-
if(active) {
284-
text = Messages.MavenSettingsPreferencePage_globalSettingslink2;
285-
}
286-
globalSettingsLink.setText(text);
259+
private void updateLink(Link link, String path, File defaultFile, String activeText, String inactiveText) {
260+
File file = path != null ? new File(path) : defaultFile;
261+
boolean active = file != null && file.canRead();
262+
link.setText(active ? activeText : inactiveText);
287263
}
288264

289265
protected void updateLocalRepository() {
@@ -310,42 +286,43 @@ protected void checkSettings() {
310286
// NB: enable/disable links regardless of validation errors
311287

312288
String globalSettings = getGlobalSettings();
313-
updateGlobalSettingsLink(globalSettings);
289+
updateLink(globalSettingsLink, globalSettings, null, Messages.MavenSettingsPreferencePage_globalSettingslink2,
290+
Messages.MavenSettingsPreferencePage_globalSettingslink1);
314291

315292
String userSettings = getUserSettings();
316-
updateUserSettingsLink(userSettings);
317-
318-
if(globalSettings != null
319-
&& !checkSettings(globalSettings, Messages.MavenSettingsPreferencePage_error_globalSettingsMissing,
320-
Messages.MavenSettingsPreferencePage_error_globalSettingsParse)) {
321-
//work is done in if-condition
322-
} else if(userSettings != null) {
323-
checkSettings(userSettings, Messages.MavenSettingsPreferencePage_error_userSettingsMissing,
324-
Messages.MavenSettingsPreferencePage_error_userSettingsParse);
325-
}
293+
updateLink(userSettingsLink, userSettings, SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE,
294+
Messages.MavenSettingsPreferencePage_userSettingslink2, Messages.MavenSettingsPreferencePage_userSettingslink1);
295+
296+
setMessage(null);
297+
checkSettings(globalSettings, Messages.MavenSettingsPreferencePage_error_globalSettingsMissing,
298+
l -> maven.validateSettings(l).stream().map(SettingsProblem::getMessage),
299+
Messages.MavenSettingsPreferencePage_error_globalSettingsParse);
300+
checkSettings(userSettings, Messages.MavenSettingsPreferencePage_error_userSettingsMissing,
301+
l -> maven.validateSettings(l).stream().map(SettingsProblem::getMessage),
302+
Messages.MavenSettingsPreferencePage_error_userSettingsParse);
326303
}
327304

328-
private boolean checkSettings(String location, String errorMissing, String errorParse) {
329-
if(!new File(location).canRead()) {
330-
setMessage(errorMissing, IMessageProvider.WARNING);
331-
return false;
332-
}
333-
List<SettingsProblem> result = maven.validateSettings(location);
334-
if(!result.isEmpty()) {
335-
setMessage(NLS.bind(errorParse, result.get(0).getMessage()), IMessageProvider.WARNING);
336-
return false;
305+
private void checkSettings(String location, String errorMissing, Function<String, Stream<String>> validator,
306+
String errorParse) {
307+
if(location != null) {
308+
String newMessage = !new File(location).canRead() //
309+
? errorMissing
310+
: validator.apply(location).findFirst().map(msg -> NLS.bind(errorParse, msg)).orElse(null);
311+
if(newMessage != null) {
312+
String prefix = getMessage() != null ? getMessage() + " and " : "";
313+
setMessage(prefix + newMessage, IMessageProvider.WARNING);
314+
}
337315
}
338-
return true;
339316
}
340317

341-
void openEditor(String fileName) {
318+
void openEditor(File file) {
342319
IWorkbench workbench = PlatformUI.getWorkbench();
343320
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
344321
IWorkbenchPage page = window.getActivePage();
345322

346323
IEditorDescriptor desc = workbench.getEditorRegistry().getDefaultEditor("settings.xml"); //$NON-NLS-1$
347324

348-
IEditorInput input = new FileStoreEditorInput(EFS.getLocalFileSystem().fromLocalFile(new File(fileName)));
325+
IEditorInput input = new FileStoreEditorInput(EFS.getLocalFileSystem().fromLocalFile(file));
349326
try {
350327
IEditorPart editor = IDE.openEditor(page, input, desc.getId());
351328
if(editor == null) {
@@ -354,19 +331,19 @@ void openEditor(String fileName) {
354331
}
355332
editor.addPropertyListener((source, propId) -> {
356333
if(!editor.isDirty()) {
357-
log.info("Refreshing settings {}", fileName); //$NON-NLS-1$
334+
log.info("Refreshing settings {}", file); //$NON-NLS-1$
358335
}
359336
});
360337
} catch(PartInitException ex) {
361338
log.error(ex.getMessage(), ex);
362339
}
363340
}
364341

365-
String getUserSettings() {
342+
private String getUserSettings() {
366343
return getSettings(userSettingsText);
367344
}
368345

369-
String getGlobalSettings() {
346+
private String getGlobalSettings() {
370347
return getSettings(globalSettingsText);
371348
}
372349

0 commit comments

Comments
 (0)