001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.cleverphl.gui;
025:
026: import jacareto.cleverphl.CleverPHL;
027: import jacareto.system.Customization;
028: import jacareto.system.Environment;
029: import jacareto.system.Language;
030: import jacareto.toolkit.EnhancedHashtable;
031:
032: import java.awt.BorderLayout;
033: import java.awt.Color;
034: import java.awt.Point;
035: import java.awt.event.ActionEvent;
036: import java.awt.event.ActionListener;
037: import java.awt.event.MouseEvent;
038: import java.awt.event.MouseListener;
039:
040: import java.util.Enumeration;
041: import java.util.Hashtable;
042: import java.util.Vector;
043:
044: import javax.swing.DefaultListSelectionModel;
045: import javax.swing.JButton;
046: import javax.swing.JDialog;
047: import javax.swing.JList;
048: import javax.swing.JPanel;
049: import javax.swing.JScrollPane;
050: import javax.swing.border.EmptyBorder;
051: import javax.swing.border.LineBorder;
052: import javax.swing.border.TitledBorder;
053:
054: /**
055: * Dialog for choosing the record type.
056: *
057: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
058: * @version 1.01
059: */
060: public class RecordTypeDialog extends JDialog implements ActionListener {
061: /** The CleverPHL instance. */
062: private CleverPHL cleverPHL;
063:
064: /** The environment. */
065: private Environment env;
066:
067: /** The list of types. */
068: private JList list;
069:
070: /** The name of the type which has been selected. */
071: private String selectedType;
072:
073: /** The map of al types. */
074: private EnhancedHashtable typesMap;
075:
076: /** Maps the language strings to types. */
077: private Hashtable languageToType;
078:
079: /** The ok button. */
080: private JButton okButton;
081:
082: /** The cancel button. */
083: private JButton cancelButton;
084:
085: /**
086: * Creates a new dialog (visible).
087: *
088: * @param cleverPHL the CleverPHL instance
089: */
090: public RecordTypeDialog(CleverPHL cleverPHL) {
091: super (cleverPHL.getMainFrame(), true);
092: this .cleverPHL = cleverPHL;
093: this .env = cleverPHL.getEnvironment();
094: selectedType = null;
095: setName(cleverPHL.getCustomization().getString(
096: "Components.JacaretoComponent", "JACARETO_COMPONENT"));
097:
098: Language language = env.getLanguage();
099: Customization customization = env.getCustomization();
100:
101: setTitle(language.getString("CleverPHL.RecordTypeDialog.Title"));
102:
103: // Load known record types
104: typesMap = customization.getMap("RootElement.Types",
105: new EnhancedHashtable());
106: languageToType = new Hashtable();
107:
108: Vector typeNames = new Vector(typesMap.size());
109: Enumeration enumeration = typesMap.keys();
110:
111: while (enumeration.hasMoreElements()) {
112: String type = (String) enumeration.nextElement();
113: String langString = language.getString(type);
114: typeNames.add(langString);
115: languageToType.put(langString, type);
116: }
117:
118: list = new JList(typeNames);
119: list
120: .setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
121:
122: TitledBorder listBorder = new TitledBorder(new LineBorder(
123: Color.black), language
124: .getString("CleverPHL.RecordTypeDialog.ListLabel"));
125:
126: //list.setFixedCellWidth ((int) listBorder.getMinimumSize(this).getWidth());
127: list.setFixedCellWidth(200);
128: list.setVisibleRowCount(10);
129: list.addMouseListener(new ListMouseListener(this ));
130:
131: JScrollPane listScrollPane = new JScrollPane(list);
132: listScrollPane.setBorder(listBorder);
133:
134: JPanel leftPanelWrapper = new JPanel();
135: leftPanelWrapper.setLayout(new BorderLayout());
136: leftPanelWrapper.setBorder(new EmptyBorder(5, 5, 5, 5));
137: leftPanelWrapper.add(listScrollPane, BorderLayout.NORTH);
138:
139: // The panel with the two buttons
140: JPanel buttonPanel = new JPanel();
141: okButton = new JButton(language.getString("General.Ok"));
142: okButton.addActionListener(this );
143: cancelButton = new JButton(language.getString("General.Cancel"));
144: cancelButton.addActionListener(this );
145: buttonPanel.add(okButton);
146: buttonPanel.add(cancelButton);
147: buttonPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
148:
149: // Add all to the content pane
150: getContentPane().setLayout(new BorderLayout());
151: getContentPane().add(leftPanelWrapper, BorderLayout.CENTER);
152: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
153:
154: Point ownerLoc = cleverPHL.getMainFrame().getLocation();
155: setLocation(((int) ownerLoc.getX()) + 30, ((int) ownerLoc
156: .getY()) + 30);
157:
158: pack();
159: setResizable(false);
160: setVisible(true);
161: }
162:
163: /**
164: * Called when one of the two buttons has been pressed.
165: *
166: * @param e the action event
167: */
168: public void actionPerformed(ActionEvent e) {
169: Language language = cleverPHL.getLanguage();
170: Object source = e.getSource();
171:
172: if (source == okButton) {
173: if (list.isSelectionEmpty()) {
174: new CleverPHLMsgDialog(
175: cleverPHL,
176: this ,
177: language.getString("General.Error"),
178: language
179: .getString("CleverPHL.RecordTypeDialog.Error.NoSelectedType"),
180: language.getString("General.Ok"));
181: } else {
182: close();
183: }
184: } else if (source == cancelButton) {
185: dispose();
186: }
187: }
188:
189: /**
190: * Closes this dialog and starts the selected program.
191: */
192: private void close() {
193: if (list.isSelectionEmpty()) {
194: Language language = cleverPHL.getLanguage();
195: new CleverPHLMsgDialog(
196: cleverPHL,
197: this ,
198: language.getString("General.Error"),
199: language
200: .getString("CleverPHL.RecordTypeDialog.Error.NoSelectedType"),
201: language.getString("General.Ok"));
202: } else {
203: setVisible(false);
204:
205: String typeName = (String) languageToType.get(list
206: .getSelectedValue());
207: selectedType = (String) typesMap.get(typeName);
208: dispose();
209: }
210: }
211:
212: /**
213: * Returns the name of the selected type after this dialog has been closed, or
214: * <code>null</code> if no type has been selected.
215: *
216: * @return DOCUMENT ME!
217: */
218: public String getSelectedType() {
219: return selectedType;
220: }
221:
222: /**
223: * Listener for the double click on the list elements.
224: */
225: static class ListMouseListener implements MouseListener {
226: //~ Instance fields ------------------------------------------------------------------------
227:
228: /** The dialog this listener belongs to. */
229: private RecordTypeDialog dialog;
230:
231: //~ Constructors ---------------------------------------------------------------------------
232:
233: /**
234: * Creates a new listener object.
235: *
236: * @param dialog DOCUMENT ME!
237: */
238: public ListMouseListener(RecordTypeDialog dialog) {
239: this .dialog = dialog;
240: }
241:
242: //~ Methods --------------------------------------------------------------------------------
243:
244: /**
245: * Calls the <code>notifyDoubleClick</code> method of the starter frame when the mouse has
246: * been double clicked.
247: *
248: * @param e the mouse event.
249: */
250: public void mouseClicked(MouseEvent e) {
251: if (e.getClickCount() == 2) {
252: dialog.close();
253: }
254: }
255:
256: /**
257: * {@inheritDoc}
258: */
259: public void mouseEntered(MouseEvent e) {
260: }
261:
262: /**
263: * {@inheritDoc}
264: */
265: public void mouseExited(MouseEvent e) {
266: }
267:
268: /**
269: * {@inheritDoc}
270: */
271: public void mousePressed(MouseEvent e) {
272: }
273:
274: /**
275: * {@inheritDoc}
276: */
277: public void mouseReleased(MouseEvent e) {
278: }
279: }
280: }
|