Skip to content

Commit

Permalink
Add macro description as a tool tip (#2331)
Browse files Browse the repository at this point in the history
* Added macro description as a tooltip
* Fixed so that the macro editing panel no longer overflows when adding long macros
* Now only use macro name in menus and toolbox
  • Loading branch information
breiler authored Oct 7, 2023
1 parent 3b11911 commit cef6e79
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ This file is part of Universal Gcode Sender (UGS).

public class MacroSettingsPanel extends JPanel implements UGSEventListener {
private static final Logger logger = Logger.getLogger(MacroSettingsPanel.class.getName());
private static final String MIN_WIDTH = "width 100:100:, growx";

private final BackendAPI backend;
private final transient BackendAPI backend;
private final List<JButton> moveUpButtons = new ArrayList<>();
private final List<JButton> moveDownButtons = new ArrayList<>();
private final List<JButton> tryButton = new ArrayList<>();
Expand Down Expand Up @@ -97,7 +98,7 @@ public class MacroSettingsPanel extends JPanel implements UGSEventListener {
private final JLabel deleteHeader = new JLabel("");

private final JPanel buttonPanel = new JPanel(new MigLayout("fill, ins 0"));
private List<Macro> macros;
private final List<Macro> macros;

public void save() {
backend.getSettings().setMacros(macros);
Expand All @@ -111,11 +112,8 @@ private enum MacroFieldEnum {
}

public MacroSettingsPanel(BackendAPI backend) {
super(new MigLayout("fillx, wrap 7", "[fill, grow 10, sg 1]r[fill, grow 10]r[fill, grow 10]r[fill, grow 45]r[fill, grow 45]r[fill]r[fill]"));
super(new MigLayout("fillx, wrap 7", "[fill, grow 10, sg 1]r[fill, grow 10]r[fill, grow 10]r[fill, grow 60]r[fill, grow 30]r[fill]r[fill]"));

if (backend == null) {
throw new RuntimeException();
}
this.backend = backend;
this.backend.addUGSEventListener(this);

Expand Down Expand Up @@ -155,9 +153,9 @@ public void doLayout() {
for (int i = 0; i < tryButton.size(); i++) {
add(moveUpButtons.get(i), "sg 1");
add(moveDownButtons.get(i));
add(macroNameFields.get(i));
add(macroGcodeFields.get(i));
add(macroDescriptionFields.get(i));
add(macroNameFields.get(i), MIN_WIDTH);
add(macroGcodeFields.get(i), MIN_WIDTH);
add(macroDescriptionFields.get(i), MIN_WIDTH);
add(tryButton.get(i));
add(deleteButtons.get(i));
}
Expand Down Expand Up @@ -245,6 +243,7 @@ private void update(Macro macro, MacroFieldEnum field, String text) {
case DESCRIPTION:
macro.setDescription(text);
break;
default:
}

// Add it if it doesn't exists
Expand Down Expand Up @@ -320,12 +319,12 @@ private void addListeners() {
JFileChooser fileChooser = new JFileChooser(backend.getSettings().getLastOpenedFilename());
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
try {
Collection<Macro> macros = backend.getSettings().getMacros();
Collection<Macro> macroList = backend.getSettings().getMacros();

try (OutputStream fileOutputStream = new FileOutputStream(fileChooser.getSelectedFile())) {
Writer writer = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
writer.write(gson.toJson(macros, Collection.class));
writer.write(gson.toJson(macroList, Collection.class));
writer.flush();
}
} catch (Exception ex) {
Expand All @@ -343,8 +342,8 @@ private void addListeners() {

try (InputStream reader = new FileInputStream(importFile)) {
Type type = new TypeToken<ArrayList<Macro>>(){}.getType();
List<Macro> macros = new Gson().fromJson(new InputStreamReader(reader, StandardCharsets.UTF_8), type);
this.macros.addAll(macros);
List<Macro> macroList = new Gson().fromJson(new InputStreamReader(reader, StandardCharsets.UTF_8), type);
this.macros.addAll(macroList);

// Update the window.
SwingUtilities.invokeLater(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.openide.util.Exceptions;

import javax.swing.AbstractAction;
import javax.swing.Action;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.io.Serializable;
Expand Down Expand Up @@ -56,20 +57,26 @@ private void updateMacroName() {
String name = macro.getName();
putValue(NAME, name);
putValue("menuText", name);

if (StringUtils.isNotEmpty(macro.getDescription())) {
putValue(Action.SHORT_DESCRIPTION, macro.getDescription());
}
}

@Override
public void actionPerformed(ActionEvent e) {
if (macro != null && macro.getGcode() != null) {
EventQueue.invokeLater(() -> {
try {
MacroHelper.executeCustomGcode(macro.getGcode(), getBackend());
} catch (Exception ex) {
GUIHelpers.displayErrorDialog(ex.getMessage());
Exceptions.printStackTrace(ex);
}
});
return;
}

EventQueue.invokeLater(() -> {
try {
MacroHelper.executeCustomGcode(macro.getGcode(), getBackend());
} catch (Exception ex) {
GUIHelpers.displayErrorDialog(ex.getMessage());
Exceptions.printStackTrace(ex);
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2016-2019 Will Winder
Copyright 2016-2023 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand Down Expand Up @@ -37,10 +37,9 @@ This file is part of Universal Gcode Sender (UGS).
import java.util.logging.Logger;

/**
*
* @author wwinder
*/
@ServiceProvider(service=MacroService.class)
@ServiceProvider(service = MacroService.class)
public final class MacroService {
private static final Logger logger = Logger.getLogger(MacroService.class.getName());

Expand All @@ -65,28 +64,26 @@ public void reInitActions() {
String actionPath = "/Actions/" + actionCategory;
FileUtil.createFolder(root, actionPath).delete();

ActionRegistrationService ars = Lookup.getDefault().lookup(ActionRegistrationService.class);
ActionRegistrationService ars = Lookup.getDefault().lookup(ActionRegistrationService.class);
BackendAPI backend = CentralLookup.getDefault().lookup(BackendAPI.class);
Settings settings = backend.getSettings();

List<Macro> macros = settings.getMacros();
macros.forEach(macro -> {
int index = macros.indexOf(macro);
try {
String text;
if (Strings.isNullOrEmpty(macro.getNameAndDescription())){
int index = macros.indexOf(macro);
String text = macro.getName();
if (Strings.isNullOrEmpty(text)) {
text = Integer.toString(index + 1);
} else {
text = macro.getNameAndDescription();
}

ars.registerAction(MacroAction.class.getCanonicalName() + "." + macro.getUuid(), text, actionCategory, null, menuPath, index, localized, new MacroAction(macro));
} catch (IOException e) {
logger.log(Level.WARNING, "Couldn't register macro action: \"" + macro.getName() + "\"", e);
logger.log(Level.WARNING, String.format("Couldn't register macro action: \"%s\"", macro.getName()), e);
}
});
} catch (Exception e) {
logger.log(Level.WARNING, "Couldn't register macro actions", e);
logger.log(Level.WARNING, "Could not register macro actions", e);
}
}
}

0 comments on commit cef6e79

Please sign in to comment.