001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.properties.editors;
020:
021: import java.awt.Component;
022: import java.awt.Graphics;
023: import java.awt.Rectangle;
024: import java.io.File;
025:
026: import javax.swing.ImageIcon;
027: import javax.swing.JOptionPane;
028: import javax.swing.SwingUtilities;
029:
030: import com.jeta.forms.project.ProjectManager;
031: import com.jeta.forms.store.properties.IconProperty;
032: import com.jeta.open.i18n.I18N;
033: import com.jeta.open.registry.JETARegistry;
034: import com.jeta.swingbuilder.gui.filechooser.FileChooserConfig;
035: import com.jeta.swingbuilder.gui.filechooser.TSFileChooserFactory;
036: import com.jeta.swingbuilder.gui.filechooser.TSFileFilter;
037: import com.jeta.swingbuilder.gui.properties.JETAPropertyEditor;
038: import com.jeta.swingbuilder.gui.utils.FormDesignerUtils;
039: import com.jeta.swingbuilder.resources.Icons;
040:
041: /**
042: * Editor/renderer for handling images and icons
043: *
044: * @author Jeff Tassin
045: */
046: public class IconEditor extends JETAPropertyEditor {
047: /**
048: * Used to render the value of our border
049: */
050: private ValuePainter m_value_painter;
051:
052: private static ImageIcon[] m_icon = { (ImageIcon) FormDesignerUtils
053: .loadImage(Icons.PORTRAIT_16) };
054:
055: /**
056: * ctor
057: */
058: public IconEditor() {
059: m_value_painter = new ValuePainter();
060: m_value_painter.setPreImages(m_icon);
061: }
062:
063: /**
064: * Invokes a dialog used to update the property
065: */
066: public void invokePropertyDialog(Component comp) {
067: FileChooserConfig fcc = new FileChooserConfig(".img",
068: new TSFileFilter("gif,png,jpg,jpeg",
069: "Image Files(*.gif,*.png,*.jpg)"));
070: fcc.setParentComponent(SwingUtilities.getWindowAncestor(comp));
071: File f = TSFileChooserFactory.showOpenDialog(fcc);
072: if (f != null) {
073: try {
074:
075: ProjectManager pmgr = (ProjectManager) JETARegistry
076: .lookup(ProjectManager.COMPONENT_ID);
077: /**
078: * check if the path is contained in a valid package for the
079: * project
080: */
081: // @todo fix to allow embedded images from anywhere
082: if (!pmgr.isValidAbsolutePath(f.getPath())) {
083: String msg = I18N
084: .getLocalizedMessage("Selected image is not in source path.");
085: String title = I18N.getLocalizedMessage("Error");
086: JOptionPane.showMessageDialog(comp, msg, title,
087: JOptionPane.ERROR_MESSAGE);
088: return;
089: }
090:
091: String relativepath = pmgr.getRelativePath(f.getPath());
092: IconProperty iprop = new IconProperty();
093: iprop.setValue((IconProperty) getValue());
094: iprop.setRelativePath(relativepath);
095: setValue(iprop);
096: } catch (Exception e) {
097: String msg = I18N
098: .getLocalizedMessage("Unable to load image.");
099: String title = I18N.getLocalizedMessage("Error");
100: JOptionPane.showMessageDialog(comp, msg, title,
101: JOptionPane.ERROR_MESSAGE);
102: }
103: }
104: }
105:
106: /**
107: * Determines whether this class renders itself using the
108: * paintValue(Graphics g, Rectangle rect) method. Generally, editors that
109: * are not JComponents are paintable.
110: */
111: public boolean isPaintable() {
112: return true;
113: }
114:
115: /**
116: * Method that renders the text on the given graphics context
117: */
118: public void paintValue(Graphics g, Rectangle rect) {
119: // forward the call to the value painter
120: m_value_painter.paintValue(g, rect);
121: }
122:
123: /**
124: *
125: */
126: public void setValue(Object value) {
127: if (value instanceof IconProperty) {
128: super .setValue(value);
129: IconProperty iprop = (IconProperty) value;
130: m_value_painter.setValue(iprop.getDescription());
131: } else if (value == null) {
132: super .setValue(value);
133: m_value_painter.setValue(null);
134: } else {
135: assert (false);
136: }
137: }
138:
139: /**
140: * @return true since we have a custom editor dialog for this type
141: */
142: public boolean supportsCustomEditor() {
143: return true;
144: }
145: }
|