Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d44706b262166e4cbe2500fc5515d2237992ed7a (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
/*
 * Copyright (c) 2014-2017 Eike Stepper (Loehne, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v20.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.oomph.setup.internal.installer;

import org.eclipse.oomph.internal.ui.GeneralDragAdapter;
import org.eclipse.oomph.internal.ui.OomphTransferDelegate;
import org.eclipse.oomph.p2.P2Factory;
import org.eclipse.oomph.p2.core.Agent;
import org.eclipse.oomph.p2.core.P2Util;
import org.eclipse.oomph.p2.core.Profile;
import org.eclipse.oomph.setup.internal.core.util.SetupCoreUtil;
import org.eclipse.oomph.setup.ui.AbstractSetupDialog;
import org.eclipse.oomph.setup.util.SetupUtil;
import org.eclipse.oomph.util.OomphPlugin;
import org.eclipse.oomph.util.PropertiesUtil;

import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.IProvidedCapability;
import org.eclipse.equinox.p2.metadata.VersionRange;
import org.eclipse.equinox.p2.query.QueryUtil;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import org.osgi.framework.Bundle;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Eike Stepper
 */
public final class AboutDialog extends AbstractSetupDialog
{
  private static final int ECLIPSE_VERSION_COLUMN_INDEX = 1;

  private static final String SHOW_ALL_PLUGINS = "SHOW_ALL_PLUGINS";

  private static final int DND_OPERATIONS = DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK;

  private static final List<? extends OomphTransferDelegate> DND_DELEGATES = Collections.singletonList(new OomphTransferDelegate.TextTransferDelegate());

  private static final Transfer[] DND_TRANSFERS = new Transfer[] { DND_DELEGATES.get(0).getTransfer() };

  private static final int VERSION_COLUMN_PADDING = 10;

  private final IDialogSettings dialogSettings = getDialogSettings();

  private final String version;

  private boolean showAllPlugins;

  private Profile profile;

  private Table table;

  private TableColumn idColumn;

  private TableColumn versionColumn;

  private ControlAdapter columnResizer = new ControlAdapter()
  {
    @Override
    public void controlResized(ControlEvent e)
    {
      Point size = table.getSize();
      ScrollBar bar = table.getVerticalBar();
      if (bar != null && bar.isVisible())
      {
        size.x -= bar.getSize().x;
      }

      versionColumn.pack();

      idColumn.setWidth(size.x - versionColumn.getWidth() - VERSION_COLUMN_PADDING);
    }
  };

  private Color gray;

  public AboutDialog(Shell parentShell, String theVersion)
  {
    super(parentShell, "About " + parentShell.getText(), 700, 500, SetupInstallerPlugin.INSTANCE, false);
    version = theVersion;
    showAllPlugins = dialogSettings.getBoolean(SHOW_ALL_PLUGINS);
  }

  @Override
  protected String getDefaultMessage()
  {
    return "Installer version: " + version + "\n" + SetupUtil.INSTALLER_UPDATE_URL;
  }

  @Override
  protected void createUI(Composite parent)
  {
    table = new Table(parent, SWT.FULL_SELECTION | SWT.MULTI | SWT.NO_SCROLL | SWT.V_SCROLL);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    table.setLayoutData(new GridData(GridData.FILL_BOTH));
    table.addControlListener(columnResizer);

    idColumn = new TableColumn(table, SWT.NONE);
    idColumn.setText("Plugin");
    idColumn.setResizable(false);
    idColumn.setMoveable(false);

    versionColumn = new TableColumn(table, SWT.NONE);
    versionColumn.setText("Version");
    versionColumn.setResizable(false);
    versionColumn.setMoveable(false);

    Agent agent = P2Util.getAgentManager().getCurrentAgent();
    profile = agent.getCurrentProfile();

    gray = getShell().getDisplay().getSystemColor(SWT.COLOR_DARK_BLUE);

    fillTable();

    TableViewer tableViewer = new TableViewer(table);
    GeneralDragAdapter generalDragAdapter = new GeneralDragAdapter(tableViewer, new GeneralDragAdapter.DraggedObjectsFactory()
    {
      public List<Object> createDraggedObjects(ISelection selection) throws Exception
      {
        List<Object> result = new ArrayList<Object>();
        for (Object object : ((IStructuredSelection)selection).toArray())
        {
          if (object instanceof IInstallableUnit)
          {
            IInstallableUnit iu = (IInstallableUnit)object;
            result.add(P2Factory.eINSTANCE.createRequirement(iu.getId(), new VersionRange(iu.getVersion().toString())));
          }
        }

        return result;
      }
    }, DND_DELEGATES);

    tableViewer.addDragSupport(DND_OPERATIONS, DND_TRANSFERS, generalDragAdapter);
  }

  private void fillTable()
  {
    List<IInstallableUnit> plugins = getPlugins();
    Collections.sort(plugins);

    for (IInstallableUnit plugin : plugins)
    {
      TableItem item = new TableItem(table, SWT.NONE);
      item.setData(plugin);

      String id = plugin.getId();
      item.setText(0, id);

      String version = plugin.getVersion().toString();

      if (id.startsWith(SetupCoreUtil.OOMPH_NAMESPACE))
      {
        try
        {
          Bundle[] bundles = Platform.getBundles(id, version);
          if (bundles != null)
          {
            for (Bundle bundle : bundles)
            {
              String buildID = OomphPlugin.getBuildID(bundle);
              if (buildID != null)
              {
                version += " Build " + buildID;
                break;
              }
            }
          }
        }
        catch (Exception ex)
        {
          SetupInstallerPlugin.INSTANCE.log(ex);
        }
      }
      else
      {
        item.setForeground(gray);
      }

      item.setText(ECLIPSE_VERSION_COLUMN_INDEX, version);
    }

    versionColumn.pack();
    versionColumn.setWidth(versionColumn.getWidth() + VERSION_COLUMN_PADDING);

    table.getDisplay().asyncExec(new Runnable()
    {
      public void run()
      {
        columnResizer.controlResized(null);
      }
    });
  }

  private List<IInstallableUnit> getPlugins()
  {
    if (profile == null)
    {
      return Collections.emptyList();
    }

    List<IInstallableUnit> plugins = new ArrayList<IInstallableUnit>();
    for (IInstallableUnit iu : P2Util.asIterable(profile.query(QueryUtil.createIUAnyQuery(), null)))
    {
      if (showAllPlugins || iu.getId().startsWith(SetupCoreUtil.OOMPH_NAMESPACE))
      {
        for (IProvidedCapability capability : iu.getProvidedCapabilities())
        {
          if ("osgi.bundle".equals(capability.getNamespace()))
          {
            plugins.add(iu);
            break;
          }
        }
      }
    }

    return plugins;
  }

  @Override
  protected void createButtonsForButtonBar(Composite parent)
  {
    final Button showAllPluginsButton = createCheckbox(parent, "Show all plugins");
    showAllPluginsButton.setSelection(showAllPlugins);
    showAllPluginsButton.addSelectionListener(new SelectionAdapter()
    {
      @Override
      public void widgetSelected(SelectionEvent e)
      {
        showAllPlugins = showAllPluginsButton.getSelection();
        dialogSettings.put(SHOW_ALL_PLUGINS, showAllPlugins);

        table.removeAll();
        fillTable();
      }
    });

    createButton(parent, IDialogConstants.OK_ID, "Close", true);
  }

  @Override
  protected String getShellText()
  {
    return PropertiesUtil.getProductName();
  }
}

Back to the top