Skip to content

Commit

Permalink
Improve desktop UI
Browse files Browse the repository at this point in the history
  • Loading branch information
charphi committed Sep 27, 2024
1 parent 8ae1534 commit 5b8d92e
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ private XmlDataSourceRef() {

public static final Xml.Parser<List<DataSourceRef>> PARSER = Jaxb.Parser.of(DataSourceBeans.class).andThen(DataSourceBeans::to);
public static final Xml.Formatter<List<DataSourceRef>> FORMATTER = Jaxb.Formatter.of(DataSourceBeans.class).compose(DataSourceBeans::from);
public static final Xml.Formatter<List<DataSourceRef>> FORMATTER2 = Jaxb.Formatter.of(DataSourceBeans.class).withFormatted(true).compose(DataSourceBeans::from);

@XmlRootElement
@lombok.Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private void onSelectionChange(PropertyChangeEvent evt) {
}

public void addToolWindow(String name, Icon icon, Component window) {
window.setPreferredSize(new Dimension(50, 300));
int index = windows.size();
bar.add(newButton(index, name, icon));
windows.add(window);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package internal.sdmxdl.desktop.util;

import javax.swing.*;

public final class XLabel extends JLabel {

public XLabel() {
setOpaque(true);
JList<?> resource = new JList<>();
// setBackground(resource.getSelectionForeground());
// setForeground(resource.getSelectionBackground());
setFont(resource.getFont().deriveFont(resource.getFont().getSize2D() * 2));
setHorizontalAlignment(SwingConstants.CENTER);
}
}
43 changes: 37 additions & 6 deletions sdmx-dl-desktop/src/main/java/sdmxdl/desktop/MainComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.NonNull;
import nbbrd.io.function.IOBiConsumer;
import org.kordamp.ikonli.Ikon;
import sdmxdl.CatalogRef;
import sdmxdl.desktop.panels.*;
import sdmxdl.ext.Persistence;
import sdmxdl.provider.ri.drivers.RiHttpUtils;
Expand All @@ -24,10 +25,12 @@
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collections;
import java.util.Objects;
import java.util.function.BiPredicate;
import java.util.function.Supplier;
Expand All @@ -38,6 +41,7 @@
import static internal.sdmxdl.desktop.util.Actions.onActionPerformed;
import static internal.sdmxdl.desktop.util.JTrees.toDefaultMutableTreeNode;
import static internal.sdmxdl.desktop.util.MouseListeners.onDoubleClick;
import static j2html.TagCreator.*;
import static java.awt.event.KeyEvent.VK_ENTER;
import static java.util.stream.Collectors.toList;
import static javax.swing.KeyStroke.getKeyStroke;
Expand Down Expand Up @@ -178,13 +182,13 @@ private JMenu newDatasetsMenu() {
item.setText("Open dump folder");

item = hideWhenDisabled(result.add(DataRefCommand.of(DataSourceRef.class).execution(MainComponent::copyPath).build().toAction(this)));
item.setText("Copy path");
item.setText("Copy Path/Reference...");

item = hideWhenDisabled(result.add(DataRefCommand.of(DataSetRef.class).execution(MainComponent::openDataSet).predicate((c, ref) -> ref.getKey().isSeries()).build().toAction(this)));
item.setText("<html><b>Open");

item = hideWhenDisabled(result.add(DataRefCommand.of(DataSetRef.class).execution(MainComponent::copyPath).build().toAction(this)));
item.setText("Copy path");
item.setText("Copy Path/Reference...");

item = hideWhenDisabled(result.add(DataRefCommand.of(Exception.class).execution(MainComponent::openException).build().toAction(this)));
item.setText("<html><b>Open");
Expand Down Expand Up @@ -526,13 +530,25 @@ private void openDumpFolder(DataSourceRef ref) throws IOException {
}

private void copyPath(DataSourceRef ref) throws IOException {
String path = ref.getSource() + "/" + ref.getCatalog() + "/" + ref.getFlow();
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(path), null);
String uri = "sdmxdl:/" + ref.getSource() + "/" + ref.getFlow() + (!ref.getCatalog().equals(CatalogRef.NO_CATALOG) ? "?c=" + ref.getCatalog() : "");
String command = "sdmx-dl fetch keys " + ref.getSource() + " " + ref.getFlow() + " all" + (!ref.getCatalog().equals(CatalogRef.NO_CATALOG) ? " -c" + ref.getCatalog() : "");
String xml = XmlDataSourceRef.FORMATTER2.formatToString(Collections.singletonList(ref));

JMenu menu = new JMenu();
menu.add(copyToClipboard(uri)).setText(html(text("SDMX-DL URI: "), code(uri)).render());
menu.add(copyToClipboard(command)).setText(html(text("CLI command: "), code(command)).render());
menu.add(copyToClipboard(xml)).setText(html(text("XML reference")).render());
showMenuAsPopup(menu);
}

private void copyPath(DataSetRef ref) throws IOException {
String path = ref.getDataSourceRef().getSource() + "/" + ref.getDataSourceRef().getCatalog() + "/" + ref.getDataSourceRef().getFlow() + "/" + ref.getKey();
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(path), null);
String uri = "sdmxdl:/" + ref.getDataSourceRef().getSource() + "/" + ref.getDataSourceRef().getFlow() + "/" + ref.getKey() + (!ref.getDataSourceRef().getCatalog().equals(CatalogRef.NO_CATALOG) ? "?c=" + ref.getDataSourceRef().getCatalog() : "");
String command = "sdmx-dl fetch data " + ref.getDataSourceRef().getSource() + " " + ref.getDataSourceRef().getFlow() + " " + ref.getKey() + (!ref.getDataSourceRef().getCatalog().equals(CatalogRef.NO_CATALOG) ? " -c" + ref.getDataSourceRef().getCatalog() : "");

JMenu menu = new JMenu();
menu.add(copyToClipboard(uri)).setText(html(text("SDMX-DL URI: "), code(uri)).render());
menu.add(copyToClipboard(command)).setText(html(text("CLI command: "), code(command)).render());
showMenuAsPopup(menu);
}

public void load() {
Expand Down Expand Up @@ -562,5 +578,20 @@ private static void reportException(Exception ex) {
JOptionPane.showMessageDialog(null, panel, ex.getClass().getSimpleName(), JOptionPane.ERROR_MESSAGE);
}

private static AbstractAction copyToClipboard(String text) {
return new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), null);
}
};
}

private void showMenuAsPopup(JMenu menu) {
Point mousePosition = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(mousePosition, this);
menu.getPopupMenu().show(this, mousePosition.x, mousePosition.y);
}

private static final Preferences PREFERENCES = Preferences.userNodeForPackage(MainComponent.class).node(MainComponent.class.getSimpleName());
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public Icon toHeaderIcon(DataSetRef value, Runnable onUpdate) {
public JDocument<DataSetRef> toView(MainComponent main, DataSetRef value) {
JDocument<DataSetRef> result = new JDocument<>();
result.setModel(value);
result.addComponent("...", new JLabel("loading"));
XLabel loading = new XLabel();
loading.setText("<html><center><b>Loading</b><br>" + toHeaderText(value, null));
result.addComponent("...", loading);

result.addToolBarItem(new ButtonBuilder()
.action(BrowseCommand.ofURL(DataSetRefRenderer::getWebsite)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private URL getWebsite(JDocument<WebSource> o) {

private static DomContent dom(WebSource value, Languages languages) {
return join(
labelTag(value.getId(), getColor(value.getConfidentiality())),
small(labelTag(value.getId(), getColor(value.getConfidentiality()))),
text(" " + value.getName(languages))
);
}
Expand Down

0 comments on commit 5b8d92e

Please sign in to comment.