001: package org.netbeans.modules.mashup.tables.wizard;
002:
003: import java.awt.Component;
004: import java.io.File;
005: import java.util.HashMap;
006: import java.util.HashSet;
007: import java.util.Iterator;
008: import java.util.List;
009: import java.util.Map;
010: import java.util.Set;
011: import javax.swing.event.ChangeEvent;
012: import javax.swing.event.ChangeListener;
013:
014: import org.netbeans.modules.mashup.db.bootstrap.TemplateFactory;
015: import org.netbeans.modules.mashup.db.common.PropertyKeys;
016: import org.netbeans.modules.mashup.db.model.FlatfileDBTable;
017: import org.netbeans.modules.mashup.db.model.FlatfileDatabaseModel;
018: import org.netbeans.modules.mashup.db.model.impl.FlatfileDBTableImpl;
019: import org.openide.WizardDescriptor;
020: import org.openide.util.HelpCtx;
021:
022: /**
023: * Panel for configuring table details such as table type,etc.
024: * @author karthik
025: */
026: public class TableDetailsPanel implements WizardDescriptor.Panel {
027:
028: /**
029: * The visual component that displays this panel. If you need to access the
030: * component from this class, just use getComponent().
031: */
032: private Component component;
033: private int currentIndex = -1;
034: private WizardDescriptor wd;
035:
036: public Component getComponent() {
037: if (component == null) {
038: component = new TableDetailsVisualPanel(this );
039: }
040: return component;
041: }
042:
043: public HelpCtx getHelp() {
044: return HelpCtx.DEFAULT_HELP;
045: }
046:
047: public boolean isValid() {
048: return canAdvance();
049: }
050:
051: private final Set<ChangeListener> listeners = new HashSet<ChangeListener>(
052: 1);
053:
054: public final void addChangeListener(ChangeListener l) {
055: synchronized (listeners) {
056: listeners.add(l);
057: }
058: }
059:
060: public final void removeChangeListener(ChangeListener l) {
061: synchronized (listeners) {
062: listeners.remove(l);
063: }
064: }
065:
066: protected final void fireChangeEvent() {
067: Iterator<ChangeListener> it;
068: synchronized (listeners) {
069: it = new HashSet<ChangeListener>(listeners).iterator();
070: }
071: ChangeEvent ev = new ChangeEvent(this );
072: while (it.hasNext()) {
073: it.next().stateChanged(ev);
074: }
075: }
076:
077: public void readSettings(Object settings) {
078: if (settings instanceof WizardDescriptor) {
079: wd = (WizardDescriptor) settings;
080: FlatfileDatabaseModel model = (FlatfileDatabaseModel) wd
081: .getProperty(MashupTableWizardIterator.PROP_FLATFILEDBMODEL);
082: int index = Integer
083: .parseInt((String) wd
084: .getProperty(MashupTableWizardIterator.TABLE_INDEX));
085: currentIndex = index;
086: String jdbcUrl = (String) wd.getProperty("url");
087: List tables = (List) wd
088: .getProperty(MashupTableWizardIterator.TABLE_LIST);
089: List urls = (List) wd
090: .getProperty(MashupTableWizardIterator.URL_LIST);
091: Map map = (Map) wd
092: .getProperty(MashupTableWizardIterator.TABLE_MAP);
093: FlatfileDBTable tbl = (FlatfileDBTable) map.get(tables
094: .get(index));
095: FlatfileDBTable table = null;
096: String fileName = null;
097: if (tbl == null) {
098: table = new FlatfileDBTableImpl();
099: File f = new File((String) tables.get(index));
100: if (f.exists()) {
101: table.setFileName(f.getName());
102: table.setLocalFilePath(f.getAbsoluteFile());
103: fileName = f.getName();
104: } else {
105: fileName = (String) urls.get(index);
106: }
107: } else {
108: table = tbl;
109: fileName = (String) urls.get(index);
110: }
111: ((FlatfileDBTableImpl) table).setOrPutProperty(
112: PropertyKeys.URL, urls.get(index));
113: ((TableDetailsVisualPanel) getComponent())
114: .setCurrentTable(table);
115: ((TableDetailsVisualPanel) getComponent())
116: .setFileName(fileName);
117: ((TableDetailsVisualPanel) getComponent())
118: .setJDBCUrl(jdbcUrl);
119: ((TableDetailsVisualPanel) getComponent())
120: .setResourceUrl((String) urls.get(index));
121: ((TableDetailsVisualPanel) getComponent())
122: .guessParserType(table);
123: ((TableDetailsVisualPanel) getComponent())
124: .setDBModel(model);
125: wd.putProperty(MashupTableWizardIterator.PROP_CURRENTTABLE,
126: table);
127: }
128: }
129:
130: public void storeSettings(Object settings) {
131: if (settings instanceof WizardDescriptor) {
132: WizardDescriptor wizDes = (WizardDescriptor) settings;
133: FlatfileDatabaseModel model = (FlatfileDatabaseModel) wd
134: .getProperty(MashupTableWizardIterator.PROP_FLATFILEDBMODEL);
135: int index = Integer
136: .parseInt((String) wd
137: .getProperty(MashupTableWizardIterator.TABLE_INDEX));
138: List tables = (List) wizDes
139: .getProperty(MashupTableWizardIterator.TABLE_LIST);
140: Map map = (Map) wizDes
141: .getProperty(MashupTableWizardIterator.TABLE_MAP);
142: if (map == null) {
143: map = new HashMap<String, FlatfileDBTable>();
144: }
145: List urls = (List) wd
146: .getProperty(MashupTableWizardIterator.URL_LIST);
147: if (currentIndex == index) {
148: FlatfileDBTableImpl table = (FlatfileDBTableImpl) wd
149: .getProperty(MashupTableWizardIterator.PROP_CURRENTTABLE);
150: String tblName = ((TableDetailsVisualPanel) getComponent())
151: .getTableName();
152: String type = ((TableDetailsVisualPanel) getComponent())
153: .getTableType();
154: if (tblName != null && type != null
155: && !tblName.equals("") && !type.equals("")) {
156: table.setName(tblName);
157: table.setParseType(type);
158: table
159: .setEncodingScheme(((TableDetailsVisualPanel) getComponent())
160: .getEncoding());
161: if (type.equals(PropertyKeys.DELIMITED)
162: || type.equals(PropertyKeys.FIXEDWIDTH)) {
163: File f = new File((String) urls.get(index));
164: if (f.exists()) {
165: table.setLocalFilePath(f.getAbsoluteFile());
166: table.setFileName(f.getName());
167: } else {
168: table.setOrPutProperty(
169: PropertyKeys.FILENAME, urls
170: .get(index));
171: }
172: } else {
173: table.setOrPutProperty(PropertyKeys.FILENAME,
174: urls.get(index));
175: }
176: tables.set(index, table.getName());
177: map.put(tblName, table);
178: wizDes.putProperty(
179: MashupTableWizardIterator.TABLE_LIST,
180: tables);
181: wizDes.putProperty(
182: MashupTableWizardIterator.TABLE_MAP, map);
183: table.setProperties(TemplateFactory
184: .getProperties(type));
185: table.setOrPutProperty(PropertyKeys.URL, urls
186: .get(index));
187: model.addTable(table);
188: currentIndex = -1;
189: }
190: }
191: }
192: }
193:
194: private boolean canAdvance() {
195: return ((TableDetailsVisualPanel) getComponent()).canAdvance();
196: }
197: }
|