Skip to content

Commit

Permalink
Favorites
Browse files Browse the repository at this point in the history
  • Loading branch information
Riekr committed Oct 21, 2022
1 parent 1b80fac commit 4abd5d4
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## In progress:

## v0.2.6
- General:
- Preliminary support for favorites folders\
specify `-Djloga.favorites=favorites.properties` to enable the menu, check sample [file](favorites-sample.properties)
- Build:
- Get version from _CHANGELOG.md_
- Upgrade to gradle 7.5.1 and latest wrapper
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Head directly to [this page](https://github.com/Riekr/jloga/releases/latest) for
- Save and reload search parameters
- Capable of running [external commands](#external-commands) as new analyzers
- Convert and show data into [finos perspective](#finos-perspective)
- Favorites folders support

## File and memory limits
There is no limit in file size, maximum number of lines is 2<sup>63</sup>-1.\
Expand Down Expand Up @@ -128,6 +129,11 @@ java -cp build/libs/jloga-all.jar org.riekr.jloga.httpd.FinosPerspectiveServer i
```
Once you close the browser, the app will automatically close.

## Favorites folders
The app supports a preliminary menu for accessing favorites folders, there is no way other than modifying an external file to list favorites. Create a _properties_ file like [favorites-sample.properties](favorites-sample.properties) and use the system property `-Djloga.favorites=favorites.properties` to specify the file you just created.

If you launch jloga from a workspace configuration you will find this feature useful :)

## Future and licensing
This software is free as in speech, do whatever you want with it.

Expand Down
2 changes: 2 additions & 0 deletions favorites-sample.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GRADLE=gradle
EXT=ext-search-samples
83 changes: 83 additions & 0 deletions src/main/java/org/riekr/jloga/prefs/Favorites.java
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);
}

}
11 changes: 10 additions & 1 deletion src/main/java/org/riekr/jloga/ui/MainPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.riekr.jloga.io.TextFileSource;
import org.riekr.jloga.io.TextSource;
import org.riekr.jloga.misc.FileDropListener;
import org.riekr.jloga.prefs.Favorites;
import org.riekr.jloga.prefs.KeyBindings;
import org.riekr.jloga.prefs.PrefPanel;
import org.riekr.jloga.prefs.Preferences;
Expand Down Expand Up @@ -63,8 +64,16 @@ public MainPanel() {
toolBar.addSeparator();
toolBar.add(newBorderlessButton("\u292D Mix", this::openMixDialog, "Pick'n'mix open log files"));
toolBar.addSeparator();
toolBar.add(_refreshBtn = newBorderlessButton("\uD83D\uDDD8 Refresh", this::refreshCurrentTab, "Refresh current tab"));
_refreshBtn = newBorderlessButton("\uD83D\uDDD8 Refresh", this::refreshCurrentTab, "Refresh current tab");
_refreshBtn.setEnabled(false);
toolBar.add(_refreshBtn);
if (Favorites.hasFavorites()) {
toolBar.addSeparator();
JButton favoritesBtn = newBorderlessButton("\u2605 Favorites", null, "Open favorites popup");
Favorites.setup(favoritesBtn);
favoritesBtn.addActionListener((e) -> UIUtils.showComponentMenu(favoritesBtn));
toolBar.add(favoritesBtn);
}
Component glue = Box.createGlue();
FrameDragListener.associate(this, glue);
toolBar.add(glue);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/riekr/jloga/ui/MenuSelectedListener.java
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) {}
}
7 changes: 6 additions & 1 deletion src/main/java/org/riekr/jloga/utils/UIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public static <T extends JComponent> T makeBorderless(T comp) {

public static JButton newButton(String text, Runnable action) {
JButton btn = new JButton(text);
btn.addActionListener((e) -> action.run());
if (action != null)
btn.addActionListener((e) -> action.run());
return btn;
}

Expand Down Expand Up @@ -352,4 +353,8 @@ public static void relativeLocation(Component currComponent, Component parent, P
currComponent = currComponent.getParent();
}
}

public static void showComponentMenu(JComponent component) {
component.getComponentPopupMenu().show(component, 0, component.getHeight());
}
}

0 comments on commit 4abd5d4

Please sign in to comment.