Commit 75d33d19 by Jessica Hawkwell

added proper logging, menutabs

close #1, close #3
1 parent a682c75d
Showing with 758 additions and 186 deletions
......@@ -16,6 +16,17 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
<relativePath>../..</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
......
......@@ -27,11 +27,13 @@ import me.felinewith.lang_toolkit.apps.bootstrap.util.MavenArtifact;
public class Bootstrap {
private static String PROPERTIES = "META-INF/maven/me.felinewith.lang-toolkit.apps/bootstrap/pom.properties";
private static String SETTINGS;
private static Logger log;
public static void main(String[] args) throws IOException {
StringBuilder sb = new StringBuilder(System.getProperty("user.home"));
sb.append("/.local/share/lang-toolkit/");
SETTINGS = sb.toString();
log = Logger.getGlobal();
File folder = new File(SETTINGS);
if ( !folder.exists() ) { folder.mkdirs(); }
......@@ -127,7 +129,7 @@ public class Bootstrap {
pt = ze.getName().replaceFirst("[^/]+", "");
f = new File(mvn_folder.getCanonicalPath().concat(pt));
System.out.println(pt);
log.info(pt);
if ( ze.isDirectory() ) {
f.mkdirs();
}
......@@ -249,8 +251,7 @@ public class Bootstrap {
}
private static void exec(File pwd, String command) throws IOException, InterruptedException {
System.out.print("[shell]\t");
System.out.println(command);
log.info("[shell]\t".concat(command));
ProcessBuilder pb = new ProcessBuilder();
//pb.inheritIO();
......@@ -264,7 +265,7 @@ public class Bootstrap {
Thread t = new Thread() {
@Override@SuppressWarnings("unchecked")
public void run() {
System.out.println("Booting...");
log.info("Booting...");
Class<?> cls = null;
try { cls = bcl.loadClass("me.felinewith.lang_toolkit.apps.bootstrap.BootstrapApp"); }
catch (ClassNotFoundException ex) {
......
......@@ -6,8 +6,8 @@ import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import me.felinewith.lang_toolkit.apps.bootstrap.util.LoaderHelper;
/**
......@@ -18,16 +18,18 @@ public class BootstrapApp {
private ClassLoader cl;
String[] args;
static Logger log;
public static void init(String[] args) {
log = LogManager.getLogger();
ClassLoader cl = BootstrapApp.class.getClassLoader();
System.out.print("ClassLoader: ");
System.out.println(cl.toString());
log.info("ClassLoader: ".concat(cl.getClass().getCanonicalName()));
// fetch all available bootstrap descriptors
Enumeration<URL> urls = null;
try { urls = cl.getResources("bootstrap/bootstrap.properties"); }
catch (IOException ex) { Logger.getLogger(BootstrapApp.class.getName()).log(Level.SEVERE, null, ex); }
catch (IOException ex) { log.fatal(ex.getMessage(), ex); }
Properties props = new Properties();
URL u;
......@@ -38,11 +40,10 @@ public class BootstrapApp {
try { props.load(u.openStream()); }
catch (IOException ex) {
Logger.getLogger(BootstrapApp.class.getName()).log(Level.WARNING, null, ex);
log.warn(ex.getMessage(), ex);
}
}
props.list(System.out);
LoaderHelper<IStrappedApp> appHelper = new LoaderHelper<>(cl, IStrappedApp.class);
LoaderHelper<IStrappedPlugin> plugHelper = new LoaderHelper<>(cl, IStrappedPlugin.class);
......@@ -55,19 +56,17 @@ public class BootstrapApp {
try {
app = appHelper.fetchClass(props.getProperty("app"));
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
Logger.getLogger(BootstrapApp.class.getName()).log(Level.SEVERE, null, ex);
log.fatal(ex.getMessage(), ex);
return;
}
}
else {
Logger.getLogger(BootstrapApp.class.getName()).log(Level.SEVERE, null,
new NoClassDefFoundError("Application did not provide an application class."));
log.fatal("Class not found.", new NoClassDefFoundError("Application did not provide an application class."));
return;
}
if ( app == null ) {
Logger.getLogger(BootstrapApp.class.getName()).log(Level.SEVERE, null,
new ClassNotFoundException("Application class could not be loaded."));
log.fatal("Can not load!", new ClassNotFoundException("Application class could not be loaded."));
return;
}
......@@ -79,7 +78,7 @@ public class BootstrapApp {
if ( !name.equals("app") ) {
try { plug = plugHelper.fetchClass(props.getProperty(name)); }
catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
Logger.getLogger(BootstrapApp.class.getName()).log(Level.WARNING, null, ex);
log.warn(ex.getMessage(), ex);
}
if ( plug != null ) { plugins.put(plug.getName(), plug); }
......
......@@ -60,6 +60,8 @@ public class BootstrapClassLoader extends ClassLoader
*/
private final ClassLoader parent;
private Logger log;
/**
* Constructor.
*/
......@@ -71,6 +73,7 @@ public class BootstrapClassLoader extends ClassLoader
pkgs = new HashMap<>();
clss = new HashMap<>();
maps = new HashMap<>();
log = Logger.getGlobal();
}
/**
......@@ -85,6 +88,7 @@ public class BootstrapClassLoader extends ClassLoader
pkgs = new HashMap<>();
clss = new HashMap<>();
maps = new HashMap<>();
log = Logger.getGlobal();
}
/** Initialize this ClassLoader.
......@@ -331,7 +335,7 @@ public class BootstrapClassLoader extends ClassLoader
if ( mf.getMainAttributes().containsKey(Name.CLASS_PATH) )
{ addClassPath(mf.getMainAttributes().getValue(Name.CLASS_PATH)); }
addDirHelper(dir, null, "", mf);
System.out.println("Added Dir ".concat(pt));
log.info("Added Dir ".concat(pt));
}
catch (IOException x) {}
}
......@@ -396,7 +400,7 @@ public class BootstrapClassLoader extends ClassLoader
defPkg(tp, jf.getName(), msf, je.getAttributes());
}
}
System.out.println("Added Jar ".concat(pt));
log.info("Added Jar ".concat(pt));
}
catch ( IOException x ) {}
}
......
package me.felinewith.lang_toolkit.apps.bootstrap.util;
import java.net.URI;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Order;
import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
import org.apache.logging.log4j.core.config.plugins.Plugin;
/**
*
* @author jlhawkwell
*/
@Plugin(name = "CustomConfigurationFactory", category = ConfigurationFactory.CATEGORY)
@Order(50)
public class Log4jConfig extends ConfigurationFactory {
public static Configuration createConfiguration(final String name, ConfigurationBuilder<BuiltConfiguration> builder) {
builder.setConfigurationName(name);
builder.setStatusLevel(Level.DEBUG);
builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL).
addAttribute("level", Level.DEBUG));
AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").
addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT);
appenderBuilder.add(builder.newLayout("PatternLayout").
addAttribute("pattern", "%d{UNIX_MILLIS} [%style{%t}{cyan}/%style{%c{1}}{yellow}] %highlight{%-5level}: %msg%n%throwable"));
appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.DENY,
Filter.Result.NEUTRAL).addAttribute("marker", "FLOW"));
builder.add(appenderBuilder);
builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG).
add(builder.newAppenderRef("Stdout")).
addAttribute("additivity", false));
builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout")));
return builder.build();
}
@Override protected String[] getSupportedTypes() { return new String[]{"*"}; }
@Override public Configuration getConfiguration(LoggerContext lc, ConfigurationSource cs) {
return getConfiguration(lc, cs.toString(), null); }
@Override public Configuration getConfiguration(LoggerContext loggerContext, String name, URI configLocation) {
ConfigurationBuilder<BuiltConfiguration> builder = newConfigurationBuilder();
return createConfiguration(name, builder);
}
}
......@@ -22,6 +22,10 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
<artifactId>commons-configuration2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.freedesktop.tango</groupId>
<artifactId>tango-icon-theme</artifactId>
</dependency>
......
package me.felinewith.lang_toolkit.apps.langbuilder;
import me.felinewith.lang_toolkit.apps.langbuilder.window.WindowAdapterListener;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import me.felinewith.lang_toolkit.apps.bootstrap.IStrappedApp;
import me.felinewith.lang_toolkit.apps.bootstrap.IStrappedPlugin;
import me.felinewith.lang_toolkit.apps.langbuilder.actions.ListenAttach;
import me.felinewith.lang_toolkit.apps.langbuilder.util.ConfigHelper;
import me.felinewith.lang_toolkit.apps.langbuilder.window.WindowComponentListener;
import me.felinewith.lang_toolkit.apps.langbuilder.window.WindowPropertyChangeListener;
import me.felinewith.lang_toolkit.apps.langbuilder.util.DesktopMenu;
import me.felinewith.lang_toolkit.apps.langbuilder.window.WindowUnifiedListener;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.XMLConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
*
......@@ -35,21 +44,29 @@ public class LangBuilder implements IStrappedApp {
private HashMap<String, IStrappedPlugin> plugins;
private JFrame jFrame;
private JTabbedPane jTabbedPane;
private JSplitPane jSplitPane;
private JScrollPane jScrollPane;
private JTree jTree;
private JDesktopPane jDesktopPane;
private Configuration config;
private WindowAdapterListener adapterListener;
private WindowComponentListener componentListener;
private WindowPropertyChangeListener propertyChangeListener;
private Logger log;
WindowUnifiedListener unifiedListener;
@Override public void preStart(String[] argsStrings) {
args = argsStrings;
log = LogManager.getLogger();
try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); }
catch (ClassNotFoundException | InstantiationException | IllegalAccessException |
UnsupportedLookAndFeelException ex) {
}
// configuration!
System.out.println("[Desktop] Setting up configuration...");
log.info("[Desktop] Setting up configuration...");
StringBuilder sb = new StringBuilder(System.getProperty("user.home"));
sb.append("/.local/share/lang-toolkit/config/desktop.xml");
File f = new File(sb.toString());
......@@ -62,7 +79,7 @@ public class LangBuilder implements IStrappedApp {
params.xml().setValidating(false);
FileBasedConfigurationBuilder<XMLConfiguration> builder =
new FileBasedConfigurationBuilder<XMLConfiguration>(XMLConfiguration.class);
new FileBasedConfigurationBuilder<>(XMLConfiguration.class);
builder.configure(params.xml());
if ( f.exists() ) {
......@@ -74,20 +91,40 @@ public class LangBuilder implements IStrappedApp {
config = null;
try { config = new ConfigHelper(builder.getConfiguration(), builder); }
catch (ConfigurationException ex)
{ Logger.getLogger(LangBuilder.class.getName()).log(Level.SEVERE, "Config setup FAILED!", ex); }
{ log.fatal("Config setup FAILED!", ex); }
if ( !f.exists() ) {
if ( !builder.getFileHandler().isLocationDefined() )
{ builder.getFileHandler().setFileName(sb.toString()); }
}
//builder.setAutoSave(true);
System.out.println("[Desktop] Setting up window...");
log.info("[Desktop] Setting up window...");
plugins = new HashMap<>();
jFrame = new JFrame("LangBuilder");
jFrame.setLayout(new BorderLayout());
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
unifiedListener = new WindowUnifiedListener(jFrame, config);
// TODO: toolbar menu
jTabbedPane = new JTabbedPane(JTabbedPane.TOP);
jTabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
JToolBar toolBar = new JToolBar("LangBuilder Desktop", JToolBar.HORIZONTAL);
toolBar.setFloatable(false);
DesktopMenu tdm = new DesktopMenu(toolBar, unifiedListener);
tdm.addButton("New", "new", null, "16x16/actions/document-new.png");
tdm.addButton("Load", "load", null, "16x16/actions/document-open.png");
tdm.addButton("Save", "save", null, "16x16/actions/document-save.png");
tdm.addButton("Save As", "saveas", null, "16x16/actions/document-save-as.png");
tdm.addButton("Exit", "exit", null, "16x16/actions/system-log-out.png");
URL u = this.getClass().getResource("/org/freedesktop/tango/16x16/places/user-desktop.png");
jTabbedPane.addTab("LangBuilder Desktop", new ImageIcon(u), toolBar);
jFrame.add(jTabbedPane, BorderLayout.PAGE_START);
jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
jSplitPane.setContinuousLayout(true);
......@@ -99,10 +136,11 @@ public class LangBuilder implements IStrappedApp {
jSplitPane.setLeftComponent(jScrollPane);
jSplitPane.setRightComponent(jDesktopPane);
jFrame.add(jSplitPane);
jFrame.add(jSplitPane, BorderLayout.CENTER);
// set component names
jFrame.setName("window");
jTabbedPane.setName("window.menu");
jSplitPane.setName("window.splitter");
jTree.setName("window.tree");
jDesktopPane.setName("window.desktop");
......@@ -110,7 +148,7 @@ public class LangBuilder implements IStrappedApp {
// configure the window
// set some sensible defaults
System.out.println("[Desktop] Detecting screen sizes...");
log.info("[Desktop] Detecting screen sizes...");
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
DisplayMode dm;
int sx = 0, sy = 0;
......@@ -139,25 +177,34 @@ public class LangBuilder implements IStrappedApp {
config.getInt("window.preferredSize.y", sy / 2)
)
);
jFrame.setLocation(
new Point(
config.getInt("window.location.x", sx / 8),
config.getInt("window.location.y", sy / 8)
)
);
jSplitPane.setDividerLocation(config.getInt("window.splitter.dividerLocation", sx / 8));
adapterListener = new WindowAdapterListener(this, config);
jFrame.addWindowListener(adapterListener);
jFrame.addWindowStateListener(adapterListener);
jFrame.addWindowFocusListener(adapterListener);
Component[] comps = {jFrame, jTabbedPane, jSplitPane, jTree, jDesktopPane};
componentListener = new WindowComponentListener(this, config);
jFrame.addComponentListener(componentListener);
jSplitPane.addComponentListener(componentListener);
jTree.addComponentListener(componentListener);
jDesktopPane.addComponentListener(componentListener);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for ( Component c : comps ) { ListenAttach.attach(c, unifiedListener, "addWindowFocusListener"); }
/*jFrame.addWindowListener(unifiedListener);
jFrame.addWindowStateListener(unifiedListener);
jTabbedPane.addChangeListener(unifiedListener);
jFrame.addComponentListener(unifiedListener);
jTabbedPane.addComponentListener(unifiedListener);
jSplitPane.addComponentListener(unifiedListener);
jTree.addComponentListener(unifiedListener);
jDesktopPane.addComponentListener(unifiedListener);
propertyChangeListener = new WindowPropertyChangeListener(this, config);
jFrame.addPropertyChangeListener(propertyChangeListener);
jSplitPane.addPropertyChangeListener(propertyChangeListener);
jTree.addPropertyChangeListener(propertyChangeListener);
jDesktopPane.addPropertyChangeListener(propertyChangeListener);
jFrame.addPropertyChangeListener(unifiedListener);
jTabbedPane.addPropertyChangeListener(unifiedListener);
jSplitPane.addPropertyChangeListener(unifiedListener);
jTree.addPropertyChangeListener(unifiedListener);
jDesktopPane.addPropertyChangeListener(unifiedListener); // */
}
......
package me.felinewith.lang_toolkit.apps.langbuilder.actions;
/**
*
* @author jlhawkwell
*/
public abstract class ActionAccept {
public void handleAction(ActionHandle action) { handleEvent(action); }
public void handleChange(ActionHandle action) { handleEvent(action); }
public void handleComponent(ActionHandle action) { handleEvent(action); }
public void handleProperty(ActionHandle action) { handleEvent(action); }
public void handleWindow(ActionHandle action) { handleEvent(action); }
public abstract void handleEvent(ActionHandle action);
}
package me.felinewith.lang_toolkit.apps.langbuilder.actions;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import javax.swing.event.ChangeEvent;
/**
*
* @author jlhawkwell
*/
public class ActionHandle {
private Object source;
private String name;
private int intval;
private Object obval;
private ActionSource as;
public ActionHandle(ActionEvent actionEvent) {
source = actionEvent.getSource();
name = actionEvent.getActionCommand();
intval = actionEvent.getModifiers();
obval = source;
as = ActionSource.EVENT_ACTION;
}
public ActionHandle(ChangeEvent changeEvent) {
source = changeEvent.getSource();
name = changeEvent.toString();
intval = 0;
obval = source;
as = ActionSource.EVENT_CHANGE;
}
public ActionHandle(ComponentEvent componentEvent) {
source = componentEvent.getComponent();
name = componentEvent.paramString();
intval = componentEvent.getID();
obval = source;
as = ActionSource.EVENT_COMPONENT;
}
public ActionHandle(PropertyChangeEvent propertyChangeEvent) {
source = propertyChangeEvent.getSource();
name = propertyChangeEvent.getPropertyName();
intval = 0;
obval = propertyChangeEvent.getNewValue();
as = ActionSource.EVENT_PROPERTY;
}
public ActionHandle(WindowEvent windowEvent) {
source = windowEvent.getWindow();
name = windowEvent.paramString();
intval = windowEvent.getID();
obval = windowEvent.getOppositeWindow();
as = ActionSource.EVENT_WINDOW;
}
public Object getSource() { return source; }
public String getName() { return name; }
public int getIntValue() { return intval; }
public Object getObjectValue() { return obval; }
public ActionSource getActionSource() { return as; }
public ActionTypes getActionType() { return ActionTypes.valueOf(intval); }
}
package me.felinewith.lang_toolkit.apps.langbuilder.actions;
/**
*
* @author jlhawkwell
*/
public enum ActionSource {
EVENT_ACTION (true, true , true , false),
EVENT_CHANGE (true, false, false, false),
EVENT_COMPONENT(true, false, true , false),
EVENT_PROPERTY (true, true , false, true ),
EVENT_WINDOW (true, true , true , true );
private boolean source;
private boolean name;
private boolean intval;
private boolean obval;
private ActionSource(boolean source, boolean name, boolean intval, boolean obval) {
this.source = source;
this.name = name;
this.intval = intval;
this.obval = obval;
}
public boolean getSource() { return source; }
public boolean getName() { return name; }
public boolean getIntValue() { return intval; }
public boolean getObjectValue() { return obval; }
}
package me.felinewith.lang_toolkit.apps.langbuilder.actions;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowEvent;
/**
*
* @author jlhawkwell
*/
public enum ActionTypes {
UNKNOWN(-1),
// ActionEvent/ActionListener
ACTION_SHIFT(TypeAction.SHIFT),
ACTION_CONTROL(TypeAction.CONTROL),
ACTION_META(TypeAction.META),
ACTION_ALT(TypeAction.ALT),
// ChangeEvent/ChangeListener
CHANGE(0),
// ComponentEvent/ComponentListener
COMPONENT_MOVED(TypeComponent.MOVED), // 100
COMPONENT_RESIZED(TypeComponent.RESIZED), // 101
COMPONENT_SHOWN(TypeComponent.SHOWN), // 102
COMPONENT_HIDDEN(TypeComponent.HIDDEN), // 103
// PropertyChangeEvent/PropertyChangeListener
PROPERTY(0),
// WindowEvent/WindowAdapter
WINDOW_OPENED(TypeWindow.OPENED), //200
WINDOW_CLOSING(TypeWindow.CLOSING), // 201
WINDOW_CLOSED(TypeWindow.CLOSED), // 202
WINDOW_ICONIFIED(TypeWindow.ICONIFIED), //203
WINDOW_DEICONIFIED(TypeWindow.DEICONIFIED), // 204
WINDOW_ACTIVATED(TypeWindow.ACTIVATED), // 205
WINDOW_DEACTIVATED(TypeWindow.DEACTIVATED), // 206
WINDOW_GAINED_FOCUS(TypeWindow.GAINED_FOCUS), // 207
WINDOW_LOST_FOCUS(TypeWindow.LOST_FOCUS), // 208
WINDOW_STATE_CHANGED(TypeWindow.STATE_CHANGED) // 209
;
public enum TypeAction implements IntEnum {
SHIFT(ActionEvent.SHIFT_MASK), // 1
CONTROL(ActionEvent.CTRL_MASK), // 2
META(ActionEvent.META_MASK), // 4
ALT(ActionEvent.ALT_MASK); // 8
private int i;
private TypeAction(int value) { i = value; }
@Override public int getValue() { return i; }
}
public enum TypeComponent implements IntEnum {
MOVED(ComponentEvent.COMPONENT_MOVED), // 100
RESIZED(ComponentEvent.COMPONENT_RESIZED), // 101
SHOWN(ComponentEvent.COMPONENT_SHOWN), // 102
HIDDEN(ComponentEvent.COMPONENT_HIDDEN); // 103
private int i;
private TypeComponent(int value) { i = value; }
@Override public int getValue() { return i; }
}
public enum TypeWindow implements IntEnum {
OPENED(WindowEvent.WINDOW_OPENED), //200
CLOSING(WindowEvent.WINDOW_CLOSING), // 201
CLOSED(WindowEvent.WINDOW_CLOSED), // 202
ICONIFIED(WindowEvent.WINDOW_ICONIFIED), //203
DEICONIFIED(WindowEvent.WINDOW_DEICONIFIED), // 204
ACTIVATED(WindowEvent.WINDOW_ACTIVATED), // 205
DEACTIVATED(WindowEvent.WINDOW_DEACTIVATED), // 206
GAINED_FOCUS(WindowEvent.WINDOW_GAINED_FOCUS), // 207
LOST_FOCUS(WindowEvent.WINDOW_LOST_FOCUS), // 208
STATE_CHANGED(WindowEvent.WINDOW_STATE_CHANGED); // 209
private int i;
private TypeWindow(int value) { i = value; }
@Override public int getValue() { return i; }
}
private int i;
private ActionTypes(int value) { i = value; }
private ActionTypes(IntEnum v) { i = v.getValue(); }
public int getValue() { return i; }
public static ActionTypes valueOf(int value) {
for ( ActionTypes at : ActionTypes.values() ) {
if ( at.i == value ) { return at; }
}
return ActionTypes.UNKNOWN;
}
private interface IntEnum {
public int getValue();
}
}
package me.felinewith.lang_toolkit.apps.langbuilder.actions;
import java.awt.Component;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
*
* @author jlhawkwell
*/
public class ListenAttach {
private static ArrayList<Class<?>> types;
private static Logger log;
public static void attach(Component comp, UnifiedListener listener, String... excludeMembers) {
if ( types == null ) {
Class<?>[] ts = UnifiedListener.class.getInterfaces();
types = new ArrayList<>();
types.addAll(Arrays.asList(ts));
}
if ( log == null ) { log = LogManager.getLogger(); }
ArrayList<String> exList = new ArrayList<>();
if ( (excludeMembers != null) && (excludeMembers.length != 0) ) {
exList.addAll(Arrays.asList(excludeMembers));
}
StringBuilder sb;
Class<?>[] params;
Method[] methods = comp.getClass().getMethods();
for ( Method m : methods ) {
if ( m.getName().startsWith("add") && (m.getParameterCount() == 1) ) {
params = m.getParameterTypes();
if ( types.contains(params[0]) ) {
if ( !exList.contains(m.getName()) ) {
try { m.invoke(comp, listener); }
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
sb = new StringBuilder(comp.getClass().getSimpleName());
sb.append(".");
sb.append(m.getName().concat("("));
sb.append(params[0]);
sb.append(")\n\tFAILED: ");
sb.append(ex);
log.warn(sb.toString());
}
}
}
}
}
}
}
package me.felinewith.lang_toolkit.apps.langbuilder.actions;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
*
* @author jlhawkwell
*/
public abstract class UnifiedListener
extends ActionAccept
implements ActionListener, ChangeListener, ComponentListener, PropertyChangeListener,
WindowFocusListener , WindowListener, WindowStateListener {
@Override final public void actionPerformed(ActionEvent e) { handleAction(new ActionHandle(e)); }
@Override final public void stateChanged(ChangeEvent e) { handleChange(new ActionHandle(e)); }
@Override final public void componentHidden(ComponentEvent e) { handleComponent(new ActionHandle(e)); }
@Override final public void componentMoved(ComponentEvent e) { handleComponent(new ActionHandle(e)); }
@Override final public void componentResized(ComponentEvent e) { handleComponent(new ActionHandle(e)); }
@Override final public void componentShown(ComponentEvent e) { handleComponent(new ActionHandle(e)); }
@Override final public void propertyChange(PropertyChangeEvent e) { handleProperty(new ActionHandle(e)); }
@Override final public void windowActivated(WindowEvent e) { handleWindow(new ActionHandle(e)); }
@Override final public void windowClosed(WindowEvent e) { handleWindow(new ActionHandle(e)); }
@Override final public void windowClosing(WindowEvent e) { handleWindow(new ActionHandle(e)); }
@Override final public void windowDeactivated(WindowEvent e) { handleWindow(new ActionHandle(e)); }
@Override final public void windowDeiconified(WindowEvent e) { handleWindow(new ActionHandle(e)); }
@Override final public void windowGainedFocus(WindowEvent e) { handleWindow(new ActionHandle(e)); }
@Override final public void windowIconified(WindowEvent e) { handleWindow(new ActionHandle(e)); }
@Override final public void windowLostFocus(WindowEvent e) { handleWindow(new ActionHandle(e)); }
@Override final public void windowOpened(WindowEvent e) { handleWindow(new ActionHandle(e)); }
@Override final public void windowStateChanged(WindowEvent e) { handleWindow(new ActionHandle(e)); }
}
package me.felinewith.lang_toolkit.apps.langbuilder.actions;
import java.util.ArrayList;
import java.util.function.Consumer;
/**
*
* @author jlhawkwell
*/
public final class UnifiedRepeater
extends UnifiedListener {
//implements ActionListener, ChangeListener, ComponentListener, PropertyChangeListener, WindowListener {
private ArrayList<ActionAccept> list;
public UnifiedRepeater() {
list = new ArrayList<>();
}
public void addActionAccept(ActionAccept actionAccept) { list.add(actionAccept); }
public void removeActionAccept(ActionAccept actionAccept) { list.remove(actionAccept); }
public void clear() { list.clear(); }
private void exec(Consumer<ActionAccept> con) {
if ( list.isEmpty() ) { return; }
list.forEach(con);
}
@Override public void handleAction(ActionHandle action) { exec(new LooperAction(action)); }
@Override public void handleChange(ActionHandle action) { exec(new LooperChange(action)); }
@Override public void handleComponent(ActionHandle action) { exec(new LooperComponent(action)); }
@Override public void handleProperty(ActionHandle action) { exec(new LooperProperty(action)); }
@Override public void handleWindow(ActionHandle action) { exec(new LooperWindow(action)); }
@Override public void handleEvent(ActionHandle action) { }
private abstract class Looper implements Consumer<ActionAccept> {
ActionHandle handle;
public Looper(ActionHandle actionHandle) { handle = actionHandle; }
}
private class LooperAction extends Looper {
public LooperAction(ActionHandle actionHandle) { super(actionHandle); }
@Override public void accept(ActionAccept t) { t.handleAction(handle); }
}
private class LooperChange extends Looper {
public LooperChange(ActionHandle actionHandle) { super(actionHandle); }
@Override public void accept(ActionAccept t) { t.handleChange(handle); }
}
private class LooperComponent extends Looper {
public LooperComponent(ActionHandle actionHandle) { super(actionHandle); }
@Override public void accept(ActionAccept t) { t.handleComponent(handle); }
}
private class LooperProperty extends Looper {
public LooperProperty(ActionHandle actionHandle) { super(actionHandle); }
@Override public void accept(ActionAccept t) { t.handleProperty(handle); }
}
private class LooperWindow extends Looper {
public LooperWindow(ActionHandle actionHandle) { super(actionHandle); }
@Override public void accept(ActionAccept t) { t.handleWindow(handle); }
}
}
......@@ -9,8 +9,6 @@ import java.util.Map;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.ConfigurationDecoder;
import org.apache.commons.configuration2.ImmutableConfiguration;
......@@ -20,6 +18,8 @@ import org.apache.commons.configuration2.interpol.ConfigurationInterpolator;
import org.apache.commons.configuration2.interpol.Lookup;
import org.apache.commons.configuration2.sync.LockMode;
import org.apache.commons.configuration2.sync.Synchronizer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
*
......@@ -30,12 +30,14 @@ public class ConfigHelper implements Configuration {
Configuration config;
FileBasedConfigurationBuilder<?> builder;
Timer saver;
Logger log;
public ConfigHelper(Configuration configuration, FileBasedConfigurationBuilder<?> configBuilder) {
if ( configuration == null ) { throw new NullPointerException("Should never be null!"); }
if ( configBuilder == null ) { throw new NullPointerException("Should never be null!"); }
config = configuration;
builder = configBuilder;
log = LogManager.getLogger();
}
@Override public Configuration subset(String prefix) { return config.subset(prefix); }
......@@ -130,6 +132,7 @@ public class ConfigHelper implements Configuration {
@Override public Object getArray(Class<?> cls, String key) { return config.getArray(cls, key); }
@SuppressWarnings("deprecation")
@Override public Object getArray(Class<?> cls, String key, Object defaultValue) { return config.getArray(cls, key, defaultValue); }
@Override public <T> List<T> getList(Class<T> cls, String key) { return config.getList(cls, key); }
......@@ -159,10 +162,10 @@ public class ConfigHelper implements Configuration {
@Override public void run() {
try {
builder.save();
System.out.println("[autoSave] Configuration saved.");
log.info("[autoSave] Configuration saved.");
}
catch (ConfigurationException ex)
{ Logger.getLogger(ConfigHelper.class.getName()).log(Level.SEVERE, null, ex); }
{ log.fatal("Unable to save config!", ex); }
}
}, 5000L);
}
......
package me.felinewith.lang_toolkit.apps.langbuilder.util;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.HashMap;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;
/**
*
* @author jlhawkwell
*/
public class DesktopMenu {
private JToolBar toolBar;
private HashMap<String, Component> tbItems;
private ActionListener al;
public DesktopMenu() {
toolBar = new JToolBar();
}
public DesktopMenu(JToolBar jToolBar) {
toolBar = jToolBar;
tbItems = new HashMap<>();
}
public DesktopMenu(JToolBar jToolBar, ActionListener actions) {
toolBar = jToolBar;
tbItems = new HashMap<>();
al = actions;
}
public void addButton(String text) { addButton(text, text, null, null, true); }
public void addButton(String text, String name) { addButton(text, name, null, null, true); }
public void addButton(String text, String name, String toolTip) { addButton(text, name, toolTip, null, true); }
public void addButton(String text, String name, String toolTip, String icon) {
URL u = this.getClass().getResource("/org/freedesktop/tango/".concat(icon));
addButton(text, name, toolTip, u);
}
public void addButton(String text, URL icon) { addButton(text, text, null, icon); }
public void addButton(String text, String name, URL icon) { addButton(text, name, null, icon); }
public void addButton(String text, String name, String toolTip, URL icon) {
ImageIcon i = new ImageIcon(icon);
addButton(text, name, toolTip, i, true);
}
public void addButton(String text, Icon icon) { addButton(text, text, null, icon, true); }
public void addButton(String text, String name, Icon icon) { addButton(text, name, null, icon, true); }
public void addButton(String text, String name, String toolTip, Icon icon) {
addButton(text, name, toolTip, icon, true);
}
private void addButton(String text, String name, String toolTip, Icon icon, boolean rawr) {
JButton b = new JButton(text);
b.setName(name);
if ( toolTip != null ) { if ( !toolTip.isEmpty() ) { b.setToolTipText(toolTip); } }
if ( icon != null ) { b.setIcon(icon); }
addComponent(b);
}
public void addSeparator() { toolBar.addSeparator(); }
public void addSeparator(Dimension size) { toolBar.addSeparator(size); }
public void addComponent(Component component) {
if ( component.getName() == null ) { return; }
addComponent(component.getName(), component);
}
public void addComponent(String name, Component component) {
tbItems.put(name, component);
toolBar.add(name, component);
if ( al == null ) { return; }
try {
Method m = component.getClass().getMethod("addActionListener", ActionListener.class);
m.invoke(component, al);
}
catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException |
InvocationTargetException ex) {}
try {
Method m = component.getClass().getMethod("setActionCommand", String.class);
m.invoke(component, name);
}
catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException |
InvocationTargetException ex) {}
}
public Component getComponent(String name) { return tbItems.get(name); }
}
package me.felinewith.lang_toolkit.apps.langbuilder.window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import me.felinewith.lang_toolkit.apps.langbuilder.LangBuilder;
import org.apache.commons.configuration2.Configuration;
/**
*
* @author jlhawkwell
*/
public class WindowAdapterListener extends WindowAdapter {
private LangBuilder lb;
private Configuration config;
public WindowAdapterListener(LangBuilder langbuilder, Configuration configuration) {
lb = langbuilder;
config = configuration;
}
@Override public void windowStateChanged(WindowEvent e) {
super.windowStateChanged(e);
System.out.println("[windowStateChanged] ".concat(e.paramString()));
}
private void printChange(WindowEvent e) {
System.out.print("[windowChanged]");
System.out.println(e.paramString());
}
}
package me.felinewith.lang_toolkit.apps.langbuilder.window;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JFrame;
import me.felinewith.lang_toolkit.apps.langbuilder.LangBuilder;
import org.apache.commons.configuration2.Configuration;
/**
*
* @author jlhawkwell
*/
public class WindowComponentListener implements ComponentListener {
private LangBuilder lb;
private Configuration config;
public WindowComponentListener(LangBuilder langbuilder, Configuration configuration) {
lb = langbuilder;
config = configuration;
}
@Override public void componentResized(ComponentEvent e) {
if ( e.getComponent() instanceof JFrame ) {
Dimension size = e.getComponent().getSize();
config.setProperty("window.preferredSize.x", size.width);
config.setProperty("window.preferredSize.y", size.height);
}
}
@Override public void componentMoved(ComponentEvent e) {
}
@Override public void componentShown(ComponentEvent e) {
}
@Override public void componentHidden(ComponentEvent e) {
}
private void printChange(ComponentEvent e) {
System.out.print("[componentChanged] ");
System.out.println(e.paramString());
}
}
package me.felinewith.lang_toolkit.apps.langbuilder.window;
import java.awt.Component;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import me.felinewith.lang_toolkit.apps.langbuilder.LangBuilder;
import org.apache.commons.configuration2.Configuration;
/**
*
* @author jlhawkwell
*/
public class WindowPropertyChangeListener implements PropertyChangeListener {
private LangBuilder lb;
private Configuration config;
public WindowPropertyChangeListener(LangBuilder langBuilder, Configuration configuration) {
lb = langBuilder;
config = configuration;
}
@Override public void propertyChange(PropertyChangeEvent evt) {
String className = evt.getSource().getClass().getSimpleName();
if ( className.equals("JSplitPane") ) {
switch ( evt.getPropertyName() ) {
case "dividerLocation": saveHelper(evt); break;
}
}
else { printChange(evt); }
}
private void printChange(PropertyChangeEvent evt) {
StringBuilder sb = new StringBuilder("[propertyChange] ");
sb.append(evt.getSource().getClass().getSimpleName());
sb.append("/");
sb.append(evt.getPropertyName());
sb.append("/");
sb.append(evt.getNewValue());
System.out.println(sb.toString());
}
private void saveHelper(PropertyChangeEvent evt) {
String name = null;
if ( evt.getSource() instanceof Component ) {
name = ((Component)evt.getSource()).getName();
}
if ( name == null ) { name = evt.getSource().getClass().getSimpleName(); }
String propertyName = evt.getPropertyName();
StringBuilder sb = new StringBuilder(name);
sb.append(".");
sb.append(propertyName);
config.setProperty(sb.toString(), evt.getNewValue());
}
}
package me.felinewith.lang_toolkit.apps.langbuilder.window;
import java.awt.Component;
import java.awt.Rectangle;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import me.felinewith.lang_toolkit.apps.langbuilder.actions.ActionHandle;
import me.felinewith.lang_toolkit.apps.langbuilder.actions.UnifiedListener;
import org.apache.commons.configuration2.Configuration;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
*
* @author jlhawkwell
*/
public class WindowUnifiedListener extends UnifiedListener {
JFrame frame;
Configuration config;
Logger log;
public WindowUnifiedListener(JFrame jFrame, Configuration configuration) {
frame = jFrame;
config = configuration;
log = LogManager.getLogger();
}
@Override public void handleAction(ActionHandle action) {
if ( action.getName().equals("exit") ) {
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
return;
}
super.handleAction(action);
}
@Override public void handleComponent(ActionHandle action) {
if ( action.getSource() instanceof JFrame ) {
JFrame jFrame = (JFrame) action.getSource();
Rectangle bounds = jFrame.getBounds();
switch ( action.getActionType() ) {
case COMPONENT_RESIZED:
config.setProperty("window.preferredSize.x", bounds.width);
config.setProperty("window.preferredSize.y", bounds.height);
return;
case COMPONENT_MOVED:
config.setProperty("window.location.x", bounds.x);
config.setProperty("window.location.y", bounds.y);
return;
default: super.handleComponent(action);
}
}
super.handleComponent(action);
}
@Override public void handleProperty(ActionHandle action) {
String className = action.getSource().getClass().getSimpleName();
if ( className.equals("JSplitPane") ) {
switch ( action.getName() ) {
case "dividerLocation": saveHelper(action); return;
default: super.handleProperty(action);
}
}
super.handleProperty(action);
}
@Override public void handleEvent(ActionHandle action) {
StringBuilder sb = new StringBuilder("[");
sb.append(action.getActionSource().toString());
sb.append("/");
sb.append(action.getActionType().toString());
sb.append("] ");
sb.append(action.getSource().getClass().getSimpleName());
sb.append("/");
sb.append(action.getName());
sb.append("/");
sb.append(action.getIntValue());
sb.append("/");
sb.append(action.getObjectValue());
log.debug(sb.toString());
}
private void saveHelper(ActionHandle action) {
String name = null;
if ( action.getSource() instanceof Component ) { name = ((Component)action.getSource()).getName(); }
if ( name == null ) { name = action.getSource().getClass().getSimpleName(); }
String propertyName = action.getName();
StringBuilder sb = new StringBuilder(name);
sb.append(".");
sb.append(propertyName);
config.setProperty(sb.toString(), action.getObjectValue());
}
}
......@@ -139,6 +139,16 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!