001 /* 002 Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.com>, 003 Laurent Martelli <laurent@aopsys.com> 004 005 This program is free software; you can redistribute it and/or modify 006 it under the terms of the GNU Lesser General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This program is distributed in the hope that it will be useful, but 011 WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 Lesser General Public License for more details. 014 015 You should have received a copy of the GNU Lesser General Public 016 License along with this program; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 018 USA */ 019 020 package org.objectweb.jac.aspects.gui.swing; 021 022 import java.awt.Dimension; 023 import java.awt.Point; 024 import javax.swing.JTextField; 025 import org.objectweb.jac.aspects.gui.FieldEditor; 026 import org.objectweb.jac.core.rtti.FieldItem; 027 import org.objectweb.jac.util.WrappedThrowableException; 028 029 /** 030 * A Swing editor component for date values. 031 */ 032 033 public class PointEditor extends AbstractFieldEditor 034 implements FieldEditor 035 { 036 protected JTextField textField; 037 038 /** 039 * Constructs a new date editor. */ 040 041 public PointEditor(Object substance, FieldItem field) { 042 super(substance,field); 043 textField = new JTextField(10); 044 // swing "bug" workaround : prevent the textField from extending 045 // vertically. 046 textField.setMaximumSize(new Dimension(1000,20)); 047 textField.setText("0,0"); 048 textField.addFocusListener(this); 049 add(textField); 050 } 051 052 public Object getValue() { 053 try { 054 if (textField.getText().equals("")) 055 return null; 056 else 057 return parsePoint(textField.getText()); 058 } catch (Exception e) { 059 throw new WrappedThrowableException(e); 060 } 061 } 062 063 /** 064 * Parses a string like "<x_coord>,<y_coord>" 065 */ 066 public static Point parsePoint(String str) { 067 int coma = str.indexOf(","); 068 if (coma!=-1) { 069 return new Point(Integer.parseInt(str.substring(0,coma)), 070 Integer.parseInt(str.substring(coma+1,str.length()))); 071 } else { 072 throw new RuntimeException("Malformed point string: \""+str+"\""); 073 } 074 } 075 076 public void setValue(Object value) { 077 super.setValue(value); 078 if (value != null) { 079 Point p = (Point)value; 080 textField.setText(p.x+","+p.y); 081 } 082 } 083 084 public void setWidth(int width) { 085 super.setWidth(width); 086 textField.setColumns(width); 087 } 088 089 }