Skip to content

Commit

Permalink
Add component for editing boolean values, including "Default" button …
Browse files Browse the repository at this point in the history
…and background coloring.
  • Loading branch information
breaker27 committed Jun 29, 2014
1 parent af581a3 commit 9dd5535
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 16 deletions.
86 changes: 86 additions & 0 deletions tools/shc_e2p_editor/src/main/java/shcee/editors/BoolArea.java
@@ -0,0 +1,86 @@
/*
* This file is part of smarthomatic, http://www.smarthomatic.org.
* Copyright (c) 2013 Uwe Freese
*
* smarthomatic is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* smarthomatic is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with smarthomatic. If not, see <http://www.gnu.org/licenses/>.
*/

package shcee.editors;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JCheckBox;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.PlainDocument;

import shcee.Util;

/**
* UIntTextArea is a JTextArea with a limit on characters that can be entered
* and automatic input checking.
* If the input is not correct, the background is red.
* The validity can be read with isValid().
* @author uwe
*/
public class BoolArea extends JCheckBox
{
private static final long serialVersionUID = 4158223774920676647L;

private boolean valid;
private Boolean defaultVal;

public BoolArea(Boolean defaultVal)
{
super();

this.defaultVal = defaultVal;
this.setText("true / active / on");

this.addItemListener(new ItemListener() {

@Override
public void itemStateChanged(ItemEvent arg0) {
checkInput();
}

});

checkInput();
}

protected void checkInput()
{
valid = true;

if ((null != defaultVal) && (defaultVal != this.isSelected()))
{
setBackground(Color.YELLOW);
}
else
{
setBackground(Color.WHITE);
}
}

public boolean dataIsValid()
{
return valid;
}
}
71 changes: 55 additions & 16 deletions tools/shc_e2p_editor/src/main/java/shcee/editors/BoolEditor.java
Expand Up @@ -18,8 +18,17 @@

package shcee.editors;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.text.DefaultCaret;

import org.w3c.dom.Node;
Expand All @@ -30,32 +39,64 @@ public class BoolEditor extends AbstractEditor
{
private static final long serialVersionUID = -7593115068369714873L;

private long minVal;
private long maxVal;
private int bits;
private UIntTextArea input;
private Boolean defaultVal;
private BoolArea input;

public BoolEditor(Node root, Color baseColor, int arrayIndex)
{
super(root, baseColor, arrayIndex);

minVal = 0;
maxVal = 1;
bits = 8;

// add label about format
format = "Boolean of 8 bits";

if (null != defaultVal)
{
format += " (default: " + defaultVal + ")";
}

addLabel(format);

// add input
input = new UIntTextArea(minVal, maxVal, null);
add(input);
JPanel inputPanel = new JPanel();
//inputPanel.setLayout(new BoxLayout(inputPanel, javax.swing.BoxLayout.X_AXIS));
inputPanel.setLayout(new BorderLayout());
inputPanel.setBackground(this.getBackground());

input = new BoolArea(defaultVal);
inputPanel.add(input, BorderLayout.WEST);

if (null != defaultVal)
{
JButton buttonDefault = new JButton("Default");

buttonDefault.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
onButtonDefault();
}
});

Component glue = Box.createRigidArea(new Dimension(6, 6));
glue.setPreferredSize(new Dimension(1000, 6));
inputPanel.add(glue, BorderLayout.CENTER); // space between components
inputPanel.add(buttonDefault, BorderLayout.EAST);
}

add(inputPanel);

// add description
description = Util.getChildNodeValue(root, "Description");
addLabel(description);
}

private void onButtonDefault()
{
input.setSelected(defaultVal);
}

@Override
public boolean dataIsValid()
{
Expand All @@ -65,28 +106,26 @@ public boolean dataIsValid()
@Override
public void setDefinitionParameter(Node n)
{
// no parameters
String name = n.getNodeName();

if (name.equals("DefaultVal"))
defaultVal = n.getFirstChild().getNodeValue().equals("true");
}

@Override
public int readFromEepromArray(byte[] eeprom, int offsetBit)
{
int data = Util.getIntFromByteArray(eeprom, offsetBit, bits);

// temporarily avoid scrolling to the element when content changes,
// then set text
DefaultCaret caret = (DefaultCaret)input.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
input.setText("" + data);
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

input.setSelected(data != 0);

return 8;
}

@Override
public int writeToEepromArray(byte[] eeprom, int offsetBit)
{
int value = Integer.parseInt(input.getText());
int value = input.isSelected() ? 1 : 0;
Util.setIntInByteArray(value, eeprom, offsetBit, bits);
return 8;
}
Expand Down

0 comments on commit 9dd5535

Please sign in to comment.