Skip to content

Commit

Permalink
add missing tool classes
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Nov 20, 2024
1 parent a7530ee commit c4000cc
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 0 deletions.
100 changes: 100 additions & 0 deletions src/main/java/ai/nets/samj/gui/components/ComboBoxItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*-
* #%L
* Library to call models of the family of SAM (Segment Anything Model) from Java
* %%
* Copyright (C) 2024 SAMJ developers.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package ai.nets.samj.gui.components;


import net.imglib2.RandomAccessibleInterval;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.RealType;
/**
* Class that allows identifying the objects in the list of a ComboBox by a unique identifier.
* This class helps associating a unique identifier to objects specific to a software.
* This class needs to be implemented on each software to be able to handle the native structures for images
* from the particualr software.
* @author CArlos Garcia
*/
public abstract class ComboBoxItem {
final private int id;
final private Object value;
private static final String SELECT_IMAGE_STR = "Select image";

/**
* An object that associates a unique identifier with an Object.
* The unique identifier cannot be -1, as it is reserved for empty objects
* @param uniqueID
* the unique identifier
* @param seq
* the object
*/
public ComboBoxItem(int uniqueID, Object seq) {
if (uniqueID == -1) throw new IllegalArgumentException("The unique identifier cannot be -1.");
this.id = uniqueID;
this.value = seq;
}

/**
* An object that associates a unique identifier with an Object.
* In this case it is empty, the unique identifier will by -1.
*/
public ComboBoxItem() {
this.id = -1;
this.value = null;
}

/**
*
* @return the unique identifier of the instance
*/
public int getId() {
return id;
}

/**
*
* @return the Object of interest
*/
public Object getValue() {
return value;
}

/**
*
* @return the name of the image. The way to retrieve the image (Object) name depends on the software where this is used,
* so it needs to be implemented on the software side
*/
public abstract String getImageName();

/**
* Convert the software specific image into an ImgLib2 {@link RandomAccessibleInterval}
* @param <T>
* the possible ImgLib2 data types of the {@link RandomAccessibleInterval}
* @return the {@link RandomAccessibleInterval} created from the software image object
*/
public abstract < T extends RealType< T > & NativeType< T > > RandomAccessibleInterval<T> getImageAsImgLib2();

@Override
/**
* {@inheritDoc}
*/
public String toString() {
if (value == null) return SELECT_IMAGE_STR;
return getImageName();
}
}
118 changes: 118 additions & 0 deletions src/main/java/ai/nets/samj/gui/tools/Files.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*-
* #%L
* Library to call models of the family of SAM (Segment Anything Model) from Java
* %%
* Copyright (C) 2024 SAMJ developers.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package ai.nets.samj.gui.tools;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;

/**
* Class that contains helper methods to browse and get specific files in the {@link JFileChooser}
* graphical user interface
* @author Daniel Sage
*/
public class Files {

/**
*
* @return a String with the full path of the directory from where the code is being executed
*/
public static String getWorkingDirectory() {
return System.getProperty("user.dir");
}

/**
*
* @return the path to the home directory of the computer
*/
public static String getHomeDirectory() {
return FileSystemView.getFileSystemView().getHomeDirectory().getAbsolutePath() + File.separator;
}

/**
*
* @return String path to the desktop directory
*/
public static String getDesktopDirectory() {
return getHomeDirectory() + "Desktop" + File.separator;
}

/**
* Method that returns a {@link File} if the path provided is a File or false otherwise
* @param path
* String path to the file of interest
* @return a {@link File} object for the file of interest or null if it does not exist
*/
public static File browseFile(String path) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
File dir = new File(path);
if (dir.exists())
fc.setCurrentDirectory(dir);

int ret = fc.showOpenDialog(null);
if (ret == JFileChooser.APPROVE_OPTION) {
File file = new File(fc.getSelectedFile().getAbsolutePath());
if (file.exists())
return file;
}
return null;
}

/**
* Method that returns a {@link File} if the path provided is a directory or false otherwise
* @param path
* String path to the directory of interest
* @return a {@link File} object for the directory of interest or null if it does not exist
*/
public static File browseDirectory(String path) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
File dir = new File(path);
if (dir.exists())
fc.setCurrentDirectory(dir);

int ret = fc.showOpenDialog(null);
if (ret == JFileChooser.APPROVE_OPTION) {
File file = new File(fc.getSelectedFile().getAbsolutePath());
if (file.exists())
return file;
}
return null;
}
/**
* Delete all the contents of a directory
* @param folder
* the File which is going to be deleted
*/
public static void deleteFolder(File folder) {
File[] files = folder.listFiles();
if (files == null) {
folder.delete();
return;
}
for(File f: files) {
if(f.isDirectory()) deleteFolder(f);
else f.delete();
}
folder.delete();
}
}
50 changes: 50 additions & 0 deletions src/main/java/ai/nets/samj/gui/tools/Tools.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*-
* #%L
* Library to call models of the family of SAM (Segment Anything Model) from Java
* %%
* Copyright (C) 2024 SAMJ developers.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package ai.nets.samj.gui.tools;

import java.awt.Desktop;
import java.net.URL;

/**
* Class that contains helper tools for SAMJ
* @author Daniel Sage
* @author Carlos Garcia
*/
public class Tools {

/**
* Method that opens a website where there is some documentation about SAMJ
* @return true if the method was able to access the website or false otherwise
*/
static public boolean help() {
String url = "https://github.com/segment-anything-models-java/SAMJ-IJ";
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
try {
desktop.browse(new URL(url).toURI());
return true;
}
catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
}

0 comments on commit c4000cc

Please sign in to comment.