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.editor;
025:
026: import jacareto.starter.AppletStarter;
027: import jacareto.starter.Starter;
028: import jacareto.system.Environment;
029: import jacareto.system.Language;
030:
031: import java.awt.BorderLayout;
032: import java.awt.Container;
033: import java.awt.Dialog;
034: import java.awt.Dimension;
035: import java.awt.FlowLayout;
036: import java.awt.Frame;
037: import java.awt.GridBagConstraints;
038: import java.awt.GridBagLayout;
039: import java.awt.Insets;
040: import java.awt.Point;
041: import java.awt.Window;
042: import java.awt.event.ActionEvent;
043: import java.awt.event.ActionListener;
044:
045: import java.util.Enumeration;
046: import java.util.Hashtable;
047:
048: import javax.swing.JButton;
049: import javax.swing.JDialog;
050: import javax.swing.JLabel;
051: import javax.swing.JPanel;
052: import javax.swing.JScrollPane;
053: import javax.swing.JTable;
054: import javax.swing.JTextField;
055: import javax.swing.ListSelectionModel;
056: import javax.swing.SwingUtilities;
057: import javax.swing.event.ListSelectionEvent;
058: import javax.swing.event.ListSelectionListener;
059: import javax.swing.table.DefaultTableModel;
060:
061: /**
062: * The panel for the systemproperties in the starters..
063: *
064: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
065: * @version 1.03
066: */
067: public class SystemPropertiesPanel extends JPanel implements
068: ActionListener, ListSelectionListener {
069: /** The language. */
070: private Language language;
071:
072: /** The table. */
073: private JTable table;
074:
075: /** The table model. */
076: private DefaultTableModel tableModel;
077:
078: /** The associated editor. */
079: private Editor editor;
080:
081: /** The button for a new image entry. */
082: private JButton newButton;
083:
084: /** The button for editing an image entry. */
085: private JButton editButton;
086:
087: /** The delete button. */
088: private JButton deleteButton;
089:
090: /** Whether or not the starter should be updated. */
091: private boolean isUpdateOnChange;
092:
093: /**
094: * Creates a new instance.
095: *
096: * @param env the environemt
097: * @param editor the associated starter editor
098: * @param isUpdateOnChange whether or not the starter should be updated
099: */
100: public SystemPropertiesPanel(Environment env, Editor editor,
101: boolean isUpdateOnChange) {
102: this .editor = editor;
103: this .isUpdateOnChange = isUpdateOnChange;
104: language = env.getLanguage();
105:
106: setLayout(new BorderLayout());
107:
108: table = new JTable(0, 2) {
109: public boolean isCellEditable(int row, int column) {
110: return false;
111: }
112: };
113:
114: ListSelectionModel selectionModel = table.getSelectionModel();
115: selectionModel
116: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
117: selectionModel.addListSelectionListener(this );
118:
119: JScrollPane tablePane = new JScrollPane(table) {
120: public Insets getInsets() {
121: return new Insets(10, 10, 10, 10);
122: }
123: };
124:
125: tablePane.setPreferredSize(new Dimension(400, 300));
126: tableModel = (DefaultTableModel) table.getModel();
127:
128: Object[] columnNames = new Object[2];
129: columnNames[0] = language
130: .getString("Starters.Starter.System.Property");
131: columnNames[1] = language
132: .getString("Starters.Starter.System.PropertyValue");
133: tableModel.setColumnIdentifiers(columnNames);
134:
135: if (editor.getElement() != null) {
136: Hashtable params = ((AppletStarter) editor.getElement())
137: .getSystemProperties();
138: Enumeration enumeration = params.keys();
139:
140: while (enumeration.hasMoreElements()) {
141: String[] rowData = new String[2];
142: rowData[0] = (String) enumeration.nextElement();
143: rowData[1] = (String) params.get(rowData[0]);
144: tableModel.addRow(rowData);
145: }
146: }
147:
148: JPanel buttonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
149: newButton = new JButton(language.getString("General.New"));
150: newButton.addActionListener(this );
151: buttonPane.add(newButton);
152: editButton = new JButton(language.getString("General.Edit"));
153: editButton.addActionListener(this );
154: editButton.setEnabled(false);
155: buttonPane.add(editButton);
156: deleteButton = new JButton(language.getString("General.Delete"));
157: deleteButton.addActionListener(this );
158: deleteButton.setEnabled(false);
159: buttonPane.add(deleteButton);
160:
161: add(tablePane, BorderLayout.CENTER);
162: add(buttonPane, BorderLayout.SOUTH);
163: }
164:
165: /**
166: * Called when a button has been pressed.
167: *
168: * @param event the action event representing the button action
169: */
170: public void actionPerformed(ActionEvent event) {
171: Window window = SwingUtilities.getWindowAncestor(editor
172: .getComponent());
173:
174: if (event.getSource() == newButton) {
175: SystemPropertiesEditorDialog dialog = null;
176:
177: if (window instanceof Frame) {
178: dialog = new SystemPropertiesEditorDialog(
179: (Frame) window);
180: } else if (window instanceof Dialog) {
181: dialog = new SystemPropertiesEditorDialog(
182: (Dialog) window);
183: }
184:
185: String key = dialog.getKey();
186: String value = dialog.getValue();
187:
188: if ((key != null) && (!key.equals(""))) {
189: Object[] rowData = new Object[2];
190: rowData[0] = key;
191: rowData[1] = value;
192: tableModel.addRow(rowData);
193:
194: if (isUpdateOnChange && (editor.getElement() != null)) {
195: ((Starter) editor.getElement()).setSystemProperty(
196: key, value);
197: }
198: }
199: } else if (event.getSource() == editButton) {
200: int selectedRow = table.getSelectedRow();
201:
202: if (selectedRow >= 0) {
203: String key = (String) tableModel.getValueAt(
204: selectedRow, 0);
205: String value = (String) tableModel.getValueAt(
206: selectedRow, 1);
207: SystemPropertiesEditorDialog dialog = null;
208:
209: if (window instanceof Frame) {
210: dialog = new SystemPropertiesEditorDialog(
211: (Frame) window, key, value);
212: } else if (window instanceof Dialog) {
213: dialog = new SystemPropertiesEditorDialog(
214: (Dialog) window, key, value);
215: }
216:
217: key = dialog.getKey();
218: value = dialog.getValue();
219:
220: if ((key != null) && (!key.equals(""))) {
221: tableModel.setValueAt(key, selectedRow, 0);
222: tableModel.setValueAt(value, selectedRow, 1);
223:
224: if (isUpdateOnChange
225: && (editor.getElement() != null)) {
226: ((Starter) editor.getElement())
227: .setSystemProperty(key, value);
228: }
229: }
230: }
231: } else if (event.getSource() == deleteButton) {
232: int selectedRow = table.getSelectedRow();
233:
234: if (selectedRow >= 0) {
235: String key = (String) tableModel.getValueAt(
236: selectedRow, 0);
237: tableModel.removeRow(selectedRow);
238:
239: if (isUpdateOnChange && (editor.getElement() != null)) {
240: ((Starter) editor.getElement())
241: .removeSystemProperty(key);
242: }
243:
244: deleteButton.setEnabled(false);
245: editButton.setEnabled(false);
246: }
247: }
248: }
249:
250: public void addRow(String[] data) {
251: tableModel.addRow(data);
252: }
253:
254: /**
255: * Called when the list selection has changed.
256: *
257: * @param event DOCUMENT ME!
258: */
259: public void valueChanged(ListSelectionEvent event) {
260: editButton.setEnabled(true);
261: deleteButton.setEnabled(true);
262: }
263:
264: /**
265: * Clears the table.
266: */
267: private void clearTable() {
268: for (int i = tableModel.getRowCount() - 1; i >= 0; i--) {
269: tableModel.removeRow(i);
270: }
271: }
272:
273: /**
274: * Sets the data of the table
275: *
276: * @param data DOCUMENT ME!
277: */
278: public void setTableData(Hashtable data) {
279: clearTable();
280:
281: Enumeration enum2 = data.keys();
282:
283: while (enum2.hasMoreElements()) {
284: String[] rowData = new String[2];
285: rowData[0] = (String) enum2.nextElement();
286: rowData[1] = (String) data.get(rowData[0]);
287: addRow(rowData);
288: }
289: }
290:
291: //
292: // The cursor entry editor
293: //
294:
295: /**
296: * The editor dialog for cursor entries.
297: */
298: class SystemPropertiesEditorDialog extends JDialog implements
299: ActionListener {
300: //~ Instance fields ------------------------------------------------------------------------
301:
302: private String key;
303: private String value;
304: private JButton okButton;
305: private JButton cancelButton;
306: private JTextField valueField;
307: private JTextField nameField;
308:
309: //~ Constructors ---------------------------------------------------------------------------
310:
311: SystemPropertiesEditorDialog(Frame frame) {
312: this (frame, "", "");
313: }
314:
315: SystemPropertiesEditorDialog(Frame frame, String key,
316: String value) {
317: super (frame, true);
318:
319: Point frameLocation = frame.getLocationOnScreen();
320: create(key, value, frameLocation.x, frameLocation.y, frame
321: .getWidth(), frame.getHeight());
322: }
323:
324: SystemPropertiesEditorDialog(Dialog dialog) {
325: this (dialog, "", "");
326: }
327:
328: SystemPropertiesEditorDialog(Dialog dialog, String key,
329: String value) {
330: super (dialog, true);
331:
332: Point dialogLocation = dialog.getLocationOnScreen();
333: create(key, value, dialogLocation.x, dialogLocation.y,
334: dialog.getWidth(), dialog.getHeight());
335: }
336:
337: //~ Methods --------------------------------------------------------------------------------
338:
339: private void create(String key, String value, int frameX,
340: int frameY, int frameWidth, int frameHeight) {
341: this .key = key;
342: this .value = value;
343:
344: Container content = getContentPane();
345: content.setLayout(new GridBagLayout());
346:
347: GridBagConstraints c = new GridBagConstraints();
348: c.fill = GridBagConstraints.NONE;
349: c.anchor = GridBagConstraints.CENTER;
350: c.insets = new Insets(5, 5, 5, 5);
351:
352: // Row for the name
353: c.gridx = 0;
354: c.gridy = 0;
355: c.weightx = 30;
356: c.weighty = 20;
357:
358: JLabel nameLabel = new JLabel(language
359: .getString("Starters.Starter.System.Property"));
360: content.add(nameLabel, c);
361:
362: c.gridx = 1;
363: c.weightx = 60;
364: nameField = new JTextField(key, 20);
365: nameLabel.setLabelFor(nameField);
366: content.add(nameField, c);
367:
368: // Row for the value
369: c.gridx = 0;
370: c.gridy = 1;
371:
372: JLabel valueLabel = new JLabel(language
373: .getString("Starters.Starter.System.PropertyValue"));
374: content.add(valueLabel, c);
375:
376: c.gridx = 1;
377: valueField = new JTextField(value, 20);
378: valueLabel.setLabelFor(valueField);
379: content.add(valueField, c);
380:
381: // Row for the buttons
382: c.gridwidth = 2;
383: c.gridx = 0;
384: c.gridy = 2;
385: c.weighty = 40;
386:
387: JPanel buttonRow = new JPanel(new FlowLayout(
388: FlowLayout.CENTER));
389: okButton = new JButton(language.getString("General.Ok"));
390: okButton.addActionListener(this );
391: cancelButton = new JButton(language
392: .getString("General.Cancel"));
393: cancelButton.addActionListener(this );
394: buttonRow.add(okButton);
395: buttonRow.add(cancelButton);
396: content.add(buttonRow, c);
397:
398: pack();
399:
400: int x = frameX + ((frameWidth - getWidth()) / 2);
401: int y = frameY + ((frameHeight - getHeight()) / 2);
402: setLocation(x, y);
403:
404: setVisible(true);
405: }
406:
407: public void actionPerformed(ActionEvent event) {
408: if (event.getSource() == okButton) {
409: this .key = nameField.getText();
410: this .value = valueField.getText();
411: dispose();
412: } else {
413: this .key = null;
414: this .value = null;
415: dispose();
416: }
417: }
418:
419: /**
420: * Returns the key, or <code>null</code> when the user has canceled.
421: *
422: * @return DOCUMENT ME!
423: */
424: public String getKey() {
425: return key;
426: }
427:
428: /**
429: * Returns the value, or <code>null</code> when the user has canceled.
430: *
431: * @return DOCUMENT ME!
432: */
433: public String getValue() {
434: return value;
435: }
436: }
437: }
|