001: /*
002: JSmooth: a VM wrapper toolkit for Windows
003: Copyright (C) 2003 Rodrigo Reyes <reyes@charabia.net>
004:
005: This program is free software; you can redistribute it and/or modify
006: it under the terms of the GNU General Public License as published by
007: the Free Software Foundation; either version 2 of the License, or
008: (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public License
016: along with this program; if not, write to the Free Software
017: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018:
019: */
020:
021: package net.charabia.jsmoothgen.application.gui.editors;
022:
023: import net.charabia.jsmoothgen.skeleton.*;
024: import net.charabia.jsmoothgen.application.*;
025: import net.charabia.jsmoothgen.application.gui.*;
026: import net.charabia.jsmoothgen.application.gui.util.*;
027: import javax.swing.*;
028: import java.awt.*;
029: import java.util.*;
030: import java.io.*;
031: import com.l2fprod.common.swing.*;
032: import com.l2fprod.common.propertysheet.*;
033:
034: public class ExecutableIcon extends Editor {
035: private FileSelectionTextField m_selector = new FileSelectionTextField();
036: private JLabel m_iconDisplay = new JLabel("(no image)");
037:
038: public ExecutableIcon() {
039: setLayout(new BorderLayout());
040: add(BorderLayout.CENTER, m_selector);
041: add(BorderLayout.SOUTH, m_iconDisplay);
042:
043: m_iconDisplay.setHorizontalAlignment(JLabel.CENTER);
044:
045: m_selector
046: .addListener(new FileSelectionTextField.FileSelected() {
047: public void fileSelected(String filename) {
048: // System.out.println("new icon: " + filename);
049: setIconLocation(new File(filename));
050: }
051: });
052: }
053:
054: public void dataChanged() {
055: if (getBaseDir() != null)
056: m_selector.setBaseDir(getBaseDir());
057:
058: if (m_model.getIconLocation() != null) {
059: m_selector.setFile(getAbsolutePath(new java.io.File(m_model
060: .getIconLocation())));
061: setIconLocation(getAbsolutePath(new java.io.File(m_model
062: .getIconLocation())));
063:
064: } else {
065: m_selector.setFile(null);
066: setIconLocation(new File(""));
067: }
068: }
069:
070: public void updateModel() {
071: File f = m_selector.getFile();
072: if (f != null)
073: m_model.setIconLocation(m_selector.getFile().toString());
074: else
075: m_model.setIconLocation(null);
076: }
077:
078: public String getLabel() {
079: return "ICONLOCATION_LABEL";
080: }
081:
082: public String getDescription() {
083: return "ICONLOCATION_HELP";
084: }
085:
086: private void setIconLocation(File iconfile) {
087: if (iconfile.isAbsolute() == false) {
088: iconfile = new File(m_basedir, iconfile.toString());
089: }
090: ImageIcon icon = null;
091:
092: // System.out.println("setIconLocation: " + iconfile);
093:
094: if (iconfile.toString().toUpperCase().endsWith(".ICO")) {
095: //
096: // Try to load with our ico codec...
097: //
098: try {
099: java.awt.image.BufferedImage[] images = net.charabia.util.codec.IcoCodec
100: .loadImages(iconfile);
101: if ((images != null) && (images.length > 0)) {
102: java.awt.Image img = images[0];
103: icon = new ImageIcon(img);
104: }
105: } catch (java.io.IOException exc) {
106: exc.printStackTrace();
107: }
108: } else // Otherwise try with the standard toolkit functions...
109: {
110: icon = new javax.swing.ImageIcon(
111: iconfile.getAbsolutePath(), "default icon");
112: }
113:
114: if (icon != null) {
115: int width = icon.getIconWidth();
116: int height = icon.getIconHeight();
117:
118: m_iconDisplay.setIcon(icon);
119: m_iconDisplay.setText("");
120: m_model.setIconLocation(iconfile.getAbsolutePath());
121: this .validate();
122: this .invalidate();
123: } else {
124: m_iconDisplay.setIcon(null);
125: m_iconDisplay.setText("(no image)");
126: m_model.setIconLocation(null);
127: }
128:
129: doLayout();
130: invalidate();
131: validate();
132: repaint();
133: }
134:
135: }
|