JTree expandPath works with File but not with wrapper class - java

I wonder what the requirements are so that the JTree properly works. I have written some code, displaying a filesystem tree and then call expandPath() to expand a path on it.
When I use a File object everything works fine, but the tree shows the full path on each node. So I wrapped it in a class which changes the toString() so that only the dir/filename is shown, but now expandPath() no longer works and the path is not expanded.
package tools.controls.TreeControl.Filesystem;
import java.io.File;
import java.net.URI;
public class TreeFile
extends File
implements Cloneable
, Comparable<File>
{
private static final long serialVersionUID = 1L;
public TreeFile(TreeFile oOther)
{
super(oOther.getParentFile(), oOther.getName());
}
public TreeFile(File oFile)
{
super(oFile.getParentFile(), oFile.getName());
}
public TreeFile(String oPath)
{
super(oPath);
}
public TreeFile(URI oFileURL)
{
super(oFileURL);
}
public TreeFile(String oParent, String oChild)
{
super(oParent, oChild);
}
public TreeFile(File oParent, String oChild)
{
super(oParent, oChild);
}
public TreeFile getParentTreeFile()
{
File f = getParentFile();
if(f == null)
return null;
return new TreeFile(f);
}
public String toString()
{
return getName();
}
public TreeFile clone()
{
return new TreeFile(this);
}
#Override
public int compareTo(File oObject)
{
return super.compareTo(oObject);
}
}

Related

how to implement public AbstractEntryProcessor(boolean applyOnBackup){} in 5.x.x for the backup in Hazelcast

Help me in the following code and how to used the backup on the Hazelcast
migration of the hazelcast 3.x.x to 5.x.x
package com.hazelcast.map;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.HazelcastInstanceAware;
import com.hazelcast.nio.serialization.impl.BinaryInterface;
import java.util.Map;
// Interface AbstractEntryProcessor
#BinaryInterface
public abstract class AbstractEntryProcessor<K,V> implements EntryProcessor<K,V> {
private final EntryBackupProcessor<K,V> entryBackupProcessor;
// Non Parameterize Constructor
public AbstractEntryProcessor() {
this(true);
}
// Parameterize Constructor AbstractEntryProcessor
public AbstractEntryProcessor(boolean applyOnBackup) {
if (applyOnBackup) {
entryBackupProcessor = new EntryBackupProcessorImpl();
} else {
entryBackupProcessor = null;
}
}
//EntryBackupProcessor
#Override
public final EntryBackupProcessor getBackupProcessor() {
return entryBackupProcessor;
}
// class EntryBackupProcessorImpl
private class EntryBackupProcessorImpl implements EntryBackupProcessor<k,V>, HazelcastInstanceAware {
// generated for EntryBackupProcessorImpl which doesn't implement HazelcastInstanceAware
static final long serialVersionUID = -5081502753526394129L;
#Override
public void processBackup(Map.Entry<K,V> entry) {
process(entry);
}
#Override
public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
final AbstractEntryProcessor<k,V> outer = AbstractEntryProcessor.this;
if (outer instanceof HazelcastInstanceAware) {
((HazelcastInstanceAware) outer).setHazelcastInstance(hazelcastInstance);
}
}
}
}
How to used the backup methods in 5.x.x versons of series
how to used the backup in the above question ?
This should work:
public abstract class AbstractEntryProcessor implements EntryProcessor, HazelcastInstanceAware {
protected transient HazelcastInstance hazelcastInstance;
private final boolean applyOnBackup;
// Non Parameterize Constructor
public AbstractEntryProcessor() {
this(true);
}
// Parameterize Constructor AbstractEntryProcessor
public AbstractEntryProcessor(boolean applyOnBackup) {
this.applyOnBackup = applyOnBackup;
}
//EntryBackupProcessor
#Override
public final EntryProcessor getBackupProcessor() {
if (!applyOnBackup || this instanceof ReadOnly) {
return null;
}
return this;
}
#Override
public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
this.hazelcastInstance = hazelcastInstance;
}
}

Designing resource access: Final enums vs static members of extendable classes

I am trying to design a framework for loading resources. So I have two options
Approach 1
Underlying structure: An interface with a simple abstract class implementation and more specific extends.
Access: static final members of class initialized to the class.
Approach 2
Underlying structure: A specific enum from the get go, implementing all interface methods: no leveraging partial implementations of abstract classes.
Access: as enum entries
While approach 1 is a lot more flexible and reusable, I like the way enums provide a clean usable list, ready to use as opposed to static finals with scope for Class.instance.instance.instance. ...
Is there a standard way to do this? Is there a better way to do this?
Though not strictly needed here's the code
Approach 1
Interface LoadableResource<T>
import java.util.ArrayDeque;
import java.util.Queue;
public interface LoadableResource<T> {
Queue<Exception> exceptionQueue=new ArrayDeque<>();
boolean load();//Load the resource and return status
boolean isLoaded() ;
T getResource();
void onLoadFail();
void onLoadSuccess();
void onException(Exception ex);
Queue<Exception> getExcpetions();
}
Abstract SimpleLoadableResource<T>
import java.util.Queue;
public abstract class SimpleLoadableResource<T> implements LoadableResource<T> {
private boolean FLAG_LOADED = false;
private T resource;
#Override
public boolean isLoaded() {
return FLAG_LOADED;
}
#Override
public T getResource() {
return resource;
}
#Override
public void onLoadFail() {}
#Override
public void onLoadSuccess() {}
#Override
public void onException(Exception ex) {exceptionQueue.add(ex); }
#Override
public Queue<Exception> getExcpetions() { return exceptionQueue; }
protected void setLoaded(boolean FLAG_LOADED) {
this.FLAG_LOADED = FLAG_LOADED;
}
public abstract T loader() throws Exception;
#Override
public boolean load() {
try {
resource=loader();
} catch (Exception e) { onException(e); }
if (isLoaded())
onLoadSuccess();
else
onLoadFail();
return isLoaded();
}
}
Specific SimpleLoadableImage
import javax.imageio.ImageIO;
import java.awt.*;
public class SimpleLoadableImage extends SimpleLoadableResource<Image>{
public static final SimpleLoadableImage LOGO1=new SimpleLoadableImage("1.jpg");
public static final SimpleLoadableImage LOGO2=new SimpleLoadableImage("2.jpg");
private final String path;
public SimpleLoadableImage(String path) {
this.path = path;
super.load();
}
#Override
public Image loader() throws Exception {
var res=ImageIO.read(this.getClass().getClassLoader().getResourceAsStream(path));
setLoaded(true);
return res;
}
}
Approach 2
import javax.imageio.ImageIO;
import java.awt.*;
import java.util.ArrayDeque;
import java.util.Queue;
public enum SimpleLoadableImage_Enum {
LOGO1("1.jpg"),
LOGO("2.jpg");
private final String path;
Queue<Exception> exceptionQueue=new ArrayDeque<>();
private boolean FLAG_LOADED = false;
private Image resource;
private SimpleLoadableImage_Enum(String path){
this.path=path;
try {
resource=ImageIO.read(this.getClass().getClassLoader().getResourceAsStream(path));
FLAG_LOADED=true;
} catch (Exception e) {
exceptionQueue.add(e);
}
}
public boolean isLoaded() { return FLAG_LOADED; }
public Image getResource() { return resource; }
public Queue<Exception> getExcpetions() { return exceptionQueue; }
}

Restrain the type while inheriting

I created a java project to apply my GraphTheory course and enhance my java skills.
In this project :
I created a class Sommet<S>(Vertex in English) with an attribute Id with a generic type called <S>.
I created a class Arc<S>(Edge in English) with two attributes Sommet(Vertex).
I created a class EnsembleArc which is an HashSet of Arc
I also created a class ArcValue which inherit from Arc and have an int attribute Valeur(Value in English)
Here everything is fine and I dont have any problem.
But then I created a class EnsembleArcValue which inherit from EnsembleArc because every method from EnsembleArc will be useful to EnsembleArcValue.
But I also want EnsembleArcValue to be an HashSet of ArcValue (and I dont want an Arc which is not an ArcValue). And with the inheritance EnsembleArcValue is able to have an "simple" Arc in his Set.
So my question after all this explanation is :
Is there a way for EnsembleArcValue to inherit from EnsembleArc but will only accept an ArcValue in his Set.
Here is an image of The UML Project
I hope it will help to understand my problem (dont look at the bottom).
Here is the code :
public class Sommet<S>
{
//attributes
private S id;
public Sommet(S s)
{
setId(s);
}
public S getId()
{
return id;
}
public void setId(S s)
{
assert s!= null: "Objet null passé en paramètre";
id = s;
}
#SuppressWarnings("unchecked")
#Override
public boolean equals(Object obj)
{
boolean callback;
if(obj.getClass()!=getClass())
{
callback=false;
}
else
{
if(((Sommet<S>)obj).getId().equals(getId()))
{
callback=true;
}
else
{
callback=false;
}
}
return callback;
}
#Override
public int hashCode()
{
return getId().hashCode();
}
#Override
public String toString()
{
return getId().toString();
}
}
public class Arc<S>
{
private Sommet<S> depart;
private Sommet<S> arrivee;
public Arc(Sommet<S> dep, Sommet<S> arr)
{
setDepart(dep);
setArrivee(arr);
}
#Override
public String toString()
{
String str="("+getDepart().getId()+","+getArrivee().getId()+")";
return str;
}
public Sommet<S> getDepart()
{
return depart;
}
public Sommet<S> getArrivee()
{
return arrivee;
}
public void setDepart(Sommet<S> depart)
{
this.depart = depart;
}
public void setArrivee(Sommet<S> arrivee)
{
this.arrivee = arrivee;
}
#SuppressWarnings("unchecked")
#Override
public boolean equals(Object obj)
{
boolean callback;
if(obj.getClass()!=getClass())
{
callback=false;
}
else
{
if(((Arc<S>)obj).getDepart().equals(getDepart())&&((Arc<S>)obj).getArrivee().equals(getArrivee()))
{
callback=true;
}
else
{
callback=false;
}
}
return callback;
}
#Override
public int hashCode()
{
return getArrivee().hashCode()+getDepart().hashCode();
}
}
public class ArcValue<S,V> extends Arc<S>
{
private V valeur;
public ArcValue (Sommet<S> depart, Sommet<S> arrivee, V valeur)
{
super(arrivee,depart);
this.valeur=valeur;
}
public V getValeur()
{
return valeur;
}
}
import java.util.HashSet;
public class Ensemble<E> extends HashSet<E> implements Cloneable
{
private static final long serialVersionUID = -4354387895748449845L;
public Ensemble ()
{
super();
}
public Ensemble (Ensemble<E> ensemble)
{
for (E e : ensemble)
{
add(e);
}
}
public String toString()
{
StringBuffer str=new StringBuffer("{");
for(E e: this)
{
str=str.append(e.toString()+",");
}
str.setCharAt(str.length()-1, '}');
return str.toString();
}
#SuppressWarnings("unchecked")
#Override
public Ensemble<E> clone()
{
return (Ensemble<E>)super.clone();
}
}
public class EnsembleArc<S> extends Ensemble<Arc<S>>
{
public EnsembleArc(Ensemble<Arc<S>> ensemble)
{
super(ensemble);
}
public EnsembleArc()
{
super();
}
private static final long serialVersionUID = -4099925554493145279L;
public EnsembleSommet<S> listSucc(Sommet<S> sommet)
{
EnsembleSommet<S> XSucc=new EnsembleSommet<S>();
for (Arc<S> arc : this)
{
if (arc.getDepart()==sommet)
{
XSucc.add(arc.getArrivee());
}
}
return XSucc;
}
public EnsembleSommet<S> listPred(Sommet<S> sommet)
{
EnsembleSommet<S> XPred=new EnsembleSommet<S>();
for (Arc<S> arc : this)
{
if (arc.getArrivee()==sommet)
{
XPred.add(arc.getDepart());
}
}
return XPred;
}
public void add(Sommet<S> depart,Sommet<S>arrivee)
{
add(new Arc<S>(depart,arrivee));
}
#Override
public EnsembleArc<S> clone ()
{
return (EnsembleArc<S>)super.clone();
}
}
//import java.util.Collection;
public class EnsembleArcValues<S,V> extends EnsembleArc<S> //implements Collection<ArcValue<S,V>>
{
//TODO faire en sorte que ensembleArcValués ne contienne que des ArcsValue
private static final long serialVersionUID = -7163498825360866323L;
}
And you'll need this one to :
public class EnsembleSommet<S> extends Ensemble<Sommet<S>>
{
public EnsembleSommet()
{
super();
}
public EnsembleSommet(EnsembleSommet<S> ensemble)
{
super(ensemble);
}
private static final long serialVersionUID = 7278825382690341067L;
#Override
public EnsembleSommet<S> clone ()
{
return (EnsembleSommet<S>)super.clone();
}
public Sommet<S> firstSommet()
{
#SuppressWarnings("unchecked")
Sommet<S>[] tab=new Sommet[size()];
return toArray(tab)[0];
}
}
The only way you can achieve this is to make the type of Arc you want part of your generic deceleration. Rename your existing EnsembleArc to AbstractEnsembleArc and change it's generic decleration from < S > to < S, T extends Arc< S > > i.e.:
public abstract class AbstractEnsembleArc<S, T extends Arc<S>> extends Ensemble<T> {
// PUT ALL YOUR LOGIC CURRENTLY IN EnsembleArc HERE
}
Now create a new Class Called EnsembleArc and extend the new abstract class you've added, this new class will work identically to your existing EnsembleArc and class decleration should now look like:
public class EnsembleArc<S> extends AbstractEnsembleArc<S, Arc<S>> {
}
Finally have EnsembleArcValues extend the Abstract class instead of EnsembleArc so that you can declare that it should only accepts ArcValue and not simple Arc, do that like this:
public class EnsembleArcValues<S, V> extends AbstractEnsembleArc<S, ArcValue<S, V>> {
}

How to get methods inside a class

So I'm working on a plugin loader for a game and what it does is load plugins (class objects) from a .jar inside a folder, then add them to an ArrayList (so users can make their own mods). But my java experience isn't the best :/ So far it gets the classes in the jar but I need to check what methods are in the class (such as the constructor for labeling). Or if someone knows a better way to do this. Any help would be appreciated.
Plugin.java:
package me.rigamortis.faurax.plugin;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import net.minecraft.client.Minecraft;
import me.rigamortis.faurax.core.Core;
public class Plugin {
public Minecraft mc = Minecraft.getMinecraft();
final File folder = new File(mc.mcDataDir + File.separator + "Faurax/Plugins");
public String jar;
public static String className;
private String name;
private String desc;
private int color;
private int key;
public Plugin() {
if(!folder.exists())
folder.mkdirs();
loadPlugins(folder);
if(folder != null)
getClassNames(folder);
}
public void setName(String newName) {
name = newName;
}
public void setDesc(String newDesc) {
desc = newDesc;
}
public void setColor(int newColor) {
color = newColor;
}
public void setKey(int newKey) {
key = newKey;
}
public String getName() {
return this.name;
}
public String getDesc() {
return this.desc;
}
public int getKey() {
return this.key;
}
public int getColor() {
return this.color;
}
/**
* A hook for plugins
*/
public void onTick() {}
public void loadPlugins(final File folder) {
try{
for (final File fileEntry : folder.listFiles()) {
if ( !fileEntry.isDirectory()) {
if(fileEntry.getName().endsWith(".jar")){
jar = fileEntry.getName();
Core.logNormal("Loading " + fileEntry.getName());
}
}
}
}
catch(Exception e) {
Core.logError("Error? jar isin't a plugin.");
e.printStackTrace();
}
}
public void getClassNames(final File dir) {
try{
ZipInputStream zip = new ZipInputStream(new FileInputStream(dir + File.seperator + jar));
for(ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry())
if(entry.getName().endsWith(".class") && !entry.isDirectory()) {
Core.logNormal("Found "+entry);
className = entry.toString();
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
PluginLoader.java:
package me.rigamortis.faurax.plugin;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.client.Minecraft;
import me.rigamortis.faurax.core.Core;
public class PluginLoader {
public static List<Plugin> plugins = new ArrayList<Plugin>();
public PluginLoader() {
loadPlugins();
}
public void loadPlugins() {}
}
I answered a similar question to this not too long ago. Here's the code to get all methods from a given class, I think it's fairly self-explanatory:
void foo(Object o)
{
Class<?> c = o.getClass();
for (Method m : c.getDeclaredMethods())
{
//do whatever
}
}
This takes advantage of the Java's Reflection API. You can read more about it here.

what data structure to use in my case?

A FileManager Class has a static filed to hold a file collection, this collection may contains files or folders or both , a folder may contains files or folders or both, the FileManager Class contains public method for client code to call such as addFile, addFolder, deleteFile, deleteFolder, these method operate on the collection. My question is:
What java data structure is best for this case ?
How to create model class for File and Folder ?
some example will be good.
best regars.
// added # 2011/05/27
thank everybody !
acutaly I am trying to buidl a eclipse-rcp application to manage some jdbc connection profile.
here is my code:
package com.amarsoft.sysconfig.plugin.model;
/**
* #author ggfan#amarsoft
*
*/
public class TreeNode {
/**
* unique key
*/
private String key;
/**
* used as label in a JFace TreeViewer,
*/
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setKey(String key) {
this.key = key;
}
public String getKey() {
return key;
}
}
public class LeafNode extends TreeNode {
private FolderNode parent;
public void setParent(FolderNode parent) {
this.parent = parent;
}
public TreeNode getParent() {
return parent;
}
}
package com.amarsoft.sysconfig.plugin.model;
import java.util.List;
public class FolderNode extends TreeNode {
private List<TreeNode> children;
public void setChildren(List<TreeNode> children) {
this.children = children;
}
public List<TreeNode> getChildren() {
return children;
}
}
package com.amarsoft.sysconfig.plugin.model;
import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;
/**
* 连接配置模型类
* #author ggfan#amarsoft
*
*/
public class ConnectionProfile extends LeafNode{
/**
* url
*/
private String url;
/**
* JDBC driver id
*/
private int driver;
/**
* user name for logon
*/
private String user;
/**
* password for logon
*/
private String pswd;
/**
* default constructor
*/
public ConnectionProfile() {
}
/**
* construct a instance using a XML element
* #param xmlElement the XML element
*/
public ConnectionProfile(Element xmlElement){
this.setName(xmlElement.attributeValue("name"));
this.setUrl(xmlElement.element("url").getTextTrim());
this.setUser(xmlElement.element("user").getTextTrim());
this.setPswd(xmlElement.element("password").getTextTrim());
}
/**
* serialize as XML
* #return
*/
public Element asXML(){
Element e = new DefaultElement("profile");
e.addAttribute("name", this.getName());
e.addElement("url", escapeNull(this.getUrl()));
e.addElement("user", escapeNull(this.getUser()));
e.addElement("password", escapeNull(this.getPswd()));
return e;
}
private String escapeNull(String s) {
return s == null ? "" : s;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPswd() {
return pswd;
}
public void setPswd(String pswd) {
this.pswd = pswd;
}
public void setDriver(int driver) {
this.driver = driver;
}
public int getDriver() {
return driver;
}
}
public class ConnectionProfileManager {
private static List<TreeNode> profiles = new ArrayList<TreeNode>();
public static void loadProfiles() throws DocumentException{
Element profiles = XMLUtil.readRoot(ConnectionProfileManager.class.getResourceAsStream("samples_profile.xml"));
//Element profiles = XMLUtil.readRoot(new File(ApplicationFiles.CONNNECTION_PROFILES));
if(profiles != null){
for(Element profile : profiles.elements()){
loadNode(profile, ConnectionProfileManager.profiles);
}
}
}
private static void loadNode(Element node, List<TreeNode> parent){
if(node.getName().equals(XMLConstants.CP_TAG_PROFILE)){
ConnectionProfile profile = new ConnectionProfile(node);
parent.add(profile);
}else if(node.getName().equals(XMLConstants.CP_TAG_FOLDER)){
FolderNode folder = new FolderNode();
folder.setChildren(new ArrayList<TreeNode>());
folder.setName(node.attributeValue(XMLConstants.CP_ATTR_NAME));
for(Element child : node.elements()){
loadNode(child, folder.getChildren());
}
parent.add(folder);
}
}
public static void saveProfiles(){
Element root = new DefaultElement(XMLConstants.CP_TAG_PROFILES);
for(TreeNode node : ConnectionProfileManager.profiles){
saveNode(node, root);
}
XMLUtil.save(root, new File("c:\\1.xml"));
}
private static void saveNode(TreeNode node, Element root) {
if(node instanceof ConnectionProfile){
ConnectionProfile p = (ConnectionProfile)node;
root.add(p.asXML());
}else if(node instanceof FolderNode){
FolderNode folder = (FolderNode)node;
Element e = new DefaultElement(XMLConstants.CP_TAG_FOLDER);
e.addAttribute(XMLConstants.CP_ATTR_NAME, node.getName());
for(TreeNode child : folder.getChildren()){
saveNode(child, e);
}
root.add(e);
}
}
public static void addProfile(ConnectionProfile profile){
profiles.add(profile);
}
public static void addProfile(TreeNode parentNode, ConnectionProfile profile){
}
public static List<TreeNode> getProfiles() {
return profiles;
}
}
with these class I get my tree works, but I found It's hard to support add operation.
You've kinda of dictacted the answer already in the question..
The File class is (according to the JavaDocs) an:
abstract representation of file and directory pathnames.
So from what you've described:
// A file manager class
class FileManager {
// has a static field to hold a file collection
static Collection<File> fileCollection;
// contains public methods such as
public addFile(File f) { }
public deleteFile(File f) { }
public addFolder(File f) { }
public deleteFolder(File f { }
}
If you have to look at implementing your own version of the File class, then the JavaDocs for that should be a good start to understanding this.
AS to what collection is best for the file collection, I think a Set makes most sense. There's no point having more than one file (e.g. a List and two entries of the same file would be meaningless), and testing membership of a set is a very quick operation. For example, in addFile you might check it exists before trying to add, and similarly for delete you'd want to make sure that it exists before you delete it.
A couple of points about the design you've mentioned.
Static fields like this as nasty. They make it difficult to test and are a pain for multi-threading. Could you make it an instance variable?
Given that File is an abstract representation of a path name, why'd you need the method addFile and addFolder, they are going to have the same implementation?
A Collection of Files?
Most collections support add and delete, so there is no need for a special data structure. Java File can be a file and a directory. Simply call isDirectory to find out if it is a directory or a file.
Unless you have more requirements, I think this would make up a pretty easy to use FileManager:
List<File> fileManager = new ArrayList<File>();

Categories

Resources