0001: /*
0002: * Copyright 2001-2006 C:1 Financial Services GmbH
0003: *
0004: * This software is free software; you can redistribute it and/or
0005: * modify it under the terms of the GNU Lesser General Public
0006: * License Version 2.1, as published by the Free Software Foundation.
0007: *
0008: * This software is distributed in the hope that it will be useful,
0009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0011: * Lesser General Public License for more details.
0012: *
0013: * You should have received a copy of the GNU Lesser General Public
0014: * License along with this library; if not, write to the Free Software
0015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
0016: */
0017:
0018: package de.finix.contelligent.client.gui.module;
0019:
0020: import java.awt.BorderLayout;
0021: import java.awt.Component;
0022: import java.awt.Dimension;
0023: import java.awt.GridLayout;
0024: import java.awt.Rectangle;
0025: import java.awt.Toolkit;
0026: import java.awt.event.ActionEvent;
0027: import java.awt.event.ActionListener;
0028: import java.awt.event.ItemEvent;
0029: import java.awt.event.ItemListener;
0030: import java.io.IOException;
0031: import java.io.StringReader;
0032: import java.net.MalformedURLException;
0033: import java.net.URL;
0034: import java.util.logging.Level;
0035: import java.util.logging.Logger;
0036:
0037: import javax.jnlp.BasicService;
0038: import javax.jnlp.ServiceManager;
0039: import javax.jnlp.UnavailableServiceException;
0040: import javax.swing.AbstractAction;
0041: import javax.swing.AbstractButton;
0042: import javax.swing.Action;
0043: import javax.swing.ButtonGroup;
0044: import javax.swing.DefaultCellEditor;
0045: import javax.swing.JButton;
0046: import javax.swing.JCheckBox;
0047: import javax.swing.JComboBox;
0048: import javax.swing.JComponent;
0049: import javax.swing.JLabel;
0050: import javax.swing.JOptionPane;
0051: import javax.swing.JPanel;
0052: import javax.swing.JRadioButton;
0053: import javax.swing.JScrollPane;
0054: import javax.swing.JTable;
0055: import javax.swing.JTextArea;
0056: import javax.swing.JTextField;
0057: import javax.swing.border.TitledBorder;
0058: import javax.swing.table.DefaultTableModel;
0059: import javax.swing.table.TableCellRenderer;
0060: import javax.swing.table.TableColumn;
0061: import javax.swing.text.JTextComponent;
0062: import javax.xml.parsers.DocumentBuilder;
0063: import javax.xml.parsers.DocumentBuilderFactory;
0064: import javax.xml.parsers.ParserConfigurationException;
0065:
0066: import org.w3c.dom.Document;
0067: import org.w3c.dom.Element;
0068: import org.w3c.dom.Node;
0069: import org.w3c.dom.NodeList;
0070: import org.xml.sax.InputSource;
0071: import org.xml.sax.SAXException;
0072: import org.xml.sax.SAXParseException;
0073: import org.xml.sax.helpers.AttributesImpl;
0074:
0075: import de.finix.contelligent.client.ContelligentClient;
0076: import de.finix.contelligent.client.base.ComponentFactory;
0077: import de.finix.contelligent.client.base.ComponentNotFoundException;
0078: import de.finix.contelligent.client.base.ContelligentComponent;
0079: import de.finix.contelligent.client.base.resource.ContelligentTextResource;
0080: import de.finix.contelligent.client.event.ContelligentEvent;
0081: import de.finix.contelligent.client.gui.AbstractComponentResourceEditor;
0082: import de.finix.contelligent.client.gui.ContelligentAction;
0083: import de.finix.contelligent.client.gui.GUIDescription;
0084: import de.finix.contelligent.client.gui.GUIModification;
0085: import de.finix.contelligent.client.gui.GUIRegistry;
0086: import de.finix.contelligent.client.gui.ViewRegistry;
0087: import de.finix.contelligent.client.i18n.Resources;
0088: import de.finix.contelligent.client.modules.ModuleRegistry;
0089: import de.finix.contelligent.client.util.ButtonComposer;
0090: import de.finix.contelligent.client.util.ExceptionDialog;
0091: import de.finix.contelligent.client.util.TableLayout;
0092: import de.finix.contelligent.client.util.dnd.ComponentDropEvent;
0093: import de.finix.contelligent.client.util.dnd.ComponentDropListener;
0094: import de.finix.contelligent.client.util.dnd.JPanelDND;
0095: import de.finix.contelligent.client.util.list.NameValueComboBox;
0096: import de.finix.contelligent.client.util.list.NameValueListModel;
0097: import de.finix.contelligent.client.util.xml.XMLUtil;
0098: import de.zeigermann.xml.XMLEncode;
0099: import de.zeigermann.xml.XMLStringWriter;
0100: import de.zeigermann.xml.XMLWriter;
0101: import de.zeigermann.xml.simpleImporter.ConversionHelpers;
0102: import de.zeigermann.xml.simpleImporter.DefaultSimpleImportHandler;
0103: import de.zeigermann.xml.simpleImporter.SimpleImporter;
0104: import de.zeigermann.xml.simpleImporter.SimplePath;
0105:
0106: public class ModuleEditor extends AbstractComponentResourceEditor
0107: implements ItemListener {
0108:
0109: private static Logger logger = Logger.getLogger(ModuleEditor.class
0110: .getName());
0111:
0112: private String displayName = "";
0113:
0114: private String moduleImpl = "";
0115:
0116: private String category = "";
0117:
0118: private String tooltip = "";
0119:
0120: private String moduleConfiguration = "";
0121:
0122: private boolean createMenuEntry;
0123:
0124: private boolean createToolbarEntry;
0125:
0126: private boolean autoOpenModule;
0127:
0128: private boolean createTab;
0129:
0130: private NameValueComboBox categorySelection;
0131:
0132: private JComboBox moduleType;
0133:
0134: private JPanel locationPanel, configurePanel;
0135:
0136: private JRadioButton windowButton, tabButton, environmentButton;
0137:
0138: private JCheckBox toolbarButton;
0139:
0140: private JCheckBox autoOpenButton;
0141:
0142: private JTextComponent configuration = new JTextField();
0143:
0144: private JTextField moduleName = new JTextField();
0145:
0146: private JComponent executable = new JLabel();
0147:
0148: private boolean initialized = false;
0149:
0150: private JPanelDND path;
0151:
0152: private JLabel pathLabel = new JLabel("[ - ]");
0153:
0154: private NameValueComboBox rootGUI;
0155:
0156: private JTextField allowedTypes = new JTextField();
0157:
0158: private JTextField favoriteTypes = new JTextField();
0159:
0160: private JCheckBox allowEdit = new JCheckBox();
0161:
0162: private JCheckBox allowCommit = new JCheckBox();
0163:
0164: private JCheckBox allowBlueprintDef = new JCheckBox();
0165:
0166: private DefaultTableModel guiMappingTableModel;
0167:
0168: private JTable guiMappingTable = new JTable();
0169:
0170: private Action addMappingAction;
0171:
0172: private static DocumentBuilder builder = null;
0173:
0174: private static DocumentBuilderFactory factory = DocumentBuilderFactory
0175: .newInstance();
0176:
0177: private final static int MODULE_MODE = 0;
0178:
0179: private final static int VIEW_MODE = 1;
0180:
0181: private final static int LAUNCH_MODE = 2;
0182:
0183: private final static int URL_MODE = 3;
0184:
0185: private int mode = URL_MODE;
0186:
0187: public void init() {
0188: setResourceComponent(this );
0189: JPanel staticPanel = new JPanel(new BorderLayout());
0190: staticPanel.setOpaque(false);
0191:
0192: JPanel moduleSelectionPanel = new JPanel(new TableLayout(
0193: new double[][] {
0194: { TableLayout.PREFERRED, TableLayout.FILL },
0195: { 25, 25, 25 } }));
0196: moduleSelectionPanel.setOpaque(false);
0197: moduleSelectionPanel.setBorder(new TitledBorder(Resources
0198: .getLocalString("select_name_type_module_title")));
0199:
0200: JLabel nameLabel = new JLabel(Resources
0201: .getLocalString("name_module_request")
0202: + ":");
0203: moduleSelectionPanel.add(nameLabel, "0,0");
0204: moduleSelectionPanel.add(moduleName, "1,0");
0205:
0206: JLabel categoryLabel = new JLabel(Resources
0207: .getLocalString("module_category_request")
0208: + ":");
0209: categorySelection = new NameValueComboBox(
0210: NameValueListModel
0211: .buildLocaleNameValuePairs(ContelligentAction.actionTypes));
0212: categorySelection.setSelectedItemByValue("file");
0213:
0214: moduleSelectionPanel.add(categoryLabel, "0,1");
0215: moduleSelectionPanel.add(categorySelection, "1,1");
0216:
0217: JLabel typeLabel = new JLabel(Resources
0218: .getLocalString("module_type_request")
0219: + ":");
0220: moduleType = new JComboBox(new String[] {
0221: Resources.getLocalString("module_title"),
0222: Resources.getLocalString("component_view"),
0223: Resources.getLocalString("launch_app"),
0224: Resources.getLocalString("launch_browser") });
0225: moduleSelectionPanel.add(typeLabel, "0,2");
0226: moduleSelectionPanel.add(moduleType, "1,2");
0227: staticPanel.add(moduleSelectionPanel, BorderLayout.NORTH);
0228:
0229: path = new JPanelDND(new BorderLayout(), getView()
0230: .getEnvironment());
0231: path.setOpaque(false);
0232: path.add(pathLabel, BorderLayout.CENTER);
0233: path.addComponentDropListener(new ComponentDropListener() {
0234: public void componentDropped(ComponentDropEvent e) {
0235: ContelligentComponent draggedComponent = e
0236: .getDraggedComponent();
0237: path.setComponent(draggedComponent);
0238: pathLabel.setText(draggedComponent.getPath() + "/");
0239: pathLabel.setIcon(draggedComponent.getSmallIcon());
0240: path.validate();
0241: path.repaint();
0242: }
0243: });
0244:
0245: JPanel mainPanel = new JPanel(new BorderLayout());
0246: mainPanel.setOpaque(false);
0247: JScrollPane mainScrollPane = new JScrollPane(mainPanel);
0248: mainScrollPane.getViewport().setOpaque(false);
0249: mainScrollPane.setOpaque(false);
0250:
0251: add(mainScrollPane, BorderLayout.CENTER);
0252:
0253: mainPanel.add(staticPanel, BorderLayout.NORTH);
0254:
0255: configurePanel = new JPanel(new BorderLayout());
0256: configurePanel.setOpaque(false);
0257: configurePanel.setBorder(new TitledBorder(Resources
0258: .getLocalString("module_configuration_title")));
0259: mainPanel.add(configurePanel, BorderLayout.SOUTH);
0260:
0261: locationPanel = new JPanel(new GridLayout(0, 1));
0262: locationPanel.setOpaque(false);
0263: locationPanel.setBorder(new TitledBorder(Resources
0264: .getLocalString("module_location_title")));
0265: JLabel locationTitle = new JLabel(Resources
0266: .getLocalString("module_configuration_request")
0267: + ":");
0268: ButtonGroup buttonGroup = new ButtonGroup();
0269: windowButton = new JRadioButton(Resources
0270: .getLocalString("module_frame_title"));
0271: windowButton.setOpaque(false);
0272: windowButton.addActionListener(new ActionListener() {
0273: public void actionPerformed(ActionEvent e) {
0274: if (windowButton.isSelected()) {
0275: toolbarButton.setEnabled(true);
0276: autoOpenButton.setEnabled(true);
0277: }
0278: toolbarButton.validate();
0279: autoOpenButton.validate();
0280: }
0281: });
0282: tabButton = new JRadioButton(Resources
0283: .getLocalString("module_tab_title"));
0284: tabButton.setOpaque(false);
0285: tabButton.addActionListener(new ActionListener() {
0286: public void actionPerformed(ActionEvent e) {
0287: if (tabButton.isSelected()) {
0288: toolbarButton.setSelected(false);
0289: toolbarButton.setEnabled(false);
0290: autoOpenButton.setSelected(false);
0291: autoOpenButton.setEnabled(false);
0292: }
0293: toolbarButton.validate();
0294: autoOpenButton.validate();
0295: }
0296: });
0297: environmentButton = new JRadioButton(Resources
0298: .getLocalString("module_environment_title"));
0299: environmentButton.setOpaque(false);
0300: environmentButton.addActionListener(new ActionListener() {
0301: public void actionPerformed(ActionEvent e) {
0302: if (environmentButton.isSelected()) {
0303: toolbarButton.setSelected(false);
0304: toolbarButton.setEnabled(false);
0305: autoOpenButton.setSelected(false);
0306: autoOpenButton.setEnabled(false);
0307: }
0308: toolbarButton.validate();
0309: autoOpenButton.validate();
0310: }
0311: });
0312: windowButton.setSelected(true);
0313: createMenuEntry = true;
0314:
0315: toolbarButton = new JCheckBox(Resources
0316: .getLocalString("module_toolbar_title"));
0317: toolbarButton.setOpaque(false);
0318: autoOpenButton = new JCheckBox(Resources
0319: .getLocalString("module_autoopen_title"));
0320: autoOpenButton.setOpaque(false);
0321: buttonGroup.add(windowButton);
0322: buttonGroup.add(tabButton);
0323: buttonGroup.add(environmentButton);
0324: locationPanel.add(windowButton);
0325: locationPanel.add(toolbarButton);
0326: locationPanel.add(autoOpenButton);
0327: locationPanel.add(tabButton);
0328: locationPanel.add(environmentButton);
0329: staticPanel.add(locationPanel, BorderLayout.SOUTH);
0330:
0331: addMappingAction = new EditMappingAction(-1); // -1 indicates addition
0332: // of new row
0333: addMappingAction.putValue(Action.NAME, Resources
0334: .getLocalString("add_mapping_action"));
0335:
0336: initialized = true;
0337: update();
0338: }
0339:
0340: public void itemStateChanged(ItemEvent e) {
0341: updateComponent();
0342: mode = moduleType.getSelectedIndex();
0343: moduleConfiguration = "";
0344: switch (mode) {
0345: case MODULE_MODE:
0346: moduleImpl = ModuleRegistry.getInstance().getDefaultImpl();
0347: break;
0348: case VIEW_MODE:
0349: moduleImpl = ViewRegistry.getInstance().getDefaultImpl();
0350: moduleConfiguration = "<view path=\""
0351: + getComponent().getPath()
0352: + "\" root=\"/\" rootGUI=\""
0353: + GUIRegistry.getInstance().getDefaultViewGuiImpl()
0354: + "\"/>\n";
0355: break;
0356: case LAUNCH_MODE:
0357: moduleImpl = Resources.getLocalString("enter_exec_request");
0358: break;
0359: case URL_MODE:
0360: moduleImpl = "";
0361: break;
0362: default:
0363: logger
0364: .log(Level.SEVERE,
0365: "Strange combo box entry found in module type selection box!");
0366: }
0367: logger.log(Level.FINE, "Action event on combo box performed: "
0368: + e);
0369: update(mode);
0370: }
0371:
0372: private void composeEditor(int mode) {
0373: configurePanel.removeAll();
0374:
0375: if (mode == MODULE_MODE) {
0376: JPanel executablePanel = new JPanel(
0377: new TableLayout(
0378: new double[][] {
0379: { TableLayout.PREFERRED,
0380: TableLayout.FILL }, { 25 } }));
0381: executablePanel.setOpaque(false);
0382: JLabel executableTitle = new JLabel(Resources
0383: .getLocalString("module_implementation_title")
0384: + ": ");
0385: executablePanel.add(executableTitle, "0,0");
0386:
0387: // XXX this is ugly: mapping name to name itself. Should be no
0388: // mapping at all,
0389: // but this would lead to changes in obsucre parts in this file
0390: // resulting from previous
0391: // internationalized versions
0392: executable = new NameValueComboBox(NameValueListModel
0393: .buildNameValuePairs(
0394: ModuleRegistry.getInstance()
0395: .getSymbolicNames().toArray(),
0396: (String[]) ModuleRegistry.getInstance()
0397: .getSymbolicNames().toArray(
0398: new String[0])).sort(
0399: NameValueListModel.SORT_DESCENDING));
0400:
0401: executablePanel.add(executable, "1,0");
0402: JPanel configPanel = new JPanel(new BorderLayout());
0403: configPanel.setOpaque(false);
0404: configurePanel.add(executablePanel, BorderLayout.NORTH);
0405: JLabel configTitle = new JLabel(
0406: Resources
0407: .getLocalString("enter_module_configuration_request")
0408: + ":");
0409: configPanel.add(configTitle, BorderLayout.NORTH);
0410: configuration = new JTextArea();
0411: JScrollPane configurationScroller = new JScrollPane(
0412: configuration);
0413: configurationScroller.getViewport().setOpaque(false);
0414: configurationScroller.setOpaque(false);
0415: configPanel.add(configurationScroller, BorderLayout.CENTER);
0416: configurePanel.add(configPanel, BorderLayout.CENTER);
0417: } else if (mode == VIEW_MODE) {
0418: JPanel viewPanel = new JPanel(new BorderLayout());
0419: viewPanel.setOpaque(false);
0420: JPanel viewParameter = new JPanel(
0421: new TableLayout(
0422: new double[][] {
0423: { TableLayout.PREFERRED,
0424: TableLayout.FILL },
0425: { 25, 25, 25, 25, 25, 25, 25, 25 } }));
0426: viewParameter.setOpaque(false);
0427: JLabel executableTitle = new JLabel(Resources
0428: .getLocalString("module_view_request")
0429: + ": ");
0430: viewParameter.add(executableTitle, "0,0");
0431:
0432: // XXX this is ugly: mapping name to name itself. Should be no
0433: // mapping at all,
0434: // but this would lead to changes in obsucre parts in this file
0435: // resulting from previous
0436: // internationalized versions
0437: executable = new NameValueComboBox(NameValueListModel
0438: .buildNameValuePairs(
0439: ViewRegistry.getInstance()
0440: .getVisibleSymbolicNames()
0441: .toArray(),
0442: (String[]) ViewRegistry.getInstance()
0443: .getVisibleSymbolicNames().toArray(
0444: new String[0])).sort(
0445: NameValueListModel.SORT_DESCENDING));
0446:
0447: viewParameter.add(executable, "1,0");
0448:
0449: JLabel pathTitle = new JLabel(Resources
0450: .getLocalString("module_view_root_request")
0451: + ":");
0452: viewParameter.add(pathTitle, "0,1");
0453: viewParameter.add(path, "1,1");
0454: JLabel rootGUILabel = new JLabel(Resources
0455: .getLocalString("module_view_gui_request")
0456: + ":");
0457: viewParameter.add(rootGUILabel, "0,2");
0458:
0459: // XXX this is ugly: mapping name to name itself. Should be no
0460: // mapping at all,
0461: // but this would lead to changes in obsucre parts in this file
0462: // resulting from previous
0463: // internationalized versions
0464: rootGUI = new NameValueComboBox(NameValueListModel
0465: .buildNameValuePairs(
0466: GUIRegistry.getInstance()
0467: .getViewGuiSymbolicNames()
0468: .toArray(),
0469: (String[]) GUIRegistry.getInstance()
0470: .getViewGuiSymbolicNames().toArray(
0471: new String[0])).sort(
0472: NameValueListModel.SORT_DESCENDING));
0473:
0474: viewParameter.add(rootGUI, "1,2");
0475:
0476: JLabel allowedTypesLabel = new JLabel(Resources
0477: .getLocalString("module_view_allowed_types")
0478: + ":");
0479: viewParameter.add(allowedTypesLabel, "0,3");
0480: viewParameter.add(allowedTypes, "1,3");
0481:
0482: JLabel favoriteTypesLabel = new JLabel(Resources
0483: .getLocalString("module_view_favorite_types")
0484: + ":");
0485: viewParameter.add(favoriteTypesLabel, "0,4");
0486: viewParameter.add(favoriteTypes, "1,4");
0487:
0488: JLabel allowEditLabel = new JLabel(
0489: Resources
0490: .getLocalString("module_view_deny_edit_outside_workflow")
0491: + ":");
0492: viewParameter.add(allowEditLabel, "0,5");
0493: allowEdit.setOpaque(false);
0494: viewParameter.add(allowEdit, "1,5");
0495:
0496: JLabel allowCommitLabel = new JLabel(Resources
0497: .getLocalString("module_view_allow_commit")
0498: + ":");
0499: viewParameter.add(allowCommitLabel, "0,6");
0500: allowCommit.setOpaque(false);
0501: viewParameter.add(allowCommit, "1,6");
0502:
0503: JLabel allowBlueprintLabel = new JLabel(
0504: Resources
0505: .getLocalString("module_view_deny_blueprint_definition")
0506: + ":");
0507: viewParameter.add(allowBlueprintLabel, "0,7");
0508: allowBlueprintDef.setOpaque(false);
0509: viewParameter.add(allowBlueprintDef, "1,7");
0510:
0511: viewPanel.add(viewParameter, BorderLayout.NORTH);
0512: JPanel mappingPanel = new JPanel(new BorderLayout());
0513: mappingPanel.setOpaque(false);
0514: JLabel guiMappingsLabel = new JLabel(Resources
0515: .getLocalString("module_view_mapping_request")
0516: + ":");
0517: mappingPanel.add(guiMappingsLabel, BorderLayout.NORTH);
0518: JButton jb = new JButton("test");
0519: jb.setAction(addMappingAction);
0520: mappingPanel.add(jb, BorderLayout.NORTH);
0521: JScrollPane tableScroller = new JScrollPane(guiMappingTable);
0522: tableScroller.getViewport().setOpaque(false);
0523: tableScroller.setOpaque(false);
0524: mappingPanel.add(tableScroller, BorderLayout.CENTER);
0525: viewPanel.add(mappingPanel, BorderLayout.CENTER);
0526: configurePanel.add(viewPanel, BorderLayout.CENTER);
0527: } else if (mode == LAUNCH_MODE) {
0528: JPanel configPanel = new JPanel(new GridLayout(0, 1));
0529: configPanel.setOpaque(false);
0530: JPanel executablePanel = new JPanel(new BorderLayout());
0531: executablePanel.setOpaque(false);
0532: JLabel executableTitle = new JLabel(Resources
0533: .getLocalString("module_exec_title")
0534: + ": ");
0535: executablePanel.add(executableTitle, BorderLayout.WEST);
0536: executable = new JTextField();
0537: executablePanel.add(executable, BorderLayout.CENTER);
0538: configPanel.add(executablePanel);
0539: JButton testButton = new JButton(Resources
0540: .getLocalString("test"));
0541: testButton.addActionListener(new ActionListener() {
0542: public void actionPerformed(ActionEvent e) {
0543: try {
0544: Runtime.getRuntime().exec(
0545: new String[] {
0546: ((JTextField) executable)
0547: .getText(),
0548: configuration.getText() });
0549: } catch (IOException ioe) {
0550: ExceptionDialog.show(ioe);
0551: }
0552: }
0553: });
0554: executablePanel.add(testButton, BorderLayout.EAST);
0555: JPanel argumentsPanel = new JPanel(new BorderLayout());
0556: argumentsPanel.setOpaque(false);
0557: JLabel argumentsTitle = new JLabel(Resources
0558: .getLocalString("optional_arguments")
0559: + ": ");
0560: configuration = new JTextField();
0561: argumentsPanel.add(argumentsTitle, BorderLayout.WEST);
0562: argumentsPanel.add(configuration, BorderLayout.CENTER);
0563: configPanel.add(argumentsPanel);
0564: configurePanel.add(configPanel, BorderLayout.NORTH);
0565: } else if (mode == URL_MODE) {
0566: JPanel configPanel = new JPanel(new BorderLayout());
0567: configPanel.setOpaque(false);
0568: JLabel configTitle = new JLabel(Resources
0569: .getLocalString("url")
0570: + ": ");
0571: configPanel.add(configTitle, BorderLayout.WEST);
0572: configuration = new JTextField();
0573: configPanel.add(configuration, BorderLayout.CENTER);
0574: JButton test = new JButton(Resources.getLocalString("test"));
0575: test.addActionListener(new ActionListener() {
0576: public void actionPerformed(ActionEvent e) {
0577: try {
0578: ((BasicService) ServiceManager
0579: .lookup("javax.jnlp.BasicService"))
0580: .showDocument(new URL(configuration
0581: .getText()));
0582: } catch (MalformedURLException mue) {
0583: ExceptionDialog.show(mue);
0584: } catch (UnavailableServiceException use) {
0585: ExceptionDialog.show(use);
0586: } // TRY
0587: }
0588: });
0589: configPanel.add(test, BorderLayout.EAST);
0590: configurePanel.add(configPanel, BorderLayout.NORTH);
0591: }
0592:
0593: validate();
0594: repaint();
0595: }
0596:
0597: private int detectMode() {
0598: // detect moduleType...
0599: if (ModuleRegistry.getInstance().isImplRegistered(moduleImpl)) {
0600: mode = MODULE_MODE;
0601: } else if (ViewRegistry.getInstance().isImplRegistered(
0602: moduleImpl)) {
0603: mode = VIEW_MODE;
0604: } else if (moduleImpl != null && moduleImpl.trim().length() > 0) {
0605: mode = LAUNCH_MODE;
0606: } else {
0607: mode = URL_MODE;
0608: }
0609: return mode;
0610: }
0611:
0612: protected void displayResource() {
0613: ContelligentTextResource resource = (ContelligentTextResource) getGUI()
0614: .getResource();
0615: if (resource != null) {
0616: ModuleHandler handler = new ModuleHandler();
0617: SimpleImporter importer = new SimpleImporter();
0618: importer.addSimpleImportHandler(handler);
0619: try {
0620: importer.parse(new InputSource(new StringReader(
0621: XMLEncode.xmlDecodeTextToCDATA(resource
0622: .getText()))));
0623: } catch (Exception e) {
0624: logger.log(Level.SEVERE,
0625: "Caught exception while parsing module", e);
0626: }
0627: }
0628: update(detectMode());
0629: }
0630:
0631: private void update(int mode) {
0632: composeEditor(mode);
0633:
0634: moduleName.setText(displayName);
0635: if (category != null) {
0636: categorySelection.setSelectedItemByValue(category);
0637: }
0638: windowButton.setSelected(createMenuEntry);
0639: tabButton.setSelected(createTab);
0640: toolbarButton.setEnabled(createMenuEntry && isEditable());
0641: toolbarButton.setSelected(createToolbarEntry);
0642: autoOpenButton.setEnabled(createMenuEntry && isEditable());
0643: autoOpenButton.setSelected(autoOpenModule);
0644: environmentButton.setSelected((!createMenuEntry)
0645: && (!createTab));
0646:
0647: // add widgets for editing a contelligent module/view
0648: if (mode == MODULE_MODE) {
0649: String symbolicName = ModuleRegistry.getInstance()
0650: .getSymbolicName(moduleImpl);
0651: ((NameValueComboBox) executable)
0652: .setSelectedItemByValue(symbolicName);
0653: configuration.setText(moduleConfiguration);
0654: } else if (mode == VIEW_MODE) {
0655: String symbolicName = ViewRegistry.getInstance()
0656: .getSymbolicName(moduleImpl);
0657: ((NameValueComboBox) executable)
0658: .setSelectedItemByValue(symbolicName);
0659: parseViewConfiguration();
0660: } else if (mode == LAUNCH_MODE) {
0661: ((JTextComponent) executable).setText(moduleImpl);
0662: configuration.setText(moduleConfiguration);
0663: } else if (mode == URL_MODE) {
0664: configuration.setText(moduleConfiguration);
0665: }
0666: setEditable(isEditable());
0667:
0668: moduleType.removeItemListener(this );
0669: moduleType.setSelectedIndex(mode);
0670: moduleType.addItemListener(this );
0671:
0672: validate();
0673: repaint();
0674: }
0675:
0676: private void parseViewConfiguration() {
0677: try {
0678: builder = factory.newDocumentBuilder();
0679: logger.log(Level.FINE, "Config: " + moduleConfiguration);
0680: Document document = builder.parse(new InputSource(
0681: new StringReader(moduleConfiguration)));
0682: Element viewElement = (Element) document
0683: .getElementsByTagName("view").item(0);
0684: String symbolicName = GUIRegistry.getInstance()
0685: .getSymbolicName(
0686: XMLUtil.getAttributeValue(viewElement,
0687: "rootGUI"));
0688: rootGUI.setSelectedItemByValue(symbolicName);
0689:
0690: String configuredPath = null;
0691: try {
0692: configuredPath = XMLUtil.getOptionalAttributeValue(
0693: viewElement, "root", "/");
0694: ContelligentComponent pathComponent = ComponentFactory
0695: .getInstance().getComponent(configuredPath);
0696: path.setComponent(pathComponent);
0697: pathLabel.setText(pathComponent.getPath() + "/");
0698: pathLabel.setIcon(pathComponent.getSmallIcon());
0699: } catch (ComponentNotFoundException e) {
0700: logger.log(Level.WARNING, "Path component ["
0701: + configuredPath + "]not found!", e);
0702: }
0703:
0704: // add guiMappings
0705: NodeList mappings = viewElement
0706: .getElementsByTagName("gui-mapping");
0707: Object[][] data = new Object[mappings.getLength()][6];
0708: for (int j = 0; j < mappings.getLength(); j++) {
0709: Node mappingElement = mappings.item(j);
0710: String type = XMLUtil.getAttributeValue(mappingElement,
0711: "type");
0712: String gui = XMLUtil.getAttributeValue(mappingElement,
0713: "gui");
0714: // FIXME: deprecated, use gui with descriptions in future
0715: GUIModification[] modifications = GUIDescription
0716: .convertEnumerationToModifications(gui);
0717: for (int i = 0; i < modifications.length; i++) {
0718: data[j][0] = type;
0719: data[j][1] = modifications[i].getCommandAsString();
0720: data[j][2] = GUIRegistry.getInstance()
0721: .getSymbolicName(
0722: modifications[i]
0723: .getGuiDescription()
0724: .getGuiImpl());
0725: String configuration = modifications[i]
0726: .getGuiDescription().getConfiguration();
0727: if (configuration == null) {
0728: configuration = "";
0729: }
0730: data[j][3] = configuration;
0731: data[j][4] = new DeleteMappingAction(j);
0732: data[j][5] = new EditMappingAction(j);
0733: }
0734: }
0735: if (mappings.getLength() == 0) {
0736: NodeList guiConfigurations = viewElement
0737: .getElementsByTagName("gui-configuration");
0738: data = new Object[guiConfigurations.getLength()][6];
0739: for (int j = 0; j < guiConfigurations.getLength(); j++) {
0740: Node configurationElement = guiConfigurations
0741: .item(j);
0742: String type = XMLUtil.getAttributeValue(
0743: configurationElement, "type");
0744: String command = XMLUtil.getAttributeValue(
0745: configurationElement, "command");
0746: String gui = XMLUtil.getAttributeValue(
0747: configurationElement, "gui");
0748: String configuration = XMLEncode
0749: .xmlDecodeTextToCDATA(XMLUtil
0750: .getContent(configurationElement));
0751: data[j][0] = type;
0752: data[j][1] = command;
0753: data[j][2] = gui;
0754: data[j][3] = configuration;
0755: data[j][4] = new DeleteMappingAction(j);
0756: data[j][5] = new EditMappingAction(j);
0757: }
0758: }
0759: guiMappingTableModel = new DefaultTableModel(
0760: data,
0761: new String[] {
0762: Resources.getLocalString("mapping_type"),
0763: Resources.getLocalString("mapping_command"),
0764: Resources.getLocalString("mapping_gui"),
0765: Resources
0766: .getLocalString("mapping_configuration"),
0767: "", "" });
0768: guiMappingTable.setModel(guiMappingTableModel);
0769: guiMappingTable.setRowHeight(28);
0770: TableColumn actionColumn = guiMappingTable.getColumnModel()
0771: .getColumn(4);
0772: actionColumn.setMaxWidth(28);
0773: actionColumn.setCellEditor(new ActionCellEditor());
0774: actionColumn.setCellRenderer(new ActionCellRenderer());
0775: TableColumn editColumn = guiMappingTable.getColumnModel()
0776: .getColumn(5);
0777: editColumn.setMaxWidth(28);
0778: editColumn.setCellEditor(new ActionCellEditor());
0779: editColumn.setCellRenderer(new ActionCellRenderer());
0780:
0781: Node allowedTypesElement = viewElement
0782: .getElementsByTagName("allowed-types").item(0);
0783: String allowedTypesString = "";
0784: if (allowedTypesElement != null) {
0785: allowedTypesString = XMLUtil.getAttributeValue(
0786: allowedTypesElement, "value");
0787: }
0788: allowedTypes.setText(allowedTypesString);
0789:
0790: Node favoriteTypesElement = viewElement
0791: .getElementsByTagName("favorite-types").item(0);
0792: String favoriteTypesString = "";
0793: if (favoriteTypesElement != null) {
0794: favoriteTypesString = XMLUtil.getAttributeValue(
0795: favoriteTypesElement, "value");
0796: }
0797: favoriteTypes.setText(favoriteTypesString);
0798:
0799: Node allowedEditElement = viewElement.getElementsByTagName(
0800: "allow-edit-in-workflow-only").item(0);
0801: String allowedEditString = "false";
0802: if (allowedEditElement != null) {
0803: allowedEditString = XMLUtil.getAttributeValue(
0804: allowedEditElement, "value");
0805: }
0806: allowEdit.setSelected("true".equals(allowedEditString));
0807:
0808: Node allowedCommitElement = viewElement
0809: .getElementsByTagName("allow-commit").item(0);
0810: String allowedCommitString = "true"; // Enabled for legacy
0811: // configurations
0812: if (allowedCommitElement != null) {
0813: allowedCommitString = XMLUtil.getAttributeValue(
0814: allowedCommitElement, "value");
0815: }
0816: allowCommit.setSelected("true".equals(allowedCommitString));
0817:
0818: Node denyBlueprintElement = viewElement
0819: .getElementsByTagName("forbid-blueprint-definition")
0820: .item(0);
0821: String denyBlueprintString = "false";
0822: if (denyBlueprintElement != null) {
0823: denyBlueprintString = XMLUtil.getAttributeValue(
0824: denyBlueprintElement, "value");
0825: }
0826: allowBlueprintDef.setSelected("true"
0827: .equals(denyBlueprintString));
0828:
0829: } catch (ParserConfigurationException pce) {
0830: // Parser with specified options can't be built
0831: logger.log(Level.SEVERE, "Could not initialize view!", pce);
0832: } catch (SAXParseException spe) {
0833: logger.log(Level.SEVERE, "Could not initialize view!", spe);
0834: } catch (SAXException sxe) {
0835: logger.log(Level.SEVERE, "Could not initialize view!", sxe);
0836: } catch (IOException ioe) {
0837: logger.log(Level.SEVERE, "Could not initialize view!", ioe);
0838: }
0839: }
0840:
0841: protected void updateComponent() {
0842: logger.log(Level.FINE, "Update component!");
0843:
0844: XMLStringWriter xmlWriter = XMLStringWriter.create();
0845: XMLStringWriter configurationWriter = XMLStringWriter.create();
0846:
0847: displayName = moduleName.getText();
0848: category = (String) categorySelection.getSelectedItemValue();
0849: createMenuEntry = windowButton.isSelected();
0850: createTab = tabButton.isSelected();
0851: createToolbarEntry = toolbarButton.isSelected();
0852: autoOpenModule = autoOpenButton.isSelected();
0853:
0854: if (mode == MODULE_MODE) {
0855: moduleImpl = ModuleRegistry.getInstance().getImpl(
0856: (String) ((NameValueComboBox) executable)
0857: .getSelectedItemValue());
0858: configurationWriter.writePCData(configuration.getText());
0859: } else if (mode == VIEW_MODE) {
0860: moduleImpl = ViewRegistry.getInstance().getImpl(
0861: (String) ((NameValueComboBox) executable)
0862: .getSelectedItemValue());
0863: } else if (mode == LAUNCH_MODE) {
0864: if (executable instanceof JTextField) {
0865: moduleImpl = ((JTextField) executable).getText();
0866: configurationWriter
0867: .writePCData(configuration.getText());
0868: }
0869: } else if (mode == URL_MODE) {
0870: moduleImpl = "";
0871: configurationWriter.writePCData(configuration.getText());
0872: }
0873: if (mode == VIEW_MODE) {
0874: String viewRoot = "";
0875: if (path.getComponent() != null) {
0876: viewRoot = path.getComponent().getPath();
0877: }
0878: String rootGui;
0879: if (rootGUI == null) {
0880: rootGui = GUIRegistry.getInstance().getImpl("Default");
0881: } else {
0882: rootGui = GUIRegistry.getInstance().getImpl(
0883: (String) rootGUI.getSelectedItemValue());
0884: }
0885: configurationWriter.writeStartTag(XMLWriter.createStartTag(
0886: "view", new String[] { "root", "rootGUI" },
0887: new String[] { viewRoot, rootGui }));
0888: // add guiMappings
0889: for (int i = 0; i < guiMappingTableModel.getRowCount(); i++) {
0890: String type = (String) guiMappingTableModel.getValueAt(
0891: i, 0);
0892: String command = (String) guiMappingTableModel
0893: .getValueAt(i, 1);
0894: String gui = (String) guiMappingTableModel.getValueAt(
0895: i, 2);
0896: String description = (String) guiMappingTableModel
0897: .getValueAt(i, 3);
0898: configurationWriter.writeElementWithPCData(
0899: XMLWriter
0900: .createStartTag("gui-configuration",
0901: new String[] { "type",
0902: "command", "gui" },
0903: new String[] { type, command,
0904: gui }), description,
0905: XMLWriter.createEndTag("gui-configuration"));
0906: }
0907: // add allowed types
0908: String allowedTypesString = allowedTypes.getText();
0909: configurationWriter.writeEmptyElement(XMLWriter
0910: .createEmptyTag("allowed-types", "value",
0911: allowedTypesString));
0912: // add favorite types
0913: String favoriteTypesString = favoriteTypes.getText();
0914: configurationWriter.writeEmptyElement(XMLWriter
0915: .createEmptyTag("favorite-types", "value",
0916: favoriteTypesString));
0917: // add edit in active workflow only
0918: String editInActiveWorkflowOnly = Boolean
0919: .toString(allowEdit.isSelected());
0920: configurationWriter.writeEmptyElement(XMLWriter
0921: .createEmptyTag("allow-edit-in-workflow-only",
0922: "value", editInActiveWorkflowOnly));
0923: // add commit allowed
0924: String commitAllowed = Boolean.toString(allowCommit
0925: .isSelected());
0926: configurationWriter.writeEmptyElement(XMLWriter
0927: .createEmptyTag("allow-commit", "value",
0928: commitAllowed));
0929: // add deny blueprint definiton
0930: String denyBlueprintDefintion = Boolean
0931: .toString(allowBlueprintDef.isSelected());
0932: configurationWriter.writeEmptyElement(XMLWriter
0933: .createEmptyTag("forbid-blueprint-definition",
0934: "value", denyBlueprintDefintion));
0935: configurationWriter.writeEndTag(XMLWriter
0936: .createEndTag("view"));
0937: }
0938: xmlWriter.writeStartTag(XMLWriter.createStartTag(
0939: "client-module", new String[] { "name", "moduleImpl",
0940: "category", "tooltip", "createMenuEntry",
0941: "createToolbarEntry", "autoOpenModule",
0942: "createTab" }, new String[] { displayName,
0943: moduleImpl, category, tooltip,
0944: Boolean.valueOf(createMenuEntry).toString(),
0945: Boolean.valueOf(createToolbarEntry).toString(),
0946: Boolean.valueOf(autoOpenModule).toString(),
0947: Boolean.valueOf(createTab).toString() }));
0948: xmlWriter.writeCData(configurationWriter.toString());
0949: xmlWriter.writeEndTag(XMLWriter.createEndTag("client-module"));
0950: ContelligentTextResource resource = (ContelligentTextResource) getGUI()
0951: .getResource();
0952: if (resource != null && !getGUI().isResourceInherited()) {
0953: resource.setText(xmlWriter.toString());
0954: if (resource.isModified()) {
0955: getComponent().setResourceModified(true);
0956: }
0957: } else {
0958: resource = new ContelligentTextResource(getGUI()
0959: .getResourceCategoryMap(), xmlWriter.toString());
0960: resource.setModified(true);
0961: getGUI().setResource(resource);
0962: getComponent().setResourceModified(true);
0963: }
0964: }
0965:
0966: public void setEditable(boolean editable) {
0967: super .setEditable(editable);
0968: if (isInitialized()) {
0969: moduleType.setEnabled(editable);
0970: categorySelection.setEnabled(editable);
0971: windowButton.setEnabled(editable);
0972: tabButton.setEnabled(editable);
0973: environmentButton.setEnabled(editable);
0974: toolbarButton.setEnabled(createMenuEntry && isEditable());
0975: autoOpenButton.setEnabled(createMenuEntry && isEditable());
0976: configuration.setEditable(editable);
0977: moduleName.setEditable(editable);
0978: executable.setEnabled(editable);
0979: path.setDropEnabled(editable);
0980: addMappingAction.setEnabled(editable);
0981: if (guiMappingTable != null) {
0982: guiMappingTable.setEnabled(editable);
0983: }
0984: if (rootGUI != null) {
0985: rootGUI.setEnabled(editable);
0986: }
0987: favoriteTypes.setEditable(editable);
0988: allowedTypes.setEditable(editable);
0989: allowEdit.setEnabled(editable);
0990: allowCommit.setEnabled(editable);
0991: allowBlueprintDef.setEnabled(editable);
0992:
0993: }
0994: }
0995:
0996: private boolean isInitialized() {
0997: return initialized;
0998: }
0999:
1000: public void rollback() {
1001: // it is better to make updates in render mode
1002: setEditable(false);
1003: super .rollback();
1004: }
1005:
1006: public Action[] getActions() {
1007: return new Action[] { addMappingAction };
1008: }
1009:
1010: protected void componentChanged(ContelligentEvent event) {
1011: update();
1012: }
1013:
1014: protected void childComponentAdded(ContelligentEvent event) {
1015: }
1016:
1017: protected void childComponentRemoved(ContelligentEvent event) {
1018: }
1019:
1020: protected void childComponentChanged(ContelligentEvent event) {
1021: }
1022:
1023: protected void descendentComponentChanged(ContelligentEvent event) {
1024: }
1025:
1026: final static class ActionCellEditor extends DefaultCellEditor {
1027: ContelligentAction action = null;
1028:
1029: AbstractButton button;
1030:
1031: public ActionCellEditor() {
1032: super (new JCheckBox());
1033: }
1034:
1035: public Object getCellEditorValue() {
1036: return action;
1037: }
1038:
1039: public Component getTableCellEditorComponent(JTable table,
1040: Object value, boolean isSelected, int row, int column) {
1041: action = (ContelligentAction) value;
1042: return ButtonComposer.createButton(action, false, null,
1043: true, true);
1044: }
1045: }
1046:
1047: final static class ActionCellRenderer implements TableCellRenderer {
1048: public Component getTableCellRendererComponent(JTable table,
1049: Object value, boolean isSelected, boolean hasFocus,
1050: int row, int column) {
1051: return ButtonComposer
1052: .createButton((ContelligentAction) value, false,
1053: null, true, true);
1054: }
1055: }
1056:
1057: final class DeleteMappingAction extends AbstractAction implements
1058: ContelligentAction {
1059: private int row;
1060:
1061: public DeleteMappingAction(int row) {
1062: super ("delete_mapping_action",
1063: Resources.deleteGUIMappingIcon);
1064: putValue(TYPE, PUSH_ACTION);
1065: putValue(ACTION_TYPE, EDIT_ACTION);
1066: putValue(MENU_TARGET, MENU);
1067: putValue(BUTTON_TARGET, TOOLBAR);
1068: this .row = row;
1069: }
1070:
1071: public void actionPerformed(ActionEvent e) {
1072: guiMappingTableModel.removeRow(row);
1073: }
1074: }
1075:
1076: final class EditMappingAction extends AbstractAction implements
1077: ContelligentAction {
1078: private int row;
1079:
1080: public EditMappingAction(int row) {
1081: super ("edit_mapping_action", Resources.editIcon);
1082: if (row == -1) {
1083: putValue(Action.SMALL_ICON, Resources.addGUIMappingIcon);
1084: }
1085: putValue(TYPE, PUSH_ACTION);
1086: putValue(ACTION_TYPE, EDIT_ACTION);
1087: putValue(MENU_TARGET, MENU);
1088: putValue(BUTTON_TARGET, TOOLBAR);
1089: this .row = row;
1090: }
1091:
1092: public void actionPerformed(ActionEvent e) {
1093: String type = null, command = null, gui = null, configuration = null;
1094: ContelligentAction remove = null, edit = null;
1095: if (row != -1) {
1096: type = (String) guiMappingTableModel.getValueAt(row, 0);
1097: command = (String) guiMappingTableModel.getValueAt(row,
1098: 1);
1099: gui = (String) guiMappingTableModel.getValueAt(row, 2);
1100: configuration = (String) guiMappingTableModel
1101: .getValueAt(row, 3);
1102: }
1103: MappingDialog mappingDialog = new MappingDialog(
1104: ContelligentClient.getFrame(),
1105: Resources
1106: .getLocalString("configure_mapping_request"),
1107: type, command, gui, configuration);
1108: mappingDialog.pack();
1109: Dimension screenDim = Toolkit.getDefaultToolkit()
1110: .getScreenSize();
1111: Rectangle winDim = mappingDialog.getBounds();
1112: mappingDialog.setLocation(
1113: (screenDim.width - winDim.width) / 2,
1114: (screenDim.height - winDim.height) / 2);
1115: mappingDialog.setVisible(true);
1116: if (mappingDialog.getOption() == JOptionPane.OK_OPTION) {
1117: type = mappingDialog.getType();
1118: command = mappingDialog.getCommand();
1119: gui = mappingDialog.getGUI();
1120: configuration = mappingDialog.getConfiguration();
1121: if (row == -1) {
1122: int newRow = guiMappingTableModel.getRowCount();
1123: guiMappingTableModel.addRow(new Object[] { type,
1124: command, gui, configuration,
1125: new DeleteMappingAction(newRow),
1126: new EditMappingAction(newRow) });
1127: } else {
1128: guiMappingTableModel.setValueAt(type, row, 0);
1129: guiMappingTableModel.setValueAt(command, row, 1);
1130: guiMappingTableModel.setValueAt(gui, row, 2);
1131: guiMappingTableModel.setValueAt(configuration, row,
1132: 3);
1133: guiMappingTableModel.setValueAt(remove, row, 4);
1134: guiMappingTableModel.setValueAt(edit, row, 5);
1135: }
1136: }
1137: }
1138: }
1139:
1140: public final class ModuleHandler extends DefaultSimpleImportHandler {
1141: public void startElement(SimplePath path, String name,
1142: AttributesImpl attrs, String leadingCData) {
1143: try {
1144: if (path.matches("client-module")) {
1145: displayName = attrs.getValue("name");
1146: moduleImpl = attrs.getValue("moduleImpl");
1147: category = attrs.getValue("category");
1148: tooltip = attrs.getValue("tooltip");
1149: createMenuEntry = ConversionHelpers.getBoolean(
1150: attrs.getValue("createMenuEntry"), false);
1151: createToolbarEntry = ConversionHelpers
1152: .getBoolean(attrs
1153: .getValue("createToolbarEntry"),
1154: false);
1155: autoOpenModule = ConversionHelpers.getBoolean(attrs
1156: .getValue("autoOpenModule"), false);
1157: createTab = ConversionHelpers.getBoolean(attrs
1158: .getValue("createTab"), false);
1159: moduleConfiguration = leadingCData;
1160: }
1161: } catch (Throwable t) {
1162: logger.log(Level.SEVERE,
1163: "Caught exception while parsing module", t);
1164: }
1165: }
1166: }
1167: }
|