View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans.
5    *
6    * CosyBeans is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui;
21  
22  import java.awt.Component;
23  import java.awt.Dimension;
24  import java.awt.Graphics;
25  import java.awt.GridBagConstraints;
26  import java.awt.GridBagLayout;
27  import java.awt.Insets;
28  import java.awt.event.KeyAdapter;
29  import java.awt.event.KeyEvent;
30  
31  import javax.swing.Icon;
32  import javax.swing.ImageIcon;
33  import javax.swing.JLabel;
34  import javax.swing.JPanel;
35  import javax.swing.JTextField;
36  import javax.swing.SwingConstants;
37  
38  import com.cosylab.gui.components.util.IconHelper;
39  import com.cosylab.gui.components.util.PaintHelper;
40  
41  /**
42   * <code>IconEditor</code> is a simple editor which enables selecting an icon.
43   * User can enter the path to the icon and the icon will be shown next to the
44   * input field. Icon can be selected from the classpath.
45   * 
46   * @author Jaka Bobnar, Cosylab
47   *
48   */
49  public class IconEditor extends JPanel {
50  	
51  	private static final long serialVersionUID = 7625485410292542663L;
52  
53  	public static final String ICON = "icon";
54  	
55  	private JLabel iconLabel;
56  	private JLabel titleLabel;
57  	private JTextField pathField;
58  	private String title;
59  	private Icon selectedIcon;
60  	private static final int HEIGHT = 32;
61  	private static final int WIDTH = 32;
62  	private static final Icon NULL_ICON = new Icon() {
63  		public int getIconHeight() {
64  			return HEIGHT;
65  		}
66  		public int getIconWidth() {
67  			return WIDTH;
68  		}
69  		public void paintIcon(Component c, Graphics g, int x, int y) {
70  			PaintHelper.paintDisabled(g, x, y, WIDTH, HEIGHT);			
71  		}
72  	};
73  	
74  	/**
75  	 * Constructs a new IconEditor.
76  	 *
77  	 */
78  	public IconEditor() {
79  		this("Icon");
80  	}
81  	
82  	/**
83  	 * Constructs a new IconEditor with the given title.
84  	 * 
85  	 * @param title
86  	 */
87  	public IconEditor(String title) {
88  		super();
89  		this.title = title;
90  		initialize();
91  	}
92  	
93  	private void initialize() {
94  		setLayout(new GridBagLayout());
95  		titleLabel = new JLabel(title);
96  		this.add(titleLabel, new GridBagConstraints(0,0,2,1,1,0,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(11,11,1,11),0,0));
97  		this.add(getPathField(), new GridBagConstraints(0,1,1,1,1,0,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(1,11,11,0),0,0));
98  		this.add(getIconLabel(), new GridBagConstraints(1,1,1,1,0,0,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(1,4,11,11),0,0));
99  	}
100 	
101 	private JTextField getPathField() {
102 		if (pathField == null) {
103 			pathField = new JTextField();
104 			pathField.setMaximumSize(new Dimension(100,21));
105 			pathField.setPreferredSize(new Dimension(100,21));
106 			pathField.setMinimumSize(new Dimension(100,21));
107 			pathField.addKeyListener(new KeyAdapter() {
108 				@Override
109 				public void keyReleased(KeyEvent e) {
110 					internalSetIcon(IconHelper.createIcon(pathField.getText()), false);
111 				}
112 			});
113 			pathField.setToolTipText("Type image file path from classpath.");
114 		}
115 
116 		return pathField;
117 	}
118 	
119 	private JLabel getIconLabel() {
120 		if (iconLabel == null) {
121 			iconLabel = new JLabel();
122 			iconLabel.setHorizontalAlignment(SwingConstants.RIGHT);
123 			iconLabel.setMinimumSize(new Dimension(WIDTH + 5, HEIGHT + 5));
124 			iconLabel.setMaximumSize(new Dimension(WIDTH + 5, HEIGHT + 5));
125 			iconLabel.setPreferredSize(new Dimension(WIDTH + 5, HEIGHT + 5));
126 			iconLabel.setIcon(NULL_ICON);
127 		}
128 		return iconLabel;
129 	}
130 	
131 	/**
132 	 * Sets the Icon to this editor.
133 	 * 
134 	 * @param icon
135 	 */
136 	public void setIcon(Icon icon) {
137 		internalSetIcon(icon, true);
138 	}
139 	
140 	/**
141 	 * Sets the path to the icon.
142 	 * 
143 	 * @param path
144 	 */
145 	public void setIcon(String path) {
146 		String old = getIconPath();
147 		if ((old != null && old.equals(path)) || (old == null && path == null)) return;
148 		internalSetIcon(IconHelper.createIcon(path), true);
149 		getPathField().setText(path);
150 	}
151 	
152 	private void internalSetIcon(Icon icon, boolean setPathField) {
153 		Icon oldValue = getIconLabel().getIcon();
154 		if ((oldValue != null && oldValue.equals(icon)) || 
155 			(oldValue == null && icon == null)) return;
156 		selectedIcon = icon;
157 		if (icon instanceof ImageIcon && icon.getIconHeight() > HEIGHT && icon.getIconWidth() > WIDTH) {
158 			getIconLabel().setIcon(IconCustomizer.getScaledIcon((ImageIcon)icon, HEIGHT,WIDTH));
159 		} else if (icon != null){
160 			getIconLabel().setIcon(icon);
161 		} else {
162 			getIconLabel().setIcon(NULL_ICON);
163 		}
164 		
165 		if (icon != null && setPathField)
166 			getPathField().setText(icon.toString());
167 		
168 		firePropertyChange(ICON, oldValue, icon);
169 	}
170 	
171 	/**
172 	 * Returns the selected icon.
173 	 * @return
174 	 */
175 	public Icon getIcon() {
176 		return selectedIcon;
177 	}
178 
179 	/**
180 	 * Returns the title.
181 	 * 
182 	 * @return
183 	 */
184 	public String getTitle() {
185 		return titleLabel.getText();
186 	}
187 	
188 	/**
189 	 * Returns the path to the icon.
190 	 * 
191 	 * @return
192 	 */
193 	public String getIconPath() {
194 		return getPathField().getText();
195 	}
196 
197 	/**
198 	 * Sets the title.
199 	 * 
200 	 * @param title
201 	 */
202 	public void setTitle(String title) {
203 		this.title = title;
204 		titleLabel.setText(title);
205 	}
206 }