1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
| import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.DirectoryStream.Filter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Objects;
import javax.activation.MimetypesFileTypeMap;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;
public class FileSelectorPanel extends JPanel {
private static final long serialVersionUID = 1L;
private static final byte[] CROSS = {
0x00,0x00,0x00,0x00,0x50,0x00,0x00,0x05,(byte)0xa4,0x01,0x40,0x1a,(byte)0xa4,0x06,(byte)0x90,0x1a,(byte)0x90,0x1a,(byte)0xa4,0x06,0x40,0x6a,0x69,0x00,0x00,(byte)0xa9,0x6a,0x00,0x00,(byte)0xa4,0x1a,0x00,0x00,(byte)0xa4,0x1a,0x00,0x00,(byte)0xa9,0x6a,0x00,0x40,0x6a,(byte)0xa9,0x01,(byte)0x90,0x1a,(byte)0xa4,0x06,(byte)0xa4,0x06,(byte)0x90,0x1a,(byte)0xa4,0x01,0x40,0x1a,0x50,0x00,0x00,0x05,0x00,0x00,0x00,0x00
};
private static final ImageIcon ICON = new ImageIcon(createCrossImage(Color.BLACK, Color.WHITE));
private static final ImageIcon ICON_HOVER = new ImageIcon(createCrossImage(Color.BLACK, Color.LIGHT_GRAY));
private static final ImageIcon ICON_CLICK = new ImageIcon(createCrossImage(Color.BLACK, Color.DARK_GRAY));
private final JList<Path> jlist;
private final JButton button;
private final DefaultListModel<Path> listModel;
private final FileSelectorRenderer cellRenderer;
private String dialogTitle = "Choose file...";
private String approveButtonText = null;
private Path lastDir;
private FileFilter fileFilter;
private Point lastPoint;
private int clickedIndex=-1;
private int hoverIndex=-1;
public FileSelectorPanel(String lastDir, FileFilter fileFilter) {
this(lastDir==null?null:Paths.get(lastDir), fileFilter);
}
public FileSelectorPanel(File lastDir, FileFilter fileFilter) {
this(lastDir==null?null:lastDir.toPath(), fileFilter);
}
public FileSelectorPanel(Path lastDir, FileFilter fileFilter) {
super(new BorderLayout());
this.lastDir = lastDir==null?Paths.get(System.getProperty("user.home")):lastDir;
if ( !Files.isDirectory(lastDir) ) {
lastDir = lastDir.getParent();
if ( lastDir==null ) {
lastDir = Paths.get(System.getProperty("user.home"));
}
}
this.fileFilter = fileFilter;
listModel = new DefaultListModel<>();
jlist = new JList<>(listModel);
jlist.setLayoutOrientation(JList.HORIZONTAL_WRAP);
//jlist.setFixedCellWidth(100);
jlist.setVisibleRowCount(-1);
cellRenderer = new FileSelectorRenderer();
jlist.setCellRenderer(cellRenderer);
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent event) {
if ( event.getButton()==MouseEvent.BUTTON1 ) {
armclick(event.getPoint());
}
}
@Override
public void mouseReleased(MouseEvent event) {
if ( event.getButton()==MouseEvent.BUTTON1 ) {
click(event.getPoint());
}
}
@Override
public void mouseMoved(MouseEvent e) {
mouseHover(e.getPoint());
}
@Override
public void mouseDragged(MouseEvent e) {
mouseHover(e.getPoint());
}
@Override
public void mouseExited(MouseEvent e) {
endHover();
}
};
jlist.addMouseListener(mouseAdapter);
jlist.addMouseMotionListener(mouseAdapter);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
button = new JButton("Choose file");
button.addActionListener(e->chooseFile(fileFilter));
buttonPanel.add(button);
add(new JScrollPane(jlist, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
add(buttonPanel, BorderLayout.SOUTH);
}
private void mouseHover(Point point) {
int index = jlist.locationToIndex(point);
Path item = (Path) jlist.getModel().getElementAt(index);
if ( iconIntersects(index, point, item) ) {
if ( hoverIndex!=index ) {
hoverIndex = index;
jlist.repaint();
}
}
else {
endHover();
}
}
private void endHover() {
if ( hoverIndex!=-1 ) {
hoverIndex = -1;
jlist.repaint();
}
}
private void armclick(Point point) {
lastPoint = point;
int index = jlist.locationToIndex(point);
Path item = (Path) jlist.getModel().getElementAt(index);
if ( iconIntersects(index, point, item) ) {
if ( clickedIndex!=index ) {
clickedIndex = index;
jlist.repaint();
}
}
}
private void click(Point point) {
if ( lastPoint==null || !jlist.getBounds().contains(point) || point.distance(lastPoint)>5 ) {
clickedIndex = -1;
return;
}
int index = jlist.locationToIndex(point);
Path item = (Path) jlist.getModel().getElementAt(index);
if ( iconIntersects(index, point, item) ) {
listModel.removeElement(item);
}
clickedIndex = -1;
}
private boolean iconIntersects(int index, Point point, Path item) {
Rectangle viewR = new Rectangle();
viewR.setFrame(jlist.getCellBounds(index, index));
Rectangle iconR = new Rectangle();
Rectangle textR = new Rectangle();
SwingUtilities.layoutCompoundLabel(cellRenderer,
cellRenderer.getFontMetrics(cellRenderer.getFont()),
item.getFileName().toString(),
cellRenderer.getIcon(),
cellRenderer.getVerticalAlignment(),
cellRenderer.getHorizontalAlignment(),
cellRenderer.getVerticalTextPosition(),
cellRenderer.getHorizontalTextPosition(),
viewR,
iconR,
textR,
cellRenderer.getIconTextGap());
return iconR.contains(point);
}
public void setDialogTitle(String dialogTitle) {
this.dialogTitle = dialogTitle;
}
public void setApproveButtonText(String approveButtonText) {
this.approveButtonText = approveButtonText;
}
public void setChooseButtonText(String buttonText) {
this.button.setText(buttonText);
}
private void chooseFile(FileFilter fileFilter) {
JFileChooser chooser = new JFileChooser(lastDir.toFile());
if ( dialogTitle!=null ) chooser.setDialogTitle(dialogTitle);
chooser.setFileFilter(fileFilter);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setMultiSelectionEnabled(true);
if ( chooser.showDialog(this, approveButtonText)==JFileChooser.APPROVE_OPTION ) {
add(chooser.getSelectedFiles());
lastDir = chooser.getCurrentDirectory().toPath();
}
}
public void add(Collection<?> objects) {
for(Object object : objects) {
if ( object instanceof Path) {
add((Path)object);
}
else if ( object instanceof File ) {
add((File)object);
}
}
}
public void add(Path...paths) {
for(Path path : paths ) {
add(path);
}
}
public void add(File...files) {
for(File file : files ) {
add(file);
}
}
public void add(File file) {
add(file.toPath());
}
public void add(Path path) {
Objects.requireNonNull(path);
listModel.addElement(path);
}
public Path getDirectory() {
return lastDir;
}
public void setDirectory(File file) {
setDirectory(file.toPath());
}
public void setDirectory(Path dir) {
listModel.removeAllElements();
if ( dir!=null && Files.isDirectory(dir)) {
try(DirectoryStream<Path> ds = Files.newDirectoryStream(dir, new Filter<Path>() {
@Override
public boolean accept(Path entry) throws IOException {
if ( Files.isRegularFile(entry) ) {
if ( fileFilter==null ) {
return true;
}
else {
return fileFilter.accept(entry.toFile());
}
}
else {
return false;
}
}
})) {
for(Path path : dir) {
add(path);
}
} catch (IOException e) {
}
}
}
public void setDirectory(String path) {
setDirectory(Paths.get(path));
}
private class FileSelectorRenderer extends DefaultListCellRenderer {
private static final long serialVersionUID = 1L;
public FileSelectorRenderer() {
setIconTextGap(5);
setHorizontalTextPosition(SwingConstants.LEFT);
}
@Override
public Component getListCellRendererComponent(@SuppressWarnings("rawtypes") JList list, Object obj, int index, boolean isSelected, boolean cellHasFocus) {
Path path = (Path)obj;
Component comp = super.getListCellRendererComponent(list, path.getFileName().toString(), index, false, cellHasFocus);
if ( clickedIndex!=-1 ) {
if ( index==clickedIndex && index==hoverIndex ) {
setIcon(ICON_CLICK);
} else {
setIcon(ICON);
}
}
else if ( index==hoverIndex ) {
setIcon(ICON_HOVER);
}
else {
setIcon(ICON);
}
return comp;
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FileSelectorPanel flowPanel = new FileSelectorPanel(System.getProperty("user.dir"), new FileFilter() {
@Override
public String getDescription() {
return "Images";
}
@Override
public boolean accept(File f) {
return f.isDirectory() || isImage(f.getName());
}
private boolean isImage(String name) {
return MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(name).startsWith("image/");
}
});
flowPanel.setDirectory(flowPanel.getDirectory());
frame.add(flowPanel);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static BufferedImage createCrossImage(Color border, Color main) {
BufferedImage image = new BufferedImage(16,16,BufferedImage.TYPE_INT_ARGB);
for(int i=0; i<CROSS.length; i++) {
int b=CROSS[i];
for(int j=0; j<4; j++) {
int x=(i*4+j)%16;
int y=(i*4+j)/16;
switch(b&3) {
case 1:
image.setRGB(x, y, border.getRGB());
break;
case 2:
image.setRGB(x, y, main.getRGB());
break;
}
b>>=2;
}
}
return image;
}
} |