uk.theretiredprogrammer.nbreleaseplugin.VersionUpdaterPanel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nbreleaseplugin Show documentation
Show all versions of nbreleaseplugin Show documentation
Plugin to support the Maven Release Processes from within Netbeans IDE
/*
* Copyright 2017 Richard Linsdale.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package uk.theretiredprogrammer.nbreleaseplugin;
import java.awt.Color;
import java.awt.Font;
import static java.awt.Font.BOLD;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.stream.Collectors;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import static javax.swing.border.BevelBorder.RAISED;
import javax.swing.border.TitledBorder;
import static javax.swing.border.TitledBorder.CENTER;
import static javax.swing.border.TitledBorder.TOP;
/**
* The Panel that displays the VersionUpdater information. These are the module
* coordinate and parent coordinates (if they exist). The VersionUpdater Panel
* allows the user to edit the top-level version and automatically updates any
* child's parent version.
*
* @author Richard Linsdale (richard at theretiredprogrammer)
*/
public class VersionUpdaterPanel extends JPanel {
private int nextrow = 0;
private final Pom pom;
private List referringPoms;
private final ElementValue version;
/**
* Create the VersionUpdater Panel.
*
* @param pom the Pom to be used as the data model for the VersionUpdater
* Panel
* @return the newly created VersionUpdater Panel
*/
public static VersionUpdaterPanel create(Pom pom) {
return new VersionUpdaterPanel(pom);
}
@SuppressWarnings("OverridableMethodCallInConstructor")
private VersionUpdaterPanel(Pom pom) {
this.pom = pom;
setBorder(new TitledBorder(new BevelBorder(RAISED),
pom.getElementValue("name").getText(),
CENTER, TOP, new Font("SansSerif", BOLD, 18)));
setLayout(new GridBagLayout());
//
Box box = Box.createHorizontalBox();
box.add(elementDisplay(pom.getElementValue("groupId")));
box.add(display(SEPARATOR_TEXT, OK_COLOR));
box.add(elementDisplay(pom.getElementValue("artifactId")));
box.add(display(SEPARATOR_TEXT, OK_COLOR));
version = pom.getElementValue("version");
box.add(elementEditableDisplay(version));
insertLine("Coordinate", box);
//
if (pom.hasElement("parent")) {
ElementValue parentelement = pom.getElementValue("parent");
box = Box.createHorizontalBox();
box.add(elementDisplay(pom.findElementValue(parentelement, "groupId")));
box.add(display(SEPARATOR_TEXT, OK_COLOR));
box.add(elementDisplay(pom.findElementValue(parentelement, "artifactId")));
box.add(display(SEPARATOR_TEXT, OK_COLOR));
box.add(elementDisplay(pom.findElementValue(parentelement, "version")));
insertLine("Parent", box);
}
//
List modulePoms = pom.getChildren();
if (modulePoms.size() > 0) {
referringPoms = modulePoms.stream().filter((modulepom) -> (modulepom.isParent(pom))).collect(Collectors.toList());
insertLine("Child Modules", new JLabel(
Integer.toString(referringPoms.size()) + " of " + Integer.toString(modulePoms.size())
+ " refer back to this module in their parent element"
));
}
modulePoms.forEach((modulepom) -> {
insertValue(new VersionUpdaterPanel(modulepom));
});
}
private final static Color OK_COLOR = Color.BLUE;
private final static Color PROBLEM_COLOR = Color.RED;
private final static Color INHERITED_COLOR = Color.BLACK;
private final static String UNDEFINED_TEXT = "???";
private final static String SEPARATOR_TEXT = ": ";
private JComponent elementDisplay(ElementValue value) {
switch (value.getType()) {
case OK:
return display(value.getText(), OK_COLOR);
case INHERITED:
case SUBSTITUTED:
case DEFAULTVALUE:
return display(value.getText(), INHERITED_COLOR);
}
throw new RuntimeException("Cant get here! - unless missing which should not occur");
}
private JComponent elementEditableDisplay(ElementValue value) {
switch (value.getType()) {
case OK:
return actionableDisplay(value.getText(),
new UpdateListener("Version", value));
case INHERITED:
case SUBSTITUTED:
case DEFAULTVALUE:
return display(value.getText(), INHERITED_COLOR);
case MISSING:
case INHERITED_MISSING:
return display(UNDEFINED_TEXT, PROBLEM_COLOR);
}
throw new RuntimeException("Cant get here!");
}
private JLabel display(String text, Color col) {
JLabel comp = new JLabel(text);
comp.setForeground(col);
return comp;
}
private JButton actionableDisplay(String text, ActionListener listener) {
JButton comp = new JButton(text);
comp.setForeground(OK_COLOR);
comp.addActionListener(listener);
return comp;
}
private void insertLine(String label, JComponent value) {
insertLabel(label);
insertValue(value);
}
private void insertLabel(String label) {
GridBagConstraints c = new GridBagConstraints();
c.gridy = nextrow;
c.anchor = GridBagConstraints.PAGE_START;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.weightx = 0.0;
c.ipady = 5;
c.ipadx = 10;
add(new JLabel(label), c);
}
private void insertValue(JComponent value) {
GridBagConstraints c = new GridBagConstraints();
c.gridy = nextrow++;
c.anchor = GridBagConstraints.PAGE_START;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.weightx = 1.0;
c.ipady = 5;
c.ipadx = 10;
add(value, c);
}
private class UpdateListener implements ActionListener {
private final ElementValue value;
private final String title;
public UpdateListener(String title, ElementValue value) {
this.value = value;
this.title = title;
}
@Override
public final void actionPerformed(ActionEvent e) {
VersionUpdaterDialog.show(title, value, VersionUpdaterPanel.this, new ChangePanel());
}
}
/**
* Update the version Element in the pom associated with this VersionUpdater
* Panel. Then update any child's parent version element to match the new
* value
*
* @param pev The version element reference;
* @param newvalue the new text value to be updated
*/
public void updateElement(ElementValue pev, String newvalue) {
// update the version
pev.updateContent(pom, newvalue);
// look at all the children and update their parent/version elements
if (referringPoms != null) {
referringPoms.stream().forEach((cpom) -> {
cpom.updateParentVersion(newvalue);
});
}
}
}