You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

343 lines
11 KiB
Java

/* Copyright (C) Edward M. Kagan - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by Edward M. Kagan <pagan@idp-crew.com>, 2015
*/
package org.idp.laf;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.metal.DefaultMetalTheme;
import javax.swing.plaf.metal.MetalLookAndFeel;
import org.openide.util.Exceptions;
import org.openide.windows.WindowManager;
/**
* @author Edward M. Kagan <pagan@idp-crew.com>
*/
public class Kernel {
private static final String scheme_file_name = "netbeans.idp_scheme";
public static void load_kernel ()
{
if (prepare_metal_laf ())
{
String nb_etc = retrieve_netbeans_etc_dir();
setup_font_antialiaing_hints(nb_etc);
String cfh_dir = retrieve_app_folder();
boolean a = (new File (cfh_dir).mkdirs());
load_setup (cfh_dir);
}
else
{
System.err.println("Unable to set Metall Look And Feel - no "
+ "modifications done. Follow your ugly way of live.");
}
}
private static String retrieve_app_folder ()
{
if (System.getProperty("os.name").startsWith("Windows")) {
return System.getenv("APPDATA") + File.separator + "idp_scheme";
} else {
return retrieve_netbeans_etc_dir ();
}
}
private static boolean prepare_metal_laf ()
{
try {
UIManager.getDefaults().clear();
MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
return true;
} catch (ClassNotFoundException ex) {
Exceptions.printStackTrace(ex);
return false;
} catch (InstantiationException ex) {
Exceptions.printStackTrace(ex);
return false;
} catch (IllegalAccessException ex) {
Exceptions.printStackTrace(ex);
return false;
} catch (UnsupportedLookAndFeelException ex) {
Exceptions.printStackTrace(ex);
return false;
}
}
private static String retrieve_netbeans_etc_dir ()
{
String nb_home = System.getProperty("netbeans.home");
String nb_conf = nb_home.substring(0, nb_home.length() - "platform".length()) + "etc" + File.separator;
return nb_conf;
}
private static boolean setup_font_antialiaing_hints (String nb_etc)
{
try
{
System.setProperty("awt.useSystemAAFontSettings","lcd");
System.setProperty("swing.aatext", "true");
System.setProperty("nb.useSwingHtmlRendering", "true");
Path path = FileSystems.getDefault().getPath(nb_etc, "netbeans.conf");
if( Files.isWritable(path))
{
List<String> linesL = Files.readAllLines(path);
String [] lines = new String[linesL.size()];
int i = 0;
for (String str : linesL)
{
lines[i] = str;
i++;
}
for (i =0; i < lines.length; i ++)
{
if (lines[i].contains("netbeans_default_options="))
{
if (!lines[i].contains("-J-Dawt.useSystemAAFontSettings"))
{
System.err.println("No rendering hints defined - fixing...");
String [] parts = lines[i].split("\"");
String new_line = parts[0] + "\"" + parts[1] + " -J-Dswing.aatext=true -J-Dawt.useSystemAAFontSettings=lcd" + "\"";
lines[i] = new_line;
write_scheme(nb_etc + "netbeans.conf", lines);
System.out.println("Restart Netbeabs to see the effect.");
}
}
}
return true;
}
else
{
System.err.println("Unable to fix config - bye-bye :(");
return false;
}
}
catch (IOException ex)
{
Exceptions.printStackTrace(ex);
System.err.println("Unable to apply rendering hints :( Sorry...");
return false;
}
}
private static boolean load_setup (String nb_etc)
{
File scheme_file = new File(nb_etc + File.separator + scheme_file_name);
String path = scheme_file.getAbsolutePath();
if (!scheme_file.exists())
{
init_scheme();
if (save_scheme(path))
{
return read_scheme (path);
}
else
{
return false;
}
}
else
{
return read_scheme (path);
}
}
private static Random rand = new Random(System.currentTimeMillis());
public static void save ()
{
String nb_etc = retrieve_app_folder();
File scheme_file = new File(nb_etc + File.separator + scheme_file_name);
String path = scheme_file.getAbsolutePath();
save_scheme (path);
}
public static void reload ()
{
String nb_etc = retrieve_app_folder();
File scheme_file = new File(nb_etc + File.separator + scheme_file_name);
String path = scheme_file.getAbsolutePath();
read_scheme (path);
}
public static void load (String path)
{
read_scheme (path);
}
private static void init_scheme ()
{
ArrayList<Color> color_map = new ArrayList<Color>();
for (int i = 0; i < Keys.color_keys.length; i ++)
{
Object o = UIManager.get(Keys.color_keys[i]);
if (o != null)
{
if (o.getClass().equals(javax.swing.plaf.ColorUIResource.class))
{
ColorUIResource oo = (ColorUIResource) o;
Color c = new Color(Keys.color_keys[i], Color.ColorClass.SF, oo.getRed(), oo.getGreen(), oo.getBlue(), oo.getAlpha(),"...");
color_map.add(c);
//System.out.println( "\"" + Keys.color_keys[i] + "\",");
}
else if (o.getClass().equals(java.awt.Color.class))
{
java.awt.Color oo = (java.awt.Color) o;
Color c = new Color(Keys.color_keys[i], Color.ColorClass.AT, oo.getRed(), oo.getGreen(), oo.getBlue(), oo.getAlpha(),"...");
color_map.add(c);
//System.out.println( "\"" + Keys.color_keys[i] + "\",");
}
}
else
{
ColorUIResource oo = new ColorUIResource(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
Color c = new Color(Keys.color_keys[i], Color.ColorClass.SF, oo.getRed(), oo.getGreen(), oo.getBlue(), oo.getAlpha(),"...");
color_map.add(c);
//System.out.println( " \"" + Keys.color_keys[i] + "\",");
}
}
color_scheme_loaded = new Color[color_map.size()];
color_map.toArray(color_scheme_loaded);
}
private static boolean save_scheme(String scheme_file) {
Path path = FileSystems.getDefault().getPath(scheme_file);
try
{
PrintWriter writer = new PrintWriter(scheme_file , "UTF-8");
for (int i = 0; i < color_scheme_loaded.length; i ++)
{
writer.println(color_scheme_loaded[i].toStringC());
}
writer.close();
return true;
}
catch (FileNotFoundException ex)
{
Exceptions.printStackTrace(ex);
return false;
}
catch (UnsupportedEncodingException ex)
{
Exceptions.printStackTrace(ex);
return false;
}
//}
// else
// {
// JOptionPane.showMessageDialog(null, "Unable to write '" + scheme_file + "'" );
// return false;
// }
}
public static Color[] color_scheme_loaded;
private static boolean read_scheme(String scheme_file) {
try
{
ArrayList<Color> color_map = new ArrayList<Color>();
List<String> C = Files.readAllLines(Paths.get(scheme_file), Charset.forName("UTF-8"));
for (String s : C)
{
color_map.add(new Color (s));
}
color_scheme_loaded = new Color[color_map.size()];
color_map.toArray(color_scheme_loaded);
reloadUI();
return true;
}
catch (IOException ex)
{
Exceptions.printStackTrace(ex);
return false;
}
}
public static void reloadUI()
{
for (int i = 0; i < color_scheme_loaded.length; i ++)
{
UIManager.put(color_scheme_loaded[i].getName(), color_scheme_loaded[i].getObject());
}
repaintUI();
}
private static void repaintUI()
{
SwingUtilities.updateComponentTreeUI(WindowManager.getDefault().getMainWindow());
WindowManager.getDefault().getMainWindow().pack();
WindowManager.getDefault().getMainWindow().repaint();
}
private static void write_scheme(String path, String [] data) throws IOException
{
File fout = new File(path);
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
for (int i = 0; i < data.length; i ++)
{
bw.write(data[i]);
bw.newLine();
}
bw.close();
}
public static Color[] getColors() {
if (color_scheme_loaded == null)
{
return new Color[0];
}
else
{
return color_scheme_loaded;
}
}
}