//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
*
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.framework.dataeditor;
import com.nexirius.framework.datamodel.DoubleModel;
import com.nexirius.framework.datamodel.IntModel;
import com.nexirius.framework.datamodel.LimitIntModel;
import com.nexirius.framework.datamodel.SimpleModel;
import com.nexirius.framework.dataviewer.DataViewer;
import javax.swing.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
/**
* This viewer is designed to show LimitIntModel or DoubleModel or IntModel data.
* It is implemented as a Swing JSlider.
* <br>
* A DoubleModel is displayed as slider 0.0 to 1.0 (0%-100%) if < 0.0 0.0 if > 1.0 then 100%
* <br>
* An IntModel is displayed as slider 0 to 100 (0%-100%) if < 0 100 if > 100 then 100%
* <br>
* An LimitIntModel is displayed as progress min to max
*
* @author Marcel Baumann
*/
public class SliderEditor extends DataViewer {
public static final String s_viewername = "SliderEditor";
protected JSlider slider;
/**
* Creates a Slider viewer based on a simple model
*/
public SliderEditor(SimpleModel model) {
super(model);
}
public boolean isEditor() {
return true;
}
/**
* Returns the actual percentage or -1 if indeterminate
*/
public int getPercentage() {
double percentage = -1.0;
if (getDataModel() instanceof DoubleModel) {
percentage = ((DoubleModel) getDataModel()).getDouble();
} else if (getDataModel() instanceof LimitIntModel) {
LimitIntModel lim = (LimitIntModel) getDataModel();
percentage = (double) (lim.getInt() - lim.getLowLimit()) / (double) (lim.getHighLimit() - lim.getLowLimit());
} else if (getDataModel() instanceof IntModel) {
percentage = ((IntModel) getDataModel()).getInt() / 100.0;
}
if (percentage < 0.0) {
return -1;
}
if (percentage > 1.0) {
return 100;
}
return (int) (percentage * 100.0);
}
public void setPercentage(int percent) {
if (model instanceof IntModel) {
((IntModel)model).setInt(percent);
} else if (model instanceof DoubleModel) {
((DoubleModel)model).setDouble(((double)percent) / 100.0);
}
}
/**
* Returns the created JSlider
*/
public JSlider getJSlider() {
return (JSlider) getJComponent();
}
/**
* Creates the slider
*/
public void create() {
slider = new JSlider();
setJComponent(slider);
update();
slider.addChangeListener(new SliderChangeListener());
}
/**
* Set the actual value of the slider
* data model.
*/
public void update() {
if (isCreated()) {
if (model instanceof LimitIntModel) {
LimitIntModel lim = (LimitIntModel) model;
getJSlider().setMinimum(lim.getLowLimit());
getJSlider().setMaximum(lim.getHighLimit());
getJSlider().setValue(lim.getInt());
} else {
int percentage = getPercentage();
if (model.isExceptional() || percentage < 0) {
getJSlider().setValue(0);
} else {
getJSlider().setMaximum(100);
getJSlider().setValue(percentage);
}
}
}
}
/**
* Only used for debugging
*/
public String getViewerName() {
return s_viewername;
}
/**
* Not implemented for JSlider
*/
public void grabFocus() {
}
class SliderChangeListener implements ChangeListener {
public void stateChanged(ChangeEvent e) {
int value = slider.getValue();
if (model instanceof LimitIntModel) {
LimitIntModel limitIntModel = (LimitIntModel)model;
limitIntModel.setInt(value);
} else {
setPercentage(value);
}
}
}
}