Premier commit - Introduction à git
This commit is contained in:
commit
2f8b475c6a
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
.classpath
|
||||
.project
|
||||
.settings
|
||||
bin/
|
||||
45
src/com/bernard/console/CommandsPrompter.java
Normal file
45
src/com/bernard/console/CommandsPrompter.java
Normal file
@ -0,0 +1,45 @@
|
||||
package com.bernard.console;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import javax.swing.JTextField;
|
||||
|
||||
import com.bernard.console.commands.Commande;
|
||||
|
||||
public class CommandsPrompter {
|
||||
public static void command(String command) {
|
||||
String[] noms = command.split(" ");
|
||||
if (noms.length <= 0)
|
||||
throw new IllegalArgumentException("'" + command + "' is not a valid command");
|
||||
Commande c = Commande.allCommands.get(noms[0].toLowerCase());
|
||||
if(c==null){
|
||||
Console.out("La commande n'existe pas", "ERROR");
|
||||
}else{
|
||||
c.execute(noms);
|
||||
}
|
||||
}
|
||||
public static void askCompletion(String start,JTextField textField){
|
||||
final List<String> accepted = new ArrayList<>();
|
||||
Commande.allCommands.keySet().forEach(new Consumer<String>() {
|
||||
|
||||
@Override
|
||||
public void accept(String s) {
|
||||
if(s.startsWith(start))
|
||||
accepted.add(s);
|
||||
}
|
||||
});
|
||||
switch (accepted.size()) {
|
||||
case 0:
|
||||
Console.out("Aucune commande ne commence par \""+start+"\"","COMMAND");
|
||||
return;
|
||||
case 1:
|
||||
textField.setText(accepted.get(0));
|
||||
return;
|
||||
default:
|
||||
Console.out(accepted.size() + " commandes commencent par \""+start+"\" : " +String.join(" , ", accepted),"COMMAND");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
152
src/com/bernard/console/Console.java
Normal file
152
src/com/bernard/console/Console.java
Normal file
@ -0,0 +1,152 @@
|
||||
package com.bernard.console;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Date;
|
||||
|
||||
import com.bernard.console.message.ComplexMessage;
|
||||
import com.bernard.console.message.ComplexMessageComponent;
|
||||
import com.bernard.console.message.ComplexMessageComponentsCreator;
|
||||
import com.bernard.console.view.ConsoleFrame;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Mysaa
|
||||
*
|
||||
*/
|
||||
public final class Console {
|
||||
|
||||
/**
|
||||
* The Configuration class of the console
|
||||
*/
|
||||
public static ConsoleConfig config = null;
|
||||
|
||||
/**
|
||||
* The main frame of the console (with outs and command prompts)
|
||||
*/
|
||||
public static ConsoleFrame frame = null;
|
||||
|
||||
/**
|
||||
* Create the console and display it
|
||||
* Configure System.out for the Console
|
||||
* Before they are called before this function, the out functions will crash
|
||||
* Is equivalent as calling <code>createConsole(true);</code>
|
||||
*/
|
||||
public static void createConsole() {
|
||||
createConsole(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the console and display it
|
||||
* Configure System.out for the Console if the parameter setSysout is evaluated as true
|
||||
* Before they are called before this function, the out functions will crash
|
||||
* @param setSysout whether the System.out have to be configured for the Console.
|
||||
*/
|
||||
public static void createConsole(boolean setSysout) {
|
||||
config = new ConsoleConfig();
|
||||
frame = new ConsoleFrame();
|
||||
if(setSysout)
|
||||
try {
|
||||
System.setOut(new ConsolePrintStream(new File("out.out")));
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static String out(String text) {
|
||||
if (frame == null)
|
||||
throw new IllegalStateException("The console may be iniialized by a call to Console.createConsole()");
|
||||
|
||||
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
|
||||
StackTraceElement ste = stackTraceElements[2];
|
||||
//TODO verifier ArrayOutOfBounds
|
||||
Date theDate = new Date();
|
||||
ComplexMessageComponent date = new ComplexMessageComponent(Console.config.shortDateFormat.format(theDate), Console.config.dateColor, Console.config.complexDateFormat.format(theDate));
|
||||
ComplexMessageComponent sender = new ComplexMessageComponent(ste.getMethodName(), Console.config.senderColor, "from "+ ste.toString());
|
||||
frame.outputText.say(new ComplexMessage(new Object[]{null,null,text},new ComplexMessageComponent[]{date,sender, ComplexMessageComponentsCreator.getComplexMessageComponent(text)}));
|
||||
//TODO Beautiful this
|
||||
// ,ste.getClassName()+"."+ste.getMethodName());
|
||||
return text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs the objects params in a row
|
||||
* Can be used like this : <code>out(myObject).someFunctionOfMyObject();</code>
|
||||
* @param components The objects to draw
|
||||
* @return the first object (if exists) on the parameter
|
||||
*/
|
||||
public static Object out(Object... components) {
|
||||
if (frame == null)
|
||||
throw new IllegalStateException("The console may be initialized by a call to Console.createConsole()");
|
||||
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
|
||||
StackTraceElement ste = stackTraceElements[2];
|
||||
//TODO verifier ArrayOutOfBounds
|
||||
Date theDate = new Date();
|
||||
ComplexMessageComponent date = new ComplexMessageComponent(Console.config.shortDateFormat.format(theDate), Console.config.dateColor, Console.config.complexDateFormat.format(theDate));
|
||||
ComplexMessageComponent sender = new ComplexMessageComponent(ste.getMethodName(), Console.config.senderColor, "from "+ ste.toString());
|
||||
|
||||
Object[] allComponents = new Object[2 + components.length];
|
||||
allComponents[0] = allComponents[1] = null;
|
||||
System.arraycopy(components, 0, allComponents, 2, components.length);
|
||||
|
||||
ComplexMessageComponent[] allMessageComponents = new ComplexMessageComponent[2 + components.length];
|
||||
allMessageComponents[0] = date;
|
||||
allMessageComponents[1] = sender;
|
||||
|
||||
for (int i = 2; i < allMessageComponents.length; i++) {
|
||||
allMessageComponents[i] = ComplexMessageComponentsCreator.getComplexMessageComponent(components[i-2]);
|
||||
}
|
||||
|
||||
frame.outputText.say(new ComplexMessage(allComponents,allMessageComponents));
|
||||
return (components.length>0)?components[0]:null;
|
||||
}
|
||||
public static Object outA(Object[]... components) {
|
||||
if (frame == null)
|
||||
throw new IllegalStateException("The console may be initialized by a call to Console.createConsole()");
|
||||
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
|
||||
StackTraceElement ste = stackTraceElements[2];
|
||||
//TODO verifier ArrayOutOfBounds
|
||||
Date theDate = new Date();
|
||||
ComplexMessageComponent date = new ComplexMessageComponent(Console.config.shortDateFormat.format(theDate), Console.config.dateColor, Console.config.complexDateFormat.format(theDate));
|
||||
ComplexMessageComponent sender = new ComplexMessageComponent(ste.getMethodName(), Console.config.senderColor, "from "+ ste.toString());
|
||||
|
||||
Object[] allComponents = new Object[2 + components.length];
|
||||
allComponents[0] = allComponents[1] = null;
|
||||
System.arraycopy(components, 0, allComponents, 2, components.length);
|
||||
|
||||
ComplexMessageComponent[] allMessageComponents = new ComplexMessageComponent[2 + components.length];
|
||||
allMessageComponents[0] = date;
|
||||
allMessageComponents[1] = sender;
|
||||
|
||||
for (int i = 2; i < allMessageComponents.length; i++) {
|
||||
allMessageComponents[i] = ComplexMessageComponentsCreator.getComplexMessageComponent(components[i-2]);
|
||||
}
|
||||
|
||||
frame.outputText.say(new ComplexMessage(allComponents,allMessageComponents));
|
||||
return (components.length>0)?components[0]:null;
|
||||
}
|
||||
|
||||
|
||||
public static String outS(Object... components) {
|
||||
if (frame == null)
|
||||
throw new IllegalStateException("The console may be initialized by a call to Console.createConsole()");
|
||||
Object[] newComponents = new Object[components.length * 2 + 1 /* -1(array length) + 2(date & sender) */];
|
||||
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
|
||||
StackTraceElement ste = stackTraceElements[2];
|
||||
//TODO verifier ArrayOutOfBounds
|
||||
Date theDate = new Date();
|
||||
newComponents[0] = new ComplexMessageComponent(Console.config.shortDateFormat.format(theDate), Console.config.dateColor, Console.config.complexDateFormat.format(theDate));
|
||||
newComponents[1] = new ComplexMessageComponent(ste.getMethodName(), Console.config.senderColor, "from "+ ste.toString());
|
||||
|
||||
for (int i = 0; i < newComponents.length - 2; i++) {
|
||||
if (i % 2 == 0)
|
||||
newComponents[i+2] = components[Math.floorDiv(i, 2)];
|
||||
else
|
||||
newComponents[i+2] = new String(" ");
|
||||
}
|
||||
ComplexMessage message = new ComplexMessage(newComponents);
|
||||
frame.outputText.say(message);
|
||||
return message.getConcatedText();
|
||||
}
|
||||
|
||||
}
|
||||
59
src/com/bernard/console/ConsoleConfig.java
Normal file
59
src/com/bernard/console/ConsoleConfig.java
Normal file
@ -0,0 +1,59 @@
|
||||
package com.bernard.console;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.text.DateFormat;
|
||||
|
||||
public class ConsoleConfig {
|
||||
public Font consoleFont = new Font("Arial Gras", Font.PLAIN, 25);
|
||||
public Font descFont = new Font("Arial Gras", Font.PLAIN, 12);
|
||||
public boolean isSenderShown = true;
|
||||
public boolean isDateShown = true;
|
||||
public Color senderColor = Color.GREEN;
|
||||
public Color dateColor = Color.MAGENTA;
|
||||
public Color backgroundColor = Color.WHITE;
|
||||
public Color messageColor = Color.BLACK;
|
||||
public DateFormat complexDateFormat = DateFormat.getTimeInstance(DateFormat.FULL);
|
||||
public DateFormat shortDateFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
|
||||
public Color numberColor = Color.GRAY;
|
||||
public Color nullColor = Color.RED;
|
||||
public Color arrayColor = Color.LIGHT_GRAY;
|
||||
public FontByModifierGetter fontByModifierGetter = new FontByModifierGetter() {
|
||||
|
||||
@Override
|
||||
public Font getFont(String modifiers) {
|
||||
Font font = descFont;
|
||||
if (modifiers.contains("static"))
|
||||
font = descFont.deriveFont(Font.ITALIC);
|
||||
if (modifiers.contains("final"))
|
||||
font = descFont.deriveFont(Font.BOLD);
|
||||
if (modifiers.contains("static") && modifiers.contains("final"))
|
||||
font = descFont.deriveFont(Font.BOLD | Font.ITALIC);
|
||||
|
||||
return font;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Color getFontColor(String modifiers) {
|
||||
if (modifiers.contains("public"))
|
||||
return Color.BLUE;
|
||||
if (modifiers.contains("protected"))
|
||||
return Color.LIGHT_GRAY;
|
||||
return Color.DARK_GRAY;
|
||||
}
|
||||
};
|
||||
|
||||
public Font getFont(String modifiers) {
|
||||
return fontByModifierGetter.getFont(modifiers);
|
||||
}
|
||||
|
||||
public Color getFontColor(String modifiers) {
|
||||
return fontByModifierGetter.getFontColor(modifiers);
|
||||
}
|
||||
|
||||
public static interface FontByModifierGetter {
|
||||
Font getFont(String modifiers);
|
||||
|
||||
Color getFontColor(String modifiers);
|
||||
}
|
||||
}
|
||||
236
src/com/bernard/console/ConsolePrintStream.java
Normal file
236
src/com/bernard/console/ConsolePrintStream.java
Normal file
@ -0,0 +1,236 @@
|
||||
package com.bernard.console;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Locale;
|
||||
|
||||
public class ConsolePrintStream extends PrintStream {
|
||||
private static File fileToDelete;
|
||||
private static File getTheFileToDelete(){
|
||||
|
||||
File f = new File("fichierasupprimertoutdesuitemercijava");
|
||||
if(!f.exists()){
|
||||
fileToDelete = new File("fichierasupprimertoutdesuitemercijava");
|
||||
return fileToDelete;
|
||||
}else{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public ConsolePrintStream() {
|
||||
super(getTheFileToDelete());
|
||||
}
|
||||
public ConsolePrintStream(OutputStream out) {
|
||||
super(out);
|
||||
}
|
||||
|
||||
public ConsolePrintStream(String fileName) throws FileNotFoundException {
|
||||
super(fileName);
|
||||
}
|
||||
|
||||
public ConsolePrintStream(File file) throws FileNotFoundException {
|
||||
super(file);
|
||||
}
|
||||
|
||||
public ConsolePrintStream(OutputStream out, boolean autoFlush) {
|
||||
super(out, autoFlush);
|
||||
}
|
||||
|
||||
public ConsolePrintStream(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
|
||||
super(fileName, csn);
|
||||
}
|
||||
|
||||
public ConsolePrintStream(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
|
||||
super(file, csn);
|
||||
}
|
||||
|
||||
public ConsolePrintStream(OutputStream out, boolean autoFlush, String encoding)
|
||||
throws UnsupportedEncodingException {
|
||||
super(out, autoFlush, encoding);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrintStream append(char c) {
|
||||
// TODO Auto-generated method stub
|
||||
return super.append(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrintStream append(CharSequence csq, int start, int end) {
|
||||
// TODO Auto-generated method stub
|
||||
return super.append(csq, start, end);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrintStream append(CharSequence csq) {
|
||||
// TODO Auto-generated method stub
|
||||
return super.append(csq);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkError() {
|
||||
// TODO Auto-generated method stub
|
||||
return super.checkError();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void clearError() {
|
||||
// TODO Auto-generated method stub
|
||||
super.clearError();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
// TODO Auto-generated method stub
|
||||
super.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
// TODO Auto-generated method stub
|
||||
super.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrintStream format(Locale l, String format, Object... args) {
|
||||
// TODO Auto-generated method stub
|
||||
return super.format(l, format, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrintStream format(String format, Object... args) {
|
||||
// TODO Auto-generated method stub
|
||||
return super.format(format, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(boolean b) {
|
||||
print((Boolean)b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(char c) {
|
||||
print((Character)c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(char[] s) {
|
||||
print(new String(s));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(double d) {
|
||||
print((Double)d);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(float f) {
|
||||
print((Float)f);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(int i) {
|
||||
print((Integer)i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(long l) {
|
||||
print((Long)l);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(Object obj) {
|
||||
Console.out(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(String s) {
|
||||
Console.out(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrintStream printf(Locale l, String format, Object... args) {
|
||||
// TODO Auto-generated method stub
|
||||
return super.printf(l, format, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrintStream printf(String format, Object... args) {
|
||||
// TODO Auto-generated method stub
|
||||
return super.printf(format, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println() {
|
||||
Console.out();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(boolean x) {
|
||||
Console.out(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(char x) {
|
||||
Console.out(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(char[] x) {
|
||||
Console.out(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(double x) {
|
||||
Console.out(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(float x) {
|
||||
Console.out(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(int x) {
|
||||
Console.out(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(long x) {
|
||||
Console.out(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(Object x) {
|
||||
Console.out(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(String x) {
|
||||
Console.out(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setError() {
|
||||
// TODO Auto-generated method stub
|
||||
super.setError();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] buf, int off, int len) {
|
||||
// TODO Auto-generated method stub
|
||||
super.write(buf, off, len);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) {
|
||||
// TODO Auto-generated method stub
|
||||
super.write(b);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
5
src/com/bernard/console/commands/Action.java
Normal file
5
src/com/bernard/console/commands/Action.java
Normal file
@ -0,0 +1,5 @@
|
||||
package com.bernard.console.commands;
|
||||
|
||||
public interface Action {
|
||||
void command(String[] args);
|
||||
}
|
||||
108
src/com/bernard/console/commands/Commande.java
Normal file
108
src/com/bernard/console/commands/Commande.java
Normal file
@ -0,0 +1,108 @@
|
||||
package com.bernard.console.commands;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.bernard.console.Console;
|
||||
|
||||
public final class Commande {
|
||||
public static Map<String, Commande> allCommands = new HashMap<>();;
|
||||
String name;
|
||||
String help;
|
||||
Action action;
|
||||
|
||||
public Commande(String name, Action action) {
|
||||
this(name, "No help here , sorry", action);
|
||||
}
|
||||
|
||||
public Commande(String name, String help, Action action) {
|
||||
this.name = name;
|
||||
this.help = help;
|
||||
this.action = action;
|
||||
allCommands.put(name, this);
|
||||
}
|
||||
|
||||
public static final Commande baseCommand = new Commande("help", "Display commands's helps", new Action() {
|
||||
|
||||
@Override
|
||||
public void command(String[] args) {
|
||||
if (args.length > 1) {
|
||||
Console.out(allCommands.get(args[1]).help, "HELP");
|
||||
} else {
|
||||
Console.out("Il manque un argument :", "ERROR");
|
||||
}
|
||||
}
|
||||
});
|
||||
public static final Commande threadsCommand = new Commande("threads", "Manipule les threads", new Action() {
|
||||
|
||||
@Override
|
||||
public void command(String[] args) {
|
||||
Thread[] tTab = new Thread[1000];
|
||||
Thread.enumerate(tTab);
|
||||
for (Thread t : tTab) {
|
||||
if (t != null)
|
||||
Console.out(t.getName(), "Threads");
|
||||
}
|
||||
}
|
||||
});
|
||||
public static final Commande clearCommand = new Commande("clear", "Vide la console", new Action() {
|
||||
|
||||
@Override
|
||||
public void command(String[] args) {
|
||||
Console.frame.outputText.clearText();
|
||||
}
|
||||
});
|
||||
|
||||
public static final Commande printCommand = new Commande("print", "Affiche une variable", new Action() {
|
||||
|
||||
@Override
|
||||
public void command(String[] args) {
|
||||
if (args.length <= 1) {
|
||||
Console.out("Il manque un argument :", "ERROR");
|
||||
} else {
|
||||
String[] nomes = args[1].split("\\.");
|
||||
if (nomes.length <= 1) {
|
||||
Console.out("Le nom de la classe n'est pas valide", "ERROR");
|
||||
} else {
|
||||
String[] className = new String[nomes.length];
|
||||
Arrays.fill(className, new String());
|
||||
System.arraycopy(nomes, 0, className, 0, nomes.length - 1);
|
||||
String classNameS = String.join(".", className);
|
||||
while (classNameS.endsWith("."))
|
||||
classNameS = classNameS.substring(0, classNameS.length() - 1);
|
||||
try {
|
||||
Class<?> clazz = Class.forName(classNameS);
|
||||
Field f = clazz.getField(nomes[nomes.length - 1]);
|
||||
if (!Modifier.isStatic(f.getModifiers())) {
|
||||
Console.out("L'attribut " + nomes[nomes.length - 1] + " n'est pas static dans la classe "
|
||||
+ classNameS, "ERROR");
|
||||
} else {
|
||||
Console.out(f.get(null));
|
||||
}
|
||||
} catch (ClassNotFoundException | NoClassDefFoundError e) {
|
||||
Console.out("La classe " + classNameS + " n'existe pas", "ERROR");
|
||||
} catch (NoSuchFieldException e) {
|
||||
Console.out(
|
||||
"L'attribut " + nomes[nomes.length - 1] + " n'existe pas dans la classe " + classNameS,
|
||||
"ERROR");
|
||||
} catch (SecurityException e) {
|
||||
Console.out("Vous n'avez pas l'autorisation d'acceder à l'attribut " + nomes[nomes.length - 1]
|
||||
+ " dans la classe " + classNameS, "ERROR");
|
||||
} catch (IllegalArgumentException e) {
|
||||
System.err.println("Erreur interne : E001");
|
||||
} catch (IllegalAccessException e) {
|
||||
System.err.println("Erreur interne : E002");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
public void execute(String[] args) {
|
||||
action.command(args);
|
||||
}
|
||||
|
||||
}
|
||||
88
src/com/bernard/console/message/ComplexMessage.java
Normal file
88
src/com/bernard/console/message/ComplexMessage.java
Normal file
@ -0,0 +1,88 @@
|
||||
package com.bernard.console.message;
|
||||
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics;
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.bernard.console.Console;
|
||||
import com.bernard.console.view.DescribingObjectFrame;
|
||||
|
||||
public class ComplexMessage {
|
||||
ComplexMessageComponent[] content;
|
||||
Object[] objects;
|
||||
|
||||
public ComplexMessage() {
|
||||
content = new ComplexMessageComponent[0];
|
||||
objects = new Object[0];
|
||||
}
|
||||
|
||||
public ComplexMessage(Object... contenu) {
|
||||
objects = contenu;
|
||||
content = new ComplexMessageComponent[contenu.length];
|
||||
for (int i = 0; i < contenu.length; i++) {
|
||||
content[i] = ComplexMessageComponentsCreator.getComplexMessageComponent(contenu[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public ComplexMessage(Object[] contenu,ComplexMessageComponent[] components) {
|
||||
objects = contenu;
|
||||
content = components;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Arrays.toString(content);
|
||||
}
|
||||
|
||||
public String getConcatedText() {
|
||||
String out = new String();
|
||||
for (ComplexMessageComponent complexMessageComponent : content) {
|
||||
out += complexMessageComponent.message;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
public String getTooltipTextAt(int pos) {
|
||||
return getComplexMessageComponentAt(pos).getTooltip();
|
||||
}
|
||||
|
||||
public ComplexMessageComponent getComplexMessageComponentAt(int textPos) {
|
||||
for (ComplexMessageComponent complexMessageComponent : content) {
|
||||
textPos -= complexMessageComponent.message.length();
|
||||
if (textPos < 0) {
|
||||
return complexMessageComponent;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object getObjectAt(int textPos) {
|
||||
for (int i = 0;i<content.length;i++) {
|
||||
textPos -= content[i].message.length();
|
||||
if (textPos < 0) {
|
||||
return objects[i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void draw(Graphics g, int y) {
|
||||
int decalage = 0;
|
||||
FontMetrics fm = g.getFontMetrics(Console.config.consoleFont);
|
||||
g.setFont(Console.config.consoleFont);
|
||||
for (ComplexMessageComponent component : content) {
|
||||
g.setColor(component.txtColor);
|
||||
g.drawString(component.message, decalage, y);
|
||||
decalage += fm.stringWidth(component.message);
|
||||
}
|
||||
}
|
||||
|
||||
public void displayFrame(int letterPos) {
|
||||
Object toDisplay = getObjectAt(letterPos);
|
||||
if(toDisplay != null)
|
||||
new DescribingObjectFrame(toDisplay);
|
||||
}
|
||||
|
||||
}
|
||||
46
src/com/bernard/console/message/ComplexMessageComponent.java
Normal file
46
src/com/bernard/console/message/ComplexMessageComponent.java
Normal file
@ -0,0 +1,46 @@
|
||||
package com.bernard.console.message;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
public class ComplexMessageComponent {
|
||||
String message;
|
||||
Color txtColor;
|
||||
String tooltip;
|
||||
|
||||
public ComplexMessageComponent(String message, Color txtColor, String tooltip) {
|
||||
this.message = message;
|
||||
this.txtColor = txtColor;
|
||||
this.tooltip = tooltip;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public Color getTextColor() {
|
||||
return txtColor;
|
||||
}
|
||||
|
||||
public void setTextColor(Color txtColor) {
|
||||
this.txtColor = txtColor;
|
||||
}
|
||||
|
||||
public String getTooltip() {
|
||||
return tooltip;
|
||||
}
|
||||
|
||||
public void setTooltip(String tooltip) {
|
||||
this.tooltip = tooltip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ComplexMessageComponent [message \"" + message + "\" in color " + txtColor + " with tooltip " + tooltip
|
||||
+ "]";
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,120 @@
|
||||
package com.bernard.console.message;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import com.bernard.console.Console;
|
||||
|
||||
/**
|
||||
* Create a ComplexMessageComponent for a specific class <T> , is used when an object wants to be printed :
|
||||
* he is passed as an argument of <code>getComplexMessageComponent</code> which returns, if the
|
||||
* argument object match the class, the return of the abstract method <code>getComponent</code>
|
||||
*
|
||||
*
|
||||
* @author Mysaa
|
||||
*
|
||||
* @param <T> The object from which the ComplexMessageComponents are created
|
||||
*/
|
||||
public abstract class ComplexMessageComponentsCreator<T> {
|
||||
private static Map<Class<?>, ComplexMessageComponentsCreator<?>> assossiations = new HashMap<>();
|
||||
|
||||
protected abstract ComplexMessageComponent getComponent(T object);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private ComplexMessageComponent getComponentO(Object object) {
|
||||
return getComponent((T) object);
|
||||
}
|
||||
|
||||
public ComplexMessageComponentsCreator(Class<T> clazz) {
|
||||
assossiations.put(clazz, this);
|
||||
}
|
||||
|
||||
public static ComplexMessageComponent getComplexMessageComponent(Object o) {
|
||||
if (o == null)
|
||||
return new ComplexMessageComponent("null", Console.config.nullColor, null);
|
||||
if(o.getClass().isArray()){
|
||||
Class<?> type = o.getClass().getComponentType();
|
||||
return new ComplexMessageComponent("[" + Array.getLength(o) + " " + type.getSimpleName() + "]", Console.config.arrayColor, type.getName());
|
||||
}
|
||||
if (assossiations.containsKey(o.getClass())) {
|
||||
return assossiations.get(o.getClass()).getComponentO(o);
|
||||
} else {
|
||||
return new ComplexMessageComponent(o.toString(), Console.config.messageColor, null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static ComplexMessageComponentsCreator<ComplexMessageComponent> complexMessageComponentCreator = new ComplexMessageComponentsCreator<ComplexMessageComponent>(
|
||||
ComplexMessageComponent.class) {
|
||||
|
||||
@Override
|
||||
protected ComplexMessageComponent getComponent(ComplexMessageComponent object) {
|
||||
return object;
|
||||
}
|
||||
};
|
||||
public static ComplexMessageComponentsCreator<Double> doubleMessageComponentCreator = new ComplexMessageComponentsCreator<Double>(
|
||||
Double.class) {
|
||||
|
||||
@Override
|
||||
protected ComplexMessageComponent getComponent(Double object) {
|
||||
return new ComplexMessageComponent(Double.toString(object), Console.config.numberColor, null);
|
||||
}
|
||||
};
|
||||
public static ComplexMessageComponentsCreator<Long> longMessageComponentCreator = new ComplexMessageComponentsCreator<Long>(
|
||||
Long.class) {
|
||||
|
||||
@Override
|
||||
protected ComplexMessageComponent getComponent(Long object) {
|
||||
return new ComplexMessageComponent(Long.toString(object), Console.config.numberColor, null);
|
||||
}
|
||||
};
|
||||
public static ComplexMessageComponentsCreator<Date> dateMessageComponentCreator = new ComplexMessageComponentsCreator<Date>(
|
||||
Date.class) {
|
||||
|
||||
@Override
|
||||
protected ComplexMessageComponent getComponent(Date object) {
|
||||
return new ComplexMessageComponent("[" + Console.config.shortDateFormat.format(object) + "]",
|
||||
Console.config.dateColor, Console.config.complexDateFormat.format(object));
|
||||
|
||||
}
|
||||
};
|
||||
public static ComplexMessageComponentsCreator<Method> methodMessageComponentCreator = new ComplexMessageComponentsCreator<Method>(
|
||||
Method.class) {
|
||||
|
||||
@Override
|
||||
protected ComplexMessageComponent getComponent(Method object) {
|
||||
return new ComplexMessageComponent(object.getName(), Console.config.senderColor, "from " + object.getDeclaringClass().getName());
|
||||
}
|
||||
};
|
||||
public static ComplexMessageComponentsCreator<StackTraceElement> steMessageComponentCreator = new ComplexMessageComponentsCreator<StackTraceElement>(
|
||||
StackTraceElement.class) {
|
||||
|
||||
@Override
|
||||
protected ComplexMessageComponent getComponent(StackTraceElement object) {
|
||||
return new ComplexMessageComponent(object.getMethodName(), Console.config.senderColor, "from " + object.getClassName());
|
||||
}
|
||||
};
|
||||
|
||||
public static ComplexMessageComponentsCreator<Random> randMessageComponentCreator = new ComplexMessageComponentsCreator<Random>(
|
||||
Random.class) {
|
||||
|
||||
@Override
|
||||
protected ComplexMessageComponent getComponent(Random object) {
|
||||
try {
|
||||
Field seed = Random.class.getDeclaredField("seed");
|
||||
if (!seed.isAccessible())
|
||||
seed.setAccessible(true);
|
||||
return new ComplexMessageComponent("Random", Console.config.messageColor, "seed : " + seed.get(object));
|
||||
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
|
||||
e.printStackTrace();
|
||||
return new ComplexMessageComponent("Random", Console.config.messageColor, "Unable to get the seed");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
62
src/com/bernard/console/message/Message.java
Normal file
62
src/com/bernard/console/message/Message.java
Normal file
@ -0,0 +1,62 @@
|
||||
package com.bernard.console.message;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
@Deprecated
|
||||
public class Message {
|
||||
public static DateFormat globalFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM);
|
||||
Date date;
|
||||
String sender;
|
||||
String message;
|
||||
|
||||
public Message(Date date, String sender, String message) {
|
||||
this.date = date;
|
||||
this.sender = sender;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public String getDateText() {
|
||||
return "["+globalFormat.format(date)+"]";
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public String getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public void setSender(String sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
|
||||
public String getText(boolean isDateShown, boolean isSenderShown) {
|
||||
if (isDateShown) {
|
||||
if (isSenderShown)
|
||||
return "[" + globalFormat.format(date) + "]" + sender + ":" + message;
|
||||
else
|
||||
return "[" + globalFormat.format(date) + "]" + message;
|
||||
|
||||
} else {
|
||||
if (isSenderShown)
|
||||
return sender + ":" + message;
|
||||
else
|
||||
return message;
|
||||
}
|
||||
}
|
||||
}
|
||||
81
src/com/bernard/console/view/ConsoleFrame.java
Normal file
81
src/com/bernard/console/view/ConsoleFrame.java
Normal file
@ -0,0 +1,81 @@
|
||||
package com.bernard.console.view;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
import com.bernard.console.CommandsPrompter;
|
||||
import com.bernard.console.Console;
|
||||
|
||||
public class ConsoleFrame extends JFrame {
|
||||
|
||||
private static final long serialVersionUID = 2337117842283193253L;
|
||||
|
||||
/*
|
||||
* GraphicsEnvironment.getLocalGraphicsEnvironment() .getAllFonts()[(int)
|
||||
* Math .floor(Math.random() *
|
||||
* GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts().length)]
|
||||
* .deriveFont(Font.PLAIN, 20);
|
||||
*/
|
||||
public JTextField inputText;
|
||||
public OutputPane outputText;
|
||||
|
||||
public ConsoleFrame() {
|
||||
this.setSize(700, 400);
|
||||
this.setLocation(2, 2);
|
||||
this.setTitle("Console");
|
||||
this.addKeyListener(new Listener());
|
||||
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
this.addWindowListener(new WindowAdapter() {
|
||||
|
||||
@Override
|
||||
public void windowDeactivated(WindowEvent e) {
|
||||
//System.out.println("Desctivated");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowActivated(WindowEvent e) {
|
||||
//System.out.println("Activated");
|
||||
}
|
||||
});
|
||||
|
||||
inputText = new JTextField();
|
||||
inputText.setFont(Console.config.consoleFont);
|
||||
inputText.addKeyListener(new Listener());
|
||||
|
||||
outputText = new OutputPane();
|
||||
this.getContentPane().setLayout(new BorderLayout());
|
||||
this.getContentPane().add(inputText, BorderLayout.SOUTH);
|
||||
this.getContentPane().add(outputText, BorderLayout.CENTER);
|
||||
|
||||
this.setVisible(true);
|
||||
}
|
||||
|
||||
public class Listener implements KeyListener {
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
||||
Console.out(inputText.getText(), "YOU");
|
||||
CommandsPrompter.command(inputText.getText());
|
||||
inputText.setText("");
|
||||
}else if(e.getKeyCode() == KeyEvent.VK_TAB){
|
||||
CommandsPrompter.askCompletion(inputText.getText(),inputText);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
210
src/com/bernard/console/view/DescribingObjectFrame.java
Normal file
210
src/com/bernard/console/view/DescribingObjectFrame.java
Normal file
@ -0,0 +1,210 @@
|
||||
package com.bernard.console.view;
|
||||
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseMotionListener;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTabbedPane;
|
||||
import javax.swing.ToolTipManager;
|
||||
|
||||
import com.bernard.console.Console;
|
||||
import com.bernard.console.message.ComplexMessageComponentsCreator;
|
||||
|
||||
public class DescribingObjectFrame extends JFrame implements MouseMotionListener {
|
||||
|
||||
private static final long serialVersionUID = 8224683741648232027L;
|
||||
|
||||
public Object object;
|
||||
public JTabbedPane globalPane;
|
||||
|
||||
public DescribingObjectFrame(Object toDescribe) {
|
||||
this.setSize(400, 400);
|
||||
this.setTitle("Descriprtion d'un " + toDescribe.getClass().getSimpleName());
|
||||
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
object = toDescribe;
|
||||
|
||||
init();
|
||||
|
||||
globalPane.addMouseMotionListener(this);
|
||||
this.setVisible(true);
|
||||
}
|
||||
|
||||
private void init() {
|
||||
globalPane = new JTabbedPane();
|
||||
Class<?> objectClass = object.getClass();
|
||||
|
||||
JPanel fieldsPan = new JPanel();
|
||||
Field[] fields = objectClass.getDeclaredFields();
|
||||
Arrays.sort(fields, new Comparator<Field>() {
|
||||
|
||||
@Override
|
||||
public int compare(Field f1, Field f2) {
|
||||
String f1modifiers = Modifier.toString(f1.getModifiers()),
|
||||
f2modifiers = Modifier.toString(f2.getModifiers());
|
||||
int f1val = 0b00, f2val = 0b00;
|
||||
if (f1modifiers.contains("final"))
|
||||
f1val +=0b01;
|
||||
if (f1modifiers.contains("static"))
|
||||
f1val +=0b10;
|
||||
if (f2modifiers.contains("final"))
|
||||
f2val +=0b01;
|
||||
if (f2modifiers.contains("static"))
|
||||
f2val +=0b10;
|
||||
return Integer.compare(f1val, f2val);
|
||||
}
|
||||
});
|
||||
GridLayout fieldsLayout = new GridLayout(fields.length, 2);
|
||||
fieldsLayout.setHgap(10);
|
||||
fieldsLayout.setVgap(20);
|
||||
fieldsPan.setLayout(fieldsLayout);
|
||||
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
Field f = fields[i];
|
||||
if (!f.isAccessible())
|
||||
f.setAccessible(true);
|
||||
JLabel fieldName = new JLabel(f.getName());
|
||||
String modifiersString = Modifier.toString(f.getModifiers());
|
||||
fieldName.setFont(Console.config.getFont(modifiersString));
|
||||
fieldName.setForeground(Console.config.getFontColor(modifiersString));
|
||||
fieldName.setToolTipText(modifiersString);
|
||||
JLabel fieldValue;
|
||||
try {
|
||||
fieldValue = new JLabel(
|
||||
ComplexMessageComponentsCreator.getComplexMessageComponent(f.get(object)).getMessage());
|
||||
fieldValue.setFont(Console.config.descFont);
|
||||
fieldValue.setToolTipText(f.getType().getName());
|
||||
} catch (IllegalArgumentException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
fieldValue = new JLabel("ERROR");
|
||||
}
|
||||
fieldsPan.add(fieldName);
|
||||
fieldsPan.add(fieldValue);
|
||||
}
|
||||
fieldsPan.addMouseMotionListener(this);
|
||||
globalPane.addTab("Fields", null, new JScrollPane(fieldsPan), "All the fields of the class");
|
||||
|
||||
JPanel methodsPan = new JPanel();
|
||||
Method[] methods = objectClass.getDeclaredMethods();
|
||||
Arrays.sort(methods, new Comparator<Method>() {
|
||||
|
||||
@Override
|
||||
public int compare(Method f1, Method f2) {
|
||||
String f1modifiers = Modifier.toString(f1.getModifiers()),
|
||||
f2modifiers = Modifier.toString(f2.getModifiers());
|
||||
int f1val = 0b00, f2val = 0b00;
|
||||
if (f1modifiers.contains("final"))
|
||||
f1val +=0b01;
|
||||
if (f1modifiers.contains("static"))
|
||||
f1val +=0b10;
|
||||
if (f2modifiers.contains("final"))
|
||||
f2val +=0b01;
|
||||
if (f2modifiers.contains("static"))
|
||||
f2val +=0b10;
|
||||
return Integer.compare(f1val, f2val);
|
||||
}
|
||||
});
|
||||
GridLayout methodsLayout = new GridLayout(methods.length, 2);
|
||||
methodsLayout.setHgap(10);
|
||||
methodsLayout.setVgap(20);
|
||||
methodsPan.setLayout(methodsLayout);
|
||||
|
||||
for (int i = 0; i < methods.length; i++) {
|
||||
Method m = methods[i];
|
||||
if (!m.isAccessible())
|
||||
m.setAccessible(true);
|
||||
JLabel methodName = new JLabel(m.getName());
|
||||
String modifiersString = Modifier.toString(m.getModifiers());
|
||||
methodName.setToolTipText(modifiersString);
|
||||
methodName.setFont(Console.config.getFont(modifiersString));
|
||||
methodName.setForeground(Console.config.getFontColor(getName()));
|
||||
JLabel methodValue;
|
||||
try {
|
||||
methodValue = new JLabel(Arrays.toString(m.getParameterTypes()));
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
methodValue = new JLabel("ERROR");
|
||||
}
|
||||
methodsPan.add(methodName);
|
||||
methodsPan.add(methodValue);
|
||||
}
|
||||
methodsPan.addMouseMotionListener(this);
|
||||
globalPane.addTab("Methods", null, new JScrollPane(methodsPan), "All the methods of the class");
|
||||
|
||||
JPanel constructorsPan = new JPanel();
|
||||
Constructor<?>[] constructeurs = objectClass.getConstructors();
|
||||
Arrays.sort(constructeurs, new Comparator<Constructor<?>>() {
|
||||
|
||||
@Override
|
||||
public int compare(Constructor<?> f1, Constructor<?> f2) {
|
||||
String f1modifiers = Modifier.toString(f1.getModifiers()),
|
||||
f2modifiers = Modifier.toString(f2.getModifiers());
|
||||
int f1val = 0b000, f2val = 0b000;
|
||||
if (f1modifiers.contains("public"))
|
||||
f1val +=0b001;
|
||||
if (f1modifiers.contains("protected"))
|
||||
f1val +=0b010;
|
||||
if (f1modifiers.contains("private"))
|
||||
f1val +=0b100;
|
||||
|
||||
if (f2modifiers.contains("public"))
|
||||
f2val +=0b001;
|
||||
if (f2modifiers.contains("protected"))
|
||||
f2val +=0b010;
|
||||
if (f2modifiers.contains("private"))
|
||||
f2val +=0b100;
|
||||
return Integer.compare(f1val, f2val);
|
||||
}
|
||||
});
|
||||
GridLayout constructorsLayout = new GridLayout(constructeurs.length, 2);
|
||||
constructorsLayout.setHgap(10);
|
||||
constructorsLayout.setVgap(20);
|
||||
constructorsPan.setLayout(constructorsLayout);
|
||||
|
||||
for (int i = 0; i < constructeurs.length; i++) {
|
||||
Constructor<?> c = constructeurs[i];
|
||||
if (!c.isAccessible())
|
||||
c.setAccessible(true);
|
||||
String stringModifiers = Modifier.toString(c.getModifiers());
|
||||
JLabel constructorModifiers = new JLabel(stringModifiers);
|
||||
constructorModifiers.setFont(Console.config.getFont(stringModifiers));
|
||||
constructorModifiers.setForeground(Console.config.getFontColor(stringModifiers));
|
||||
JLabel constructorParams;
|
||||
try {
|
||||
constructorParams = new JLabel(Arrays.toString(c.getParameterTypes()));
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
constructorParams = new JLabel("ERROR");
|
||||
}
|
||||
constructorsPan.add(constructorModifiers);
|
||||
constructorsPan.add(constructorParams);
|
||||
}
|
||||
constructorsPan.addMouseMotionListener(this);
|
||||
globalPane.addTab("Constructeurs", null, new JScrollPane(constructorsPan), "All the constructors of the class");
|
||||
|
||||
|
||||
this.setContentPane(globalPane);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseDragged(MouseEvent arg0) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent e) {
|
||||
ToolTipManager.sharedInstance().setEnabled(false);
|
||||
ToolTipManager.sharedInstance().setEnabled(true);
|
||||
}
|
||||
|
||||
}
|
||||
222
src/com/bernard/console/view/OutputPane.java
Normal file
222
src/com/bernard/console/view/OutputPane.java
Normal file
@ -0,0 +1,222 @@
|
||||
package com.bernard.console.view;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.awt.event.MouseMotionListener;
|
||||
import java.awt.event.MouseWheelEvent;
|
||||
import java.awt.event.MouseWheelListener;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.ToolTipManager;
|
||||
|
||||
import com.bernard.console.Console;
|
||||
import com.bernard.console.message.ComplexMessage;
|
||||
import com.bernard.console.message.ComplexMessageComponent;
|
||||
|
||||
public class OutputPane extends JPanel implements KeyListener, MouseWheelListener, MouseMotionListener, MouseListener {
|
||||
|
||||
private static final long serialVersionUID = 5736681656945317831L;
|
||||
public int fontSize = Console.config.consoleFont.getSize();
|
||||
float screenDecalage = 0.0f;
|
||||
ToolTipManager ttip = ToolTipManager.sharedInstance();
|
||||
List<ComplexMessage> messages = new ArrayList<>();
|
||||
|
||||
public OutputPane() {
|
||||
this.addKeyListener(this);
|
||||
this.addMouseMotionListener(this);
|
||||
this.addMouseWheelListener(this);
|
||||
this.addMouseListener(this);
|
||||
this.setFont(Console.config.consoleFont);
|
||||
ttip.setInitialDelay(0);
|
||||
}
|
||||
|
||||
public void say(String text, String sender) {
|
||||
say(new ComplexMessage(
|
||||
new ComplexMessageComponent("[" + Console.config.shortDateFormat.format(new Date()) + "]",
|
||||
Console.config.dateColor, new Date().toString()),
|
||||
new ComplexMessageComponent(sender, Console.config.senderColor, null),
|
||||
new ComplexMessageComponent(text, Console.config.messageColor, null)));
|
||||
}
|
||||
|
||||
public void say(ComplexMessage message) {
|
||||
messages.add(message);
|
||||
if (screenDecalage > 0.0f) {
|
||||
screenDecalage++;
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (e.getKeyChar() == '+' && e.isControlDown()) {
|
||||
fontSize = Math.min(200, fontSize + 1);
|
||||
}
|
||||
if (e.getKeyChar() == '-' && e.isControlDown()) {
|
||||
fontSize = Math.max(1, fontSize - 1);
|
||||
}
|
||||
System.out.println("Setted font to " + fontSize);
|
||||
this.setFont(Console.config.consoleFont.deriveFont((int) fontSize));
|
||||
repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dimension getPreferredSize() {
|
||||
int maxlenght = 10;
|
||||
FontMetrics fm = getFontMetrics(Console.config.consoleFont);
|
||||
final Iterator<ComplexMessage> messagesF = messages.iterator();
|
||||
while (messagesF.hasNext()) {
|
||||
maxlenght = Math.max(maxlenght, fm.stringWidth(messagesF.next().getConcatedText()));
|
||||
}
|
||||
return new Dimension(maxlenght, messages.size() * fontSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dimension getSize() {
|
||||
return getPreferredSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
g.setColor(Console.config.backgroundColor);
|
||||
g.fillRect(0, 0, getWidth(), getHeight());
|
||||
if (getHeight() / fontSize > messages.size()) {
|
||||
//On peux afficher tous les messages
|
||||
int y = (int) fontSize;
|
||||
ComplexMessage[] messagesF = new ComplexMessage[messages.size()];
|
||||
messages.toArray(messagesF);
|
||||
for (int i = 0, l = messagesF.length; i < l; i++) {
|
||||
messagesF[i].draw(g, y);
|
||||
y += fontSize;
|
||||
}
|
||||
} else {
|
||||
List<ComplexMessage> subList = messages.subList(
|
||||
messages.size() - (getHeight() / fontSize) - (int) screenDecalage,
|
||||
messages.size() - (int) screenDecalage);
|
||||
int y = (int) fontSize;
|
||||
for (ComplexMessage m : subList) {
|
||||
m.draw(g, y);
|
||||
y += fontSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseWheelMoved(MouseWheelEvent e) {
|
||||
screenDecalage -= e.getWheelRotation() ;
|
||||
screenDecalage = Math.min(Math.max(screenDecalage, 0), Math.max(messages.size() - (getHeight() / fontSize), 0));
|
||||
repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent e) {
|
||||
if (Console.frame.isFocused()) {
|
||||
int messageId = Math.max(messages.size() - (getHeight() / fontSize), 0)-(int)screenDecalage + Math.floorDiv(e.getY(), fontSize);
|
||||
if (messageId < messages.size()) {
|
||||
ComplexMessage m = messages.get(messageId);
|
||||
String text = m.getConcatedText();
|
||||
int letterPos = 0;
|
||||
int txtSize = 0;
|
||||
FontMetrics fm = getFontMetrics(Console.config.consoleFont);
|
||||
while (letterPos < text.length() && txtSize < e.getX()) {
|
||||
letterPos++;
|
||||
txtSize = fm.stringWidth(text.substring(0, letterPos));
|
||||
}
|
||||
letterPos--;
|
||||
if (letterPos < text.length()) {
|
||||
String ttipText = m.getTooltipTextAt(letterPos);
|
||||
if (ttipText != null) {
|
||||
setToolTipText(ttipText);
|
||||
ttip.setEnabled(false);
|
||||
ttip.setEnabled(true);
|
||||
} else {
|
||||
ttip.setEnabled(false);
|
||||
}
|
||||
} else {
|
||||
ttip.setEnabled(false);
|
||||
}
|
||||
|
||||
} else {
|
||||
ttip.setEnabled(false);
|
||||
}
|
||||
} else {
|
||||
ttip.setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void clearText() {
|
||||
messages.clear();
|
||||
repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
if (e.getButton() == MouseEvent.BUTTON1) {
|
||||
// Left click
|
||||
int messageId = Math.max(messages.size() - (getHeight() / fontSize), 0)-(int)screenDecalage + Math.floorDiv(e.getY(), fontSize);
|
||||
if (messageId < messages.size()) {
|
||||
ComplexMessage m = messages.get(messageId);
|
||||
String text = m.getConcatedText();
|
||||
int letterPos = 0;
|
||||
int txtSize = 0;
|
||||
FontMetrics fm = getFontMetrics(Console.config.consoleFont);
|
||||
while (letterPos < text.length() && txtSize < e.getX()) {
|
||||
letterPos++;
|
||||
txtSize = fm.stringWidth(text.substring(0, letterPos));
|
||||
}
|
||||
letterPos--;
|
||||
if (letterPos < text.length()) {
|
||||
m.displayFrame(letterPos);
|
||||
}
|
||||
|
||||
}
|
||||
} else if (e.getButton() == MouseEvent.BUTTON2) {
|
||||
// Molette click
|
||||
} else if (e.getButton() == MouseEvent.BUTTON3) {
|
||||
// Right click
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
21
src/com/bernard/main/ConsoleMain.java
Normal file
21
src/com/bernard/main/ConsoleMain.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.bernard.main;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import com.bernard.console.Console;
|
||||
|
||||
public class ConsoleMain {
|
||||
public static void main(String[] args) {
|
||||
Console.createConsole(false);
|
||||
Console.out("Salut");
|
||||
Console.out("c'est");
|
||||
Console.out("moi");
|
||||
double d = 0.65436158364581341;
|
||||
Double[] array = {3.14,15.92,6.53};
|
||||
Console.outA(array);
|
||||
Console.outS(4523,d);
|
||||
Console.out(new Random());
|
||||
Console.out(new TestClass());
|
||||
}
|
||||
|
||||
}
|
||||
21
src/com/bernard/main/TestClass.java
Normal file
21
src/com/bernard/main/TestClass.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.bernard.main;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class TestClass {
|
||||
public long pFIELD = 12;
|
||||
public static long psField = 46;
|
||||
public final long pfField = 4641;
|
||||
public static final long psfField = 4684;
|
||||
|
||||
protected long tFIELD = 120;
|
||||
protected static long tsField = 460;
|
||||
protected final long tfField = 46401;
|
||||
protected static final long tsfField = 46804;
|
||||
|
||||
private long xFIELD = 1452;
|
||||
private static long xsField = 4546;
|
||||
private final long xfField = 464541;
|
||||
private static final long xsfField = 46454;
|
||||
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user