use of com.willwinder.universalgcodesender.types.Macro in project Universal-G-Code-Sender by winder.
the class MacroActionPanel method doLayout.
@Override
public void doLayout() {
Settings s = backend.getSettings();
// Lookup macros.
if (macrosDirty) {
Integer lastMacroIndex = s.getLastMacroIndex() + 1;
macros.clear();
for (int i = 0; i < lastMacroIndex; i++) {
Macro m = s.getMacro(i);
if (StringUtils.isNotEmpty(m.getGcode())) {
macros.add(s.getMacro(i));
}
}
}
// Cache the largest width amongst the buttons.
int maxWidth = 0;
int maxHeight = 0;
// Create buttons.
for (int i = 0; i < macros.size(); i++) {
final int index = i;
Macro macro = macros.get(i);
JButton button;
if (customGcodeButtons.size() <= i) {
button = new JButton(i + "");
button.setEnabled(false);
customGcodeButtons.add(button);
// Add action listener
button.addActionListener((ActionEvent evt) -> {
customGcodeButtonActionPerformed(index);
});
} else {
button = customGcodeButtons.get(i);
}
if (!StringUtils.isEmpty(macro.getName())) {
button.setText(macro.getName());
} else if (!StringUtils.isEmpty(macro.getDescription())) {
button.setText(macro.getDescription());
}
if (!StringUtils.isEmpty(macro.getDescription())) {
button.setToolTipText(macro.getDescription());
}
if (button.getPreferredSize().width > maxWidth)
maxWidth = button.getPreferredSize().width;
if (button.getPreferredSize().height > maxHeight)
maxHeight = button.getPreferredSize().height;
}
// If button count was reduced, clear out any extras.
if (customGcodeButtons.size() > macros.size()) {
this.macroPanel.removeAll();
this.macroPanel.repaint();
for (int i = customGcodeButtons.size(); i > macros.size(); i--) {
JButton b = customGcodeButtons.remove(i - 1);
}
}
// Calculate columns/rows which can fit in the space we have.
int columns = (getWidth() - (2 * INSET)) / (maxWidth + PADDING);
int rows = (getHeight() - (2 * INSET)) / (maxHeight + PADDING);
// At least one column.
columns = Math.max(columns, 1);
// Update number of rows if more are needed.
if (columns * rows < customGcodeButtons.size()) {
rows = customGcodeButtons.size() / columns;
if (customGcodeButtons.size() % columns != 0)
rows++;
}
// Layout for buttons.
StringBuilder columnConstraint = new StringBuilder();
for (int i = 0; i < columns; i++) {
if (i > 0) {
columnConstraint.append("unrelated");
}
columnConstraint.append("[fill, sg 1]");
}
MigLayout layout = new MigLayout("fillx, wrap " + columns + ", inset " + INSET, columnConstraint.toString());
macroPanel.setLayout(layout);
// Put buttons in grid.
int x = 0;
int y = 0;
for (JButton button : customGcodeButtons) {
macroPanel.add(button, "cell " + x + " " + y);
y++;
if (y == rows) {
x++;
y = 0;
}
}
super.doLayout();
}
use of com.willwinder.universalgcodesender.types.Macro in project Universal-G-Code-Sender by winder.
the class MacroActionPanel method customGcodeButtonActionPerformed.
private void customGcodeButtonActionPerformed(int macroIndex) {
Macro macro = backend.getSettings().getMacro(macroIndex);
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
MacroHelper.executeCustomGcode(macro.getGcode(), backend);
} catch (Exception ex) {
GUIHelpers.displayErrorDialog(ex.getMessage());
}
}
});
}
use of com.willwinder.universalgcodesender.types.Macro in project Universal-G-Code-Sender by winder.
the class MacroPanel method doLayout.
@Override
public void doLayout() {
Integer lastMacroIndex = backend.getSettings().getLastMacroIndex() + 1;
// Create components if needed
for (int i = customGcodeButtons.size(); i <= lastMacroIndex; i++) {
Macro macro = backend.getSettings().getMacro(i);
customGcodeButtons.add(createMacroButton(i));
macroGcodeFields.add(createMacroField(i, MACRO_FIELD.CODE, macro.getGcode()));
macroNameFields.add(createMacroField(i, MACRO_FIELD.NAME, macro.getName()));
macroDescriptionFields.add(createMacroField(i, MACRO_FIELD.DESCRIPTION, macro.getDescription()));
}
MigLayout layout = new MigLayout("fillx, wrap 4", "[fill, sg 1]r[fill, grow 10]r[fill, grow 45]r[fill, grow 45]");
setLayout(layout);
add(this.helpButton, "span 4");
add(buttonHeader, "sg 1");
add(nameHeader);
add(gcodeHeader);
add(descriptionHeader);
for (int i = 0; i < customGcodeButtons.size(); i++) {
add(customGcodeButtons.get(i), "sg 1");
add(macroNameFields.get(i));
add(macroGcodeFields.get(i));
add(macroDescriptionFields.get(i));
}
updateCustomGcodeControls(backend.isIdle());
super.doLayout();
}
use of com.willwinder.universalgcodesender.types.Macro in project Universal-G-Code-Sender by winder.
the class MacroPanel method customGcodeButtonActionPerformed.
private void customGcodeButtonActionPerformed(int i) {
Macro macro = backend.getSettings().getMacro(i);
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
MacroHelper.executeCustomGcode(macro.getGcode(), backend);
} catch (Exception ex) {
GUIHelpers.displayErrorDialog(ex.getMessage());
}
}
});
}
use of com.willwinder.universalgcodesender.types.Macro in project Universal-G-Code-Sender by winder.
the class MacroService method reInitActions.
public void reInitActions() {
String menuPath = "Menu/Machine/Macros";
String actionCategory = "Macro";
String localCategory = Localization.getString("platform.menu.macros");
String localized = String.format("Menu/%s/%s", Localization.getString("platform.menu.machine"), Localization.getString("platform.menu.macros"));
try {
FileObject root = FileUtil.getConfigRoot();
// Clear out the menu items.
FileUtil.createFolder(root, menuPath).delete();
FileUtil.createFolder(root, menuPath);
String actionPath = "/Actions/" + actionCategory;
FileUtil.createFolder(root, actionPath).delete();
// FileObject actionsObject = FileUtil.createFolder(root, actionPath);
ActionRegistrationService ars = Lookup.getDefault().lookup(ActionRegistrationService.class);
BackendAPI backend = CentralLookup.getDefault().lookup(BackendAPI.class);
Settings settings = backend.getSettings();
int numMacros = settings.getNumMacros();
for (int i = 0; i < numMacros; i++) {
Macro m = settings.getMacro(i);
ars.registerAction(MacroAction.class.getCanonicalName() + "." + m.getName(), m.getName(), actionCategory, localCategory, null, menuPath, localized, new MacroAction(settings, backend, i));
}
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations