001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (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
016 package org.qedeq.gui.se.control;
017
018 import java.awt.event.ActionEvent;
019 import java.io.IOException;
020
021 import javax.swing.AbstractAction;
022 import javax.swing.JComboBox;
023 import javax.swing.JOptionPane;
024
025 import org.qedeq.base.trace.Trace;
026 import org.qedeq.kernel.bo.KernelContext;
027 import org.qedeq.kernel.se.common.ModuleAddress;
028
029 /**
030 * Load QEDEQ module file.
031 *
032 * @version $Revision: 1.7 $
033 * @author Michael Meyling
034 */
035 class AddAction extends AbstractAction {
036
037 /** This class. */
038 private static final Class CLASS = AddAction.class;
039
040 /** Reference to controller. */
041 private final QedeqController controller;
042
043 /**
044 * Constructor.
045 *
046 * @param controller Reference to controller.
047 */
048 AddAction(final QedeqController controller) {
049 this.controller = controller;
050 }
051
052 public void actionPerformed(final ActionEvent e) {
053 Trace.trace(CLASS, this, "actionPerformed", e);
054
055 // Messages
056 final Object[] message = new Object[2];
057 message[0] = "Please choose or enter a module URL:";
058
059 final JComboBox cb = new JComboBox();
060 for (int i = 0; i < controller.getModuleHistory().size()
061 && i < QedeqController.MAXIMUM_MODULE_HISTORY; i++) {
062 cb.addItem(controller.getModuleHistory().get(i));
063 }
064 cb.setEditable(true);
065 message[1] = cb;
066
067 // Options
068 final String[] options = {"Ok", "Cancel"};
069 final int result = JOptionPane.showOptionDialog(
070 controller.getMainFrame(), // the parent that the dialog blocks
071 message, // the dialog message array
072 "Loading of a module", // the title of the dialog window
073 JOptionPane.CANCEL_OPTION, // option type
074 JOptionPane.QUESTION_MESSAGE, // message type
075 null, // optional icon, use null to use the default icon
076 options, // options string array, will be made into buttons
077 options[0] // option that should be made into a default button
078 );
079 final ModuleAddress address;
080 switch(result) {
081 case 0: // ok
082 try {
083 address = KernelContext.getInstance().getModuleAddress(
084 (String) cb.getSelectedItem());
085 controller.addToModuleHistory(address.toString());
086 } catch (IOException ie) {
087 Trace.trace(CLASS, this, "actionPerformed", "no correct URL", ie);
088 JOptionPane.showMessageDialog(
089 controller.getMainFrame(), // the parent that the dialog blocks
090 "this is no valid URL: " + cb.getSelectedItem()
091 + "\n" + ie.getMessage(), // message
092 "Error", // title
093 JOptionPane.ERROR_MESSAGE // message type
094 );
095 return;
096 }
097 break;
098 case 1: // cancel, fall through default
099 default:
100 return;
101 }
102 final Thread thread = new Thread() {
103 public void run() {
104 KernelContext.getInstance().loadModule(address);
105 }
106 };
107 thread.setDaemon(true);
108 thread.start();
109 }
110
111 }
|