Skip to content

Commit

Permalink
Support names for Updates repositories in product definition #345
Browse files Browse the repository at this point in the history
  • Loading branch information
jcompagner committed Oct 27, 2023
1 parent 74f7aa0 commit 629cbdc
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public interface IRepositoryInfo extends IProductObject {

void setURL(String url);

String getName();

void setName(String name);

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ public interface IRepositoryReference extends ISiteObject {

void setURL(String url) throws CoreException;

String getName();

void setName(String name) throws CoreException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,32 @@ public class RepositoryInfo extends ProductObject implements IRepositoryInfo {

private static final long serialVersionUID = 1L;

public static final String P_NAME = "name"; //$NON-NLS-1$
public static final String P_LOCATION = "location"; //$NON-NLS-1$
public static final String P_ENABLED = "enabled"; //$NON-NLS-1$

private String fURL;
private String fName;
private boolean fEnabled = true; // enabled unless specified otherwise

public RepositoryInfo(IProductModel model) {
super(model);
}

@Override
public void setName(String name) {
String old = fName;
fName = name;
if (isEditable()) {
firePropertyChanged(P_NAME, old, fName);
}
}

@Override
public String getName() {
return fName;
}

@Override
public void setURL(String url) {
String old = fURL;
Expand All @@ -58,7 +74,7 @@ public boolean getEnabled() {
public void setEnabled(boolean enabled) {
boolean old = fEnabled;
fEnabled = enabled;
if (isEditable()) {
if (isEditable() && old != fEnabled) {
firePropertyChanged(P_ENABLED, old, fEnabled);
}
}
Expand All @@ -68,6 +84,7 @@ public void parse(Node node) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
fURL = element.getAttribute("location"); //$NON-NLS-1$
fName = element.getAttribute("name"); //$NON-NLS-1$
fEnabled = Boolean.parseBoolean(element.getAttribute(P_ENABLED));
}
}
Expand All @@ -77,6 +94,7 @@ public void parse(Node node) {
public void write(String indent, PrintWriter writer) {
if (isURLDefined()) {
writer.print(indent + "<repository location=\"" + fURL + "\""); //$NON-NLS-1$ //$NON-NLS-2$
writer.print(" name=\"" + fName + "\""); //$NON-NLS-1$//$NON-NLS-2$
writer.print(" enabled=\"" + fEnabled + "\""); //$NON-NLS-1$//$NON-NLS-2$
writer.println(" />"); //$NON-NLS-1$
}
Expand All @@ -99,7 +117,7 @@ public boolean equals(Object obj) {
if (!(obj instanceof RepositoryInfo other)) {
return false;
}
return Objects.equals(fURL, other.fURL);
return Objects.equals(fURL, other.fURL) && Objects.equals(fName, other.fName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,31 @@ public class RepositoryReference extends SiteObject implements IRepositoryRefere

private static final long serialVersionUID = 1L;

public static final String P_NAME = "name"; //$NON-NLS-1$
public static final String P_LOCATION = "location"; //$NON-NLS-1$
public static final String P_ENABLED = "enabled"; //$NON-NLS-1$

private String fName;
private String fURL;
private boolean fEnabled = true; // enabled unless specified otherwise

public RepositoryReference() {
super();
}

@Override
public String getName() {
return fName;
}

@Override
public void setName(String name) throws CoreException {
String old = fName;
fName = name;
ensureModelEditable();
firePropertyChanged(P_NAME, old, fName);
}

@Override
public void setURL(String url) throws CoreException {
String old = fURL;
Expand Down Expand Up @@ -65,6 +80,7 @@ public void parse(Node node) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
fURL = element.getAttribute("location"); //$NON-NLS-1$
fName = element.getAttribute("name"); //$NON-NLS-1$
fEnabled = Boolean.parseBoolean(element.getAttribute(P_ENABLED));
}
}
Expand All @@ -73,6 +89,7 @@ public void parse(Node node) {
public void write(String indent, PrintWriter writer) {
if (isURLDefined()) {
writer.print(indent + "<repository-reference location=\"" + fURL + "\""); //$NON-NLS-1$ //$NON-NLS-2$
writer.print(" name=\"" + fName + "\""); //$NON-NLS-1$//$NON-NLS-2$
writer.print(" enabled=\"" + fEnabled + "\""); //$NON-NLS-1$//$NON-NLS-2$
writer.println(" />"); //$NON-NLS-1$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3071,13 +3071,16 @@ public class PDEUIMessages extends NLS {
public static String UpdatesSection_description;
public static String UpdatesSection_RepositoryDialogTitle;
public static String UpdatesSection_Location;
public static String UpdatesSection_Name;
public static String UpdatesSection_ErrorLocationNoName;
public static String UpdatesSection_ErrorInvalidURL;
public static String UpdatesSection_LocationColumn;

public static String UpdatesSection_LocationMessage;
public static String UpdatesSection_EnabledColumn;

public static String UpdatesSection_NameColumn;

public static String CustomizationPage_title;

public static String PreferencesSection_title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,44 +44,54 @@
*/
public class RepositoryDialog extends StatusDialog {
private Text fLocation;
private Text fName;
private final String fRepoURL;
private String fLocationStr;
private final String fNameStr;
private RepositoryResult result;

/**
* @param shell
* the shell to use for the dialog which may not be null
* @param repoURL
* The initial value of the URL, which may be null
*/
public RepositoryDialog(Shell shell, String repoURL) {
public RepositoryDialog(Shell shell, String repoURL, String name) {
super(shell);
fRepoURL = repoURL;
fNameStr = name;
}

@Override
protected Control createDialogArea(Composite parent) {
Composite comp = (Composite) super.createDialogArea(parent);
((GridLayout) comp.getLayout()).numColumns = 2;
WidgetFactory.label(SWT.NONE).text(PDEUIMessages.UpdatesSection_Name).create(comp);
fName = WidgetFactory.text(SWT.BORDER).create(comp);
WidgetFactory.label(SWT.NONE).text(PDEUIMessages.UpdatesSection_Location).create(comp);
fLocation = WidgetFactory.text(SWT.BORDER).create(comp);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH);
fLocation.setLayoutData(data);
fName.setLayoutData(data);
DropTarget target = new DropTarget(fLocation, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK);
target.setTransfer(new Transfer[] { URLTransfer.getInstance(), FileTransfer.getInstance() });
target.addDropListener(new TextURLDropAdapter(fLocation, true));
fLocation.addModifyListener(e -> validate());
fName.addModifyListener(e -> validate());

if (fRepoURL != null) {
fLocation.setText(fRepoURL);
}
if (fNameStr != null) {
fName.setText(fNameStr);
}
validate();
return comp;
}

protected void validate() {
fLocationStr = fLocation.getText().trim();
updateStatus(isValidURL(fLocationStr));
result = new RepositoryResult(fName.getText().trim(), fLocation.getText().trim());
updateStatus(isValidURL(result.url()));
}

private IStatus isValidURL(String location) {
Expand Down Expand Up @@ -116,8 +126,11 @@ protected Control createHelpControl(Composite parent) {
*
* @return the repository URL
*/
public String getResult() {
return fLocationStr;
public RepositoryResult getResult() {
return result;
}

public record RepositoryResult(String name, String url) {
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eclipse.pde.internal.ui.PDEPluginImages;
import org.eclipse.pde.internal.ui.PDEUIMessages;
import org.eclipse.pde.internal.ui.dialogs.RepositoryDialog;
import org.eclipse.pde.internal.ui.dialogs.RepositoryDialog.RepositoryResult;
import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
import org.eclipse.pde.internal.ui.editor.PDEFormPage;
import org.eclipse.pde.internal.ui.editor.TableSection;
Expand Down Expand Up @@ -76,7 +77,7 @@ public Object[] getElements(Object inputElement) {
private class LabelProvider extends PDELabelProvider {
@Override
public Image getColumnImage(Object obj, int index) {
if (index == 0)
if (index == 1)
return get(PDEPluginImages.DESC_REPOSITORY_OBJ);
return null;
}
Expand All @@ -85,8 +86,9 @@ public Image getColumnImage(Object obj, int index) {
public String getColumnText(Object obj, int index) {
IRepositoryReference repo = (IRepositoryReference) obj;
return switch (index) {
case 0 -> repo.getURL();
case 1 -> Boolean.toString(repo.getEnabled());
case 0 -> repo.getName();
case 1 -> repo.getURL();
case 2 -> Boolean.toString(repo.getEnabled());
default -> null;
};
}
Expand Down Expand Up @@ -129,13 +131,17 @@ protected void createClient(Section section, FormToolkit toolkit) {

final Table table = fRepositoryTable.getTable();

final TableColumn nameColumn = new TableColumn(table, SWT.LEFT);
nameColumn.setText(PDEUIMessages.UpdatesSection_NameColumn);
nameColumn.setWidth(120);

final TableColumn locationColumn = new TableColumn(table, SWT.LEFT);
locationColumn.setText(PDEUIMessages.UpdatesSection_LocationColumn);
locationColumn.setWidth(240);
locationColumn.setWidth(200);

final TableColumn enabledColumn = new TableColumn(table, SWT.LEFT);
enabledColumn.setText(PDEUIMessages.UpdatesSection_EnabledColumn);
enabledColumn.setWidth(120);
enabledColumn.setWidth(80);

GridData data = (GridData) tablePart.getControl().getLayoutData();
data.minimumWidth = 200;
Expand All @@ -155,8 +161,9 @@ public void controlMoved(ControlEvent e) {
@Override
public void controlResized(ControlEvent e) {
int size = table.getSize().x;
locationColumn.setWidth(size / 6 * 5);
enabledColumn.setWidth(size / 6 * 1);
locationColumn.setWidth(size / 7 * 4);
enabledColumn.setWidth(size / 7 * 1);
nameColumn.setWidth(size / 7 * 2);
}

});
Expand Down Expand Up @@ -215,28 +222,30 @@ private void handleEdit(IStructuredSelection selection) {
clearEditors();
if (!selection.isEmpty()) {
IRepositoryReference repo = (IRepositoryReference) selection.toArray()[0];
RepositoryDialog dialog = getRepositoryDialog(repo.getURL());
RepositoryDialog dialog = getRepositoryDialog(repo.getName(), repo.getURL());
if (dialog.open() == Window.OK) {
updateModel(repo, dialog.getResult());
}
}
}

private RepositoryDialog getRepositoryDialog(String repoURL) {
RepositoryDialog dialog = new RepositoryDialog(PDEPlugin.getActiveWorkbenchShell(), repoURL);
private RepositoryDialog getRepositoryDialog(String name, String repoURL) {
RepositoryDialog dialog = new RepositoryDialog(PDEPlugin.getActiveWorkbenchShell(), repoURL, name);
dialog.setTitle(PDEUIMessages.RepositorySection_title);
return dialog;
}

private void updateModel(IRepositoryReference repo, String repoURL) {
private void updateModel(IRepositoryReference repo, RepositoryResult result) {
try {
if (repo != null) {
getSite().removeRepositoryReferences(new IRepositoryReference[] { repo });
}
ISiteModelFactory factory = getModel().getFactory();
IRepositoryReference newRepo = factory.createRepositoryReference();
newRepo.setURL(repoURL.trim());
newRepo.setURL(result.url().trim());
newRepo.setEnabled(true);
if (result.name() != null)
newRepo.setName(result.name().trim());
getSite().addRepositoryReferences(new IRepositoryReference[] { newRepo });
fRepositoryTable.refresh();
fRepositoryTable.setSelection(new StructuredSelection(newRepo));
Expand Down Expand Up @@ -276,7 +285,7 @@ private void handleRemoveAll() {

private void handleAdd() {
clearEditors();
RepositoryDialog dialog = getRepositoryDialog(null);
RepositoryDialog dialog = getRepositoryDialog(null, null);
if (dialog.open() == Window.OK) {
updateModel(null, dialog.getResult());
}
Expand Down Expand Up @@ -358,17 +367,17 @@ private void showControls() {
final IRepositoryReference repo = (IRepositoryReference) selection.getFirstElement();
final CCombo combo = new CCombo(table, SWT.BORDER | SWT.READ_ONLY);
combo.setItems(new String[] {Boolean.toString(true), Boolean.toString(false)});
combo.setText(item.getText(1));
combo.setText(item.getText(2));
combo.pack();
combo.addSelectionListener(widgetSelectedAdapter(e -> {
item.setText(1, combo.getText());
item.setText(2, combo.getText());
try {
repo.setEnabled(Boolean.parseBoolean(combo.getText()));
} catch (CoreException ex) {
PDEPlugin.log(ex);
}
}));
fEnabledColumnEditor.setEditor(combo, item, 1);
fEnabledColumnEditor.setEditor(combo, item, 2);
}
}

Expand Down
Loading

0 comments on commit 629cbdc

Please sign in to comment.