001: package org.netbeans.modules.mashup.tables.wizard;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.IOException;
006: import java.io.InputStream;
007: import java.net.URL;
008: import java.util.HashMap;
009: import java.util.Map;
010: import java.util.SortedMap;
011: import java.util.TreeMap;
012: import javax.swing.JPanel;
013: import javax.swing.table.DefaultTableModel;
014: import javax.swing.text.BadLocationException;
015: import javax.swing.text.EditorKit;
016: import javax.swing.text.Element;
017: import javax.swing.text.ElementIterator;
018: import javax.swing.text.html.HTMLDocument;
019: import javax.swing.text.html.HTMLEditorKit;
020: import net.java.hulp.i18n.Logger;
021: import org.netbeans.modules.etl.logger.Localizer;
022: import org.netbeans.modules.etl.logger.LogUtil;
023:
024: public final class ChooseTableVisualPanel extends JPanel {
025:
026: private String requiredUrl;
027: private ChooseTablePanel owner;
028: private PatchedHTMLEditorKit ek;
029: private int tableNum;
030: private static transient final Logger mLogger = LogUtil
031: .getLogger(ChooseTableVisualPanel.class.getName());
032: private static transient final Localizer mLoc = Localizer.get();
033:
034: public int getTableNum() {
035: return tableNum;
036: }
037:
038: public void setTableNum(int tableNum) {
039: this .tableNum = tableNum;
040: }
041:
042: private SortedMap<String, Integer> tableDepth = new TreeMap<String, Integer>();
043: private Map<String, javax.swing.text.Element> elementMap = new HashMap<String, javax.swing.text.Element>();
044:
045: /**
046: * Creates new form ChooseTableVisualPanel
047: */
048: public ChooseTableVisualPanel(ChooseTablePanel panel) {
049: owner = panel;
050: initComponents();
051: }
052:
053: public String getName() {
054: return "Choose a (HTML) Table";
055: }
056:
057: public boolean canAdvance() {
058: Object obj = "Table #" + tableNum;
059: if (obj != null) {
060: return true;
061: }
062: return false;
063: }
064:
065: public int getTableDepth() {
066: String tableName = "Table #" + tableNum;
067: return tableDepth.get(tableName);
068: }
069:
070: public DefaultTableModel getTableDetails() {
071: DefaultTableModel model = new DefaultTableModel();
072: model.setRowCount(0);
073: model.setColumnCount(5);
074: String str = "Table #" + tableNum;
075: Element element = elementMap.get(str);
076: ElementIterator it = new ElementIterator(element);
077: Element elem = null;
078: int i = 0;
079: int count = 0;
080: while ((elem = it.next()) != null) {
081: if (elem.getName().equalsIgnoreCase("tr")) {
082: if (i++ == 1) {
083: break;
084: }
085: } else if (elem.getName().equalsIgnoreCase("th")
086: || elem.getName().equalsIgnoreCase("td")) {
087: count++;
088: }
089: }
090: for (i = 0; i < count; i++) {
091: Object[] obj = new Object[5];
092: obj[0] = i + 1;
093: obj[1] = "Column_" + String.valueOf(i + 1);
094: obj[2] = 60;
095: obj[3] = "varchar";
096: obj[4] = new Boolean(true);
097: model.addRow(obj);
098: }
099: return model;
100: }
101:
102: /** This method is called from within the constructor to
103: * initialize the form.
104: * WARNING: Do NOT modify this code. The content of this method is
105: * always regenerated by the Form Editor.
106: */
107: // <editor-fold defaultstate="collapsed" desc="Generated Code">
108: private void initComponents() {
109:
110: jLabel1 = new javax.swing.JLabel();
111: preview = new javax.swing.JButton();
112: jScrollPane2 = new javax.swing.JScrollPane();
113: jEditorPane1 = new javax.swing.JEditorPane();
114: setMaximumSize(new java.awt.Dimension(450, 300));
115: setMinimumSize(new java.awt.Dimension(100, 100));
116: setPreferredSize(new java.awt.Dimension(400, 200));
117: String nbBundle30 = mLoc.t("PRSR001: Choose a Table");
118: jLabel1.getAccessibleContext().setAccessibleName(
119: Localizer.parse(nbBundle30));
120: jLabel1.setDisplayedMnemonic(Localizer.parse(nbBundle30)
121: .charAt(0));
122: String nbBundle31 = mLoc.t("PRSR001: Preview");
123: preview.getAccessibleContext().setAccessibleName(
124: Localizer.parse(nbBundle31));
125: preview.setMnemonic(Localizer.parse(nbBundle31).charAt(0));
126: preview.addActionListener(new java.awt.event.ActionListener() {
127:
128: public void actionPerformed(java.awt.event.ActionEvent evt) {
129: previewActionPerformed(evt);
130: }
131: });
132:
133: jEditorPane1.setBackground(new java.awt.Color(236, 233, 216));
134: jScrollPane2.setViewportView(jEditorPane1);
135:
136: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
137: this );
138: this .setLayout(layout);
139: layout.setHorizontalGroup(layout.createParallelGroup(
140: org.jdesktop.layout.GroupLayout.LEADING).add(
141: layout.createSequentialGroup().add(jLabel1).add(14, 14,
142: 14).addPreferredGap(
143: org.jdesktop.layout.LayoutStyle.RELATED, 152,
144: Short.MAX_VALUE).add(preview)).add(
145: org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane2,
146: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400,
147: Short.MAX_VALUE));
148: layout
149: .setVerticalGroup(layout
150: .createParallelGroup(
151: org.jdesktop.layout.GroupLayout.LEADING)
152: .add(
153: layout
154: .createSequentialGroup()
155: .addContainerGap()
156: .add(
157: layout
158: .createParallelGroup(
159: org.jdesktop.layout.GroupLayout.BASELINE,
160: false)
161: .add(jLabel1)
162: .add(preview))
163: .addPreferredGap(
164: org.jdesktop.layout.LayoutStyle.RELATED)
165: .add(
166: jScrollPane2,
167: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
168: 160, Short.MAX_VALUE)));
169: }// </editor-fold>
170:
171: private void previewActionPerformed(java.awt.event.ActionEvent evt) {
172:
173: jEditorPane1.setEditable(false);
174: ek = new PatchedHTMLEditorKit(jEditorPane1, this );
175: jEditorPane1.setEditorKitForContentType("text/html", ek);
176: try {
177: jEditorPane1.setPage(requiredUrl);
178: } catch (IOException e) {
179: System.err.println("can't connect to the URL:"
180: + requiredUrl);
181: }
182: }
183:
184: /*
185: * This method reads the html file and
186: * gets all the table data in comma seperated form.
187: *
188: */
189: public void populateTablesList(String url) {
190: InputStream in = null;
191: requiredUrl = url;
192: File f = new File(url);
193: try {
194: if (f.exists()) {
195: in = new FileInputStream(f);
196: } else {
197: in = new URL(url).openStream();
198: }
199: } catch (Exception ex) {
200: //ignore
201: }
202: EditorKit kit = new HTMLEditorKit();
203: HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
204: doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
205: try {
206: kit.read(in, doc, 0);
207: } catch (IOException ex) {
208: //ignore
209: } catch (BadLocationException ex) {
210: //ignore
211: }
212: int tableCount = 1;
213: int count = 1;
214: ElementIterator it = new ElementIterator(doc);
215: javax.swing.text.Element element = null;
216: while ((element = it.next()) != null) {
217: // read all table elements.
218: if ("table".equalsIgnoreCase(element.getName())) {
219: if (checkIfInnerMostTable(element)) {
220: tableDepth.put("Table #" + String.valueOf(count),
221: tableCount++);
222: elementMap.put("Table #" + String.valueOf(count++),
223: element);
224: } else {
225: tableCount++;
226: }
227: }
228: }
229: }
230:
231: private boolean checkIfInnerMostTable(
232: javax.swing.text.Element element) {
233: ElementIterator it = new ElementIterator(element);
234: javax.swing.text.Element elem = null;
235: it.next();
236: while ((elem = it.next()) != null) {
237: if ("table".equalsIgnoreCase(elem.getName())) {
238: return false;
239: }
240: }
241: return true;
242: }
243:
244: // Variables declaration - do not modify
245: private javax.swing.JEditorPane jEditorPane1;
246: private javax.swing.JLabel jLabel1;
247: private javax.swing.JScrollPane jScrollPane2;
248: private javax.swing.JButton preview;
249: // End of variables declaration
250: }
|