001: package org.netbeans.modules.mashup.db.wizard;
002:
003: import java.awt.Component;
004: import java.awt.Dialog;
005: import java.io.File;
006: import java.sql.Connection;
007: import java.sql.SQLException;
008: import java.text.MessageFormat;
009: import java.io.FileInputStream;
010: import java.io.FileNotFoundException;
011: import java.io.IOException;
012: import java.util.Properties;
013: import javax.swing.JComponent;
014:
015: import net.java.hulp.i18n.Logger;
016: import org.netbeans.modules.etl.logger.Localizer;
017: import org.netbeans.modules.etl.logger.LogUtil;
018: import org.netbeans.modules.etl.ui.ETLEditorSupport;
019: import org.netbeans.modules.mashup.db.ui.AxionDBConfiguration;
020: import org.netbeans.modules.mashup.tables.wizard.MashupTableWizardIterator;
021: import org.netbeans.modules.sql.framework.common.utils.DBExplorerUtil;
022: import org.openide.DialogDisplayer;
023: import org.openide.NotifyDescriptor;
024: import org.openide.WizardDescriptor;
025: import org.openide.util.HelpCtx;
026: import org.openide.util.actions.CallableSystemAction;
027:
028: public final class NewFlatfileDatabaseWizardAction extends
029: CallableSystemAction {
030:
031: private WizardDescriptor.Panel[] panels;
032: public static final String DEFAULT_FLATFILE_JDBC_URL_PREFIX = "jdbc:axiondb:";
033: private static transient final Logger mLogger = LogUtil
034: .getLogger(NewFlatfileDatabaseWizardAction.class.getName());
035: private static transient final Localizer mLoc = Localizer.get();
036: public String nbBundle1 = mLoc.t("PRSR001: Create Mashup Database");
037:
038: public void performAction() {
039: WizardDescriptor wizardDescriptor = new WizardDescriptor(
040: getPanels());
041: // {0} will be replaced by WizardDesriptor.Panel.getComponent().getName()
042: wizardDescriptor.setTitleFormat(new MessageFormat("{0}"));
043: wizardDescriptor.setTitle(Localizer.parse(nbBundle1));
044: Dialog dialog = DialogDisplayer.getDefault().createDialog(
045: wizardDescriptor);
046: dialog
047: .getAccessibleContext()
048: .setAccessibleDescription(
049: "This is the Dialog which lets the user create a mashup database");
050: dialog.setSize(630, 334);
051: dialog.setVisible(true);
052: dialog.toFront();
053: boolean cancelled = wizardDescriptor.getValue() != WizardDescriptor.FINISH_OPTION;
054: if (!cancelled) {
055: String dbName = (String) wizardDescriptor
056: .getProperty("dbName");
057: boolean status = handle(dbName);
058: if (status) {
059: String nbBundle2 = mLoc.t(
060: "PRSR001: Database {0} successfully created.",
061: dbName);
062: NotifyDescriptor d = new NotifyDescriptor.Message(
063: Localizer.parse(nbBundle2),
064: NotifyDescriptor.INFORMATION_MESSAGE);
065: DialogDisplayer.getDefault().notify(d);
066: }
067: }
068: }
069:
070: /**
071: * Initialize panels representing individual wizard's steps and sets
072: * various properties for them influencing wizard appearance.
073: */
074: private WizardDescriptor.Panel[] getPanels() {
075: if (panels == null) {
076: panels = new WizardDescriptor.Panel[] { new NewFlatfileDatabaseWizardPanel() };
077: String[] steps = new String[panels.length];
078: for (int i = 0; i < panels.length; i++) {
079: Component c = panels[i].getComponent();
080: // Default step name to component name of panel. Mainly useful
081: // for getting the name of the target chooser to appear in the
082: // list of steps.
083: steps[i] = c.getName();
084: if (c instanceof JComponent) { // assume Swing components
085: JComponent jc = (JComponent) c;
086: // Sets step number of a component
087: jc.putClientProperty(
088: "WizardPanel_contentSelectedIndex",
089: new Integer(i));
090: // Sets steps names for a panel
091: jc.putClientProperty("WizardPanel_contentData",
092: steps);
093: // Turn on subtitle creation on each step
094: jc.putClientProperty("WizardPanel_autoWizardStyle",
095: Boolean.TRUE);
096: // Show steps on the left side with the image on the background
097: jc.putClientProperty(
098: "WizardPanel_contentDisplayed",
099: Boolean.TRUE);
100: // Turn on numbering of all steps
101: jc.putClientProperty("WizardPanel_contentNumbered",
102: Boolean.TRUE);
103: }
104: }
105: }
106: return panels;
107: }
108:
109: public String getName() {
110: return Localizer.parse(nbBundle1);
111: }
112:
113: @Override
114: public String iconResource() {
115: return null;
116: }
117:
118: public HelpCtx getHelpCtx() {
119: return HelpCtx.DEFAULT_HELP;
120: }
121:
122: @Override
123: protected boolean asynchronous() {
124: return false;
125: }
126:
127: private String getDefaultWorkingFolder() {
128: File conf = AxionDBConfiguration.getConfigFile();
129: Properties prop = new Properties();
130: try {
131: FileInputStream in = new FileInputStream(conf);
132: prop.load(in);
133: } catch (FileNotFoundException ex) {
134: //ignore
135: } catch (IOException ex) {
136: //ignore
137: }
138: String loc = prop.getProperty(AxionDBConfiguration.PROP_DB_LOC);
139: File db = new File(loc);
140: if (!db.exists()) {
141: db.mkdir();
142: }
143: return loc;
144: }
145:
146: private boolean handle(String name) {
147: String location = null;
148: if (MashupTableWizardIterator.IS_PROJECT_CALL) {
149: location = ETLEditorSupport.PRJ_PATH
150: + "\\nbproject\\private\\databases";
151: } else {
152: location = getDefaultWorkingFolder();
153: }
154: MashupTableWizardIterator.IS_PROJECT_CALL = false;
155: boolean status = false;
156: String url = DEFAULT_FLATFILE_JDBC_URL_PREFIX + name + ":"
157: + location + "\\" + name;
158: File f = new File(location + name);
159: char[] ch = name.toCharArray();
160: if (ch == null) {
161: String nbBundle3 = mLoc
162: .t("PRSR001: No Database name specified.");
163: NotifyDescriptor d = new NotifyDescriptor.Message(Localizer
164: .parse(nbBundle3),
165: NotifyDescriptor.INFORMATION_MESSAGE);
166: DialogDisplayer.getDefault().notify(d);
167: } else if (f.exists()) {
168: String nbBundle4 = mLoc.t(
169: "PRSR001: Database {0} already exists.", name);
170: NotifyDescriptor d = new NotifyDescriptor.Message(Localizer
171: .parse(nbBundle4),
172: NotifyDescriptor.INFORMATION_MESSAGE);
173: DialogDisplayer.getDefault().notify(d);
174: } else {
175: Connection conn = null;
176: try {
177: conn = DBExplorerUtil
178: .createConnection(
179: "org.axiondb.jdbc.AxionDriver", url,
180: "sa", "sa");
181: if (conn != null) {
182: status = true;
183: }
184: } catch (Exception ex) {
185: String nbBundle5 = mLoc
186: .t("PRSR001: Axion driver could not be loaded.");
187: NotifyDescriptor d = new NotifyDescriptor.Message(
188: Localizer.parse(nbBundle5),
189: NotifyDescriptor.INFORMATION_MESSAGE);
190: DialogDisplayer.getDefault().notify(d);
191: } finally {
192: try {
193: if (conn != null) {
194: conn.createStatement().execute("shutdown");
195: conn.close();
196: }
197: } catch (SQLException ex) {
198: conn = null;
199: }
200: }
201: }
202: return status;
203: }
204: }
|