001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2007 Sun Microsystems, Inc.
038: */
039: package org.netbeans.modules.mashup.tables.wizard;
040:
041: import java.awt.Color;
042: import java.awt.Graphics;
043: import java.awt.Point;
044: import java.awt.Rectangle;
045: import java.awt.Robot;
046: import java.awt.Shape;
047: import java.awt.event.MouseEvent;
048: import java.io.Serializable;
049: import java.net.URL;
050: import java.util.Enumeration;
051: import java.util.HashMap;
052: import java.util.LinkedList;
053: import java.util.Map;
054: import java.util.SortedMap;
055: import java.util.TreeMap;
056: import javax.swing.JComboBox;
057: import javax.swing.JEditorPane;
058: import javax.swing.event.MouseInputAdapter;
059: import javax.swing.text.Document;
060: import javax.swing.text.ElementIterator;
061: import javax.swing.text.Highlighter;
062: import javax.swing.text.JTextComponent;
063: import javax.swing.text.StyleConstants;
064: import javax.swing.text.html.HTML;
065: import javax.swing.text.html.HTMLDocument;
066: import javax.swing.text.html.HTMLEditorKit;
067:
068: /**
069: *
070: * @author RamaChandraiah
071: */
072: class PatchedHTMLEditorKit extends HTMLEditorKit {
073:
074: private javax.swing.text.Element tableElement;
075: private int tableNumber;
076: private PatchedHTMLEditorKit ek;
077: private JEditorPane jep;
078: private ChooseTableVisualPanel chooseTableVisualPanel;
079: private SortedMap<String, Integer> tableDepth = new TreeMap<String, Integer>();
080: private Map<String, javax.swing.text.Element> elementMap = new HashMap<String, javax.swing.text.Element>();
081:
082: public PatchedHTMLEditorKit(JEditorPane jEditorPane1,
083: ChooseTableVisualPanel chooseTableVisualPanel) {
084: this .chooseTableVisualPanel = chooseTableVisualPanel;
085: jep = jEditorPane1;
086: }
087:
088: public void setTableNumber(int tableNumberIn) {
089: tableNumber = tableNumberIn;
090: }
091:
092: public int getTableNumber() {
093: calcTableNumber();
094: return tableNumber;
095: }
096:
097: public void setTableElement(javax.swing.text.Element element) {
098: tableElement = element;
099: }
100:
101: public javax.swing.text.Element getTableElement() {
102: return tableElement;
103: }
104:
105: public void highlightTable(JComboBox jcb) {
106: int tableToHighlight = Integer.parseInt(jcb.getSelectedItem()
107: .toString());
108:
109: setTableNumber(tableToHighlight);
110: ek.myController.highlightTable(tableToHighlight);
111: }
112:
113: public void highlightTable(int tableToHighlight) {
114: setTableNumber(tableToHighlight);
115: ek.myController.highlightTable(tableToHighlight);
116: }
117:
118: LinkController myController = new LinkController();
119: LinkedList ranges;
120:
121: void calcTableNumber() {
122: HTMLDocument hdoc = (HTMLDocument) jep.getDocument();
123:
124: ElementIterator it = new ElementIterator(hdoc);
125: javax.swing.text.Element element = null;
126: int count = 0;
127:
128: try {
129: while ((element = it.next()) != null) {
130: if ("table".equalsIgnoreCase(element.getName())) {
131: if (checkIfInnerMostTable(element)) {
132: tableDepth.put("Table #"
133: + String.valueOf(count), tableNumber++);
134: elementMap.put("Table #"
135: + String.valueOf(count++), element);
136: } else {
137: tableNumber++;
138: }
139: if ((element.getStartOffset() == tableElement
140: .getStartOffset())
141: && (element.getEndOffset() == tableElement
142: .getEndOffset())
143: && checkIfInnerMostTable(element)) {
144: break;
145: }
146: }
147: setTableNumber(tableNumber);
148: }
149: } catch (Exception e) {
150: System.out.println("No Table is Clicked");
151: }
152: }
153:
154: private boolean checkIfInnerMostTable(
155: javax.swing.text.Element element) {
156: ElementIterator it = new ElementIterator(element);
157: javax.swing.text.Element elem = null;
158: it.next();
159: while ((elem = it.next()) != null) {
160: if ("table".equalsIgnoreCase(elem.getName())) {
161: return false;
162: }
163: }
164: return true;
165: }
166:
167: class Node {
168:
169: int tableNo;
170: long minRange;
171: long maxRange;
172: }
173:
174: @Override
175: public void install(JEditorPane c) {
176: c.addMouseListener(myController);
177: c.addMouseMotionListener(myController);
178: }
179:
180: public class LinkController extends MouseInputAdapter implements
181: Serializable {
182:
183: URL currentUrl = null;
184: boolean firstTime = true;
185: Object tableWidth = null;
186:
187: @Override
188: public void mouseClicked(MouseEvent e) {
189: JEditorPane editor = (JEditorPane) e.getSource();
190: if (!editor.isEditable()) {
191: Point pt = new Point(e.getX(), e.getY());
192: int pos = editor.viewToModel(pt);
193: if (pos >= 0) {
194: activateLink(pos, editor);
195: }
196: try {
197: Robot r = new Robot();
198: r.keyPress(java.awt.event.KeyEvent.VK_DOWN);
199: r.keyPress(java.awt.event.KeyEvent.VK_DOWN);
200: r.keyPress(java.awt.event.KeyEvent.VK_DOWN);
201: } catch (Exception ex) {
202: ex.printStackTrace();
203: }
204: int tableNumber = getTableNumber();
205: System.out.println("Table:" + tableNumber);
206: chooseTableVisualPanel.setTableNum(tableNumber);
207: }
208: }
209:
210: protected void activateLink(int pos, JEditorPane html) {
211: Document doc = html.getDocument();
212:
213: if (doc instanceof HTMLDocument) {
214: HTMLDocument hdoc = (HTMLDocument) doc;
215: javax.swing.text.Element e = hdoc
216: .getCharacterElement(pos);
217:
218: int textPos = e.getEndOffset();
219:
220: javax.swing.text.Element tableElement = null;
221:
222: try {
223: while (true) {
224: Object name = e.getAttributes().getAttribute(
225: StyleConstants.NameAttribute);
226: if ((name instanceof HTML.Tag)
227: && (name == HTML.Tag.TABLE)) {
228: tableElement = e;
229: break;
230: } else if ((name instanceof HTML.Tag)
231: && (name == HTML.Tag.BODY)) {
232: break;
233: }
234: e = e.getParentElement();
235: }
236: } catch (Exception ex) {
237:
238: }
239:
240: setTableNumber(tableNumber);
241: highlightTable(tableElement);
242: setTableElement(tableElement);
243: }
244: }
245:
246: public void highlightTable(javax.swing.text.Element element) {
247: Highlighter h = jep.getHighlighter();
248: h.removeAllHighlights();
249: if (element != null) {
250: javax.swing.text.AttributeSet tableAttributes = element
251: .getAttributes();
252: Enumeration en = tableAttributes.getAttributeNames();
253:
254: tableWidth = null;
255:
256: while (en.hasMoreElements()) {
257: Object attribName = en.nextElement();
258: if (attribName.toString().toLowerCase().equals(
259: "width")) {
260: tableWidth = tableAttributes
261: .getAttribute(attribName);
262: }
263: }
264:
265: if (tableWidth == null) {
266: int maxWidth = 0;
267: Rectangle r;
268:
269: for (int i = element.getStartOffset(); i <= element
270: .getEndOffset(); i++) {
271: try {
272: r = jep.modelToView(i);
273: } catch (Exception ex) {
274: break;
275: }
276:
277: if (r.y > maxWidth) {
278: maxWidth = r.y;
279: }
280: }
281:
282: System.out.println("calculated width :: "
283: + maxWidth);
284:
285: tableWidth = Integer.toString(maxWidth);
286: }
287:
288: try {
289: h.addHighlight(element.getStartOffset(), element
290: .getEndOffset(), new TableHighlightPainter(
291: tableWidth));
292: } catch (Exception ex) {
293: System.err
294: .println("Error in Highlighting/No Table Clicked :: "
295: + ex);
296: }
297:
298: tableWidth = null;
299: }
300: }
301:
302: public void highlightTable(int tableNumber) {
303: HTMLDocument hdoc = (HTMLDocument) jep.getDocument();
304:
305: ElementIterator iterator = new ElementIterator(hdoc);
306: javax.swing.text.Element element = null;
307:
308: while (tableNumber > 0
309: && ((element = iterator.next()) != null)) {
310: Object name = element.getAttributes().getAttribute(
311: StyleConstants.NameAttribute);
312: if ((name instanceof HTML.Tag)
313: && (name == HTML.Tag.TABLE)) {
314: tableNumber--;
315: }
316: }
317:
318: if (element != null) {
319: highlightTable(element);
320: }
321: }
322:
323: class TableHighlightPainter implements
324: Highlighter.HighlightPainter {
325:
326: Object tableWidth;
327:
328: TableHighlightPainter(Object tableWidth) {
329: this .tableWidth = tableWidth;
330: }
331:
332: public void paint(Graphics g, int p0, int p1, Shape bounds,
333: JTextComponent c) {
334: Rectangle r1, r2;
335:
336: try {
337: r1 = c.modelToView(p0);
338: r2 = c.modelToView(p1);
339:
340: g = jep.getGraphics();
341:
342: int w = -1;
343: String width = tableWidth.toString();
344:
345: if (width.charAt(width.length() - 1) == '%') {
346: w = Integer.parseInt(width.substring(0, width
347: .length() - 1))
348: * (jep.getWidth()) / 100;
349:
350: g.setColor(new Color(255, 0, 0));
351:
352: g.drawRect(r1.x - 1, r1.y - 1, w - 22,
353: ((r1.y > r2.y) ? (r1.y - r2.y)
354: : (r2.y - r1.y)) - 8);
355: g.drawRect(r1.x, r1.y, w - 23,
356: ((r1.y > r2.y) ? (r1.y - r2.y)
357: : (r2.y - r1.y)) - 9);
358: g.drawRect(r1.x, r1.y, w - 24,
359: ((r1.y > r2.y) ? (r1.y - r2.y)
360: : (r2.y - r1.y)) - 10);
361: } else {
362: w = 0;
363:
364: for (int i = 0; i < width.length(); i++) {
365: Character ch = width.charAt(i);
366: if (Character.isDigit(ch)) {
367: w = w
368: * 10
369: + (int) Integer.parseInt(ch
370: .toString());
371: } else {
372: break;
373: }
374: }
375:
376: g.setColor(new Color(255, 0, 0));
377: g.drawRect(r1.x - 1, r1.y - 1, w - 12,
378: ((r1.y > r2.y) ? (r1.y - r2.y)
379: : (r2.y - r1.y)) - 8);
380: g.drawRect(r1.x, r1.y, w - 13,
381: ((r1.y > r2.y) ? (r1.y - r2.y)
382: : (r2.y - r1.y)) - 9);
383: g.drawRect(r1.x, r1.y, w - 14,
384: ((r1.y > r2.y) ? (r1.y - r2.y)
385: : (r2.y - r1.y)) - 10);
386: }
387: } catch (Exception e) {
388: //e.printStackTrace();
389: }
390: }
391: }
392: }
393: }
|