Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 788b8726a0bd6b0bdd05fa5d514888eb57c82821 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*******************************************************************************
 * 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.internal.preferences;

import java.io.File;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IPropertyListener;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

import org.apache.maven.cli.MavenCli;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.building.SettingsProblem;

import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.core.MavenLogger;
import org.eclipse.m2e.core.core.Messages;
import org.eclipse.m2e.core.embedder.IMaven;
import org.eclipse.m2e.core.embedder.IMavenConfiguration;
import org.eclipse.m2e.core.embedder.MavenRuntime;
import org.eclipse.m2e.core.embedder.MavenRuntimeManager;
import org.eclipse.m2e.core.index.IndexManager;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.MavenUpdateRequest;


/**
 * Maven installations preference page
 * 
 * @author Eugene Kuleshov
 */
public class MavenSettingsPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {

  final MavenPlugin mavenPlugin;

  final MavenRuntimeManager runtimeManager;
  
  final IMavenConfiguration mavenConfiguration;
  
  final IMaven maven;

  MavenRuntime defaultRuntime;


  Text userSettingsText;

  Text localRepositoryText;

  boolean dirty = false;

  private Link userSettingsLink;

  public MavenSettingsPreferencePage() {
    setTitle(org.eclipse.m2e.core.internal.Messages.MavenSettingsPreferencePage_title);

    this.mavenPlugin = MavenPlugin.getDefault();
    this.runtimeManager = mavenPlugin.getMavenRuntimeManager();
    this.mavenConfiguration = MavenPlugin.getDefault().getMavenConfiguration();
    this.maven = MavenPlugin.getDefault().getMaven();
  }

  public void init(IWorkbench workbench) {
  }

  
  /* (non-Javadoc)
   * @see org.eclipse.jface.dialogs.DialogPage#setVisible(boolean)
   */
  public void setVisible(boolean visible) {
    super.setVisible(visible);
    if(visible){
      updateLocalRepository();
    }
  }

  protected void performDefaults() {
    userSettingsText.setText(MavenCli.DEFAULT_USER_SETTINGS_FILE.getAbsolutePath());
    setDirty(true);
    updateLocalRepository();
    super.performDefaults();
  }

  protected void updateSettings(final boolean updateMavenDependencies){
    final String userSettings = getUserSettings();
    
    new Job(org.eclipse.m2e.core.internal.Messages.MavenSettingsPreferencePage_job_updating) {
      protected IStatus run(IProgressMonitor monitor) {
        try {
          final File localRepositoryDir = new File(maven.getLocalRepository().getBasedir());
          if(userSettings.length() > 0) {
            mavenConfiguration.setUserSettingsFile(userSettings);
          } else {
            mavenConfiguration.setUserSettingsFile(null);
          }

          File newRepositoryDir = new File(maven.getLocalRepository().getBasedir());
          if(!newRepositoryDir.equals(localRepositoryDir)) {
            IndexManager indexManager = mavenPlugin.getIndexManager();
            indexManager.getWorkspaceIndex().updateIndex(true, monitor);
          }
          if(updateMavenDependencies){
            IMavenProjectFacade[] projects = MavenPlugin.getDefault().getMavenProjectManager().getProjects();
            ArrayList<IProject> allProjects = new ArrayList<IProject>();
            if(projects != null){
              MavenPlugin.getDefault().getMaven().reloadSettings();
              SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, projects.length);
              for(int i=0;i<projects.length;i++){
                subMonitor.beginTask(NLS.bind(org.eclipse.m2e.core.internal.Messages.MavenSettingsPreferencePage_task_updating, projects[i].getProject().getName()), 1);
                allProjects.add(projects[i].getProject());
              }
              IMavenConfiguration configuration = MavenPlugin.getDefault().getMavenConfiguration();
              MavenPlugin.getDefault().getMavenProjectManager().refresh(new MavenUpdateRequest(allProjects.toArray(new IProject[]{}), configuration.isOffline(), true));
              subMonitor.done();
            }
          }
          return Status.OK_STATUS;
        } catch (CoreException e) {
          return e.getStatus();
        }
      }
    }.schedule();
  }
  
  protected void performApply() {
    if(dirty){
      updateSettings(false);
    }
  }
  
  public boolean performOk() {
    if (dirty) {
      updateSettings(false);
    }
    return true;
  }
  
  public void setDirty(boolean dirty){
    this.dirty = dirty;
  }
  
  public boolean isDirty(){
    return this.dirty;
  }

  protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    GridLayout gridLayout = new GridLayout(4, false);
    gridLayout.marginBottom = 5;
    gridLayout.marginRight = 5;
    gridLayout.marginHeight = 0;
    gridLayout.marginWidth = 0;
    composite.setLayout(gridLayout);

    createUserSettings(composite);
    Label localRepositoryLabel = new Label(composite, SWT.NONE);
    GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false, 4, 1);
    gd.verticalIndent=25;
    localRepositoryLabel.setLayoutData(gd);
    localRepositoryLabel.setText(org.eclipse.m2e.core.internal.Messages.MavenSettingsPreferencePage_lblLocal);
    
    localRepositoryText = new Text(composite, SWT.READ_ONLY|SWT.BORDER);
    localRepositoryText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
    localRepositoryText.setData("name", "localRepositoryText"); //$NON-NLS-1$ //$NON-NLS-2$
    localRepositoryText.setEditable(false);
    Button reindexButton = new Button(composite, SWT.NONE);
    reindexButton.setLayoutData(new GridData(SWT.FILL, SWT.RIGHT, false, false, 1, 1));
    reindexButton.setText(Messages.getString("preferences.reindexButton"));     //$NON-NLS-1$
    reindexButton.addSelectionListener(new SelectionAdapter(){

      /* (non-Javadoc)
       * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
       */
      public void widgetSelected(SelectionEvent e) {
        new WorkspaceJob(org.eclipse.m2e.core.internal.Messages.MavenSettingsPreferencePage_job_indexing) {
            public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
              IndexManager indexManager = mavenPlugin.getIndexManager();
              indexManager.getWorkspaceIndex().updateIndex(true, monitor);
              return Status.OK_STATUS;
            }
         }.schedule();
      }
    });
    defaultRuntime = runtimeManager.getDefaultRuntime();

    String userSettings = mavenConfiguration.getUserSettingsFile();
    if(userSettings == null || userSettings.length() == 0) {
      userSettingsText.setText(MavenCli.DEFAULT_USER_SETTINGS_FILE.getAbsolutePath());
    } else {
      userSettingsText.setText(userSettings);
    }

    checkSettings();
    updateLocalRepository();

    userSettingsText.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent modifyevent) {
        updateLocalRepository();
        checkSettings();
        setDirty(true);
      }
    });

    return composite;
  }
  
  public void updateSettingsLink(boolean active){
    String text = org.eclipse.m2e.core.internal.Messages.MavenSettingsPreferencePage_link1;
    if(active){
      text = org.eclipse.m2e.core.internal.Messages.MavenSettingsPreferencePage_link2;
    }
    userSettingsLink.setText(text);
  }
  /**
   * @param composite
   */
  private void createUserSettings(Composite composite) {

    userSettingsLink = new Link(composite, SWT.NONE);
    userSettingsLink.setData("name", "userSettingsLink"); //$NON-NLS-1$ //$NON-NLS-2$
    userSettingsLink.setText(org.eclipse.m2e.core.internal.Messages.MavenSettingsPreferencePage_link2);
    userSettingsLink.setToolTipText(org.eclipse.m2e.core.internal.Messages.MavenSettingsPreferencePage_link_tooltip);
    GridData gd_userSettingsLabel = new GridData(SWT.FILL, SWT.CENTER, true, false, 4, 1);
    
    gd_userSettingsLabel.verticalIndent = 15;
    userSettingsLink.setLayoutData(gd_userSettingsLabel);
    userSettingsLink.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        String userSettings = getUserSettings();
        if(userSettings.length() == 0) {
          userSettings = MavenCli.DEFAULT_USER_SETTINGS_FILE.getAbsolutePath();
        }
        openEditor(userSettings);
      }
    });
    userSettingsText = new Text(composite, SWT.BORDER);
    userSettingsText.setData("name", "userSettingsText"); //$NON-NLS-1$ //$NON-NLS-2$
    GridData gd_userSettingsText = new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1);
    gd_userSettingsText.verticalIndent = 5;
    gd_userSettingsText.widthHint = 100;
    userSettingsText.setLayoutData(gd_userSettingsText);

    Button userSettingsBrowseButton = new Button(composite, SWT.NONE);
    GridData gd_userSettingsBrowseButton = new GridData(SWT.FILL, SWT.RIGHT, false, false, 1, 1);
   
    userSettingsBrowseButton.setLayoutData(gd_userSettingsBrowseButton);
    userSettingsBrowseButton.setText(org.eclipse.m2e.core.internal.Messages.MavenSettingsPreferencePage_btnBrowse);
    userSettingsBrowseButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
        if(getUserSettings().length() > 0) {
          dialog.setFileName(getUserSettings());
        }
        String file = dialog.open();
        if(file != null) {
          file = file.trim();
          if(file.length() > 0) {
            userSettingsText.setText(file);
            updateLocalRepository();
            checkSettings();
          }
        }
      }
    });
    
    Button updateSettings = new Button(composite, SWT.NONE);
    GridData gd = new GridData(SWT.FILL, SWT.LEFT, false, false, 1, 1);
    updateSettings.setText(org.eclipse.m2e.core.internal.Messages.MavenSettingsPreferencePage_btnUpdate);
    updateSettings.addSelectionListener(new SelectionAdapter(){
      public void widgetSelected(SelectionEvent e){
        updateSettings(true);
      }
    });
  }

  protected void updateLocalRepository() {
    final String userSettings = getUserSettings();
    String globalSettings = runtimeManager.getGlobalSettingsFile();
    try {
      Settings settings = maven.buildSettings(globalSettings, userSettings);
      String localRepository = settings.getLocalRepository();
      if(localRepository == null){
        localRepository = RepositorySystem.defaultUserLocalRepository.getAbsolutePath();
      }
      if(!localRepositoryText.isDisposed()) {
        localRepositoryText.setText(localRepository == null ? "" : localRepository); //$NON-NLS-1$
      }
    } catch (CoreException e) {
      setMessage(e.getMessage(), IMessageProvider.ERROR);
    }
  }

  protected void checkSettings() {
    setErrorMessage(null);
    setMessage(null);
    boolean fileExists = false;
    String userSettings = getUserSettings();
    if(userSettings != null && userSettings.length() > 0) {
      File userSettingsFile = new File(userSettings);
      if(!userSettingsFile.exists()) {
        setMessage(org.eclipse.m2e.core.internal.Messages.MavenSettingsPreferencePage_error_missing, IMessageProvider.WARNING);
        userSettings = null;
        
      } else {
        fileExists = true;
      }
      
    } else {
      userSettings = null;
    }
    updateSettingsLink(fileExists);
    List<SettingsProblem> result = maven.validateSettings(userSettings);
    if(result.size() > 0) {
      setMessage(NLS.bind(org.eclipse.m2e.core.internal.Messages.MavenSettingsPreferencePage_error_parse, result.get(0).getMessage()), IMessageProvider.WARNING);
    }
  }


  
  @SuppressWarnings("unchecked")
  void openEditor(final String fileName) {
    IWorkbench workbench = PlatformUI.getWorkbench();
    IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
    IWorkbenchPage page = window.getActivePage();

    IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor("settings.xml"); //$NON-NLS-1$

    File file = new File(fileName);
    IEditorInput input = null;
    try {
      //class implementing editor input for external file has been renamed in eclipse 3.3, hence reflection
      Class javaInput = null;
      try {
        javaInput = Class.forName("org.eclipse.ui.internal.editors.text.JavaFileEditorInput"); //$NON-NLS-1$
        Constructor cons = javaInput.getConstructor(new Class[] {File.class});
        input = (IEditorInput) cons.newInstance(new Object[] {file});
      } catch(Exception e) {
        try {
          IFileStore fileStore = EFS.getLocalFileSystem().fromLocalFile(file);
          Class storeInput = Class.forName("org.eclipse.ui.ide.FileStoreEditorInput"); //$NON-NLS-1$
          Constructor cons = storeInput.getConstructor(new Class[] {IFileStore.class});
          input = (IEditorInput) cons.newInstance(new Object[] {fileStore});
        } catch(Exception ex) {
          //ignore...
        }
      }
      final IEditorPart editor = IDE.openEditor(page, input, desc.getId());
      editor.addPropertyListener(new IPropertyListener() {
        public void propertyChanged(Object source, int propId) {
          if(!editor.isDirty()) {
            mavenPlugin.getConsole().logMessage("Refreshing settings " + fileName);
          }
        }
      });

    } catch(PartInitException ex) {
      MavenLogger.log(ex);
    }
  }

  String getUserSettings() {
    return userSettingsText.getText().trim();
  }


}

Back to the top