001/* 002 * Copyright 2010-2015 Institut Pasteur. 003 * 004 * This file is part of Icy. 005 * 006 * Icy is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Icy is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Icy. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package icy.gui.component.editor; 020 021import java.awt.Component; 022 023import javax.swing.AbstractCellEditor; 024import javax.swing.JSlider; 025import javax.swing.JTable; 026import javax.swing.JTree; 027import javax.swing.event.ChangeEvent; 028import javax.swing.event.ChangeListener; 029import javax.swing.table.TableCellEditor; 030import javax.swing.tree.TreeCellEditor; 031 032/** 033 * @author Stephane 034 */ 035public class SliderCellEditor extends AbstractCellEditor implements TableCellEditor, TreeCellEditor, ChangeListener 036{ 037 /** 038 * 039 */ 040 private static final long serialVersionUID = -4946926120641246542L; 041 042 private JSlider slider; 043 044 /** 045 * internals 046 */ 047 private JTable table; 048 private int row; 049 private int column; 050 boolean liveUpdate; 051 052 /** 053 * Create a SliderEditor for JTable or JTree. 054 * 055 * @param liveUpdate 056 * set to true if you want live update on slider change 057 */ 058 public SliderCellEditor(boolean liveUpdate) 059 { 060 slider = new JSlider(0, 1000); 061 062 slider.addChangeListener(this); 063 slider.setRequestFocusEnabled(false); 064 065 table = null; 066 this.liveUpdate = liveUpdate; 067 } 068 069 @Override 070 public Object getCellEditorValue() 071 { 072 return Integer.valueOf(slider.getValue()); 073 } 074 075 @Override 076 public void stateChanged(ChangeEvent e) 077 { 078 if (slider.getValueIsAdjusting()) 079 { 080 // not very elegant but needed for live update on JTable 081 if (liveUpdate && (table != null)) 082 { 083 final int value = slider.getValue(); 084 085 if (((Integer) table.getValueAt(row, column)).intValue() != value) 086 table.setValueAt(Integer.valueOf(value), row, column); 087 } 088 } 089 else 090 stopCellEditing(); 091 } 092 093 @Override 094 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) 095 { 096 this.table = table; 097 this.row = row; 098 this.column = column; 099 100 final int intValue = ((Integer) value).intValue(); 101 102 if (slider.getValue() != intValue) 103 slider.setValue(intValue); 104 105 slider.invalidate(); 106 107 return slider; 108 } 109 110 @Override 111 public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, 112 boolean leaf, int row) 113 { 114 final int intValue = ((Integer) value).intValue(); 115 116 if (slider.getValue() != intValue) 117 slider.setValue(intValue); 118 119 return slider; 120 } 121}