Categoria: Java

JExplorerTree – Swing / Java

Developing a file manager is not such a trivial task, there are many micro operations that have to be taken into account. This project was born for this purpose.

If you are developing an application (or IDE) that needs a project file manager, this component was developed for this purpose:

Features

  • Lazy load (only when it expands)
  • No extra dependencies (extends JPanel and use a JTree)
  • External Drag and Drop (from and to OS)
  • Internal Drag and Drop (inside tree)
  • Keyboard Support: F2, DELETE, ENTER
  • CTRL+C, CTRL+V, CTRL+X (internal)
  • CTRL+V (from external Files)
  • CTRL+C (allow copy form JTree to external)
  • React at external changes (grayout)
  • Native Icons Support
  • Basic operations
    • New File
    • New Directory
    • Rename
    • Refresh (sync external changes.)
    • Multi-File Copy / Delete
    • Copy / Move Directory (see #1)
    • Delete (with UNDO)

https://github.com/ricardojlrufino/JExplorerTree

Mensure Execution time in Java

[PT] Vim aqui para compartilhar uma simples classe para calcular o tempo de execução de vários métodos de forma prática, sem precisar ficar criando variáveis ou efetuando cálculos…

[EN] I came here to share a simple class to calculate the execution time of various methods in a practical way, without having to create variables or perform calculations …

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;

public class MensureTime {
  
  public static final Map<String, Long> mensures = new HashMap<>();
  
  private static long startTime = System.nanoTime();
  
  public static void start() {
    startTime = System.nanoTime();
  }
  
  public static void end(String name) {
    
    long endTime = System.nanoTime();
    
    long durationInNano = (endTime - startTime);  //Total execution time in nano seconds
    
    long durationInMillis = TimeUnit.NANOSECONDS.toMillis(durationInNano);  //Total execution time in nano seconds
    
    System.out.println(" ##### " + name +  " - time: " + (durationInMillis) + "ms  -- thread:" + Thread.currentThread().getName());
    
    mensures.put(name, durationInMillis);
  }
  
  public static void printResults() {
    System.out.println("====[ RESULTS ]======");
    for (String name : mensures.keySet()) {
      System.out.println(" ##### " + name +  " - time: " + (mensures.get(name)) + "ms");
    }
  }
  
  public static void printResultsGui() {
    JFrame frame = new JFrame("Results");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
    DefaultListModel<String> model = new DefaultListModel<>();
    JList<String> list = new JList<>();
    
    for (String name : mensures.keySet()) {
      model.addElement(" ##### " + name +  " - time: " + (mensures.get(name)) + "ms");
    }
    
    list.setModel(model);
    frame.add(list);
    frame.setSize(400, 500);
    frame.setVisible(true);
    
  }
}
    MensureTime.start();
    BaseNoGui.initLogger();
    MensureTime.end("initLogger");
    
    MensureTime.start();
    BaseNoGui.initPlatform();
    MensureTime.end("initPlatform");
    
    MensureTime.printResults();

##### initLogger – time: 93ms — thread:main
##### initPlatform – time: 4ms — thread:main
====[ RESULTS ]======
##### initLogger – time: 93ms
##### initPlatform – time: 4ms

References

Support multiple versions of Java

On ubuntu/linux you can switch java version using

update-alternatives –config java

But before, you need install the version.

You can use this script (./install-java.sh) to install multiple JVMs

#!/bin/sh
(
lookforJdks=$PWD
echo "Directory: $lookforJdks"
jdks=`test -e ./javac || find $lookforJdks -type d -iname '*jdk1.*' 2> /dev/null`
#set -e
echo 'which jdk do you want to choose? looking for jdks. This might take a while'
echo "$jdks" | awk '{printf("%5d : %s\n", NR,$0)}'
read choose
test -e ./javac || cd `echo "$jdks" | tr '\n' ',' | cut -d',' -f $choose`/bin
for e in appletviewer extcheck idlj jar jarsigner java javac javadoc javah javap jconsole \
 jdb jhat jinfo jmap jps jrunscript jsadebugd jstack jstat jstatd native2ascii rmic \
 schemagen serialver wsgen wsimport xjc jvisualvm jmc; do sudo update-alternatives \
 --install /usr/bin/$e $e $(readlink -f ./$e) 100; done
)

echo "RUN update-alternatives --config java"

Put this script in folder where has unpacked the JVM(s), an run:

./install-java.sh

Next use: update-alternatives –config java