-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
GRADLE=gradle | ||
EXT=ext-search-samples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.riekr.jloga.prefs; | ||
|
||
import javax.swing.*; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
import org.riekr.jloga.Main; | ||
import org.riekr.jloga.ui.MenuSelectedListener; | ||
|
||
public final class Favorites { | ||
private Favorites() {} | ||
|
||
private static final class Lazy { | ||
private Lazy() {} | ||
|
||
public static final JPopupMenu MENU; | ||
|
||
static { | ||
String favoritesFileName = System.getProperty("jloga.favorites"); | ||
JPopupMenu menu = null; | ||
if (favoritesFileName != null && !(favoritesFileName = favoritesFileName.trim()).isEmpty()) { | ||
List<JMenuItem> menuItems = new ArrayList<>(); | ||
try (FileReader reader = new FileReader(favoritesFileName)) { | ||
Properties props = new Properties(); | ||
props.load(reader); | ||
for (Map.Entry<Object, Object> entry : props.entrySet()) { | ||
String title = entry.getKey().toString(); | ||
File folder = new File(entry.getValue().toString()); | ||
if (folder.isDirectory()) | ||
menuItems.add(scan(folder, title)); | ||
} | ||
if (!menuItems.isEmpty()) { | ||
menu = new JPopupMenu(); | ||
menuItems.forEach(menu::add); | ||
} | ||
} catch (Throwable e) { | ||
System.err.println("Unable to load favorites from: " + favoritesFileName); | ||
e.printStackTrace(System.err); | ||
menu = null; | ||
} | ||
} | ||
MENU = menu; | ||
} | ||
|
||
private static JMenuItem scan(File folder) { | ||
return scan(folder, folder.getName()); | ||
} | ||
|
||
private static JMenuItem scan(File file, String title) { | ||
if (file.isDirectory()) { | ||
File[] files = file.listFiles(); | ||
if (files != null && files.length > 0) { | ||
JMenu container = new JMenu(title); | ||
container.addMenuListener((MenuSelectedListener)e -> { | ||
container.removeAll(); | ||
for (File child : files) | ||
container.add(scan(child)); | ||
}); | ||
return container; | ||
} | ||
return new JMenuItem(title); | ||
} else { | ||
JMenuItem container = new JMenuItem(title); | ||
container.addActionListener((e) -> Main.getMain().openFile(file)); | ||
return container; | ||
} | ||
} | ||
} | ||
|
||
public static boolean hasFavorites() { | ||
return Lazy.MENU != null; | ||
} | ||
|
||
public static void setup(JComponent component) { | ||
if (hasFavorites()) | ||
component.setComponentPopupMenu(Lazy.MENU); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/org/riekr/jloga/ui/MenuSelectedListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.riekr.jloga.ui; | ||
|
||
import javax.swing.event.MenuEvent; | ||
import javax.swing.event.MenuListener; | ||
|
||
public interface MenuSelectedListener extends MenuListener { | ||
@Override | ||
default void menuDeselected(MenuEvent e) {} | ||
|
||
@Override | ||
default void menuCanceled(MenuEvent e) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters