001 /****************************************************************************** 002 * Copyright (C) MActor Developers. All rights reserved. * 003 * ---------------------------------------------------------------------------* 004 * This file is part of MActor. * 005 * * 006 * MActor is free software; you can redistribute it and/or modify * 007 * it under the terms of the GNU General Public License as published by * 008 * the Free Software Foundation; either version 2 of the License, or * 009 * (at your option) any later version. * 010 * * 011 * MActor is distributed in the hope that it will be useful, * 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 014 * GNU General Public License for more details. * 015 * * 016 * You should have received a copy of the GNU General Public License * 017 * along with MActor; if not, write to the Free Software * 018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * 019 ******************************************************************************/ 020 package org.mactor.ui.gui.project.editors; 021 022 import java.awt.event.ActionEvent; 023 import java.awt.event.ItemListener; 024 import java.io.File; 025 import java.io.FileWriter; 026 import java.io.IOException; 027 028 import javax.swing.AbstractAction; 029 import javax.swing.JButton; 030 import javax.swing.JFileChooser; 031 import javax.swing.JLabel; 032 import javax.swing.JOptionPane; 033 034 import org.dom4j.Element; 035 import org.mactor.framework.MactorException; 036 import org.mactor.framework.spec.ProjectContext; 037 import org.mactor.ui.gui.GuiUtil; 038 039 public class DataSourceEditor extends SimpleFormPanel { 040 CommandPrototypeComboBox protoCb = new CommandPrototypeComboBox(); 041 CommandTypeComboBox typeCb = new CommandTypeComboBox(); 042 GlobalConfigDBGroupComboBox gcDbGroup = new GlobalConfigDBGroupComboBox(); 043 Element data; 044 SimpleRowPanel dbPanel = new SimpleRowPanel(); 045 CsvPanel csvPanel = new CsvPanel(); 046 JButton test = new JButton(new AbstractAction("Test...") { 047 public void actionPerformed(ActionEvent e) { 048 try { 049 applyListener.onApply(); 050 new DataSourceTestDlg(JOptionPane.getFrameForComponent(DataSourceEditor.this), data).setVisible(true); 051 } catch (MactorException me) { 052 GuiUtil.showGuiError(DataSourceEditor.this, me); 053 } 054 } 055 }); 056 ItemListener typeCbListener = new ItemListener() { 057 public void itemStateChanged(java.awt.event.ItemEvent e) { 058 NodeEditorConfig.CommandType type = (NodeEditorConfig.CommandType) typeCb.getSelectedItem(); 059 String proto = protoCb.getSelectedPrototype(); 060 if (type != null) { 061 protoCb.setConfig(type.getPrototypes()); 062 boolean showDbPanel = "sql".equalsIgnoreCase(type.getName()); 063 dbPanel.setVisible(showDbPanel); 064 if (showDbPanel) 065 gcDbGroup.load(); 066 csvPanel.setVisible("file".equalsIgnoreCase(type.getName())); 067 } else { 068 protoCb.setConfig(null); 069 dbPanel.setVisible(false); 070 csvPanel.setVisible(false); 071 } 072 protoCb.setSelectedPrototype(proto); 073 }; 074 }; 075 public void setTipListener(TipListener tipListener) { 076 protoCb.setTipListener(tipListener); 077 typeCb.setTipListener(tipListener); 078 } 079 ApplyListener applyListener; 080 public DataSourceEditor(String dataSourceCaption, ApplyListener applyListener) { 081 this.applyListener = applyListener; 082 add(new JLabel(dataSourceCaption)); 083 SimpleRowPanel panel = new SimpleRowPanel(); 084 panel.add(typeCb); 085 dbPanel.add(new JLabel(":")); 086 dbPanel.add(gcDbGroup); 087 panel.add(dbPanel); 088 panel.add(new JLabel(":")); 089 panel.add(protoCb); 090 panel.add(csvPanel); 091 panel.add(test); 092 add(panel); 093 typeCb.addItemListener(typeCbListener); 094 } 095 public void applyChanges() { 096 if (data.attribute("data_source") == null) 097 data.addAttribute("data_source", ""); 098 if (dbPanel.isVisible()) 099 data.attribute("data_source").setValue(typeCb.getSelectedType() + ":" + gcDbGroup.getSelectedGroup() + ":" + protoCb.getSelectedPrototype()); 100 else 101 data.attribute("data_source").setValue(typeCb.getSelectedType() + ":" + protoCb.getSelectedPrototype()); 102 } 103 public void setData(Object data) throws MactorException { 104 this.data = (Element) data; 105 String command = this.data.attributeValue("data_source"); 106 String[] parts = null; 107 if (command != null) 108 parts = command.split(":"); 109 String type = null; 110 String group = null; 111 String proto = null; 112 if (parts != null) { 113 if (parts.length >= 1) 114 type = parts[0]; 115 if (parts.length > 2) { 116 proto = parts[2]; 117 group = parts[1]; 118 } else if (parts.length == 2) 119 proto = parts[1]; 120 } 121 typeCb.setSelectedType(type); 122 gcDbGroup.setSelectedGroup(group); 123 protoCb.setSelectedPrototype(proto); 124 if (!"sql".equalsIgnoreCase(type)) 125 dbPanel.setVisible(false); 126 ; 127 } 128 public void setConfig(NodeEditorConfig config) { 129 typeCb.setConfig(config.getCommandTypes()); 130 } 131 private class CsvPanel extends SimpleRowPanel { 132 JButton csvEdit = new JButton(new AbstractAction("Edit...") { 133 public void actionPerformed(ActionEvent e) { 134 applyListener.onApply(); 135 String name = protoCb.getSelectedPrototype(); 136 if (name != null && name.length() > 0) { 137 File path = ProjectContext.getGlobalInstance().getAbsolutePath(name); 138 if (!path.exists()) { 139 if (JOptionPane.OK_OPTION != JOptionPane.showConfirmDialog(CsvPanel.this, "The file '" + name + "' does not exist. Create it?", "Create file", JOptionPane.OK_CANCEL_OPTION)) { 140 return; 141 } 142 try { 143 FileWriter fw = new FileWriter(path); 144 fw.write("MyParam1;MyParam2\nval01;val02\nval11;val12"); 145 fw.close(); 146 } catch (IOException ioe) { 147 GuiUtil.showGuiError(DataSourceEditor.this, ioe); 148 return; 149 } 150 } 151 new TextFileEditor(name, "Edit CSV File", "Tip: use a real editor").setVisible(true); 152 } 153 } 154 }); 155 JFileChooser fc = new JFileChooser(ProjectContext.getGlobalInstance().getProjectDir()); 156 JButton selectButton = new JButton(new AbstractAction("Select..") { 157 public void actionPerformed(ActionEvent e) { 158 try { 159 if (protoCb.getSelectedPrototype() != null && protoCb.getSelectedPrototype().toLowerCase().endsWith(".csv")) 160 fc.setSelectedFile(ProjectContext.getGlobalInstance().getAbsolutePath(protoCb.getSelectedPrototype())); 161 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 162 int returnVal = fc.showOpenDialog(DataSourceEditor.this); 163 if (returnVal == JFileChooser.APPROVE_OPTION) { 164 File f = fc.getSelectedFile(); 165 if (!f.getName().toLowerCase().endsWith(".csv")) 166 f = new File(f.getAbsolutePath() + ".csv"); 167 f.createNewFile(); 168 protoCb.setSelectedPrototype(ProjectContext.getGlobalInstance().getRelativePath(f)); 169 } 170 } catch (IOException ioe) { 171 GuiUtil.showGuiError(DataSourceEditor.this, ioe); 172 } 173 } 174 }); 175 public CsvPanel() { 176 add(selectButton); 177 add(csvEdit); 178 } 179 } 180 }