org.bidib.wizard.dmx.client.view.DmxToolBarProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bidibwizard-dmx-client Show documentation
Show all versions of bidibwizard-dmx-client Show documentation
jBiDiB BiDiB Wizard DMX client POM
The newest version!
package org.bidib.wizard.dmx.client.view;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import org.bidib.wizard.api.locale.Resources;
import org.bidib.wizard.common.context.DefaultApplicationContext;
import org.bidib.wizard.dmx.client.controller.listener.DmxModelerControllerListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.vlsolutions.swing.docking.Dockable;
import com.vlsolutions.swing.docking.DockingDesktop;
import com.vlsolutions.swing.toolbars.ToolBarConstraints;
import com.vlsolutions.swing.toolbars.ToolBarPanel;
import com.vlsolutions.swing.toolbars.VLToolBar;
public class DmxToolBarProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(DmxToolBarProvider.class);
private static final String READ = "read";
private static final String WRITE = "write";
private JButton readCvButton;
private JButton writeCvButton;
private VLToolBar toolbarCvDefinition;
private final DockingDesktop desktop;
private final DmxModelerControllerListener dmxModelerControllerListener;
public DmxToolBarProvider(final DockingDesktop desktop,
final DmxModelerControllerListener dmxModelerControllerListener) {
this.desktop = desktop;
this.dmxModelerControllerListener = dmxModelerControllerListener;
}
public VLToolBar createToolBar() {
toolbarCvDefinition = new VLToolBar("readyDmxCvDefinition2");
addToolBarButtons(toolbarCvDefinition);
addToolBar(toolbarCvDefinition, new ToolBarConstraints(0, 2));
// initially invisible
toolbarCvDefinition.setVisible(false);
return toolbarCvDefinition;
}
public VLToolBar getToolBar() {
return toolbarCvDefinition;
}
private void addToolBarButtons(VLToolBar toolBar) {
// read CV button
readCvButton =
makeNavigationButton("loadfromnode", "/32x32", READ, Resources.getString(getClass(), "toolbar.readallcv"),
Resources.getString(getClass(), "toolbar.readallcv.alttext"));
readCvButton.setEnabled(false);
toolBar.add(readCvButton);
readCvButton.addActionListener(e -> {
LOGGER.info("Read CV values from node.");
dmxModelerControllerListener.readAllCvValuesFromNode();
});
// write CV button
writeCvButton =
makeNavigationButton("savetonode", "/32x32", WRITE, Resources.getString(getClass(), "toolbar.writeallcv"),
Resources.getString(getClass(), "toolbar.writeallcv.alttext"));
toolBar.add(writeCvButton);
writeCvButton.setEnabled(false);
writeCvButton.addActionListener(e -> {
LOGGER.info("Write changed CV values to node.");
// write the changed CV values to the node
// DmxDimmerConfigView.this.dmxDimmerConfigTableView.writeChangedCvValues();
this.dmxModelerControllerListener.writeChangedCvValuesToNode();
});
}
private JButton makeNavigationButton(
String imageName, String pathExt, String actionCommand, String toolTipText, String altText) {
// Look for the image.
String imgLocation = "/icons/" + imageName + ".png";
if (pathExt != null) {
imgLocation = "/icons" + pathExt + "/" + imageName + ".png";
}
URL imageURL = DmxDimmerConfigView.class.getResource(imgLocation);
// Create and initialize the button.
JButton button = new JButton();
button.setActionCommand(actionCommand);
button.setToolTipText(toolTipText);
if (imageURL != null) { // image found
button.setIcon(new ImageIcon(imageURL, altText));
}
else { // no image found
button.setText(altText);
LOGGER.warn("Resource not found: {}", imgLocation);
}
return button;
}
private void addToolBar(final VLToolBar toolBar, ToolBarConstraints constraints) {
// add the toolbar to the top toolbar panel
ToolBarPanel topToolBarPanel =
(ToolBarPanel) DefaultApplicationContext.getInstance().get(DefaultApplicationContext.KEY_TOPTOOLBARPANEL);
topToolBarPanel.add(toolBar, constraints);
}
private void removeToolBar(final VLToolBar toolBar) {
// remove the toolbar from the top toolbar panel
ToolBarPanel topToolBarPanel =
(ToolBarPanel) DefaultApplicationContext.getInstance().get(DefaultApplicationContext.KEY_TOPTOOLBARPANEL);
topToolBarPanel.remove(toolBar);
}
public void setVisible(final Dockable dockable, boolean visible) {
visibilityMap.compute(dockable.getDockKey().getKey(), (k, v) -> visible);
boolean isVisible = visibilityMap.containsValue(Boolean.TRUE);
LOGGER.info("Set toolbar visible: {}, dockable: {}, toolbar isVisible: {}", visible, dockable, isVisible);
toolbarCvDefinition.setVisible(isVisible);
}
public boolean setPendingChanges(final Dockable dockable, boolean pendingChanges) {
LOGGER.trace("Set pendingChanges: {}, dockable: {}", pendingChanges, dockable);
pendingChangesMap.compute(dockable.getDockKey().getKey(), (k, v) -> pendingChanges);
boolean hasPendingChanges = pendingChangesMap.containsValue(Boolean.TRUE);
if (this.writeCvButton != null) {
this.writeCvButton.setEnabled(hasPendingChanges);
}
return hasPendingChanges;
}
private final Map visibilityMap = new HashMap<>();
private final Map pendingChangesMap = new HashMap<>();
public void removeView(final Dockable dockable) {
try {
LOGGER.info("Remove view: {}", dockable.getDockKey().getKey());
visibilityMap.remove(dockable.getDockKey().getKey());
if (visibilityMap.isEmpty()) {
LOGGER.info("Remove the readyDmxCvDefinition2 toolbar.");
removeToolBar(toolbarCvDefinition);
}
}
catch (Exception ex) {
LOGGER.warn("Remove view from visibilityMap failed.", ex);
}
}
}