001 package net.sourceforge.mp3dings.editors;
002 import javax.swing.AbstractCellEditor;
003 import javax.swing.JButton;
004 import javax.swing.JOptionPane;
005 import javax.swing.JTable;
006 import javax.swing.SwingUtilities;
007 import javax.swing.table.TableCellEditor;
008 import java.awt.Component;
009 import java.awt.event.ActionListener;
010 import java.awt.event.MouseEvent;
011 import java.awt.event.ActionEvent;
012 import java.awt.event.KeyEvent;
013 import java.awt.event.WindowAdapter;
014 import java.awt.event.WindowEvent;
015 import java.util.EventObject;
016 import helliker.id3.APICImage;
017 import net.sourceforge.mp3dings.Globals;
018
019 /*
020 Copyright (C) 2007 Moritz Ringler
021 This library is free software; you can redistribute it and/or
022 modify it under the terms of the GNU Lesser General Public
023 License as published by the Free Software Foundation; either
024 version 2.1 of the License, or (at your option) any later version.
025 This library is distributed in the hope that it will be useful,
026 but WITHOUT ANY WARRANTY; without even the implied warranty of
027 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
028 Lesser General Public License for more details.
029 You should have received a copy of the GNU Lesser General Public
030 License along with this library; if not, write to the Free Software
031 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
032 */
033
034 public class ImageCellEditor extends AbstractCellEditor implements TableCellEditor{
035 private APICImageEditor.EditorDialog dialog;
036 private APICImage image;
037 private Component parent;
038 private JButton button = new JButton();
039
040 public ImageCellEditor(){
041 button.addActionListener(new ActionListener(){
042 public void actionPerformed(ActionEvent e){
043 buttonPressed();
044 }
045 });
046 button.setText(Globals.getString("IMAGE_EDITING"));
047 }
048
049 public Object getCellEditorValue() {
050 return image;
051 }
052
053 private void hideDialog(){
054 if(dialog != null){
055 if(dialog.isVisible()){
056 dialog.setVisible(false);
057 }
058 dialog.reset();
059 }
060 }
061
062 public void cancelCellEditing(){
063 hideDialog();
064 fireEditingCanceled();
065 }
066
067 public boolean stopCellEditing(){
068 try{
069 if(dialog != null){
070 image = dialog.getImage();
071 hideDialog();
072 }
073 } catch (Exception ex){
074 JOptionPane.showMessageDialog(parent, ex,Globals.getString("EDIT_PICTURE_ERR"), JOptionPane.WARNING_MESSAGE);
075 }
076 fireEditingStopped();
077 return true;
078 }
079
080 public boolean isCellEditable(EventObject event) {
081 boolean result = false;
082 if(event instanceof MouseEvent){
083 result = (((MouseEvent)event).getClickCount() >= 2);
084 } else if (event instanceof KeyEvent){
085 KeyEvent kevent = (KeyEvent) event;
086 result = (kevent.getID() == KeyEvent.KEY_PRESSED) &&
087 (kevent.getKeyChar() == ' ');
088 } else if
089 // Workaround for
090 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4852305
091 (event == null){
092 int[] jversion = Globals.getJavaVersion();
093 String vmvendor = System.getProperty("java.vm.vendor");
094 result = (vmvendor != null) &&
095 (vmvendor.indexOf("Sun") >= 0) &&
096 jversion != null &&
097 jversion.length >= 2 &&
098 jversion[0] == 1 &&
099 jversion[1] <= 5;
100 }
101 return result;
102 }
103
104 public Component getTableCellEditorComponent(JTable table,
105 Object value, boolean isSelected, int row, int column)
106 {
107 image = (APICImage) value;
108 parent = table;
109 // Assures the dialog is openend.
110 // If we don't do this the dialog is not opened
111 // in Java < 1.6, and opened in Java 1.6.
112 SwingUtilities.invokeLater(new Runnable(){
113 public void run(){
114 if(dialog == null || !dialog.isVisible()){
115 button.doClick();
116 }
117 }
118 });
119 return button;
120 }
121
122 private void buttonPressed(){
123 if(dialog == null){
124 dialog = APICImageEditor.getDialog(parent,Globals.getString("EDIT_PICTURE"), APICImageEditor.getDefault());
125 dialog.addWindowListener(new WindowAdapter(){
126 public void windowClosing(WindowEvent e){
127 cancelCellEditing();
128 dialog.setVisible(false);
129 }
130 });
131 dialog.addActionListener(new ActionListener(){
132 public void actionPerformed(ActionEvent e){
133 final String cmd = e.getActionCommand();
134 if(dialog.OK.equals(cmd)){
135 stopCellEditing();
136 } else if(dialog.CANCEL.equals(cmd)){
137 cancelCellEditing();
138 }
139 }
140 });
141 }
142 dialog.setImage(image);
143 dialog.pack();
144 //the following three lines
145 //show the dialog keeping the focus on the window with the jtable.
146 //so that cursor navigation through the JTable is not interrupted
147 dialog.setFocusableWindowState(false);
148 dialog.setVisible(true);
149 dialog.setFocusableWindowState(true);
150 }
151 }