001: package org.netbeans.modules.mashup.db.ui.wizard;
002:
003: import java.awt.Component;
004: import java.sql.Connection;
005: import java.sql.SQLException;
006: import java.util.HashMap;
007:
008: import net.java.hulp.i18n.Logger;
009: import org.netbeans.modules.etl.logger.Localizer;
010: import org.netbeans.modules.etl.logger.LogUtil;
011: import org.netbeans.modules.mashup.db.model.FlatfileDBConnectionDefinition;
012: import org.netbeans.modules.mashup.db.model.FlatfileDBTable;
013: import org.netbeans.modules.mashup.db.model.FlatfileDatabaseModel;
014: import org.netbeans.modules.mashup.db.model.FlatfileDefinition;
015: import org.netbeans.modules.mashup.db.model.impl.FlatfileDBConnectionDefinitionImpl;
016: import org.netbeans.modules.mashup.db.model.impl.FlatfileDatabaseModelImpl;
017: import org.netbeans.modules.mashup.tables.wizard.MashupTableWizardIterator;
018: import org.openide.ErrorManager;
019: import org.openide.WizardDescriptor;
020: import org.openide.util.HelpCtx;
021:
022: public class SelectDatabasePanel extends AbstractWizardPanel {
023: private static transient final Logger mLogger = LogUtil
024: .getLogger(SelectDatabasePanel.class.getName());
025: private static transient final Localizer mLoc = Localizer.get();
026: /**
027: * The visual component that displays this panel. If you need to access the
028: * component from this class, just use getComponent().
029: */
030: private SelectDatabaseVisualPanel component;
031:
032: // Get the visual component for the panel. In this template, the component
033: // is kept separate. This can be more efficient: if the wizard is created
034: // but never displayed, or not all panels are displayed, it is better to
035: // create only those which really need to be visible.
036: public Component getComponent() {
037: if (component == null) {
038: component = new SelectDatabaseVisualPanel(this );
039: }
040: return component;
041: }
042:
043: public HelpCtx getHelp() {
044: return HelpCtx.DEFAULT_HELP;
045: }
046:
047: public boolean isValid() {
048: return someCondition();
049: }
050:
051: // You can use a settings object to keep track of state. Normally the
052: // settings object will be the WizardDescriptor, so you can use
053: // WizardDescriptor.getProperty & putProperty to store information entered
054: // by the user.
055: public void readSettings(Object settings) {
056: if (settings instanceof WizardDescriptor) {
057: WizardDescriptor wd = (WizardDescriptor) settings;
058: Connection conn = (Connection) wd
059: .getProperty(MashupTableWizardIterator.CONNECTION);
060: if (conn != null) {
061: try {
062: conn.createStatement().execute("shutdown");
063: conn.close();
064: } catch (SQLException ex) {
065: //ignore
066: }
067: }
068: wd.putProperty(MashupTableWizardIterator.CONNECTION, null);
069: }
070: }
071:
072: public void storeSettings(Object settings) {
073: if (settings instanceof WizardDescriptor) {
074: WizardDescriptor wd = (WizardDescriptor) settings;
075: SelectDatabaseVisualPanel panel = (SelectDatabaseVisualPanel) getComponent();
076: String database = panel.getSelectedDatabase();
077: if (database != null) {
078: database = database.trim();
079: int end = database.indexOf(":", "jdbc.axiondb:"
080: .length());
081: if (end == -1) {
082: end = database.trim().length();
083: }
084: String dbName = database.substring("jdbc:axiondb:"
085: .length(), end);
086: wd.putProperty("url", database);
087:
088: FlatfileDatabaseModel model = (FlatfileDatabaseModel) wd
089: .getProperty(MashupTableWizardIterator.PROP_FLATFILEDBMODEL);
090: if (model == null) {
091: FlatfileDBConnectionDefinition def = new FlatfileDBConnectionDefinitionImpl(
092: dbName);
093: def.setConnectionURL(database);
094: model = new FlatfileDatabaseModelImpl(database, def);
095: model.setConnectionName(dbName);
096: model.setDescription(dbName);
097: FlatfileDefinition ffDefn;
098: try {
099: ffDefn = new FlatfileDefinition(dbName);
100: ffDefn.setInstanceName(dbName);
101: ffDefn.setFlatfileDatabaseModel(model);
102: } catch (Exception ex) {
103: ErrorManager.getDefault().log(
104: ErrorManager.ERROR, ex.getMessage());
105: ErrorManager.getDefault().notify(
106: ErrorManager.ERROR, ex);
107:
108: }
109: wd
110: .putProperty(
111: MashupTableWizardIterator.PROP_FLATFILEDBMODEL,
112: model);
113: wd.putProperty(MashupTableWizardIterator.TABLE_MAP,
114: new HashMap<String, FlatfileDBTable>());
115: }
116: } else {
117: ErrorManager.getDefault().log(
118: "Create the database before adding tables.");
119: }
120: }
121: }
122:
123: private boolean someCondition() {
124: SelectDatabaseVisualPanel panel = (SelectDatabaseVisualPanel) getComponent();
125: String dbUrl = panel.getSelectedDatabase();
126: if (dbUrl == null) {
127: return false;
128: } else if (panel.isPopulated()) {
129: return true;
130: }
131: return true;
132: }
133:
134: public String getStepLabel() {
135: String nbBundle1 = mLoc.t("PRSR001: Select Database");
136: return Localizer.parse(nbBundle1);
137: }
138:
139: public String getTitle() {
140: return "Select Database";
141: }
142: }
|