PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/netbeans-7.3/print/src/org/netbeans/modules/print/provider/EditorProvider.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 231 lines | 147 code | 30 blank | 54 comment | 37 complexity | 5d89095d9ea2f200fc1091617471c261 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.print.provider;
  45. import java.awt.Component;
  46. import java.awt.Container;
  47. import java.text.AttributedCharacterIterator;
  48. import java.util.ArrayList;
  49. import java.util.Date;
  50. import java.util.List;
  51. import javax.swing.JComponent;
  52. import javax.swing.JEditorPane;
  53. import java.awt.event.ActionEvent;
  54. import java.awt.event.ActionListener;
  55. import javax.swing.text.BadLocationException;
  56. import javax.swing.text.Document;
  57. import javax.swing.text.JTextComponent;
  58. import org.openide.cookies.EditorCookie;
  59. import org.openide.util.Lookup;
  60. import org.netbeans.modules.print.util.Config;
  61. import static org.netbeans.modules.print.util.UI.*;
  62. /**
  63. * @author Vladimir Yaroslavskiy
  64. * @version 2006.04.04
  65. */
  66. public final class EditorProvider extends ComponentProvider {
  67. public EditorProvider(EditorCookie editor, Date lastModified) {
  68. super(null, getName(editor), lastModified);
  69. myEditor = editor;
  70. }
  71. @Override
  72. protected JComponent getComponent() {
  73. JTextComponent text = getTextComponent();
  74. if (text == null) {
  75. return null;
  76. }
  77. if (Config.getDefault().isAsEditor()) {
  78. return getEditorComponent(text);
  79. }
  80. Document document = myEditor.getDocument();
  81. if (document == null) {
  82. return null;
  83. }
  84. int start;
  85. int end;
  86. if (Config.getDefault().isSelection()) {
  87. start = text.getSelectionStart();
  88. end = text.getSelectionEnd();
  89. }
  90. else {
  91. start = 0;
  92. end = document.getLength();
  93. }
  94. AttributedCharacterIterator[] iterators = getIterators(document, start, end);
  95. //out();
  96. //out("iterators: " + iterators);
  97. //out();
  98. if (iterators != null) {
  99. return new ComponentDocument(iterators);
  100. }
  101. try {
  102. return new ComponentDocument(text.getText(start, end - start));
  103. }
  104. catch (BadLocationException e) {
  105. return null;
  106. }
  107. }
  108. private AttributedCharacterIterator[] getIterators(Document document, int start, int end) {
  109. ActionListener action = (ActionListener) Lookup.getDefault().lookup(ActionListener.class);
  110. //out();
  111. //out("Action: " + action);
  112. //out();
  113. if (action == null) {
  114. return null;
  115. }
  116. if ( !action.getClass().getName().contains(".print.")) { // NOI18N
  117. return null;
  118. }
  119. List<Object> source = new ArrayList<Object>();
  120. source.add(document);
  121. source.add(Integer.valueOf(start));
  122. source.add(Integer.valueOf(end));
  123. ActionEvent event = new ActionEvent(source, 0, null);
  124. action.actionPerformed(event);
  125. Object object = event.getSource();
  126. if ( !(object instanceof List)) {
  127. return null;
  128. }
  129. List list = (List) object;
  130. if (list.size() != 2*2) {
  131. return null;
  132. }
  133. Object param = list.get(1 + 2);
  134. if ( !(param instanceof AttributedCharacterIterator[])) {
  135. return null;
  136. }
  137. return (AttributedCharacterIterator[]) param;
  138. }
  139. private static String getName(EditorCookie editor) {
  140. Document document = editor.getDocument();
  141. if (document == null) {
  142. return null;
  143. }
  144. String title = (String) document.getProperty(Document.TitleProperty);
  145. if (title == null) {
  146. return null;
  147. }
  148. return title.replace('\\', '/'); // NOI18N
  149. }
  150. private JTextComponent getTextComponent() {
  151. JEditorPane[] panes = myEditor.getOpenedPanes();
  152. if (panes == null || panes.length == 0) {
  153. return null;
  154. }
  155. return panes[0];
  156. }
  157. private JComponent getEditorComponent(JComponent text) {
  158. if ( !Config.getDefault().isLineNumbers()) {
  159. return text;
  160. }
  161. JComponent lineNumber = getLineNumberComponent(getParent(text));
  162. if (lineNumber == null) {
  163. return text;
  164. }
  165. List<JComponent> components = new ArrayList<JComponent>();
  166. components.add(lineNumber);
  167. components.add(text);
  168. return new ComponentPanel(components);
  169. }
  170. private JComponent getLineNumberComponent(Component component) {
  171. if (component == null) {
  172. return null;
  173. }
  174. //out(" see: " + component.getClass().getName());
  175. if (component.getClass().getName().equals("org.netbeans.editor.GlyphGutter")) { // NOI18N
  176. if (component instanceof JComponent) {
  177. return (JComponent) component;
  178. }
  179. }
  180. if ( !(component instanceof Container)) {
  181. return null;
  182. }
  183. Container container = (Container) component;
  184. Component[] children = container.getComponents();
  185. for (Component child : children) {
  186. JComponent lineNumberComponent = getLineNumberComponent(child);
  187. if (lineNumberComponent != null) {
  188. return lineNumberComponent;
  189. }
  190. }
  191. return null;
  192. }
  193. private Component getParent(Component component) {
  194. Component parent = component.getParent();
  195. if (parent == null) {
  196. return component;
  197. }
  198. return getParent(parent);
  199. }
  200. private EditorCookie myEditor;
  201. }