001: package org.netbeans.modules.mashup.tables.wizard;
002:
003: import java.awt.Component;
004: import java.util.Stack;
005: import java.util.HashSet;
006: import java.util.Iterator;
007: import java.util.Map;
008: import java.util.NoSuchElementException;
009: import java.util.Set;
010: import javax.swing.JComponent;
011: import java.util.ArrayList;
012: import java.util.List;
013: import javax.swing.event.ChangeEvent;
014: import javax.swing.event.ChangeListener;
015: import org.netbeans.modules.etl.ui.ETLEditorSupport;
016: import org.netbeans.modules.mashup.db.common.PropertyKeys;
017: import org.netbeans.modules.mashup.db.model.FlatfileDBTable;
018: import org.netbeans.modules.mashup.db.model.FlatfileDatabaseModel;
019: import org.netbeans.modules.mashup.db.model.impl.FlatfileDBTableImpl;
020: import org.netbeans.modules.mashup.db.ui.wizard.ParseContentPanel;
021: import org.netbeans.modules.mashup.db.ui.wizard.SelectDatabasePanel;
022: import org.netbeans.modules.mashup.db.ui.wizard.TableDefinitionPanel;
023: import org.netbeans.modules.sql.framework.common.utils.DBExplorerUtil;
024: import org.openide.WizardDescriptor;
025:
026: public final class MashupTableWizardIterator implements
027: WizardDescriptor.Iterator {
028:
029: private WizardDescriptor desc;
030: private List<String> tables = new ArrayList<String>();
031: public static final String TABLE_INDEX = "tableIndex";
032: public static final String TABLE_LIST = "tableList";
033: public static final String URL_LIST = "urlList";
034: public static final String TABLE_MAP = "tableMap";
035: public static final String CONNECTION = "connection";
036: /** Property key: current flatfile */
037: public static final String PROP_CURRENTTABLE = "CurrentTable"; // NOI18N
038: /** Property key: FlatfileDBModel instances */
039: public static final String PROP_FLATFILEDBMODEL = "FlatfileDBModel"; // NOI18N
040: private int index;
041: private int tracker = -1;
042: private WizardDescriptor.Panel[] panels;
043: //private Deque<Integer> stack = new ArrayDeque<Integer>();
044: private Stack<Integer> stack = new Stack<Integer>();
045:
046: public MashupTableWizardIterator() {
047: super ();
048: addToStack(0);
049: }
050:
051: /**
052: * Initialize panels representing individual wizard's steps and sets
053: * various properties for them influencing wizard appearance.
054: */
055: private WizardDescriptor.Panel[] getPanels() {
056: if (panels == null) {
057: panels = new WizardDescriptor.Panel[] {
058: new SelectDatabasePanel(),
059: new FileSelectionPanel(), new TableDetailsPanel(),
060: new SpreadsheetChooserPanel(),
061: new ChooseTablePanel(), new ParseContentPanel(),
062: new TableDefinitionPanel() };
063: String[] steps = new String[panels.length];
064: for (int i = 0; i < panels.length; i++) {
065: Component c = panels[i].getComponent();
066: // Default step name to component name of panel.
067: steps[i] = c.getName();
068: if (c instanceof JComponent) {
069: // assume Swing components
070: JComponent jc = (JComponent) c;
071: // Sets step number of a component
072: jc.putClientProperty(
073: "WizardPanel_contentSelectedIndex",
074: new Integer(i));
075: // Sets steps names for a panel
076: jc.putClientProperty("WizardPanel_contentData",
077: steps);
078: // Turn on subtitle creation on each step
079: jc.putClientProperty("WizardPanel_autoWizardStyle",
080: Boolean.TRUE);
081: // Show steps on the left side with the image on the background
082: jc.putClientProperty(
083: "WizardPanel_contentDisplayed",
084: Boolean.TRUE);
085: // Turn on numbering of all steps
086: jc.putClientProperty("WizardPanel_contentNumbered",
087: Boolean.TRUE);
088: }
089: }
090: }
091: return panels;
092: }
093:
094: public WizardDescriptor.Panel current() {
095: return getPanels()[index];
096: }
097:
098: public String name() {
099: return index + 1 + ". from " + getPanels().length;
100: }
101:
102: public boolean hasNext() {
103: return findNext();
104: }
105:
106: public boolean hasPrevious() {
107: return index > 0 && !stack.isEmpty();
108: }
109:
110: public void nextPanel() {
111: if (!hasNext()) {
112: throw new NoSuchElementException();
113: }
114: WizardDescriptor.Panel current = current();
115: if (current instanceof FileSelectionPanel) {
116: tables = (List<String>) desc.getProperty(TABLE_LIST);
117: tracker = 0;
118: index++;
119: } else if (current instanceof TableDefinitionPanel) {
120: if (++tracker < tables.size()) {
121: index = 2;
122: }
123: } else if (current instanceof JDBCTablePanel) {
124: if (++tracker < tables.size()) {
125: index = 2;
126: }
127: } else if (current instanceof SpreadsheetChooserPanel
128: || current instanceof ChooseTablePanel) {
129: index = 5;
130: } else if (current instanceof TableDetailsPanel) {
131: FlatfileDBTable tbl = (FlatfileDBTable) desc
132: .getProperty(PROP_CURRENTTABLE);
133: String type = tbl.getParserType();
134: if (type.equals("WEB")) {
135: index = 4;
136: } else if (type.equals("SPREADSHEET")) {
137: index = 3;
138: } else {
139: index = 5;
140: }
141: } else {
142: index++;
143: }
144: if (current() instanceof TableDetailsPanel) {
145: desc.putProperty(TABLE_INDEX, String.valueOf(tracker));
146: }
147: addToStack(index);
148: }
149:
150: public void previousPanel() {
151: if (!hasPrevious()) {
152: throw new NoSuchElementException();
153: }
154: if (stack.elementAt(stack.size() - 1) != null) {
155: if (index == Integer.valueOf(stack
156: .elementAt(stack.size() - 1))) {
157: stack.removeElementAt(stack.size() - 1);
158: }
159: index = Integer.valueOf(stack.elementAt(stack.size() - 1));
160: } else {
161: index--;
162: }
163: WizardDescriptor.Panel current = current();
164: if (current instanceof TableDefinitionPanel) {
165: setNewTable();
166: } else if (current instanceof FileSelectionPanel) {
167: tracker = -1;
168: tables.clear();
169: }
170: }
171:
172: public void setDescriptor(WizardDescriptor wiz) {
173: desc = wiz;
174: }
175:
176: private Set<ChangeListener> listeners = new HashSet<ChangeListener>(
177: 1);
178:
179: public final void addChangeListener(ChangeListener l) {
180: synchronized (listeners) {
181: listeners.add(l);
182: }
183: }
184:
185: public final void removeChangeListener(ChangeListener l) {
186: synchronized (listeners) {
187: listeners.remove(l);
188: }
189: }
190:
191: protected final void fireChangeEvent() {
192: Iterator<ChangeListener> it;
193: synchronized (listeners) {
194: it = new HashSet<ChangeListener>(listeners).iterator();
195: }
196: ChangeEvent ev = new ChangeEvent(this );
197: while (it.hasNext()) {
198: it.next().stateChanged(ev);
199: }
200: }
201:
202: private boolean findNext() {
203: if (tracker != -1) {
204: if ((tracker + 1) == tables.size() && index >= 6) {
205: return false;
206: }
207: }
208: return true;
209: }
210:
211: private void setNewTable() {
212: tracker--;
213: if (tracker < 0) {
214: return;
215: } else {
216: String name = ((List<String>) desc.getProperty(TABLE_LIST))
217: .get(tracker);
218: desc.putProperty(TABLE_INDEX, String.valueOf(tracker));
219: List<String> urls = (List<String>) desc
220: .getProperty(URL_LIST);
221: FlatfileDatabaseModel model = (FlatfileDatabaseModel) desc
222: .getProperty(MashupTableWizardIterator.PROP_FLATFILEDBMODEL);
223: FlatfileDBTable tempTable = (FlatfileDBTable) desc
224: .getProperty(PROP_CURRENTTABLE);
225: if (tempTable != null) {
226: if (!model.deleteTable(tempTable.getTableName())) {
227: tempTable.setName("TEMPTBL");
228: model.deleteTable(tempTable.getTableName());
229: }
230: }
231: FlatfileDBTable table = ((Map<String, FlatfileDBTable>) desc
232: .getProperty(TABLE_MAP)).get(name);
233: if (table != null) {
234: ((FlatfileDBTableImpl) table).setOrPutProperty(
235: PropertyKeys.FILENAME, urls.get(tracker));
236: ((FlatfileDBTableImpl) table).setOrPutProperty(
237: PropertyKeys.URL, urls.get(tracker));
238: desc.putProperty(
239: MashupTableWizardIterator.PROP_CURRENTTABLE,
240: table);
241: }
242: }
243: }
244:
245: private void addToStack(int i) {
246: try {
247: int stackSize = stack.size();
248: if (stackSize == 0 && i == 0) {
249: stack.add(i);
250: } else {
251: if (stack.elementAt(stackSize - 1) != null
252: && !stack.elementAt(stackSize - 1).equals(i)) {
253: int last = stack.elementAt(stackSize - 1);
254: if (i > last) {
255: stack.add(i);
256: } else if (i < last && last >= 6) {
257: stack.add(i);
258: }
259: }
260: }
261: } finally {
262: }
263: }
264:
265: public static void setProjectInfo(String name, String prjInfo,
266: boolean value) {
267: IS_PROJECT_CALL = value;
268: ETLEditorSupport.PRJ_NAME = name;
269: ETLEditorSupport.PRJ_PATH = DBExplorerUtil.unifyPath(prjInfo);
270: }
271:
272: public static boolean IS_PROJECT_CALL = false;
273: }
|