Commit 3a665d12 authored by Gwenael Cattez's avatar Gwenael Cattez

Add common module common-valuedevice that provides a Swing GUI for devices...

Add common module common-valuedevice that provides a Swing GUI for devices that have a valuable state
parent 669ac92b
......@@ -40,6 +40,7 @@
<module>common-homeautomation-zibase-mock</module>
<module>common-homeautomation-switch</module>
<module>common-homeautomation-light</module>
<module>common-homeautomation-videoscreen-mock</module>
</modules>
<build>
......
<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright (C) 2008-2011 INRIA, University of Lille 1 -->
<!-- -->
<!-- This library is free software; you can redistribute it and/or -->
<!-- modify it under the terms of the GNU Lesser General Public -->
<!-- License as published by the Free Software Foundation; either -->
<!-- version 2 of the License, or (at your option) any later version. -->
<!-- -->
<!-- This library 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 -->
<!-- Lesser General Public License for more details. -->
<!-- -->
<!-- You should have received a copy of the GNU Lesser General Public -->
<!-- License along with this library; if not, write to the Free Software -->
<!-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -->
<!-- -->
<!-- Contact: frascati@ow2.org -->
<!-- -->
<!-- Author: Gwenael CATTEZ -->
<!-- Contributors : -->
<!-- -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>common</artifactId>
<groupId>org.ow2.frascati.demo</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>common-valuedevice</artifactId>
<name>Common valued device module</name>
</project>
\ No newline at end of file
/**
* OW2 FraSCAti Assembly Factory
* Copyright (C) 2008-2012 Inria, University of Lille
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact: frascati@ow2.org
*
* Author: Gwénaël Cattez
*
* Contributor(s):
*
*/
package org.ow2.frascati.demo.common.valuedevice;
import javax.swing.ImageIcon;
/**
* Interface for device that have a valued state like a video screen that can be open, close and in an intermediate state
*/
public interface IValueDevice
{
/**
* Set the Current Value of the device
*
* @return Current value of the device
*/
public int setCurrentValue(int value);
/**
* Get the icon to display depending on the value of the device
*
* @param value : the value of the device
* @return The Swing ImgaIcon
*/
public ImageIcon getImageIcon(int value);
}
/**
* OW2 FraSCAti Assembly Factory
* Copyright (C) 2008-2012 Inria, University of Lille
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact: frascati@ow2.org
*
* Author: Gwénaël Cattez
*
* Contributor(s):
*
*/
package org.ow2.frascati.demo.common.valuedevice;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JSpinner.NumberEditor;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.WindowConstants;
import org.osoa.sca.annotations.Property;
/**
* Implementation of the IValueDevice interface, this class display a Swing GUI that represent the device, configuration is done via SCA properties
*/
/**
*
*/
public abstract class SwValueDevice extends JFrame implements IValueDevice
{
private static final long serialVersionUID = 8711912671655826452L;
/**
* The title of the JFrame
*
* @Property
*/
protected String frameTitle;
/**
* The current value of the device
*
* @Property
*/
protected int currentValue=0;
/**
* The minimum value for the device
*
* @Property
*/
protected int minValue=0;
/**
* The maximum value for the device
*
* @Property
*/
protected int maxValue=100;
/**
* The step size between two values in the JSpinner
*
* @Property
*/
protected int stepSize=1;
/**
* The label that holds the current icon of the device
*/
protected JLabel imageValueLabel;
/**
* The spinner that allow user to change the value of the device
*/
protected JSpinner setValueSpinner;
/**
* The action button to set the value of the device
*/
protected JButton setValueButton;
/**
* The icon display if no user icon is define for a value
*/
protected ImageIcon defaultIcon;
/**
* Constructor, set title of the Frame and initalise the Swing components
*/
protected SwValueDevice()
{
super();
updateTitle();
initComponents();
}
/* (non-Javadoc)
* @see org.ow2.frascati.demo.common.valuedevice.IValueDevice#dim(int)
*/
public int setCurrentValue(int value)
{
setValue(value);
setCurrentValueAction(value);
return this.currentValue;
}
/**
* Method call after graphical changes on the JFrame, allow inherited class to do specific actions
*
* @param value : The current value of the device
*/
public abstract void setCurrentValueAction(int value);
/**
* Create the different swing component, add listener to the button
*/
private void initComponents()
{
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Dimension preferredSize=new Dimension(300,300);
setPreferredSize(preferredSize);
setResizable(false);
defaultIcon=new ImageIcon(getClass().getClassLoader().getResource("images/default.png"));
imageValueLabel = new JLabel();
imageValueLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
ImageIcon icon=getImageIcon(currentValue);
imageValueLabel.setIcon(icon);
JPanel spinnerContainer=new JPanel();
preferredSize=new Dimension(30,20);
spinnerContainer.setPreferredSize(preferredSize);
spinnerContainer.setAlignmentX(Component.CENTER_ALIGNMENT);
SpinnerNumberModel model=new SpinnerNumberModel(currentValue,minValue,maxValue,stepSize);
setValueSpinner=new JSpinner(model);
NumberEditor numberEditor=(NumberEditor)setValueSpinner.getEditor();
JFormattedTextField formattedTextField=numberEditor.getTextField();
formattedTextField.setEditable(false);
spinnerContainer.add(setValueSpinner);
setValueButton = new JButton();
setValueButton.setAlignmentX(Component.CENTER_ALIGNMENT);
setValueButton.setText("Set value");
setValueButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
setValueActionPerformed();
}
});
BoxLayout boxLayout=new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS);
getContentPane().setLayout(boxLayout);
getContentPane().add(imageValueLabel);
getContentPane().add(spinnerContainer);
getContentPane().add(setValueButton);
pack();
setVisible(true);
}
/**
* Listener on the "set value" button
*/
private void setValueActionPerformed()
{
Integer value=(Integer) setValueSpinner.getValue();
setCurrentValue(value);
}
/**Utils for getter and setter**/
private void updateSpinnerModel()
{
SpinnerModel model=new SpinnerNumberModel(currentValue,minValue,maxValue,stepSize);
setValueSpinner.setModel(model);
}
private void updateTitle()
{
setTitle(this.frameTitle+", value: "+this.currentValue);
}
/**Properties getter and setter**/
public String getFrameTitle()
{
return frameTitle;
}
@Property
public void setFrameTitle(String title)
{
this.frameTitle = title;
updateTitle();
}
public int getValue()
{
return currentValue;
}
/**
* Set the value of the device, checks if the value is in the device value range (if not correct it with min and max values) then changes icon and title
*
* @param value
*/
@Property
public void setValue(int value)
{
if(value > maxValue)
{
this.currentValue=maxValue;
}
else if(value < minValue)
{
this.currentValue=minValue;
}
else
{
this.currentValue = value;
}
setValueSpinner.setValue(value);
updateTitle();
ImageIcon icon=this.getImageIcon(value);
if(icon!=null)
{
this.imageValueLabel.setIcon(icon);
}
else
{
this.imageValueLabel.setIcon(defaultIcon);
}
}
public int getMinValue()
{
return minValue;
}
@Property
public void setMinValue(int minValue)
{
this.minValue = minValue;
updateSpinnerModel();
}
public int getMaxValue()
{
return maxValue;
}
@Property
public void setMaxValue(int maxValue)
{
this.maxValue = maxValue;
updateSpinnerModel();
}
public int getStepSize()
{
return stepSize;
}
@Property
public void setStepSize(int stepSize)
{
this.stepSize = stepSize;
updateSpinnerModel();
}
}
......@@ -38,6 +38,7 @@
<module>onoffdevice</module>
<module>twitter</module>
<module>common-homeautomation</module>
<module>common-valuedevice</module>
</modules>
</project>
\ No newline at end of file
......@@ -47,7 +47,6 @@
<org.ow2.frascati.bootstrap>org.ow2.frascati.bootstrap.FraSCAti</org.ow2.frascati.bootstrap>
<org.ow2.frascati.composite>org.ow2.frascati.FraSCAti</org.ow2.frascati.composite>
<org.ow2.frascati.binding.uri.base>http://localhost:8765</org.ow2.frascati.binding.uri.base>
<!-- <org.ow2.frascati.binding.uri.base>http://localhost:18000</org.ow2.frascati.binding.uri.base> -->
<frascati.version>1.5-SNAPSHOT</frascati.version>
<cxf.version>2.4.2</cxf.version>
......@@ -242,7 +241,13 @@
<dependency>
<groupId>org.ow2.frascati.demo</groupId>
<artifactId>common-onoffdevice</artifactId>
<version>${itemis.interface.version}</version>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.ow2.frascati.demo</groupId>
<artifactId>common-valuedevice</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment