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.images;
020:
021: import java.awt.Toolkit;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.io.File;
025:
026: import javax.swing.ImageIcon;
027: import javax.swing.JOptionPane;
028:
029: import com.jeta.forms.project.ProjectManager;
030: import com.jeta.open.gui.framework.JETAController;
031: import com.jeta.open.i18n.I18N;
032: import com.jeta.open.registry.JETARegistry;
033: import com.jeta.swingbuilder.gui.filechooser.FileChooserConfig;
034: import com.jeta.swingbuilder.gui.filechooser.TSFileChooserFactory;
035: import com.jeta.swingbuilder.gui.filechooser.TSFileFilter;
036:
037: /**
038: * Handles events for the ImagePropertiesView
039: *
040: * @author Jeff Tassin
041: */
042: public class ImagePropertiesController extends JETAController {
043: /**
044: * The view
045: */
046: private ImagePropertiesView m_view;
047:
048: /**
049: * ctor
050: */
051: public ImagePropertiesController(ImagePropertiesView view) {
052: super (view);
053: m_view = view;
054: assignAction(ImagePropertiesNames.ID_FILE_BUTTON,
055: new FileAction());
056: }
057:
058: /**
059: * Action for loading an image from a file.
060: */
061: public class FileAction implements ActionListener {
062: public void actionPerformed(ActionEvent evt) {
063: FileChooserConfig fcc = new FileChooserConfig(".img",
064: new TSFileFilter("gif,png,jpg,jpeg",
065: "Image Files(*.gif,*.png,*.jpg)"));
066: fcc.setParentComponent(m_view);
067: File f = TSFileChooserFactory.showOpenDialog(fcc);
068: if (f != null) {
069: try {
070:
071: ProjectManager pmgr = (ProjectManager) JETARegistry
072: .lookup(ProjectManager.COMPONENT_ID);
073: /**
074: * check if the path is contained in a valid package for the
075: * project
076: */
077: // @todo fix to allow embedded images from anywhere
078: if (!pmgr.isValidAbsolutePath(f.getPath())) {
079: String msg = I18N
080: .getLocalizedMessage("Selected image is not in source path.");
081: String title = I18N
082: .getLocalizedMessage("Error");
083: JOptionPane.showMessageDialog(m_view, msg,
084: title, JOptionPane.ERROR_MESSAGE);
085: return;
086: }
087:
088: /**
089: * The toolkit should be used to create the image, otherwise
090: * it may be pulled from the toolkit image cache.
091: */
092: Toolkit toolkit = Toolkit.getDefaultToolkit();
093: ImageIcon image = new ImageIcon(toolkit
094: .createImage(f.getPath()));
095: m_view.setImage(image);
096: m_view.setText(ImagePropertiesNames.ID_FILE_FIELD,
097: pmgr.getRelativePath(f.getPath()));
098: m_view.setText(
099: ImagePropertiesNames.ID_DESCRIPTION_FIELD,
100: f.getName());
101: } catch (Exception e) {
102: String msg = I18N
103: .getLocalizedMessage("Unable to load image.");
104: String title = I18N.getLocalizedMessage("Error");
105: JOptionPane.showMessageDialog(m_view, msg, title,
106: JOptionPane.ERROR_MESSAGE);
107: }
108: }
109: }
110: }
111: }
|