Skip to content

Commit

Permalink
Merge pull request #7155 from mbien/nb-upgrade-module-cleanup
Browse files Browse the repository at this point in the history
Cleanup NetBeans Upgrader module.
  • Loading branch information
mbien authored Mar 18, 2024
2 parents bd8966b + 3034cb5 commit 3be92f1
Show file tree
Hide file tree
Showing 73 changed files with 389 additions and 1,034 deletions.
3 changes: 2 additions & 1 deletion nb/o.n.upgrader/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
# under the License.

javac.compilerargs=-Xlint:unchecked
javac.source=1.8
javac.source=11
javac.target=11
module.jar.dir=core

javadoc.arch=${basedir}/arch.xml
Expand Down
145 changes: 24 additions & 121 deletions nb/o.n.upgrader/src/org/netbeans/upgrade/AutoUpgrade.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,106 +21,69 @@
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.beans.PropertyVetoException;
import java.io.*;
import java.net.URL;
import java.util.Arrays;
import java.io.File;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import org.netbeans.util.Util;
import org.openide.ErrorManager;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.LocalFileSystem;
import org.openide.filesystems.MultiFileSystem;
import org.openide.filesystems.XMLFileSystem;
import org.openide.modules.InstalledFileLocator;
import org.openide.modules.SpecificationVersion;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;
import org.xml.sax.SAXException;
import org.openide.util.UserCancelException;

/** pending
/**
* NetBeans configuration migration.
*
* <p>Copies some files of the old user dir to the new user dir.
*
* @author Jiri Rechtacek, Jiri Skrivanek
*/
public final class AutoUpgrade {

private static final Logger LOGGER = Logger.getLogger(AutoUpgrade.class.getName());

public static void main (String[] args) throws Exception {
// show warning if starts for the 1st time on changed userdir (see issue 196075)
String noteChangedDefaults = "";
if (madeObsoleteMessagesLog()) {
noteChangedDefaults = NbBundle.getMessage (AutoUpgrade.class, "MSG_ChangedDefaults", System.getProperty ("netbeans.user", "")); // NOI18N
}

// try new place
File sourceFolder = checkPreviousOnOsSpecificPlace (APACHE_VERSION_TO_CHECK);
public static void main(String[] args) throws Exception {
File sourceFolder = findPreviousUserDir(APACHE_VERSION_TO_CHECK);
if (sourceFolder != null) {
if (!showUpgradeDialog (sourceFolder, noteChangedDefaults)) {
throw new org.openide.util.UserCancelException ();
if (!showUpgradeDialog(sourceFolder)) {
throw new UserCancelException();
}
} else if (! noteChangedDefaults.isEmpty()) {
// show a note only
showNoteDialog(noteChangedDefaults);
}
}

static final Comparator<String> APACHE_VERSION_COMPARATOR = (v1, v2) -> new SpecificationVersion(v1).compareTo(new SpecificationVersion(v2));

static final List<String> APACHE_VERSION_TO_CHECK = Arrays.asList(NbBundle.getMessage(AutoUpgrade.class, "apachenetbeanspreviousversion").split(",")).stream().sorted(APACHE_VERSION_COMPARATOR.reversed()).collect(Collectors.toList());
static final List<String> APACHE_VERSION_TO_CHECK =
Stream.of(NbBundle.getMessage(AutoUpgrade.class, "apachenetbeanspreviousversion")
.split(",")).sorted(APACHE_VERSION_COMPARATOR.reversed()).collect(Collectors.toList());

private static File checkPreviousOnOsSpecificPlace (final List<String> versionsToCheck) {
String defaultUserdirRoot = System.getProperty ("netbeans.default_userdir_root"); // NOI18N
File sourceFolder;
private static File findPreviousUserDir(final List<String> versionsToCheck) {
String defaultUserdirRoot = System.getProperty("netbeans.default_userdir_root"); // NOI18N
if (defaultUserdirRoot != null) {
File userHomeFile = new File (defaultUserdirRoot);
File userHomeFile = new File(defaultUserdirRoot);
for (String ver : versionsToCheck) {
sourceFolder = new File (userHomeFile.getAbsolutePath (), ver);
if (sourceFolder.exists () && sourceFolder.isDirectory ()) {
File sourceFolder = new File(userHomeFile.getAbsolutePath(), ver);
if (sourceFolder.exists() && sourceFolder.isDirectory()) {
return sourceFolder;
}
}
}
return null;
}

private static boolean madeObsoleteMessagesLog() {
String ud = System.getProperty ("netbeans.user", "");
if ((Utilities.isMac() || Utilities.isWindows()) && ud.endsWith(File.separator + "dev")) { // NOI18N
String defaultUserdirRoot = System.getProperty ("netbeans.default_userdir_root", null); // NOI18N
if (defaultUserdirRoot != null) {
if (new File(ud).getParentFile().equals(new File(defaultUserdirRoot))) {
// check the former default root
String userHome = System.getProperty("user.home"); // NOI18N
if (userHome != null) {
File oldUserdir = new File(new File (userHome).getAbsolutePath (), ".netbeans/dev"); // NOI18N
if (oldUserdir.exists() && ! oldUserdir.equals(new File(ud))) {
// 1. modify messages log
File log = new File (oldUserdir, "/var/log/messages.log");
File obsolete = new File (oldUserdir, "/var/log/messages.log.obsolete");
if (! obsolete.exists() && log.exists()) {
return log.renameTo(obsolete);
}
}
}
}
}
}
return false;
}

private static boolean showUpgradeDialog (final File source, String note) {
private static boolean showUpgradeDialog(final File source) {
Util.setDefaultLookAndFeel();

JPanel panel = new JPanel(new BorderLayout());
panel.add(new AutoUpgradePanel (source.getAbsolutePath (), note), BorderLayout.CENTER);
panel.add(new AutoUpgradePanel(source.getAbsolutePath()), BorderLayout.CENTER);
JProgressBar progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
Expand All @@ -133,73 +96,13 @@ private static boolean showUpgradeDialog (final File source, String note) {
JButton bNO = new JButton("No");
bNO.setMnemonic(KeyEvent.VK_N);
JButton[] options = new JButton[] {bYES, bNO};
JOptionPane p = new JOptionPane (panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null, options, bYES);
JDialog d = Util.createJOptionProgressDialog(p, NbBundle.getMessage (AutoUpgrade.class, "MSG_Confirmation_Title"), source, progressBar);
JOptionPane p = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null, options, bYES);
JDialog d = Util.createJOptionProgressDialog(p, NbBundle.getMessage(AutoUpgrade.class, "MSG_Confirmation_Title"), source, progressBar);
d.setVisible (true);

return Integer.valueOf(JOptionPane.YES_OPTION).equals (p.getValue ());
}

private static void showNoteDialog (String note) {
Util.setDefaultLookAndFeel();
JOptionPane p = new JOptionPane(new AutoUpgradePanel (null, note), JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION);
JDialog d = Util.createJOptionDialog(p, NbBundle.getMessage (AutoUpgrade.class, "MSG_Note_Title"));
d.setVisible (true);
}

static void doUpgrade (File source, String oldVersion)
throws java.io.IOException, java.beans.PropertyVetoException {
File userdir = new File(System.getProperty ("netbeans.user", "")); // NOI18N

java.util.Set<?> includeExclude;
try (Reader r = new InputStreamReader (
AutoUpgrade.class.getResourceAsStream ("copy" + oldVersion), // NOI18N
"utf-8"); // NOI18N
) {
includeExclude = IncludeExclude.create (r);
} catch (IOException ex) {
throw new IOException("Cannot import from version: " + oldVersion, ex);
}

ErrorManager.getDefault ().log (
ErrorManager.USER, "Import: Old version: " // NOI18N
+ oldVersion + ". Importing from " + source + " to " + userdir // NOI18N
);

File oldConfig = new File (source, "config"); // NOI18N
org.openide.filesystems.FileSystem old;
{
LocalFileSystem lfs = new LocalFileSystem ();
lfs.setRootDirectory (oldConfig);

XMLFileSystem xmlfs = null;
try {
URL url = AutoUpgrade.class.getResource("layer" + oldVersion + ".xml"); // NOI18N
xmlfs = (url != null) ? new XMLFileSystem(url) : null;
} catch (SAXException ex) {
throw new IOException("Cannot import from version: " + oldVersion, ex);
}

old = (xmlfs != null) ? createLayeredSystem(lfs, xmlfs) : lfs;
}

Copy.copyDeep (old.getRoot (), FileUtil.getConfigRoot (), includeExclude, PathTransformation.getInstance(oldVersion));

}

static MultiFileSystem createLayeredSystem(final LocalFileSystem lfs, final XMLFileSystem xmlfs) {
MultiFileSystem old;

old = new MultiFileSystem (
new org.openide.filesystems.FileSystem[] { lfs, xmlfs }
) {
{
setPropagateMasks(true);
}
};
return old;
}

/* Copy files from source folder to current userdir according to include/exclude
* patterns in etc/netbeans.import file. */
private static void copyToUserdir(File source) throws IOException, PropertyVetoException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="txtVersions" alignment="0" pref="583" max="32767" attributes="0"/>
<Component id="txtVersions" alignment="0" pref="254" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
Expand All @@ -54,11 +54,11 @@
<SubComponents>
<Component class="javax.swing.JTextArea" name="txtVersions">
<Properties>
<Property name="editable" type="boolean" value="false"/>
<Property name="background" type="java.awt.Color" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<Connection component="Form" name="background" type="property"/>
</Property>
<Property name="columns" type="int" value="50"/>
<Property name="editable" type="boolean" value="false"/>
<Property name="font" postCode="txtVersions.setFont(new java.awt.Font(&quot;Dialog&quot;, 0, 12));"/>
<Property name="lineWrap" type="boolean" value="true"/>
<Property name="rows" type="int" editor="org.netbeans.modules.form.RADConnectionPropertyEditor" postCode="if (source != null) {">
Expand Down
35 changes: 7 additions & 28 deletions nb/o.n.upgrader/src/org/netbeans/upgrade/AutoUpgradePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@

package org.netbeans.upgrade;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.JPanel;
import javax.swing.event.ChangeListener;
import org.openide.util.NbBundle;


Expand All @@ -35,36 +31,21 @@ final class AutoUpgradePanel extends JPanel {
private String source;
private String note;

/** Creates new form UpgradePanel */
public AutoUpgradePanel (String directory, String note) {
public AutoUpgradePanel(String directory) {
this(directory, "");
}

public AutoUpgradePanel(String directory, String note) {
this.source = directory;
this.note = note;
initComponents();
initAccessibility();
}

/** Remove a listener to changes of the panel's validity.
* @param l the listener to remove
*/
void removeChangeListener(ChangeListener l) {
changeListeners.remove(l);
}

/** Add a listener to changes of the panel's validity.
* @param l the listener to add
* @see #isValid
*/
void addChangeListener(ChangeListener l) {
if (!changeListeners.contains(l)) {
changeListeners.add(l);
}
}

private void initAccessibility() {
this.getAccessibleContext().setAccessibleDescription(bundle.getString("MSG_Confirmation")); // NOI18N
this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(AutoUpgradePanel.class, "MSG_Confirmation")); // NOI18N
}


/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
Expand Down Expand Up @@ -94,7 +75,7 @@ private void initComponents() {
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtVersions, javax.swing.GroupLayout.DEFAULT_SIZE, 583, Short.MAX_VALUE)
.addComponent(txtVersions, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
Expand All @@ -106,7 +87,5 @@ private void initComponents() {
private javax.swing.JTextArea txtVersions;
// End of variables declaration//GEN-END:variables

private static final ResourceBundle bundle = NbBundle.getBundle(AutoUpgradePanel.class);
private List<ChangeListener> changeListeners = new ArrayList<ChangeListener>(1);

}
4 changes: 0 additions & 4 deletions nb/o.n.upgrader/src/org/netbeans/upgrade/Bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,4 @@ MSG_Confirmation_Title = Confirm Import Settings
MSG_ImportingSettings = Importing Settings...
MSG_MigratingSystemOptions = Migrating System Options...

MSG_Note_Title=Note
MSG_ChangedDefaults=Note: The default location of NetBeans userdir was changed to {0}\n\
See http://wiki.netbeans.org/UserdirAndCachedirFoldersInSystemSpecificPaths\n\n

apachenetbeanspreviousversion=@@metabuild.apachepreviousversion@@
7 changes: 5 additions & 2 deletions nb/o.n.upgrader/src/org/netbeans/upgrade/ColoringStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ static Map loadColorings (
}

private static class ColoringsReader extends XMLStorage.Handler {
private Map<String, SimpleAttributeSet> colorings = new HashMap<String, SimpleAttributeSet> ();
private final Map<String, SimpleAttributeSet> colorings = new HashMap<> ();
private SimpleAttributeSet last;

@Override
Object getResult () {
return colorings;
}

@Override
public void startElement (
String uri,
String localName,
Expand Down Expand Up @@ -153,6 +155,7 @@ public void startElement (
}
}

@Override
public InputSource resolveEntity (String pubid, String sysid) {
return new InputSource (
new java.io.ByteArrayInputStream (new byte [0])
Expand Down Expand Up @@ -264,7 +267,7 @@ private static String getFolderName (
String[] mimeTypes,
String profile
) {
StringBuffer sb = new StringBuffer ();
StringBuilder sb = new StringBuilder ();
sb.append ("Editors");
int i, k = mimeTypes.length;
for (i = 0; i < k; i++)
Expand Down
Loading

0 comments on commit 3be92f1

Please sign in to comment.