001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.gui.directory;
019:
020: import java.awt.Color;
021: import java.awt.Frame;
022: import java.awt.event.FocusAdapter;
023: import java.awt.event.FocusEvent;
024: import java.awt.event.KeyAdapter;
025: import java.awt.event.KeyEvent;
026: import java.awt.event.WindowAdapter;
027: import java.awt.event.WindowEvent;
028: import java.beans.PropertyChangeEvent;
029: import java.beans.PropertyChangeListener;
030: import java.util.Collections;
031: import java.util.Comparator;
032: import java.util.Iterator;
033: import java.util.Vector;
034: import java.util.logging.Logger;
035:
036: import javax.swing.JComboBox;
037: import javax.swing.JLabel;
038: import javax.swing.JList;
039: import javax.swing.JOptionPane;
040: import javax.swing.ListCellRenderer;
041:
042: import de.finix.contelligent.client.base.Type;
043: import de.finix.contelligent.client.base.TypeFactory;
044: import de.finix.contelligent.client.i18n.Resources;
045: import de.finix.contelligent.client.util.EscapeDialog;
046:
047: public class ConvertTypeDialog extends EscapeDialog {
048:
049: private static Logger logger = Logger
050: .getLogger(ConvertTypeDialog.class.getName());
051:
052: private String type = null;
053:
054: private JComboBox typeBox;
055:
056: private JOptionPane optionPane;
057:
058: private String incrementalSearchString = "";
059:
060: private Vector typeList;
061:
062: private String packageIdentifier = "";
063:
064: public ConvertTypeDialog(Frame frame, String preselectedTypeName) {
065: super (frame, true);
066:
067: addFocusListener(new FocusAdapter() {
068: public void focusGained(FocusEvent e) {
069: toFront();
070: }
071: });
072:
073: setTitle(Resources.getLocalString("convert_type_title"));
074: JLabel warningLabel = new JLabel(Resources
075: .getLocalString("convert_type_message"));
076:
077: typeList = new Vector(TypeFactory.getInstance()
078: .getComposedTypes().values());
079: Collections.sort(typeList, new Comparator() {
080: public int compare(Object o1, Object o2) {
081: String s1 = ((Type) o1).getName();
082: String s2 = ((Type) o2).getName();
083: return (s1.compareToIgnoreCase(s2));
084: }
085:
086: public boolean equals(Object obj) {
087: if (obj == this )
088: return true;
089: return false;
090: }
091:
092: public int hashCode() {
093: return -1;
094: }
095: });
096:
097: typeBox = new JComboBox(typeList);
098: typeBox.setBackground(Color.white);
099: typeBox.setRenderer(new ComboBoxRenderer());
100: typeBox.addFocusListener(new FocusAdapter() {
101: public void focusLost(FocusEvent e) {
102: incrementalSearchString = "";
103: }
104: });
105:
106: typeBox.addKeyListener(new KeyAdapter() {
107: public void keyReleased(KeyEvent e) {
108: if (e.getKeyCode() == KeyEvent.VK_DELETE
109: || e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
110: if (incrementalSearchString.length() == 1) {
111: incrementalSearchString = "";
112: } else if (incrementalSearchString.length() > 1) {
113: incrementalSearchString = incrementalSearchString
114: .substring(0, incrementalSearchString
115: .length() - 1);
116: }
117: searchIncremental();
118: e.consume();
119: } else if (!e.isActionKey() && !e.isControlDown()) {
120: char c = e.getKeyChar();
121: if (Character.isLetterOrDigit(c)
122: || Character.isSpaceChar(c)) {
123: incrementalSearchString = incrementalSearchString
124: + c;
125: searchIncremental();
126: e.consume();
127: }
128: }
129: }
130: });
131: if (preselectedTypeName != null) {
132: typeBox
133: .setSelectedIndex(findIndexForTypeStartingWith(preselectedTypeName));
134: }
135:
136: Object[] array = { warningLabel, typeBox };
137:
138: optionPane = new JOptionPane(array,
139: JOptionPane.QUESTION_MESSAGE,
140: JOptionPane.OK_CANCEL_OPTION);
141:
142: setContentPane(optionPane);
143: setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
144:
145: addWindowListener(new WindowAdapter() {
146: public void windowClosing(WindowEvent we) {
147: /*
148: * Instead of directly closing the window, we're going to change
149: * the JOptionPane's value property.
150: */
151: optionPane.setValue(new Integer(
152: JOptionPane.CLOSED_OPTION));
153: }
154: });
155:
156: optionPane
157: .addPropertyChangeListener(new PropertyChangeListener() {
158: public void propertyChange(PropertyChangeEvent e) {
159: String prop = e.getPropertyName();
160:
161: if (isVisible()
162: && (e.getSource() == optionPane)
163: && (prop
164: .equals(JOptionPane.VALUE_PROPERTY) || prop
165: .equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
166: Object value = optionPane.getValue();
167:
168: if (value == JOptionPane.UNINITIALIZED_VALUE) {
169: // ignore reset
170: return;
171: }
172:
173: int option = JOptionPane.CANCEL_OPTION;
174: if (value instanceof Integer) {
175: option = ((Integer) value).intValue();
176: }
177: if (option == JOptionPane.OK_OPTION) {
178: // Check input
179: boolean validName = true;
180: if (validName) {
181: type = ((Type) typeBox
182: .getSelectedItem())
183: .getName();
184: setVisible(false);
185: } else {
186: // Reset the JOptionPane's value.
187: // If you don't do this, then if the user
188: // presses the same button next time, no
189: // property change event will be fired.
190: optionPane
191: .setValue(JOptionPane.UNINITIALIZED_VALUE);
192: }
193: } else { // user closed dialog or clicked cancel
194: type = null;
195: setVisible(false);
196: }
197: }
198: }
199: });
200: }
201:
202: public int getOption() {
203: if (optionPane.getValue() instanceof Integer
204: && ((Integer) optionPane.getValue()).intValue() == JOptionPane.OK_OPTION) {
205: return JOptionPane.OK_OPTION;
206: } else {
207: return JOptionPane.CANCEL_OPTION;
208: }
209: }
210:
211: public String getType() {
212: return type;
213: }
214:
215: private void searchIncremental() {
216: if (typeList != null) {
217: if (incrementalSearchString.length() == 0) {
218: typeBox.setSelectedIndex(0);
219: } else {
220: incrementalSearchString = incrementalSearchString
221: .toLowerCase();
222: typeBox
223: .setSelectedIndex(findIndexForTypeStartingWith(incrementalSearchString));
224: }
225: }
226: }
227:
228: private int findIndexForTypeStartingWith(String searchString) {
229: int i = 0;
230: for (Iterator it = typeList.iterator(); it.hasNext(); i++) {
231: Type type = (Type) it.next();
232: String name = type.getName();
233: if (name.toLowerCase().startsWith(searchString)) {
234: typeBox.setSelectedIndex(i);
235: return i;
236: }
237: }
238: return 0;
239: }
240:
241: class ComboBoxRenderer extends JLabel implements ListCellRenderer {
242:
243: public ComboBoxRenderer() {
244: setOpaque(true);
245: setHorizontalAlignment(LEFT);
246: setVerticalAlignment(CENTER);
247: }
248:
249: public java.awt.Component getListCellRendererComponent(
250: JList list, Object value, int index,
251: boolean isSelected, boolean cellHasFocus) {
252:
253: if (isSelected) {
254: setBackground(list.getSelectionBackground());
255: setForeground(list.getSelectionForeground());
256: } else {
257: setBackground(Color.white);
258: setForeground(list.getForeground());
259: }
260:
261: Type type = (Type) value;
262: setIcon(type.getIcon());
263: setText(type.getName());
264:
265: return this;
266: }
267: }
268: }
|