Skip to content

Commit

Permalink
New abstract class SerialHardwareBean, in the vein of #281.
Browse files Browse the repository at this point in the history
  • Loading branch information
bengtmartensson committed Aug 11, 2021
1 parent f08cc35 commit 514642d
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 202 deletions.
130 changes: 15 additions & 115 deletions src/main/java/org/harctoolbox/guicomponents/CommandFusionBean.java
Expand Up @@ -19,12 +19,7 @@

import java.awt.Cursor;
import java.io.IOException;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import org.harctoolbox.harchardware.HarcHardwareException;
import org.harctoolbox.harchardware.IHarcHardware;
import org.harctoolbox.harchardware.comm.LocalSerialPort;
import org.harctoolbox.harchardware.ir.CommandFusion;
import org.harctoolbox.ircore.InvalidArgumentException;
import org.harctoolbox.ircore.IrSignal;
Expand All @@ -33,14 +28,10 @@
/**
*
*/
public final class CommandFusionBean extends HardwareBean {
public final class CommandFusionBean extends SerialHardwareBean {

private String portName;
private String version;

/**
* Creates new form SerialPortSimpleBean
*/
public CommandFusionBean() {
this(null);
}
Expand All @@ -52,62 +43,12 @@ public CommandFusionBean(GuiUtils guiUtils) {
public CommandFusionBean(GuiUtils guiUtils, boolean verbose, String initialPort) {
super(guiUtils, verbose, 0);
initComponents();
DefaultComboBoxModel<String> model;
try {
List<String> portList = LocalSerialPort.getSerialPortNames(true);
model = new DefaultComboBoxModel<>(portList.toArray(new String[portList.size()]));
} catch (IOException | LinkageError ex) {
model = new DefaultComboBoxModel<>(new String[]{ initialPort != null ? initialPort : NOT_INITIALIZED });
}

portComboBox.setModel(model);
boolean hit = false;
if (initialPort != null) {
for (int i = 0; i < model.getSize(); i++) {
if (initialPort.equalsIgnoreCase(model.getElementAt(i))) {
hit = true;
portComboBox.setSelectedIndex(i);
break;
}
}
}
String actualPort = initialPort;
if (!hit) {
// Got a problem here, want to select a port that is not there, at least not now
if (model.getSize() > 0) {
portComboBox.setSelectedIndex(0);
actualPort = portComboBox.getItemAt(0);
}
}
setPortName(actualPort);
}

/**
* @return the port
*/
public String getPortName() {
return portName;
}

/**
* @param portName the port to set
*/
public void setPortName(String portName) {
if (portName == null || portName.isEmpty())
return;

openToggleButton.setEnabled(! isOpen());
String oldPort = this.portName;
this.portName = portName;
// this propery changer should set up the hardware and call setHardware()
propertyChangeSupport.firePropertyChange(PROP_PORTNAME, oldPort, portName);
setupPortComboBox(portComboBox, initialPort);
}

private void setHardware(IHarcHardware hardware) {
this.hardware = hardware;
openToggleButton.setEnabled(hardware != null);
openToggleButton.setSelected(hardware.isValid());
setVersion();
@Override
protected void setupHardware() throws IOException {
hardware = new CommandFusion(portName, verbose);
}

/**
Expand All @@ -126,70 +67,24 @@ private void setVersion(String version) {
//propertyChangeSupport.firePropertyChange(PROP_VERSION, oldVersion, version);
}

private void setVersion() {
@Override
protected void setVersion() {
try {
setVersion(isOpen() ? hardware.getVersion() : NOT_CONNECTED);
} catch (IOException ex) {
}
}

private void setup() throws IOException {
setup(portName);
}

private void setup(String desiredPort) throws IOException {
ComboBoxModel<String> model = portComboBox.getModel();
if (model == null || model.getSize() == 0 || ((model.getSize() == 1) && ((String)portComboBox.getSelectedItem()).equals(NOT_INITIALIZED)))
setupPortComboBox(true);

portComboBox.setSelectedItem(desiredPort != null ? desiredPort : portName);
}

private void setupPortComboBox(boolean useCached) throws IOException {
if (hardware != null)
hardware.close();

List<String> portNames = LocalSerialPort.getSerialPortNames(useCached);
portNames.add(0, "");
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(portNames.toArray(new String[portNames.size()]));
portComboBox.setModel(model);
}

private void enableStuff(boolean isOpen) {
portComboBox.setEnabled(! isOpen);
}

private void enableStuff() {
enableStuff(isOpen());
portComboBox.setModel(createModel(useCached));
}

@Override
public void open() throws HarcHardwareException, IOException {
boolean oldIsOpen = isOpen();
try {
if (!oldIsOpen) {
hardware = new CommandFusion(portName, verbose);
hardware.open();
}
} finally {
enableStuff();
setVersion();
propertyChangeSupport.firePropertyChange(PROP_ISOPEN, oldIsOpen, hardware.isValid());
}
}

@Override
public void close() throws IOException {
boolean oldIsOpen = isOpen();
try {
if (oldIsOpen)
hardware.close();
} finally {
enableStuff(false);
setVersion();
propertyChangeSupport.firePropertyChange(PROP_ISOPEN, oldIsOpen, isOpen());
hardware = null;
}
protected void enableStuff(boolean isOpen) {
portComboBox.setEnabled(! isOpen);
}

@Override
Expand Down Expand Up @@ -217,6 +112,11 @@ public String getName() {
return CommandFusion.COMMAND_FUSION;
}

@Override
protected void enableOpenToggleButton(boolean enabled) {
openToggleButton.setEnabled(enabled);
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
Expand Down
106 changes: 20 additions & 86 deletions src/main/java/org/harctoolbox/guicomponents/IrWidgetBean.java
Expand Up @@ -19,20 +19,16 @@

import java.awt.Cursor;
import java.io.IOException;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import org.harctoolbox.harchardware.HarcHardwareException;
import org.harctoolbox.harchardware.comm.LocalSerialPort;
import org.harctoolbox.harchardware.comm.NonExistingPortException;
import static org.harctoolbox.harchardware.ir.IIrReader.DEFAULT_BEGIN_TIMEOUT;
import org.harctoolbox.harchardware.ir.IrWidget;
import org.harctoolbox.ircore.ModulatedIrSequence;

/**
*
*/
public final class IrWidgetBean extends HardwareBean {

private String portName;
public final class IrWidgetBean extends SerialHardwareBean {

public IrWidgetBean() {
this(null);
Expand All @@ -44,111 +40,49 @@ public IrWidgetBean(GuiUtils guiUtils) {

public IrWidgetBean(GuiUtils guiUtils, boolean verbose, int timeout, String initialPort) {
super(guiUtils, verbose, timeout);
initComponents();
DefaultComboBoxModel<String> model;
try {
List<String> portList = LocalSerialPort.getSerialPortNames(true);
model = new DefaultComboBoxModel<>(portList.toArray(new String[portList.size()]));
} catch (IOException | LinkageError ex) {
model = new DefaultComboBoxModel<>(new String[]{ initialPort != null ? initialPort : NOT_INITIALIZED });
}

portComboBox.setModel(model);
boolean hit = false;
if (initialPort != null) {
for (int i = 0; i < model.getSize(); i++) {
if (initialPort.equalsIgnoreCase(model.getElementAt(i))) {
hit = true;
portComboBox.setSelectedIndex(i);
break;
}
}
}
String actualPort = initialPort;
if (!hit) {
// Got a problem here, want to select a port that is not there, at least not now
if (model.getSize() > 0) {
portComboBox.setSelectedIndex(0);
actualPort = portComboBox.getItemAt(0);
}
}
setPortName(actualPort);
initComponents();
setupPortComboBox(portComboBox, initialPort);
}

/**
* @return the port
*/
public String getPortName() {
return portName;
@Override
protected void setupHardware() throws IOException, NonExistingPortException {
hardware = new IrWidget(portName, verbose, timeout);
}

/**
* @param portName the port to set
*/
public void setPortName(String portName) {
if (portName == null || portName.isEmpty())
return;

openToggleButton.setEnabled(! isOpen());
String oldPort = this.portName;
this.portName = portName;
// this propery changer should set up the hardware and call setHardware()
propertyChangeSupport.firePropertyChange(PROP_PORTNAME, oldPort, portName);
@Override
protected void setVersion() {
}

private void setupPortComboBox(boolean useCached) throws IOException {
if (hardware != null)
hardware.close();

List<String> portNames = LocalSerialPort.getSerialPortNames(useCached);
portNames.add(0, "");
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(portNames.toArray(new String[portNames.size()]));
portComboBox.setModel(model);
portComboBox.setModel(createModel(useCached));
}

private void enableStuff(boolean isOpen) {
@Override
protected void enableStuff(boolean isOpen) {
portComboBox.setEnabled(!isOpen);
}

private void enableStuff() {
enableStuff(isOpen());
}

@Override
public void open() throws HarcHardwareException, IOException {
boolean oldIsOpen = isOpen();
try {
if (!oldIsOpen) {
hardware = new IrWidget(portName, verbose, timeout);
hardware.open();
}
} finally {
enableStuff();
propertyChangeSupport.firePropertyChange(PROP_ISOPEN, oldIsOpen, hardware.isValid());
}
public boolean canCapture() {
return true;
}

@Override
public void close() throws IOException {
boolean oldIsOpen = isOpen();
try {
if (oldIsOpen)
hardware.close();
} finally {
enableStuff(false);
propertyChangeSupport.firePropertyChange(PROP_ISOPEN, oldIsOpen, isOpen());
hardware = null;
}
public ModulatedIrSequence capture() throws IOException {
return ((IrWidget) hardware).capture();
}

@Override
public boolean canCapture() {
return true;
public String getName() {
return IrWidget.IRWIDGET;
}

@Override
public ModulatedIrSequence capture() throws IOException {
return ((IrWidget) hardware).capture();
protected void enableOpenToggleButton(boolean enabled) {
openToggleButton.setEnabled(enabled);
}

/**
Expand Down

0 comments on commit 514642d

Please sign in to comment.